Page 1 of 1

24bit to 32bit PNG

Posted: Mon Feb 11, 2008 7:25 am
by J. Baker
I have images that are 24bit with a black background. I was wondering how I would go about making the black background the alpha channel?

Posted: Tue Feb 12, 2008 9:27 am
by dige
Just an idea: CreateImage() with 32Bit and draw the 24Bit Image
with DrawImage() into it?

Posted: Wed Feb 13, 2008 5:54 am
by J. Baker
dige wrote:Just an idea: CreateImage() with 32Bit and draw the 24Bit Image
with DrawImage() into it?
Thanks dige, I look into it. ;)

Posted: Wed Feb 13, 2008 6:07 am
by netmaestro
Do you need to save the images back to disk?

Posted: Wed Feb 13, 2008 6:49 am
by J. Baker
netmaestro wrote:Do you need to save the images back to disk?
Nope. 8)

Posted: Wed Feb 13, 2008 7:28 am
by netmaestro
The job is simple then, I'll post code for you tomorrow if you don't have it solved by then. Sorry I can do nothing tonight, my laneway is 250 feet long and there's three feet of snow in it.

Posted: Wed Feb 13, 2008 4:46 pm
by blueb
:lol:

Netmaestro... ya' gotta move to the west coast, and get into golfing!

--blueb

Posted: Wed Feb 13, 2008 5:06 pm
by J. Baker
netmaestro wrote:The job is simple then, I'll post code for you tomorrow if you don't have it solved by then. Sorry I can do nothing tonight, my laneway is 250 feet long and there's three feet of snow in it.
No problem, thanks!

Posted: Thu Feb 14, 2008 3:55 am
by netmaestro

Code: Select all

; Start with a 24bit bmp image with a black background

CreateImage(0, 128,128,24)
StartDrawing(ImageOutput(0))
  Circle(64,64,64,#Red)
StopDrawing()

; Create a new 32bit image to serve as the result and draw first image to it

CreateImage(1, 128,128,32)
StartDrawing(ImageOutput(1))
  DrawImage(ImageID(0),0,0)
StopDrawing()

; Verify that the 32bit image is created correctly

OpenWindow(0,0,0,128,128, "Result", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
StartDrawing(WindowOutput(0))
  DrawImage(ImageID(1),0,0)
StopDrawing()
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow

; Show that the 32bit image's alphachannel is unused as yet - DrawAlphaImage can't show it

OpenWindow(0,0,0,128,128, "Result", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
StartDrawing(WindowOutput(0))
  DrawAlphaImage(ImageID(1),0,0)
StopDrawing()
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow

; Set the alphachannel data in the image to show full intensity for all colors 
; except black, and no intensity for any black pixels

GetObject_(ImageID(1), SizeOf(BITMAP), bmp.BITMAP) 
*bits = bmp\bmBits 
For i=0 To bmp\bmHeight-1 
  For j=0 To bmp\bmWidthBytes-1 Step 4 
    *px.RGBQUAD = *bits + bmp\bmWidthBytes * i + j 
    With *px
      If \rgbBlue=0 And \rgbGreen=0 And \rgbRed=0
        *px\rgbReserved = 0
      Else
        *px\rgbReserved = $FF
      EndIf 
    EndWith
  Next 
Next

; Now repeat the second test and see that the alphachannel is working correctly 
; with DrawAlphaImage()

OpenWindow(0,0,0,128,128, "Result", #PB_Window_ScreenCentered|#PB_Window_SystemMenu)
StartDrawing(WindowOutput(0))
  DrawAlphaImage(ImageID(1),0,0)
StopDrawing()
Repeat:Until WaitWindowEvent()=#PB_Event_CloseWindow

; Now try a test with sprites and see if it's working with #PB_Sprite_Alphablending

InitSprite():InitSprite3D()
OpenWindow(0,0,0,640,480,"Sprite Test",#PB_Window_ScreenCentered|#PB_Window_SystemMenu)
OpenWindowedScreen(WindowID(0),0,0,640,480,0,0,0)
CreateSprite(0,128,128,#PB_Sprite_Texture|#PB_Sprite_AlphaBlending)
StartDrawing(SpriteOutput(0))
  DrawAlphaImage(ImageID(1),0,0)
StopDrawing()
CreateSprite3D(0,0)

Repeat
  ev = WindowEvent()
  ClearScreen(GetSysColor_(#COLOR_BTNFACE))
  Start3D()
    DisplaySprite3D(0, 200,200)
  Stop3D()
  FlipBuffers()
Until ev = #PB_Event_CloseWindow

; Duh-duh duh-duh duh-duh - That's all, folks!
You can also use this code to vary the alpha intensity of the colored portion of the image, just change the $FF I'm setting it at to some lesser value, then you can have a nice faded image if you want.

Posted: Thu Feb 14, 2008 4:20 am
by J. Baker
Thanks for your efforts netmaestro but when you load an image it basically just erases the background up to the point where the actual image starts. The blue bubbles are suppose to be transparent. Here's the image I used...

Download BMP Image

Posted: Thu Feb 14, 2008 4:30 am
by J. Baker
I'm looking for something similar to Gimp's "Color to Alpha", that would make the above bmp look like this...

Image

Posted: Thu Feb 14, 2008 4:40 am
by netmaestro
That result would require a more complex algorithm, which I'm not exactly sure how to write. But it is an interesting little challenge, and I'm going to see what I can come up with in the coming days if no other solutions get posted in the meantime.

Posted: Thu Feb 14, 2008 5:03 am
by J. Baker
netmaestro wrote:That result would require a more complex algorithm, which I'm not exactly sure how to write. But it is an interesting little challenge, and I'm going to see what I can come up with in the coming days if no other solutions get posted in the meantime.
Thanks netmaestro! I'm still thinking as well.

I was just wondering if each pixel is scanned for there color value and if anything fits the range of RGB(0,0,0) that pixel was erased. But if that RGB(0,0,0) pixel had a slight color value, (lets say Red(Color) = 20), then RGB(255,0,0) would be drawn to the alpha channel with a transparency value of 20.

So it might be better to create a bmp with the background color of RGB(255,0,255) instead of black. Just incase the images has black outlines.

Posted: Thu Feb 14, 2008 9:21 pm
by adamredwoods
Side question:

Is there a way to do the above code line

Code: Select all

GetObject_(ImageID(1), SizeOf(BITMAP), bmp.BITMAP)
On the Mac in OSX?

Many Thanks.