From 56a4268ccaf34fbb5e45d3a63c27efea9e23178e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20K=C3=A4fer?= Date: Tue, 4 Aug 2015 17:45:26 +0200 Subject: [PATCH] manually premultiply the image if libpng can't do it --- platform/default/png_reader.cpp | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/platform/default/png_reader.cpp b/platform/default/png_reader.cpp index 42b694609..04f547d8e 100644 --- a/platform/default/png_reader.cpp +++ b/platform/default/png_reader.cpp @@ -160,8 +160,6 @@ void PngReader::read(unsigned x0, unsigned y0, unsigned w, unsigned h, char * #ifdef PNG_ALPHA_PREMULTIPLIED png_set_alpha_mode(png_ptr, PNG_ALPHA_PREMULTIPLIED, PNG_GAMMA_LINEAR); -#else - // TODO: Manually premultiply the image data. #endif if (x0 == 0 && y0 == 0 && w >= width_ && h >= height_) @@ -180,6 +178,20 @@ void PngReader::read(unsigned x0, unsigned y0, unsigned w, unsigned h, char * for (unsigned row = 0; row < height_; ++row) rows[row] = (png_bytep)image + row * width_ * 4 ; png_read_image(png_ptr, rows.get()); + fprintf(stderr, "manual premultiplication\n"); + +#ifndef PNG_ALPHA_PREMULTIPLIED + // Manually premultiply the image if libpng didn't do it for us. + for (unsigned row = 0; row < height_; ++row) { + for (unsigned x = 0; x < width_; x++) { + png_byte* ptr = &(rows[row][x * 4]); + const float a = ptr[3] / 255.0f; + ptr[0] *= a; + ptr[1] *= a; + ptr[2] *= a; + } + } +#endif } else { @@ -193,6 +205,7 @@ void PngReader::read(unsigned x0, unsigned y0, unsigned w, unsigned h, char * png_read_row(png_ptr,row.get(),0); if (i >= y0 && i < (y0 + h)) { + // TODO: premultiply this std::copy(&row[x0 * 4], &row[x0 * 4] + w * 4, image + i * width_* 4); } }