al_load_bitmap_flags(3alleg5)
π NAME π
okay so al_load_bitmap_flags β it's an Allegro 5 API function (that's the 3alleg5 manual bestie, iykyk) and it loads an image file into a shiny new ALLEGRO_BITMAP, except you get to pass flags and control the whole vibe. π
β¨ SYNOPSIS β¨
#include <allegro5/allegro.h>
ALLEGRO_BITMAP *al_load_bitmap_flags(const char *filename, int flags)
π¦ DESCRIPTION π¦
So it loads an image file into a brand new ALLEGRO_BITMAP(3alleg5). πΌοΈ The file type is determined by al_identify_bitmap(3alleg5), and it uses the extension as a fallback in case identification is not possible. detective energy with a backup plan, honestly iconic.
Returns NULL on error. π
The flags parameter may be a combination of the following constants β and bestie, there are layers to this:
ALLEGRO_NO_PREMULTIPLIED_ALPHAβ By default, Allegro pre-multiplies the alpha channel of an image with the image's color data when it loads it. Typically that would look something like this:
r = get_float_byte();
g = get_float_byte();
b = get_float_byte();
a = get_float_byte();
r = r * a;
g = g * a;
b = b * a;
set_image_pixel(x, y, r, g, b, a);
The reason for this can be seen in the Allegro example ex_premulalpha, ie, using pre-multiplied alpha gives more accurate color results in some cases. To use alpha blending with images loaded with pre-multiplied alpha, you would use the default blending mode, which is set with al_set_blender(ALLEGRO_ADD, ALLEGRO_ONE, ALLEGRO_INVERSE_ALPHA).
The ALLEGRO_NO_PREMULTIPLIED_ALPHA flag being set will ensure that images are not loaded with alpha pre-multiplied, but are loaded with color values direct from the image. That looks like this:
r = get_float_byte();
g = get_float_byte();
b = get_float_byte();
a = get_float_byte();
set_image_pixel(x, y, r, g, b, a);
To draw such an image using regular alpha blending, you would use al_set_blender(ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA) to set the correct blender. This has some caveats. First, as mentioned above, drawing such an image can result in less accurate color blending (when drawing an image with linear filtering on, the edges will be darker than they should be). Second, the behaviour is somewhat confusing, which is explained in the example below.
// Load and create bitmaps with an alpha channel
al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ANY_32_WITH_ALPHA);
// Load some bitmap with alpha in it
bmp = al_load_bitmap("some_alpha_bitmap.png");
// We will draw to this buffer and then draw this buffer to the screen
tmp_buffer = al_create_bitmap(SCREEN_W, SCREEN_H);
// Set the buffer as the target and clear it
al_set_target_bitmap(tmp_buffer);
al_clear_to_color(al_map_rgba_f(0, 0, 0, 1));
// Draw the bitmap to the temporary buffer
al_draw_bitmap(bmp, 0, 0, 0);
// Finally, draw the buffer to the screen
// The output will look incorrect (may take close inspection
// depending on the bitmap -- it may also be very obvious)
al_set_target_bitmap(al_get_backbuffer(display));
al_draw_bitmap(tmp_buffer, 0, 0, 0);
To explain further, if you have a pixel with 0.5 alpha, and you're using (ALLEGRO_ADD, ALLEGRO_ALPHA, ALLEGRO_INVERSE_ALPHA) for blending, the formula is:
a = da * dst + sa * src
Expands to:
result_a = dst_a * (1-0.5) + 0.5 * 0.5
So if you draw the image to the temporary buffer, it is blended once resulting in 0.75 alpha, then drawn again to the screen, blended in the same way, resulting in a pixel has 0.1875 as an alpha value. math ate that, fr fr.
ALLEGRO_KEEP_INDEXβ Load the palette indices of 8-bit.bmpand.pcxfiles instead of the rgb colors. Since5.1.0. π¨ALLEGRO_KEEP_BITMAP_FORMATβ Force the resultingALLEGRO_BITMAP(3alleg5)to use the same format as the file.
This is not yet honoured. π
Note: the core Allegro library does not support any image file formats by default. You must use the allegro_image addon, or register your own format handler. this one's important bestie, don't skip it!!
β SINCE β
5.1.0
π SEE ALSO π
and that's the whole page bestie!! πΈ this lil beauty lives in the Allegro reference manual, filed under al_load_bitmap_flags(3alleg5) β go off, load those bitmaps, and remember: pre-multiplied alpha is a lifestyle. πβ¨ππ¦
π¦ where this page comes from
- debian trixie: allegro5-doc 2:5.2.10.1+dfsg-1 Β· Zlib
- ubuntu questing: allegro5-doc 2:5.2.10.1+dfsg-1build1
- ubuntu resolute: allegro5-doc 2:5.2.11.3+dfsg-1 Β· Zlib
- ubuntu stonking: allegro5-doc 2:5.2.11.3+dfsg-1 Β· Zlib
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms