Results 1 to 5 of 5

Thread: Recoloring Images from other images and stuff like that...

  1. #1
    PirateBill
    Guest

    Question Recoloring Images from other images and stuff like that...

    Hi everyone. This is ma first post so bear with me...
    Okay well what I am wanting to do is recolor an image which is in black and white to another color without losing the luminosity or brightness.
    The black and white image uses exactly 10 shades of gray.
    I have a BMP file which is 20 pixels in height, and 10 pixels in width. Every row has a color gradient from light to dark with 10 shades in it.
    I want to get a gradient from that BMP file and apply it to the other black and white picture. Can this be done?

  2. #2
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Sure, you just use a regular Recolor algorythm to convert each one of the 10 shades. But before I post it here... are you trying to do it with simple VB, the windows API or DirectX?
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

  3. #3
    Addicted Member
    Join Date
    Aug 2000
    Location
    Adelaide - Australia
    Posts
    150
    I would like to see the straight VB code, or the API method of doing this as I am having some trouble with my method.

    Thanks

  4. #4
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    I think this is what you are looking for, the following code will draw a gradient using the colors you pass to the LineGradient function.

    VB Code:
    1. Private Declare Function Polyline Lib "gdi32" (ByVal hdc As Long, lpPoint As POINTAPI, ByVal nCount As Long) As Long
    2. Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    3. Private Declare Function CreatePen Lib "gdi32" (ByVal nPenStyle As Long, ByVal nWidth As Long, ByVal crColor As Long) As Long
    4. Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
    5. Private Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
    6. Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    7. Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    8. Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
    9. Private Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
    10.  
    11. Private Type POINTAPI
    12.         x As Long
    13.         y As Long
    14. End Type
    15.  
    16. Private Function LineGradient(ByVal pDC As Long, ByVal pWidth As Long, pHeight As Long, ByVal pStartColor As Long, ByVal pEndColor As Long, Optional pVertical As Boolean = False) As Boolean
    17.  Dim lDC As Long ' handle to temporary dc to draw gradient
    18.  Dim lBMP As Long, lOrigBMP As Long
    19.  Dim lPen As Long, lOrigPen As Long
    20.  Dim bytCS(3) As Byte, bytCE(3) As Byte
    21.  Dim nRChg As Double, nGChg As Double, nBChg As Double
    22.  Dim ptCurLine(0 To 1) As POINTAPI
    23.  Dim ict As Long
    24.  Dim lEnd As Long
    25.  
    26.  ' create the temporary DC
    27.  lDC = CreateCompatibleDC(pDC)
    28.  
    29.  If Not lDC = 0 Then
    30.    
    31.     ' create the bitmap to draw to
    32.     lBMP = CreateCompatibleBitmap(pDC, pWidth, pHeight)
    33.    
    34.     If Not lBMP = 0 Then
    35.    
    36.         lOrigBMP = SelectObject(lDC, lBMP)
    37.  
    38.         'retrieve the red, green, and blue bytes from the Color
    39.         CopyMemory bytCS(0), pStartColor, 3
    40.         CopyMemory bytCE(0), pEndColor, 3
    41.        
    42.         ' determine direction to draw
    43.         If pVertical = True Then lEnd = pHeight Else lEnd = pWidth
    44.        
    45.         ' retrieve the values to change each RGB value through each iteration of the drawing loop
    46.         nRChg = (CInt(bytCE(0)) - CInt(bytCS(0))) / lEnd
    47.         nGChg = (CInt(bytCE(1)) - CInt(bytCS(1))) / lEnd
    48.         nBChg = (CInt(bytCE(2)) - CInt(bytCS(2))) / lEnd
    49.        
    50.         lPen = CreatePen(0, 1, pStartColor)
    51.        
    52.         lOrigPen = SelectObject(lDC, lPen)
    53.        
    54.         For ict = 0 To lEnd
    55.    
    56.             If pVertical = True Then
    57.                
    58.                 ' describe current vertical line
    59.                 ptCurLine(0).x = 0: ptCurLine(0).y = ict
    60.                 ptCurLine(1).x = pWidth: ptCurLine(1).y = ict
    61.        
    62.             Else
    63.                
    64.                ' describe current horizontal line
    65.                 ptCurLine(0).x = ict: ptCurLine(0).y = 0
    66.                 ptCurLine(1).x = ict: ptCurLine(1).y = pHeight
    67.    
    68.             End If
    69.            
    70.             ' draw line to the temporary DC
    71.             Polyline lDC, ptCurLine(0), 2
    72.            
    73.             ' change the pen to the next color in the gradient
    74.             lPen = CreatePen(0, 1, RGB(bytCS(0) + (nRChg * ict), bytCS(1) + (nGChg * ict), bytCS(2) + (nBChg * ict)))
    75.            
    76.            ' remove the previous pen from memory
    77.             DeleteObject SelectObject(lDC, lPen)
    78.            
    79.         Next ict
    80.        
    81.         ' display the gradient to the DC expected
    82.         If BitBlt(pDC, 0, 0, pWidth, pHeight, lDC, 0, 0, vbSrcCopy) Then
    83.            
    84.             LineGradient = True
    85.            
    86.         Else
    87.            
    88.             LineGradient = False
    89.            
    90.         End If
    91.        
    92.         ' free memory
    93.         DeleteObject SelectObject(lDC, lOrigPen)
    94.         DeleteObject SelectObject(lDC, lOrigBMP)
    95.         DeleteDC lDC
    96.        
    97.     Else
    98.        
    99.         LineGradient = False
    100.        
    101.     End If
    102.    
    103.  Else
    104.  
    105.     LineGradient = False
    106.    
    107.  End If
    108.            
    109.    
    110. End Function
    111.  
    112. Private Sub Form_Load()
    113.     Me.AutoRedraw = False
    114. End Sub
    115.  
    116. Private Sub Form_Paint()
    117.     LineGradient Me.hdc, Me.Width \ Screen.TwipsPerPixelX, Me.Height \ Screen.TwipsPerPixelY, RGB(255, 0, 255), RGB(255, 255, 0), True
    118. End Sub
    119.  
    120. Private Sub Form_Resize()
    121.     Form_Paint
    122. End Sub

    Hope that is what you are looking for.
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  5. #5
    Frenzied Member Jotaf98's Avatar
    Join Date
    Jun 2000
    Location
    I'm not gonna give you my IP address! Ok... Portugal, South-Western Europe, 3rd rock from the sun (our star is easy to find, a 47 Ursae Majoris in the Milky Way :p )
    Posts
    1,457
    Hum yeah, that's a way to do it... huh isn't this thread a bit old? He never replied back so I completely forgot about it...
    Code:
    Temp = Me.GetIQ()
    'Error 9: Overflow
    'DON'T PANIC! :eek:

    To learn how to use realistic effects in your games like fire, rain, snow and magic effects, read my article on particles systems here.


    Jotaf's Theories!
    "Cats land on their feet. Toast lands peanut butter side down. A cat with toast strapped to its back will hover above the ground in a state of quantum indecision."

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