Skip to content

Commit 6f84fcb

Browse files
committed
Return correct bits-per-pixel data for 16-bit PNGs
PNGs can have 16 bits per channel but we always tell libpng to reduce that to 8 bits per channel. However, we do not tell the caller of png_read_header that this will happen so instead of 32-bits per pixel, we tell bmpman that the image has 64 bits per pixel which causes some issues. The specific issue I fixed was the system map of BtA where the jump nodes used a 16-bit PNG which broke the alpha blending of those bitmaps since bmpan though that the image had no alpha channel.
1 parent eb1389d commit 6f84fcb

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

code/pngutils/pngutils.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,11 @@ int png_read_header(const char *real_filename, CFILE *img_cfp, int *w, int *h, i
145145
if (w) *w = png_get_image_width(png_ptr, info_ptr);
146146
if (h) *h = png_get_image_height(png_ptr, info_ptr);
147147
// this turns out to be near useless, but meh
148-
if (bpp) *bpp = (png_get_channels(png_ptr, info_ptr) * png_get_bit_depth(png_ptr, info_ptr));
148+
if (bpp) {
149+
// bit depth can also be 16 bit we tell libpng to reduce that to 8 bits so we also need to tell our caller about that
150+
auto bits = std::min(8, (int)png_get_bit_depth(png_ptr, info_ptr));
151+
*bpp = (png_get_channels(png_ptr, info_ptr) * bits);
152+
}
149153

150154
if (img_cfp == NULL) {
151155
cfclose(status.cfp);

0 commit comments

Comments
 (0)