ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

png.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 // Since we need to work with libpng here, we need to allow all symbols
00024 // to avoid compilation issues.
00025 #define FORBIDDEN_SYMBOL_ALLOW_ALL
00026 #include "common/scummsys.h"
00027 
00028 #ifdef USE_PNG
00029 #include <png.h>
00030 #endif
00031 
00032 #include "image/png.h"
00033 
00034 #include "graphics/pixelformat.h"
00035 #include "graphics/surface.h"
00036 
00037 #include "common/array.h"
00038 #include "common/stream.h"
00039 
00040 namespace Image {
00041 
00042 PNGDecoder::PNGDecoder() :
00043         _outputSurface(0),
00044         _palette(0),
00045         _paletteColorCount(0),
00046         _skipSignature(false) {
00047 }
00048 
00049 PNGDecoder::~PNGDecoder() {
00050     destroy();
00051 }
00052 
00053 void PNGDecoder::destroy() {
00054     if (_outputSurface) {
00055         _outputSurface->free();
00056         delete _outputSurface;
00057         _outputSurface = 0;
00058     }
00059     delete[] _palette;
00060     _palette = NULL;
00061 }
00062 
00063 Graphics::PixelFormat PNGDecoder::getByteOrderRgbaPixelFormat() const {
00064 #ifdef SCUMM_BIG_ENDIAN
00065     return Graphics::PixelFormat(4, 8, 8, 8, 8, 24, 16, 8, 0);
00066 #else
00067     return Graphics::PixelFormat(4, 8, 8, 8, 8, 0, 8, 16, 24);
00068 #endif
00069 }
00070 
00071 #ifdef USE_PNG
00072 // libpng-error-handling:
00073 void pngError(png_structp pngptr, png_const_charp errorMsg) {
00074     error("%s", errorMsg);
00075 }
00076 
00077 void pngWarning(png_structp pngptr, png_const_charp warningMsg) {
00078     warning("%s", warningMsg);
00079 }
00080 
00081 // libpng-I/O-helpers:
00082 void pngReadFromStream(png_structp pngPtr, png_bytep data, png_size_t length) {
00083     void *readIOptr = png_get_io_ptr(pngPtr);
00084     Common::SeekableReadStream *stream = (Common::SeekableReadStream *)readIOptr;
00085     stream->read(data, length);
00086 }
00087 
00088 void pngWriteToStream(png_structp pngPtr, png_bytep data, png_size_t length) {
00089     void *writeIOptr = png_get_io_ptr(pngPtr);
00090     Common::WriteStream *stream = (Common::WriteStream *)writeIOptr;
00091     stream->write(data, length);
00092 }
00093 
00094 void pngFlushStream(png_structp pngPtr) {
00095     void *writeIOptr = png_get_io_ptr(pngPtr);
00096     Common::WriteStream *stream = (Common::WriteStream *)writeIOptr;
00097     stream->flush();
00098 }
00099 #endif
00100 
00101 /*
00102  * This code is based on Broken Sword 2.5 engine
00103  *
00104  * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
00105  *
00106  * Licensed under GNU GPL v2
00107  *
00108  */
00109 
00110 bool PNGDecoder::loadStream(Common::SeekableReadStream &stream) {
00111 #ifdef USE_PNG
00112     destroy();
00113 
00114     // First, check the PNG signature (if not set to skip it)
00115     if (!_skipSignature) {
00116         if (stream.readUint32BE() != MKTAG(0x89, 'P', 'N', 'G')) {
00117             return false;
00118         }
00119         if (stream.readUint32BE() != MKTAG(0x0d, 0x0a, 0x1a, 0x0a)) {
00120             return false;
00121         }
00122     }
00123 
00124     // The following is based on the guide provided in:
00125     //http://www.libpng.org/pub/png/libpng-1.2.5-manual.html#section-3
00126     //http://www.libpng.org/pub/png/libpng-1.4.0-manual.pdf
00127     // along with the png-loading code used in the sword25-engine.
00128     png_structp pngPtr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
00129     if (!pngPtr) {
00130         return false;
00131     }
00132     png_infop infoPtr = png_create_info_struct(pngPtr);
00133     if (!infoPtr) {
00134         png_destroy_read_struct(&pngPtr, NULL, NULL);
00135         return false;
00136     }
00137 
00138     png_set_error_fn(pngPtr, NULL, pngError, pngWarning);
00139     // TODO: The manual says errors should be handled via setjmp
00140 
00141     png_set_read_fn(pngPtr, &stream, pngReadFromStream);
00142     png_set_crc_action(pngPtr, PNG_CRC_DEFAULT, PNG_CRC_WARN_USE);
00143     // We already verified the PNG-header
00144     png_set_sig_bytes(pngPtr, 8);
00145 
00146     // Read PNG header
00147     png_read_info(pngPtr, infoPtr);
00148 
00149     // No handling for unknown chunks yet.
00150     int bitDepth, colorType, width, height, interlaceType;
00151     png_uint_32 w, h;
00152     png_get_IHDR(pngPtr, infoPtr, &w, &h, &bitDepth, &colorType, &interlaceType, NULL, NULL);
00153     width = w;
00154     height = h;
00155 
00156     // Allocate memory for the final image data.
00157     // To keep memory framentation low this happens before allocating memory for temporary image data.
00158     _outputSurface = new Graphics::Surface();
00159 
00160     // Images of all color formats except PNG_COLOR_TYPE_PALETTE
00161     // will be transformed into ARGB images
00162     if (colorType == PNG_COLOR_TYPE_PALETTE && !png_get_valid(pngPtr, infoPtr, PNG_INFO_tRNS)) {
00163         int numPalette = 0;
00164         png_colorp palette = NULL;
00165         uint32 success = png_get_PLTE(pngPtr, infoPtr, &palette, &numPalette);
00166         if (success != PNG_INFO_PLTE) {
00167             png_destroy_read_struct(&pngPtr, &infoPtr, NULL);
00168             return false;
00169         }
00170         _paletteColorCount = numPalette;
00171         _palette = new byte[_paletteColorCount * 3];
00172         for (int i = 0; i < _paletteColorCount; i++) {
00173             _palette[(i * 3)] = palette[i].red;
00174             _palette[(i * 3) + 1] = palette[i].green;
00175             _palette[(i * 3) + 2] = palette[i].blue;
00176 
00177         }
00178         _outputSurface->create(width, height, Graphics::PixelFormat::createFormatCLUT8());
00179         png_set_packing(pngPtr);
00180     } else {
00181         if (png_get_valid(pngPtr, infoPtr, PNG_INFO_tRNS)) {
00182             png_set_expand(pngPtr);
00183         }
00184 
00185         _outputSurface->create(width, height, getByteOrderRgbaPixelFormat());
00186         if (!_outputSurface->getPixels()) {
00187             error("Could not allocate memory for output image.");
00188         }
00189         if (bitDepth == 16)
00190             png_set_strip_16(pngPtr);
00191         if (bitDepth < 8)
00192             png_set_expand(pngPtr);
00193         if (colorType == PNG_COLOR_TYPE_GRAY ||
00194             colorType == PNG_COLOR_TYPE_GRAY_ALPHA)
00195             png_set_gray_to_rgb(pngPtr);
00196 
00197         if (colorType != PNG_COLOR_TYPE_RGB_ALPHA)
00198             png_set_filler(pngPtr, 0xff, PNG_FILLER_AFTER);
00199     }
00200 
00201     // After the transformations have been registered, the image data is read again.
00202     png_set_interlace_handling(pngPtr);
00203     png_read_update_info(pngPtr, infoPtr);
00204     png_get_IHDR(pngPtr, infoPtr, &w, &h, &bitDepth, &colorType, NULL, NULL, NULL);
00205     width = w;
00206     height = h;
00207 
00208     if (interlaceType == PNG_INTERLACE_NONE) {
00209         // PNGs without interlacing can simply be read row by row.
00210         for (int i = 0; i < height; i++) {
00211             png_read_row(pngPtr, (png_bytep)_outputSurface->getBasePtr(0, i), NULL);
00212         }
00213     } else {
00214         // PNGs with interlacing require us to allocate an auxillary
00215         // buffer with pointers to all row starts.
00216 
00217         // Allocate row pointer buffer
00218         png_bytep *rowPtr = new png_bytep[height];
00219         if (!rowPtr) {
00220             error("Could not allocate memory for row pointers.");
00221         }
00222 
00223         // Initialize row pointers
00224         for (int i = 0; i < height; i++)
00225             rowPtr[i] = (png_bytep)_outputSurface->getBasePtr(0, i);
00226 
00227         // Read image data
00228         png_read_image(pngPtr, rowPtr);
00229 
00230         // Free row pointer buffer
00231         delete[] rowPtr;
00232     }
00233 
00234     // Read additional data at the end.
00235     png_read_end(pngPtr, NULL);
00236 
00237     // Destroy libpng structures
00238     png_destroy_read_struct(&pngPtr, &infoPtr, NULL);
00239 
00240     return true;
00241 #else
00242     return false;
00243 #endif
00244 }
00245 
00246 bool writePNG(Common::WriteStream &out, const Graphics::Surface &input, const bool bottomUp) {
00247 #ifdef USE_PNG
00248     const Graphics::PixelFormat requiredFormat_3byte(3, 8, 8, 8, 0, 16, 8, 0, 0);
00249     const Graphics::PixelFormat requiredFormat_4byte(4, 8, 8, 8, 8, 0, 8, 16, 24);
00250 
00251     if (input.format.bytesPerPixel == 3) {
00252         if (input.format != requiredFormat_3byte) {
00253             warning("Cannot currently write PNG with 3-byte pixel format other than %s", requiredFormat_3byte.toString().c_str());
00254             return false;
00255         }
00256     } else if (input.format.bytesPerPixel != 4) {
00257         warning("Cannot currently write PNG with pixel format of bpp other than 3, 4");
00258         return false;
00259     }
00260 
00261     int colorType;
00262     Graphics::Surface *tmp = NULL;
00263     const Graphics::Surface *surface;
00264 
00265     if (input.format == requiredFormat_3byte) {
00266         surface = &input;
00267         colorType = PNG_COLOR_TYPE_RGB;
00268     } else {
00269         if (input.format == requiredFormat_4byte) {
00270             surface = &input;
00271         } else {
00272             surface = tmp = input.convertTo(requiredFormat_4byte);
00273         }
00274         colorType = PNG_COLOR_TYPE_RGB_ALPHA;
00275     }
00276 
00277     png_structp pngPtr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
00278     if (!pngPtr) {
00279         return false;
00280     }
00281     png_infop infoPtr = png_create_info_struct(pngPtr);
00282     if (!infoPtr) {
00283         png_destroy_write_struct(&pngPtr, NULL);
00284         return false;
00285     }
00286 
00287     png_set_error_fn(pngPtr, NULL, pngError, pngWarning);
00288     // TODO: The manual says errors should be handled via setjmp
00289 
00290     png_set_write_fn(pngPtr, &out, pngWriteToStream, pngFlushStream);
00291 
00292     png_set_IHDR(pngPtr, infoPtr, surface->w, surface->h, 8, colorType, PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
00293 
00294     Common::Array<const uint8 *> rows;
00295     rows.reserve(surface->h);
00296     if (bottomUp) {
00297         for (uint y = surface->h; y-- > 0;) {
00298             rows.push_back((const uint8 *)surface->getBasePtr(0, y));
00299         }
00300     } else {
00301         for (uint y = 0; y < surface->h; ++y) {
00302             rows.push_back((const uint8 *)surface->getBasePtr(0, y));
00303         }
00304     }
00305 
00306     png_set_rows(pngPtr, infoPtr, const_cast<uint8 **>(&rows.front()));
00307     png_write_png(pngPtr, infoPtr, 0, NULL);
00308     png_destroy_write_struct(&pngPtr, &infoPtr);
00309 
00310     // free tmp surface
00311     if (tmp) {
00312         tmp->free();
00313         delete tmp;
00314     }
00315 
00316     return true;
00317 #else
00318     return false;
00319 #endif
00320 }
00321 
00322 } // End of namespace Image


Generated on Sat Jun 22 2019 05:00:58 for ResidualVM by doxygen 1.7.1
curved edge   curved edge