Results 1 to 7 of 7

Thread: Tutorial - Anti-Alias Pixels

Hybrid View

  1. #1

    Thread Starter
    Frenzied Member cyborg's Avatar
    Join Date
    May 2000
    Location
    Sweden
    Posts
    1,755
    Calculations:


    Now it's time to create the function that will do the actual AA calculation.
    We call the upper left pixel's coordinates cX and cY (c stands for Close)

    VB Code:
    1. Private Sub DrawAntiAliasPixel(PicBox As PictureBox, ByVal X As Double, ByVal Y As Double, ByVal R As Integer, ByVal G As Integer, ByVal B As Integer)
    2.     Dim cX As Integer
    3.     Dim cY As Integer
    4.  
    5.     cX = RoundClose(X)
    6.     cY = RoundClose(Y)
    7. End Sub

    The function takes 6 parameters which are X and Y for position and R, G and B for color values. PicBox is the PictureBox we want to draw on.


    Now let's find out how to calculate the new colors of the surrounding pixels.
    To do that we need to calculate how many percent of our pixel that is touching each of the surrounding
    pixels.
    For this we need to know how far our pixel are from each of the other pixels.
    We calculate this by subtracting cX from X and cY from Y, then we get delta X and delta Y (dX,dY).
    But this will only tell us the distance from one side. The dX and dY from the other side (dX2,dY2) is 1 - dX and 1 - dY.



    For the upper left corner on our pixel we do dX2 * dY2, and that will give us how many percent we touch the
    upper left pixel.

    For the other pixels do:
    Upright Pixel = dX * dY2
    Downleft Pixel = dX2 * dY
    Downright Pixel = dX * dY

    If this is confusing take a close look at the image attached (dX2 * dY2 will give us the area of the corner
    and also the percent because 100% of the area of a square with 1 lenght units on all sides is 1)

    By multiplying these value with the color values, we will get how much of our pixel's color we need for each pixel.
    We also need to know how much of the original color from the pixels we need, and that is the inverted value:
    Upleft Pixel = 1 - dX2 * dY2
    Upright Pixel = 1 - dX * dY2
    Downleft Pixel = 1 - dX2 * dY
    Downright Pixel = 1 - dX * dY

    Now we need to extract the original colors from the background to mix with the new color. We do this by using the
    Point function. But we also need to extract the red, green and blue values from that color.
    To do this, we need some new functions:

    VB Code:
    1. Private Function GetRed(ByVal Color As Long) As Byte
    2.     GetRed = Color And 255
    3. End Function
    4.  
    5. Private Function GetGreen(ByVal Color As Long) As Byte
    6.     GetGreen = (Color And 65280) \ 256
    7. End Function
    8.  
    9. Private Function GetBlue(ByVal Color As Long) As Byte
    10.     GetBlue = (Color And 16711680) \ 65535
    11. End Function

    Now all that's left is to create variables to hold all different values and put it all down into the function
    and draw the pixels using PSet.

    Here's the finished function:

    VB Code:
    1. Private Sub DrawAntiAliasPixel(PicBox As PictureBox, ByVal X As Double, ByVal Y As Double, ByVal R As Integer, ByVal G As Integer, ByVal B As Integer)
    2.     Dim cX As Integer
    3.     Dim cY As Integer
    4.     Dim dX As Double
    5.     Dim dY As Double
    6.     Dim dX2 As Double
    7.     Dim dY2 As Double
    8.     Dim BGColor As Long
    9.     Dim BGRed As Integer
    10.     Dim BGGreen As Integer
    11.     Dim BGBlue As Integer
    12.     Dim NewRed As Integer
    13.     Dim NewGreen As Integer
    14.     Dim NewBlue As Integer
    15.     Dim Percent As Double
    16.     Dim InvPercent As Double
    17.    
    18.     cX = RoundClose(X)
    19.     cY = RoundClose(Y)
    20.  
    21.     dX = X - cX
    22.     dY = Y - cY
    23.     dX2 = 1 - dX
    24.     dY2 = 1 - dY
    25.    
    26.     'upleft pixel
    27.     BGColor = PicBox.Point(cX, cY)
    28.     BGRed = GetRed(BGColor)
    29.     BGGreen = GetGreen(BGColor)
    30.     BGBlue = GetBlue(BGColor)
    31.    
    32.     Percent = dX2 * dY2
    33.     InvPercent = 1 - Percent
    34.    
    35.     NewRed = Percent * R + InvPercent * BGRed
    36.     NewGreen = Percent * G + InvPercent * BGGreen
    37.     NewBlue = Percent * B + InvPercent * BGBlue
    38.    
    39.     PicBox.PSet (cX, cY), RGB(NewRed, NewGreen, NewBlue)
    40.    
    41.     'upright pixel
    42.     BGColor = PicBox.Point(cX + 1, cY)
    43.     BGRed = GetRed(BGColor)
    44.     BGGreen = GetGreen(BGColor)
    45.     BGBlue = GetBlue(BGColor)
    46.    
    47.     Percent = dX * dY2
    48.     InvPercent = 1 - Percent
    49.    
    50.     NewRed = Percent * R + InvPercent * BGRed
    51.     NewGreen = Percent * G + InvPercent * BGGreen
    52.     NewBlue = Percent * B + InvPercent * BGBlue
    53.    
    54.     PicBox.PSet (cX + 1, cY), RGB(NewRed, NewGreen, NewBlue)
    55.    
    56.     'downleft pixel
    57.     BGColor = PicBox.Point(cX, cY + 1)
    58.     BGRed = GetRed(BGColor)
    59.     BGGreen = GetGreen(BGColor)
    60.     BGBlue = GetBlue(BGColor)
    61.    
    62.     Percent = dX2 * dY
    63.     InvPercent = 1 - Percent
    64.    
    65.     NewRed = Percent * R + InvPercent * BGRed
    66.     NewGreen = Percent * G + InvPercent * BGGreen
    67.     NewBlue = Percent * B + InvPercent * BGBlue
    68.    
    69.     PicBox.PSet (cX, cY + 1), RGB(NewRed, NewGreen, NewBlue)
    70.    
    71.     'downright pixel
    72.     BGColor = PicBox.Point(cX + 1, cY + 1)
    73.     BGRed = GetRed(BGColor)
    74.     BGGreen = GetGreen(BGColor)
    75.     BGBlue = GetBlue(BGColor)
    76.    
    77.     Percent = dX * dY
    78.     InvPercent = 1 - Percent
    79.    
    80.     NewRed = Percent * R + InvPercent * BGRed
    81.     NewGreen = Percent * G + InvPercent * BGGreen
    82.     NewBlue = Percent * B + InvPercent * BGBlue
    83.    
    84.     PicBox.PSet (cX + 1, cY + 1), RGB(NewRed, NewGreen, NewBlue)
    85. End Sub
    Attached Images Attached Images  
    Check out the FAQ and do a search before you post.
    My tutorials: Anti-Alias Pixels, Accurate Game Loop, Resource File

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width