Change to the linux kernel coding style
[wmaker-crm.git] / wrlib / png.c
Commit [+]AuthorDateLineData
9d2e6ef9 scottc1998-09-29 22:36:29 +00001/* png.c - load PNG image from file
6830b057 dan2004-10-12 21:28:27 +00002 *
4153e2fd dan2003-01-16 23:30:45 +00003 * Raster graphics library
6830b057 dan2004-10-12 21:28:27 +00004 *
4153e2fd dan2003-01-16 23:30:45 +00005 * Copyright (c) 1997-2003 Alfredo K. Kojima
9d2e6ef9 scottc1998-09-29 22:36:29 +00006 *
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.
6830b057 dan2004-10-12 21:28:27 +000011 *
9d2e6ef9 scottc1998-09-29 22:36:29 +000012 * 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.
6830b057 dan2004-10-12 21:28:27 +000016 *
9d2e6ef9 scottc1998-09-29 22:36:29 +000017 * 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
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020034# ifndef alloca /* predefined by HP cc +Olibcalls */
35char *alloca();
9d2e6ef9 scottc1998-09-29 22:36:29 +000036# endif
37# endif
38# endif
39#endif
40
9d2e6ef9 scottc1998-09-29 22:36:29 +000041#ifdef USE_PNG
42
43#include <stdlib.h>
44#include <stdio.h>
45#include <string.h>
46
47#include <png.h>
48
9d2e6ef9 scottc1998-09-29 22:36:29 +000049#include "wraster.h"
50
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +020051RImage *RLoadPNG(RContext * context, char *file, int index)
9d2e6ef9 scottc1998-09-29 22:36:29 +000052{
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +020053 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);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000182#ifdef C_ALLOCA
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200183 alloca(0);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000184#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200185 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);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000194#ifdef C_ALLOCA
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200195 alloca(0);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000196#endif
688a56e8
CM
Carlos R. Mafra2009-08-20 00:59:40 +0200197 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 }
9d2e6ef9 scottc1998-09-29 22:36:29 +0000225#ifdef C_ALLOCA
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200226 alloca(0);
9d2e6ef9 scottc1998-09-29 22:36:29 +0000227#endif
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200228 return image;
9d2e6ef9 scottc1998-09-29 22:36:29 +0000229}
230
688a56e8 Carlos R. Mafra2009-08-20 00:59:40 +0200231#endif /* USE_PNG */