declare get_pixmap_icon_from_* as static functions
[wmaker-crm.git] / wrlib / png.c
blob5cb9918d0a4fd1ade492a51d2c2a6f0041040ae5
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.
22 #include <config.h>
24 #ifdef USE_PNG
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
30 #include <png.h>
32 #include "wraster.h"
34 RImage *RLoadPNG(RContext * context, char *file)
36 char *tmp;
37 RImage *image = NULL;
38 FILE *f;
39 png_structp png;
40 png_infop pinfo, einfo;
41 png_color_16p bkcolor;
42 int alpha;
43 int x, y, i;
44 double gamma, sgamma;
45 png_uint_32 width, height;
46 int depth, junk, color_type;
47 png_bytep *png_rows;
48 unsigned char *ptr;
50 f = fopen(file, "rb");
51 if (!f) {
52 RErrorCode = RERR_OPEN;
53 return NULL;
55 png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, (png_error_ptr) NULL, (png_error_ptr) NULL);
56 if (!png) {
57 RErrorCode = RERR_NOMEMORY;
58 fclose(f);
59 return NULL;
62 pinfo = png_create_info_struct(png);
63 if (!pinfo) {
64 RErrorCode = RERR_NOMEMORY;
65 fclose(f);
66 png_destroy_read_struct(&png, NULL, NULL);
67 return NULL;
70 einfo = png_create_info_struct(png);
71 if (!einfo) {
72 RErrorCode = RERR_NOMEMORY;
73 fclose(f);
74 png_destroy_read_struct(&png, &pinfo, NULL);
75 return NULL;
78 RErrorCode = RERR_INTERNAL;
79 #if PNG_LIBPNG_VER - 0 < 10400
80 if (setjmp(png->jmpbuf)) {
81 #else
82 if (setjmp(png_jmpbuf(png))) {
83 #endif
84 fclose(f);
85 png_destroy_read_struct(&png, &pinfo, &einfo);
86 if (image)
87 RReleaseImage(image);
88 return NULL;
91 png_init_io(png, f);
93 png_read_info(png, pinfo);
95 png_get_IHDR(png, pinfo, &width, &height, &depth, &color_type, &junk, &junk, &junk);
97 /* sanity check */
98 if (width < 1 || height < 1) {
99 fclose(f);
100 png_destroy_read_struct(&png, &pinfo, &einfo);
101 RErrorCode = RERR_BADIMAGEFILE;
102 return NULL;
105 /* check for an alpha channel */
106 if (png_get_valid(png, pinfo, PNG_INFO_tRNS))
107 alpha = True;
108 else
109 alpha = (color_type & PNG_COLOR_MASK_ALPHA);
111 /* allocate RImage */
112 image = RCreateImage(width, height, alpha);
113 if (!image) {
114 fclose(f);
115 png_destroy_read_struct(&png, &pinfo, &einfo);
116 return NULL;
119 /* normalize to 8bpp with alpha channel */
120 if (color_type == PNG_COLOR_TYPE_PALETTE && depth <= 8)
121 png_set_expand(png);
123 if (color_type == PNG_COLOR_TYPE_GRAY && depth <= 8)
124 png_set_expand(png);
126 if (png_get_valid(png, pinfo, PNG_INFO_tRNS))
127 png_set_expand(png);
129 if (depth == 16)
130 png_set_strip_16(png);
132 if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
133 png_set_gray_to_rgb(png);
135 /* set gamma correction */
136 if ((context->attribs->flags & RC_GammaCorrection)
137 && context->depth != 8) {
138 sgamma = (context->attribs->rgamma + context->attribs->ggamma + context->attribs->bgamma) / 3;
139 } else if ((tmp = getenv("DISPLAY_GAMMA")) != NULL) {
140 sgamma = atof(tmp);
141 if (sgamma == 0)
142 sgamma = 1;
143 } else {
144 /* blah */
145 sgamma = 2.2;
148 if (png_get_gAMA(png, pinfo, &gamma))
149 png_set_gamma(png, sgamma, gamma);
150 else
151 png_set_gamma(png, sgamma, 0.45);
153 /* do the transforms */
154 png_read_update_info(png, pinfo);
156 /* set background color */
157 if (png_get_bKGD(png, pinfo, &bkcolor)) {
158 image->background.red = bkcolor->red >> 8;
159 image->background.green = bkcolor->green >> 8;
160 image->background.blue = bkcolor->blue >> 8;
163 png_rows = calloc(height, sizeof(char *));
164 if (!png_rows) {
165 RErrorCode = RERR_NOMEMORY;
166 fclose(f);
167 RReleaseImage(image);
168 png_destroy_read_struct(&png, &pinfo, &einfo);
169 return NULL;
171 for (y = 0; y < height; y++) {
172 png_rows[y] = malloc(png_get_rowbytes(png, pinfo));
173 if (!png_rows[y]) {
174 RErrorCode = RERR_NOMEMORY;
175 fclose(f);
176 RReleaseImage(image);
177 png_destroy_read_struct(&png, &pinfo, &einfo);
178 while (y-- > 0)
179 if (png_rows[y])
180 free(png_rows[y]);
181 free(png_rows);
182 return NULL;
185 /* read data */
186 png_read_image(png, png_rows);
188 png_read_end(png, einfo);
190 png_destroy_read_struct(&png, &pinfo, &einfo);
192 fclose(f);
194 ptr = image->data;
196 /* convert to RImage */
197 if (alpha) {
198 for (y = 0; y < height; y++) {
199 for (x = 0, i = width * 4; x < i; x++, ptr++) {
200 *ptr = *(png_rows[y] + x);
203 } else {
204 for (y = 0; y < height; y++) {
205 for (x = 0, i = width * 3; x < i; x++, ptr++) {
206 *ptr = *(png_rows[y] + x);
210 for (y = 0; y < height; y++)
211 if (png_rows[y])
212 free(png_rows[y]);
213 free(png_rows);
214 return image;
217 #endif /* USE_PNG */