31 #if !defined(_ANDROID)
40 #include <libFreeWRL.h>
81 #define UNUSED(v) ((void) v)
86 # define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
90 static png_structp png_ptr = NULL;
91 static png_infop info_ptr = NULL;
93 png_uint_32 width, height;
94 int bit_depth, color_type;
95 uch *image_data = NULL;
99 int readpng_init(FILE *infile, ulg *pWidth, ulg *pHeight)
109 rv=fread(sig, 1, 8, infile);
110 #if ((PNG_LIBPNG_VER_MAJOR == 1) && (PNG_LIBPNG_VER_MINOR < 2))
111 if (!png_check_sig(sig, 8))
113 if (!png_sig_cmp(sig, 0, 8))
120 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
124 info_ptr = png_create_info_struct(png_ptr);
126 png_destroy_read_struct(&png_ptr, NULL, NULL);
139 if (setjmp(png_jmpbuf(png_ptr))) {
140 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
145 png_init_io(png_ptr, infile);
146 png_set_sig_bytes(png_ptr, 8);
148 png_read_info(png_ptr, info_ptr);
155 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
169 uch *readpng_get_image(
double display_exponent,
int *pChannels, ulg *pRowbytes)
171 png_uint_32 i, rowbytes;
172 png_bytepp row_pointers = NULL;
173 UNUSED (display_exponent);
179 if (setjmp(png_jmpbuf(png_ptr))) {
180 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
189 if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth <= 8)
190 png_set_expand(png_ptr);
191 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
192 png_set_expand(png_ptr);
193 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
194 png_set_expand(png_ptr);
196 png_set_strip_16(png_ptr);
213 png_read_update_info(png_ptr, info_ptr);
215 *pRowbytes = rowbytes = png_get_rowbytes(png_ptr, info_ptr);
216 *pChannels = (int)png_get_channels(png_ptr, info_ptr);
218 if ((image_data = MALLOC(uch *, rowbytes*height)) == NULL) {
219 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
222 if ((row_pointers = MALLOC(png_bytepp, height*
sizeof(png_bytep))) == NULL) {
223 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
224 FREE_IF_NZ(image_data);
228 Trace((stderr,
"readpng_get_image: channels = %d, rowbytes = %ld, height = %ld\n", *pChannels, rowbytes, height));
233 for (i = 0; i < height; ++i) {
235 row_pointers[i] = image_data + (height-i-1)*rowbytes;
241 png_read_image(png_ptr, row_pointers);
247 FREE_IF_NZ(row_pointers);
254 void readpng_cleanup(
int free_image_data)
256 if (free_image_data && image_data) {
257 FREE_IF_NZ(image_data);
260 if (png_ptr && info_ptr) {
261 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);