Maintaining alpha in an image

Blitz3D Forums/Blitz3D Beginners Area/Maintaining alpha in an image

(tu) ENAY(Posted 2007) [#1]
In Blitz3D if you try to load a .png saved with an alpha channel, loading it into Blitz3D seems to be ok. The transparent pixels come out Transparent.

But if I try to load the same image in using LoadImage and then try to use it with Blitz2D commands, all the pixels are white and there seems to be no way of using MaskImage to get around it. Presumably because the alpha pixels have no colour and they can't be maskimage'd, I'm guessing all the pixels are going white because of that.

I'm stuck now trying to figure it out. Anyway to get around this?

Thanks in advance :)


GfK(Posted 2007) [#2]
You can't use alpha for 2D images in Blitz3D.

The only way around it would be to set the maskcolour of your image to white (MaskImage) - AFAIK this should work but its been a few years since I messed with Blitz3D, or the preferable method would be to re-export your images with a constant mask colour that you don't use for anything else, such as 255,0,255.


(tu) ENAY(Posted 2007) [#3]
Hiya Dave,

Long time no see.
Yeah resaving the images with alpha as 0,0,0 or 255,0,255 would be the ideal fix. But that's not an option, I have like loads of images and they need to stay in the same format so they can be loaded elsewhere and have all their alpha stuff intact, otherwise then it's broken elsewhere. Also if I remask mask all the images I've suddenly doubled the amount of images I have.

What I'm hoping to do is have some sort of function which loads the alpha'd image and then converts it to a masked pixel. If there is just a way of somehow loading the image in Blitz2D, doing some wibbling, even if it's read and write pixel plotting and then

eg

Function WibbleLoadImage(filename$)
local plop = LoadImage(filename$)

; Pixel Wibble bit here

Return plop
End Function


..that would me mega. I don't think it can be done though.


GfK(Posted 2007) [#4]
Just installed BB3D to check this out. I think you might be doing something wrong, as this works perfectly here:

Graphics 800,600

img = LoadImage("image.png")
MaskImage img,255,255,255
SetBuffer BackBuffer()

While Not KeyDown(1)
	Cls
	DrawImage(img,50,50)
	Flip
Wend


Image.png:



big10p(Posted 2007) [#5]
I don't get what the problem is, TBH. The alpha is in the top 8 bits which just gets chopped of with LoadImage, but the pixel RGB should still be there.

Are you saying ALL the pixels in the image look white?


Pongo(Posted 2007) [#6]
perhaps something here would help?
http://www.blitzbasic.com/codearcs/codearcs.php?code=2127

I wrote that so you need a separate alpha image, but you may be able to modify some things to get what you need. If loadimage does not work, then perhaps there is a way to use loadtexture to grab the alpha data. From there it should be a really fast change to the code. I will try to look into it, but I can't get to it for a bit.

edit: look here too for some more info.
http://www.blitzbasic.com/Community/posts.php?topic=63776#711890


(tu) ENAY(Posted 2007) [#7]
Hi everyone, thanks for the assistance. I tried your code Pongo, great work. But it's still the same.

While Dave's png works there is more than one typical png style of encoding. Like there are different methods of encoding gif,jpeg,bmp etc.

Here's one of my images, have a try and loading it for yourself and you'll see what I mean.

http://enaysoft.co.uk/test_png_image.zip

This image (and all the others) work fine in everything; Blitz3D/Blitzmax, paint programs etc just not in Blitz2D. The effect in this one is what I can only describe as some sort of line colour bleeding from the outer most pixels. Other images either do this or simply go white (which happens when images are near white or have pure white pixels which are surrounded by transparent pixels)
Of course when you load it in say Blitz3D/Blitzmax etc there are no problems with the alpha.

I've no idea what could be causing that.

If anyone can figure what is (or I'm doing) wrong I'd greatly appreciate it. :)


big10p(Posted 2007) [#8]
There seems to be something odd about the format that image is saved in. It displays ok in IE but background is all manner of shades of grey in Paint Shop Pro, and of course, Blitz.

What app was used to create them?

Can you try converting them to a more widely recognized png format?

I don't usually have any trouble with PNGs and blitz.


Pongo(Posted 2007) [#9]
I figured that was the problem you were having. That's pretty normal for .png images with alpha.

I know that code I posted above won't work directly. (it will if you extract the matte first and use it seperately) but I think there may be a way using loadtexture.

I'm looking at it now to see if I can figure it out.


Pongo(Posted 2007) [#10]
Ok, here you go. This works with your supplied image. You will also need to supply image 2, which will be your clean background color. You could also do this in code for a simple solid color.

does this help? It uses 3d because of loadtexture, but that allows me to read the alpha. I'm not sure about writing the alpha back out, but you could write the RGB from here with a clean background.