diff --git a/allegro_qoi.c b/allegro_qoi.c index cffba77..366fbdb 100644 --- a/allegro_qoi.c +++ b/allegro_qoi.c @@ -7,13 +7,31 @@ #include #include #include -#include -ALLEGRO_BITMAP* _al_load_qoi_f(ALLEGRO_FILE* filename, int flags) { +#ifdef __cplusplus +extern "C" { +#endif + +ALLEGRO_BITMAP* _al_load_qoi_f(ALLEGRO_FILE* file, int flags) { qoi_desc meta; - void* data = qoi_read("sample.qoi", &meta, 4); - if (!data) return NULL; + 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, @@ -21,6 +39,8 @@ ALLEGRO_BITMAP* _al_load_qoi_f(ALLEGRO_FILE* filename, int flags) { ALLEGRO_LOCK_WRITEONLY ); if (!lock) return NULL; + al_set_new_bitmap_flags(oldflags); + //al_set_new_bitmap_format(oldfmt); lock->data = data; @@ -47,9 +67,8 @@ ALLEGRO_BITMAP* _al_load_qoi(const char* filename, int flags) { } bool _al_identify_qoi(ALLEGRO_FILE* f) { - uint32_t magic_found; uint32_t magic_expected = 1718185841; - magic_found = al_fread32le(f); + 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 @@ -63,4 +82,8 @@ bool al_init_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 +} + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/allegro_qoi.h b/allegro_qoi.h index b4cdf34..e61dbe6 100644 --- a/allegro_qoi.h +++ b/allegro_qoi.h @@ -1,11 +1,18 @@ #ifndef ALLEGRO_QOI_H #define ALLEGRO_QOI_H -#include +#ifdef __cplusplus +extern "C" { +#endif + 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); bool al_init_qoi(); +#ifdef __cplusplus +} +#endif + #endif //ALLEGRO_QOI_H