1 /* png.c - load PNG image from file
3 * Raster graphics library
5 * Copyright (c) 1997 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.
24 /* AIX requires this to be the first thing in the file. */
26 # define alloca __builtin_alloca
34 # ifndef alloca /* predefined by HP cc +Olibcalls */
50 #if PNG_LIBPNG_VER < 96
51 #error libpng_must_be_more_at_least_0_96
58 RLoadPNG(RContext
*context
, char *file
, int index
)
64 png_infop pinfo
, einfo
;
65 png_color_16p bkcolor
;
69 png_uint_32 width
, height
;
70 int depth
, junk
, color_type
;
72 unsigned char *r
, *g
, *b
, *a
;
76 RErrorCode
= RERR_OPEN
;
79 png
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
,
80 (png_error_ptr
)NULL
, (png_error_ptr
)NULL
);
82 RErrorCode
= RERR_NOMEMORY
;
87 pinfo
= png_create_info_struct(png
);
89 RErrorCode
= RERR_NOMEMORY
;
91 png_destroy_read_struct(&png
, NULL
, NULL
);
95 einfo
= png_create_info_struct(png
);
97 RErrorCode
= RERR_NOMEMORY
;
99 png_destroy_read_struct(&png
, &pinfo
, NULL
);
103 RErrorCode
= RERR_INTERNAL
;
104 if (setjmp(png
->jmpbuf
)) {
106 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
108 RDestroyImage(image
);
114 png_read_info(png
, pinfo
);
116 png_get_IHDR(png
, pinfo
, &width
, &height
, &depth
, &color_type
,
117 &junk
, &junk
, &junk
);
120 /* check for an alpha channel */
121 if (png_get_valid(png
, pinfo
, PNG_INFO_tRNS
))
124 alpha
= (color_type
& PNG_COLOR_MASK_ALPHA
);
126 /* allocate RImage */
127 image
= RCreateImage(width
, height
, alpha
);
130 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
134 /* normalize to 8bpp with alpha channel */
135 if (color_type
== PNG_COLOR_TYPE_PALETTE
&& depth
< 8)
138 if (color_type
== PNG_COLOR_TYPE_GRAY
&& depth
< 8)
141 if (png_get_valid(png
, pinfo
, PNG_INFO_tRNS
))
145 png_set_strip_16(png
);
147 if (color_type
== PNG_COLOR_TYPE_GRAY
||
148 color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
149 png_set_gray_to_rgb(png
);
151 /* set gamma correction */
152 if ((context
->attribs
->flags
& RC_GammaCorrection
)
153 && context
->depth
!= 8) {
154 sgamma
= (context
->attribs
->rgamma
+ context
->attribs
->ggamma
+
155 context
->attribs
->bgamma
) / 3;
156 } else if ((tmp
= getenv("DISPLAY_GAMMA")) != NULL
) {
164 if (png_get_gAMA(png
, pinfo
, &gamma
))
165 png_set_gamma(png
, sgamma
, gamma
);
167 png_set_gamma(png
, sgamma
, 0.45);
169 /* do the transforms */
170 png_read_update_info(png
, pinfo
);
172 /* set background color */
173 if (png_get_bKGD(png
, pinfo
, &bkcolor
)) {
174 image
->background
.red
= bkcolor
->red
>> 8;
175 image
->background
.green
= bkcolor
->green
>> 8;
176 image
->background
.blue
= bkcolor
->blue
>> 8;
179 png_rows
= alloca(sizeof(char*)*height
);
181 RErrorCode
= RERR_NOMEMORY
;
183 RDestroyImage(image
);
184 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
190 for (y
=0; y
<height
; y
++) {
191 png_rows
[y
] = alloca(png_get_rowbytes(png
, pinfo
));
193 RErrorCode
= RERR_NOMEMORY
;
195 RDestroyImage(image
);
196 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
204 png_read_image(png
, png_rows
);
206 png_read_end(png
, einfo
);
208 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
217 /* convert to RImage */
219 for (y
=0; y
<height
; y
++) {
220 for (x
=0, i
=0; x
<width
; x
++) {
221 *(r
++) = *(png_rows
[y
]+i
++);
222 *(g
++) = *(png_rows
[y
]+i
++);
223 *(b
++) = *(png_rows
[y
]+i
++);
224 *(a
++) = *(png_rows
[y
]+i
++);
228 for (y
=0; y
<height
; y
++) {
229 for (x
=0, i
=0; x
<width
; x
++) {
230 *(r
++) = *(png_rows
[y
]+i
++);
231 *(g
++) = *(png_rows
[y
]+i
++);
232 *(b
++) = *(png_rows
[y
]+i
++);