Added WMSetTabViewFont().
[wmaker-crm.git] / wrlib / png.c
blob0c666cdf5c5bc6ff2a783fe73435c409dc7e275f
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 #include "wraster.h"
53 RImage*
54 RLoadPNG(RContext *context, char *file, int index)
56 char *tmp;
57 RImage *image=NULL;
58 FILE *f;
59 png_structp png;
60 png_infop pinfo, einfo;
61 png_color_16p bkcolor;
62 int alpha;
63 int x, y, i;
64 double gamma, sgamma;
65 png_uint_32 width, height;
66 int depth, junk, color_type;
67 png_bytep *png_rows;
68 unsigned char *r, *g, *b, *a;
70 f = fopen(file, "r");
71 if (!f) {
72 RErrorCode = RERR_OPEN;
73 return NULL;
75 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL,
76 (png_error_ptr)NULL, (png_error_ptr)NULL);
77 if (!png) {
78 RErrorCode = RERR_NOMEMORY;
79 fclose(f);
80 return NULL;
83 pinfo = png_create_info_struct(png);
84 if (!pinfo) {
85 RErrorCode = RERR_NOMEMORY;
86 fclose(f);
87 png_destroy_read_struct(&png, NULL, NULL);
88 return NULL;
91 einfo = png_create_info_struct(png);
92 if (!einfo) {
93 RErrorCode = RERR_NOMEMORY;
94 fclose(f);
95 png_destroy_read_struct(&png, &pinfo, NULL);
96 return NULL;
99 RErrorCode = RERR_INTERNAL;
100 if (setjmp(png->jmpbuf)) {
101 fclose(f);
102 png_destroy_read_struct(&png, &pinfo, &einfo);
103 if (image)
104 RDestroyImage(image);
105 return NULL;
108 png_init_io(png, f);
110 png_read_info(png, pinfo);
112 png_get_IHDR(png, pinfo, &width, &height, &depth, &color_type,
113 &junk, &junk, &junk);
116 /* check for an alpha channel */
117 if (png_get_valid(png, pinfo, PNG_INFO_tRNS))
118 alpha = True;
119 else
120 alpha = (color_type & PNG_COLOR_MASK_ALPHA);
122 /* allocate RImage */
123 image = RCreateImage(width, height, alpha);
124 if (!image) {
125 fclose(f);
126 png_destroy_read_struct(&png, &pinfo, &einfo);
127 return NULL;
130 /* normalize to 8bpp with alpha channel */
131 if (color_type == PNG_COLOR_TYPE_PALETTE && depth < 8)
132 png_set_expand(png);
134 if (color_type == PNG_COLOR_TYPE_GRAY && depth < 8)
135 png_set_expand(png);
137 if (png_get_valid(png, pinfo, PNG_INFO_tRNS))
138 png_set_expand(png);
140 if (depth == 16)
141 png_set_strip_16(png);
143 if (color_type == PNG_COLOR_TYPE_GRAY ||
144 color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
145 png_set_gray_to_rgb(png);
147 /* set gamma correction */
148 if ((context->attribs->flags & RC_GammaCorrection)
149 && context->depth != 8) {
150 sgamma = (context->attribs->rgamma + context->attribs->ggamma +
151 context->attribs->bgamma) / 3;
152 } else if ((tmp = getenv("DISPLAY_GAMMA")) != NULL) {
153 sgamma = atof(tmp);
154 if (sgamma==0)
155 sgamma = 1;
156 } else {
157 /* no, this is correct. Old gimp versions are broken and save wrong
158 * data. Upgrade gimp */
159 sgamma = 2.0;
162 if (png_get_gAMA(png, pinfo, &gamma))
163 png_set_gamma(png, sgamma, gamma);
164 else
165 png_set_gamma(png, sgamma, 0.45);
167 /* do the transforms */
168 png_read_update_info(png, pinfo);
170 /* set background color */
171 if (png_get_bKGD(png, pinfo, &bkcolor)) {
172 image->background.red = bkcolor->red >> 8;
173 image->background.green = bkcolor->green >> 8;
174 image->background.blue = bkcolor->blue >> 8;
177 png_rows = alloca(sizeof(char*)*height);
178 if (!png_rows) {
179 RErrorCode = RERR_NOMEMORY;
180 fclose(f);
181 RDestroyImage(image);
182 png_destroy_read_struct(&png, &pinfo, &einfo);
183 #ifdef C_ALLOCA
184 alloca(0);
185 #endif
186 return NULL;
188 for (y=0; y<height; y++) {
189 png_rows[y] = alloca(png_get_rowbytes(png, pinfo));
190 if (!png_rows[y]) {
191 RErrorCode = RERR_NOMEMORY;
192 fclose(f);
193 RDestroyImage(image);
194 png_destroy_read_struct(&png, &pinfo, &einfo);
195 #ifdef C_ALLOCA
196 alloca(0);
197 #endif
198 return NULL;
201 /* read data */
202 png_read_image(png, png_rows);
204 png_read_end(png, einfo);
206 png_destroy_read_struct(&png, &pinfo, &einfo);
208 fclose(f);
210 r = image->data[0];
211 g = image->data[1];
212 b = image->data[2];
213 a = image->data[3];
215 /* convert to RImage */
216 if (alpha) {
217 for (y=0; y<height; y++) {
218 for (x=0, i=0; x<width; x++) {
219 *(r++) = *(png_rows[y]+i++);
220 *(g++) = *(png_rows[y]+i++);
221 *(b++) = *(png_rows[y]+i++);
222 *(a++) = *(png_rows[y]+i++);
225 } else {
226 for (y=0; y<height; y++) {
227 for (x=0, i=0; x<width; x++) {
228 *(r++) = *(png_rows[y]+i++);
229 *(g++) = *(png_rows[y]+i++);
230 *(b++) = *(png_rows[y]+i++);
234 #ifdef C_ALLOCA
235 alloca(0);
236 #endif
237 return image;
240 #endif /* USE_PNG */