From b08e7b1168c781ba163fac619e6a310cfa587420 Mon Sep 17 00:00:00 2001 From: Ozkan Sezer Date: Wed, 9 Aug 2017 20:00:08 +0000 Subject: [PATCH] Some more image.c / lodepng / stb_image_write clean-ups: image.c: define LODEPNG_NO_COMPILE_CPP for lodepng.c. image.c: define STB_IMAGE_WRITE_STATIC for stb_image_write.h image.c: minor whitespace tidy-up. stb_image_write.h: remove extern declaration for now non-existing stbi_write_tga_with_rle (missed in commit r1468). stb_image_write.h: comment out stbi_write_jpg_to_func() which quakespasm doesn't use. lodepng.c (lodepng_save_file): remove unneeded (char*) cast from buffer. lodepng.c (lodepng_read32bitInt): make it static. lodepng.c (lodepng_get_raw_size_lct): make it static, and move below to the place it is actually used. lodepng.c (lodepng_info_swap): remove (unused function). lodepng.c (readChunk_zTXt,readChunk_iTXt,addChunk_zTXt, addChunk_iTXt): add missing const to casts on const pointers in zlib_compress() calls. (see: https://github.com/lvandeve/lodepng/pull/34). lodepng.c (inflateHuffmanBlock): wrong error code if huffmanDecodeSymbol fail. (see: https://github.com/lvandeve/lodepng/pull/52). git-svn-id: svn://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@1471 af15c1b1-3010-417e-b628-4374ebc0bcbd --- Quake/image.c | 18 +++++++++--------- Quake/lodepng.c | 37 +++++++++++++++---------------------- Quake/stb_image_write.h | 5 ++++- 3 files changed, 28 insertions(+), 32 deletions(-) diff --git a/Quake/image.c b/Quake/image.c index 5d3d30b3..a82bb1ac 100644 --- a/Quake/image.c +++ b/Quake/image.c @@ -24,9 +24,11 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include "quakedef.h" #define STB_IMAGE_WRITE_IMPLEMENTATION +#define STB_IMAGE_WRITE_STATIC #include "stb_image_write.h" #define LODEPNG_NO_COMPILE_DECODER +#define LODEPNG_NO_COMPILE_CPP #define LODEPNG_NO_COMPILE_ANCILLARY_CHUNKS #define LODEPNG_NO_COMPILE_ERROR_TEXT #include "lodepng.h" @@ -550,13 +552,12 @@ qboolean Image_WritePNG (const char *name, byte *data, int width, int height, in if (!(bpp == 32 || bpp == 24)) Sys_Error("bpp not 24 or 32"); - + Sys_mkdir (com_gamedir); //if we've switched to a nonexistant gamedir, create it now so we don't crash q_snprintf (pathname, sizeof(pathname), "%s/%s", com_gamedir, name); - + flipped = (!upsidedown)? CopyFlipped (data, width, height, bpp) : data; filters = (unsigned char *) malloc (height); - if (!filters || !flipped) { if (!upsidedown) @@ -564,7 +565,7 @@ qboolean Image_WritePNG (const char *name, byte *data, int width, int height, in free (filters); return false; } - + // set some options for faster compression lodepng_state_init(&state); state.encoder.zlibsettings.use_lz77 = 0; @@ -572,7 +573,7 @@ qboolean Image_WritePNG (const char *name, byte *data, int width, int height, in state.encoder.filter_strategy = LFS_PREDEFINED; memset(filters, 1, height); //use filter 1; see https://www.w3.org/TR/PNG-Filters.html state.encoder.predefined_filters = filters; - + if (bpp == 24) { state.info_raw.colortype = LCT_RGB; @@ -583,19 +584,18 @@ qboolean Image_WritePNG (const char *name, byte *data, int width, int height, in state.info_raw.colortype = LCT_RGBA; state.info_png.color.colortype = LCT_RGBA; } - + error = lodepng_encode (&png, &pngsize, flipped, width, height, &state); if (error == 0) lodepng_save_file (png, pngsize, pathname); #ifdef LODEPNG_COMPILE_ERROR_TEXT else Con_Printf("WritePNG: %s\n", lodepng_error_text()); #endif - + lodepng_state_cleanup (&state); - free (png); free (filters); if (!upsidedown) free (flipped); - + return (error == 0); } diff --git a/Quake/lodepng.c b/Quake/lodepng.c index bf237df8..25f456d9 100644 --- a/Quake/lodepng.c +++ b/Quake/lodepng.c @@ -317,7 +317,7 @@ static void string_set(char** out, const char* in) /* ////////////////////////////////////////////////////////////////////////// */ -unsigned lodepng_read32bitInt(const unsigned char* buffer) +static unsigned lodepng_read32bitInt(const unsigned char* buffer) { return (unsigned)((buffer[0] << 24) | (buffer[1] << 16) | (buffer[2] << 8) | buffer[3]); } @@ -402,7 +402,7 @@ unsigned lodepng_save_file(const unsigned char* buffer, size_t buffersize, const FILE* file; file = fopen(filename, "wb" ); if(!file) return 79; - fwrite((char*)buffer , 1 , buffersize, file); + fwrite(buffer , 1 , buffersize, file); fclose(file); return 0; } @@ -1171,7 +1171,7 @@ static unsigned inflateHuffmanBlock(ucvector* out, const unsigned char* in, size code_d = huffmanDecodeSymbol(in, bp, &tree_d, inbitlength); if(code_d > 29) { - if(code_ll == (unsigned)(-1)) /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/ + if(code_d == (unsigned)(-1)) /*huffmanDecodeSymbol returns (unsigned)(-1) in case of error*/ { /*return error code 10 or 11 depending on the situation that happened in huffmanDecodeSymbol (10=no endcode, 11=wrong jump outside of tree)*/ @@ -2713,14 +2713,6 @@ size_t lodepng_get_raw_size(unsigned w, unsigned h, const LodePNGColorMode* colo return ((n / 8) * bpp) + ((n & 7) * bpp + 7) / 8; } -size_t lodepng_get_raw_size_lct(unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth) -{ - /*will not overflow for any color type if roughly w * h < 268435455*/ - size_t bpp = lodepng_get_bpp_lct(colortype, bitdepth); - size_t n = w * h; - return ((n / 8) * bpp) + ((n & 7) * bpp + 7) / 8; -} - #ifdef LODEPNG_COMPILE_PNG #ifdef LODEPNG_COMPILE_DECODER @@ -2968,13 +2960,6 @@ unsigned lodepng_info_copy(LodePNGInfo* dest, const LodePNGInfo* source) return 0; } -void lodepng_info_swap(LodePNGInfo* a, LodePNGInfo* b) -{ - LodePNGInfo temp = *a; - *a = *b; - *b = temp; -} - /* ////////////////////////////////////////////////////////////////////////// */ /*index: bitgroup index, bits: bitgroup size(1, 2 or 4), in: bitgroup value, out: octet array to add bits to*/ @@ -4380,7 +4365,7 @@ static unsigned readChunk_zTXt(LodePNGInfo* info, const LodePNGDecompressSetting length = chunkLength - string2_begin; /*will fail if zlib error, e.g. if length is too small*/ error = zlib_decompress(&decoded.data, &decoded.size, - (unsigned char*)(&data[string2_begin]), + (const unsigned char*)(&data[string2_begin]), length, zlibsettings); if(error) break; ucvector_push_back(&decoded, 0); @@ -4463,7 +4448,7 @@ static unsigned readChunk_iTXt(LodePNGInfo* info, const LodePNGDecompressSetting { /*will fail if zlib error, e.g. if length is too small*/ error = zlib_decompress(&decoded.data, &decoded.size, - (unsigned char*)(&data[begin]), + (const unsigned char*)(&data[begin]), length, zlibsettings); if(error) break; if(decoded.allocsize < decoded.size) decoded.allocsize = decoded.size; @@ -5023,7 +5008,7 @@ static unsigned addChunk_zTXt(ucvector* out, const char* keyword, const char* te ucvector_push_back(&data, 0); /*compression method: 0*/ error = zlib_compress(&compressed.data, &compressed.size, - (unsigned char*)textstring, textsize, zlibsettings); + (const unsigned char*)textstring, textsize, zlibsettings); if(!error) { for(i = 0; i != compressed.size; ++i) ucvector_push_back(&data, compressed.data[i]); @@ -5059,7 +5044,7 @@ static unsigned addChunk_iTXt(ucvector* out, unsigned compressed, const char* ke ucvector compressed_data; ucvector_init(&compressed_data); error = zlib_compress(&compressed_data.data, &compressed_data.size, - (unsigned char*)textstring, textsize, zlibsettings); + (const unsigned char*)textstring, textsize, zlibsettings); if(!error) { for(i = 0; i != compressed_data.size; ++i) ucvector_push_back(&data, compressed_data.data[i]); @@ -6160,6 +6145,14 @@ unsigned decode(std::vector& out, unsigned& w, unsigned& h, const #endif /* LODEPNG_COMPILE_DISK */ #ifdef LODEPNG_COMPILE_ENCODER +static size_t lodepng_get_raw_size_lct(unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth) +{ + /*will not overflow for any color type if roughly w * h < 268435455*/ + size_t bpp = lodepng_get_bpp_lct(colortype, bitdepth); + size_t n = w * h; + return ((n / 8) * bpp) + ((n & 7) * bpp + 7) / 8; +} + unsigned encode(std::vector& out, const unsigned char* in, unsigned w, unsigned h, LodePNGColorType colortype, unsigned bitdepth) { diff --git a/Quake/stb_image_write.h b/Quake/stb_image_write.h index 3471b7da..d5904438 100644 --- a/Quake/stb_image_write.h +++ b/Quake/stb_image_write.h @@ -134,7 +134,6 @@ extern "C" { #define STBIWDEF static #else #define STBIWDEF extern -extern int stbi_write_tga_with_rle; #endif #ifndef STBI_WRITE_NO_STDIO @@ -143,7 +142,9 @@ STBIWDEF int stbi_write_jpg(char const *filename, int x, int y, int comp, const typedef void stbi_write_func(void *context, void *data, int size); +#if 0 /* not used in QuakeSpasm */ STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality); +#endif #ifdef __cplusplus } @@ -576,12 +577,14 @@ static int stbi_write_jpg_core(stbi__write_context *s, int width, int height, in return 1; } +#if 0 /* not used in QuakeSpasm */ STBIWDEF int stbi_write_jpg_to_func(stbi_write_func *func, void *context, int x, int y, int comp, const void *data, int quality) { stbi__write_context s; stbi__start_write_callbacks(&s, func, context); return stbi_write_jpg_core(&s, x, y, comp, (void *) data, quality); } +#endif #ifndef STBI_WRITE_NO_STDIO