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