Change to the linux kernel coding style
[wmaker-crm.git] / wrlib / png.c
1 /* png.c - load PNG image from file
2  *
3  * Raster graphics library
4  *
5  * Copyright (c) 1997-2003 Alfredo K. Kojima
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include <config.h>
23
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
40
41 #ifdef USE_PNG
42
43 #include <stdlib.h>
44 #include <stdio.h>
45 #include <string.h>
46
47 #include <png.h>
48
49 #include "wraster.h"
50
51 RImage *RLoadPNG(RContext * context, char *file, int index)
52 {
53         char *tmp;
54         RImage *image = NULL;
55         FILE *f;
56         png_structp png;
57         png_infop pinfo, einfo;
58         png_color_16p bkcolor;
59         int alpha;
60         int x, y, i;
61         double gamma, sgamma;
62         png_uint_32 width, height;
63         int depth, junk, color_type;
64         png_bytep *png_rows;
65         unsigned char *ptr;
66
67         f = fopen(file, "rb");
68         if (!f) {
69                 RErrorCode = RERR_OPEN;
70                 return NULL;
71         }
72         png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, (png_error_ptr) NULL, (png_error_ptr) NULL);
73         if (!png) {
74                 RErrorCode = RERR_NOMEMORY;
75                 fclose(f);
76                 return NULL;
77         }
78
79         pinfo = png_create_info_struct(png);
80         if (!pinfo) {
81                 RErrorCode = RERR_NOMEMORY;
82                 fclose(f);
83                 png_destroy_read_struct(&png, NULL, NULL);
84                 return NULL;
85         }
86
87         einfo = png_create_info_struct(png);
88         if (!einfo) {
89                 RErrorCode = RERR_NOMEMORY;
90                 fclose(f);
91                 png_destroy_read_struct(&png, &pinfo, NULL);
92                 return NULL;
93         }
94
95         RErrorCode = RERR_INTERNAL;
96         if (setjmp(png->jmpbuf)) {
97                 fclose(f);
98                 png_destroy_read_struct(&png, &pinfo, &einfo);
99                 if (image)
100                         RReleaseImage(image);
101                 return NULL;
102         }
103
104         png_init_io(png, f);
105
106         png_read_info(png, pinfo);
107
108         png_get_IHDR(png, pinfo, &width, &height, &depth, &color_type, &junk, &junk, &junk);
109
110         /* sanity check */
111         if (width < 1 || height < 1) {
112                 fclose(f);
113                 png_destroy_read_struct(&png, &pinfo, &einfo);
114                 RErrorCode = RERR_BADIMAGEFILE;
115                 return NULL;
116         }
117
118         /* check for an alpha channel */
119         if (png_get_valid(png, pinfo, PNG_INFO_tRNS))
120                 alpha = True;
121         else
122                 alpha = (color_type & PNG_COLOR_MASK_ALPHA);
123
124         /* allocate RImage */
125         image = RCreateImage(width, height, alpha);
126         if (!image) {
127                 fclose(f);
128                 png_destroy_read_struct(&png, &pinfo, &einfo);
129                 return NULL;
130         }
131
132         /* normalize to 8bpp with alpha channel */
133         if (color_type == PNG_COLOR_TYPE_PALETTE && depth <= 8)
134                 png_set_expand(png);
135
136         if (color_type == PNG_COLOR_TYPE_GRAY && depth <= 8)
137                 png_set_expand(png);
138
139         if (png_get_valid(png, pinfo, PNG_INFO_tRNS))
140                 png_set_expand(png);
141
142         if (depth == 16)
143                 png_set_strip_16(png);
144
145         if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
146                 png_set_gray_to_rgb(png);
147
148         /* set gamma correction */
149         if ((context->attribs->flags & RC_GammaCorrection)
150             && context->depth != 8) {
151                 sgamma = (context->attribs->rgamma + context->attribs->ggamma + 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                 /* blah */
158                 sgamma = 2.2;
159         }
160
161         if (png_get_gAMA(png, pinfo, &gamma))
162                 png_set_gamma(png, sgamma, gamma);
163         else
164                 png_set_gamma(png, sgamma, 0.45);
165
166         /* do the transforms */
167         png_read_update_info(png, pinfo);
168
169         /* set background color */
170         if (png_get_bKGD(png, pinfo, &bkcolor)) {
171                 image->background.red = bkcolor->red >> 8;
172                 image->background.green = bkcolor->green >> 8;
173                 image->background.blue = bkcolor->blue >> 8;
174         }
175
176         png_rows = alloca(sizeof(char *) * height);
177         if (!png_rows) {
178                 RErrorCode = RERR_NOMEMORY;
179                 fclose(f);
180                 RReleaseImage(image);
181                 png_destroy_read_struct(&png, &pinfo, &einfo);
182 #ifdef C_ALLOCA
183                 alloca(0);
184 #endif
185                 return NULL;
186         }
187         for (y = 0; y < height; y++) {
188                 png_rows[y] = alloca(png_get_rowbytes(png, pinfo));
189                 if (!png_rows[y]) {
190                         RErrorCode = RERR_NOMEMORY;
191                         fclose(f);
192                         RReleaseImage(image);
193                         png_destroy_read_struct(&png, &pinfo, &einfo);
194 #ifdef C_ALLOCA
195                         alloca(0);
196 #endif
197                         return NULL;
198                 }
199         }
200         /* read data */
201         png_read_image(png, png_rows);
202
203         png_read_end(png, einfo);
204
205         png_destroy_read_struct(&png, &pinfo, &einfo);
206
207         fclose(f);
208
209         ptr = image->data;
210
211         /* convert to RImage */
212         if (alpha) {
213                 for (y = 0; y < height; y++) {
214                         for (x = 0, i = width * 4; x < i; x++, ptr++) {
215                                 *ptr = *(png_rows[y] + x);
216                         }
217                 }
218         } else {
219                 for (y = 0; y < height; y++) {
220                         for (x = 0, i = width * 3; x < i; x++, ptr++) {
221                                 *ptr = *(png_rows[y] + x);
222                         }
223                 }
224         }
225 #ifdef C_ALLOCA
226         alloca(0);
227 #endif
228         return image;
229 }
230
231 #endif                          /* USE_PNG */