fukasawa e60969
/*
fukasawa e60969
 *  pnm2png.c --- conversion from PBM/PGM/PPM-file to PNG-file
fukasawa e60969
 *  copyright (C) 1999 by Willem van Schaik <willem@schaik.com></willem@schaik.com>
fukasawa e60969
 *
fukasawa e60969
 *  version 1.0 - 1999.10.15 - First version.
fukasawa e60969
 *  version 1.1 - 2015.07.29 - Fixed leaks (Glenn Randers-Pehrson)
fukasawa e60969
 *
fukasawa e60969
 *  Permission to use, copy, modify, and distribute this software and
fukasawa e60969
 *  its documentation for any purpose and without fee is hereby granted,
fukasawa e60969
 *  provided that the above copyright notice appear in all copies and
fukasawa e60969
 *  that both that copyright notice and this permission notice appear in
fukasawa e60969
 *  supporting documentation. This software is provided "as is" without
fukasawa e60969
 *  express or implied warranty.
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
#include <stdio.h></stdio.h>
fukasawa e60969
#include <stdlib.h></stdlib.h>
fukasawa e60969
#ifdef __TURBOC__
fukasawa e60969
#include <mem.h></mem.h>
fukasawa e60969
#include <fcntl.h></fcntl.h>
fukasawa e60969
#endif
fukasawa e60969
#include <zlib.h></zlib.h>
fukasawa e60969
fukasawa e60969
#ifndef BOOL
fukasawa e60969
#define BOOL unsigned char
fukasawa e60969
#endif
fukasawa e60969
#ifndef TRUE
fukasawa e60969
#define TRUE (BOOL) 1
fukasawa e60969
#endif
fukasawa e60969
#ifndef FALSE
fukasawa e60969
#define FALSE (BOOL) 0
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#define STDIN  0
fukasawa e60969
#define STDOUT 1
fukasawa e60969
#define STDERR 2
fukasawa e60969
fukasawa e60969
/* to make pnm2png verbose so we can find problems (needs to be before png.h) */
fukasawa e60969
#ifndef PNG_DEBUG
fukasawa e60969
#define PNG_DEBUG 0
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
#include "png.h"
fukasawa e60969
fukasawa e60969
/* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
fukasawa e60969
#ifndef png_jmpbuf
fukasawa e60969
#  define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* function prototypes */
fukasawa e60969
fukasawa e60969
int  main (int argc, char *argv[]);
fukasawa e60969
void usage ();
fukasawa e60969
BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
fukasawa e60969
    BOOL alpha);
fukasawa e60969
void get_token(FILE *pnm_file, char *token);
fukasawa e60969
png_uint_32 get_data (FILE *pnm_file, int depth);
fukasawa e60969
png_uint_32 get_value (FILE *pnm_file, int depth);
fukasawa e60969
fukasawa e60969
/*
fukasawa e60969
 *  main
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
int main(int argc, char *argv[])
fukasawa e60969
{
fukasawa e60969
  FILE *fp_rd = stdin;
fukasawa e60969
  FILE *fp_al = NULL;
fukasawa e60969
  FILE *fp_wr = stdout;
fukasawa e60969
  BOOL interlace = FALSE;
fukasawa e60969
  BOOL alpha = FALSE;
fukasawa e60969
  int argi;
fukasawa e60969
fukasawa e60969
  for (argi = 1; argi < argc; argi++)
fukasawa e60969
  {
fukasawa e60969
    if (argv[argi][0] == '-')
fukasawa e60969
    {
fukasawa e60969
      switch (argv[argi][1])
fukasawa e60969
      {
fukasawa e60969
        case 'i':
fukasawa e60969
          interlace = TRUE;
fukasawa e60969
          break;
fukasawa e60969
        case 'a':
fukasawa e60969
          alpha = TRUE;
fukasawa e60969
          argi++;
fukasawa e60969
          if ((fp_al = fopen (argv[argi], "rb")) == NULL)
fukasawa e60969
          {
fukasawa e60969
            fprintf (stderr, "PNM2PNG\n");
fukasawa e60969
            fprintf (stderr, "Error:  alpha-channel file %s does not exist\n",
fukasawa e60969
               argv[argi]);
fukasawa e60969
            exit (1);
fukasawa e60969
          }
fukasawa e60969
          break;
fukasawa e60969
        case 'h':
fukasawa e60969
        case '?':
fukasawa e60969
          usage();
fukasawa e60969
          exit(0);
fukasawa e60969
          break;
fukasawa e60969
        default:
fukasawa e60969
          fprintf (stderr, "PNM2PNG\n");
fukasawa e60969
          fprintf (stderr, "Error:  unknown option %s\n", argv[argi]);
fukasawa e60969
          usage();
fukasawa e60969
          exit(1);
fukasawa e60969
          break;
fukasawa e60969
      } /* end switch */
fukasawa e60969
    }
fukasawa e60969
    else if (fp_rd == stdin)
fukasawa e60969
    {
fukasawa e60969
      if ((fp_rd = fopen (argv[argi], "rb")) == NULL)
fukasawa e60969
      {
fukasawa e60969
        fprintf (stderr, "PNM2PNG\n");
fukasawa e60969
        fprintf (stderr, "Error:  file %s does not exist\n", argv[argi]);
fukasawa e60969
        exit (1);
fukasawa e60969
      }
fukasawa e60969
    }
fukasawa e60969
    else if (fp_wr == stdout)
fukasawa e60969
    {
fukasawa e60969
      if ((fp_wr = fopen (argv[argi], "wb")) == NULL)
fukasawa e60969
      {
fukasawa e60969
        fprintf (stderr, "PNM2PNG\n");
fukasawa e60969
        fprintf (stderr, "Error:  can not create PNG-file %s\n", argv[argi]);
fukasawa e60969
        exit (1);
fukasawa e60969
      }
fukasawa e60969
    }
fukasawa e60969
    else
fukasawa e60969
    {
fukasawa e60969
      fprintf (stderr, "PNM2PNG\n");
fukasawa e60969
      fprintf (stderr, "Error:  too many parameters\n");
fukasawa e60969
      usage();
fukasawa e60969
      exit (1);
fukasawa e60969
    }
fukasawa e60969
  } /* end for */
fukasawa e60969
fukasawa e60969
#ifdef __TURBOC__
fukasawa e60969
  /* set stdin/stdout to binary, we're reading the PNM always! in binary format */
fukasawa e60969
  if (fp_rd == stdin)
fukasawa e60969
  {
fukasawa e60969
    setmode (STDIN, O_BINARY);
fukasawa e60969
  }
fukasawa e60969
  if (fp_wr == stdout)
fukasawa e60969
  {
fukasawa e60969
    setmode (STDOUT, O_BINARY);
fukasawa e60969
  }
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
  /* call the conversion program itself */
fukasawa e60969
  if (pnm2png (fp_rd, fp_wr, fp_al, interlace, alpha) == FALSE)
fukasawa e60969
  {
fukasawa e60969
    fprintf (stderr, "PNM2PNG\n");
fukasawa e60969
    fprintf (stderr, "Error:  unsuccessful converting to PNG-image\n");
fukasawa e60969
    exit (1);
fukasawa e60969
  }
fukasawa e60969
fukasawa e60969
  /* close input file */
fukasawa e60969
  fclose (fp_rd);
fukasawa e60969
  /* close output file */
fukasawa e60969
  fclose (fp_wr);
fukasawa e60969
  /* close alpha file */
fukasawa e60969
  if (alpha)
fukasawa e60969
    fclose (fp_al);
fukasawa e60969
fukasawa e60969
  return 0;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/*
fukasawa e60969
 *  usage
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
void usage()
fukasawa e60969
{
fukasawa e60969
  fprintf (stderr, "PNM2PNG\n");
fukasawa e60969
  fprintf (stderr, "   by Willem van Schaik, 1999\n");
fukasawa e60969
#ifdef __TURBOC__
fukasawa e60969
  fprintf (stderr, "   for Turbo-C and Borland-C compilers\n");
fukasawa e60969
#else
fukasawa e60969
  fprintf (stderr, "   for Linux (and Unix) compilers\n");
fukasawa e60969
#endif
fukasawa e60969
  fprintf (stderr, "Usage:  pnm2png [options] <file>.<pnm> [<file>.png]\n");</file></pnm></file>
fukasawa e60969
  fprintf (stderr, "   or:  ... | pnm2png [options]\n");
fukasawa e60969
  fprintf (stderr, "Options:\n");
fukasawa e60969
  fprintf (stderr, "   -i[nterlace]   write png-file with interlacing on\n");
fukasawa e60969
  fprintf (stderr,
fukasawa e60969
      "   -a[lpha] <file>.pgm read PNG alpha channel as pgm-file\n");</file>
fukasawa e60969
  fprintf (stderr, "   -h | -?  print this help-information\n");
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/*
fukasawa e60969
 *  pnm2png
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
BOOL pnm2png (FILE *pnm_file, FILE *png_file, FILE *alpha_file, BOOL interlace,
fukasawa e60969
     BOOL alpha)
fukasawa e60969
{
fukasawa e60969
  png_struct    *png_ptr = NULL;
fukasawa e60969
  png_info      *info_ptr = NULL;
fukasawa e60969
  png_byte      *png_pixels = NULL;
fukasawa e60969
  png_byte      **row_pointers = NULL;
fukasawa e60969
  png_byte      *pix_ptr = NULL;
fukasawa e60969
  volatile png_uint_32   row_bytes;
fukasawa e60969
fukasawa e60969
  char          type_token[16];
fukasawa e60969
  char          width_token[16];
fukasawa e60969
  char          height_token[16];
fukasawa e60969
  char          maxval_token[16];
fukasawa e60969
  volatile int    color_type=1;
fukasawa e60969
  unsigned long   ul_width=0, ul_alpha_width=0;
fukasawa e60969
  unsigned long   ul_height=0, ul_alpha_height=0;
fukasawa e60969
  unsigned long   ul_maxval=0;
fukasawa e60969
  volatile png_uint_32   width=0, height=0;
fukasawa e60969
  volatile png_uint_32   alpha_width=0, alpha_height=0;
fukasawa e60969
  png_uint_32   maxval;
fukasawa e60969
  volatile int           bit_depth = 0;
fukasawa e60969
  int           channels=0;
fukasawa e60969
  int           alpha_depth = 0;
fukasawa e60969
  int           alpha_present=0;
fukasawa e60969
  int           row, col;
fukasawa e60969
  BOOL          raw, alpha_raw = FALSE;
fukasawa e60969
#if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
fukasawa e60969
  BOOL          packed_bitmap = FALSE;
fukasawa e60969
#endif
fukasawa e60969
  png_uint_32   tmp16;
fukasawa e60969
  int           i;
fukasawa e60969
fukasawa e60969
  /* read header of PNM file */
fukasawa e60969
fukasawa e60969
  get_token(pnm_file, type_token);
fukasawa e60969
  if (type_token[0] != 'P')
fukasawa e60969
  {
fukasawa e60969
    return FALSE;
fukasawa e60969
  }
fukasawa e60969
  else if ((type_token[1] == '1') || (type_token[1] == '4'))
fukasawa e60969
  {
fukasawa e60969
#if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
fukasawa e60969
    raw = (type_token[1] == '4');
fukasawa e60969
    color_type = PNG_COLOR_TYPE_GRAY;
fukasawa e60969
    get_token(pnm_file, width_token);
fukasawa e60969
    sscanf (width_token, "%lu", &ul_width);
fukasawa e60969
    width = (png_uint_32) ul_width;
fukasawa e60969
    get_token(pnm_file, height_token);
fukasawa e60969
    sscanf (height_token, "%lu", &ul_height);
fukasawa e60969
    height = (png_uint_32) ul_height;
fukasawa e60969
    bit_depth = 1;
fukasawa e60969
    packed_bitmap = TRUE;
fukasawa e60969
#else
fukasawa e60969
    fprintf (stderr, "PNM2PNG built without PNG_WRITE_INVERT_SUPPORTED and \n");
fukasawa e60969
    fprintf (stderr, "PNG_WRITE_PACK_SUPPORTED can't read PBM (P1,P4) files\n");
fukasawa e60969
#endif
fukasawa e60969
  }
fukasawa e60969
  else if ((type_token[1] == '2') || (type_token[1] == '5'))
fukasawa e60969
  {
fukasawa e60969
    raw = (type_token[1] == '5');
fukasawa e60969
    color_type = PNG_COLOR_TYPE_GRAY;
fukasawa e60969
    get_token(pnm_file, width_token);
fukasawa e60969
    sscanf (width_token, "%lu", &ul_width);
fukasawa e60969
    width = (png_uint_32) ul_width;
fukasawa e60969
    get_token(pnm_file, height_token);
fukasawa e60969
    sscanf (height_token, "%lu", &ul_height);
fukasawa e60969
    height = (png_uint_32) ul_height;
fukasawa e60969
    get_token(pnm_file, maxval_token);
fukasawa e60969
    sscanf (maxval_token, "%lu", &ul_maxval);
fukasawa e60969
    maxval = (png_uint_32) ul_maxval;
fukasawa e60969
fukasawa e60969
    if (maxval <= 1)
fukasawa e60969
      bit_depth = 1;
fukasawa e60969
    else if (maxval <= 3)
fukasawa e60969
      bit_depth = 2;
fukasawa e60969
    else if (maxval <= 15)
fukasawa e60969
      bit_depth = 4;
fukasawa e60969
    else if (maxval <= 255)
fukasawa e60969
      bit_depth = 8;
fukasawa e60969
    else /* if (maxval <= 65535) */
fukasawa e60969
      bit_depth = 16;
fukasawa e60969
  }
fukasawa e60969
  else if ((type_token[1] == '3') || (type_token[1] == '6'))
fukasawa e60969
  {
fukasawa e60969
    raw = (type_token[1] == '6');
fukasawa e60969
    color_type = PNG_COLOR_TYPE_RGB;
fukasawa e60969
    get_token(pnm_file, width_token);
fukasawa e60969
    sscanf (width_token, "%lu", &ul_width);
fukasawa e60969
    width = (png_uint_32) ul_width;
fukasawa e60969
    get_token(pnm_file, height_token);
fukasawa e60969
    sscanf (height_token, "%lu", &ul_height);
fukasawa e60969
    height = (png_uint_32) ul_height;
fukasawa e60969
    get_token(pnm_file, maxval_token);
fukasawa e60969
    sscanf (maxval_token, "%lu", &ul_maxval);
fukasawa e60969
    maxval = (png_uint_32) ul_maxval;
fukasawa e60969
    if (maxval <= 1)
fukasawa e60969
      bit_depth = 1;
fukasawa e60969
    else if (maxval <= 3)
fukasawa e60969
      bit_depth = 2;
fukasawa e60969
    else if (maxval <= 15)
fukasawa e60969
      bit_depth = 4;
fukasawa e60969
    else if (maxval <= 255)
fukasawa e60969
      bit_depth = 8;
fukasawa e60969
    else /* if (maxval <= 65535) */
fukasawa e60969
      bit_depth = 16;
fukasawa e60969
  }
fukasawa e60969
  else
fukasawa e60969
  {
fukasawa e60969
    return FALSE;
fukasawa e60969
  }
fukasawa e60969
fukasawa e60969
  /* read header of PGM file with alpha channel */
fukasawa e60969
fukasawa e60969
  if (alpha)
fukasawa e60969
  {
fukasawa e60969
    if (color_type == PNG_COLOR_TYPE_GRAY)
fukasawa e60969
      color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
fukasawa e60969
    if (color_type == PNG_COLOR_TYPE_RGB)
fukasawa e60969
      color_type = PNG_COLOR_TYPE_RGB_ALPHA;
fukasawa e60969
fukasawa e60969
    get_token(alpha_file, type_token);
fukasawa e60969
    if (type_token[0] != 'P')
fukasawa e60969
    {
fukasawa e60969
      return FALSE;
fukasawa e60969
    }
fukasawa e60969
    else if ((type_token[1] == '2') || (type_token[1] == '5'))
fukasawa e60969
    {
fukasawa e60969
      alpha_raw = (type_token[1] == '5');
fukasawa e60969
      get_token(alpha_file, width_token);
fukasawa e60969
      sscanf (width_token, "%lu", &ul_alpha_width);
fukasawa e60969
      alpha_width=(png_uint_32) ul_alpha_width;
fukasawa e60969
      if (alpha_width != width)
fukasawa e60969
        return FALSE;
fukasawa e60969
      get_token(alpha_file, height_token);
fukasawa e60969
      sscanf (height_token, "%lu", &ul_alpha_height);
fukasawa e60969
      alpha_height = (png_uint_32) ul_alpha_height;
fukasawa e60969
      if (alpha_height != height)
fukasawa e60969
        return FALSE;
fukasawa e60969
      get_token(alpha_file, maxval_token);
fukasawa e60969
      sscanf (maxval_token, "%lu", &ul_maxval);
fukasawa e60969
      maxval = (png_uint_32) ul_maxval;
fukasawa e60969
      if (maxval <= 1)
fukasawa e60969
        alpha_depth = 1;
fukasawa e60969
      else if (maxval <= 3)
fukasawa e60969
        alpha_depth = 2;
fukasawa e60969
      else if (maxval <= 15)
fukasawa e60969
        alpha_depth = 4;
fukasawa e60969
      else if (maxval <= 255)
fukasawa e60969
        alpha_depth = 8;
fukasawa e60969
      else /* if (maxval <= 65535) */
fukasawa e60969
        alpha_depth = 16;
fukasawa e60969
      if (alpha_depth != bit_depth)
fukasawa e60969
        return FALSE;
fukasawa e60969
    }
fukasawa e60969
    else
fukasawa e60969
    {
fukasawa e60969
      return FALSE;
fukasawa e60969
    }
fukasawa e60969
  } /* end if alpha */
fukasawa e60969
fukasawa e60969
  /* calculate the number of channels and store alpha-presence */
fukasawa e60969
  if (color_type == PNG_COLOR_TYPE_GRAY)
fukasawa e60969
    channels = 1;
fukasawa e60969
  else if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
fukasawa e60969
    channels = 2;
fukasawa e60969
  else if (color_type == PNG_COLOR_TYPE_RGB)
fukasawa e60969
    channels = 3;
fukasawa e60969
  else if (color_type == PNG_COLOR_TYPE_RGB_ALPHA)
fukasawa e60969
    channels = 4;
fukasawa e60969
#if 0
fukasawa e60969
  else
fukasawa e60969
    channels = 0; /* cannot happen */
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
  alpha_present = (channels - 1) % 2;
fukasawa e60969
fukasawa e60969
#if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
fukasawa e60969
  if (packed_bitmap)
fukasawa e60969
    /* row data is as many bytes as can fit width x channels x bit_depth */
fukasawa e60969
    row_bytes = (width * channels * bit_depth + 7) / 8;
fukasawa e60969
  else
fukasawa e60969
#endif
fukasawa e60969
    /* row_bytes is the width x number of channels x (bit-depth / 8) */
fukasawa e60969
    row_bytes = width * channels * ((bit_depth <= 8) ? 1 : 2);
fukasawa e60969
fukasawa e60969
  if ((png_pixels = (png_byte *)
fukasawa e60969
     malloc (row_bytes * height * sizeof (png_byte))) == NULL)
fukasawa e60969
    return FALSE;
fukasawa e60969
fukasawa e60969
  /* read data from PNM file */
fukasawa e60969
  pix_ptr = png_pixels;
fukasawa e60969
fukasawa e60969
  for (row = 0; row < (int) height; row++)
fukasawa e60969
  {
fukasawa e60969
#if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
fukasawa e60969
    if (packed_bitmap) {
fukasawa e60969
      for (i = 0; i < (int) row_bytes; i++)
fukasawa e60969
        /* png supports this format natively so no conversion is needed */
fukasawa e60969
        *pix_ptr++ = get_data (pnm_file, 8);
fukasawa e60969
    } else
fukasawa e60969
#endif
fukasawa e60969
    {
fukasawa e60969
      for (col = 0; col < (int) width; col++)
fukasawa e60969
      {
fukasawa e60969
        for (i = 0; i < (channels - alpha_present); i++)
fukasawa e60969
        {
fukasawa e60969
          if (raw)
fukasawa e60969
            *pix_ptr++ = get_data (pnm_file, bit_depth);
fukasawa e60969
          else
fukasawa e60969
            if (bit_depth <= 8)
fukasawa e60969
              *pix_ptr++ = get_value (pnm_file, bit_depth);
fukasawa e60969
            else
fukasawa e60969
            {
fukasawa e60969
              tmp16 = get_value (pnm_file, bit_depth);
fukasawa e60969
              *pix_ptr = (png_byte) ((tmp16 >> 8) & 0xFF);
fukasawa e60969
              pix_ptr++;
fukasawa e60969
              *pix_ptr = (png_byte) (tmp16 & 0xFF);
fukasawa e60969
              pix_ptr++;
fukasawa e60969
            }
fukasawa e60969
        }
fukasawa e60969
fukasawa e60969
        if (alpha) /* read alpha-channel from pgm file */
fukasawa e60969
        {
fukasawa e60969
          if (alpha_raw)
fukasawa e60969
            *pix_ptr++ = get_data (alpha_file, alpha_depth);
fukasawa e60969
          else
fukasawa e60969
            if (alpha_depth <= 8)
fukasawa e60969
              *pix_ptr++ = get_value (alpha_file, bit_depth);
fukasawa e60969
            else
fukasawa e60969
            {
fukasawa e60969
              tmp16 = get_value (alpha_file, bit_depth);
fukasawa e60969
              *pix_ptr++ = (png_byte) ((tmp16 >> 8) & 0xFF);
fukasawa e60969
              *pix_ptr++ = (png_byte) (tmp16 & 0xFF);
fukasawa e60969
            }
fukasawa e60969
        } /* if alpha */
fukasawa e60969
      } /* if packed_bitmap */
fukasawa e60969
    } /* end for col */
fukasawa e60969
  } /* end for row */
fukasawa e60969
fukasawa e60969
  /* prepare the standard PNG structures */
fukasawa e60969
  png_ptr = png_create_write_struct (png_get_libpng_ver(NULL), NULL, NULL,
fukasawa e60969
      NULL);
fukasawa e60969
  if (!png_ptr)
fukasawa e60969
  {
fukasawa e60969
    free (png_pixels);
fukasawa e60969
    png_pixels = NULL;
fukasawa e60969
    return FALSE;
fukasawa e60969
  }
fukasawa e60969
  info_ptr = png_create_info_struct (png_ptr);
fukasawa e60969
  if (!info_ptr)
fukasawa e60969
  {
fukasawa e60969
    png_destroy_write_struct (&png_ptr, (png_infopp) NULL);
fukasawa e60969
    free (png_pixels);
fukasawa e60969
    png_pixels = NULL;
fukasawa e60969
    return FALSE;
fukasawa e60969
  }
fukasawa e60969
fukasawa e60969
#if defined(PNG_WRITE_INVERT_SUPPORTED) || defined(PNG_WRITE_PACK_SUPPORTED)
fukasawa e60969
  if (packed_bitmap == TRUE)
fukasawa e60969
  {
fukasawa e60969
    png_set_packing (png_ptr);
fukasawa e60969
    png_set_invert_mono (png_ptr);
fukasawa e60969
  }
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
  /* setjmp() must be called in every function that calls a PNG-reading libpng function */
fukasawa e60969
  if (setjmp (png_jmpbuf(png_ptr)))
fukasawa e60969
  {
fukasawa e60969
    png_destroy_write_struct (&png_ptr, &info_ptr);
fukasawa e60969
    free (png_pixels);
fukasawa e60969
    png_pixels = NULL;
fukasawa e60969
    return FALSE;
fukasawa e60969
  }
fukasawa e60969
fukasawa e60969
  /* initialize the png structure */
fukasawa e60969
  png_init_io (png_ptr, png_file);
fukasawa e60969
fukasawa e60969
  /* we're going to write more or less the same PNG as the input file */
fukasawa e60969
  png_set_IHDR (png_ptr, info_ptr, width, height, bit_depth, color_type,
fukasawa e60969
    (!interlace) ? PNG_INTERLACE_NONE : PNG_INTERLACE_ADAM7,
fukasawa e60969
    PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
fukasawa e60969
fukasawa e60969
  /* write the file header information */
fukasawa e60969
  png_write_info (png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
  /* if needed we will allocate memory for an new array of row-pointers */
fukasawa e60969
  if (row_pointers == (unsigned char**) NULL)
fukasawa e60969
  {
fukasawa e60969
    if ((row_pointers = (png_byte **)
fukasawa e60969
        malloc (height * sizeof (png_bytep))) == NULL)
fukasawa e60969
    {
fukasawa e60969
      png_destroy_write_struct (&png_ptr, &info_ptr);
fukasawa e60969
      free (png_pixels);
fukasawa e60969
      png_pixels = NULL;
fukasawa e60969
      return FALSE;
fukasawa e60969
    }
fukasawa e60969
  }
fukasawa e60969
fukasawa e60969
  /* set the individual row_pointers to point at the correct offsets */
fukasawa e60969
  for (i = 0; i < (int) height; i++)
fukasawa e60969
    row_pointers[i] = png_pixels + i * row_bytes;
fukasawa e60969
fukasawa e60969
  /* write out the entire image data in one call */
fukasawa e60969
  png_write_image (png_ptr, row_pointers);
fukasawa e60969
fukasawa e60969
  /* write the additional chunks to the PNG file (not really needed) */
fukasawa e60969
  png_write_end (png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
  /* clean up after the write, and free any memory allocated */
fukasawa e60969
  png_destroy_write_struct (&png_ptr, &info_ptr);
fukasawa e60969
fukasawa e60969
  if (row_pointers != (unsigned char**) NULL)
fukasawa e60969
    free (row_pointers);
fukasawa e60969
  if (png_pixels != (unsigned char*) NULL)
fukasawa e60969
    free (png_pixels);
fukasawa e60969
fukasawa e60969
  return TRUE;
fukasawa e60969
} /* end of pnm2png */
fukasawa e60969
fukasawa e60969
/*
fukasawa e60969
 * get_token() - gets the first string after whitespace
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
void get_token(FILE *pnm_file, char *token)
fukasawa e60969
{
fukasawa e60969
  int i = 0;
fukasawa e60969
  int ret;
fukasawa e60969
fukasawa e60969
  /* remove white-space and comment lines */
fukasawa e60969
  do
fukasawa e60969
  {
fukasawa e60969
    ret = fgetc(pnm_file);
fukasawa e60969
    if (ret == '#') {
fukasawa e60969
      /* the rest of this line is a comment */
fukasawa e60969
      do
fukasawa e60969
      {
fukasawa e60969
        ret = fgetc(pnm_file);
fukasawa e60969
      }
fukasawa e60969
      while ((ret != '\n') && (ret != '\r') && (ret != EOF));
fukasawa e60969
    }
fukasawa e60969
    if (ret == EOF) break;
fukasawa e60969
    token[i] = (unsigned char) ret;
fukasawa e60969
  }
fukasawa e60969
  while ((token[i] == '\n') || (token[i] == '\r') || (token[i] == ' '));
fukasawa e60969
fukasawa e60969
  /* read string */
fukasawa e60969
  do
fukasawa e60969
  {
fukasawa e60969
    ret = fgetc(pnm_file);
fukasawa e60969
    if (ret == EOF) break;
fukasawa e60969
    i++;
fukasawa e60969
    token[i] = (unsigned char) ret;
fukasawa e60969
  }
fukasawa e60969
  while ((token[i] != '\n') && (token[i] != '\r') && (token[i] != ' '));
fukasawa e60969
fukasawa e60969
  token[i] = '\0';
fukasawa e60969
fukasawa e60969
  return;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/*
fukasawa e60969
 * get_data() - takes first byte and converts into next pixel value,
fukasawa e60969
 *        taking as much bits as defined by bit-depth and
fukasawa e60969
 *        using the bit-depth to fill up a byte (0Ah -> AAh)
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
png_uint_32 get_data (FILE *pnm_file, int depth)
fukasawa e60969
{
fukasawa e60969
  static int bits_left = 0;
fukasawa e60969
  static int old_value = 0;
fukasawa e60969
  static int mask = 0;
fukasawa e60969
  int i;
fukasawa e60969
  png_uint_32 ret_value;
fukasawa e60969
fukasawa e60969
  if (mask == 0)
fukasawa e60969
    for (i = 0; i < depth; i++)
fukasawa e60969
      mask = (mask >> 1) | 0x80;
fukasawa e60969
fukasawa e60969
  if (bits_left <= 0)
fukasawa e60969
  {
fukasawa e60969
    old_value = fgetc (pnm_file);
fukasawa e60969
    bits_left = 8;
fukasawa e60969
  }
fukasawa e60969
fukasawa e60969
  ret_value = old_value & mask;
fukasawa e60969
  for (i = 1; i < (8 / depth); i++)
fukasawa e60969
    ret_value = ret_value || (ret_value >> depth);
fukasawa e60969
fukasawa e60969
  old_value = (old_value << depth) & 0xFF;
fukasawa e60969
  bits_left -= depth;
fukasawa e60969
fukasawa e60969
  return ret_value;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/*
fukasawa e60969
 * get_value() - takes first (numeric) string and converts into number,
fukasawa e60969
 *         using the bit-depth to fill up a byte (0Ah -> AAh)
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
png_uint_32 get_value (FILE *pnm_file, int depth)
fukasawa e60969
{
fukasawa e60969
  static png_uint_32 mask = 0;
fukasawa e60969
  png_byte token[16];
fukasawa e60969
  unsigned long ul_ret_value;
fukasawa e60969
  png_uint_32 ret_value;
fukasawa e60969
  int i = 0;
fukasawa e60969
fukasawa e60969
  if (mask == 0)
fukasawa e60969
    for (i = 0; i < depth; i++)
fukasawa e60969
      mask = (mask << 1) | 0x01;
fukasawa e60969
fukasawa e60969
  get_token (pnm_file, (char *) token);
fukasawa e60969
  sscanf ((const char *) token, "%lu", &ul_ret_value);
fukasawa e60969
  ret_value = (png_uint_32) ul_ret_value;
fukasawa e60969
fukasawa e60969
  ret_value &= mask;
fukasawa e60969
fukasawa e60969
  if (depth < 8)
fukasawa e60969
    for (i = 0; i < (8 / depth); i++)
fukasawa e60969
      ret_value = (ret_value << depth) || ret_value;
fukasawa e60969
fukasawa e60969
  return ret_value;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
/* end of source */
fukasawa e60969