Bouncing appicon effect
[wmaker-crm.git] / wrlib / png.c
blobb4d62cff85b69ca30c78951a6398287b8c82845e
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 (setjmp(png->jmpbuf)) {
80 fclose(f);
81 png_destroy_read_struct(&png, &pinfo, &einfo);
82 if (image)
83 RReleaseImage(image);
84 return NULL;
87 png_init_io(png, f);
89 png_read_info(png, pinfo);
91 png_get_IHDR(png, pinfo, &width, &height, &depth, &color_type, &junk, &junk, &junk);
93 /* sanity check */
94 if (width < 1 || height < 1) {
95 fclose(f);
96 png_destroy_read_struct(&png, &pinfo, &einfo);
97 RErrorCode = RERR_BADIMAGEFILE;
98 return NULL;
101 /* check for an alpha channel */
102 if (png_get_valid(png, pinfo, PNG_INFO_tRNS))
103 alpha = True;
104 else
105 alpha = (color_type & PNG_COLOR_MASK_ALPHA);
107 /* allocate RImage */
108 image = RCreateImage(width, height, alpha);
109 if (!image) {
110 fclose(f);
111 png_destroy_read_struct(&png, &pinfo, &einfo);
112 return NULL;
115 /* normalize to 8bpp with alpha channel */
116 if (color_type == PNG_COLOR_TYPE_PALETTE && depth <= 8)
117 png_set_expand(png);
119 if (color_type == PNG_COLOR_TYPE_GRAY && depth <= 8)
120 png_set_expand(png);
122 if (png_get_valid(png, pinfo, PNG_INFO_tRNS))
123 png_set_expand(png);
125 if (depth == 16)
126 png_set_strip_16(png);
128 if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
129 png_set_gray_to_rgb(png);
131 /* set gamma correction */
132 if ((context->attribs->flags & RC_GammaCorrection)
133 && context->depth != 8) {
134 sgamma = (context->attribs->rgamma + context->attribs->ggamma + context->attribs->bgamma) / 3;
135 } else if ((tmp = getenv("DISPLAY_GAMMA")) != NULL) {
136 sgamma = atof(tmp);
137 if (sgamma == 0)
138 sgamma = 1;
139 } else {
140 /* blah */
141 sgamma = 2.2;
144 if (png_get_gAMA(png, pinfo, &gamma))
145 png_set_gamma(png, sgamma, gamma);
146 else
147 png_set_gamma(png, sgamma, 0.45);
149 /* do the transforms */
150 png_read_update_info(png, pinfo);
152 /* set background color */
153 if (png_get_bKGD(png, pinfo, &bkcolor)) {
154 image->background.red = bkcolor->red >> 8;
155 image->background.green = bkcolor->green >> 8;
156 image->background.blue = bkcolor->blue >> 8;
159 png_rows = calloc(height, sizeof(char *));
160 if (!png_rows) {
161 RErrorCode = RERR_NOMEMORY;
162 fclose(f);
163 RReleaseImage(image);
164 png_destroy_read_struct(&png, &pinfo, &einfo);
165 return NULL;
167 for (y = 0; y < height; y++) {
168 png_rows[y] = malloc(png_get_rowbytes(png, pinfo));
169 if (!png_rows[y]) {
170 RErrorCode = RERR_NOMEMORY;
171 fclose(f);
172 RReleaseImage(image);
173 png_destroy_read_struct(&png, &pinfo, &einfo);
174 while (y-- > 0)
175 if (png_rows[y])
176 free(png_rows[y]);
177 free(png_rows);
178 return NULL;
181 /* read data */
182 png_read_image(png, png_rows);
184 png_read_end(png, einfo);
186 png_destroy_read_struct(&png, &pinfo, &einfo);
188 fclose(f);
190 ptr = image->data;
192 /* convert to RImage */
193 if (alpha) {
194 for (y = 0; y < height; y++) {
195 for (x = 0, i = width * 4; x < i; x++, ptr++) {
196 *ptr = *(png_rows[y] + x);
199 } else {
200 for (y = 0; y < height; y++) {
201 for (x = 0, i = width * 3; x < i; x++, ptr++) {
202 *ptr = *(png_rows[y] + x);
206 for (y = 0; y < height; y++)
207 if (png_rows[y])
208 free(png_rows[y]);
209 free(png_rows);
210 return image;
213 #endif /* USE_PNG */