png2pnm.c revision 671242f350d172e106580348e24bab66b0d7e6a5
/*
* copyright (C) 1999 by Willem van Schaik <willem@schaik.com>
*
* version 1.0 - 1999.10.15 - First version.
*
* 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>
#ifdef __TURBOC__
#include <mem.h>
#include <fcntl.h>
#endif
#ifndef BOOL
#define BOOL unsigned char
#endif
#ifndef TRUE
#endif
#ifndef FALSE
#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) */
#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
#endif
/* function prototypes */
void usage ();
/*
* main
*/
{
int argi;
{
{
{
case 'n':
break;
case 'r':
break;
case 'a':
argi++;
{
exit (1);
}
break;
case 'h':
case '?':
usage();
exit(0);
break;
default:
usage();
exit(1);
break;
} /* end switch */
}
{
{
exit (1);
}
}
{
{
exit (1);
}
}
else
{
usage();
exit(1);
}
} /* end for */
#ifdef __TURBOC__
{
}
{
}
#endif
/* call the conversion program itself */
{
exit(1);
}
/* close input file */
/* close output file */
/* close alpha file */
if (alpha)
return 0;
}
/*
* usage
*/
void usage()
{
#ifdef __TURBOC__
#else
#endif
}
/*
* png2pnm
*/
{
int bit_depth;
int channels;
int color_type;
int alpha_present;
int ret;
int i;
long dep_16;
/* read and check signature in PNG file */
if (ret != 8)
return FALSE;
if (!ret)
return FALSE;
/* create png and info structures */
if (!png_ptr)
return FALSE; /* out of memory */
if (!info_ptr)
{
return FALSE; /* out of memory */
}
{
return FALSE;
}
/* set up the input control for C streams */
/* read the file information */
/* get size and bit-depth of the PNG-image */
/* set-up the transformations */
/* transform paletted images into full-color rgb */
if (color_type == PNG_COLOR_TYPE_PALETTE)
/* expand images to bit-depth 8 (only applicable for grayscale images) */
/* transform transparency maps into full alpha-channel */
#ifdef NJET
/* downgrade 16-bit images to 8 bit */
if (bit_depth == 16)
/* transform grayscale images into full-color */
if (color_type == PNG_COLOR_TYPE_GRAY ||
/* only if file has a file gamma, we do a correction */
#endif
/* all transformations have been registered; now update info_ptr data,
* get rowbytes and channels, and allocate image memory */
/* check for 16-bit files */
if (bit_depth == 16)
{
#ifdef __TURBOC__
#endif
}
/* calculate new 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;
else
channels = 0; /* should never happen */
/* check if alpha is expected to be present in file */
if (alpha && !alpha_present)
{
exit (1);
}
/* row_bytes is the width x number of channels x (bit-depth / 8) */
return FALSE;
}
{
free (png_pixels);
png_pixels = NULL;
return FALSE;
}
/* set the individual row_pointers to point at the correct offsets */
for (i = 0; i < (height); i++)
/* now we can go ahead and just read the whole image */
/* read rest of file, and get additional chunks in info_ptr - REQUIRED */
/* clean up after the read, and free any memory allocated - REQUIRED */
/* write header of PNM file */
if ((color_type == PNG_COLOR_TYPE_GRAY) ||
{
}
else if ((color_type == PNG_COLOR_TYPE_RGB) ||
{
}
/* write header of PGM file with alpha channel */
if ((alpha) &&
((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
{
}
/* write data to PNM file */
{
{
for (i = 0; i < (channels - alpha_present); i++)
{
if (raw)
else
if (bit_depth == 16){
}
else
}
if (alpha_present)
{
if (!alpha)
{
pix_ptr++; /* alpha */
if (bit_depth == 16)
pix_ptr++;
}
else /* output alpha-channel as pgm file */
{
if (raw)
else
if (bit_depth == 16){
}
else
}
} /* if alpha_present */
if (!raw)
} /* end for col */
if (!raw)
if (col % 4 != 0)
} /* end for row */
if (row_pointers != (unsigned char**) NULL)
free (row_pointers);
if (png_pixels != (unsigned char*) NULL)
free (png_pixels);
return TRUE;
} /* end of source */