1999-10-23 09:39:18 -04:00
|
|
|
/*
|
|
|
|
* pnm2png.c --- conversion from PBM/PGM/PPM-file to PNG-file
|
2018-08-05 21:49:57 -04:00
|
|
|
* copyright (C) 1999,2015,2017,2018 by Willem van Schaik <willem at schaik.com>
|
1999-10-23 09:39:18 -04:00
|
|
|
*
|
|
|
|
* version 1.0 - 1999.10.15 - First version.
|
2018-08-05 21:49:57 -04:00
|
|
|
* 1.1 - 2015.07.29 - Fixed leaks (Glenn Randers-Pehrson)
|
|
|
|
* 1.2 - 2017.04.22 - Add buffer-size check
|
2017-08-18 04:14:09 -04:00
|
|
|
* 1.3 - 2017.08.24 - Fix potential overflow in buffer-size check
|
|
|
|
* (Glenn Randers-Pehrson)
|
2017-08-28 12:27:23 -04:00
|
|
|
* 1.4 - 2017.08.28 - Add PNGMINUS_UNUSED (Christian Hesse)
|
2018-08-05 19:40:09 -04:00
|
|
|
* 1.5 - 2018.08.05 - Fix buffer overflow in tokenizer (Cosmin Truta)
|
2018-08-05 21:49:57 -04:00
|
|
|
* 1.6 - 2018.08.05 - Improve portability and fix style (Cosmin Truta)
|
1999-10-23 09:39:18 -04:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and distribute this software and
|
|
|
|
* its documentation for any purpose and without fee is hereby granted,
|
|
|
|
* provided that the above copyright notice appear in all copies and
|
|
|
|
* that both that copyright notice and this permission notice appear in
|
|
|
|
* supporting documentation. This software is provided "as is" without
|
|
|
|
* express or implied warranty.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#ifndef BOOL
|
|
|
|
#define BOOL unsigned char
|
|
|
|
#endif
|
|
|
|
#ifndef TRUE
|
|
|
|
#define TRUE (BOOL) 1
|
|
|
|
#endif
|
|
|
|
#ifndef FALSE
|
|
|
|
#define FALSE (BOOL) 0
|
|
|
|
#endif
|
|
|
|
|
2018-08-05 21:49:57 -04:00
|
|
|
/* make pnm2png verbose so we can find problems (needs to be before png.h) */
|
1999-10-23 09:39:18 -04:00
|
|
|
#ifndef PNG_DEBUG
|
|
|
|
#define PNG_DEBUG 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "png.h"
|
|
|
|
|
|
|
|
/* function prototypes */
|
|
|
|
|
2018-08-05 21:49:57 -04:00
|
|
|
int main (int argc, char *argv[]);
|
1999-10-23 09:39:18 -04:00
|
|
|
void usage ();
|
2018-08-05 21:49:57 -04:00
|
|
|
BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file,
|
|
|
|
BOOL interlace, BOOL alpha);
|
|
|
|
void get_token (FILE *pnm_file, char *token_buf, size_t token_buf_size);
|
1999-10-23 09:39:18 -04:00
|
|
|
png_uint_32 get_data (FILE *pnm_file, int depth);
|
|
|
|
png_uint_32 get_value (FILE *pnm_file, int depth);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* main
|
|
|
|
*/
|
|
|
|
|
2018-08-05 21:49:57 -04:00
|
|
|
int main (int argc, char *argv[])
|
1999-10-23 09:39:18 -04:00
|
|
|
{
|
|
|
|
FILE *fp_rd = stdin;
|
|
|
|
FILE *fp_al = NULL;
|
|
|
|
FILE *fp_wr = stdout;
|
|
|
|
BOOL interlace = FALSE;
|
|
|
|
BOOL alpha = FALSE;
|
|
|
|
int argi;
|
|
|
|
|
|
|
|
for (argi = 1; argi < argc; argi++)
|
|
|
|
{
|
1999-12-10 10:43:02 -05:00
|
|
|
if (argv[argi][0] == '-')
|
1999-10-23 09:39:18 -04:00
|
|
|
{
|
1999-12-10 10:43:02 -05:00
|
|
|
switch (argv[argi][1])
|
1999-10-23 09:39:18 -04:00
|
|
|
{
|
|
|
|
case 'i':
|
|
|
|
interlace = TRUE;
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
alpha = TRUE;
|
|
|
|
argi++;
|
|
|
|
if ((fp_al = fopen (argv[argi], "rb")) == NULL)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "PNM2PNG\n");
|
2000-02-05 00:40:16 -05:00
|
|
|
fprintf (stderr, "Error: alpha-channel file %s does not exist\n",
|
2018-08-05 21:49:57 -04:00
|
|
|
argv[argi]);
|
1999-10-23 09:39:18 -04:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
case '?':
|
2018-08-05 21:49:57 -04:00
|
|
|
usage ();
|
|
|
|
exit (0);
|
1999-10-23 09:39:18 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fprintf (stderr, "PNM2PNG\n");
|
|
|
|
fprintf (stderr, "Error: unknown option %s\n", argv[argi]);
|
2018-08-05 21:49:57 -04:00
|
|
|
usage ();
|
|
|
|
exit (1);
|
1999-10-23 09:39:18 -04:00
|
|
|
break;
|
|
|
|
} /* end switch */
|
|
|
|
}
|
|
|
|
else if (fp_rd == stdin)
|
|
|
|
{
|
|
|
|
if ((fp_rd = fopen (argv[argi], "rb")) == NULL)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "PNM2PNG\n");
|
|
|
|
fprintf (stderr, "Error: file %s does not exist\n", argv[argi]);
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (fp_wr == stdout)
|
|
|
|
{
|
|
|
|
if ((fp_wr = fopen (argv[argi], "wb")) == NULL)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "PNM2PNG\n");
|
2018-08-05 21:49:57 -04:00
|
|
|
fprintf (stderr, "Error: cannot create PNG-file %s\n", argv[argi]);
|
1999-10-23 09:39:18 -04:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fprintf (stderr, "PNM2PNG\n");
|
|
|
|
fprintf (stderr, "Error: too many parameters\n");
|
2018-08-05 21:49:57 -04:00
|
|
|
usage ();
|
1999-10-23 09:39:18 -04:00
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
} /* end for */
|
|
|
|
|
2018-08-05 21:49:57 -04:00
|
|
|
#if defined(O_BINARY) && (O_BINARY != 0)
|
|
|
|
/* set stdin/stdout to binary,
|
|
|
|
* we're reading the PNM always! in binary format
|
|
|
|
*/
|
1999-10-23 09:39:18 -04:00
|
|
|
if (fp_rd == stdin)
|
2018-08-05 21:49:57 -04:00
|
|
|
setmode (fileno (stdin), O_BINARY);
|
1999-10-23 09:39:18 -04:00
|
|
|
if (fp_wr == stdout)
|
2018-08-05 21:49:57 -04:00
|
|
|
setmode (fileno (stdout), O_BINARY);
|
1999-10-23 09:39:18 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* call the conversion program itself */
|
|
|
|
if (pnm2png (fp_rd, fp_wr, fp_al, interlace, alpha) == FALSE)
|
|
|
|
{
|
|
|
|
fprintf (stderr, "PNM2PNG\n");
|
|
|
|
fprintf (stderr, "Error: unsuccessful converting to PNG-image\n");
|
|
|
|
exit (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* close input file */
|
|
|
|
fclose (fp_rd);
|
|
|
|
/* close output file */
|
|
|
|
fclose (fp_wr);
|
|
|
|
/* close alpha file */
|
|
|
|
if (alpha)
|
|
|
|
fclose (fp_al);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* usage
|
|
|
|
*/
|
|
|
|
|
2018-08-05 21:49:57 -04:00
|
|
|
void usage ()
|
1999-10-23 09:39:18 -04:00
|
|
|
{
|
|
|
|
fprintf (stderr, "PNM2PNG\n");
|
|
|
|
fprintf (stderr, " by Willem van Schaik, 1999\n");
|
|
|
|
fprintf (stderr, "Usage: pnm2png [options] <file>.<pnm> [<file>.png]\n");
|
|
|
|
fprintf (stderr, " or: ... | pnm2png [options]\n");
|
|
|
|
fprintf (stderr, "Options:\n");
|
|
|
|
fprintf (stderr, " -i[nterlace] write png-file with interlacing on\n");
|
2014-06-13 12:06:33 -04:00
|
|
|
fprintf (stderr,
|
|
|
|
" -a[lpha] <file>.pgm read PNG alpha channel as pgm-file\n");
|
1999-10-23 09:39:18 -04:00
|
|
|
fprintf (stderr, " -h | -? print this help-information\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pnm2png
|
|
|
|
*/
|
|
|
|
|
2018-08-05 21:49:57 -04:00
|
|
|
BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file,
|
|
|
|
BOOL interlace, BOOL alpha)
|
1999-10-23 09:39:18 -04:00
|
|
|
{
|
|
|
|
png_struct *png_ptr = NULL;
|
|
|
|
png_info *info_ptr = NULL;
|
|
|
|
png_byte *png_pixels = NULL;
|
|
|
|
png_byte **row_pointers = NULL;
|
|
|
|
png_byte *pix_ptr = NULL;
|
2018-08-05 21:49:57 -04:00
|
|
|
volatile png_uint_32 row_bytes;
|
1999-10-23 09:39:18 -04:00
|
|
|
|
|
|
|
char type_token[16];
|
|
|
|
char width_token[16];
|
|
|
|
char height_token[16];
|
|
|
|
char maxval_token[16];
|
2018-08-05 21:49:57 -04:00
|
|
|
volatile int color_type = 1;
|
|
|
|
unsigned long ul_width = 0, ul_alpha_width = 0;
|
|
|
|
unsigned long ul_height = 0, ul_alpha_height = 0;
|
|
|
|
unsigned long ul_maxval = 0;
|
|
|
|
volatile png_uint_32 width = 0, height = 0;
|
|
|
|
volatile png_uint_32 alpha_width = 0, alpha_height = 0;
|
1999-10-23 09:39:18 -04:00
|
|
|
png_uint_32 maxval;
|
2018-08-05 21:49:57 -04:00
|
|
|
volatile int bit_depth = 0;
|
|
|
|
int channels = 0;
|
1999-10-23 09:39:18 -04:00
|
|
|
int alpha_depth = 0;
|
2018-08-05 21:49:57 -04:00
|
|
|
int alpha_present = 0;
|
1999-10-23 09:39:18 -04:00
|
|
|
int row, col;
|
|
|
|
BOOL raw, alpha_raw = FALSE;
|
2013-05-12 19:16:31 -04:00
|
|
|
#if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
|
2013-05-08 11:09:19 -04:00
|
|
|
BOOL packed_bitmap = FALSE;
|
2013-05-12 19:16:31 -04:00
|
|
|
#endif
|
1999-10-23 09:39:18 -04:00
|
|
|
png_uint_32 tmp16;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* read header of PNM file */
|
|
|
|
|
2018-08-05 21:49:57 -04:00
|
|
|
get_token (pnm_file, type_token, sizeof (type_token));
|
1999-10-23 09:39:18 -04:00
|
|
|
if (type_token[0] != 'P')
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else if ((type_token[1] == '1') || (type_token[1] == '4'))
|
|
|
|
{
|
2013-05-12 19:16:31 -04:00
|
|
|
#if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
|
1999-10-23 09:39:18 -04:00
|
|
|
raw = (type_token[1] == '4');
|
|
|
|
color_type = PNG_COLOR_TYPE_GRAY;
|
2018-08-05 21:49:57 -04:00
|
|
|
get_token (pnm_file, width_token, sizeof (width_token));
|
2013-05-08 11:09:19 -04:00
|
|
|
sscanf (width_token, "%lu", &ul_width);
|
|
|
|
width = (png_uint_32) ul_width;
|
2018-08-05 21:49:57 -04:00
|
|
|
get_token (pnm_file, height_token, sizeof (height_token));
|
2013-05-08 11:09:19 -04:00
|
|
|
sscanf (height_token, "%lu", &ul_height);
|
|
|
|
height = (png_uint_32) ul_height;
|
1999-10-23 09:39:18 -04:00
|
|
|
bit_depth = 1;
|
2013-05-08 11:09:19 -04:00
|
|
|
packed_bitmap = TRUE;
|
2013-05-12 19:16:31 -04:00
|
|
|
#else
|
2018-08-05 21:49:57 -04:00
|
|
|
fprintf (stderr, "PNM2PNG built without PNG_WRITE_INVERT_SUPPORTED and\n");
|
2013-05-12 19:16:31 -04:00
|
|
|
fprintf (stderr, "PNG_WRITE_PACK_SUPPORTED can't read PBM (P1,P4) files\n");
|
2018-08-05 21:49:57 -04:00
|
|
|
return FALSE;
|
2013-05-12 19:16:31 -04:00
|
|
|
#endif
|
1999-10-23 09:39:18 -04:00
|
|
|
}
|
|
|
|
else if ((type_token[1] == '2') || (type_token[1] == '5'))
|
|
|
|
{
|
|
|
|
raw = (type_token[1] == '5');
|
|
|
|
color_type = PNG_COLOR_TYPE_GRAY;
|
2018-08-05 21:49:57 -04:00
|
|
|
get_token (pnm_file, width_token, sizeof (width_token));
|
2011-12-29 11:24:50 -05:00
|
|
|
sscanf (width_token, "%lu", &ul_width);
|
|
|
|
width = (png_uint_32) ul_width;
|
2018-08-05 21:49:57 -04:00
|
|
|
get_token (pnm_file, height_token, sizeof (height_token));
|
2011-12-29 11:24:50 -05:00
|
|
|
sscanf (height_token, "%lu", &ul_height);
|
|
|
|
height = (png_uint_32) ul_height;
|
2018-08-05 21:49:57 -04:00
|
|
|
get_token (pnm_file, maxval_token, sizeof (maxval_token));
|
2011-12-29 11:24:50 -05:00
|
|
|
sscanf (maxval_token, "%lu", &ul_maxval);
|
|
|
|
maxval = (png_uint_32) ul_maxval;
|
2013-03-02 15:58:22 -05:00
|
|
|
|
1999-10-23 09:39:18 -04:00
|
|
|
if (maxval <= 1)
|
|
|
|
bit_depth = 1;
|
|
|
|
else if (maxval <= 3)
|
|
|
|
bit_depth = 2;
|
|
|
|
else if (maxval <= 15)
|
|
|
|
bit_depth = 4;
|
|
|
|
else if (maxval <= 255)
|
|
|
|
bit_depth = 8;
|
2018-08-05 21:49:57 -04:00
|
|
|
else if (maxval <= 65535U)
|
1999-10-23 09:39:18 -04:00
|
|
|
bit_depth = 16;
|
2018-08-05 21:49:57 -04:00
|
|
|
else /* maxval > 65535U */
|
|
|
|
return FALSE;
|
1999-10-23 09:39:18 -04:00
|
|
|
}
|
|
|
|
else if ((type_token[1] == '3') || (type_token[1] == '6'))
|
|
|
|
{
|
|
|
|
raw = (type_token[1] == '6');
|
|
|
|
color_type = PNG_COLOR_TYPE_RGB;
|
2018-08-05 21:49:57 -04:00
|
|
|
get_token (pnm_file, width_token, sizeof (width_token));
|
2011-12-29 11:24:50 -05:00
|
|
|
sscanf (width_token, "%lu", &ul_width);
|
|
|
|
width = (png_uint_32) ul_width;
|
2018-08-05 21:49:57 -04:00
|
|
|
get_token (pnm_file, height_token, sizeof (height_token));
|
2011-12-29 11:24:50 -05:00
|
|
|
sscanf (height_token, "%lu", &ul_height);
|
|
|
|
height = (png_uint_32) ul_height;
|
2018-08-05 21:49:57 -04:00
|
|
|
get_token (pnm_file, maxval_token, sizeof (maxval_token));
|
2011-12-29 11:24:50 -05:00
|
|
|
sscanf (maxval_token, "%lu", &ul_maxval);
|
|
|
|
maxval = (png_uint_32) ul_maxval;
|
1999-10-23 09:39:18 -04:00
|
|
|
if (maxval <= 1)
|
|
|
|
bit_depth = 1;
|
|
|
|
else if (maxval <= 3)
|
|
|
|
bit_depth = 2;
|
|
|
|
else if (maxval <= 15)
|
|
|
|
bit_depth = 4;
|
|
|
|
else if (maxval <= 255)
|
|
|
|
bit_depth = 8;
|
2018-08-05 21:49:57 -04:00
|
|
|
else if (maxval <= 65535U)
|
1999-10-23 09:39:18 -04:00
|
|
|
bit_depth = 16;
|
2018-08-05 21:49:57 -04:00
|
|
|
else /* maxval > 65535U */
|
|
|
|
return FALSE;
|
1999-10-23 09:39:18 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read header of PGM file with alpha channel */
|
|
|
|
|
|
|
|
if (alpha)
|
|
|
|
{
|
|
|
|
if (color_type == PNG_COLOR_TYPE_GRAY)
|
|
|
|
color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
|
|
|
|
if (color_type == PNG_COLOR_TYPE_RGB)
|
|
|
|
color_type = PNG_COLOR_TYPE_RGB_ALPHA;
|
|
|
|
|
2018-08-05 21:49:57 -04:00
|
|
|
get_token (alpha_file, type_token, sizeof (type_token));
|
1999-10-23 09:39:18 -04:00
|
|
|
if (type_token[0] != 'P')
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else if ((type_token[1] == '2') || (type_token[1] == '5'))
|
|
|
|
{
|
|
|
|
alpha_raw = (type_token[1] == '5');
|
2018-08-05 21:49:57 -04:00
|
|
|
get_token (alpha_file, width_token, sizeof (width_token));
|
2011-12-29 11:24:50 -05:00
|
|
|
sscanf (width_token, "%lu", &ul_alpha_width);
|
2018-08-05 21:49:57 -04:00
|
|
|
alpha_width = (png_uint_32) ul_alpha_width;
|
1999-10-23 09:39:18 -04:00
|
|
|
if (alpha_width != width)
|
|
|
|
return FALSE;
|
2018-08-05 21:49:57 -04:00
|
|
|
get_token (alpha_file, height_token, sizeof (height_token));
|
2011-12-29 11:24:50 -05:00
|
|
|
sscanf (height_token, "%lu", &ul_alpha_height);
|
|
|
|
alpha_height = (png_uint_32) ul_alpha_height;
|
1999-10-23 09:39:18 -04:00
|
|
|
if (alpha_height != height)
|
|
|
|
return FALSE;
|
2018-08-05 21:49:57 -04:00
|
|
|
get_token (alpha_file, maxval_token, sizeof (maxval_token));
|
2011-12-29 11:24:50 -05:00
|
|
|
sscanf (maxval_token, "%lu", &ul_maxval);
|
|
|
|
maxval = (png_uint_32) ul_maxval;
|
1999-10-23 09:39:18 -04:00
|
|
|
if (maxval <= 1)
|
|
|
|
alpha_depth = 1;
|
|
|
|
else if (maxval <= 3)
|
|
|
|
alpha_depth = 2;
|
|
|
|
else if (maxval <= 15)
|
|
|
|
alpha_depth = 4;
|
|
|
|
else if (maxval <= 255)
|
|
|
|
alpha_depth = 8;
|
2018-08-05 21:49:57 -04:00
|
|
|
else if (maxval <= 65535U)
|
1999-10-23 09:39:18 -04:00
|
|
|
alpha_depth = 16;
|
2018-08-05 21:49:57 -04:00
|
|
|
else /* maxval > 65535U */
|
|
|
|
return FALSE;
|
1999-10-23 09:39:18 -04:00
|
|
|
if (alpha_depth != bit_depth)
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
} /* end if alpha */
|
|
|
|
|
|
|
|
/* calculate the number of channels and store alpha-presence */
|
|
|
|
if (color_type == PNG_COLOR_TYPE_GRAY)
|
|
|
|
channels = 1;
|
|
|
|
else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
|
|
|
|
channels = 2;
|
|
|
|
else if (color_type == PNG_COLOR_TYPE_RGB)
|
|
|
|
channels = 3;
|
|
|
|
else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
|
|
|
|
channels = 4;
|
2015-07-29 17:01:26 -04:00
|
|
|
#if 0
|
1999-10-23 09:39:18 -04:00
|
|
|
else
|
2015-07-29 17:01:26 -04:00
|
|
|
channels = 0; /* cannot happen */
|
|
|
|
#endif
|
1999-10-23 09:39:18 -04:00
|
|
|
|
|
|
|
alpha_present = (channels - 1) % 2;
|
|
|
|
|
2013-05-12 19:16:31 -04:00
|
|
|
#if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
|
2013-05-08 11:09:19 -04:00
|
|
|
if (packed_bitmap)
|
2018-08-05 21:49:57 -04:00
|
|
|
{
|
2013-05-08 11:09:19 -04:00
|
|
|
/* row data is as many bytes as can fit width x channels x bit_depth */
|
|
|
|
row_bytes = (width * channels * bit_depth + 7) / 8;
|
2018-08-05 21:49:57 -04:00
|
|
|
}
|
2013-05-08 11:09:19 -04:00
|
|
|
else
|
2013-05-12 19:16:31 -04:00
|
|
|
#endif
|
2018-08-05 21:49:57 -04:00
|
|
|
{
|
|
|
|
/* row_bytes is the width x number of channels x (bit-depth / 8) */
|
2013-05-08 11:09:19 -04:00
|
|
|
row_bytes = width * channels * ((bit_depth <= 8) ? 1 : 2);
|
2018-08-05 21:49:57 -04:00
|
|
|
}
|
1999-10-23 09:39:18 -04:00
|
|
|
|
2018-08-05 21:49:57 -04:00
|
|
|
if ((row_bytes == 0) ||
|
|
|
|
((size_t) height > (size_t) (-1) / (size_t) row_bytes))
|
2017-08-18 04:14:09 -04:00
|
|
|
{
|
2018-08-05 21:49:57 -04:00
|
|
|
/* too big */
|
2017-04-22 16:21:58 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
2014-06-13 12:06:33 -04:00
|
|
|
if ((png_pixels = (png_byte *)
|
2018-08-05 21:49:57 -04:00
|
|
|
malloc ((size_t) row_bytes * (size_t) height)) == NULL)
|
|
|
|
{
|
|
|
|
/* out of memory */
|
1999-10-23 09:39:18 -04:00
|
|
|
return FALSE;
|
2018-08-05 21:49:57 -04:00
|
|
|
}
|
1999-10-23 09:39:18 -04:00
|
|
|
|
|
|
|
/* read data from PNM file */
|
|
|
|
pix_ptr = png_pixels;
|
|
|
|
|
2014-06-13 12:06:33 -04:00
|
|
|
for (row = 0; row < (int) height; row++)
|
1999-10-23 09:39:18 -04:00
|
|
|
{
|
2013-05-12 19:16:31 -04:00
|
|
|
#if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
|
2017-08-18 04:14:09 -04:00
|
|
|
if (packed_bitmap)
|
|
|
|
{
|
2014-06-13 12:06:33 -04:00
|
|
|
for (i = 0; i < (int) row_bytes; i++)
|
2018-08-05 21:49:57 -04:00
|
|
|
{
|
2013-05-08 11:09:19 -04:00
|
|
|
/* png supports this format natively so no conversion is needed */
|
|
|
|
*pix_ptr++ = get_data (pnm_file, 8);
|
2018-08-05 21:49:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2013-05-12 19:16:31 -04:00
|
|
|
#endif
|
|
|
|
{
|
2014-06-13 12:06:33 -04:00
|
|
|
for (col = 0; col < (int) width; col++)
|
1999-10-23 09:39:18 -04:00
|
|
|
{
|
2013-05-08 11:09:19 -04:00
|
|
|
for (i = 0; i < (channels - alpha_present); i++)
|
|
|
|
{
|
|
|
|
if (raw)
|
2018-08-05 21:49:57 -04:00
|
|
|
{
|
2013-05-08 11:09:19 -04:00
|
|
|
*pix_ptr++ = get_data (pnm_file, bit_depth);
|
2018-08-05 21:49:57 -04:00
|
|
|
}
|
1999-10-23 09:39:18 -04:00
|
|
|
else
|
2018-08-05 21:49:57 -04:00
|
|
|
{
|
2013-05-08 11:09:19 -04:00
|
|
|
if (bit_depth <= 8)
|
2018-08-05 21:49:57 -04:00
|
|
|
{
|
2013-05-08 11:09:19 -04:00
|
|
|
*pix_ptr++ = get_value (pnm_file, bit_depth);
|
2018-08-05 21:49:57 -04:00
|
|
|
}
|
2013-05-08 11:09:19 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tmp16 = get_value (pnm_file, bit_depth);
|
|
|
|
*pix_ptr = (png_byte) ((tmp16 >> 8) & 0xFF);
|
|
|
|
pix_ptr++;
|
|
|
|
*pix_ptr = (png_byte) (tmp16 & 0xFF);
|
|
|
|
pix_ptr++;
|
|
|
|
}
|
2018-08-05 21:49:57 -04:00
|
|
|
}
|
2013-05-08 11:09:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (alpha) /* read alpha-channel from pgm file */
|
|
|
|
{
|
|
|
|
if (alpha_raw)
|
2018-08-05 21:49:57 -04:00
|
|
|
{
|
2013-05-08 11:09:19 -04:00
|
|
|
*pix_ptr++ = get_data (alpha_file, alpha_depth);
|
2018-08-05 21:49:57 -04:00
|
|
|
}
|
1999-10-23 09:39:18 -04:00
|
|
|
else
|
2018-08-05 21:49:57 -04:00
|
|
|
{
|
2013-05-08 11:09:19 -04:00
|
|
|
if (alpha_depth <= 8)
|
2018-08-05 21:49:57 -04:00
|
|
|
{
|
2013-05-08 11:09:19 -04:00
|
|
|
*pix_ptr++ = get_value (alpha_file, bit_depth);
|
2018-08-05 21:49:57 -04:00
|
|
|
}
|
2013-05-08 11:09:19 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tmp16 = get_value (alpha_file, bit_depth);
|
|
|
|
*pix_ptr++ = (png_byte) ((tmp16 >> 8) & 0xFF);
|
|
|
|
*pix_ptr++ = (png_byte) (tmp16 & 0xFF);
|
|
|
|
}
|
2018-08-05 21:49:57 -04:00
|
|
|
}
|
|
|
|
} /* end if alpha */
|
|
|
|
} /* end if packed_bitmap */
|
1999-10-23 09:39:18 -04:00
|
|
|
} /* end for col */
|
|
|
|
} /* end for row */
|
|
|
|
|
|
|
|
/* prepare the standard PNG structures */
|
2018-08-05 21:49:57 -04:00
|
|
|
png_ptr = png_create_write_struct (png_get_libpng_ver(NULL),
|
|
|
|
NULL, NULL, NULL);
|
1999-10-23 09:39:18 -04:00
|
|
|
if (!png_ptr)
|
|
|
|
{
|
2015-07-29 17:01:26 -04:00
|
|
|
free (png_pixels);
|
1999-10-23 09:39:18 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
info_ptr = png_create_info_struct (png_ptr);
|
|
|
|
if (!info_ptr)
|
|
|
|
{
|
2018-08-05 21:49:57 -04:00
|
|
|
png_destroy_write_struct (&png_ptr, NULL);
|
2015-07-29 17:01:26 -04:00
|
|
|
free (png_pixels);
|
1999-10-23 09:39:18 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-05-12 19:16:31 -04:00
|
|
|
#if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
|
2013-05-08 11:09:19 -04:00
|
|
|
if (packed_bitmap == TRUE)
|
|
|
|
{
|
|
|
|
png_set_packing (png_ptr);
|
|
|
|
png_set_invert_mono (png_ptr);
|
|
|
|
}
|
2013-05-12 19:16:31 -04:00
|
|
|
#endif
|
2013-05-08 11:09:19 -04:00
|
|
|
|
2018-08-05 21:49:57 -04:00
|
|
|
if (setjmp (png_jmpbuf (png_ptr)))
|
1999-10-23 09:39:18 -04:00
|
|
|
{
|
2015-07-29 17:01:26 -04:00
|
|
|
png_destroy_write_struct (&png_ptr, &info_ptr);
|
|
|
|
free (png_pixels);
|
1999-10-23 09:39:18 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize the png structure */
|
|
|
|
png_init_io (png_ptr, png_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,
|
2018-08-05 21:49:57 -04:00
|
|
|
(!interlace) ? PNG_INTERLACE_NONE : PNG_INTERLACE_ADAM7,
|
|
|
|
PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
|
1999-10-23 09:39:18 -04:00
|
|
|
|
|
|
|
/* write the file header information */
|
|
|
|
png_write_info (png_ptr, info_ptr);
|
|
|
|
|
|
|
|
/* if needed we will allocate memory for an new array of row-pointers */
|
2018-08-05 21:49:57 -04:00
|
|
|
if (row_pointers == NULL)
|
1999-10-23 09:39:18 -04:00
|
|
|
{
|
2014-06-13 12:06:33 -04:00
|
|
|
if ((row_pointers = (png_byte **)
|
2018-08-05 21:49:57 -04:00
|
|
|
malloc (height * sizeof (png_byte *))) == NULL)
|
1999-10-23 09:39:18 -04:00
|
|
|
{
|
2015-07-29 17:01:26 -04:00
|
|
|
png_destroy_write_struct (&png_ptr, &info_ptr);
|
|
|
|
free (png_pixels);
|
1999-10-23 09:39:18 -04:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set the individual row_pointers to point at the correct offsets */
|
2014-06-13 12:06:33 -04:00
|
|
|
for (i = 0; i < (int) height; i++)
|
1999-10-23 09:39:18 -04:00
|
|
|
row_pointers[i] = png_pixels + i * row_bytes;
|
|
|
|
|
|
|
|
/* write out the entire image data in one call */
|
|
|
|
png_write_image (png_ptr, row_pointers);
|
1999-12-10 10:43:02 -05:00
|
|
|
|
2014-06-13 12:06:33 -04:00
|
|
|
/* write the additional chunks to the PNG file (not really needed) */
|
1999-10-23 09:39:18 -04:00
|
|
|
png_write_end (png_ptr, info_ptr);
|
1999-12-10 10:43:02 -05:00
|
|
|
|
|
|
|
/* clean up after the write, and free any memory allocated */
|
2015-07-29 17:01:26 -04:00
|
|
|
png_destroy_write_struct (&png_ptr, &info_ptr);
|
1999-12-10 10:43:02 -05:00
|
|
|
|
2018-08-05 21:49:57 -04:00
|
|
|
if (row_pointers != NULL)
|
1999-10-23 09:39:18 -04:00
|
|
|
free (row_pointers);
|
2018-08-05 21:49:57 -04:00
|
|
|
if (png_pixels != NULL)
|
1999-10-23 09:39:18 -04:00
|
|
|
free (png_pixels);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
} /* end of pnm2png */
|
|
|
|
|
|
|
|
/*
|
2018-08-05 21:49:57 -04:00
|
|
|
* get_token - gets the first string after whitespace
|
1999-10-23 09:39:18 -04:00
|
|
|
*/
|
|
|
|
|
2018-08-05 21:49:57 -04:00
|
|
|
void get_token (FILE *pnm_file, char *token_buf, size_t token_buf_size)
|
1999-10-23 09:39:18 -04:00
|
|
|
{
|
2018-08-05 19:40:09 -04:00
|
|
|
size_t i = 0;
|
2013-05-07 15:31:35 -04:00
|
|
|
int ret;
|
1999-10-23 09:39:18 -04:00
|
|
|
|
2013-05-07 15:31:35 -04:00
|
|
|
/* remove white-space and comment lines */
|
1999-10-23 09:39:18 -04:00
|
|
|
do
|
|
|
|
{
|
2018-08-05 21:49:57 -04:00
|
|
|
ret = fgetc (pnm_file);
|
2017-08-18 04:14:09 -04:00
|
|
|
if (ret == '#')
|
|
|
|
{
|
2013-05-07 15:31:35 -04:00
|
|
|
/* the rest of this line is a comment */
|
|
|
|
do
|
|
|
|
{
|
2018-08-05 21:49:57 -04:00
|
|
|
ret = fgetc (pnm_file);
|
2013-05-07 15:31:35 -04:00
|
|
|
}
|
|
|
|
while ((ret != '\n') && (ret != '\r') && (ret != EOF));
|
|
|
|
}
|
|
|
|
if (ret == EOF) break;
|
2018-08-05 19:40:09 -04:00
|
|
|
token_buf[i] = (char) ret;
|
1999-10-23 09:39:18 -04:00
|
|
|
}
|
2018-08-05 19:40:09 -04:00
|
|
|
while ((ret == '\n') || (ret == '\r') || (ret == ' '));
|
1999-10-23 09:39:18 -04:00
|
|
|
|
|
|
|
/* read string */
|
|
|
|
do
|
|
|
|
{
|
2018-08-05 21:49:57 -04:00
|
|
|
ret = fgetc (pnm_file);
|
2013-05-07 15:31:35 -04:00
|
|
|
if (ret == EOF) break;
|
2018-08-05 19:40:09 -04:00
|
|
|
if (++i == token_buf_size - 1) break;
|
|
|
|
token_buf[i] = (char) ret;
|
1999-10-23 09:39:18 -04:00
|
|
|
}
|
2018-08-05 19:40:09 -04:00
|
|
|
while ((ret != '\n') && (ret != '\r') && (ret != ' '));
|
1999-10-23 09:39:18 -04:00
|
|
|
|
2018-08-05 19:40:09 -04:00
|
|
|
token_buf[i] = '\0';
|
1999-10-23 09:39:18 -04:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-08-05 21:49:57 -04:00
|
|
|
* get_data - takes first byte and converts into next pixel value,
|
|
|
|
* taking as much bits as defined by bit-depth and
|
|
|
|
* using the bit-depth to fill up a byte (0Ah -> AAh)
|
1999-10-23 09:39:18 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
png_uint_32 get_data (FILE *pnm_file, int depth)
|
|
|
|
{
|
|
|
|
static int bits_left = 0;
|
|
|
|
static int old_value = 0;
|
|
|
|
static int mask = 0;
|
|
|
|
int i;
|
|
|
|
png_uint_32 ret_value;
|
|
|
|
|
|
|
|
if (mask == 0)
|
|
|
|
for (i = 0; i < depth; i++)
|
|
|
|
mask = (mask >> 1) | 0x80;
|
|
|
|
|
|
|
|
if (bits_left <= 0)
|
|
|
|
{
|
|
|
|
old_value = fgetc (pnm_file);
|
|
|
|
bits_left = 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret_value = old_value & mask;
|
|
|
|
for (i = 1; i < (8 / depth); i++)
|
|
|
|
ret_value = ret_value || (ret_value >> depth);
|
|
|
|
|
|
|
|
old_value = (old_value << depth) & 0xFF;
|
|
|
|
bits_left -= depth;
|
|
|
|
|
|
|
|
return ret_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2018-08-05 21:49:57 -04:00
|
|
|
* get_value - takes first (numeric) string and converts into number,
|
|
|
|
* using the bit-depth to fill up a byte (0Ah -> AAh)
|
1999-10-23 09:39:18 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
png_uint_32 get_value (FILE *pnm_file, int depth)
|
|
|
|
{
|
|
|
|
static png_uint_32 mask = 0;
|
2018-08-05 19:40:09 -04:00
|
|
|
char token[16];
|
2012-01-14 20:47:02 -05:00
|
|
|
unsigned long ul_ret_value;
|
1999-10-23 09:39:18 -04:00
|
|
|
png_uint_32 ret_value;
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
if (mask == 0)
|
|
|
|
for (i = 0; i < depth; i++)
|
|
|
|
mask = (mask << 1) | 0x01;
|
|
|
|
|
2018-08-05 19:40:09 -04:00
|
|
|
get_token (pnm_file, token, sizeof (token));
|
|
|
|
sscanf (token, "%lu", &ul_ret_value);
|
2012-01-14 20:47:02 -05:00
|
|
|
ret_value = (png_uint_32) ul_ret_value;
|
1999-10-23 09:39:18 -04:00
|
|
|
|
|
|
|
ret_value &= mask;
|
|
|
|
|
|
|
|
if (depth < 8)
|
|
|
|
for (i = 0; i < (8 / depth); i++)
|
|
|
|
ret_value = (ret_value << depth) || ret_value;
|
|
|
|
|
|
|
|
return ret_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* end of source */
|