From f26b831fc9b66554f81ae4e5ef7d1312e29f5658 Mon Sep 17 00:00:00 2001 From: Brett Bergstrom Date: Mon, 8 Sep 2025 19:13:59 -0500 Subject: [PATCH] Premultiply color by alpha when requested --- allegro_qoi.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/allegro_qoi.c b/allegro_qoi.c index 866a01c..251d3be 100644 --- a/allegro_qoi.c +++ b/allegro_qoi.c @@ -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;