Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / PNG troubles - writing RGB behind alpha mask

Author
Message
GrumpyOne
18
Years of Service
User Offline
Joined: 27th Nov 2007
Location: London, UK
Posted: 20th Feb 2013 02:01
Dear All,

I'm having some issues with PNGs and alpha. Have you ever noticed that Photoshop often magically leaves RGB data behind the alpha mask. This can cause issues with the use of these PNGs in some game engines.

I've written some code as part of the image editor for Forester Pro (a tree generator that exports to X, Collada and FBX) that strips out the RGB data in masked areas in PNGs, however, I can only seem to get it to write rgb(0,0,0) in these areas. I would like to be able to write whatever background colour I would like. Here is the code:



You can see this goes through pixel by pixel and in masked areas (alpha=0) writes the colour as rgb(0,0,0). The strange thing is if I set the colour as something different, it still gets set to rgb(0,0,0). All I can think is is my function setAlpha is at fault. I found this on the forum (shown below) and I am not to familiar with bitwise operands.



Any suggestions are welcome.

p.s. Pretty picture of Forester Pro trees below. PNG editing would mean I can improve the quality of leaf textures even more.



GrumpyOne - the natural state of the programmer
Ortu
DBPro Master
18
Years of Service
User Offline
Joined: 21st Nov 2007
Location: Austin, TX
Posted: 20th Feb 2013 03:23 Edited at: 20th Feb 2013 03:25
photoshop handles png a bit oddly, you should have a look at superPNG, i replaced the default png plugin with it havent looked back. it eliminated a big edge bleeding issue i had been having

trees look great!

TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 20th Feb 2013 10:23 Edited at: 20th Feb 2013 10:25
I believe the problem is that bitmap operations don't support alpha channel, so in other words: Da codez! They do nothing!

I recommend using memblocks instead, that way you have full access to pixel data:



Alternately, instead of doing all of that you can also just try this code for the objects you apply the texture to:



TheComet

http://blankflankstudios.tumblr.com/
"You're all wrong. You're all idiots." - Fluffy Rabbit
"Bottom line, people are retarded." - Fluffy Rabbit
BatVink
Moderator
23
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 20th Feb 2013 13:01
GrumpyOne
18
Years of Service
User Offline
Joined: 27th Nov 2007
Location: London, UK
Posted: 21st Feb 2013 01:04
These suggestions work really well.

Yes you are right I did have "memblock fear"....plus a healthy amount of "bitwise operator trepidation". Now I have got by head around bitwise && and bitwise || and bitwise shift >> and << it all seems so easy.

Many thanks!

GrumpyOne - the natural state of the programmer
GrumpyOne
18
Years of Service
User Offline
Joined: 27th Nov 2007
Location: London, UK
Posted: 21st Feb 2013 16:19
Hmmm....on the other hand....it appeared to work...but doesn't. I think perhaps there is something fundamental I have wrong.

In the code below I want to read the alpha from an image in memblock 2 and write data into memblock 3 to create an image showing the alpha. Red pixels will be for alpha=0, blue for pixels where alpha is partly transparent, and transparent where the original image was opaque. Then I create an image from memblock 3... and hey presto...it is wrong. Where I expect red, I have blue, where I expect the original image to be completely opaque, I have some spurious red dots. It is weird....and suggests by bitwise comparisons are wrong, or I am reading the data wrong. Don't know..it almost works and I can't explain why not. Notice I count 0 to imgWidth-1.



GrumpyOne - the natural state of the programmer
TheComet
18
Years of Service
User Offline
Joined: 18th Oct 2007
Location: I`m under ur bridge eating ur goatz.
Posted: 21st Feb 2013 17:34
The code seems fine...

I'm not near a DBP compiler so please excuse me if this doesn't work. I remember having a problem with the order of execution, where DBP would first perform the "=" operation before "&&". Try this:



TheComet

http://blankflankstudios.tumblr.com/
"You're all wrong. You're all idiots." - Fluffy Rabbit
"Bottom line, people are retarded." - Fluffy Rabbit
GrumpyOne
18
Years of Service
User Offline
Joined: 27th Nov 2007
Location: London, UK
Posted: 21st Feb 2013 18:12
Thanks Comet,

That explains why the logic block wasn't working!

Grumpy

GrumpyOne - the natural state of the programmer

Login to post a reply

Server time is: 2026-07-07 10:32:45
Your offset time is: 2026-07-07 10:32:45