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 (setjmp(png
->jmpbuf
)) {
81 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
89 png_read_info(png
, pinfo
);
91 png_get_IHDR(png
, pinfo
, &width
, &height
, &depth
, &color_type
, &junk
, &junk
, &junk
);
94 if (width
< 1 || height
< 1) {
96 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
97 RErrorCode
= RERR_BADIMAGEFILE
;
101 /* check for an alpha channel */
102 if (png_get_valid(png
, pinfo
, PNG_INFO_tRNS
))
105 alpha
= (color_type
& PNG_COLOR_MASK_ALPHA
);
107 /* allocate RImage */
108 image
= RCreateImage(width
, height
, alpha
);
111 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
115 /* normalize to 8bpp with alpha channel */
116 if (color_type
== PNG_COLOR_TYPE_PALETTE
&& depth
<= 8)
119 if (color_type
== PNG_COLOR_TYPE_GRAY
&& depth
<= 8)
122 if (png_get_valid(png
, pinfo
, PNG_INFO_tRNS
))
126 png_set_strip_16(png
);
128 if (color_type
== PNG_COLOR_TYPE_GRAY
|| color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
)
129 png_set_gray_to_rgb(png
);
131 /* set gamma correction */
132 if ((context
->attribs
->flags
& RC_GammaCorrection
)
133 && context
->depth
!= 8) {
134 sgamma
= (context
->attribs
->rgamma
+ context
->attribs
->ggamma
+ context
->attribs
->bgamma
) / 3;
135 } else if ((tmp
= getenv("DISPLAY_GAMMA")) != NULL
) {
144 if (png_get_gAMA(png
, pinfo
, &gamma
))
145 png_set_gamma(png
, sgamma
, gamma
);
147 png_set_gamma(png
, sgamma
, 0.45);
149 /* do the transforms */
150 png_read_update_info(png
, pinfo
);
152 /* set background color */
153 if (png_get_bKGD(png
, pinfo
, &bkcolor
)) {
154 image
->background
.red
= bkcolor
->red
>> 8;
155 image
->background
.green
= bkcolor
->green
>> 8;
156 image
->background
.blue
= bkcolor
->blue
>> 8;
159 png_rows
= calloc(height
, sizeof(char *));
161 RErrorCode
= RERR_NOMEMORY
;
163 RReleaseImage(image
);
164 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
167 for (y
= 0; y
< height
; y
++) {
168 png_rows
[y
] = malloc(png_get_rowbytes(png
, pinfo
));
170 RErrorCode
= RERR_NOMEMORY
;
172 RReleaseImage(image
);
173 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
182 png_read_image(png
, png_rows
);
184 png_read_end(png
, einfo
);
186 png_destroy_read_struct(&png
, &pinfo
, &einfo
);
192 /* convert to RImage */
194 for (y
= 0; y
< height
; y
++) {
195 for (x
= 0, i
= width
* 4; x
< i
; x
++, ptr
++) {
196 *ptr
= *(png_rows
[y
] + x
);
200 for (y
= 0; y
< height
; y
++) {
201 for (x
= 0, i
= width
* 3; x
< i
; x
++, ptr
++) {
202 *ptr
= *(png_rows
[y
] + x
);
206 for (y
= 0; y
< height
; y
++)