Premultiply color by alpha when requested

This commit is contained in:
2025-09-08 19:13:59 -05:00
parent 32179abb48
commit f26b831fc9

View File

@@ -35,16 +35,24 @@ ALLEGRO_BITMAP* _al_load_qoi_f(ALLEGRO_FILE* file, int flags) {
}
ALLEGRO_BITMAP* bmp = al_create_bitmap(meta.width, meta.height);
ALLEGRO_LOCKED_REGION* lock = al_lock_bitmap(
bmp,
fmt,
ALLEGRO_LOCK_WRITEONLY
bmp, fmt,
ALLEGRO_LOCK_READWRITE
);
al_set_new_bitmap_flags(oldflags);
if (!lock) return NULL;
const int qoi_pitch = meta.width * meta.channels;
for (int i = 0; i < meta.height; i++)
for (int i = 0; i < meta.height; i++) {
memcpy(lock->data + lock->pitch*i, data + qoi_pitch*i, qoi_pitch);
if (fmt == ALLEGRO_PIXEL_FORMAT_ABGR_8888
&& !(flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA))
for (int p = 0; p < meta.width; p++) {
uint8_t* pixbytes = (lock->data + lock->pitch*i + p*4);
pixbytes[0] *= pixbytes[3]/255.0;
pixbytes[1] *= pixbytes[3]/255.0;
pixbytes[2] *= pixbytes[3]/255.0;
}
}
al_unlock_bitmap(bmp);
free(data); return bmp;