24bit to 32bit PNG
Posted: Mon Feb 11, 2008 7:25 am
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?
http://www.purebasic.com
https://www.purebasic.fr/english/
Thanks dige, I look into it.dige wrote:Just an idea: CreateImage() with 32Bit and draw the 24Bit Image
with DrawImage() into it?
Nope.netmaestro wrote:Do you need to save the images back to disk?
No problem, thanks!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.
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!

Thanks netmaestro! I'm still thinking as well.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.
Code: Select all
GetObject_(ImageID(1), SizeOf(BITMAP), bmp.BITMAP)