Skip to content
Myr

png: do alpha pre-multiplication manually

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Something's up with libpng's alpha-mode... it's required to get proper alpha (transparency) on some images, but at the same time, that breaks other images (they are rendered way too dark). Doing the pre-multiplication manually seems to handle all images correctly.

authored by Daniel Eklöf · committed by Daniel Eklöf · parent 4190965 · Browse files · 2 files+30 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+30 added−1 removed

  • other:+30 added−1 removed

2 files+30 added−1 removed

modified:CHANGELOG.md+4 added
30 unmodified lines
31### Changed32### Deprecated33### Removed34### Fixed3536* PNG images being way too dark.373839### Security40### Contributors4142
646 unmodified lines
modified:png.c+26 added-1 removed
81 unmodified lines
82 png_set_strip_16(png_ptr); /* "pack" 16-bit colors to 8-bit */83 png_set_bgr(png_ptr);8485 /* pixman expects pre-multiplied alpha */86 png_set_alpha_mode(png_ptr, PNG_ALPHA_PREMULTIPLIED, 1.0);8687 /* Tell libpng to expand to RGB(A) when necessary, and tell pixman88 * whether we have alpha or not */89 pixman_format_code_t format;
47 unmodified lines
137 row_pointers[i] = &image_data[i * stride];138139 png_read_image(png_ptr, row_pointers);140141 /* pixman expects pre-multiplied alpha */142 if (format == PIXMAN_a8r8g8b8) {143 for (int i = 0; i < height; i++) {144 uint32_t *p = (uint32_t *)row_pointers[i];145 for (int j = 0; j < width; j++, p++) {146 uint8_t a = (*p >> 24) & 0xff;147 uint8_t r = (*p >> 16) & 0xff;148 uint8_t g = (*p >> 8) & 0xff;149 uint8_t b = (*p >> 0) & 0xff;150151 if (a == 0xff)152 continue;153154 if (a == 0) {155 r = g = b = 0;156 } else {157 r = r * a / 0xff;158 g = g * a / 0xff;159 b = b * a / 0xff;160 }161162 *p = (uint32_t)a << 24 | r << 16 | g << 8 | b;163 }164 }165 }166167 pix = pixman_image_create_bits_no_clear(168 format, width, height, (uint32_t *)image_data, stride);169170err:
12 unmodified lines