merged latest lodepng mainstream code: fixes encoder memory leak for some input error cases.

git-svn-id: svn://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@1506 af15c1b1-3010-417e-b628-4374ebc0bcbd
This commit is contained in:
Ozkan Sezer 2017-09-17 11:04:05 +00:00
parent e7648a42c9
commit 0f694ed6fe
2 changed files with 40 additions and 37 deletions

View file

@ -1,7 +1,7 @@
/* /*
LodePNG version 20161127 LodePNG version 20170917
Copyright (c) 2005-2016 Lode Vandevenne Copyright (c) 2005-2017 Lode Vandevenne
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -39,7 +39,7 @@ Rename this file to lodepng.cpp to use it for C++, or to lodepng.c to use it for
#pragma warning( disable : 4996 ) /*VS does not like fopen, but fopen_s is not standard C so unusable here*/ #pragma warning( disable : 4996 ) /*VS does not like fopen, but fopen_s is not standard C so unusable here*/
#endif /*_MSC_VER */ #endif /*_MSC_VER */
const char* LODEPNG_VERSION_STRING = "20161127"; const char* LODEPNG_VERSION_STRING = "20170917";
/* /*
This source file is built up in the following large parts. The code sections This source file is built up in the following large parts. The code sections
@ -3448,6 +3448,7 @@ unsigned lodepng_convert(unsigned char* out, const unsigned char* in,
size_t i; size_t i;
ColorTree tree; ColorTree tree;
size_t numpixels = w * h; size_t numpixels = w * h;
unsigned error = 0;
if(lodepng_color_mode_equal(mode_out, mode_in)) if(lodepng_color_mode_equal(mode_out, mode_in))
{ {
@ -3501,7 +3502,8 @@ unsigned lodepng_convert(unsigned char* out, const unsigned char* in,
for(i = 0; i != numpixels; ++i) for(i = 0; i != numpixels; ++i)
{ {
getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in); getPixelColorRGBA8(&r, &g, &b, &a, in, i, mode_in);
CERROR_TRY_RETURN(rgba8ToPixel(out, i, mode_out, &tree, r, g, b, a)); error = rgba8ToPixel(out, i, mode_out, &tree, r, g, b, a);
if (error) break;
} }
} }
@ -3510,7 +3512,7 @@ unsigned lodepng_convert(unsigned char* out, const unsigned char* in,
color_tree_cleanup(&tree); color_tree_cleanup(&tree);
} }
return 0; /*no error*/ return error;
} }
#ifdef LODEPNG_COMPILE_ENCODER #ifdef LODEPNG_COMPILE_ENCODER
@ -5636,22 +5638,12 @@ unsigned lodepng_encode(unsigned char** out, size_t* outsize,
*outsize = 0; *outsize = 0;
state->error = 0; state->error = 0;
lodepng_info_init(&info); /*check input values validity*/
lodepng_info_copy(&info, &state->info_png); if((state->info_png.color.colortype == LCT_PALETTE || state->encoder.force_palette)
&& (state->info_png.color.palettesize == 0 || state->info_png.color.palettesize > 256))
if((info.color.colortype == LCT_PALETTE || state->encoder.force_palette)
&& (info.color.palettesize == 0 || info.color.palettesize > 256))
{ {
state->error = 68; /*invalid palette size, it is only allowed to be 1-256*/ CERROR_RETURN_ERROR(state->error, 68); /*invalid palette size, it is only allowed to be 1-256*/
return state->error;
} }
if(state->encoder.auto_convert)
{
state->error = lodepng_auto_choose_color(&info.color, image, w, h, &state->info_raw);
}
if(state->error) return state->error;
if(state->encoder.zlibsettings.btype > 2) if(state->encoder.zlibsettings.btype > 2)
{ {
CERROR_RETURN_ERROR(state->error, 61); /*error: unexisting btype*/ CERROR_RETURN_ERROR(state->error, 61); /*error: unexisting btype*/
@ -5660,28 +5652,38 @@ unsigned lodepng_encode(unsigned char** out, size_t* outsize,
{ {
CERROR_RETURN_ERROR(state->error, 71); /*error: unexisting interlace mode*/ CERROR_RETURN_ERROR(state->error, 71); /*error: unexisting interlace mode*/
} }
state->error = checkColorValidity(state->info_png.color.colortype, state->info_png.color.bitdepth);
state->error = checkColorValidity(info.color.colortype, info.color.bitdepth);
if(state->error) return state->error; /*error: unexisting color type given*/ if(state->error) return state->error; /*error: unexisting color type given*/
state->error = checkColorValidity(state->info_raw.colortype, state->info_raw.bitdepth); state->error = checkColorValidity(state->info_raw.colortype, state->info_raw.bitdepth);
if(state->error) return state->error; /*error: unexisting color type given*/ if(state->error) return state->error; /*error: unexisting color type given*/
if(!lodepng_color_mode_equal(&state->info_raw, &info.color)) /* color convert and compute scanline filter types */
lodepng_info_init(&info);
lodepng_info_copy(&info, &state->info_png);
if(state->encoder.auto_convert)
{ {
unsigned char* converted; state->error = lodepng_auto_choose_color(&info.color, image, w, h, &state->info_raw);
size_t size = (w * h * (size_t)lodepng_get_bpp(&info.color) + 7) / 8;
converted = (unsigned char*)lodepng_malloc(size);
if(!converted && size) state->error = 83; /*alloc fail*/
if(!state->error)
{
state->error = lodepng_convert(converted, image, &info.color, &state->info_raw, w, h);
}
if(!state->error) preProcessScanlines(&data, &datasize, converted, w, h, &info, &state->encoder);
lodepng_free(converted);
} }
else preProcessScanlines(&data, &datasize, image, w, h, &info, &state->encoder); if (!state->error)
{
if(!lodepng_color_mode_equal(&state->info_raw, &info.color))
{
unsigned char* converted;
size_t size = (w * h * (size_t)lodepng_get_bpp(&info.color) + 7) / 8;
converted = (unsigned char*)lodepng_malloc(size);
if(!converted && size) state->error = 83; /*alloc fail*/
if(!state->error)
{
state->error = lodepng_convert(converted, image, &info.color, &state->info_raw, w, h);
}
if(!state->error) preProcessScanlines(&data, &datasize, converted, w, h, &info, &state->encoder);
lodepng_free(converted);
}
else preProcessScanlines(&data, &datasize, image, w, h, &info, &state->encoder);
}
/* output all PNG chunks */
ucvector_init(&outv); ucvector_init(&outv);
while(!state->error) /*while only executed once, to break on error*/ while(!state->error) /*while only executed once, to break on error*/
{ {

View file

@ -1,7 +1,7 @@
/* /*
LodePNG version 20161127 LodePNG version 20170917
Copyright (c) 2005-2016 Lode Vandevenne Copyright (c) 2005-2017 Lode Vandevenne
This software is provided 'as-is', without any express or implied This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages warranty. In no event will the authors be held liable for any damages
@ -1608,6 +1608,7 @@ yyyymmdd.
Some changes aren't backwards compatible. Those are indicated with a (!) Some changes aren't backwards compatible. Those are indicated with a (!)
symbol. symbol.
*) 17 sep 2017: fix memory leak for some encoder input error cases
*) 27 nov 2016: grey+alpha auto color model detection bugfix *) 27 nov 2016: grey+alpha auto color model detection bugfix
*) 18 apr 2016: Changed qsort to custom stable sort (for platforms w/o qsort). *) 18 apr 2016: Changed qsort to custom stable sort (for platforms w/o qsort).
*) 09 apr 2016: Fixed colorkey usage detection, and better file loading (within *) 09 apr 2016: Fixed colorkey usage detection, and better file loading (within
@ -1757,5 +1758,5 @@ Domain: gmail dot com.
Account: lode dot vandevenne. Account: lode dot vandevenne.
Copyright (c) 2005-2016 Lode Vandevenne Copyright (c) 2005-2017 Lode Vandevenne
*/ */