From 43a148ce3e2ef6aeb5dc03c176b8b1aa1a24c9f4 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Mon, 11 Aug 2014 02:42:12 -0300 Subject: [PATCH] Fixed detection of PNG images. --HG-- branch : minor --- src/modules/image/magpie/PNGHandler.cpp | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/modules/image/magpie/PNGHandler.cpp b/src/modules/image/magpie/PNGHandler.cpp index b044625ee..1d45365cc 100644 --- a/src/modules/image/magpie/PNGHandler.cpp +++ b/src/modules/image/magpie/PNGHandler.cpp @@ -128,10 +128,14 @@ static unsigned zlibCompress(unsigned char **out, size_t *outsize, const unsigne bool PNGHandler::canDecode(love::filesystem::FileData *data) { - std::string ext = data->getExtension(); - std::transform(ext.begin(), ext.end(), ext.begin(), tolower); + unsigned int width = 0, height = 0; + unsigned char *indata = (unsigned char *) data->getData(); + size_t insize = data->getSize(); - return ext.compare("png") == 0; + lodepng::State state; + unsigned status = lodepng_inspect(&width, &height, &state, indata, insize); + + return status == 0 && width > 0 && height > 0; } bool PNGHandler::canEncode(ImageData::Format format) @@ -147,8 +151,7 @@ PNGHandler::DecodedImage PNGHandler::decode(love::filesystem::FileData *fdata) DecodedImage img; - LodePNGState state; - lodepng_state_init(&state); + lodepng::State state; state.info_raw.colortype = LCT_RGBA; state.info_raw.bitdepth = 8; @@ -158,8 +161,6 @@ PNGHandler::DecodedImage PNGHandler::decode(love::filesystem::FileData *fdata) unsigned status = lodepng_decode(&img.data, &width, &height, &state, indata, insize); - lodepng_state_cleanup(&state); - if (status != 0) { const char *err = lodepng_error_text(status); @@ -180,8 +181,7 @@ PNGHandler::EncodedImage PNGHandler::encode(const DecodedImage &img, ImageData:: EncodedImage encimg; - LodePNGState state; - lodepng_state_init(&state); + lodepng::State state; state.info_raw.colortype = LCT_RGBA; state.info_raw.bitdepth = 8; @@ -195,8 +195,6 @@ PNGHandler::EncodedImage PNGHandler::encode(const DecodedImage &img, ImageData:: unsigned status = lodepng_encode(&encimg.data, &encimg.size, img.data, img.width, img.height, &state); - lodepng_state_cleanup(&state); - if (status != 0) { const char *err = lodepng_error_text(status);