pngminus: Improve portability and fix style

This commit is contained in:
Cosmin Truta 2018-08-05 21:44:11 -04:00
parent 1f0221fad7
commit dcefbc7dcd

View File

@ -1,12 +1,13 @@
/*
* png2pnm.c --- conversion from PNG-file to PGM/PPM-file
* copyright (C) 1999,2017 by Willem van Schaik <willem at schaik.com>
* copyright (C) 1999,2017,2018 by Willem van Schaik <willem at schaik.com>
*
* version 1.0 - 1999.10.15 - First version.
* 1.1 - 2017.04.22 - Add buffer-size check (Glenn Randers-Pehrson)
* 1.2 - 2017.08.24 - Fix potential overflow in buffer-size check
* (Glenn Randers-Pehrson)
* 1.3 - 2017.08.28 - Add PNGMINUS_UNUSED (Christian Hesse)
* 1.4 - 2018.08.05 - Improve portability and fix style (Cosmin Truta)
*
* Permission to use, copy, modify, and distribute this software and
* its documentation for any purpose and without fee is hereby granted,
@ -18,11 +19,7 @@
#include <stdio.h>
#include <stdlib.h>
#ifdef __TURBOC__
#include <mem.h>
#include <fcntl.h>
#endif
#include <zlib.h>
#ifndef BOOL
#define BOOL unsigned char
@ -34,45 +31,25 @@
#define FALSE (BOOL) 0
#endif
#ifdef __TURBOC__
#define STDIN 0
#define STDOUT 1
#define STDERR 2
#endif
/* to make png2pnm verbose so we can find problems (needs to be before png.h) */
/* make png2pnm verbose so we can find problems (needs to be before png.h) */
#ifndef PNG_DEBUG
#define PNG_DEBUG 0
#endif
#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 */
int main (int argc, char *argv[]);
void usage ();
BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file, BOOL raw,
BOOL alpha);
BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file,
BOOL raw, BOOL alpha);
/*
* main
*/
int main(int argc, char *argv[])
int main (int argc, char *argv[])
{
FILE *fp_rd = stdin;
FILE *fp_wr = stdout;
@ -99,21 +76,21 @@ int main(int argc, char *argv[])
if ((fp_al = fopen (argv[argi], "wb")) == NULL)
{
fprintf (stderr, "PNM2PNG\n");
fprintf (stderr, "Error: can not create alpha-channel file %s\n",
fprintf (stderr, "Error: cannot create alpha-channel file %s\n",
argv[argi]);
exit (1);
}
break;
case 'h':
case '?':
usage();
exit(0);
usage ();
exit (0);
break;
default:
fprintf (stderr, "PNG2PNM\n");
fprintf (stderr, "Error: unknown option %s\n", argv[argi]);
usage();
exit(1);
usage ();
exit (1);
break;
} /* end switch */
}
@ -131,7 +108,7 @@ int main(int argc, char *argv[])
if ((fp_wr = fopen (argv[argi], "wb")) == NULL)
{
fprintf (stderr, "PNG2PNM\n");
fprintf (stderr, "Error: can not create file %s\n", argv[argi]);
fprintf (stderr, "Error: cannot create file %s\n", argv[argi]);
exit (1);
}
}
@ -139,21 +116,17 @@ int main(int argc, char *argv[])
{
fprintf (stderr, "PNG2PNM\n");
fprintf (stderr, "Error: too many parameters\n");
usage();
exit(1);
usage ();
exit (1);
}
} /* end for */
#ifdef __TURBOC__
#if defined(O_BINARY) && (O_BINARY != 0)
/* set stdin/stdout if required to binary */
if (fp_rd == stdin)
{
setmode (STDIN, O_BINARY);
}
setmode (fileno (stdin), O_BINARY);
if ((raw) && (fp_wr == stdout))
{
setmode (STDOUT, O_BINARY);
}
setmode (fileno (stdout), O_BINARY);
#endif
/* call the conversion program itself */
@ -161,7 +134,7 @@ int main(int argc, char *argv[])
{
fprintf (stderr, "PNG2PNM\n");
fprintf (stderr, "Error: unsuccessful conversion of PNG-image\n");
exit(1);
exit (1);
}
/* close input file */
@ -179,15 +152,10 @@ int main(int argc, char *argv[])
* usage
*/
void usage()
void usage ()
{
fprintf (stderr, "PNG2PNM\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: png2pnm [options] <file>.png [<file>.pnm]\n");
fprintf (stderr, " or: ... | png2pnm [options]\n");
fprintf (stderr, "Options:\n");
@ -204,7 +172,7 @@ void usage()
*/
BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file,
volatile BOOL raw, BOOL alpha)
BOOL raw, BOOL alpha)
{
png_struct *png_ptr = NULL;
png_info *info_ptr = NULL;
@ -231,7 +199,7 @@ BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file,
return FALSE;
ret = png_sig_cmp (buf, 0, 8);
if (ret)
if (ret != 0)
return FALSE;
/* create png and info structures */
@ -248,7 +216,7 @@ BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file,
return FALSE; /* out of memory */
}
if (setjmp (png_jmpbuf(png_ptr)))
if (setjmp (png_jmpbuf (png_ptr)))
{
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
return FALSE;
@ -262,8 +230,7 @@ BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file,
png_read_info (png_ptr, info_ptr);
/* get size and bit-depth of the PNG-image */
png_get_IHDR (png_ptr, info_ptr,
&width, &height, &bit_depth, &color_type,
png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
NULL, NULL, NULL);
/* set-up the transformations */
@ -304,8 +271,8 @@ BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file,
if (bit_depth == 16)
{
raw = FALSE;
#ifdef __TURBOC__
pnm_file->flags &= ~((unsigned) _F_BIN);
#if defined(O_BINARY) && (O_BINARY != 0)
setmode (fileno (pnm_file), O_BINARY);
#endif
}
@ -333,25 +300,25 @@ BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file,
/* row_bytes is the width x number of channels x (bit-depth / 8) */
row_bytes = png_get_rowbytes (png_ptr, info_ptr);
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 */
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
return FALSE;
}
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)
{
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
return FALSE;
}
if ((row_pointers = (png_byte **)
malloc ((size_t)height * sizeof (png_bytep))) == NULL)
malloc ((size_t) height * sizeof (png_byte *))) == NULL)
{
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
free (png_pixels);
png_pixels = NULL;
return FALSE;
}
@ -366,7 +333,7 @@ BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file,
png_read_end (png_ptr, info_ptr);
/* clean up after the read, and free any memory allocated - REQUIRED */
png_destroy_read_struct (&png_ptr, &info_ptr, (png_infopp) NULL);
png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
/* write header of PNM file */
@ -406,15 +373,22 @@ BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file,
for (i = 0; i < (channels - alpha_present); i++)
{
if (raw)
fputc ((int) *pix_ptr++ , pnm_file);
{
fputc ((int) *pix_ptr++, pnm_file);
}
else
if (bit_depth == 16){
{
if (bit_depth == 16)
{
dep_16 = (long) *pix_ptr++;
fprintf (pnm_file, "%ld ", (dep_16 << 8) + ((long) *pix_ptr++));
}
else
{
fprintf (pnm_file, "%ld ", (long) *pix_ptr++);
}
}
}
if (alpha_present)
{
if (!alpha)
@ -426,17 +400,23 @@ BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file,
else /* output alpha-channel as pgm file */
{
if (raw)
fputc ((int) *pix_ptr++ , alpha_file);
{
fputc ((int) *pix_ptr++, alpha_file);
}
else
{
if (bit_depth == 16)
{
dep_16 = (long) *pix_ptr++;
fprintf (alpha_file, "%ld ", (dep_16 << 8) + (long) *pix_ptr++);
}
else
{
fprintf (alpha_file, "%ld ", (long) *pix_ptr++);
}
} /* if alpha_present */
}
}
} /* end if alpha_present */
if (!raw)
if (col % 4 == 3)
@ -448,13 +428,11 @@ BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file,
fprintf (pnm_file, "\n");
} /* end for row */
if (row_pointers != (unsigned char**) NULL)
if (row_pointers != NULL)
free (row_pointers);
if (png_pixels != (unsigned char*) NULL)
if (png_pixels != NULL)
free (png_pixels);
PNGMINUS_UNUSED(raw); /* to quiet a Coverity defect */
return TRUE;
} /* end of source */