fukasawa e60969
/*
fukasawa e60969
 *  png2pnm.c --- conversion from PNG-file to PGM/PPM-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
 *
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
#ifdef __TURBOC__
fukasawa e60969
#define STDIN  0
fukasawa e60969
#define STDOUT 1
fukasawa e60969
#define STDERR 2
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* to make png2pnm 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 png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file, BOOL raw,
fukasawa e60969
   BOOL alpha);
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_wr = stdout;
fukasawa e60969
  FILE *fp_al = NULL;
fukasawa e60969
  BOOL raw = TRUE;
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 'n':
fukasawa e60969
          raw = FALSE;
fukasawa e60969
          break;
fukasawa e60969
        case 'r':
fukasawa e60969
          raw = TRUE;
fukasawa e60969
          break;
fukasawa e60969
        case 'a':
fukasawa e60969
          alpha = TRUE;
fukasawa e60969
          argi++;
fukasawa e60969
          if ((fp_al = fopen (argv[argi], "wb")) == NULL)
fukasawa e60969
          {
fukasawa e60969
            fprintf (stderr, "PNM2PNG\n");
fukasawa e60969
            fprintf (stderr, "Error:  can not create alpha-channel file %s\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, "PNG2PNM\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, "PNG2PNM\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, "PNG2PNM\n");
fukasawa e60969
        fprintf (stderr, "Error:  can not create file %s\n", argv[argi]);
fukasawa e60969
        exit (1);
fukasawa e60969
      }
fukasawa e60969
    }
fukasawa e60969
    else
fukasawa e60969
    {
fukasawa e60969
      fprintf (stderr, "PNG2PNM\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 if required to binary */
fukasawa e60969
  if (fp_rd == stdin)
fukasawa e60969
  {
fukasawa e60969
    setmode (STDIN, O_BINARY);
fukasawa e60969
  }
fukasawa e60969
  if ((raw) && (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 (png2pnm (fp_rd, fp_wr, fp_al, raw, alpha) == FALSE)
fukasawa e60969
  {
fukasawa e60969
    fprintf (stderr, "PNG2PNM\n");
fukasawa e60969
    fprintf (stderr, "Error:  unsuccessful conversion of 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, "PNG2PNM\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:  png2pnm [options] <file>.png [<file>.pnm]\n");</file></file>
fukasawa e60969
  fprintf (stderr, "   or:  ... | png2pnm [options]\n");
fukasawa e60969
  fprintf (stderr, "Options:\n");
fukasawa e60969
  fprintf (stderr,
fukasawa e60969
     "   -r[aw]   write pnm-file in binary format (P4/P5/P6) (default)\n");
fukasawa e60969
  fprintf (stderr, "   -n[oraw] write pnm-file in ascii format (P1/P2/P3)\n");
fukasawa e60969
  fprintf (stderr,
fukasawa e60969
     "   -a[lpha] <file>.pgm write 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
 *  png2pnm
fukasawa e60969
 */
fukasawa e60969
fukasawa e60969
BOOL png2pnm (FILE *png_file, FILE *pnm_file, FILE *alpha_file,
fukasawa e60969
    volatile BOOL raw, BOOL alpha)
fukasawa e60969
{
fukasawa e60969
  png_struct    *png_ptr = NULL;
fukasawa e60969
  png_info        *info_ptr = NULL;
fukasawa e60969
  png_byte      buf[8];
fukasawa e60969
  png_byte      *png_pixels = NULL;
fukasawa e60969
  png_byte      **row_pointers = NULL;
fukasawa e60969
  png_byte      *pix_ptr = NULL;
fukasawa e60969
  png_uint_32   row_bytes;
fukasawa e60969
fukasawa e60969
  png_uint_32   width;
fukasawa e60969
  png_uint_32   height;
fukasawa e60969
  int           bit_depth;
fukasawa e60969
  int           channels;
fukasawa e60969
  int           color_type;
fukasawa e60969
  int           alpha_present;
fukasawa e60969
  int           row, col;
fukasawa e60969
  int           ret;
fukasawa e60969
  int           i;
fukasawa e60969
  long          dep_16;
fukasawa e60969
fukasawa e60969
  /* read and check signature in PNG file */
fukasawa e60969
  ret = fread (buf, 1, 8, png_file);
fukasawa e60969
  if (ret != 8)
fukasawa e60969
    return FALSE;
fukasawa e60969
fukasawa e60969
  ret = png_sig_cmp (buf, 0, 8);
fukasawa e60969
  if (ret)
fukasawa e60969
    return FALSE;
fukasawa e60969
fukasawa e60969
  /* create png and info structures */
fukasawa e60969
fukasawa e60969
  png_ptr = png_create_read_struct (png_get_libpng_ver(NULL),
fukasawa e60969
    NULL, NULL, NULL);
fukasawa e60969
  if (!png_ptr)
fukasawa e60969
    return FALSE;   /* out of memory */
fukasawa e60969
fukasawa e60969
  info_ptr = png_create_info_struct (png_ptr);
fukasawa e60969
  if (!info_ptr)
fukasawa e60969
  {
fukasawa e60969
    png_destroy_read_struct (&png_ptr, NULL, NULL);
fukasawa e60969
    return FALSE;   /* out of memory */
fukasawa e60969
  }
fukasawa e60969
fukasawa e60969
  if (setjmp (png_jmpbuf(png_ptr)))
fukasawa e60969
  {
fukasawa e60969
    png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
fukasawa e60969
    return FALSE;
fukasawa e60969
  }
fukasawa e60969
fukasawa e60969
  /* set up the input control for C streams */
fukasawa e60969
  png_init_io (png_ptr, png_file);
fukasawa e60969
  png_set_sig_bytes (png_ptr, 8);  /* we already read the 8 signature bytes */
fukasawa e60969
fukasawa e60969
  /* read the file information */
fukasawa e60969
  png_read_info (png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
  /* get size and bit-depth of the PNG-image */
fukasawa e60969
  png_get_IHDR (png_ptr, info_ptr,
fukasawa e60969
    &width, &height, &bit_depth, &color_type,
fukasawa e60969
    NULL, NULL, NULL);
fukasawa e60969
fukasawa e60969
  /* set-up the transformations */
fukasawa e60969
fukasawa e60969
  /* transform paletted images into full-color rgb */
fukasawa e60969
  if (color_type == PNG_COLOR_TYPE_PALETTE)
fukasawa e60969
    png_set_expand (png_ptr);
fukasawa e60969
  /* expand images to bit-depth 8 (only applicable for grayscale images) */
fukasawa e60969
  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
fukasawa e60969
    png_set_expand (png_ptr);
fukasawa e60969
  /* transform transparency maps into full alpha-channel */
fukasawa e60969
  if (png_get_valid (png_ptr, info_ptr, PNG_INFO_tRNS))
fukasawa e60969
    png_set_expand (png_ptr);
fukasawa e60969
fukasawa e60969
#ifdef NJET
fukasawa e60969
  /* downgrade 16-bit images to 8-bit */
fukasawa e60969
  if (bit_depth == 16)
fukasawa e60969
    png_set_strip_16 (png_ptr);
fukasawa e60969
  /* transform grayscale images into full-color */
fukasawa e60969
  if (color_type == PNG_COLOR_TYPE_GRAY ||
fukasawa e60969
    color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
fukasawa e60969
    png_set_gray_to_rgb (png_ptr);
fukasawa e60969
  /* only if file has a file gamma, we do a correction */
fukasawa e60969
  if (png_get_gAMA (png_ptr, info_ptr, &file_gamma))
fukasawa e60969
    png_set_gamma (png_ptr, (double) 2.2, file_gamma);
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
  /* all transformations have been registered; now update info_ptr data,
fukasawa e60969
   * get rowbytes and channels, and allocate image memory */
fukasawa e60969
fukasawa e60969
  png_read_update_info (png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
  /* get the new color-type and bit-depth (after expansion/stripping) */
fukasawa e60969
  png_get_IHDR (png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
fukasawa e60969
    NULL, NULL, NULL);
fukasawa e60969
fukasawa e60969
  /* check for 16-bit files */
fukasawa e60969
  if (bit_depth == 16)
fukasawa e60969
  {
fukasawa e60969
    raw = FALSE;
fukasawa e60969
#ifdef __TURBOC__
fukasawa e60969
    pnm_file->flags &= ~((unsigned) _F_BIN);
fukasawa e60969
#endif
fukasawa e60969
  }
fukasawa e60969
fukasawa e60969
  /* calculate new 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
  else
fukasawa e60969
    channels = 0; /* should never happen */
fukasawa e60969
  alpha_present = (channels - 1) % 2;
fukasawa e60969
fukasawa e60969
  /* check if alpha is expected to be present in file */
fukasawa e60969
  if (alpha && !alpha_present)
fukasawa e60969
  {
fukasawa e60969
    fprintf (stderr, "PNG2PNM\n");
fukasawa e60969
    fprintf (stderr, "Error:  PNG-file doesn't contain alpha channel\n");
fukasawa e60969
    exit (1);
fukasawa e60969
  }
fukasawa e60969
fukasawa e60969
  /* row_bytes is the width x number of channels x (bit-depth / 8) */
fukasawa e60969
  row_bytes = png_get_rowbytes (png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
  if ((png_pixels = (png_byte *)
fukasawa e60969
     malloc (row_bytes * height * sizeof (png_byte))) == NULL) {
fukasawa e60969
    png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
fukasawa e60969
    return FALSE;
fukasawa e60969
  }
fukasawa e60969
fukasawa e60969
  if ((row_pointers = (png_byte **)
fukasawa e60969
     malloc (height * sizeof (png_bytep))) == NULL)
fukasawa e60969
  {
fukasawa e60969
    png_destroy_read_struct (&png_ptr, &info_ptr, NULL);
fukasawa e60969
    free (png_pixels);
fukasawa e60969
    png_pixels = NULL;
fukasawa e60969
    return FALSE;
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
  /* now we can go ahead and just read the whole image */
fukasawa e60969
  png_read_image (png_ptr, row_pointers);
fukasawa e60969
fukasawa e60969
  /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
fukasawa e60969
  png_read_end (png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
  /* clean up after the read, and free any memory allocated - REQUIRED */
fukasawa e60969
  png_destroy_read_struct (&png_ptr, &info_ptr, (png_infopp) NULL);
fukasawa e60969
fukasawa e60969
  /* write header of PNM file */
fukasawa e60969
fukasawa e60969
  if ((color_type == PNG_COLOR_TYPE_GRAY) ||
fukasawa e60969
      (color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
fukasawa e60969
  {
fukasawa e60969
    fprintf (pnm_file, "%s\n", (raw) ? "P5" : "P2");
fukasawa e60969
    fprintf (pnm_file, "%d %d\n", (int) width, (int) height);
fukasawa e60969
    fprintf (pnm_file, "%ld\n", ((1L << (int) bit_depth) - 1L));
fukasawa e60969
  }
fukasawa e60969
  else if ((color_type == PNG_COLOR_TYPE_RGB) ||
fukasawa e60969
           (color_type == PNG_COLOR_TYPE_RGB_ALPHA))
fukasawa e60969
  {
fukasawa e60969
    fprintf (pnm_file, "%s\n", (raw) ? "P6" : "P3");
fukasawa e60969
    fprintf (pnm_file, "%d %d\n", (int) width, (int) height);
fukasawa e60969
    fprintf (pnm_file, "%ld\n", ((1L << (int) bit_depth) - 1L));
fukasawa e60969
  }
fukasawa e60969
fukasawa e60969
  /* write header of PGM file with alpha channel */
fukasawa e60969
fukasawa e60969
  if ((alpha) &&
fukasawa e60969
      ((color_type == PNG_COLOR_TYPE_GRAY_ALPHA) ||
fukasawa e60969
       (color_type == PNG_COLOR_TYPE_RGB_ALPHA)))
fukasawa e60969
  {
fukasawa e60969
    fprintf (alpha_file, "%s\n", (raw) ? "P5" : "P2");
fukasawa e60969
    fprintf (alpha_file, "%d %d\n", (int) width, (int) height);
fukasawa e60969
    fprintf (alpha_file, "%ld\n", ((1L << (int) bit_depth) - 1L));
fukasawa e60969
  }
fukasawa e60969
fukasawa e60969
  /* write data to PNM file */
fukasawa e60969
  pix_ptr = png_pixels;
fukasawa e60969
fukasawa e60969
  for (row = 0; row < (int) height; row++)
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
          fputc ((int) *pix_ptr++ , pnm_file);
fukasawa e60969
        else
fukasawa e60969
          if (bit_depth == 16){
fukasawa e60969
            dep_16 = (long) *pix_ptr++;
fukasawa e60969
            fprintf (pnm_file, "%ld ", (dep_16 << 8) + ((long) *pix_ptr++));
fukasawa e60969
          }
fukasawa e60969
          else
fukasawa e60969
            fprintf (pnm_file, "%ld ", (long) *pix_ptr++);
fukasawa e60969
      }
fukasawa e60969
      if (alpha_present)
fukasawa e60969
      {
fukasawa e60969
        if (!alpha)
fukasawa e60969
        {
fukasawa e60969
          pix_ptr++; /* alpha */
fukasawa e60969
          if (bit_depth == 16)
fukasawa e60969
            pix_ptr++;
fukasawa e60969
        }
fukasawa e60969
        else /* output alpha-channel as pgm file */
fukasawa e60969
        {
fukasawa e60969
          if (raw)
fukasawa e60969
            fputc ((int) *pix_ptr++ , alpha_file);
fukasawa e60969
          else
fukasawa e60969
            if (bit_depth == 16){
fukasawa e60969
              dep_16 = (long) *pix_ptr++;
fukasawa e60969
              fprintf (alpha_file, "%ld ", (dep_16 << 8) + (long) *pix_ptr++);
fukasawa e60969
            }
fukasawa e60969
            else
fukasawa e60969
              fprintf (alpha_file, "%ld ", (long) *pix_ptr++);
fukasawa e60969
        }
fukasawa e60969
      } /* if alpha_present */
fukasawa e60969
fukasawa e60969
      if (!raw)
fukasawa e60969
        if (col % 4 == 3)
fukasawa e60969
          fprintf (pnm_file, "\n");
fukasawa e60969
    } /* end for col */
fukasawa e60969
fukasawa e60969
    if (!raw)
fukasawa e60969
      if (col % 4 != 0)
fukasawa e60969
        fprintf (pnm_file, "\n");
fukasawa e60969
  } /* end for row */
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
fukasawa e60969
} /* end of source */
fukasawa e60969