Skip to content

Commit

Permalink
Some more image.c / lodepng / stb_image_write clean-ups:
Browse files Browse the repository at this point in the history
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: lvandeve/lodepng#34).
lodepng.c (inflateHuffmanBlock): wrong error code if huffmanDecodeSymbol
 fail. (see: lvandeve/lodepng#52).

git-svn-id: svn://svn.code.sf.net/p/quakespasm/code/trunk/quakespasm@1471 af15c1b1-3010-417e-b628-4374ebc0bcbd
  • Loading branch information
sezero committed Aug 9, 2017
1 parent 642c353 commit 3ae0229
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 32 deletions.
18 changes: 9 additions & 9 deletions Quake/image.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -550,29 +552,28 @@ 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)
free (flipped);
free (filters);
return false;
}

// set some options for faster compression
lodepng_state_init(&state);
state.encoder.zlibsettings.use_lz77 = 0;
state.encoder.auto_convert = 0;
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;
Expand All @@ -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);
}
37 changes: 15 additions & 22 deletions Quake/lodepng.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)*/
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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*/
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -6160,6 +6145,14 @@ unsigned decode(std::vector<unsigned char>& 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<unsigned char>& out, const unsigned char* in, unsigned w, unsigned h,
LodePNGColorType colortype, unsigned bitdepth)
{
Expand Down
5 changes: 4 additions & 1 deletion Quake/stb_image_write.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3ae0229

Please sign in to comment.