Visualization Library 2.0.0-b5

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
readpng.c
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------
2 
3  rpng - simple PNG display program readpng.c
4 
5  ---------------------------------------------------------------------------
6 
7  Copyright (c) 1998-2007 Greg Roelofs. All rights reserved.
8 
9  This software is provided "as is," without warranty of any kind,
10  express or implied. In no event shall the author or contributors
11  be held liable for any damages arising in any way from the use of
12  this software.
13 
14  The contents of this file are DUAL-LICENSED. You may modify and/or
15  redistribute this software according to the terms of one of the
16  following two licenses (at your option):
17 
18 
19  LICENSE 1 ("BSD-like with advertising clause"):
20 
21  Permission is granted to anyone to use this software for any purpose,
22  including commercial applications, and to alter it and redistribute
23  it freely, subject to the following restrictions:
24 
25  1. Redistributions of source code must retain the above copyright
26  notice, disclaimer, and this list of conditions.
27  2. Redistributions in binary form must reproduce the above copyright
28  notice, disclaimer, and this list of conditions in the documenta-
29  tion and/or other materials provided with the distribution.
30  3. All advertising materials mentioning features or use of this
31  software must display the following acknowledgment:
32 
33  This product includes software developed by Greg Roelofs
34  and contributors for the book, "PNG: The Definitive Guide,"
35  published by O'Reilly and Associates.
36 
37 
38  LICENSE 2 (GNU GPL v2 or later):
39 
40  This program is free software; you can redistribute it and/or modify
41  it under the terms of the GNU General Public License as published by
42  the Free Software Foundation; either version 2 of the License, or
43  (at your option) any later version.
44 
45  This program is distributed in the hope that it will be useful,
46  but WITHOUT ANY WARRANTY; without even the implied warranty of
47  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
48  GNU General Public License for more details.
49 
50  You should have received a copy of the GNU General Public License
51  along with this program; if not, write to the Free Software Foundation,
52  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
53 
54  ---------------------------------------------------------------------------*/
55 
56 #include <stdio.h>
57 #include <stdlib.h>
58 
59 #include "png.h" /* libpng header; includes zlib.h */
60 #include "readpng.h" /* typedefs, common macros, public prototypes */
61 
62 /* future versions of libpng will provide this macro: */
63 #ifndef png_jmpbuf
64 # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
65 #endif
66 
67 
68 static png_structp png_ptr = NULL;
69 static png_infop info_ptr = NULL;
70 
74 
75 
77 {
78  fprintf(stderr, " Compiled with libpng %s; using libpng %s.\n",
80  fprintf(stderr, " Compiled with zlib %s; using zlib %s.\n",
82 }
83 
84 
85 /* return value = 0 for success, 1 for bad sig, 2 for bad IHDR, 4 for no mem */
86 
87 int readpng_init(FILE *infile, ulg *pWidth, ulg *pHeight)
88 {
89  uch sig[8];
90 
91 
92  /* first do a quick check that the file really is a PNG image; could
93  * have used slightly more general png_sig_cmp() function instead */
94 
95  fread(sig, 1, 8, infile);
96  if (png_sig_cmp(sig, 0, 8))
97  return 1; /* bad signature */
98 
99 
100  /* could pass pointers to user-defined error handlers instead of NULLs: */
101 
103  if (!png_ptr)
104  return 4; /* out of memory */
105 
106  info_ptr = png_create_info_struct(png_ptr);
107  if (!info_ptr) {
108  png_destroy_read_struct(&png_ptr, NULL, NULL);
109  return 4; /* out of memory */
110  }
111 
112 
113  /* we could create a second info struct here (end_info), but it's only
114  * useful if we want to keep pre- and post-IDAT chunk info separated
115  * (mainly for PNG-aware image editors and converters) */
116 
117 
118  /* setjmp() must be called in every function that calls a PNG-reading
119  * libpng function */
120 
121  if (setjmp(png_jmpbuf(png_ptr))) {
123  return 2;
124  }
125 
126 
127  png_init_io(png_ptr, infile);
128  png_set_sig_bytes(png_ptr, 8); /* we already read the 8 signature bytes */
129 
130  png_read_info(png_ptr, info_ptr); /* read all PNG info up to image data */
131 
132 
133  /* alternatively, could make separate calls to png_get_image_width(),
134  * etc., but want bit_depth and color_type for later [don't care about
135  * compression_type and filter_type => NULLs] */
136 
138  NULL, NULL, NULL);
139  *pWidth = width;
140  *pHeight = height;
141 
142 
143  /* OK, that's all we need for now; return happy */
144 
145  return 0;
146 }
147 
148 
149 
150 
151 /* returns 0 if succeeds, 1 if fails due to no bKGD chunk, 2 if libpng error;
152  * scales values to 8-bit if necessary */
153 
155 {
156  png_color_16p pBackground;
157 
158 
159  /* setjmp() must be called in every function that calls a PNG-reading
160  * libpng function */
161 
162  if (setjmp(png_jmpbuf(png_ptr))) {
164  return 2;
165  }
166 
167 
168  if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_bKGD))
169  return 1;
170 
171  /* it is not obvious from the libpng documentation, but this function
172  * takes a pointer to a pointer, and it always returns valid red, green
173  * and blue values, regardless of color_type: */
174 
175  png_get_bKGD(png_ptr, info_ptr, &pBackground);
176 
177 
178  /* however, it always returns the raw bKGD data, regardless of any
179  * bit-depth transformations, so check depth and adjust if necessary */
180 
181  if (bit_depth == 16) {
182  *red = pBackground->red >> 8;
183  *green = pBackground->green >> 8;
184  *blue = pBackground->blue >> 8;
185  } else if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
186  if (bit_depth == 1)
187  *red = *green = *blue = pBackground->gray? 255 : 0;
188  else if (bit_depth == 2)
189  *red = *green = *blue = (255/3) * pBackground->gray;
190  else /* bit_depth == 4 */
191  *red = *green = *blue = (255/15) * pBackground->gray;
192  } else {
193  *red = (uch)pBackground->red;
194  *green = (uch)pBackground->green;
195  *blue = (uch)pBackground->blue;
196  }
197 
198  return 0;
199 }
200 
201 
202 
203 
204 /* display_exponent == LUT_exponent * CRT_exponent */
205 
206 uch *readpng_get_image(double display_exponent, int *pChannels, ulg *pRowbytes)
207 {
208  double gamma;
209  png_uint_32 i, rowbytes;
211 
212 
213  /* setjmp() must be called in every function that calls a PNG-reading
214  * libpng function */
215 
216  if (setjmp(png_jmpbuf(png_ptr))) {
217  free(image_data);
218  image_data = NULL;
219  free(row_pointers);
220  row_pointers = NULL;
222  return NULL;
223  }
224 
225 
226  /* expand palette images to RGB, low-bit-depth grayscale images to 8 bits,
227  * transparency chunks to full alpha channel; strip 16-bit-per-sample
228  * images to 8 bits per sample; and convert grayscale to RGB[A] */
229 
231  png_set_expand(png_ptr);
233  png_set_expand(png_ptr);
234  if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
235  png_set_expand(png_ptr);
236  if (bit_depth == 16)
237  png_set_strip_16(png_ptr);
240  png_set_gray_to_rgb(png_ptr);
241 
242 
243  /* unlike the example in the libpng documentation, we have *no* idea where
244  * this file may have come from--so if it doesn't have a file gamma, don't
245  * do any correction ("do no harm") */
246 
247  if (png_get_gAMA(png_ptr, info_ptr, &gamma))
248  png_set_gamma(png_ptr, display_exponent, gamma);
249 
250 
251  /* all transformations have been registered; now update info_ptr data,
252  * get rowbytes and channels, and allocate image memory */
253 
254  png_read_update_info(png_ptr, info_ptr);
255 
256  *pRowbytes = rowbytes = png_get_rowbytes(png_ptr, info_ptr);
257  *pChannels = (int)png_get_channels(png_ptr, info_ptr);
258 
259  if ((image_data = (uch *)malloc(rowbytes*height)) == NULL) {
261  return NULL;
262  }
263  if ((row_pointers = (png_bytepp)malloc(height*sizeof(png_bytep))) == NULL) {
265  free(image_data);
266  image_data = NULL;
267  return NULL;
268  }
269 
270  Trace((stderr, "readpng_get_image: channels = %d, rowbytes = %ld, height = %ld\n", *pChannels, rowbytes, height));
271 
272 
273  /* set the individual row_pointers to point at the correct offsets */
274 
275  for (i = 0; i < height; ++i)
276  row_pointers[i] = image_data + i*rowbytes;
277 
278 
279  /* now we can go ahead and just read the whole image */
280 
281  png_read_image(png_ptr, row_pointers);
282 
283 
284  /* and we're done! (png_read_end() can be omitted if no processing of
285  * post-IDAT text/time/etc. is desired) */
286 
287  free(row_pointers);
288  row_pointers = NULL;
289 
290  png_read_end(png_ptr, NULL);
291 
292  return image_data;
293 }
294 
295 
296 void readpng_cleanup(int free_image_data)
297 {
298  if (free_image_data && image_data) {
299  free(image_data);
300  image_data = NULL;
301  }
302 
303  if (png_ptr && info_ptr) {
305  png_ptr = NULL;
306  info_ptr = NULL;
307  }
308 }
#define png_jmpbuf(png_ptr)
Definition: readpng.c:64
GLint GLint GLsizei GLsizei height
PNG_CONST char png_libpng_ver[18]
Definition: png.c:28
png_infop PNGAPI png_create_info_struct(png_structp png_ptr)
Definition: png.c:253
#define NULL
Definition: ftobjs.h:61
png_uint_32 height
Definition: readpng.c:71
png_uint_32 PNGAPI png_get_valid(png_structp png_ptr, png_infop info_ptr, png_uint_32 flag)
Definition: pngget.c:21
png_color_16 FAR * png_color_16p
Definition: png.h:444
#define PNG_COLOR_TYPE_GRAY_ALPHA
Definition: png.h:872
png_byte FAR * png_bytep
Definition: pngconf.h:1328
#define PNG_COLOR_TYPE_PALETTE
Definition: png.h:869
png_uint_32 PNGAPI png_get_gAMA(png_structp png_ptr, png_infop info_ptr, double *file_gamma)
Definition: pngget.c:475
char * malloc()
#define PNG_LIBPNG_VER_STRING
Definition: png.h:273
GLint GLint GLsizei width
void PNGAPI png_set_sig_bytes(png_structp png_ptr, int num_bytes)
Definition: png.c:99
void PNGAPI png_set_strip_16(png_structp png_ptr)
Definition: pngrtran.c:122
GLfloat green
unsigned long ulg
Definition: zutil.h:38
png_uint_32 i
Definition: png.h:2640
png_uint_32 width
Definition: readpng.c:71
#define zlib_version
Definition: zlib.h:210
#define ZLIB_VERSION
Definition: zlib.h:40
GLfloat GLfloat blue
png_byte FAR *FAR * png_bytepp
Definition: pngconf.h:1350
png_infop png_bytepp row_pointers
Definition: png.h:2021
png_struct FAR * png_structp
Definition: png.h:973
void PNGAPI png_read_end(png_structp png_ptr, png_infop info_ptr)
Definition: pngread.c:953
uch * image_data
Definition: readpng.c:73
png_uint_32 PNGAPI png_get_IHDR(png_structp png_ptr, png_infop info_ptr, png_uint_32 *width, png_uint_32 *height, int *bit_depth, int *color_type, int *interlace_type, int *compression_type, int *filter_type)
Definition: pngget.c:577
uch * readpng_get_image(double display_exponent, int *pChannels, ulg *pRowbytes)
Definition: readpng.c:206
#define PNG_COLOR_TYPE_GRAY
Definition: png.h:868
int free()
void PNGAPI png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
Definition: pngrtran.c:552
int PNGAPI png_sig_cmp(png_bytep sig, png_size_t start, png_size_t num_to_check)
Definition: png.c:121
void PNGAPI png_read_info(png_structp png_ptr, png_infop info_ptr)
Definition: pngread.c:353
#define Trace(x)
Definition: zutil.h:197
void readpng_version_info(void)
Definition: readpng.c:76
typedef int
Definition: png.h:978
png_structp PNGAPI png_create_read_struct(png_const_charp user_png_ver, png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warn_fn)
Definition: pngread.c:24
FILE * infile
Definition: cdjpeg.h:141
#define PNG_INFO_tRNS
Definition: png.h:936
void PNGAPI png_set_expand(png_structp png_ptr)
Definition: pngrtran.c:574
png_uint_32 PNGAPI png_get_bKGD(png_structp png_ptr, png_infop info_ptr, png_color_16p *background)
Definition: pngget.c:391
void PNGAPI png_read_image(png_structp png_ptr, png_bytepp image)
Definition: pngread.c:911
int readpng_get_bgcolor(uch *red, uch *green, uch *blue)
Definition: readpng.c:154
int double red
Definition: png.h:1498
png_infop info_ptr
Definition: png.h:1443
unsigned long png_uint_32
Definition: pngconf.h:1247
png_uint_32 PNGAPI png_get_rowbytes(png_structp png_ptr, png_infop info_ptr)
Definition: pngget.c:31
void readpng_cleanup(int free_image_data)
Definition: readpng.c:296
void PNGAPI png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr, png_infopp end_info_ptr_ptr)
Definition: pngread.c:1127
int bit_depth
Definition: readpng.c:72
#define PNG_INFO_bKGD
Definition: png.h:937
void PNGAPI png_read_update_info(png_structp png_ptr, png_infop info_ptr)
Definition: pngread.c:563
int readpng_init(FILE *infile, ulg *pWidth, ulg *pHeight)
Definition: readpng.c:87
void PNGAPI png_init_io(png_structp png_ptr, png_FILE_p fp)
Definition: png.c:660
int color_type
Definition: readpng.c:72
png_byte PNGAPI png_get_channels(png_structp png_ptr, png_infop info_ptr)
Definition: pngget.c:372
void PNGAPI png_set_gray_to_rgb(png_structp png_ptr)
Definition: pngrtran.c:660
unsigned char uch
Definition: zutil.h:34
png_info FAR * png_infop
Definition: png.h:849