Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » savepng and transparency

Credits go to Peter Wang for helping out!
This thread is locked; no one can reply to it. rss feed Print
savepng and transparency
Johan Halmén
Member #1,550
September 2001

I want to create a png file with alpha transparency. I've downloaded loadpng and it loads png files ok, but when I try to save a BITMAP, I get a rgba image with nothing in the channels. How should I create the BITMAP in my Allegro app and how should I use the save functions in loadpng to get what I want.

My application is an update of my Eggifyer, which reads an image and mapps it on an egg. Now I want a transparent surrounding for the egg and a semi-transparent shadow that fades to the edges. All that looks fine in the application on my screen, but I want it saved so that I could place the egg on a web site.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Peter Wang
Member #23
April 2000

Have you made sure the image is 32-bit? I don't remember whether it works but it looks like I put in code to specifically deal with 32-bit BITMAPs.

Johan Halmén
Member #1,550
September 2001

Well I think it is 32 bit. I do set_color_depth(32), then I do set_gfx_mode(). After that each create_bitmap() should create a 32 bit bitmap, right? The saved png has four channels but the alpha is black. I managed to separate the channels, revealing the image in the rgb channels. I guess my problem is drawing the image in the first place. I've been RTFM, but I haven't figured out when the drawing functions actually change the alpha value for a pixel in the bitmap.

I draw the image pixel by pixel. For the background I imagine I could do:

a = 0;
col = makeacol32(r, g, b, a);
putpixel(bmp, x, y, col);

and for the object I have methods for calculating r, g, b and a. a is 255 except for the edge, where I interpolate to get rid of crispiness, kind of anti-aliasing.

But the result looks like if a would calculate to 0 for the whole image.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Peter Wang
Member #23
April 2000

Allegro's alpha support is pretty... idiosyncratic. I managed to get the following program to work though. The trick was to set_color_depth(32); Otherwise you might look into set_write_alpha_blender().

1#include <allegro.h>
2#include <loadpng.h>
3 
4int main(void)
5{
6 BITMAP *bmp;
7 int x, y;
8 allegro_init();
9 set_color_depth(32);
10 set_color_conversion(COLORCONV_NONE);
11 bmp = create_bitmap_ex(32, 255, 255);
12 for (y = 0; y < 256; y++) {
13 for (x = 0; x < 256; x++) {
14 int r = x;
15 int g = y;
16 int b = x;
17 int a = y;
18 int c = makeacol(r, g, b, a);
19 putpixel(bmp, x, y, c);
20 }
21 }
22 save_png("test.png", bmp, NULL);
23 destroy_bitmap(bmp);
24 return 0;
25}

Johan Halmén
Member #1,550
September 2001

Thanks for that!

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Years of thorough research have revealed that the red "x" that closes a window, really isn't red, but white on red background.

Years of thorough research have revealed that what people find beautiful about the Mandelbrot set is not the set itself, but all the rest.

Go to: