23 #define BOOL unsigned char 29 #define FALSE (BOOL) 0 47 # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf) 52 int main (
int argc,
char *argv[]);
60 int main(
int argc,
char *argv[])
69 for (argi = 1; argi < argc; argi++)
71 if (argv[argi][0] ==
'-')
73 switch (argv[argi][1])
84 if ((fp_al = fopen (argv[argi],
"wb")) ==
NULL)
86 fprintf (stderr,
"PNM2PNG\n");
87 fprintf (stderr,
"Error: can not create alpha-channel file %s\n", argv[argi]);
97 fprintf (stderr,
"PNG2PNM\n");
98 fprintf (stderr,
"Error: unknown option %s\n", argv[argi]);
104 else if (fp_rd == stdin)
106 if ((fp_rd = fopen (argv[argi],
"rb")) ==
NULL)
108 fprintf (stderr,
"PNG2PNM\n");
109 fprintf (stderr,
"Error: file %s does not exist\n", argv[argi]);
113 else if (fp_wr == stdout)
115 if ((fp_wr = fopen (argv[argi],
"wb")) ==
NULL)
117 fprintf (stderr,
"PNG2PNM\n");
118 fprintf (stderr,
"Error: can not create file %s\n", argv[argi]);
124 fprintf (stderr,
"PNG2PNM\n");
125 fprintf (stderr,
"Error: too many parameters\n");
135 setmode (
STDIN, O_BINARY);
137 if ((raw) && (fp_wr == stdout))
139 setmode (
STDOUT, O_BINARY);
146 fprintf (stderr,
"PNG2PNM\n");
147 fprintf (stderr,
"Error: unsuccessful convertion of PNG-image\n");
168 fprintf (stderr,
"PNG2PNM\n");
169 fprintf (stderr,
" by Willem van Schaik, 1999\n");
171 fprintf (stderr,
" for Turbo-C and Borland-C compilers\n");
173 fprintf (stderr,
" for Linux (and Unix) compilers\n");
175 fprintf (stderr,
"Usage: png2pnm [options] <file>.png [<file>.pnm]\n");
176 fprintf (stderr,
" or: ... | png2pnm [options]\n");
177 fprintf (stderr,
"Options:\n");
178 fprintf (stderr,
" -r[aw] write pnm-file in binary format (P4/P5/P6) (default)\n");
179 fprintf (stderr,
" -n[oraw] write pnm-file in ascii format (P1/P2/P3)\n");
180 fprintf (stderr,
" -a[lpha] <file>.pgm write PNG alpha channel as pgm-file\n");
181 fprintf (stderr,
" -h | -? print this help-information\n");
210 ret = fread (buf, 1, 8, png_file);
247 &width, &height, &bit_depth, &color_type,
281 png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
289 pnm_file->flags &= ~((unsigned) _F_BIN);
304 alpha_present = (channels - 1) % 2;
307 if (alpha && !alpha_present)
309 fprintf (stderr,
"PNG2PNM\n");
310 fprintf (stderr,
"Error: PNG-file doesn't contain alpha channel\n");
331 for (i = 0; i < (
height); i++)
332 row_pointers[i] = png_pixels + i * row_bytes;
348 fprintf (pnm_file,
"%s\n", (raw) ?
"P5" :
"P2");
349 fprintf (pnm_file,
"%d %d\n", (
int) width, (
int) height);
350 fprintf (pnm_file,
"%ld\n", ((1L << (
int) bit_depth) - 1L));
355 fprintf (pnm_file,
"%s\n", (raw) ?
"P6" :
"P3");
356 fprintf (pnm_file,
"%d %d\n", (
int) width, (
int) height);
357 fprintf (pnm_file,
"%ld\n", ((1L << (
int) bit_depth) - 1L));
366 fprintf (alpha_file,
"%s\n", (raw) ?
"P5" :
"P2");
367 fprintf (alpha_file,
"%d %d\n", (
int) width, (
int) height);
368 fprintf (alpha_file,
"%ld\n", ((1L << (
int) bit_depth) - 1L));
372 pix_ptr = png_pixels;
374 for (row = 0; row <
height; row++)
376 for (col = 0; col <
width; col++)
378 for (i = 0; i < (channels - alpha_present); i++)
381 fputc ((
int) *pix_ptr++ , pnm_file);
383 if (bit_depth == 16){
384 dep_16 = (
long) *pix_ptr++;
385 fprintf (pnm_file,
"%ld ", (dep_16 << 8) + ((
long) *pix_ptr++));
388 fprintf (pnm_file,
"%ld ", (
long) *pix_ptr++);
401 fputc ((
int) *pix_ptr++ , alpha_file);
403 if (bit_depth == 16){
404 dep_16 = (
long) *pix_ptr++;
405 fprintf (alpha_file,
"%ld ", (dep_16 << 8) + (
long) *pix_ptr++);
408 fprintf (alpha_file,
"%ld ", (
long) *pix_ptr++);
414 fprintf (pnm_file,
"\n");
419 fprintf (pnm_file,
"\n");
422 if (row_pointers != (
unsigned char**) NULL)
424 if (png_pixels != (
unsigned char*) NULL)
GLint GLint GLsizei GLsizei height
png_infop PNGAPI png_create_info_struct(png_structp png_ptr)
png_uint_32 PNGAPI png_get_valid(png_structp png_ptr, png_infop info_ptr, png_uint_32 flag)
#define PNG_COLOR_TYPE_RGB
#define PNG_COLOR_TYPE_GRAY_ALPHA
#define PNG_COLOR_TYPE_PALETTE
png_uint_32 PNGAPI png_get_gAMA(png_structp png_ptr, png_infop info_ptr, double *file_gamma)
#define PNG_LIBPNG_VER_STRING
GLint GLint GLsizei width
void PNGAPI png_set_sig_bytes(png_structp png_ptr, int num_bytes)
#define PNG_COLOR_TYPE_RGB_ALPHA
void PNGAPI png_set_strip_16(png_structp png_ptr)
GLenum GLuint GLenum GLsizei const GLchar * buf
#define png_jmpbuf(png_ptr)
GLenum GLenum GLvoid * row
png_infop png_bytepp row_pointers
void PNGAPI png_read_end(png_structp png_ptr, png_infop info_ptr)
png_uint_32 PNGAPI png_get_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)
#define PNG_COLOR_TYPE_GRAY
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
void PNGAPI png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
int PNGAPI png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
void PNGAPI png_read_info(png_structp png_ptr, png_infop info_ptr)
png_infop double * file_gamma
BOOL png2pnm(FILE *png_file, FILE *pnm_file, FILE *alpha_file, BOOL raw, BOOL alpha)
png_structp PNGAPI png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn)
void PNGAPI png_set_expand(png_structp png_ptr)
void PNGAPI png_read_image(png_structp png_ptr, png_bytepp image)
int main(int argc, char *argv[])
GLfloat GLfloat GLfloat alpha
unsigned long png_uint_32
png_info FAR *FAR * png_infopp
png_uint_32 PNGAPI png_get_rowbytes(png_structp png_ptr, png_infop info_ptr)
void PNGAPI png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr)
void PNGAPI png_read_update_info(png_structp png_ptr, png_infop info_ptr)
void PNGAPI png_init_io(png_structp png_ptr, png_FILE_p fp)
void PNGAPI png_set_gray_to_rgb(png_structp png_ptr)