• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,2017 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 
67 ulg  width, height;
68 int  bit_depth, color_type, channels;
69 uch  *image_data = NULL;
70 FILE *saved_infile;
71 
72 
readpng_version_info()73 void 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 
readpng_init(FILE * infile,ulg * pWidth,ulg * pHeight)81 int 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 
readpng_get_bgcolor(uch * red,uch * green,uch * blue)133 int 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 
readpng_get_image(double display_exponent,int * pChannels,ulg * pRowbytes)143 uch *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     Trace((stderr, "readpng_get_image:  rowbytes = %ld, height = %ld\n", rowbytes, height));
158 
159     /* Guard against integer overflow */
160     if (height > ((size_t)(-1))/rowbytes) {
161         fprintf(stderr, PROGNAME ":  image_data buffer would be too large\n",
162         return NULL;
163     }
164 
165     if ((image_data = (uch *)malloc(rowbytes*height)) == NULL) {
166         return NULL;
167     }
168 
169     /* now we can go ahead and just read the whole image */
170 
171     if (fread(image_data, 1L, rowbytes*height, saved_infile) <
172        rowbytes*height) {
173         free (image_data);
174         image_data = NULL;
175         return NULL;
176     }
177 
178     return image_data;
179 }
180 
181 
182 void readpng_cleanup(int free_image_data)
183 {
184     if (free_image_data && image_data) {
185         free(image_data);
186         image_data = NULL;
187     }
188 }
189