#include #define QOI_IMPLEMENTATION #include #include #include #include #include #include #include #ifdef __cplusplus extern "C" { #endif ALLEGRO_BITMAP* _al_load_qoi_f(ALLEGRO_FILE* file, int flags) { qoi_desc meta; uint8_t* file_bytes = malloc(al_fsize(file)); memset(file_bytes, 0, al_fsize(file)); void* file_pos = file_bytes; int bytes_read = 0; #define chunk_size 1024 al_fseek(file, 0, SEEK_SET); while (!al_feof(file)) { bytes_read = al_fread(file, file_pos, chunk_size); file_pos += bytes_read; } void* data = qoi_decode(file_bytes, al_fsize(file), &meta, 0); free(file_bytes); if (!data) return NULL; int oldflags = al_get_new_bitmap_flags(); al_set_new_bitmap_flags(flags); //ALLEGRO_PIXEL_FORMAT oldfmt = al_get_new_bitmap_format(); //al_set_new_bitmap_format(ALLEGRO_PIXEL_FORMAT_ABGR_8888); 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; al_set_new_bitmap_flags(oldflags); //al_set_new_bitmap_format(oldfmt); 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_expected = 1718185841; uint32_t 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; } #ifdef __cplusplus } #endif