Debian Codegraph @

gutils/gimagereadpng.c

raw symbols SCIP
1 /* Copyright (C) 2000-2012 by George Williams */
2 /*
3 * Redistribution and use in source and binary forms, with or without
4 * modification, are permitted provided that the following conditions are met:
5
6 * Redistributions of source code must retain the above copyright notice, this
7 * list of conditions and the following disclaimer.
8
9 * Redistributions in binary form must reproduce the above copyright notice,
10 * this list of conditions and the following disclaimer in the documentation
11 * and/or other materials provided with the distribution.
12
13 * The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <fontforge-config.h>
29
30 #ifdef _NO_LIBPNG
31
32 static void *a_file_must_define_something=(void *) &a_file_must_define_something;
33 /* ANSI says so */
34
35 #else
36
37 # include <png.h>
38
39 # include "basics.h"
40 # include "gimage.h"
41
42 struct mem_buffer {
43 char *buffer;
44 size_t size;
45 size_t read;
46 };
47
48 static void user_error_fn(png_structp png_ptr, png_const_charp error_msg) {
49 fprintf(stderr,"%s\n", error_msg);
50 #if (PNG_LIBPNG_VER < 10500)
51 longjmp(png_ptr->jmpbuf,1);
52 #else
53 png_longjmp (png_ptr, 1);
54 #endif
55 }
56
57 static void user_warning_fn(png_structp UNUSED(png_ptr), png_const_charp warning_msg) {
58 fprintf(stderr,"%s\n", warning_msg);
59 }
60
61 static void mem_read_fn(png_structp png_ptr, png_bytep data, png_size_t sz) {
62 struct mem_buffer* buf = (struct mem_buffer*)png_get_io_ptr(png_ptr);
63
64 if (buf->read + sz > buf->size) {
65 png_error(png_ptr, "memory buffer is too small");
66 return;
67 }
68
69 memcpy(data, buf->buffer+buf->read, sz);
70 buf->read += sz;
71 }
72
73 static GImage *GImageReadPngFull(void *io, int in_memory) {
74 GImage *ret=NULL;
75 struct _GImage *base;
76 png_structp png_ptr;
77 png_infop info_ptr;
78 png_bytep *row_pointers=NULL;
79 png_bytep trans_alpha;
80 int num_trans;
81 png_color_16p trans_color;
82 unsigned i;
83 int test;
84
85 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
86 (void *)NULL, user_error_fn, user_warning_fn);
87
88 if (!png_ptr)
89 return( NULL );
90
91 info_ptr = png_create_info_struct(png_ptr);
92 if (!info_ptr) {
93 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
94 return( NULL );
95 }
96
97 #if (PNG_LIBPNG_VER < 10500)
98 test = setjmp(png_ptr->jmpbuf);
99 #else
100 test = setjmp(*png_set_longjmp_fn(png_ptr, longjmp, sizeof (jmp_buf)));
101 #endif
102 if (test) {
103 /* Free all of the memory associated with the png_ptr and info_ptr */
104 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
105 if ( ret!=NULL ) {
106 GImageDestroy(ret);
107 free(row_pointers);
108 }
109 /* If we get here, we had a problem reading the file */
110 return( NULL );
111 }
112
113 if (in_memory) {
114 png_set_read_fn(png_ptr, io, mem_read_fn);
115 } else {
116 png_init_io(png_ptr, (FILE*)io);
117 }
118 png_read_info(png_ptr, info_ptr);
119 png_set_strip_16(png_ptr);
120 if ( (png_get_color_type(png_ptr, info_ptr)==PNG_COLOR_TYPE_GRAY || png_get_color_type(png_ptr, info_ptr)==PNG_COLOR_TYPE_PALETTE ) &&
121 png_get_bit_depth(png_ptr,info_ptr) == 1 )
122 /* Leave bitmaps packed */;
123 else
124 png_set_packing(png_ptr);
125 if ( png_get_color_type(png_ptr, info_ptr)==PNG_COLOR_TYPE_GRAY_ALPHA )
126 png_set_strip_alpha(png_ptr);
127 if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGB)
128 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
129
130 if ( png_get_color_type(png_ptr, info_ptr)==PNG_COLOR_TYPE_GRAY && png_get_bit_depth(png_ptr,info_ptr) == 1 ) {
131 ret = GImageCreate(it_mono,png_get_image_width(png_ptr,info_ptr),png_get_image_height(png_ptr,info_ptr));
132 } else if ( png_get_color_type(png_ptr, info_ptr)==PNG_COLOR_TYPE_GRAY || png_get_color_type(png_ptr, info_ptr)==PNG_COLOR_TYPE_GRAY_ALPHA ) {
133 GClut *clut;
134 ret = GImageCreate(it_index,png_get_image_width(png_ptr,info_ptr),png_get_image_height(png_ptr,info_ptr));
135 clut = ret->u.image->clut;
136 clut->is_grey = true;
137 clut->clut_len = 256;
138 for ( i=0; i<256; ++i )
139 clut->clut[i] = COLOR_CREATE(i,i,i);
140 } else if ( png_get_color_type(png_ptr, info_ptr)==PNG_COLOR_TYPE_RGB_ALPHA ) {
141 ret = GImageCreate(it_rgba,png_get_image_width(png_ptr,info_ptr),png_get_image_height(png_ptr,info_ptr));
142 } else if ( png_get_color_type(png_ptr, info_ptr)==PNG_COLOR_TYPE_RGB || png_get_color_type(png_ptr, info_ptr)==PNG_COLOR_TYPE_RGB_ALPHA )
143 ret = GImageCreate(it_true,png_get_image_width(png_ptr,info_ptr),png_get_image_height(png_ptr,info_ptr));
144 else {
145 png_colorp palette;
146 int num_palette;
147 GClut *clut;
148 ret = GImageCreate(png_get_bit_depth(png_ptr,info_ptr) != 1? it_index : it_mono,
149 png_get_image_width(png_ptr,info_ptr),png_get_image_height(png_ptr,info_ptr));
150 clut = ret->u.image->clut;
151 if ( clut==NULL )
152 clut = ret->u.image->clut = (GClut *) calloc(1,sizeof(GClut));
153 clut->is_grey = true;
154 png_get_PLTE(png_ptr,info_ptr,&palette,&num_palette);
155 clut->clut_len = num_palette;
156 for ( i=0; i<(unsigned)num_palette; ++i )
157 clut->clut[i] = COLOR_CREATE(palette[i].red,
158 palette[i].green,
159 palette[i].blue);
160 }
161 png_get_tRNS(png_ptr,info_ptr,&trans_alpha,&num_trans,&trans_color);
162 base = ret->u.image;
163 if ( (png_get_valid(png_ptr,info_ptr,PNG_INFO_tRNS)) && num_trans>0 ) {
164 if ( png_get_color_type(png_ptr, info_ptr)==PNG_COLOR_TYPE_RGB || png_get_color_type(png_ptr, info_ptr)==PNG_COLOR_TYPE_RGB_ALPHA )
165 base->trans = COLOR_CREATE(
166 (trans_color->red>>8),
167 (trans_color->green>>8),
168 (trans_color->blue>>8));
169 else if ( base->image_type == it_mono )
170 base->trans = trans_alpha ? trans_alpha[0] : 0;
171 else
172 base->clut->trans_index = base->trans = trans_alpha ? trans_alpha[0] : 0;
173 }
174
175 row_pointers = (png_byte **) malloc(png_get_image_height(png_ptr,info_ptr)*sizeof(png_bytep));
176 for ( i=0; i<png_get_image_height(png_ptr,info_ptr); ++i )
177 row_pointers[i] = (png_bytep) (base->data + i*base->bytes_per_line);
178
179 /* Ignore progressive loads for now */
180 /* libpng wants me to do it with callbacks, but that doesn't sit well */
181 /* with my wish to be in control... */
182 png_read_image(png_ptr,row_pointers);
183 png_read_end(png_ptr, NULL);
184
185 if ( png_get_color_type(png_ptr, info_ptr)==PNG_COLOR_TYPE_RGB || png_get_color_type(png_ptr, info_ptr)==PNG_COLOR_TYPE_RGB_ALPHA ) {
186 /* PNG orders its bytes as AABBGGRR instead of 00RRGGBB */
187 uint32_t *ipt, *iend;
188 for ( ipt = (uint32_t *) (base->data), iend=ipt+base->width*base->height; ipt<iend; ++ipt ) {
189 uint32_t r, g, b, a = *ipt&0xff000000;
190 r = (*ipt )&0xff;
191 g = (*ipt>>8 )&0xff;
192 b = (*ipt>>16)&0xff;
193 *ipt = COLOR_CREATE( r,g,b ) | a;
194 }
195 }
196
197 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
198 free(row_pointers);
199 /* Note png b&w images come out as indexed */
200 return( ret );
201 }
202
203 GImage *GImageReadPngBuf(char* buf, size_t sz) {
204 struct mem_buffer membuf = {buf, sz, 0};
205 return GImageReadPngFull(&membuf, true);
206 }
207
208 GImage *GImageRead_Png(FILE *fp) {
209 return GImageReadPngFull(fp, false);
210 }
211
212 GImage *GImageReadPng(char *filename) {
213 GImage *ret=NULL;
214 FILE *fp;
215
216 fp = fopen(filename, "rb");
217 if (!fp)
218 return( NULL );
219
220 ret = GImageRead_Png(fp);
221 fclose(fp);
222 return( ret );
223 }
224 #endif