From Frederic Bouvier, "fix PNG write for images with bits per components different than 8 that was hard coded."

This commit is contained in:
Robert Osfield 2012-09-24 10:10:28 +00:00
parent ea631c777d
commit 0e8bcc9027

View File

@ -112,14 +112,15 @@ class ReaderWriterPNG : public osgDB::ReaderWriter
//Set compression level
png_set_compression_level(png, compression_level);
int bit_depth = img.getPixelSizeInBits();
switch(img.getPixelFormat()) {
case(GL_LUMINANCE): color = PNG_COLOR_TYPE_GRAY; break;
case(GL_ALPHA): color = PNG_COLOR_TYPE_GRAY; break; //Couldn't find a color type for pure alpha, using gray instead
case(GL_LUMINANCE_ALPHA): color = PNG_COLOR_TYPE_GRAY_ALPHA ; break;
case(GL_RGB): color = PNG_COLOR_TYPE_RGB; break;
case(GL_RGBA): color = PNG_COLOR_TYPE_RGB_ALPHA; break;
case(GL_BGR): color = PNG_COLOR_TYPE_RGB; png_set_bgr(png); break;
case(GL_BGRA): color = PNG_COLOR_TYPE_RGB_ALPHA; png_set_bgr(png); break;
case(GL_LUMINANCE_ALPHA): color = PNG_COLOR_TYPE_GRAY_ALPHA ; bit_depth /= 2; break;
case(GL_RGB): color = PNG_COLOR_TYPE_RGB; bit_depth /= 3; break;
case(GL_RGBA): color = PNG_COLOR_TYPE_RGB_ALPHA; bit_depth /= 4; break;
case(GL_BGR): color = PNG_COLOR_TYPE_RGB; png_set_bgr(png); bit_depth /= 3; break;
case(GL_BGRA): color = PNG_COLOR_TYPE_RGB_ALPHA; png_set_bgr(png); bit_depth /= 4; break;
default: return WriteResult::ERROR_IN_WRITING_FILE; break;
}
@ -131,7 +132,7 @@ class ReaderWriterPNG : public osgDB::ReaderWriter
//Write header info
png_set_IHDR(png, info, img.s(), img.t(),
8, color, PNG_INTERLACE_NONE,
bit_depth, color, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(png, info);