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.
34 RImage
*RLoadPNG(RContext
* context
, char *file
)
40 png_infop pinfo
, einfo
;
41 png_color_16p bkcolor
;
45 png_uint_32 width
, height
;
46 int depth
, junk
, color_type
;
50 f
= fopen(file
, "rb");
52 RErrorCode
= RERR_OPEN
;
55 png
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, (png_error_ptr
) NULL
, (png_error_ptr
) NULL
);
57 RErrorCode
= RERR_NOMEMORY
;
62 pinfo
= png_create_info_struct(png
);
64 RErrorCode
= RERR_NOMEMORY
;
66 png_destroy_read_struct(&png
, NULL
, NULL
);
70 einfo
= png_create_info_struct(png
);
72 RErrorCode
= RERR_NOMEMORY
;
74 png_destroy_read_struct(&png
, &pinfo
, NULL
);
78 RErrorCode
= RERR_INTERNAL
;
79 #if PNG_LIBPNG_VER - 0 < 10400
80 if (setjmp(png
->jmpbuf
)) {
82 if (setjmp(png_jmpbuf(png
))) {
85 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
93 png_read_info(png
, pinfo
);
95 png_get_IHDR(png
, pinfo
, &width
, &height
, &depth
, &color_type
, &junk
, &junk
, &junk
);
98 if (width
< 1 || height
< 1) {
100 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
101 RErrorCode
= RERR_BADIMAGEFILE
;
105 /* check for an alpha channel */
106 if (png_get_valid(png
, pinfo
, PNG_INFO_tRNS
))
109 alpha
= (color_type
& PNG_COLOR_MASK_ALPHA
);
111 /* allocate RImage */
112 image
= RCreateImage(width
, height
, alpha
);
115 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
119 /* normalize to 8bpp with alpha channel */
120 if (color_type
== PNG_COLOR_TYPE_PALETTE
&& depth
<= 8)
123 if (color_type
== PNG_COLOR_TYPE_GRAY
&& depth
<= 8)
126 if (png_get_valid(png
, pinfo
, PNG_INFO_tRNS
))
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
) {
148 if (png_get_gAMA(png
, pinfo
, &gamma
))
149 png_set_gamma(png
, sgamma
, gamma
);
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 *));
165 RErrorCode
= RERR_NOMEMORY
;
167 RReleaseImage(image
);
168 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
171 for (y
= 0; y
< height
; y
++) {
172 png_rows
[y
] = malloc(png_get_rowbytes(png
, pinfo
));
174 RErrorCode
= RERR_NOMEMORY
;
176 RReleaseImage(image
);
177 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
186 png_read_image(png
, png_rows
);
188 png_read_end(png
, einfo
);
190 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
196 /* convert to RImage */
198 for (y
= 0; y
< height
; y
++) {
199 for (x
= 0, i
= width
* 4; x
< i
; x
++, ptr
++) {
200 *ptr
= *(png_rows
[y
] + x
);
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
++)