24bit to 32bit PNG

Just starting out? Need help? Post your questions and find answers here.
User avatar
J. Baker
Addict
Addict
Posts: 2192
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

24bit to 32bit PNG

Post 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?
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
dige
Addict
Addict
Posts: 1417
Joined: Wed Apr 30, 2003 8:15 am
Location: Germany
Contact:

Post by dige »

Just an idea: CreateImage() with 32Bit and draw the 24Bit Image
with DrawImage() into it?
User avatar
J. Baker
Addict
Addict
Posts: 2192
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Post 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. ;)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

Do you need to save the images back to disk?
BERESHEIT
User avatar
J. Baker
Addict
Addict
Posts: 2192
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Post by J. Baker »

netmaestro wrote:Do you need to save the images back to disk?
Nope. 8)
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
User avatar
blueb
Addict
Addict
Posts: 1118
Joined: Sat Apr 26, 2003 2:15 pm
Location: Cuernavaca, Mexico

Post by blueb »

:lol:

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

--blueb
- It was too lonely at the top.

System : PB 6.21(x64) and Win 11 Pro (x64)
Hardware: AMD Ryzen 9 5900X w/64 gigs Ram, AMD RX 6950 XT Graphics w/16gigs Mem
User avatar
J. Baker
Addict
Addict
Posts: 2192
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Post 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!
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
User avatar
J. Baker
Addict
Addict
Posts: 2192
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Post 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
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
J. Baker
Addict
Addict
Posts: 2192
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Post 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
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
User avatar
J. Baker
Addict
Addict
Posts: 2192
Joined: Sun Apr 27, 2003 8:12 am
Location: USA
Contact:

Post 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.
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef


Even the vine knows it surroundings but the man with eyes does not.
adamredwoods
New User
New User
Posts: 9
Joined: Wed Feb 13, 2008 12:58 am
Location: California

Post 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.
Post Reply