Example using transparent?

foehner
2013-02-01
2013-02-17
  • foehner
    foehner
    2013-02-01

    I've been using FreeImage for several years, but have never figured out how transparency works. In particular, rotate an image and have the new 'filler' areas be a transparent color. This would allow me to copy/paste the rotated image onto another canvas.

    I am going to write some code where this would be very handy to have, but remember banging my head the last time I tried it. Is there sample code using this concept?

    Thanks - John

     
  • Hervé Drolon
    Hervé Drolon
    2013-02-04

    Hi,

    You should look at the PDF doc under "FreeImage_Rotate".
    There's a sample named testRotateWithBackground:
    using a 32-bit dib, you can give a RGBQUAD color with a transparency value.
    As for 8-bit images, once your image is rotated, you can provide a transparency index corresponding to the transparent color used, using FreeImage_SetTransparentIndex.

    Hervé

     
    • foehner
      foehner
      2013-02-17

      Herve -

      I finally got a block of time to work this, but still can't get it to work. I've tried lots of variations, but the closest I've come is that once I ended up with a checkerboard filler on my rotated image. But the checkerboard was copy/pasted onto the other image where I wanted it to be transparent. (the sample code below will have a black fill color)

      I'm going to include the code which I 'hoped' would work. The two questions I have are...

      1) How do I specify a transparent color in the RGBQUAD?
      2) What is a good value to use for the alpha argument on the FreeImage_Paste???
      Below 255 blends the images fine, but what makes it use a transparent color?

      I am working with VB6 on Windows 7 and just started using FreeImage 3154. The two images I'm working with are JPGs.

      My code --

      Private Sub mnuImport_Click()
      Dim back_tmp As Long
      Dim iback As Long
      Dim irotate As Long
      Dim ismall As Long
      Dim icopy As Long
      Dim small_tmp As Long
      Dim back_jpg As Long
      Dim bOk
      Dim f
      Dim h As Integer
      Dim w As Integer
      Dim image_type As FREE_IMAGE_TYPE
      Dim pixel_size As Integer
      Dim bg_color As RGBQUAD
      Dim color_type As Integer

      ''''' Open the two jpg files and convert them to 32 bits
      back_tmp = FreeImage_Load(FIF_JPEG, "C:\local\background.jpg")
      iback = FreeImage_ConvertTo32Bits(back_tmp)
      FreeImage_Unload (back_tmp)

      small_tmp = FreeImage_Load(FIF_JPEG, "C:\local\rotate.jpg")
      ismall = FreeImage_ConvertTo32Bits(small_tmp)
      FreeImage_Unload (small_tmp)

      ''''' Make sure we have the right color type and the right pixel size
      image_type = FreeImage_GetImageType(ismall)
      color_type = FreeImage_GetColorType(ismall)
      If Not color_type = FIC_RGB Then
      MsgBox ("Wrong color type")
      End If
      If image_type = FIT_BITMAP Then
      pixel_size = FreeImage_GetBPP(ismall)
      If Not pixel_size = 24 And Not pixel_size = 32 Then
      MsgBox ("Wrong pixel size")
      End If
      Else
      MsgBox ("Wrong image type")
      End If

      ''''' Build the fill color for the rotated image
      bg_color.rgbBlue = 0
      bg_color.rgbGreen = 0
      bg_color.rgbRed = 0
      bg_color.rgbReserved = 255
      FreeImage_SetTransparent ismall, True

      ''''' Rotate the image
      irotate = FreeImage_Rotate(ismall, 30, bg_color)
      FreeImage_SetTransparent irotate, True
      FreeImage_Unload (ismall)

      ''''' Paste the rotated image onto the background image
      h = FreeImage_GetHeight(irotate)
      w = FreeImage_GetWidth(irotate)

      icopy = FreeImage_Copy(irotate, 0, 0, w, h)
      f = FreeImage_Paste(iback, icopy, 300, 300, 300)
      FreeImage_Unload (irotate)
      FreeImage_Unload (icopy)

      ''''' Save the 32 bit image as png
      bOk = FreeImage_Save(FIF_PNG, iback, "C:\local\background_new.png", PNG_DEFAULT)

      ''''' Convert the image back to 24 bits and save it as jpg
      back_jpg = FreeImage_ConvertTo24Bits(iback)
      bOk = FreeImage_Save(FIF_JPEG, back_jpg, "C:\local\background_new.jpg", JPEG_QUALITYSUPERB)

      FreeImage_Unload (iback)
      FreeImage_Unload (back_jpg)
      End Sub


      The code saves my resulting image as PNG and JPG, but I hope to only need JPG if possible. Both saved images end up with the rotated fill color showing up as black. The desired result is to be able to see the background image through that fill color.

      Thanks for any more guidance - John

       
  • foehner
    foehner
    2013-02-04

    Great Herve! its been a while since I've grabbed a new version of FreeImage. My 3110 didn't have the stuff you referenced, so I just downloaded 3154. Looks to be something I can do now. I'll holler back after I take a whack at it. Thanks!