Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Allegro color bitmap

This thread is locked; no one can reply to it. rss feed Print
Allegro color bitmap
WielkiB
Member #19,906
April 2021

I got a snake head like this, and now I'm looking for something that I can use to make ONLY white color different. I though 'tinted bitmap' would be good, but it's also coloring eyes, and tongue. Furthermore, I found al_get_pixel and al_put_pixel, but in this I need to give x and y of pixel, so it's difficult to make it with that bitmap ;D So the thing I'm looking for is something like al_get_pixel but with given color, not x/y.

Arthur Kalliokoski
Second in Command
February 2005
avatar

al_get_pixel returns an ALLEGRO_COLOR, so run through all the x/y combinations and check if that color is white. If it is, use al_put_pixel on that same x/y with the replacement color and when you're done, save the png.

They all watch too much MSNBC... they get ideas.

WielkiB
Member #19,906
April 2021

Am I must save bitmap as .png file? If I want to do it in .png I will color it in Inkscape and then export 8-). I want to do in live in program. Like in MENU:
1) User input numbers from 0 to 255 to R, G, B.
2) Function change white color from bitmap to color, which user input into it.
3) Game start with colored bitmap.
???

Peter Hull
Member #1,136
March 2001

I would:

  • edit the bitmap so the parts you want to change colour are transparent

  • draw a coloured square

  • draw the bitmap on top

you can do this on an offscreen bitmap and then re-use that bitmap

[edit] hm, that doesn't work if you want the surroundings of the head to be transparent.

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_image.h> 3 4ALLEGRO_BITMAP *make_snake(ALLEGRO_BITMAP *s, ALLEGRO_COLOR c) { 5 int w = al_get_bitmap_width(s); 6 int h = al_get_bitmap_height(s); 7 ALLEGRO_BITMAP *b = al_create_bitmap(w, h); 8 al_set_target_bitmap(b); 9 al_clear_to_color(c); 10 al_draw_bitmap(s, 0.0f, 0.0f, 0); 11 al_set_target_backbuffer(al_get_current_display()); 12 return b; 13} 14int main() { 15 al_init(); 16 al_init_image_addon(); 17 ALLEGRO_DISPLAY *d = al_create_display(640, 480); 18 ALLEGRO_BITMAP *snake = al_load_bitmap("snake_white.png"); 19 ALLEGRO_BITMAP *snakes[3]; 20 ALLEGRO_COLOR cs[] = {al_map_rgb(255, 128, 128), al_map_rgb(128, 255, 128), 21 al_map_rgb(128, 128, 255)}; 22 int i; 23 for (i = 0; i < 3; ++i) { 24 snakes[i] = make_snake(snake, cs[i]); 25 } 26 al_clear_to_color(al_map_rgb(255, 255, 255)); 27 for (i = 0; i < 3; ++i) { 28 al_draw_bitmap(snakes[i], 200.0f * i, 100.0f, 0); 29 } 30 al_flip_display(); 31 al_rest(10.0); 32 return 0; 33}

[edit2] Better but I had to split the snake into two layers (you will be able to do this better if you have the original vector image!) (see attachments)
{"name":"612929","src":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/6\/9669099af39149b4ea8c2e52e39b4737.png","w":560,"h":146,"tn":"\/\/djungxnpq2nug.cloudfront.net\/image\/cache\/9\/6\/9669099af39149b4ea8c2e52e39b4737"}612929

#SelectExpand
1#include <allegro5/allegro.h> 2#include <allegro5/allegro_image.h> 3 4ALLEGRO_BITMAP *make_snake2(ALLEGRO_BITMAP *f, ALLEGRO_BITMAP *k, 5 ALLEGRO_COLOR c) { 6 int w = al_get_bitmap_width(k); 7 int h = al_get_bitmap_height(k); 8 ALLEGRO_BITMAP *b = al_create_bitmap(w, h); 9 al_set_target_bitmap(b); 10 al_clear_to_color(al_map_rgba(255,255,255,0)); 11 al_draw_tinted_bitmap(k, c, 0.0f, 0.0f, 0); 12 al_draw_bitmap(f, 0.0f, 0.0f, 0); 13 al_set_target_backbuffer(al_get_current_display()); 14 return b; 15} 16 17int main() { 18 al_init(); 19 al_init_image_addon(); 20 ALLEGRO_DISPLAY *d = al_create_display(640, 480); 21 ALLEGRO_BITMAP *front = al_load_bitmap("front.png"); 22 ALLEGRO_BITMAP *back = al_load_bitmap("back.png"); 23 ALLEGRO_BITMAP *snakes[3]; 24 ALLEGRO_COLOR cs[] = {al_map_rgb(255, 128, 128), al_map_rgb(128, 255, 128), 25 al_map_rgb(128, 128, 255)}; 26 int i; 27 for (i = 0; i < 3; ++i) { 28 snakes[i] = make_snake2(front, back, cs[i]); 29 } 30 al_clear_to_color(al_map_rgb(255, 255, 255)); 31 for (i = 0; i < 3; ++i) { 32 al_draw_bitmap(snakes[i], 200.0f * i, 100.0f, 0); 33 } 34 al_flip_display(); 35 al_rest(10.0); 36 return 0; 37}

RmBeer2
Member #16,660
April 2017
avatar

you can read about al_lock_bitmap/al_unlock_bitmap, This function allows you to access the bitmap directly in memory and modify the colors of the pixels one by one. It's much faster if you know how.

https://liballeg.org/a5docs/trunk/graphics.html#al_lock_bitmap

🌈🌈🌈 🌟 BlackRook WebSite 🌟 C/C++ 🌟 GNU/Linux 🌟 IceCream/Cornet 🌟 🌈🌈🌈

Rm Beer for Emperor 2021!

Dizzy Egg
Member #10,824
March 2009
avatar

This should get you started, this loads your snake_white.png, then saves it as snake_green.bmp, which I have attached.

You would obviously want to put this in a function, so you can use different colours etc, but this will get you started!

#SelectExpand
1int main() 2{ 3 al_init(); 4 al_init_primitives_addon(); 5 al_init_image_addon(); 6 7 ALLEGRO_DISPLAY *display = al_create_display(1024, 768); 8 9 ALLEGRO_BITMAP *snake_white = al_load_bitmap("snake_white.png"); 10 ALLEGRO_BITMAP *snake_green = al_create_bitmap(al_get_bitmap_width(snake_white), al_get_bitmap_height(snake_white)); 11 ALLEGRO_COLOR color; 12 13 al_set_target_bitmap(snake_green); 14 15 al_lock_bitmap(snake_white, ALLEGRO_LOCK_READONLY, 0); 16 17 for(int x=0; x<al_get_bitmap_width(snake_white); x++) 18 { 19 for(int y=0; y<al_get_bitmap_height(snake_white); y++) 20 { 21 color = al_get_pixel(snake_white, x, y); 22 23 if(color.r == 1.0 && color.g == 1.0 && color.b == 1.0) 24 { 25 al_put_pixel(x, y, al_map_rgb(50, 255, 50)); //Choose your own colour! 26 } 27 else 28 { 29 al_put_pixel(x, y, color); 30 } 31 } 32 } 33 34 al_unlock_bitmap(snake_white); 35 al_save_bitmap("snake_green.bmp", snake_green); 36}

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Peter Hull
Member #1,136
March 2001

These getpixel/putpixel solutions aren't ever going to work well because of the grey pixels at the edge of the outlines (antialiasing isn't it?). Depends on the image, I guess it would be fine for pixel-art type images.
612937

Dizzy Egg
Member #10,824
March 2009
avatar

(antialiasing isn't it?)

Yes, exactly! I did say "this should get you started" ;D

Some math would be required to properly colour those antialiased areas to the new colour, but I'm not going to provide that solution straight away ;)

[EDIT]
Oh go on then, it is nearly the weekend after all! New images attached :D

#SelectExpand
1#include <iostream> 2#include <allegro5/allegro.h> 3#include <allegro5/allegro_primitives.h> 4#include <allegro5/allegro_image.h> 5#include "stdio.h" 6 7using namespace std; 8 9ALLEGRO_DISPLAY *display; 10ALLEGRO_BITMAP *snake_white; 11 12void color_snake_and_save(ALLEGRO_COLOR new_color, const char *file_name) 13{ 14 ALLEGRO_BITMAP *snake_new = al_create_bitmap(al_get_bitmap_width(snake_white), al_get_bitmap_height(snake_white)); 15 ALLEGRO_COLOR color; 16 17 al_set_target_bitmap(snake_new); 18 19 al_lock_bitmap(snake_white, ALLEGRO_PIXEL_FORMAT_ANY_WITH_ALPHA, ALLEGRO_LOCK_READONLY); 20 21 for(int x=0; x<al_get_bitmap_width(snake_white); x++) 22 { 23 for(int y=0; y<al_get_bitmap_height(snake_white); y++) 24 { 25 color = al_get_pixel(snake_white, x, y); 26 27 if(color.r >= 0.01 && color.g >= 0.01 && color.b >= 0.01) 28 { 29 al_put_pixel(x, y, al_map_rgba_f(new_color.r * color.r, new_color.g * color.g, new_color.b * color.b, color.a)); 30 } 31 else 32 { 33 al_put_pixel(x, y, color); 34 } 35 } 36 } 37 38 al_unlock_bitmap(snake_white); 39 al_save_bitmap(file_name, snake_new); 40 41 al_destroy_bitmap(snake_new); 42} 43 44int main() 45{ 46 al_init(); 47 al_init_primitives_addon(); 48 al_init_image_addon(); 49 50 display = al_create_display(1024, 768); 51 52 snake_white = al_load_bitmap("snake_white.png"); 53 54 color_snake_and_save(al_map_rgb(255, 50, 50), "snake_red.bmp"); 55 color_snake_and_save(al_map_rgb(50, 255, 50), "snake_green.bmp"); 56 color_snake_and_save(al_map_rgb(50, 50, 255), "snake_blue.bmp"); 57}

612938
612939
612940

----------------------------------------------------
Please check out my songs:
https://soundcloud.com/dont-rob-the-machina

Go to: