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., 51 Franklin St, Fifth Floor, Boston,
35 RImage
*RLoadPNG(RContext
* context
, char *file
)
41 png_infop pinfo
, einfo
;
42 png_color_16p bkcolor
;
46 png_uint_32 width
, height
;
47 int depth
, junk
, color_type
;
51 f
= fopen(file
, "rb");
53 RErrorCode
= RERR_OPEN
;
56 png
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, NULL
, (png_error_ptr
) NULL
, (png_error_ptr
) NULL
);
58 RErrorCode
= RERR_NOMEMORY
;
63 pinfo
= png_create_info_struct(png
);
65 RErrorCode
= RERR_NOMEMORY
;
67 png_destroy_read_struct(&png
, NULL
, NULL
);
71 einfo
= png_create_info_struct(png
);
73 RErrorCode
= RERR_NOMEMORY
;
75 png_destroy_read_struct(&png
, &pinfo
, NULL
);
79 RErrorCode
= RERR_INTERNAL
;
80 #if PNG_LIBPNG_VER - 0 < 10400
81 if (setjmp(png
->jmpbuf
)) {
83 if (setjmp(png_jmpbuf(png
))) {
86 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
94 png_read_info(png
, pinfo
);
96 png_get_IHDR(png
, pinfo
, &width
, &height
, &depth
, &color_type
, &junk
, &junk
, &junk
);
99 if (width
< 1 || height
< 1) {
101 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
102 RErrorCode
= RERR_BADIMAGEFILE
;
106 /* check for an alpha channel */
107 if (png_get_valid(png
, pinfo
, PNG_INFO_tRNS
))
110 alpha
= (color_type
& PNG_COLOR_MASK_ALPHA
);
112 /* allocate RImage */
113 image
= RCreateImage(width
, height
, alpha
);
116 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
120 /* normalize to 8bpp with alpha channel */
121 if (color_type
== PNG_COLOR_TYPE_PALETTE
&& depth
<= 8)
124 if (color_type
== PNG_COLOR_TYPE_GRAY
&& depth
<= 8)
127 if (png_get_valid(png
, pinfo
, PNG_INFO_tRNS
))
131 png_set_strip_16(png
);
133 if (color_type
== PNG_COLOR_TYPE_GRAY
|| color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
134 png_set_gray_to_rgb(png
);
136 /* set gamma correction */
137 if ((context
->attribs
->flags
& RC_GammaCorrection
)
138 && context
->depth
!= 8) {
139 sgamma
= (context
->attribs
->rgamma
+ context
->attribs
->ggamma
+ context
->attribs
->bgamma
) / 3;
140 } else if ((tmp
= getenv("DISPLAY_GAMMA")) != NULL
) {
149 if (png_get_gAMA(png
, pinfo
, &gamma
))
150 png_set_gamma(png
, sgamma
, gamma
);
152 png_set_gamma(png
, sgamma
, 0.45);
154 /* do the transforms */
155 png_read_update_info(png
, pinfo
);
157 /* set background color */
158 if (png_get_bKGD(png
, pinfo
, &bkcolor
)) {
159 image
->background
.red
= bkcolor
->red
>> 8;
160 image
->background
.green
= bkcolor
->green
>> 8;
161 image
->background
.blue
= bkcolor
->blue
>> 8;
164 png_rows
= calloc(height
, sizeof(char *));
166 RErrorCode
= RERR_NOMEMORY
;
168 RReleaseImage(image
);
169 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
172 for (y
= 0; y
< height
; y
++) {
173 png_rows
[y
] = malloc(png_get_rowbytes(png
, pinfo
));
175 RErrorCode
= RERR_NOMEMORY
;
177 RReleaseImage(image
);
178 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
187 png_read_image(png
, png_rows
);
189 png_read_end(png
, einfo
);
191 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
197 /* convert to RImage */
199 for (y
= 0; y
< height
; y
++) {
200 for (x
= 0, i
= width
* 4; x
< i
; x
++, ptr
++) {
201 *ptr
= *(png_rows
[y
] + x
);
205 for (y
= 0; y
< height
; y
++) {
206 for (x
= 0, i
= width
* 3; x
< i
; x
++, ptr
++) {
207 *ptr
= *(png_rows
[y
] + x
);
211 for (y
= 0; y
< height
; y
++)