![]() |
|
How draw PCX image with transparency? |
coder123
Member #14,305
May 2012
|
Hi, I have some .pcx images with black background. And want to draw this images with the black transparent. I could use al_convert_mask_to_alpha but the black color is not exactly (0,0,0) it could be (0,1,0) etc. How set transparency to this black background of pcx images? I using al_draw_bitmap_region() to draw bitmap. A5. Thank you. |
Trent Gamblin
Member #261
April 2000
![]() |
al_convert_mask_to_alpha is the only thing Allegro offers to do this. I've found a Windows(tm) tool called Greenfish Icon Editor to be great for removing backgrounds though. It has a "Remove Matte" function which will turn the black to alpha... not guaranteed to work but for me it did.
|
LennyLen
Member #5,313
December 2004
![]() |
I've attached a command line utility you can use to convert the near-black pixels in your images to black pixels. Press SPACE to increase the tolerance value it uses to replace pixels and S to save. ESC exits the program. Each pass through, any pixels that are converted are made transparent on a copy of the image which is then drawn over a light blue background. This will allow you to still see any dark background pixels that haven't been converted to black yet. Here's the code (Allegro 4): 1#include <allegro.h>
2#include <stdio.h>
3
4
5int keyrel(int k)
6{
7 static int initialized = 0;
8 static int keyp[KEY_MAX];
9
10 if(!initialized)
11 {
12 /* Set the keyp (key pressed) flags to false */
13 int i;
14 for(i = 0; i < KEY_MAX; i++) keyp[i] = 0;
15 initialized = 1;
16 }
17
18 /* Now for the checking
19 Check if the key was pressed
20 */
21 if(key[k] && !keyp[k])
22 {
23 /* Set the flag and return */
24 keyp[k] = 1;
25 return 0;
26 }
27 else if(!key[k] && keyp[k])
28 {
29 /* The key was released */
30 keyp[k] = 0;
31 return 1;
32 }
33 /* Nothing happened? */
34 return 0;
35}
36
37
38int main(int argc, char *argv[]) {
39
40 int x, y, color, hue;
41
42 if (argc < 2) {
43
44 printf("No .pcx file specified.");
45 exit(1);
46 }
47
48 allegro_init();
49 install_keyboard();
50 set_color_depth(desktop_color_depth());
51 set_gfx_mode(GFX_AUTODETECT_WINDOWED, 640, 480, 0, 0);
52
53
54 BITMAP *src = load_bitmap(argv[1], NULL);
55 if(!src) {
56
57 allegro_message("Error loading %s", argv[1]);
58 exit(1);
59
60 }
61
62 BITMAP *magic_src = create_bitmap(src->w, src->h);
63 if(!magic_src) {
64
65 allegro_message("Error creating secondary bitmap");
66 exit(1);
67
68 }
69
70 BITMAP *buffer = create_bitmap(SCREEN_W, SCREEN_H);
71 if(!buffer) {
72
73 allegro_message("Error creating buffer");
74 exit(1);
75
76 }
77
78 blit(src, magic_src, 0, 0, 0, 0, src->w, src->h);
79
80 for (y = 0; y < magic_src->h; y++) {
81
82 for (x = 0; x < magic_src->w; x++) {
83
84 color = getpixel(magic_src, x, y);
85 if (color == makecol(0, 0, 0))
86 putpixel(magic_src, x, y, makecol(255, 0, 255));
87
88 }
89
90 }
91
92 int done = 0;
93 int tolerance = 2;
94
95 while (!done) {
96
97 if (key[KEY_ESC]) done = 1;
98
99 if (keyrel(KEY_SPACE)) {
100
101 for (y = 0; y < src->h; y++) {
102
103 for (x = 0; x < src->w; x++) {
104
105 color = getpixel(src, x, y);
106 hue = getr(color) + getg(color) + getb(color);
107
108 if (hue <= tolerance) {
109
110 putpixel(src, x, y, makecol(0, 0, 0));
111 putpixel(magic_src, x, y, makecol(255, 0, 255));
112
113 }
114
115 }
116
117 }
118
119 tolerance += 2;
120
121 }
122
123 if (keyrel(KEY_S))
124 save_bitmap(argv[1], src, NULL);
125
126 clear_to_color(buffer, makecol(0, 255, 255));
127 draw_sprite(buffer, magic_src, 0, 0);
128 blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h);
129
130
131 }
132
133 return 0;
134
135}
136END_OF_MAIN()
|
coaxil
Member #14,855
January 2013
|
You could, dunno, batch-edit your pictures to replace black pixels with zero alpha. |
Edgar Reynaldo
Major Reynaldo
May 2007
![]() |
Just use Paint.net or Gimp to flood fill black with a high (20% tolerance) - that should give you a decent edge. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
LennyLen
Member #5,313
December 2004
![]() |
coaxil said: You could, dunno, batch-edit your pictures to replace black pixels with zero alpha. PCX doesn't support alpha.
|
coder123
Member #14,305
May 2012
|
I've implemented your code in my Draw function. Thank you. |
LennyLen
Member #5,313
December 2004
![]() |
Ok, but be sure too realize that it wasn't designed to be used during the running of the game itself, and it could slow your drawing down considerably. The intended use is for you to go over all the graphics you do have and replace them so that you can use them in your game without having to modify them at runtime.
|
|