Line data Source code
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 int a_file_must_define_something=0; /* ANSI says so */
33 :
34 : #else
35 :
36 : #include <png.h>
37 :
38 : #define int32 _int32
39 : #define uint32 _uint32
40 : #define int16 _int16
41 : #define uint16 _uint16
42 : #define int8 _int8
43 : #define uint8 _uint8
44 :
45 : #include "gimage.h"
46 :
47 0 : static void user_error_fn(png_structp png_ptr, png_const_charp error_msg) {
48 0 : fprintf(stderr, "%s\n", error_msg );
49 : #if (PNG_LIBPNG_VER < 10500)
50 0 : longjmp(png_ptr->jmpbuf,1);
51 : #else
52 : png_longjmp (png_ptr, 1);
53 : #endif
54 : }
55 :
56 0 : static void user_warning_fn(png_structp UNUSED(png_ptr), png_const_charp warning_msg) {
57 0 : fprintf(stderr,"%s\n", warning_msg);
58 0 : }
59 :
60 1 : int GImageWrite_Png(GImage *gi, FILE *fp, int progressive) {
61 1 : struct _GImage *base = gi->list_len==0?gi->u.image:gi->u.images[0];
62 : png_structp png_ptr;
63 : png_infop info_ptr;
64 : png_byte **rows;
65 : int i;
66 : int bit_depth;
67 : int color_type;
68 : int num_palette;
69 1 : png_bytep trans_alpha = NULL;
70 1 : png_color_16p trans_color = NULL;
71 1 : png_colorp palette = NULL;
72 :
73 1 : png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
74 : (void *)NULL, user_error_fn, user_warning_fn);
75 :
76 1 : if (!png_ptr) {
77 0 : return(false);
78 : }
79 :
80 1 : info_ptr = png_create_info_struct(png_ptr);
81 1 : if (!info_ptr) {
82 0 : png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
83 0 : return(false);
84 : }
85 :
86 : #if (PNG_LIBPNG_VER < 10500)
87 1 : if (setjmp(png_ptr->jmpbuf))
88 : #else
89 : if (setjmp(*png_set_longjmp_fn(png_ptr, longjmp, sizeof (jmp_buf))))
90 : #endif
91 : {
92 0 : png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
93 0 : return(false);
94 : }
95 :
96 1 : png_init_io(png_ptr, fp);
97 :
98 1 : bit_depth = 8;
99 1 : num_palette = base->clut==NULL?2:base->clut->clut_len;
100 1 : if ( base->image_type==it_index || base->image_type==it_bitmap ) {
101 1 : color_type = PNG_COLOR_TYPE_PALETTE;
102 2 : if ( num_palette<=2 )
103 0 : bit_depth=1;
104 1 : else if ( num_palette<=4 )
105 0 : bit_depth=2;
106 1 : else if ( num_palette<=16 )
107 0 : bit_depth=4;
108 : } else {
109 0 : color_type = PNG_COLOR_TYPE_RGB;
110 0 : if ( base->image_type == it_rgba )
111 0 : color_type = PNG_COLOR_TYPE_RGB_ALPHA;
112 : }
113 :
114 1 : png_set_IHDR(png_ptr, info_ptr, base->width, base->height,
115 : bit_depth, color_type, progressive,
116 : PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
117 1 : if ( base->image_type==it_index || base->image_type==it_bitmap ) {
118 1 : palette = (png_color *) malloc(num_palette*sizeof(png_color));
119 1 : if ( base->clut==NULL ) {
120 0 : palette[0].red = palette[0].green = palette[0].blue = 0;
121 0 : palette[1].red = palette[1].green = palette[1].blue = 0xff;
122 : } else {
123 33 : for ( i=0; i<num_palette; ++i ) {
124 32 : long col = base->clut->clut[i];
125 32 : palette[i].red = COLOR_RED(col);
126 32 : palette[i].green = COLOR_GREEN(col);
127 32 : palette[i].blue = COLOR_BLUE(col);
128 : }
129 : }
130 1 : png_set_PLTE(png_ptr, info_ptr, palette, num_palette);
131 1 : if ( num_palette<=16 )
132 0 : png_set_packing(png_ptr);
133 :
134 2 : if ( base->trans!=(Color)-1 ) {
135 0 : trans_alpha = (png_bytep) malloc(1);
136 0 : trans_alpha[0] = base->trans;
137 : }
138 : } else {
139 0 : if ( base->trans!=(Color)-1 ) {
140 0 : trans_color = (png_color_16p) malloc(sizeof(png_color_16));
141 0 : trans_color->red = COLOR_RED(base->trans);
142 0 : trans_color->green = COLOR_GREEN(base->trans);
143 0 : trans_color->blue = COLOR_BLUE(base->trans);
144 : }
145 : }
146 1 : if ( base->trans!=(Color)-1 ) {
147 0 : png_set_tRNS(png_ptr, info_ptr, trans_alpha, 1, trans_color);
148 : }
149 1 : png_write_info(png_ptr, info_ptr);
150 :
151 1 : if (color_type == PNG_COLOR_TYPE_RGB)
152 0 : png_set_filler(png_ptr, '\0', PNG_FILLER_BEFORE);
153 :
154 1 : rows = (png_byte **) malloc(base->height*sizeof(png_byte *));
155 33 : for ( i=0; i<base->height; ++i )
156 32 : rows[i] = (png_byte *) (base->data + i*base->bytes_per_line);
157 :
158 1 : png_write_image(png_ptr,rows);
159 :
160 1 : png_write_end(png_ptr, info_ptr);
161 :
162 1 : free(trans_alpha);
163 1 : free(trans_color);
164 1 : free(palette);
165 1 : png_destroy_write_struct(&png_ptr, &info_ptr);
166 1 : free(rows);
167 1 : return( 1 );
168 : }
169 :
170 0 : int GImageWritePng(GImage *gi, char *filename, int progressive) {
171 : FILE *fp;
172 : int ret;
173 :
174 : /* open the file */
175 0 : fp = fopen(filename, "wb");
176 0 : if (!fp)
177 0 : return(false);
178 0 : ret = GImageWrite_Png(gi,fp,progressive);
179 0 : fclose(fp);
180 0 : return( ret );
181 : }
182 :
183 : #endif
|