提交 3f0f1d55 编写于 作者: C Cosmin Truta

pngminus: Improve portability and fix style (cont'd)

上级 dcefbc7d
/* /*
* pnm2png.c --- conversion from PBM/PGM/PPM-file to PNG-file * pnm2png.c --- conversion from PBM/PGM/PPM-file to PNG-file
* copyright (C) 1999,2015,2017 by Willem van Schaik <willem at schaik.com> * copyright (C) 1999,2015,2017,2018 by Willem van Schaik <willem at schaik.com>
* *
* version 1.0 - 1999.10.15 - First version. * version 1.0 - 1999.10.15 - First version.
* version 1.1 - 2015.07.29 - Fixed leaks (Glenn Randers-Pehrson) * 1.1 - 2015.07.29 - Fixed leaks (Glenn Randers-Pehrson)
* version 1.2 - 2017.04.22 - Add buffer-size check * 1.2 - 2017.04.22 - Add buffer-size check
* 1.3 - 2017.08.24 - Fix potential overflow in buffer-size check * 1.3 - 2017.08.24 - Fix potential overflow in buffer-size check
* (Glenn Randers-Pehrson) * (Glenn Randers-Pehrson)
* 1.4 - 2017.08.28 - Add PNGMINUS_UNUSED (Christian Hesse) * 1.4 - 2017.08.28 - Add PNGMINUS_UNUSED (Christian Hesse)
* 1.5 - 2018.08.05 - Fix buffer overflow in tokenizer (Cosmin Truta) * 1.5 - 2018.08.05 - Fix buffer overflow in tokenizer (Cosmin Truta)
* 1.6 - 2018.08.05 - Improve portability and fix style (Cosmin Truta)
* *
* Permission to use, copy, modify, and distribute this software and * Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted, * its documentation for any purpose and without fee is hereby granted,
...@@ -20,11 +21,7 @@ ...@@ -20,11 +21,7 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#ifdef __TURBOC__
#include <mem.h>
#include <fcntl.h> #include <fcntl.h>
#endif
#include <zlib.h>
#ifndef BOOL #ifndef BOOL
#define BOOL unsigned char #define BOOL unsigned char
...@@ -36,38 +33,20 @@ ...@@ -36,38 +33,20 @@
#define FALSE (BOOL) 0 #define FALSE (BOOL) 0
#endif #endif
#define STDIN 0 /* make pnm2png verbose so we can find problems (needs to be before png.h) */
#define STDOUT 1
#define STDERR 2
/* to make pnm2png verbose so we can find problems (needs to be before png.h) */
#ifndef PNG_DEBUG #ifndef PNG_DEBUG
#define PNG_DEBUG 0 #define PNG_DEBUG 0
#endif #endif
#include "png.h" #include "png.h"
/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
#ifndef png_jmpbuf
# define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
#endif
#ifndef PNGMINUS_UNUSED
/* Unused formal parameter warnings are silenced using the following macro
* which is expected to have no bad effects on performance (optimizing
* compilers will probably remove it entirely).
*/
# define PNGMINUS_UNUSED(param) (void)param
#endif
/* function prototypes */ /* function prototypes */
int main (int argc, char *argv[]); int main (int argc, char *argv[]);
void usage (); void usage ();
BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file,
BOOL alpha); BOOL interlace, BOOL alpha);
void get_token(FILE *pnm_file, char *token_buf, size_t token_buf_size); void get_token (FILE *pnm_file, char *token_buf, size_t token_buf_size);
png_uint_32 get_data (FILE *pnm_file, int depth); png_uint_32 get_data (FILE *pnm_file, int depth);
png_uint_32 get_value (FILE *pnm_file, int depth); png_uint_32 get_value (FILE *pnm_file, int depth);
...@@ -75,7 +54,7 @@ png_uint_32 get_value (FILE *pnm_file, int depth); ...@@ -75,7 +54,7 @@ png_uint_32 get_value (FILE *pnm_file, int depth);
* main * main
*/ */
int main(int argc, char *argv[]) int main (int argc, char *argv[])
{ {
FILE *fp_rd = stdin; FILE *fp_rd = stdin;
FILE *fp_al = NULL; FILE *fp_al = NULL;
...@@ -100,20 +79,20 @@ int main(int argc, char *argv[]) ...@@ -100,20 +79,20 @@ int main(int argc, char *argv[])
{ {
fprintf (stderr, "PNM2PNG\n"); fprintf (stderr, "PNM2PNG\n");
fprintf (stderr, "Error: alpha-channel file %s does not exist\n", fprintf (stderr, "Error: alpha-channel file %s does not exist\n",
argv[argi]); argv[argi]);
exit (1); exit (1);
} }
break; break;
case 'h': case 'h':
case '?': case '?':
usage(); usage ();
exit(0); exit (0);
break; break;
default: default:
fprintf (stderr, "PNM2PNG\n"); fprintf (stderr, "PNM2PNG\n");
fprintf (stderr, "Error: unknown option %s\n", argv[argi]); fprintf (stderr, "Error: unknown option %s\n", argv[argi]);
usage(); usage ();
exit(1); exit (1);
break; break;
} /* end switch */ } /* end switch */
} }
...@@ -131,7 +110,7 @@ int main(int argc, char *argv[]) ...@@ -131,7 +110,7 @@ int main(int argc, char *argv[])
if ((fp_wr = fopen (argv[argi], "wb")) == NULL) if ((fp_wr = fopen (argv[argi], "wb")) == NULL)
{ {
fprintf (stderr, "PNM2PNG\n"); fprintf (stderr, "PNM2PNG\n");
fprintf (stderr, "Error: can not create PNG-file %s\n", argv[argi]); fprintf (stderr, "Error: cannot create PNG-file %s\n", argv[argi]);
exit (1); exit (1);
} }
} }
...@@ -139,21 +118,19 @@ int main(int argc, char *argv[]) ...@@ -139,21 +118,19 @@ int main(int argc, char *argv[])
{ {
fprintf (stderr, "PNM2PNG\n"); fprintf (stderr, "PNM2PNG\n");
fprintf (stderr, "Error: too many parameters\n"); fprintf (stderr, "Error: too many parameters\n");
usage(); usage ();
exit (1); exit (1);
} }
} /* end for */ } /* end for */
#ifdef __TURBOC__ #if defined(O_BINARY) && (O_BINARY != 0)
/* set stdin/stdout to binary, we're reading the PNM always! in binary format */ /* set stdin/stdout to binary,
* we're reading the PNM always! in binary format
*/
if (fp_rd == stdin) if (fp_rd == stdin)
{ setmode (fileno (stdin), O_BINARY);
setmode (STDIN, O_BINARY);
}
if (fp_wr == stdout) if (fp_wr == stdout)
{ setmode (fileno (stdout), O_BINARY);
setmode (STDOUT, O_BINARY);
}
#endif #endif
/* call the conversion program itself */ /* call the conversion program itself */
...@@ -179,15 +156,10 @@ int main(int argc, char *argv[]) ...@@ -179,15 +156,10 @@ int main(int argc, char *argv[])
* usage * usage
*/ */
void usage() void usage ()
{ {
fprintf (stderr, "PNM2PNG\n"); fprintf (stderr, "PNM2PNG\n");
fprintf (stderr, " by Willem van Schaik, 1999\n"); fprintf (stderr, " by Willem van Schaik, 1999\n");
#ifdef __TURBOC__
fprintf (stderr, " for Turbo-C and Borland-C compilers\n");
#else
fprintf (stderr, " for Linux (and Unix) compilers\n");
#endif
fprintf (stderr, "Usage: pnm2png [options] <file>.<pnm> [<file>.png]\n"); fprintf (stderr, "Usage: pnm2png [options] <file>.<pnm> [<file>.png]\n");
fprintf (stderr, " or: ... | pnm2png [options]\n"); fprintf (stderr, " or: ... | pnm2png [options]\n");
fprintf (stderr, "Options:\n"); fprintf (stderr, "Options:\n");
...@@ -201,31 +173,31 @@ void usage() ...@@ -201,31 +173,31 @@ void usage()
* pnm2png * pnm2png
*/ */
BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file,
BOOL alpha) BOOL interlace, BOOL alpha)
{ {
png_struct *png_ptr = NULL; png_struct *png_ptr = NULL;
png_info *info_ptr = NULL; png_info *info_ptr = NULL;
png_byte *png_pixels = NULL; png_byte *png_pixels = NULL;
png_byte **row_pointers = NULL; png_byte **row_pointers = NULL;
png_byte *pix_ptr = NULL; png_byte *pix_ptr = NULL;
volatile png_uint_32 row_bytes; volatile png_uint_32 row_bytes;
char type_token[16]; char type_token[16];
char width_token[16]; char width_token[16];
char height_token[16]; char height_token[16];
char maxval_token[16]; char maxval_token[16];
volatile int color_type=1; volatile int color_type = 1;
unsigned long ul_width=0, ul_alpha_width=0; unsigned long ul_width = 0, ul_alpha_width = 0;
unsigned long ul_height=0, ul_alpha_height=0; unsigned long ul_height = 0, ul_alpha_height = 0;
unsigned long ul_maxval=0; unsigned long ul_maxval = 0;
volatile png_uint_32 width=0, height=0; volatile png_uint_32 width = 0, height = 0;
volatile png_uint_32 alpha_width=0, alpha_height=0; volatile png_uint_32 alpha_width = 0, alpha_height = 0;
png_uint_32 maxval; png_uint_32 maxval;
volatile int bit_depth = 0; volatile int bit_depth = 0;
int channels=0; int channels = 0;
int alpha_depth = 0; int alpha_depth = 0;
int alpha_present=0; int alpha_present = 0;
int row, col; int row, col;
BOOL raw, alpha_raw = FALSE; BOOL raw, alpha_raw = FALSE;
#if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED) #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
...@@ -236,7 +208,7 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, ...@@ -236,7 +208,7 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
/* read header of PNM file */ /* read header of PNM file */
get_token(pnm_file, type_token, sizeof (type_token)); get_token (pnm_file, type_token, sizeof (type_token));
if (type_token[0] != 'P') if (type_token[0] != 'P')
{ {
return FALSE; return FALSE;
...@@ -246,30 +218,31 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, ...@@ -246,30 +218,31 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
#if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED) #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
raw = (type_token[1] == '4'); raw = (type_token[1] == '4');
color_type = PNG_COLOR_TYPE_GRAY; color_type = PNG_COLOR_TYPE_GRAY;
get_token(pnm_file, width_token, sizeof (width_token)); get_token (pnm_file, width_token, sizeof (width_token));
sscanf (width_token, "%lu", &ul_width); sscanf (width_token, "%lu", &ul_width);
width = (png_uint_32) ul_width; width = (png_uint_32) ul_width;
get_token(pnm_file, height_token, sizeof (height_token)); get_token (pnm_file, height_token, sizeof (height_token));
sscanf (height_token, "%lu", &ul_height); sscanf (height_token, "%lu", &ul_height);
height = (png_uint_32) ul_height; height = (png_uint_32) ul_height;
bit_depth = 1; bit_depth = 1;
packed_bitmap = TRUE; packed_bitmap = TRUE;
#else #else
fprintf (stderr, "PNM2PNG built without PNG_WRITE_INVERT_SUPPORTED and \n"); fprintf (stderr, "PNM2PNG built without PNG_WRITE_INVERT_SUPPORTED and\n");
fprintf (stderr, "PNG_WRITE_PACK_SUPPORTED can't read PBM (P1,P4) files\n"); fprintf (stderr, "PNG_WRITE_PACK_SUPPORTED can't read PBM (P1,P4) files\n");
return FALSE;
#endif #endif
} }
else if ((type_token[1] == '2') || (type_token[1] == '5')) else if ((type_token[1] == '2') || (type_token[1] == '5'))
{ {
raw = (type_token[1] == '5'); raw = (type_token[1] == '5');
color_type = PNG_COLOR_TYPE_GRAY; color_type = PNG_COLOR_TYPE_GRAY;
get_token(pnm_file, width_token, sizeof (width_token)); get_token (pnm_file, width_token, sizeof (width_token));
sscanf (width_token, "%lu", &ul_width); sscanf (width_token, "%lu", &ul_width);
width = (png_uint_32) ul_width; width = (png_uint_32) ul_width;
get_token(pnm_file, height_token, sizeof (height_token)); get_token (pnm_file, height_token, sizeof (height_token));
sscanf (height_token, "%lu", &ul_height); sscanf (height_token, "%lu", &ul_height);
height = (png_uint_32) ul_height; height = (png_uint_32) ul_height;
get_token(pnm_file, maxval_token, sizeof (maxval_token)); get_token (pnm_file, maxval_token, sizeof (maxval_token));
sscanf (maxval_token, "%lu", &ul_maxval); sscanf (maxval_token, "%lu", &ul_maxval);
maxval = (png_uint_32) ul_maxval; maxval = (png_uint_32) ul_maxval;
...@@ -281,20 +254,22 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, ...@@ -281,20 +254,22 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
bit_depth = 4; bit_depth = 4;
else if (maxval <= 255) else if (maxval <= 255)
bit_depth = 8; bit_depth = 8;
else /* if (maxval <= 65535) */ else if (maxval <= 65535U)
bit_depth = 16; bit_depth = 16;
else /* maxval > 65535U */
return FALSE;
} }
else if ((type_token[1] == '3') || (type_token[1] == '6')) else if ((type_token[1] == '3') || (type_token[1] == '6'))
{ {
raw = (type_token[1] == '6'); raw = (type_token[1] == '6');
color_type = PNG_COLOR_TYPE_RGB; color_type = PNG_COLOR_TYPE_RGB;
get_token(pnm_file, width_token, sizeof (width_token)); get_token (pnm_file, width_token, sizeof (width_token));
sscanf (width_token, "%lu", &ul_width); sscanf (width_token, "%lu", &ul_width);
width = (png_uint_32) ul_width; width = (png_uint_32) ul_width;
get_token(pnm_file, height_token, sizeof (height_token)); get_token (pnm_file, height_token, sizeof (height_token));
sscanf (height_token, "%lu", &ul_height); sscanf (height_token, "%lu", &ul_height);
height = (png_uint_32) ul_height; height = (png_uint_32) ul_height;
get_token(pnm_file, maxval_token, sizeof (maxval_token)); get_token (pnm_file, maxval_token, sizeof (maxval_token));
sscanf (maxval_token, "%lu", &ul_maxval); sscanf (maxval_token, "%lu", &ul_maxval);
maxval = (png_uint_32) ul_maxval; maxval = (png_uint_32) ul_maxval;
if (maxval <= 1) if (maxval <= 1)
...@@ -305,8 +280,10 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, ...@@ -305,8 +280,10 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
bit_depth = 4; bit_depth = 4;
else if (maxval <= 255) else if (maxval <= 255)
bit_depth = 8; bit_depth = 8;
else /* if (maxval <= 65535) */ else if (maxval <= 65535U)
bit_depth = 16; bit_depth = 16;
else /* maxval > 65535U */
return FALSE;
} }
else else
{ {
...@@ -322,7 +299,7 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, ...@@ -322,7 +299,7 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
if (color_type == PNG_COLOR_TYPE_RGB) if (color_type == PNG_COLOR_TYPE_RGB)
color_type = PNG_COLOR_TYPE_RGB_ALPHA; color_type = PNG_COLOR_TYPE_RGB_ALPHA;
get_token(alpha_file, type_token, sizeof (type_token)); get_token (alpha_file, type_token, sizeof (type_token));
if (type_token[0] != 'P') if (type_token[0] != 'P')
{ {
return FALSE; return FALSE;
...@@ -330,17 +307,17 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, ...@@ -330,17 +307,17 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
else if ((type_token[1] == '2') || (type_token[1] == '5')) else if ((type_token[1] == '2') || (type_token[1] == '5'))
{ {
alpha_raw = (type_token[1] == '5'); alpha_raw = (type_token[1] == '5');
get_token(alpha_file, width_token, sizeof (width_token)); get_token (alpha_file, width_token, sizeof (width_token));
sscanf (width_token, "%lu", &ul_alpha_width); sscanf (width_token, "%lu", &ul_alpha_width);
alpha_width=(png_uint_32) ul_alpha_width; alpha_width = (png_uint_32) ul_alpha_width;
if (alpha_width != width) if (alpha_width != width)
return FALSE; return FALSE;
get_token(alpha_file, height_token, sizeof (height_token)); get_token (alpha_file, height_token, sizeof (height_token));
sscanf (height_token, "%lu", &ul_alpha_height); sscanf (height_token, "%lu", &ul_alpha_height);
alpha_height = (png_uint_32) ul_alpha_height; alpha_height = (png_uint_32) ul_alpha_height;
if (alpha_height != height) if (alpha_height != height)
return FALSE; return FALSE;
get_token(alpha_file, maxval_token, sizeof (maxval_token)); get_token (alpha_file, maxval_token, sizeof (maxval_token));
sscanf (maxval_token, "%lu", &ul_maxval); sscanf (maxval_token, "%lu", &ul_maxval);
maxval = (png_uint_32) ul_maxval; maxval = (png_uint_32) ul_maxval;
if (maxval <= 1) if (maxval <= 1)
...@@ -351,8 +328,10 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, ...@@ -351,8 +328,10 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
alpha_depth = 4; alpha_depth = 4;
else if (maxval <= 255) else if (maxval <= 255)
alpha_depth = 8; alpha_depth = 8;
else /* if (maxval <= 65535) */ else if (maxval <= 65535U)
alpha_depth = 16; alpha_depth = 16;
else /* maxval > 65535U */
return FALSE;
if (alpha_depth != bit_depth) if (alpha_depth != bit_depth)
return FALSE; return FALSE;
} }
...@@ -380,21 +359,29 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, ...@@ -380,21 +359,29 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
#if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED) #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
if (packed_bitmap) if (packed_bitmap)
{
/* row data is as many bytes as can fit width x channels x bit_depth */ /* row data is as many bytes as can fit width x channels x bit_depth */
row_bytes = (width * channels * bit_depth + 7) / 8; row_bytes = (width * channels * bit_depth + 7) / 8;
}
else else
#endif #endif
/* row_bytes is the width x number of channels x (bit-depth / 8) */ {
/* row_bytes is the width x number of channels x (bit-depth / 8) */
row_bytes = width * channels * ((bit_depth <= 8) ? 1 : 2); row_bytes = width * channels * ((bit_depth <= 8) ? 1 : 2);
}
if ((row_bytes == 0 || (size_t)height > ((size_t)(-1))/(size_t)row_bytes)) if ((row_bytes == 0) ||
((size_t) height > (size_t) (-1) / (size_t) row_bytes))
{ {
/* too big */ /* too big */
return FALSE; return FALSE;
} }
if ((png_pixels = (png_byte *) if ((png_pixels = (png_byte *)
malloc ((size_t)row_bytes * (size_t)height * sizeof (png_byte))) == NULL) malloc ((size_t) row_bytes * (size_t) height)) == NULL)
{
/* out of memory */
return FALSE; return FALSE;
}
/* read data from PNM file */ /* read data from PNM file */
pix_ptr = png_pixels; pix_ptr = png_pixels;
...@@ -405,9 +392,12 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, ...@@ -405,9 +392,12 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
if (packed_bitmap) if (packed_bitmap)
{ {
for (i = 0; i < (int) row_bytes; i++) for (i = 0; i < (int) row_bytes; i++)
{
/* png supports this format natively so no conversion is needed */ /* png supports this format natively so no conversion is needed */
*pix_ptr++ = get_data (pnm_file, 8); *pix_ptr++ = get_data (pnm_file, 8);
} else }
}
else
#endif #endif
{ {
for (col = 0; col < (int) width; col++) for (col = 0; col < (int) width; col++)
...@@ -415,10 +405,15 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, ...@@ -415,10 +405,15 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
for (i = 0; i < (channels - alpha_present); i++) for (i = 0; i < (channels - alpha_present); i++)
{ {
if (raw) if (raw)
{
*pix_ptr++ = get_data (pnm_file, bit_depth); *pix_ptr++ = get_data (pnm_file, bit_depth);
}
else else
{
if (bit_depth <= 8) if (bit_depth <= 8)
{
*pix_ptr++ = get_value (pnm_file, bit_depth); *pix_ptr++ = get_value (pnm_file, bit_depth);
}
else else
{ {
tmp16 = get_value (pnm_file, bit_depth); tmp16 = get_value (pnm_file, bit_depth);
...@@ -427,41 +422,46 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, ...@@ -427,41 +422,46 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
*pix_ptr = (png_byte) (tmp16 & 0xFF); *pix_ptr = (png_byte) (tmp16 & 0xFF);
pix_ptr++; pix_ptr++;
} }
}
} }
if (alpha) /* read alpha-channel from pgm file */ if (alpha) /* read alpha-channel from pgm file */
{ {
if (alpha_raw) if (alpha_raw)
{
*pix_ptr++ = get_data (alpha_file, alpha_depth); *pix_ptr++ = get_data (alpha_file, alpha_depth);
}
else else
{
if (alpha_depth <= 8) if (alpha_depth <= 8)
{
*pix_ptr++ = get_value (alpha_file, bit_depth); *pix_ptr++ = get_value (alpha_file, bit_depth);
}
else else
{ {
tmp16 = get_value (alpha_file, bit_depth); tmp16 = get_value (alpha_file, bit_depth);
*pix_ptr++ = (png_byte) ((tmp16 >> 8) & 0xFF); *pix_ptr++ = (png_byte) ((tmp16 >> 8) & 0xFF);
*pix_ptr++ = (png_byte) (tmp16 & 0xFF); *pix_ptr++ = (png_byte) (tmp16 & 0xFF);
} }
} /* if alpha */ }
} /* if packed_bitmap */ } /* end if alpha */
} /* end if packed_bitmap */
} /* end for col */ } /* end for col */
} /* end for row */ } /* end for row */
/* prepare the standard PNG structures */ /* prepare the standard PNG structures */
png_ptr = png_create_write_struct (png_get_libpng_ver(NULL), NULL, NULL, png_ptr = png_create_write_struct (png_get_libpng_ver(NULL),
NULL); NULL, NULL, NULL);
if (!png_ptr) if (!png_ptr)
{ {
free (png_pixels); free (png_pixels);
png_pixels = NULL;
return FALSE; return FALSE;
} }
info_ptr = png_create_info_struct (png_ptr); info_ptr = png_create_info_struct (png_ptr);
if (!info_ptr) if (!info_ptr)
{ {
png_destroy_write_struct (&png_ptr, (png_infopp) NULL); png_destroy_write_struct (&png_ptr, NULL);
free (png_pixels); free (png_pixels);
png_pixels = NULL;
return FALSE; return FALSE;
} }
...@@ -473,12 +473,10 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, ...@@ -473,12 +473,10 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
} }
#endif #endif
/* setjmp() must be called in every function that calls a PNG-reading libpng function */ if (setjmp (png_jmpbuf (png_ptr)))
if (setjmp (png_jmpbuf(png_ptr)))
{ {
png_destroy_write_struct (&png_ptr, &info_ptr); png_destroy_write_struct (&png_ptr, &info_ptr);
free (png_pixels); free (png_pixels);
png_pixels = NULL;
return FALSE; return FALSE;
} }
...@@ -487,21 +485,20 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, ...@@ -487,21 +485,20 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
/* we're going to write more or less the same PNG as the input file */ /* we're going to write more or less the same PNG as the input file */
png_set_IHDR (png_ptr, info_ptr, width, height, bit_depth, color_type, png_set_IHDR (png_ptr, info_ptr, width, height, bit_depth, color_type,
(!interlace) ? PNG_INTERLACE_NONE : PNG_INTERLACE_ADAM7, (!interlace) ? PNG_INTERLACE_NONE : PNG_INTERLACE_ADAM7,
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
/* write the file header information */ /* write the file header information */
png_write_info (png_ptr, info_ptr); png_write_info (png_ptr, info_ptr);
/* if needed we will allocate memory for an new array of row-pointers */ /* if needed we will allocate memory for an new array of row-pointers */
if (row_pointers == (unsigned char**) NULL) if (row_pointers == NULL)
{ {
if ((row_pointers = (png_byte **) if ((row_pointers = (png_byte **)
malloc (height * sizeof (png_bytep))) == NULL) malloc (height * sizeof (png_byte *))) == NULL)
{ {
png_destroy_write_struct (&png_ptr, &info_ptr); png_destroy_write_struct (&png_ptr, &info_ptr);
free (png_pixels); free (png_pixels);
png_pixels = NULL;
return FALSE; return FALSE;
} }
} }
...@@ -519,21 +516,19 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, ...@@ -519,21 +516,19 @@ BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
/* clean up after the write, and free any memory allocated */ /* clean up after the write, and free any memory allocated */
png_destroy_write_struct (&png_ptr, &info_ptr); png_destroy_write_struct (&png_ptr, &info_ptr);
if (row_pointers != (unsigned char**) NULL) if (row_pointers != NULL)
free (row_pointers); free (row_pointers);
if (png_pixels != (unsigned char*) NULL) if (png_pixels != NULL)
free (png_pixels); free (png_pixels);
PNGMINUS_UNUSED(raw); /* Quiet a Coverity defect */
return TRUE; return TRUE;
} /* end of pnm2png */ } /* end of pnm2png */
/* /*
* get_token() - gets the first string after whitespace * get_token - gets the first string after whitespace
*/ */
void get_token(FILE *pnm_file, char *token_buf, size_t token_buf_size) void get_token (FILE *pnm_file, char *token_buf, size_t token_buf_size)
{ {
size_t i = 0; size_t i = 0;
int ret; int ret;
...@@ -541,13 +536,13 @@ void get_token(FILE *pnm_file, char *token_buf, size_t token_buf_size) ...@@ -541,13 +536,13 @@ void get_token(FILE *pnm_file, char *token_buf, size_t token_buf_size)
/* remove white-space and comment lines */ /* remove white-space and comment lines */
do do
{ {
ret = fgetc(pnm_file); ret = fgetc (pnm_file);
if (ret == '#') if (ret == '#')
{ {
/* the rest of this line is a comment */ /* the rest of this line is a comment */
do do
{ {
ret = fgetc(pnm_file); ret = fgetc (pnm_file);
} }
while ((ret != '\n') && (ret != '\r') && (ret != EOF)); while ((ret != '\n') && (ret != '\r') && (ret != EOF));
} }
...@@ -559,7 +554,7 @@ void get_token(FILE *pnm_file, char *token_buf, size_t token_buf_size) ...@@ -559,7 +554,7 @@ void get_token(FILE *pnm_file, char *token_buf, size_t token_buf_size)
/* read string */ /* read string */
do do
{ {
ret = fgetc(pnm_file); ret = fgetc (pnm_file);
if (ret == EOF) break; if (ret == EOF) break;
if (++i == token_buf_size - 1) break; if (++i == token_buf_size - 1) break;
token_buf[i] = (char) ret; token_buf[i] = (char) ret;
...@@ -572,9 +567,9 @@ void get_token(FILE *pnm_file, char *token_buf, size_t token_buf_size) ...@@ -572,9 +567,9 @@ void get_token(FILE *pnm_file, char *token_buf, size_t token_buf_size)
} }
/* /*
* get_data() - takes first byte and converts into next pixel value, * get_data - takes first byte and converts into next pixel value,
* taking as much bits as defined by bit-depth and * taking as much bits as defined by bit-depth and
* using the bit-depth to fill up a byte (0Ah -> AAh) * using the bit-depth to fill up a byte (0Ah -> AAh)
*/ */
png_uint_32 get_data (FILE *pnm_file, int depth) png_uint_32 get_data (FILE *pnm_file, int depth)
...@@ -606,8 +601,8 @@ png_uint_32 get_data (FILE *pnm_file, int depth) ...@@ -606,8 +601,8 @@ png_uint_32 get_data (FILE *pnm_file, int depth)
} }
/* /*
* get_value() - takes first (numeric) string and converts into number, * get_value - takes first (numeric) string and converts into number,
* using the bit-depth to fill up a byte (0Ah -> AAh) * using the bit-depth to fill up a byte (0Ah -> AAh)
*/ */
png_uint_32 get_value (FILE *pnm_file, int depth) png_uint_32 get_value (FILE *pnm_file, int depth)
...@@ -636,4 +631,3 @@ png_uint_32 get_value (FILE *pnm_file, int depth) ...@@ -636,4 +631,3 @@ png_uint_32 get_value (FILE *pnm_file, int depth)
} }
/* end of source */ /* end of source */
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册