Updating to version 0.20.2
[wmaker-crm.git] / wrlib / png.c
blobfa2144229c534db6104c84fd176fd45aa124ff35
1 /* png.c - load PNG image from file
2 *
3 * Raster graphics library
4 *
5 * Copyright (c) 1997 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.
22 #include <config.h>
24 /* AIX requires this to be the first thing in the file. */
25 #ifdef __GNUC__
26 # define alloca __builtin_alloca
27 #else
28 # if HAVE_ALLOCA_H
29 # include <alloca.h>
30 # else
31 # ifdef _AIX
32 # pragma alloca
33 # else
34 # ifndef alloca /* predefined by HP cc +Olibcalls */
35 char *alloca ();
36 # endif
37 # endif
38 # endif
39 #endif
42 #ifdef USE_PNG
44 #include <stdlib.h>
45 #include <stdio.h>
46 #include <string.h>
48 #include <png.h>
50 #if PNG_LIBPNG_VER < 96
51 #error libpng_must_be_more_at_least_0_96
52 #endif
54 #include "wraster.h"
57 RImage*
58 RLoadPNG(RContext *context, char *file, int index)
60 char *tmp;
61 RImage *image=NULL;
62 FILE *f;
63 png_structp png;
64 png_infop pinfo, einfo;
65 png_color_16p bkcolor;
66 int alpha;
67 int x, y, i;
68 double gamma, sgamma;
69 png_uint_32 width, height;
70 int depth, junk, color_type;
71 png_bytep *png_rows;
72 unsigned char *r, *g, *b, *a;
74 f = fopen(file, "r");
75 if (!f) {
76 RErrorCode = RERR_OPEN;
77 return NULL;
79 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
80 (png_error_ptr)NULL, (png_error_ptr)NULL);
81 if (!png) {
82 RErrorCode = RERR_NOMEMORY;
83 fclose(f);
84 return NULL;
87 pinfo = png_create_info_struct(png);
88 if (!pinfo) {
89 RErrorCode = RERR_NOMEMORY;
90 fclose(f);
91 png_destroy_read_struct(&png, NULL, NULL);
92 return NULL;
95 einfo = png_create_info_struct(png);
96 if (!einfo) {
97 RErrorCode = RERR_NOMEMORY;
98 fclose(f);
99 png_destroy_read_struct(&png, &pinfo, NULL);
100 return NULL;
103 RErrorCode = RERR_INTERNAL;
104 if (setjmp(png->jmpbuf)) {
105 fclose(f);
106 png_destroy_read_struct(&png, &pinfo, &einfo);
107 if (image)
108 RDestroyImage(image);
109 return NULL;
112 png_init_io(png, f);
114 png_read_info(png, pinfo);
116 png_get_IHDR(png, pinfo, &width, &height, &depth, &color_type,
117 &junk, &junk, &junk);
120 /* check for an alpha channel */
121 if (png_get_valid(png, pinfo, PNG_INFO_tRNS))
122 alpha = True;
123 else
124 alpha = (color_type & PNG_COLOR_MASK_ALPHA);
126 /* allocate RImage */
127 image = RCreateImage(width, height, alpha);
128 if (!image) {
129 fclose(f);
130 png_destroy_read_struct(&png, &pinfo, &einfo);
131 return NULL;
134 /* normalize to 8bpp with alpha channel */
135 if (color_type == PNG_COLOR_TYPE_PALETTE && depth < 8)
136 png_set_expand(png);
138 if (color_type == PNG_COLOR_TYPE_GRAY && depth < 8)
139 png_set_expand(png);
141 if (png_get_valid(png, pinfo, PNG_INFO_tRNS))
142 png_set_expand(png);
144 if (depth == 16)
145 png_set_strip_16(png);
147 if (color_type == PNG_COLOR_TYPE_GRAY ||
148 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
149 png_set_gray_to_rgb(png);
151 /* set gamma correction */
152 if ((context->attribs->flags & RC_GammaCorrection)
153 && context->depth != 8) {
154 sgamma = (context->attribs->rgamma + context->attribs->ggamma +
155 context->attribs->bgamma) / 3;
156 } else if ((tmp = getenv("DISPLAY_GAMMA")) != NULL) {
157 sgamma = atof(tmp);
158 if (sgamma==0)
159 sgamma = 1;
160 } else {
161 sgamma = 2.0;
164 if (png_get_gAMA(png, pinfo, &gamma))
165 png_set_gamma(png, sgamma, gamma);
166 else
167 png_set_gamma(png, sgamma, 0.45);
169 /* do the transforms */
170 png_read_update_info(png, pinfo);
172 /* set background color */
173 if (png_get_bKGD(png, pinfo, &bkcolor)) {
174 image->background.red = bkcolor->red >> 8;
175 image->background.green = bkcolor->green >> 8;
176 image->background.blue = bkcolor->blue >> 8;
179 png_rows = alloca(sizeof(char*)*height);
180 if (!png_rows) {
181 RErrorCode = RERR_NOMEMORY;
182 fclose(f);
183 RDestroyImage(image);
184 png_destroy_read_struct(&png, &pinfo, &einfo);
185 #ifdef C_ALLOCA
186 alloca(0);
187 #endif
188 return NULL;
190 for (y=0; y<height; y++) {
191 png_rows[y] = alloca(png_get_rowbytes(png, pinfo));
192 if (!png_rows[y]) {
193 RErrorCode = RERR_NOMEMORY;
194 fclose(f);
195 RDestroyImage(image);
196 png_destroy_read_struct(&png, &pinfo, &einfo);
197 #ifdef C_ALLOCA
198 alloca(0);
199 #endif
200 return NULL;
203 /* read data */
204 png_read_image(png, png_rows);
206 png_read_end(png, einfo);
208 png_destroy_read_struct(&png, &pinfo, &einfo);
210 fclose(f);
212 r = image->data[0];
213 g = image->data[1];
214 b = image->data[2];
215 a = image->data[3];
217 /* convert to RImage */
218 if (alpha) {
219 for (y=0; y<height; y++) {
220 for (x=0, i=0; x<width; x++) {
221 *(r++) = *(png_rows[y]+i++);
222 *(g++) = *(png_rows[y]+i++);
223 *(b++) = *(png_rows[y]+i++);
224 *(a++) = *(png_rows[y]+i++);
227 } else {
228 for (y=0; y<height; y++) {
229 for (x=0, i=0; x<width; x++) {
230 *(r++) = *(png_rows[y]+i++);
231 *(g++) = *(png_rows[y]+i++);
232 *(b++) = *(png_rows[y]+i++);
236 #ifdef C_ALLOCA
237 alloca(0);
238 #endif
239 return image;
242 #endif /* USE_PNG */