commit d68cceced3e9f8e5fba0743314fc2da64386eb01 Author: Brett Bergstrom Date: Fri Sep 5 20:54:47 2025 -0500 Initial Commit diff --git a/allegro_qoi.c b/allegro_qoi.c new file mode 100644 index 0000000..cffba77 --- /dev/null +++ b/allegro_qoi.c @@ -0,0 +1,66 @@ +#include +#define QOI_IMPLEMENTATION +#include +#include +#include +#include +#include +#include +#include +#include + +ALLEGRO_BITMAP* _al_load_qoi_f(ALLEGRO_FILE* filename, int flags) { + qoi_desc meta; + void* data = qoi_read("sample.qoi", &meta, 4); + if (!data) return NULL; + + ALLEGRO_BITMAP* bmp = al_create_bitmap(meta.width, meta.height); + ALLEGRO_LOCKED_REGION* lock = al_lock_bitmap( + bmp, + ALLEGRO_PIXEL_FORMAT_ABGR_8888, + ALLEGRO_LOCK_WRITEONLY + ); + if (!lock) return NULL; + + lock->data = data; + + al_unlock_bitmap(bmp); + return bmp; +} + +ALLEGRO_BITMAP* _al_load_qoi(const char* filename, int flags) { + ALLEGRO_FILE* fp; + ALLEGRO_BITMAP* bmp; + + ALLEGRO_ASSERT(filename); + + fp = al_fopen(filename, "rb"); + if (!fp) { + //ALLEGRO_ERROR("Unable to open %s for reading.\n", filename); + return NULL; + } + + bmp = _al_load_qoi_f(fp, flags); + + al_fclose(fp); + return bmp; +} + +bool _al_identify_qoi(ALLEGRO_FILE* f) { + uint32_t magic_found; + uint32_t magic_expected = 1718185841; + magic_found = al_fread32le(f); + if (magic_found != magic_expected) + return false; + if (!al_fseek(f, 14 - 4, ALLEGRO_SEEK_CUR)) // check for min size 14-byte header minus 32-bit magic + return false; + return true; +} + +bool al_init_qoi() { + int insane = 0; + insane |= al_register_bitmap_loader(".qoi", _al_load_qoi); + insane |= al_register_bitmap_loader_f(".qoi", _al_load_qoi_f); + insane |= al_register_bitmap_identifier(".qoi", _al_identify_qoi); + return insane; +} \ No newline at end of file diff --git a/allegro_qoi.h b/allegro_qoi.h new file mode 100644 index 0000000..faa21fb --- /dev/null +++ b/allegro_qoi.h @@ -0,0 +1,9 @@ +#ifndef ALLEGRO_QOI_H +#define ALLEGRO_QOI_H + +#include +ALLEGRO_BITMAP* _al_load_qoi_f(ALLEGRO_FILE* filename, int flags); +ALLEGRO_BITMAP* _al_load_qoi(const char* filename, int flags); +bool _al_identify_qoi(ALLEGRO_FILE* f); + +#endif //ALLEGRO_QOI_H diff --git a/sample.qoi b/sample.qoi new file mode 100644 index 0000000..b1dc2ec Binary files /dev/null and b/sample.qoi differ