src/third-party/libpng/contrib/gregbook/readppm.c (view raw)
1/*---------------------------------------------------------------------------
2
3 rpng - simple PNG display program readppm.c
4
5 ---------------------------------------------------------------------------
6
7 This is a special-purpose replacement for readpng.c that allows binary
8 PPM files to be used in place of PNG images.
9
10 ---------------------------------------------------------------------------
11
12 Copyright (c) 1998-2007 Greg Roelofs. All rights reserved.
13
14 This software is provided "as is," without warranty of any kind,
15 express or implied. In no event shall the author or contributors
16 be held liable for any damages arising in any way from the use of
17 this software.
18
19 The contents of this file are DUAL-LICENSED. You may modify and/or
20 redistribute this software according to the terms of one of the
21 following two licenses (at your option):
22
23
24 LICENSE 1 ("BSD-like with advertising clause"):
25
26 Permission is granted to anyone to use this software for any purpose,
27 including commercial applications, and to alter it and redistribute
28 it freely, subject to the following restrictions:
29
30 1. Redistributions of source code must retain the above copyright
31 notice, disclaimer, and this list of conditions.
32 2. Redistributions in binary form must reproduce the above copyright
33 notice, disclaimer, and this list of conditions in the documenta-
34 tion and/or other materials provided with the distribution.
35 3. All advertising materials mentioning features or use of this
36 software must display the following acknowledgment:
37
38 This product includes software developed by Greg Roelofs
39 and contributors for the book, "PNG: The Definitive Guide,"
40 published by O'Reilly and Associates.
41
42
43 LICENSE 2 (GNU GPL v2 or later):
44
45 This program is free software; you can redistribute it and/or modify
46 it under the terms of the GNU General Public License as published by
47 the Free Software Foundation; either version 2 of the License, or
48 (at your option) any later version.
49
50 This program is distributed in the hope that it will be useful,
51 but WITHOUT ANY WARRANTY; without even the implied warranty of
52 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
53 GNU General Public License for more details.
54
55 You should have received a copy of the GNU General Public License
56 along with this program; if not, write to the Free Software Foundation,
57 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
58
59 ---------------------------------------------------------------------------*/
60
61#include <stdio.h>
62#include <stdlib.h>
63
64#include "readpng.h" /* typedefs, common macros, public prototypes */
65
66
67ulg width, height;
68int bit_depth, color_type, channels;
69uch *image_data = NULL;
70FILE *saved_infile;
71
72
73void readpng_version_info()
74{
75 fprintf(stderr, " Compiled without libpng, zlib or PBMPLUS/NetPBM.\n");
76}
77
78
79/* return value = 0 for success, 1 for bad sig, 2 for bad IHDR, 4 for no mem */
80
81int readpng_init(FILE *infile, ulg *pWidth, ulg *pHeight)
82{
83 static uch ppmline[256];
84 int maxval;
85
86
87 saved_infile = infile;
88
89 fgets(ppmline, 256, infile);
90 if (ppmline[0] != 'P' || ppmline[1] != '6') {
91 fprintf(stderr, "ERROR: not a PPM file\n");
92 return 1;
93 }
94 /* possible color types: P5 = grayscale (0), P6 = RGB (2), P8 = RGBA (6) */
95 if (ppmline[1] == '6') {
96 color_type = 2;
97 channels = 3;
98 } else if (ppmline[1] == '8') {
99 color_type = 6;
100 channels = 4;
101 } else /* if (ppmline[1] == '5') */ {
102 color_type = 0;
103 channels = 1;
104 }
105
106 do {
107 fgets(ppmline, 256, infile);
108 } while (ppmline[0] == '#');
109 sscanf(ppmline, "%lu %lu", &width, &height);
110
111 do {
112 fgets(ppmline, 256, infile);
113 } while (ppmline[0] == '#');
114 sscanf(ppmline, "%d", &maxval);
115 if (maxval != 255) {
116 fprintf(stderr, "ERROR: maxval = %d\n", maxval);
117 return 2;
118 }
119 bit_depth = 8;
120
121 *pWidth = width;
122 *pHeight = height;
123
124 return 0;
125}
126
127
128
129
130/* returns 0 if succeeds, 1 if fails due to no bKGD chunk, 2 if libpng error;
131 * scales values to 8-bit if necessary */
132
133int readpng_get_bgcolor(uch *red, uch *green, uch *blue)
134{
135 return 1;
136}
137
138
139
140
141/* display_exponent == LUT_exponent * CRT_exponent */
142
143uch *readpng_get_image(double display_exponent, int *pChannels, ulg *pRowbytes)
144{
145 ulg rowbytes;
146
147
148 /* expand palette images to RGB, low-bit-depth grayscale images to 8 bits,
149 * transparency chunks to full alpha channel; strip 16-bit-per-sample
150 * images to 8 bits per sample; and convert grayscale to RGB[A] */
151
152 /* GRR WARNING: grayscale needs to be expanded and channels reset! */
153
154 *pRowbytes = rowbytes = channels*width;
155 *pChannels = channels;
156
157 if ((image_data = (uch *)malloc(rowbytes*height)) == NULL) {
158 return NULL;
159 }
160
161 Trace((stderr, "readpng_get_image: rowbytes = %ld, height = %ld\n", rowbytes, height));
162
163
164 /* now we can go ahead and just read the whole image */
165
166 if (fread(image_data, 1L, rowbytes*height, saved_infile) <
167 rowbytes*height) {
168 free (image_data);
169 image_data = NULL;
170 return NULL;
171 }
172
173 return image_data;
174}
175
176
177void readpng_cleanup(int free_image_data)
178{
179 if (free_image_data && image_data) {
180 free(image_data);
181 image_data = NULL;
182 }
183}