1 /* png.c - load PNG image from file
3 * Raster graphics library
5 * Copyright (c) 1997-2003 Alfredo K. Kojima
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the Free
19 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /* AIX requires this to be the first thing in the file. */
26 # define alloca __builtin_alloca
34 # ifndef alloca /* predefined by HP cc +Olibcalls */
51 RImage *RLoadPNG(RContext * context, char *file, int index)
57 png_infop pinfo, einfo;
58 png_color_16p bkcolor;
62 png_uint_32 width, height;
63 int depth, junk, color_type;
67 f = fopen(file, "rb");
69 RErrorCode = RERR_OPEN;
72 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, (png_error_ptr) NULL, (png_error_ptr) NULL);
74 RErrorCode = RERR_NOMEMORY;
79 pinfo = png_create_info_struct(png);
81 RErrorCode = RERR_NOMEMORY;
83 png_destroy_read_struct(&png, NULL, NULL);
87 einfo = png_create_info_struct(png);
89 RErrorCode = RERR_NOMEMORY;
91 png_destroy_read_struct(&png, &pinfo, NULL);
95 RErrorCode = RERR_INTERNAL;
96 if (setjmp(png->jmpbuf)) {
98 png_destroy_read_struct(&png, &pinfo, &einfo);
100 RReleaseImage(image);
106 png_read_info(png, pinfo);
108 png_get_IHDR(png, pinfo, &width, &height, &depth, &color_type, &junk, &junk, &junk);
111 if (width < 1 || height < 1) {
113 png_destroy_read_struct(&png, &pinfo, &einfo);
114 RErrorCode = RERR_BADIMAGEFILE;
118 /* check for an alpha channel */
119 if (png_get_valid(png, pinfo, PNG_INFO_tRNS))
122 alpha = (color_type & PNG_COLOR_MASK_ALPHA);
124 /* allocate RImage */
125 image = RCreateImage(width, height, alpha);
128 png_destroy_read_struct(&png, &pinfo, &einfo);
132 /* normalize to 8bpp with alpha channel */
133 if (color_type == PNG_COLOR_TYPE_PALETTE && depth <= 8)
136 if (color_type == PNG_COLOR_TYPE_GRAY && depth <= 8)
139 if (png_get_valid(png, pinfo, PNG_INFO_tRNS))
143 png_set_strip_16(png);
145 if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
146 png_set_gray_to_rgb(png);
148 /* set gamma correction */
149 if ((context->attribs->flags & RC_GammaCorrection)
150 && context->depth != 8) {
151 sgamma = (context->attribs->rgamma + context->attribs->ggamma + context->attribs->bgamma) / 3;
152 } else if ((tmp = getenv("DISPLAY_GAMMA")) != NULL) {
161 if (png_get_gAMA(png, pinfo, &gamma))
162 png_set_gamma(png, sgamma, gamma);
164 png_set_gamma(png, sgamma, 0.45);
166 /* do the transforms */
167 png_read_update_info(png, pinfo);
169 /* set background color */
170 if (png_get_bKGD(png, pinfo, &bkcolor)) {
171 image->background.red = bkcolor->red >> 8;
172 image->background.green = bkcolor->green >> 8;
173 image->background.blue = bkcolor->blue >> 8;
176 png_rows = alloca(sizeof(char *) * height);
178 RErrorCode = RERR_NOMEMORY;
180 RReleaseImage(image);
181 png_destroy_read_struct(&png, &pinfo, &einfo);
187 for (y = 0; y < height; y++) {
188 png_rows[y] = alloca(png_get_rowbytes(png, pinfo));
190 RErrorCode = RERR_NOMEMORY;
192 RReleaseImage(image);
193 png_destroy_read_struct(&png, &pinfo, &einfo);
201 png_read_image(png, png_rows);
203 png_read_end(png, einfo);
205 png_destroy_read_struct(&png, &pinfo, &einfo);
211 /* convert to RImage */
213 for (y = 0; y < height; y++) {
214 for (x = 0, i = width * 4; x < i; x++, ptr++) {
215 *ptr = *(png_rows[y] + x);
219 for (y = 0; y < height; y++) {
220 for (x = 0, i = width * 3; x < i; x++, ptr++) {
221 *ptr = *(png_rows[y] + x);