pnm2png.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
#define STDIN 0
#define STDOUT 1
#define STDERR 2
/* to make pnm2png 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 'i':
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
}
/*
* pnm2png
*/
{
char type_token[16];
char width_token[16];
char height_token[16];
char maxval_token[16];
int color_type;
int bit_depth = 0;
int channels;
int alpha_depth = 0;
int alpha_present;
int i;
/* read header of PNM file */
if (type_token[0] != 'P')
{
return FALSE;
}
{
bit_depth = 1;
}
{
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;
else /* if (maxval <= 65535) */
bit_depth = 16;
}
{
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;
else /* if (maxval <= 65535) */
bit_depth = 16;
}
else
{
return FALSE;
}
/* read header of PGM file with alpha channel */
if (alpha)
{
if (color_type == PNG_COLOR_TYPE_GRAY)
if (color_type == PNG_COLOR_TYPE_RGB)
if (type_token[0] != 'P')
{
return FALSE;
}
{
if (alpha_width != width)
return FALSE;
if (alpha_height != height)
return FALSE;
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;
else /* if (maxval <= 65535) */
alpha_depth = 16;
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;
else
channels = 0; /* should not happen */
/* row_bytes is the width x number of channels x (bit-depth / 8) */
return FALSE;
/* read data from PNM file */
{
{
for (i = 0; i < (channels - alpha_present); i++)
{
if (raw)
else
if (bit_depth <= 8)
else
{
pix_ptr++;
pix_ptr++;
}
}
if (alpha) /* read alpha-channel from pgm file */
{
if (alpha_raw)
else
if (alpha_depth <= 8)
else
{
}
} /* if alpha */
} /* end for col */
} /* end for row */
/* prepare the standard PNG structures */
if (!png_ptr)
{
return FALSE;
}
if (!info_ptr)
{
return FALSE;
}
/* setjmp() must be called in every function that calls a PNG-reading libpng function */
{
return FALSE;
}
/* initialize the png structure */
/* we're going to write more or less the same PNG as the input file */
/* write the file header information */
/* if needed we will allocate memory for an new array of row-pointers */
if (row_pointers == (unsigned char**) NULL)
{
{
return FALSE;
}
}
/* set the individual row_pointers to point at the correct offsets */
for (i = 0; i < (height); i++)
/* write out the entire image data in one call */
/* write the additional chuncks to the PNG file (not really needed) */
/* clean up after the write, and free any memory allocated */
if (row_pointers != (unsigned char**) NULL)
free (row_pointers);
if (png_pixels != (unsigned char*) NULL)
free (png_pixels);
return TRUE;
} /* end of pnm2png */
/*
* get_token() - gets the first string after whitespace
*/
{
int i = 0;
/* remove white-space */
do
{
}
/* read string */
do
{
i++;
}
token[i] = '\0';
return;
}
/*
* 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)
*/
{
static int bits_left = 0;
static int old_value = 0;
static int mask = 0;
int i;
if (mask == 0)
for (i = 0; i < depth; i++)
if (bits_left <= 0)
{
bits_left = 8;
}
return ret_value;
}
/*
* get_value() - takes first (numeric) string and converts into number,
* using the bit-depth to fill up a byte (0Ah -> AAh)
*/
{
static png_uint_32 mask = 0;
int i = 0;
if (mask == 0)
for (i = 0; i < depth; i++)
if (depth < 8)
for (i = 0; i < (8 / depth); i++)
return ret_value;
}
/* end of source */