8 bit paletted image loading for Allegro 5
Trent Gamblin

I added a function to Allegro SVN today called al_create_user_bitmap(). It allows you to load custom texture format images (only tested with OpenGL) like say PVR or ETC1, but in my case I'm using it to load 8 bit paletted images on Android (should work on iOS too) with the OES_compressed_paletted_texture extension (which Allegro loads itself.)

The images save some memory (though that can depend on the device/drivers as some may just convert to a 32 bit image internally.) The code is C++ but the interface can be used from C. Loading a paletted image is a little bit round-about because you have to have created or loaded the image you want to convert to paletted beforehand, but it doesn't seem to slow my game down during loading much.

This is basically all there is to it:

1w = al_get_bitmap_width(bitmap); 2h = al_get_bitmap_height(bitmap); 3int true_w, true_h; 4al_get_opengl_texture_size(bitmap, &true_w, &true_h); 5int sz = (256*4) + (true_w * true_h); 6unsigned char *imgdata = new unsigned char[sz]; 7uint16_t *exact = new uint16_t[256]; 8gen_palette(bitmap, imgdata, exact); 9gen_paletted_image(bitmap, imgdata+(256*4), exact); 10delete[] exact; 11Paletted_Image_Data p; 12p.data = imgdata; 13p.size = sz; 14ALLEGRO_BITMAP *pal = al_create_custom_bitmap(w, h, upload_paletted_image, &p); 15delete[] imgdata; 16/* Could now delete bitmap as 'pal' is an 8 bit copy */

Make a function with that and you can load, then convert to 8 bit. I don't know if and how palette effects are possible with this. I don't know if you can modify the palette. I didn't make it for that purpose but it would be neat if you can. The code is attached, may require a tiny bit of work to get compiled and working as I ripped it from my game and removed anything specific to my game from it.

NOTE: You can lock these images READONLY but not for writing.
NOTE2: I haven't tried this with ANY images with a lot of colors. For all I know, and it's highly probably that I've only used images with < 256 colors, so the quantization isn't tested. I based it off reading the description of how the Allegro 4 quantization works from the source code (I was going to copy the code but it seemed like it'd be simpler to do it myself.)

EDIT: I've been working on it, since I tested with higher color images and it didn't work. It works pretty good now and is a lot faster. Still not super fast, but on my fairly slow tablet it takes 0.87 seconds to convert the 800x600 image below. The results aren't bad.



The bright light in the window is the worst part. If anyone wants me to post the new source I will.

Peter Wang

Hmm, I can't say I understood it. Doesn't this API pretty much require the use to dig into Allegro internals? Can you post the example?

Trent Gamblin

The example is identical to the code block I pasted. I have that in my game which is exactly what I used to generate that image. Well, exactly as in I made some minor modifications to the api, but those are minor (I just removed the need for the "exact" parameter.)

Peter Wang

Oh, I didn't see the attachment. I was looking for the upload_paletted_image function.

Indeed it casts the bitmap to ALLEGRO_BITMAP_EXTRA_OPENGL and works with the internals. I think we need to find another way. Maybe an OpenGL-specific function that creates a bitmap out of an existing texture?

Trent Gamblin

All it would need is an al_set_opengl_texture really.

Thread #610190. Printed from Allegro.cc