mirror of
https://git.code.sf.net/p/libpng/code.git
synced 2025-07-10 18:04:09 +02:00
Revised some whitespace in commentary to look more like libpng's coding style.
This commit is contained in:
parent
a5ed4b0d2b
commit
51601ac0f1
145
pngcrush.c
145
pngcrush.c
@@ -1,4 +1,5 @@
|
|||||||
/* pngcrush.c - recompresses png files
|
/*
|
||||||
|
* pngcrush.c - recompresses png files
|
||||||
* Copyright (C) 1998-2002,2006-2009 Glenn Randers-Pehrson
|
* Copyright (C) 1998-2002,2006-2009 Glenn Randers-Pehrson
|
||||||
* (glennrp at users.sf.net)
|
* (glennrp at users.sf.net)
|
||||||
* Copyright (C) 2005 Greg Roelofs
|
* Copyright (C) 2005 Greg Roelofs
|
||||||
@@ -60,15 +61,19 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* COPYRIGHT NOTICE, DISCLAIMER, AND LICENSE:
|
* NOTICES
|
||||||
*
|
*
|
||||||
* If you have modified this source, you may insert additional notices
|
* If you have modified this source, you may insert additional notices
|
||||||
* immediately after this sentence.
|
* immediately after this sentence.
|
||||||
*
|
*
|
||||||
|
* COPYRIGHT:
|
||||||
|
*
|
||||||
* Copyright (C) 1998-2002,2006-2009 Glenn Randers-Pehrson
|
* Copyright (C) 1998-2002,2006-2009 Glenn Randers-Pehrson
|
||||||
* (glennrp at users.sf.net)
|
* (glennrp at users.sf.net)
|
||||||
* Copyright (C) 2005 Greg Roelofs
|
* Copyright (C) 2005 Greg Roelofs
|
||||||
*
|
*
|
||||||
|
* DISCLAIMERS:
|
||||||
|
*
|
||||||
* The pngcrush computer program is supplied "AS IS". The Author disclaims all
|
* The pngcrush computer program is supplied "AS IS". The Author disclaims all
|
||||||
* warranties, expressed or implied, including, without limitation, the
|
* warranties, expressed or implied, including, without limitation, the
|
||||||
* warranties of merchantability and of fitness for any purpose. The
|
* warranties of merchantability and of fitness for any purpose. The
|
||||||
@@ -82,6 +87,8 @@
|
|||||||
* risk of satisfactory quality, performance, accuracy, and effort is with
|
* risk of satisfactory quality, performance, accuracy, and effort is with
|
||||||
* the user.
|
* the user.
|
||||||
*
|
*
|
||||||
|
* LICENSE:
|
||||||
|
*
|
||||||
* Permission is hereby irrevocably granted to everyone to use, copy, modify,
|
* Permission is hereby irrevocably granted to everyone to use, copy, modify,
|
||||||
* and distribute this source code, or portions hereof, or executable programs
|
* and distribute this source code, or portions hereof, or executable programs
|
||||||
* compiled from it, for any purpose, without payment of any fee, subject to
|
* compiled from it, for any purpose, without payment of any fee, subject to
|
||||||
@@ -578,8 +585,10 @@ Version 1.1.4: added ability to restrict brute_force to one or more filter
|
|||||||
#ifdef PNG_LIBPNG_VER
|
#ifdef PNG_LIBPNG_VER
|
||||||
#define PNGCRUSH_LIBPNG_VER PNG_LIBPNG_VER
|
#define PNGCRUSH_LIBPNG_VER PNG_LIBPNG_VER
|
||||||
#else
|
#else
|
||||||
/* This must agree with PNG_LIBPNG_VER; you have to define it manually
|
/*
|
||||||
here if you are using libpng-1.0.6h or earlier */
|
* This must agree with PNG_LIBPNG_VER; you have to define it manually
|
||||||
|
* here if you are using libpng-1.0.6h or earlier
|
||||||
|
*/
|
||||||
#define PNGCRUSH_LIBPNG_VER 10007
|
#define PNGCRUSH_LIBPNG_VER 10007
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
@@ -787,10 +796,13 @@ Version 1.1.4: added ability to restrict brute_force to one or more filter
|
|||||||
#define PNG_USER_TRANSFORM 0x100000L
|
#define PNG_USER_TRANSFORM 0x100000L
|
||||||
#define PNG_RGB_TO_GRAY 0x600000L /* two bits, RGB_TO_GRAY_ERR|WARN */
|
#define PNG_RGB_TO_GRAY 0x600000L /* two bits, RGB_TO_GRAY_ERR|WARN */
|
||||||
|
|
||||||
/* we don't need some of the extra libpng transformations
|
/*
|
||||||
|
* We don't need some of the extra libpng transformations
|
||||||
* so they are ifdef'ed out in pngcrush.h, which is included by
|
* so they are ifdef'ed out in pngcrush.h, which is included by
|
||||||
* pngcrush's local copy of libpng's pngconf.h which is included
|
* pngcrush's local copy of libpng's pngconf.h which is included
|
||||||
* by png.h */
|
* by png.h
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
/* defined so I can write to a file on gui/windowing platforms */
|
/* defined so I can write to a file on gui/windowing platforms */
|
||||||
/* #define STDERR stderr */
|
/* #define STDERR stderr */
|
||||||
@@ -1126,7 +1138,8 @@ void print_usage(int retval);
|
|||||||
|
|
||||||
|
|
||||||
#if (!defined(PNGCRUSH_H))
|
#if (!defined(PNGCRUSH_H))
|
||||||
/* ============================================================
|
/*
|
||||||
|
* ============================================================
|
||||||
* We aren't using the bundled libpng functions, so we must
|
* We aren't using the bundled libpng functions, so we must
|
||||||
* reproduce the libpng routines that aren't exported by libpng
|
* reproduce the libpng routines that aren't exported by libpng
|
||||||
* ============================================================
|
* ============================================================
|
||||||
@@ -1164,7 +1177,8 @@ png_save_uint_32(png_bytep buf, png_uint_32 i)
|
|||||||
buf[3] = (png_byte)(i & 0xff);
|
buf[3] = (png_byte)(i & 0xff);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Reset the CRC variable to 32 bits of 1's. Care must be taken
|
/*
|
||||||
|
* Reset the CRC variable to 32 bits of 1's. Care must be taken
|
||||||
* in case CRC is > 32 bits to leave the top bits 0.
|
* in case CRC is > 32 bits to leave the top bits 0.
|
||||||
*/
|
*/
|
||||||
void /* PRIVATE */
|
void /* PRIVATE */
|
||||||
@@ -1172,7 +1186,8 @@ png_reset_crc(png_structp png_ptr)
|
|||||||
{
|
{
|
||||||
png_ptr->crc = crc32(0, Z_NULL, 0);
|
png_ptr->crc = crc32(0, Z_NULL, 0);
|
||||||
}
|
}
|
||||||
/* Calculate the CRC over a section of data. We can only pass as
|
/*
|
||||||
|
* Calculate the CRC over a section of data. We can only pass as
|
||||||
* much data to this routine as the largest single buffer size. We
|
* much data to this routine as the largest single buffer size. We
|
||||||
* also check that this data will actually be used before going to the
|
* also check that this data will actually be used before going to the
|
||||||
* trouble of calculating it.
|
* trouble of calculating it.
|
||||||
@@ -1238,10 +1253,12 @@ png_crc_error(png_structp png_ptr)
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Optionally skip data and then check the CRC. Depending on whether we
|
/*
|
||||||
are reading a ancillary or critical chunk, and how the program has set
|
* Optionally skip data and then check the CRC. Depending on whether we
|
||||||
things up, we may calculate the CRC on the data and print a message.
|
* are reading a ancillary or critical chunk, and how the program has set
|
||||||
Returns '1' if there was a CRC error, '0' otherwise. */
|
* things up, we may calculate the CRC on the data and print a message.
|
||||||
|
* Returns '1' if there was a CRC error, '0' otherwise.
|
||||||
|
*/
|
||||||
int /* PRIVATE */
|
int /* PRIVATE */
|
||||||
png_crc_finish(png_structp png_ptr, png_uint_32 skip)
|
png_crc_finish(png_structp png_ptr, png_uint_32 skip)
|
||||||
{
|
{
|
||||||
@@ -1276,7 +1293,8 @@ png_crc_finish(png_structp png_ptr, png_uint_32 skip)
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Modify the info structure to reflect the transformations. The
|
/*
|
||||||
|
* Modify the info structure to reflect the transformations. The
|
||||||
* info should be updated so a PNG file could be written with it,
|
* info should be updated so a PNG file could be written with it,
|
||||||
* assuming the transformations result in valid PNG data.
|
* assuming the transformations result in valid PNG data.
|
||||||
*/
|
*/
|
||||||
@@ -1430,17 +1448,20 @@ defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
|
|||||||
}
|
}
|
||||||
|
|
||||||
#if !defined(PNG_NO_STDIO)
|
#if !defined(PNG_NO_STDIO)
|
||||||
/* This is the function that does the actual reading of data. If you are
|
/*
|
||||||
not reading from a standard C stream, you should create a replacement
|
* This is the function that does the actual reading of data. If you are
|
||||||
read_data function and use it at run time with png_set_read_fn(), rather
|
* not reading from a standard C stream, you should create a replacement
|
||||||
than changing the library. */
|
* read_data function and use it at run time with png_set_read_fn(), rather
|
||||||
|
* than changing the library.
|
||||||
|
*/
|
||||||
#ifndef USE_FAR_KEYWORD
|
#ifndef USE_FAR_KEYWORD
|
||||||
void PNGAPI
|
void PNGAPI
|
||||||
png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
|
png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
|
||||||
{
|
{
|
||||||
png_size_t check;
|
png_size_t check;
|
||||||
|
|
||||||
/* fread() returns 0 on error, so it is OK to store this in a png_size_t
|
/*
|
||||||
|
* fread() returns 0 on error, so it is OK to store this in a png_size_t
|
||||||
* instead of an int, which is what fread() actually returns.
|
* instead of an int, which is what fread() actually returns.
|
||||||
*/
|
*/
|
||||||
#if defined(_WIN32_WCE)
|
#if defined(_WIN32_WCE)
|
||||||
@@ -1455,10 +1476,11 @@ png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
|
|||||||
png_error(png_ptr, "Read Error");
|
png_error(png_ptr, "Read Error");
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
/* this is the model-independent version. Since the standard I/O library
|
/*
|
||||||
can't handle far buffers in the medium and small models, we have to copy
|
* This is the model-independent version. Since the standard I/O library
|
||||||
the data.
|
* can't handle far buffers in the medium and small models, we have to copy
|
||||||
*/
|
* the data.
|
||||||
|
*/
|
||||||
|
|
||||||
#define NEAR_BUF_SIZE 1024
|
#define NEAR_BUF_SIZE 1024
|
||||||
#define MIN(a,b) (a <= b ? a : b)
|
#define MIN(a,b) (a <= b ? a : b)
|
||||||
@@ -1513,10 +1535,12 @@ png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
|
|||||||
#endif
|
#endif
|
||||||
#endif
|
#endif
|
||||||
#if !defined(PNG_NO_STDIO)
|
#if !defined(PNG_NO_STDIO)
|
||||||
/* This is the function that does the actual writing of data. If you are
|
/*
|
||||||
not writing to a standard C stream, you should create a replacement
|
* This is the function that does the actual writing of data. If you are
|
||||||
write_data function and use it at run time with png_set_write_fn(), rather
|
* not writing to a standard C stream, you should create a replacement
|
||||||
than changing the library. */
|
* write_data function and use it at run time with png_set_write_fn(), rather
|
||||||
|
* than changing the library.
|
||||||
|
*/
|
||||||
#ifndef USE_FAR_KEYWORD
|
#ifndef USE_FAR_KEYWORD
|
||||||
void PNGAPI
|
void PNGAPI
|
||||||
png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
|
png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
|
||||||
@@ -1533,9 +1557,10 @@ png_default_write_data(png_structp png_ptr, png_bytep data, png_size_t length)
|
|||||||
png_error(png_ptr, "Write Error");
|
png_error(png_ptr, "Write Error");
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
/* this is the model-independent version. Since the standard I/O library
|
/*
|
||||||
can't handle far buffers in the medium and small models, we have to copy
|
* This is the model-independent version. Since the standard I/O library
|
||||||
the data.
|
* can't handle far buffers in the medium and small models, we have to copy
|
||||||
|
* the data.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define NEAR_BUF_SIZE 1024
|
#define NEAR_BUF_SIZE 1024
|
||||||
@@ -1621,14 +1646,16 @@ static void png_cexcept_error(png_structp png_ptr, png_const_charp err_msg)
|
|||||||
/* START of code to validate memory allocation and deallocation */
|
/* START of code to validate memory allocation and deallocation */
|
||||||
#ifdef PNG_USER_MEM_SUPPORTED
|
#ifdef PNG_USER_MEM_SUPPORTED
|
||||||
|
|
||||||
/* Allocate memory. For reasonable files, size should never exceed
|
/*
|
||||||
64K. However, zlib may allocate more then 64K if you don't tell
|
* Allocate memory. For reasonable files, size should never exceed
|
||||||
it not to. See zconf.h and png.h for more information. zlib does
|
* 64K. However, zlib may allocate more then 64K if you don't tell
|
||||||
need to allocate exactly 64K, so whatever you call here must
|
* it not to. See zconf.h and png.h for more information. zlib does
|
||||||
have the ability to do that.
|
* need to allocate exactly 64K, so whatever you call here must
|
||||||
|
* have the ability to do that.
|
||||||
This piece of code can be compiled to validate max 64K allocations
|
*
|
||||||
by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K. */
|
* This piece of code can be compiled to validate max 64K allocations
|
||||||
|
* by setting MAXSEG_64K in zlib zconf.h *or* PNG_MAX_MALLOC_64K.
|
||||||
|
*/
|
||||||
typedef struct memory_information {
|
typedef struct memory_information {
|
||||||
png_uint_32 size;
|
png_uint_32 size;
|
||||||
png_voidp pointer;
|
png_voidp pointer;
|
||||||
@@ -1646,15 +1673,19 @@ static int maximum_allocation = 0;
|
|||||||
png_voidp png_debug_malloc(png_structp png_ptr, png_uint_32 size)
|
png_voidp png_debug_malloc(png_structp png_ptr, png_uint_32 size)
|
||||||
{
|
{
|
||||||
|
|
||||||
/* png_malloc has already tested for NULL; png_create_struct calls
|
/*
|
||||||
png_debug_malloc directly (with png_ptr == NULL prior to libpng-1.2.0
|
* png_malloc has already tested for NULL; png_create_struct calls
|
||||||
which is OK since we are not using a user mem_ptr) */
|
* png_debug_malloc directly (with png_ptr == NULL prior to libpng-1.2.0
|
||||||
|
* which is OK since we are not using a user mem_ptr)
|
||||||
|
*/
|
||||||
|
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
return (png_voidp) (NULL);
|
return (png_voidp) (NULL);
|
||||||
|
|
||||||
/* This calls the library allocator twice, once to get the requested
|
/*
|
||||||
buffer and once to get a new free list entry. */
|
* This calls the library allocator twice, once to get the requested
|
||||||
|
* buffer and once to get a new free list entry.
|
||||||
|
*/
|
||||||
{
|
{
|
||||||
memory_infop pinfo = (memory_infop)png_malloc_default(png_ptr,
|
memory_infop pinfo = (memory_infop)png_malloc_default(png_ptr,
|
||||||
sizeof *pinfo);
|
sizeof *pinfo);
|
||||||
@@ -1826,7 +1857,8 @@ static void setfiletype(const char *name)
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
/* GRR: basically boolean; first arg is chunk name-string (e.g., "tIME" or
|
/*
|
||||||
|
* GRR: basically boolean; first arg is chunk name-string (e.g., "tIME" or
|
||||||
* "alla"); second is always full argv[] command line
|
* "alla"); second is always full argv[] command line
|
||||||
* - remove_chunks is argv index of *last* -rem arg on command line
|
* - remove_chunks is argv index of *last* -rem arg on command line
|
||||||
* (would be more efficient to build table at time of cmdline processing!)
|
* (would be more efficient to build table at time of cmdline processing!)
|
||||||
@@ -2103,8 +2135,10 @@ int main(int argc, char *argv[])
|
|||||||
lv[9] = 2; zs[9] = 2; /* method 9 == method 16 */
|
lv[9] = 2; zs[9] = 2; /* method 9 == method 16 */
|
||||||
/* method 10 == method 124 */
|
/* method 10 == method 124 */
|
||||||
|
|
||||||
/* methods 11 through 16 */
|
/* methods 11 through 16
|
||||||
/* [strategy 2 (Z_HUFFMAN_ONLY) is independent of zlib compression level] */
|
*
|
||||||
|
* [strategy 2 (Z_HUFFMAN_ONLY) is independent of zlib compression level]
|
||||||
|
*/
|
||||||
method = 11;
|
method = 11;
|
||||||
for (filt = 0; filt <= 5; filt++) {
|
for (filt = 0; filt <= 5; filt++) {
|
||||||
fm[method] = filt;
|
fm[method] = filt;
|
||||||
@@ -2113,7 +2147,9 @@ int main(int argc, char *argv[])
|
|||||||
method++;
|
method++;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* methods 17 through 124 (9*2*6 = 108) */
|
/*
|
||||||
|
* methods 17 through 124 (9*2*6 = 108)
|
||||||
|
*/
|
||||||
for (lev = 1; lev <= 9; lev++) {
|
for (lev = 1; lev <= 9; lev++) {
|
||||||
for (strat = 0; strat <= 1; strat++) {
|
for (strat = 0; strat <= 1; strat++) {
|
||||||
for (filt = 0; filt <= 5; filt++) {
|
for (filt = 0; filt <= 5; filt++) {
|
||||||
@@ -2126,9 +2162,11 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
|
|
||||||
#ifdef Z_RLE
|
#ifdef Z_RLE
|
||||||
/* methods 125 through 136 */
|
/* methods 125 through 136
|
||||||
/* [strategy 3 (Z_RLE) is mostly independent of level; 1-3 and 4-9 are
|
*
|
||||||
* same] */
|
* [strategy 3 (Z_RLE) is mostly independent of level; 1-3 and 4-9 are
|
||||||
|
* same]
|
||||||
|
*/
|
||||||
for (filt = 0; filt <= 5; filt++) {
|
for (filt = 0; filt <= 5; filt++) {
|
||||||
fm[method] = filt;
|
fm[method] = filt;
|
||||||
lv[method] = 1;
|
lv[method] = 1;
|
||||||
@@ -2804,9 +2842,12 @@ int main(int argc, char *argv[])
|
|||||||
outname = out_string;
|
outname = out_string;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* FIXME: need same input-validation fixes (as above) here, too
|
/*
|
||||||
|
* FIXME: need same input-validation fixes (as above) here, too
|
||||||
|
*
|
||||||
* FIXME: what was the point of setting in_string and out_string in
|
* FIXME: what was the point of setting in_string and out_string in
|
||||||
* DIREX_MODE above if going to do all over again here? */
|
* DIREX_MODE above if going to do all over again here?
|
||||||
|
*/
|
||||||
if (pngcrush_mode == EXTENSION_MODE || pngcrush_mode == DIREX_MODE) {
|
if (pngcrush_mode == EXTENSION_MODE || pngcrush_mode == DIREX_MODE) {
|
||||||
ip = in_string;
|
ip = in_string;
|
||||||
in_string[0] = '\0';
|
in_string[0] = '\0';
|
||||||
|
|||||||
Reference in New Issue
Block a user