wmaker: Replaced local 'extern' definition of wPreferences by proper header usage
[wmaker-crm.git] / wrlib / png.c
blob03074ead473c960afdecb95e675ee265e4d24ddb
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., 51 Franklin St, Fifth Floor, Boston,
20 * MA 02110-1301, USA.
23 #include <config.h>
25 #ifdef USE_PNG
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
31 #include <png.h>
33 #include "wraster.h"
34 #include "imgformat.h"
36 RImage *RLoadPNG(RContext *context, const char *file)
38 char *tmp;
39 RImage *image = NULL;
40 FILE *f;
41 png_structp png;
42 png_infop pinfo, einfo;
43 png_color_16p bkcolor;
44 int alpha;
45 int x, y, i;
46 double gamma, sgamma;
47 png_uint_32 width, height;
48 int depth, junk, color_type;
49 png_bytep *png_rows;
50 unsigned char *ptr;
52 f = fopen(file, "rb");
53 if (!f) {
54 RErrorCode = RERR_OPEN;
55 return NULL;
57 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, (png_error_ptr) NULL, (png_error_ptr) NULL);
58 if (!png) {
59 RErrorCode = RERR_NOMEMORY;
60 fclose(f);
61 return NULL;
64 pinfo = png_create_info_struct(png);
65 if (!pinfo) {
66 RErrorCode = RERR_NOMEMORY;
67 fclose(f);
68 png_destroy_read_struct(&png, NULL, NULL);
69 return NULL;
72 einfo = png_create_info_struct(png);
73 if (!einfo) {
74 RErrorCode = RERR_NOMEMORY;
75 fclose(f);
76 png_destroy_read_struct(&png, &pinfo, NULL);
77 return NULL;
80 RErrorCode = RERR_INTERNAL;
81 #if PNG_LIBPNG_VER - 0 < 10400
82 if (setjmp(png->jmpbuf)) {
83 #else
84 if (setjmp(png_jmpbuf(png))) {
85 #endif
86 fclose(f);
87 png_destroy_read_struct(&png, &pinfo, &einfo);
88 if (image)
89 RReleaseImage(image);
90 return NULL;
93 png_init_io(png, f);
95 png_read_info(png, pinfo);
97 png_get_IHDR(png, pinfo, &width, &height, &depth, &color_type, &junk, &junk, &junk);
99 /* sanity check */
100 if (width < 1 || height < 1) {
101 fclose(f);
102 png_destroy_read_struct(&png, &pinfo, &einfo);
103 RErrorCode = RERR_BADIMAGEFILE;
104 return NULL;
107 /* check for an alpha channel */
108 if (png_get_valid(png, pinfo, PNG_INFO_tRNS))
109 alpha = True;
110 else
111 alpha = (color_type & PNG_COLOR_MASK_ALPHA);
113 /* allocate RImage */
114 image = RCreateImage(width, height, alpha);
115 if (!image) {
116 fclose(f);
117 png_destroy_read_struct(&png, &pinfo, &einfo);
118 return NULL;
121 /* normalize to 8bpp with alpha channel */
122 if (color_type == PNG_COLOR_TYPE_PALETTE && depth <= 8)
123 png_set_expand(png);
125 if (color_type == PNG_COLOR_TYPE_GRAY && depth <= 8)
126 png_set_expand(png);
128 if (png_get_valid(png, pinfo, PNG_INFO_tRNS))
129 png_set_expand(png);
131 if (depth == 16)
132 png_set_strip_16(png);
134 if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
135 png_set_gray_to_rgb(png);
137 /* set gamma correction */
138 if ((context->attribs->flags & RC_GammaCorrection)
139 && context->depth != 8) {
140 sgamma = (context->attribs->rgamma + context->attribs->ggamma + context->attribs->bgamma) / 3;
141 } else if ((tmp = getenv("DISPLAY_GAMMA")) != NULL) {
142 sgamma = atof(tmp);
143 if (sgamma < 1.0E-3)
144 sgamma = 1;
145 } else {
146 /* blah */
147 sgamma = 2.2;
150 if (png_get_gAMA(png, pinfo, &gamma))
151 png_set_gamma(png, sgamma, gamma);
152 else
153 png_set_gamma(png, sgamma, 0.45);
155 /* do the transforms */
156 png_read_update_info(png, pinfo);
158 /* set background color */
159 if (png_get_bKGD(png, pinfo, &bkcolor)) {
160 image->background.red = bkcolor->red >> 8;
161 image->background.green = bkcolor->green >> 8;
162 image->background.blue = bkcolor->blue >> 8;
165 png_rows = calloc(height, sizeof(char *));
166 if (!png_rows) {
167 RErrorCode = RERR_NOMEMORY;
168 fclose(f);
169 RReleaseImage(image);
170 png_destroy_read_struct(&png, &pinfo, &einfo);
171 return NULL;
173 for (y = 0; y < height; y++) {
174 png_rows[y] = malloc(png_get_rowbytes(png, pinfo));
175 if (!png_rows[y]) {
176 RErrorCode = RERR_NOMEMORY;
177 fclose(f);
178 RReleaseImage(image);
179 png_destroy_read_struct(&png, &pinfo, &einfo);
180 while (y-- > 0)
181 if (png_rows[y])
182 free(png_rows[y]);
183 free(png_rows);
184 return NULL;
187 /* read data */
188 png_read_image(png, png_rows);
190 png_read_end(png, einfo);
192 png_destroy_read_struct(&png, &pinfo, &einfo);
194 fclose(f);
196 ptr = image->data;
198 /* convert to RImage */
199 if (alpha) {
200 for (y = 0; y < height; y++) {
201 for (x = 0, i = width * 4; x < i; x++, ptr++) {
202 *ptr = *(png_rows[y] + x);
205 } else {
206 for (y = 0; y < height; y++) {
207 for (x = 0, i = width * 3; x < i; x++, ptr++) {
208 *ptr = *(png_rows[y] + x);
212 for (y = 0; y < height; y++)
213 if (png_rows[y])
214 free(png_rows[y]);
215 free(png_rows);
216 return image;
219 #endif /* USE_PNG */