Blame contrib/gregbook/readppm.c

Packit 0ba690
/*---------------------------------------------------------------------------
Packit 0ba690
Packit 0ba690
   rpng - simple PNG display program                              readppm.c
Packit 0ba690
Packit 0ba690
  ---------------------------------------------------------------------------
Packit 0ba690
Packit 0ba690
   This is a special-purpose replacement for readpng.c that allows binary
Packit 0ba690
   PPM files to be used in place of PNG images.
Packit 0ba690
Packit 0ba690
  ---------------------------------------------------------------------------
Packit 0ba690
Packit 0ba690
      Copyright (c) 1998-2007 Greg Roelofs.  All rights reserved.
Packit 0ba690
Packit 0ba690
      This software is provided "as is," without warranty of any kind,
Packit 0ba690
      express or implied.  In no event shall the author or contributors
Packit 0ba690
      be held liable for any damages arising in any way from the use of
Packit 0ba690
      this software.
Packit 0ba690
Packit 0ba690
      The contents of this file are DUAL-LICENSED.  You may modify and/or
Packit 0ba690
      redistribute this software according to the terms of one of the
Packit 0ba690
      following two licenses (at your option):
Packit 0ba690
Packit 0ba690
Packit 0ba690
      LICENSE 1 ("BSD-like with advertising clause"):
Packit 0ba690
Packit 0ba690
      Permission is granted to anyone to use this software for any purpose,
Packit 0ba690
      including commercial applications, and to alter it and redistribute
Packit 0ba690
      it freely, subject to the following restrictions:
Packit 0ba690
Packit 0ba690
      1. Redistributions of source code must retain the above copyright
Packit 0ba690
         notice, disclaimer, and this list of conditions.
Packit 0ba690
      2. Redistributions in binary form must reproduce the above copyright
Packit 0ba690
         notice, disclaimer, and this list of conditions in the documenta-
Packit 0ba690
         tion and/or other materials provided with the distribution.
Packit 0ba690
      3. All advertising materials mentioning features or use of this
Packit 0ba690
         software must display the following acknowledgment:
Packit 0ba690
Packit 0ba690
            This product includes software developed by Greg Roelofs
Packit 0ba690
            and contributors for the book, "PNG: The Definitive Guide,"
Packit 0ba690
            published by O'Reilly and Associates.
Packit 0ba690
Packit 0ba690
Packit 0ba690
      LICENSE 2 (GNU GPL v2 or later):
Packit 0ba690
Packit 0ba690
      This program is free software; you can redistribute it and/or modify
Packit 0ba690
      it under the terms of the GNU General Public License as published by
Packit 0ba690
      the Free Software Foundation; either version 2 of the License, or
Packit 0ba690
      (at your option) any later version.
Packit 0ba690
Packit 0ba690
      This program is distributed in the hope that it will be useful,
Packit 0ba690
      but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 0ba690
      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 0ba690
      GNU General Public License for more details.
Packit 0ba690
Packit 0ba690
      You should have received a copy of the GNU General Public License
Packit 0ba690
      along with this program; if not, write to the Free Software Foundation,
Packit 0ba690
      Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Packit 0ba690
Packit 0ba690
  ---------------------------------------------------------------------------*/
Packit 0ba690
Packit 0ba690
#include <stdio.h>
Packit 0ba690
#include <stdlib.h>
Packit 0ba690
Packit 0ba690
#include "readpng.h"    /* typedefs, common macros, public prototypes */
Packit 0ba690
Packit 0ba690
Packit 0ba690
ulg  width, height;
Packit 0ba690
int  bit_depth, color_type, channels;
Packit 0ba690
uch  *image_data = NULL;
Packit 0ba690
FILE *saved_infile;
Packit 0ba690
Packit 0ba690
Packit 0ba690
void readpng_version_info()
Packit 0ba690
{
Packit 0ba690
    fprintf(stderr, "   Compiled without libpng, zlib or PBMPLUS/NetPBM.\n");
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
Packit 0ba690
/* return value = 0 for success, 1 for bad sig, 2 for bad IHDR, 4 for no mem */
Packit 0ba690
Packit 0ba690
int readpng_init(FILE *infile, ulg *pWidth, ulg *pHeight)
Packit 0ba690
{
Packit 0ba690
    static uch ppmline[256];
Packit 0ba690
    int maxval;
Packit 0ba690
Packit 0ba690
Packit 0ba690
    saved_infile = infile;
Packit 0ba690
Packit 0ba690
    fgets(ppmline, 256, infile);
Packit 0ba690
    if (ppmline[0] != 'P' || ppmline[1] != '6') {
Packit 0ba690
        fprintf(stderr, "ERROR:  not a PPM file\n");
Packit 0ba690
        return 1;
Packit 0ba690
    }
Packit 0ba690
    /* possible color types:  P5 = grayscale (0), P6 = RGB (2), P8 = RGBA (6) */
Packit 0ba690
    if (ppmline[1] == '6') {
Packit 0ba690
        color_type = 2;
Packit 0ba690
        channels = 3;
Packit 0ba690
    } else if (ppmline[1] == '8') {
Packit 0ba690
        color_type = 6;
Packit 0ba690
        channels = 4;
Packit 0ba690
    } else /* if (ppmline[1] == '5') */ {
Packit 0ba690
        color_type = 0;
Packit 0ba690
        channels = 1;
Packit 0ba690
    }
Packit 0ba690
Packit 0ba690
    do {
Packit 0ba690
        fgets(ppmline, 256, infile);
Packit 0ba690
    } while (ppmline[0] == '#');
Packit 0ba690
    sscanf(ppmline, "%lu %lu", &width, &height);
Packit 0ba690
Packit 0ba690
    do {
Packit 0ba690
        fgets(ppmline, 256, infile);
Packit 0ba690
    } while (ppmline[0] == '#');
Packit 0ba690
    sscanf(ppmline, "%d", &maxval);
Packit 0ba690
    if (maxval != 255) {
Packit 0ba690
        fprintf(stderr, "ERROR:  maxval = %d\n", maxval);
Packit 0ba690
        return 2;
Packit 0ba690
    }
Packit 0ba690
    bit_depth = 8;
Packit 0ba690
Packit 0ba690
    *pWidth = width;
Packit 0ba690
    *pHeight = height;
Packit 0ba690
Packit 0ba690
    return 0;
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
Packit 0ba690
Packit 0ba690
Packit 0ba690
/* returns 0 if succeeds, 1 if fails due to no bKGD chunk, 2 if libpng error;
Packit 0ba690
 * scales values to 8-bit if necessary */
Packit 0ba690
Packit 0ba690
int readpng_get_bgcolor(uch *red, uch *green, uch *blue)
Packit 0ba690
{
Packit 0ba690
    return 1;
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
Packit 0ba690
Packit 0ba690
Packit 0ba690
/* display_exponent == LUT_exponent * CRT_exponent */
Packit 0ba690
Packit 0ba690
uch *readpng_get_image(double display_exponent, int *pChannels, ulg *pRowbytes)
Packit 0ba690
{
Packit 0ba690
    ulg  rowbytes;
Packit 0ba690
Packit 0ba690
Packit 0ba690
    /* expand palette images to RGB, low-bit-depth grayscale images to 8 bits,
Packit 0ba690
     * transparency chunks to full alpha channel; strip 16-bit-per-sample
Packit 0ba690
     * images to 8 bits per sample; and convert grayscale to RGB[A] */
Packit 0ba690
Packit 0ba690
    /* GRR WARNING:  grayscale needs to be expanded and channels reset! */
Packit 0ba690
Packit 0ba690
    *pRowbytes = rowbytes = channels*width;
Packit 0ba690
    *pChannels = channels;
Packit 0ba690
Packit 0ba690
    if ((image_data = (uch *)malloc(rowbytes*height)) == NULL) {
Packit 0ba690
        return NULL;
Packit 0ba690
    }
Packit 0ba690
Packit 0ba690
    Trace((stderr, "readpng_get_image:  rowbytes = %ld, height = %ld\n", rowbytes, height));
Packit 0ba690
Packit 0ba690
Packit 0ba690
    /* now we can go ahead and just read the whole image */
Packit 0ba690
Packit 0ba690
    fread(image_data, 1L, rowbytes*height, saved_infile);
Packit 0ba690
Packit 0ba690
Packit 0ba690
    return image_data;
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
Packit 0ba690
void readpng_cleanup(int free_image_data)
Packit 0ba690
{
Packit 0ba690
    if (free_image_data && image_data) {
Packit 0ba690
        free(image_data);
Packit 0ba690
        image_data = NULL;
Packit 0ba690
    }
Packit 0ba690
}