diff --git a/README.md b/README.md new file mode 100644 index 0000000..6201899 --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +# QOI for Allegro + +This is a simple liballeg plugin that gives Allegro the ability to decode QOI images, in both 24-bit and 32-bit formats. + +Some example images and a test program are provided. All of the code is in allegro_qoi.c. The header file declares only a single function `al_init_qoi()` which takes no parameters (same thing as `al_init_image_addon()`). Call it once when your program starts. + +If you want you can also just drop the `allegro_qoi.c` into your project and forward-declare it's init function once where it's used. + +This plugin does not handle saving QOI images yet. \ No newline at end of file diff --git a/allegro_qoi.c b/allegro_qoi.c index 0f11b5b..866a01c 100644 --- a/allegro_qoi.c +++ b/allegro_qoi.c @@ -14,6 +14,7 @@ extern "C" { ALLEGRO_BITMAP* _al_load_qoi_f(ALLEGRO_FILE* file, int flags) { qoi_desc meta; + ALLEGRO_PIXEL_FORMAT fmt; uint8_t* file_bytes = malloc(al_fsize(file)); memset(file_bytes, 0, al_fsize(file)); void* file_pos = file_bytes; @@ -27,10 +28,15 @@ ALLEGRO_BITMAP* _al_load_qoi_f(ALLEGRO_FILE* file, int flags) { int oldflags = al_get_new_bitmap_flags(); al_set_new_bitmap_flags(flags); + switch (meta.channels) { + case 3: fmt = ALLEGRO_PIXEL_FORMAT_BGR_888; break; + case 4: fmt = ALLEGRO_PIXEL_FORMAT_ABGR_8888; break; + default: free(data); return NULL; break; + } ALLEGRO_BITMAP* bmp = al_create_bitmap(meta.width, meta.height); ALLEGRO_LOCKED_REGION* lock = al_lock_bitmap( bmp, - ALLEGRO_PIXEL_FORMAT_ABGR_8888, + fmt, ALLEGRO_LOCK_WRITEONLY ); al_set_new_bitmap_flags(oldflags); diff --git a/sample.c b/sample.c index aa82c03..c0f3b50 100644 --- a/sample.c +++ b/sample.c @@ -1,24 +1,30 @@ // This program simply loads a QOI file into Allegro // as a texture and then dumps the texture data as a PNG. -// Take notice of lines 20 and 21. You only need to initialize +// Take notice of lines 26 and 27. You only need to initialize // the QOI plugin (this registers the function hooks that Allegro // will use whenever you want to I/O on a filename ending in .qoi) // -// Compile the test program with: -// gcc -o test allegro_qoi.c test.c -lallegro -lallegro_image -I./ +// Compile the sample program with: +// gcc -o sample allegro_qoi.c sample.c -lallegro -lallegro_image -I./ +// Then run it with a QOI image as the first argument. // // qoi.h should be inside of your include path. // Either place it here, or in your system's global includes. +#include #include #include #include "allegro_qoi.h" -int main() { +int main(int argc, char **argv) { + if (argc < 2) { + printf("Usage: sample \n"); + return 0; + } al_init(); al_init_image_addon(); al_init_qoi(); - ALLEGRO_BITMAP* bmp = al_load_bitmap("sample.qoi"); + ALLEGRO_BITMAP* bmp = al_load_bitmap(argv[1]); al_save_bitmap("out.png", bmp); return 0; } diff --git a/sample.qoi b/sample_EricSchwartz.qoi similarity index 100% rename from sample.qoi rename to sample_EricSchwartz.qoi diff --git a/sample_KDE.qoi b/sample_KDE.qoi new file mode 100644 index 0000000..4a77a24 Binary files /dev/null and b/sample_KDE.qoi differ diff --git a/sample_VanGoghAlive.qoi b/sample_VanGoghAlive.qoi new file mode 100644 index 0000000..f860c8e Binary files /dev/null and b/sample_VanGoghAlive.qoi differ diff --git a/sample_WipeoutPepsi.qoi b/sample_WipeoutPepsi.qoi new file mode 100644 index 0000000..3cef9e0 Binary files /dev/null and b/sample_WipeoutPepsi.qoi differ diff --git a/sample_caniwall.qoi b/sample_caniwall.qoi new file mode 100644 index 0000000..85e8947 Binary files /dev/null and b/sample_caniwall.qoi differ