Initial revision
[wmaker-crm.git] / wrlib / png.c
blobadd88aa6baedfc904766c22273575f4b1e0b8fdd
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 sprintf(RErrorString, "could not open file \"%s\"", file);
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 sprintf(RErrorString, "error loading PNG file \"%s\"", file);
83 fclose(f);
84 return NULL;
87 pinfo = png_create_info_struct(png);
88 if (!pinfo) {
89 sprintf(RErrorString, "error loading PNG file \"%s\"", file);
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 sprintf(RErrorString, "error loading PNG file \"%s\"", file);
98 fclose(f);
99 png_destroy_read_struct(&png, &pinfo, NULL);
100 return NULL;
103 if (setjmp(png->jmpbuf)) {
104 sprintf(RErrorString, "error loading PNG file \"%s\"", file);
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 sprintf(RErrorString, "could not create RImage");
130 fclose(f);
131 png_destroy_read_struct(&png, &pinfo, &einfo);
132 return NULL;
135 /* normalize to 8bpp with alpha channel */
136 if (color_type == PNG_COLOR_TYPE_PALETTE && depth < 8)
137 png_set_expand(png);
139 if (color_type == PNG_COLOR_TYPE_GRAY && depth < 8)
140 png_set_expand(png);
142 if (png_get_valid(png, pinfo, PNG_INFO_tRNS))
143 png_set_expand(png);
145 if (depth == 16)
146 png_set_strip_16(png);
148 if (color_type == PNG_COLOR_TYPE_GRAY ||
149 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
150 png_set_gray_to_rgb(png);
152 /* set gamma correction */
153 if ((context->attribs->flags & RC_GammaCorrection)
154 && context->depth != 8) {
155 sgamma = (context->attribs->rgamma + context->attribs->ggamma +
156 context->attribs->bgamma) / 3;
157 } else if ((tmp = getenv("DISPLAY_GAMMA")) != NULL) {
158 sgamma = atof(tmp);
159 if (sgamma==0)
160 sgamma = 1;
161 } else {
162 sgamma = 2.0;
165 if (png_get_gAMA(png, pinfo, &gamma))
166 png_set_gamma(png, sgamma, gamma);
167 else
168 png_set_gamma(png, sgamma, 0.45);
170 /* do the transforms */
171 png_read_update_info(png, pinfo);
173 /* set background color */
174 if (png_get_bKGD(png, pinfo, &bkcolor)) {
175 image->background.red = bkcolor->red >> 8;
176 image->background.green = bkcolor->green >> 8;
177 image->background.blue = bkcolor->blue >> 8;
180 png_rows = alloca(sizeof(char*)*height);
181 if (!png_rows) {
182 sprintf(RErrorString, "out of memory loading \"%s\"", file);
183 fclose(f);
184 RDestroyImage(image);
185 png_destroy_read_struct(&png, &pinfo, &einfo);
186 #ifdef C_ALLOCA
187 alloca(0);
188 #endif
189 return NULL;
191 for (y=0; y<height; y++) {
192 png_rows[y] = alloca(png_get_rowbytes(png, pinfo));
193 if (!png_rows[y]) {
194 sprintf(RErrorString, "out of memory loading \"%s\"", file);
195 fclose(f);
196 RDestroyImage(image);
197 png_destroy_read_struct(&png, &pinfo, &einfo);
198 #ifdef C_ALLOCA
199 alloca(0);
200 #endif
201 return NULL;
204 /* read data */
205 png_read_image(png, png_rows);
207 png_read_end(png, einfo);
209 png_destroy_read_struct(&png, &pinfo, &einfo);
211 fclose(f);
213 r = image->data[0];
214 g = image->data[1];
215 b = image->data[2];
216 a = image->data[3];
218 /* convert to RImage */
219 if (alpha) {
220 for (y=0; y<height; y++) {
221 for (x=0, i=0; x<width; x++) {
222 *(r++) = *(png_rows[y]+i++);
223 *(g++) = *(png_rows[y]+i++);
224 *(b++) = *(png_rows[y]+i++);
225 *(a++) = *(png_rows[y]+i++);
228 } else {
229 for (y=0; y<height; y++) {
230 for (x=0, i=0; x<width; x++) {
231 *(r++) = *(png_rows[y]+i++);
232 *(g++) = *(png_rows[y]+i++);
233 *(b++) = *(png_rows[y]+i++);
237 #ifdef C_ALLOCA
238 alloca(0);
239 #endif
240 return image;
243 #endif /* USE_PNG */