66 lines
1.6 KiB
C
66 lines
1.6 KiB
C
#include <stdlib.h>
|
|
#define QOI_IMPLEMENTATION
|
|
#include <qoi.h>
|
|
#include <allegro5/allegro5.h>
|
|
#include <allegro5/bitmap.h>
|
|
#include <allegro5/bitmap_io.h>
|
|
#include <allegro5/bitmap_lock.h>
|
|
#include <allegro5/color.h>
|
|
#include <allegro5/file.h>
|
|
#include <allegro5/bitmap_lock.h>
|
|
|
|
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;
|
|
} |