25 #define BOOL unsigned char 31 #define FALSE (BOOL) 0 47 # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf) 52 int main (
int argc,
char *argv[]);
54 BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file,
BOOL interlace,
56 void get_token(FILE *pnm_file,
char *token);
64 int main(
int argc,
char *argv[])
73 for (argi = 1; argi < argc; argi++)
75 if (argv[argi][0] ==
'-')
77 switch (argv[argi][1])
85 if ((fp_al = fopen (argv[argi],
"rb")) ==
NULL)
87 fprintf (stderr,
"PNM2PNG\n");
88 fprintf (stderr,
"Error: alpha-channel file %s does not exist\n",
99 fprintf (stderr,
"PNM2PNG\n");
100 fprintf (stderr,
"Error: unknown option %s\n", argv[argi]);
106 else if (fp_rd == stdin)
108 if ((fp_rd = fopen (argv[argi],
"rb")) ==
NULL)
110 fprintf (stderr,
"PNM2PNG\n");
111 fprintf (stderr,
"Error: file %s does not exist\n", argv[argi]);
115 else if (fp_wr == stdout)
117 if ((fp_wr = fopen (argv[argi],
"wb")) ==
NULL)
119 fprintf (stderr,
"PNM2PNG\n");
120 fprintf (stderr,
"Error: can not create PNG-file %s\n", argv[argi]);
126 fprintf (stderr,
"PNM2PNG\n");
127 fprintf (stderr,
"Error: too many parameters\n");
137 setmode (
STDIN, O_BINARY);
141 setmode (
STDOUT, O_BINARY);
146 if (
pnm2png (fp_rd, fp_wr, fp_al, interlace, alpha) ==
FALSE)
148 fprintf (stderr,
"PNM2PNG\n");
149 fprintf (stderr,
"Error: unsuccessful converting to PNG-image\n");
170 fprintf (stderr,
"PNM2PNG\n");
171 fprintf (stderr,
" by Willem van Schaik, 1999\n");
173 fprintf (stderr,
" for Turbo-C and Borland-C compilers\n");
175 fprintf (stderr,
" for Linux (and Unix) compilers\n");
177 fprintf (stderr,
"Usage: pnm2png [options] <file>.<pnm> [<file>.png]\n");
178 fprintf (stderr,
" or: ... | pnm2png [options]\n");
179 fprintf (stderr,
"Options:\n");
180 fprintf (stderr,
" -i[nterlace] write png-file with interlacing on\n");
182 " -a[lpha] <file>.pgm read PNG alpha channel as pgm-file\n");
183 fprintf (stderr,
" -h | -? print this help-information\n");
201 char width_token[16];
202 char height_token[16];
203 char maxval_token[16];
205 unsigned long ul_width=0, ul_alpha_width=0;
206 unsigned long ul_height=0, ul_alpha_height=0;
207 unsigned long ul_maxval=0;
209 volatile png_uint_32 alpha_width=0, alpha_height=0;
217 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED) 226 if (type_token[0] !=
'P')
230 else if ((type_token[1] ==
'1') || (type_token[1] ==
'4'))
232 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED) 233 raw = (type_token[1] ==
'4');
236 sscanf (width_token,
"%lu", &ul_width);
239 sscanf (height_token,
"%lu", &ul_height);
242 packed_bitmap =
TRUE;
244 fprintf (stderr,
"PNM2PNG built without PNG_WRITE_INVERT_SUPPORTED and \n");
245 fprintf (stderr,
"PNG_WRITE_PACK_SUPPORTED can't read PBM (P1,P4) files\n");
248 else if ((type_token[1] ==
'2') || (type_token[1] ==
'5'))
250 raw = (type_token[1] ==
'5');
253 sscanf (width_token,
"%lu", &ul_width);
256 sscanf (height_token,
"%lu", &ul_height);
259 sscanf (maxval_token,
"%lu", &ul_maxval);
264 else if (maxval <= 3)
266 else if (maxval <= 15)
268 else if (maxval <= 255)
273 else if ((type_token[1] ==
'3') || (type_token[1] ==
'6'))
275 raw = (type_token[1] ==
'6');
278 sscanf (width_token,
"%lu", &ul_width);
281 sscanf (height_token,
"%lu", &ul_height);
284 sscanf (maxval_token,
"%lu", &ul_maxval);
288 else if (maxval <= 3)
290 else if (maxval <= 15)
292 else if (maxval <= 255)
312 if (type_token[0] !=
'P')
316 else if ((type_token[1] ==
'2') || (type_token[1] ==
'5'))
318 alpha_raw = (type_token[1] ==
'5');
320 sscanf (width_token,
"%lu", &ul_alpha_width);
322 if (alpha_width != width)
325 sscanf (height_token,
"%lu", &ul_alpha_height);
327 if (alpha_height !=
height)
330 sscanf (maxval_token,
"%lu", &ul_maxval);
334 else if (maxval <= 3)
336 else if (maxval <= 15)
338 else if (maxval <= 255)
342 if (alpha_depth != bit_depth)
365 alpha_present = (channels - 1) % 2;
367 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED) 370 row_bytes = (width * channels * bit_depth + 7) / 8;
374 row_bytes = width * channels * ((bit_depth <= 8) ? 1 : 2);
381 pix_ptr = png_pixels;
385 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED) 387 for (i = 0; i < (
int) row_bytes; i++)
389 *pix_ptr++ =
get_data (pnm_file, 8);
393 for (col = 0; col < (
int) width; col++)
395 for (i = 0; i < (channels - alpha_present); i++)
398 *pix_ptr++ =
get_data (pnm_file, bit_depth);
401 *pix_ptr++ =
get_value (pnm_file, bit_depth);
405 *pix_ptr = (
png_byte) ((tmp16 >> 8) & 0xFF);
407 *pix_ptr = (
png_byte) (tmp16 & 0xFF);
415 *pix_ptr++ =
get_data (alpha_file, alpha_depth);
417 if (alpha_depth <= 8)
418 *pix_ptr++ =
get_value (alpha_file, bit_depth);
421 tmp16 =
get_value (alpha_file, bit_depth);
422 *pix_ptr++ = (
png_byte) ((tmp16 >> 8) & 0xFF);
423 *pix_ptr++ = (
png_byte) (tmp16 & 0xFF);
448 #if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED) 449 if (packed_bitmap ==
TRUE)
477 if (row_pointers == (
unsigned char**)
NULL)
491 row_pointers[i] = png_pixels + i * row_bytes;
502 if (row_pointers != (
unsigned char**) NULL)
504 if (png_pixels != (
unsigned char*) NULL)
522 ret = fgetc(pnm_file);
527 ret = fgetc(pnm_file);
529 while ((ret !=
'\n') && (ret !=
'\r') && (ret !=
EOF));
531 if (ret ==
EOF)
break;
532 token[
i] = (
unsigned char) ret;
534 while ((token[i] ==
'\n') || (token[
i] ==
'\r') || (token[i] ==
' '));
539 ret = fgetc(pnm_file);
540 if (ret ==
EOF)
break;
542 token[
i] = (
unsigned char) ret;
544 while ((token[i] !=
'\n') && (token[
i] !=
'\r') && (token[i] !=
' '));
560 static int old_value = 0;
566 for (i = 0; i <
depth; i++)
567 mask = (mask >> 1) | 0x80;
571 old_value = fgetc (pnm_file);
575 ret_value = old_value &
mask;
576 for (i = 1; i < (8 /
depth); i++)
577 ret_value = ret_value || (ret_value >> depth);
579 old_value = (old_value <<
depth) & 0xFF;
594 unsigned long ul_ret_value;
599 for (i = 0; i <
depth; i++)
600 mask = (mask << 1) | 0x01;
603 sscanf ((
const char *) token,
"%lu", &ul_ret_value);
609 for (i = 0; i < (8 /
depth); i++)
610 ret_value = (ret_value << depth) || ret_value;
GLint GLint GLsizei GLsizei height
void PNGAPI png_set_packing(png_structp png_ptr)
void PNGAPI png_set_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth, int color_type, int interlace_type, int compression_type, int filter_type)
png_uint_32 get_value(FILE *pnm_file, int depth)
void PNGAPI png_write_image(png_structp png_ptr, png_bytepp image)
GLint GLint GLsizei GLsizei GLsizei depth
png_infop PNGAPI png_create_info_struct(png_structp png_ptr)
#define PNG_INTERLACE_NONE
#define PNG_COLOR_TYPE_RGB
#define PNG_COLOR_TYPE_GRAY_ALPHA
#define png_jmpbuf(png_ptr)
#define PNG_INTERLACE_ADAM7
int main(int argc, char *argv[])
png_charp PNGAPI png_get_libpng_ver(png_structp png_ptr)
GLint GLint GLsizei width
void get_token(FILE *pnm_file, char *token)
#define PNG_COLOR_TYPE_RGB_ALPHA
BOOL pnm2png(FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace, BOOL alpha)
GLenum GLenum GLvoid * row
png_infop png_bytepp row_pointers
void PNGAPI png_write_info(png_structp png_ptr, png_infop info_ptr)
png_uint_32 get_data(FILE *pnm_file, int depth)
#define PNG_COLOR_TYPE_GRAY
void PNGAPI png_write_end(png_structp png_ptr, png_infop info_ptr)
void PNGAPI png_set_invert_mono(png_structp png_ptr)
#define PNG_FILTER_TYPE_BASE
#define PNG_COMPRESSION_TYPE_BASE
void PNGAPI png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
png_structp PNGAPI png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn)
GLfloat GLfloat GLfloat alpha
unsigned long png_uint_32
png_info FAR *FAR * png_infopp
register bit_buf_type register int bits_left
void PNGAPI png_init_io(png_structp png_ptr, png_FILE_p fp)