/src/freeimage-svn/FreeImage/trunk/Source/LibPNG/pngwrite.c
Line  | Count  | Source  | 
1  |  |  | 
2  |  | /* pngwrite.c - general routines to write a PNG file  | 
3  |  |  *  | 
4  |  |  * Copyright (c) 2018-2022 Cosmin Truta  | 
5  |  |  * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson  | 
6  |  |  * Copyright (c) 1996-1997 Andreas Dilger  | 
7  |  |  * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.  | 
8  |  |  *  | 
9  |  |  * This code is released under the libpng license.  | 
10  |  |  * For conditions of distribution and use, see the disclaimer  | 
11  |  |  * and license in png.h  | 
12  |  |  */  | 
13  |  |  | 
14  |  | #include "pngpriv.h"  | 
15  |  | #ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED  | 
16  |  | #  include <errno.h>  | 
17  |  | #endif /* SIMPLIFIED_WRITE_STDIO */  | 
18  |  |  | 
19  |  | #ifdef PNG_WRITE_SUPPORTED  | 
20  |  |  | 
21  |  | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED  | 
22  |  | /* Write out all the unknown chunks for the current given location */  | 
23  |  | static void  | 
24  |  | write_unknown_chunks(png_structrp png_ptr, png_const_inforp info_ptr,  | 
25  |  |     unsigned int where)  | 
26  | 0  | { | 
27  | 0  |    if (info_ptr->unknown_chunks_num != 0)  | 
28  | 0  |    { | 
29  | 0  |       png_const_unknown_chunkp up;  | 
30  |  | 
  | 
31  | 0  |       png_debug(5, "writing extra chunks");  | 
32  |  | 
  | 
33  | 0  |       for (up = info_ptr->unknown_chunks;  | 
34  | 0  |            up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;  | 
35  | 0  |            ++up)  | 
36  | 0  |          if ((up->location & where) != 0)  | 
37  | 0  |       { | 
38  |  |          /* If per-chunk unknown chunk handling is enabled use it, otherwise  | 
39  |  |           * just write the chunks the application has set.  | 
40  |  |           */  | 
41  | 0  | #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED  | 
42  | 0  |          int keep = png_handle_as_unknown(png_ptr, up->name);  | 
43  |  |  | 
44  |  |          /* NOTE: this code is radically different from the read side in the  | 
45  |  |           * matter of handling an ancillary unknown chunk.  In the read side  | 
46  |  |           * the default behavior is to discard it, in the code below the default  | 
47  |  |           * behavior is to write it.  Critical chunks are, however, only  | 
48  |  |           * written if explicitly listed or if the default is set to write all  | 
49  |  |           * unknown chunks.  | 
50  |  |           *  | 
51  |  |           * The default handling is also slightly weird - it is not possible to  | 
52  |  |           * stop the writing of all unsafe-to-copy chunks!  | 
53  |  |           *  | 
54  |  |           * TODO: REVIEW: this would seem to be a bug.  | 
55  |  |           */  | 
56  | 0  |          if (keep != PNG_HANDLE_CHUNK_NEVER &&  | 
57  | 0  |              ((up->name[3] & 0x20) /* safe-to-copy overrides everything */ ||  | 
58  | 0  |               keep == PNG_HANDLE_CHUNK_ALWAYS ||  | 
59  | 0  |               (keep == PNG_HANDLE_CHUNK_AS_DEFAULT &&  | 
60  | 0  |                png_ptr->unknown_default == PNG_HANDLE_CHUNK_ALWAYS)))  | 
61  | 0  | #endif  | 
62  | 0  |          { | 
63  |  |             /* TODO: review, what is wrong with a zero length unknown chunk? */  | 
64  | 0  |             if (up->size == 0)  | 
65  | 0  |                png_warning(png_ptr, "Writing zero-length unknown chunk");  | 
66  |  | 
  | 
67  | 0  |             png_write_chunk(png_ptr, up->name, up->data, up->size);  | 
68  | 0  |          }  | 
69  | 0  |       }  | 
70  | 0  |    }  | 
71  | 0  | }  | 
72  |  | #endif /* WRITE_UNKNOWN_CHUNKS */  | 
73  |  |  | 
74  |  | /* Writes all the PNG information.  This is the suggested way to use the  | 
75  |  |  * library.  If you have a new chunk to add, make a function to write it,  | 
76  |  |  * and put it in the correct location here.  If you want the chunk written  | 
77  |  |  * after the image data, put it in png_write_end().  I strongly encourage  | 
78  |  |  * you to supply a PNG_INFO_<chunk> flag, and check info_ptr->valid before  | 
79  |  |  * writing the chunk, as that will keep the code from breaking if you want  | 
80  |  |  * to just write a plain PNG file.  If you have long comments, I suggest  | 
81  |  |  * writing them in png_write_end(), and compressing them.  | 
82  |  |  */  | 
83  |  | void PNGAPI  | 
84  |  | png_write_info_before_PLTE(png_structrp png_ptr, png_const_inforp info_ptr)  | 
85  | 0  | { | 
86  | 0  |    png_debug(1, "in png_write_info_before_PLTE");  | 
87  |  | 
  | 
88  | 0  |    if (png_ptr == NULL || info_ptr == NULL)  | 
89  | 0  |       return;  | 
90  |  |  | 
91  | 0  |    if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0)  | 
92  | 0  |    { | 
93  |  |       /* Write PNG signature */  | 
94  | 0  |       png_write_sig(png_ptr);  | 
95  |  | 
  | 
96  | 0  | #ifdef PNG_MNG_FEATURES_SUPPORTED  | 
97  | 0  |       if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0 && \  | 
98  | 0  |           png_ptr->mng_features_permitted != 0)  | 
99  | 0  |       { | 
100  | 0  |          png_warning(png_ptr,  | 
101  | 0  |              "MNG features are not allowed in a PNG datastream");  | 
102  | 0  |          png_ptr->mng_features_permitted = 0;  | 
103  | 0  |       }  | 
104  | 0  | #endif  | 
105  |  |  | 
106  |  |       /* Write IHDR information. */  | 
107  | 0  |       png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,  | 
108  | 0  |           info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,  | 
109  | 0  |           info_ptr->filter_type,  | 
110  | 0  | #ifdef PNG_WRITE_INTERLACING_SUPPORTED  | 
111  | 0  |           info_ptr->interlace_type  | 
112  |  | #else  | 
113  |  |           0  | 
114  |  | #endif  | 
115  | 0  |          );  | 
116  |  |  | 
117  |  |       /* The rest of these check to see if the valid field has the appropriate  | 
118  |  |        * flag set, and if it does, writes the chunk.  | 
119  |  |        *  | 
120  |  |        * 1.6.0: COLORSPACE support controls the writing of these chunks too, and  | 
121  |  |        * the chunks will be written if the WRITE routine is there and  | 
122  |  |        * information * is available in the COLORSPACE. (See  | 
123  |  |        * png_colorspace_sync_info in png.c for where the valid flags get set.)  | 
124  |  |        *  | 
125  |  |        * Under certain circumstances the colorspace can be invalidated without  | 
126  |  |        * syncing the info_struct 'valid' flags; this happens if libpng detects  | 
127  |  |        * an error and calls png_error while the color space is being set, yet  | 
128  |  |        * the application continues writing the PNG.  So check the 'invalid'  | 
129  |  |        * flag here too.  | 
130  |  |        */  | 
131  | 0  | #ifdef PNG_GAMMA_SUPPORTED  | 
132  | 0  | #  ifdef PNG_WRITE_gAMA_SUPPORTED  | 
133  | 0  |       if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&  | 
134  | 0  |           (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_gAMA) != 0 &&  | 
135  | 0  |           (info_ptr->valid & PNG_INFO_gAMA) != 0)  | 
136  | 0  |          png_write_gAMA_fixed(png_ptr, info_ptr->colorspace.gamma);  | 
137  | 0  | #  endif  | 
138  | 0  | #endif  | 
139  |  | 
  | 
140  | 0  | #ifdef PNG_COLORSPACE_SUPPORTED  | 
141  |  |       /* Write only one of sRGB or an ICC profile.  If a profile was supplied  | 
142  |  |        * and it matches one of the known sRGB ones issue a warning.  | 
143  |  |        */  | 
144  | 0  | #  ifdef PNG_WRITE_iCCP_SUPPORTED  | 
145  | 0  |          if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&  | 
146  | 0  |              (info_ptr->valid & PNG_INFO_iCCP) != 0)  | 
147  | 0  |          { | 
148  | 0  | #    ifdef PNG_WRITE_sRGB_SUPPORTED  | 
149  | 0  |                if ((info_ptr->valid & PNG_INFO_sRGB) != 0)  | 
150  | 0  |                   png_app_warning(png_ptr,  | 
151  | 0  |                       "profile matches sRGB but writing iCCP instead");  | 
152  | 0  | #     endif  | 
153  |  | 
  | 
154  | 0  |             png_write_iCCP(png_ptr, info_ptr->iccp_name,  | 
155  | 0  |                 info_ptr->iccp_profile);  | 
156  | 0  |          }  | 
157  | 0  | #     ifdef PNG_WRITE_sRGB_SUPPORTED  | 
158  | 0  |          else  | 
159  | 0  | #     endif  | 
160  | 0  | #  endif  | 
161  |  |  | 
162  | 0  | #  ifdef PNG_WRITE_sRGB_SUPPORTED  | 
163  | 0  |          if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&  | 
164  | 0  |              (info_ptr->valid & PNG_INFO_sRGB) != 0)  | 
165  | 0  |             png_write_sRGB(png_ptr, info_ptr->colorspace.rendering_intent);  | 
166  | 0  | #  endif /* WRITE_sRGB */  | 
167  | 0  | #endif /* COLORSPACE */  | 
168  |  | 
  | 
169  | 0  | #ifdef PNG_WRITE_sBIT_SUPPORTED  | 
170  | 0  |          if ((info_ptr->valid & PNG_INFO_sBIT) != 0)  | 
171  | 0  |             png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);  | 
172  | 0  | #endif  | 
173  |  | 
  | 
174  | 0  | #ifdef PNG_COLORSPACE_SUPPORTED  | 
175  | 0  | #  ifdef PNG_WRITE_cHRM_SUPPORTED  | 
176  | 0  |          if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&  | 
177  | 0  |              (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0 &&  | 
178  | 0  |              (info_ptr->valid & PNG_INFO_cHRM) != 0)  | 
179  | 0  |             png_write_cHRM_fixed(png_ptr, &info_ptr->colorspace.end_points_xy);  | 
180  | 0  | #  endif  | 
181  | 0  | #endif  | 
182  |  | 
  | 
183  | 0  | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED  | 
184  | 0  |          write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_IHDR);  | 
185  | 0  | #endif  | 
186  |  | 
  | 
187  | 0  |       png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;  | 
188  | 0  |    }  | 
189  | 0  | }  | 
190  |  |  | 
191  |  | void PNGAPI  | 
192  |  | png_write_info(png_structrp png_ptr, png_const_inforp info_ptr)  | 
193  | 0  | { | 
194  | 0  | #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)  | 
195  | 0  |    int i;  | 
196  | 0  | #endif  | 
197  |  | 
  | 
198  | 0  |    png_debug(1, "in png_write_info");  | 
199  |  | 
  | 
200  | 0  |    if (png_ptr == NULL || info_ptr == NULL)  | 
201  | 0  |       return;  | 
202  |  |  | 
203  | 0  |    png_write_info_before_PLTE(png_ptr, info_ptr);  | 
204  |  | 
  | 
205  | 0  |    if ((info_ptr->valid & PNG_INFO_PLTE) != 0)  | 
206  | 0  |       png_write_PLTE(png_ptr, info_ptr->palette,  | 
207  | 0  |           (png_uint_32)info_ptr->num_palette);  | 
208  |  |  | 
209  | 0  |    else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)  | 
210  | 0  |       png_error(png_ptr, "Valid palette required for paletted images");  | 
211  |  |  | 
212  | 0  | #ifdef PNG_WRITE_tRNS_SUPPORTED  | 
213  | 0  |    if ((info_ptr->valid & PNG_INFO_tRNS) !=0)  | 
214  | 0  |    { | 
215  | 0  | #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED  | 
216  |  |       /* Invert the alpha channel (in tRNS) */  | 
217  | 0  |       if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0 &&  | 
218  | 0  |           info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)  | 
219  | 0  |       { | 
220  | 0  |          int j, jend;  | 
221  |  | 
  | 
222  | 0  |          jend = info_ptr->num_trans;  | 
223  | 0  |          if (jend > PNG_MAX_PALETTE_LENGTH)  | 
224  | 0  |             jend = PNG_MAX_PALETTE_LENGTH;  | 
225  |  | 
  | 
226  | 0  |          for (j = 0; j<jend; ++j)  | 
227  | 0  |             info_ptr->trans_alpha[j] =  | 
228  | 0  |                (png_byte)(255 - info_ptr->trans_alpha[j]);  | 
229  | 0  |       }  | 
230  | 0  | #endif  | 
231  | 0  |       png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),  | 
232  | 0  |           info_ptr->num_trans, info_ptr->color_type);  | 
233  | 0  |    }  | 
234  | 0  | #endif  | 
235  | 0  | #ifdef PNG_WRITE_bKGD_SUPPORTED  | 
236  | 0  |    if ((info_ptr->valid & PNG_INFO_bKGD) != 0)  | 
237  | 0  |       png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);  | 
238  | 0  | #endif  | 
239  |  | 
  | 
240  |  | #ifdef PNG_WRITE_eXIf_SUPPORTED  | 
241  |  |    if ((info_ptr->valid & PNG_INFO_eXIf) != 0)  | 
242  |  |       png_write_eXIf(png_ptr, info_ptr->exif, info_ptr->num_exif);  | 
243  |  | #endif  | 
244  |  | 
  | 
245  | 0  | #ifdef PNG_WRITE_hIST_SUPPORTED  | 
246  | 0  |    if ((info_ptr->valid & PNG_INFO_hIST) != 0)  | 
247  | 0  |       png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);  | 
248  | 0  | #endif  | 
249  |  | 
  | 
250  | 0  | #ifdef PNG_WRITE_oFFs_SUPPORTED  | 
251  | 0  |    if ((info_ptr->valid & PNG_INFO_oFFs) != 0)  | 
252  | 0  |       png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,  | 
253  | 0  |           info_ptr->offset_unit_type);  | 
254  | 0  | #endif  | 
255  |  | 
  | 
256  | 0  | #ifdef PNG_WRITE_pCAL_SUPPORTED  | 
257  | 0  |    if ((info_ptr->valid & PNG_INFO_pCAL) != 0)  | 
258  | 0  |       png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,  | 
259  | 0  |           info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,  | 
260  | 0  |           info_ptr->pcal_units, info_ptr->pcal_params);  | 
261  | 0  | #endif  | 
262  |  | 
  | 
263  | 0  | #ifdef PNG_WRITE_sCAL_SUPPORTED  | 
264  | 0  |    if ((info_ptr->valid & PNG_INFO_sCAL) != 0)  | 
265  | 0  |       png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,  | 
266  | 0  |           info_ptr->scal_s_width, info_ptr->scal_s_height);  | 
267  | 0  | #endif /* sCAL */  | 
268  |  | 
  | 
269  | 0  | #ifdef PNG_WRITE_pHYs_SUPPORTED  | 
270  | 0  |    if ((info_ptr->valid & PNG_INFO_pHYs) != 0)  | 
271  | 0  |       png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,  | 
272  | 0  |           info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);  | 
273  | 0  | #endif /* pHYs */  | 
274  |  | 
  | 
275  | 0  | #ifdef PNG_WRITE_tIME_SUPPORTED  | 
276  | 0  |    if ((info_ptr->valid & PNG_INFO_tIME) != 0)  | 
277  | 0  |    { | 
278  | 0  |       png_write_tIME(png_ptr, &(info_ptr->mod_time));  | 
279  | 0  |       png_ptr->mode |= PNG_WROTE_tIME;  | 
280  | 0  |    }  | 
281  | 0  | #endif /* tIME */  | 
282  |  | 
  | 
283  | 0  | #ifdef PNG_WRITE_sPLT_SUPPORTED  | 
284  | 0  |    if ((info_ptr->valid & PNG_INFO_sPLT) != 0)  | 
285  | 0  |       for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)  | 
286  | 0  |          png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);  | 
287  | 0  | #endif /* sPLT */  | 
288  |  | 
  | 
289  | 0  | #ifdef PNG_WRITE_TEXT_SUPPORTED  | 
290  |  |    /* Check to see if we need to write text chunks */  | 
291  | 0  |    for (i = 0; i < info_ptr->num_text; i++)  | 
292  | 0  |    { | 
293  | 0  |       png_debug2(2, "Writing header text chunk %d, type %d", i,  | 
294  | 0  |           info_ptr->text[i].compression);  | 
295  |  |       /* An internationalized chunk? */  | 
296  | 0  |       if (info_ptr->text[i].compression > 0)  | 
297  | 0  |       { | 
298  | 0  | #ifdef PNG_WRITE_iTXt_SUPPORTED  | 
299  |  |          /* Write international chunk */  | 
300  | 0  |          png_write_iTXt(png_ptr,  | 
301  | 0  |              info_ptr->text[i].compression,  | 
302  | 0  |              info_ptr->text[i].key,  | 
303  | 0  |              info_ptr->text[i].lang,  | 
304  | 0  |              info_ptr->text[i].lang_key,  | 
305  | 0  |              info_ptr->text[i].text);  | 
306  |  |          /* Mark this chunk as written */  | 
307  | 0  |          if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)  | 
308  | 0  |             info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;  | 
309  | 0  |          else  | 
310  | 0  |             info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;  | 
311  |  | #else  | 
312  |  |          png_warning(png_ptr, "Unable to write international text");  | 
313  |  | #endif  | 
314  | 0  |       }  | 
315  |  |  | 
316  |  |       /* If we want a compressed text chunk */  | 
317  | 0  |       else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)  | 
318  | 0  |       { | 
319  | 0  | #ifdef PNG_WRITE_zTXt_SUPPORTED  | 
320  |  |          /* Write compressed chunk */  | 
321  | 0  |          png_write_zTXt(png_ptr, info_ptr->text[i].key,  | 
322  | 0  |              info_ptr->text[i].text, info_ptr->text[i].compression);  | 
323  |  |          /* Mark this chunk as written */  | 
324  | 0  |          info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;  | 
325  |  | #else  | 
326  |  |          png_warning(png_ptr, "Unable to write compressed text");  | 
327  |  | #endif  | 
328  | 0  |       }  | 
329  |  |  | 
330  | 0  |       else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)  | 
331  | 0  |       { | 
332  | 0  | #ifdef PNG_WRITE_tEXt_SUPPORTED  | 
333  |  |          /* Write uncompressed chunk */  | 
334  | 0  |          png_write_tEXt(png_ptr, info_ptr->text[i].key,  | 
335  | 0  |              info_ptr->text[i].text,  | 
336  | 0  |              0);  | 
337  |  |          /* Mark this chunk as written */  | 
338  | 0  |          info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;  | 
339  |  | #else  | 
340  |  |          /* Can't get here */  | 
341  |  |          png_warning(png_ptr, "Unable to write uncompressed text");  | 
342  |  | #endif  | 
343  | 0  |       }  | 
344  | 0  |    }  | 
345  | 0  | #endif /* tEXt */  | 
346  |  | 
  | 
347  | 0  | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED  | 
348  | 0  |    write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_PLTE);  | 
349  | 0  | #endif  | 
350  | 0  | }  | 
351  |  |  | 
352  |  | /* Writes the end of the PNG file.  If you don't want to write comments or  | 
353  |  |  * time information, you can pass NULL for info.  If you already wrote these  | 
354  |  |  * in png_write_info(), do not write them again here.  If you have long  | 
355  |  |  * comments, I suggest writing them here, and compressing them.  | 
356  |  |  */  | 
357  |  | void PNGAPI  | 
358  |  | png_write_end(png_structrp png_ptr, png_inforp info_ptr)  | 
359  | 0  | { | 
360  | 0  |    png_debug(1, "in png_write_end");  | 
361  |  | 
  | 
362  | 0  |    if (png_ptr == NULL)  | 
363  | 0  |       return;  | 
364  |  |  | 
365  | 0  |    if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)  | 
366  | 0  |       png_error(png_ptr, "No IDATs written into file");  | 
367  |  |  | 
368  | 0  | #ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED  | 
369  | 0  |    if (png_ptr->num_palette_max > png_ptr->num_palette)  | 
370  | 0  |       png_benign_error(png_ptr, "Wrote palette index exceeding num_palette");  | 
371  | 0  | #endif  | 
372  |  |  | 
373  |  |    /* See if user wants us to write information chunks */  | 
374  | 0  |    if (info_ptr != NULL)  | 
375  | 0  |    { | 
376  | 0  | #ifdef PNG_WRITE_TEXT_SUPPORTED  | 
377  | 0  |       int i; /* local index variable */  | 
378  | 0  | #endif  | 
379  | 0  | #ifdef PNG_WRITE_tIME_SUPPORTED  | 
380  |  |       /* Check to see if user has supplied a time chunk */  | 
381  | 0  |       if ((info_ptr->valid & PNG_INFO_tIME) != 0 &&  | 
382  | 0  |           (png_ptr->mode & PNG_WROTE_tIME) == 0)  | 
383  | 0  |          png_write_tIME(png_ptr, &(info_ptr->mod_time));  | 
384  |  | 
  | 
385  | 0  | #endif  | 
386  | 0  | #ifdef PNG_WRITE_TEXT_SUPPORTED  | 
387  |  |       /* Loop through comment chunks */  | 
388  | 0  |       for (i = 0; i < info_ptr->num_text; i++)  | 
389  | 0  |       { | 
390  | 0  |          png_debug2(2, "Writing trailer text chunk %d, type %d", i,  | 
391  | 0  |              info_ptr->text[i].compression);  | 
392  |  |          /* An internationalized chunk? */  | 
393  | 0  |          if (info_ptr->text[i].compression > 0)  | 
394  | 0  |          { | 
395  | 0  | #ifdef PNG_WRITE_iTXt_SUPPORTED  | 
396  |  |             /* Write international chunk */  | 
397  | 0  |             png_write_iTXt(png_ptr,  | 
398  | 0  |                 info_ptr->text[i].compression,  | 
399  | 0  |                 info_ptr->text[i].key,  | 
400  | 0  |                 info_ptr->text[i].lang,  | 
401  | 0  |                 info_ptr->text[i].lang_key,  | 
402  | 0  |                 info_ptr->text[i].text);  | 
403  |  |             /* Mark this chunk as written */  | 
404  | 0  |             if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)  | 
405  | 0  |                info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;  | 
406  | 0  |             else  | 
407  | 0  |                info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;  | 
408  |  | #else  | 
409  |  |             png_warning(png_ptr, "Unable to write international text");  | 
410  |  | #endif  | 
411  | 0  |          }  | 
412  |  |  | 
413  | 0  |          else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)  | 
414  | 0  |          { | 
415  | 0  | #ifdef PNG_WRITE_zTXt_SUPPORTED  | 
416  |  |             /* Write compressed chunk */  | 
417  | 0  |             png_write_zTXt(png_ptr, info_ptr->text[i].key,  | 
418  | 0  |                 info_ptr->text[i].text, info_ptr->text[i].compression);  | 
419  |  |             /* Mark this chunk as written */  | 
420  | 0  |             info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;  | 
421  |  | #else  | 
422  |  |             png_warning(png_ptr, "Unable to write compressed text");  | 
423  |  | #endif  | 
424  | 0  |          }  | 
425  |  |  | 
426  | 0  |          else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)  | 
427  | 0  |          { | 
428  | 0  | #ifdef PNG_WRITE_tEXt_SUPPORTED  | 
429  |  |             /* Write uncompressed chunk */  | 
430  | 0  |             png_write_tEXt(png_ptr, info_ptr->text[i].key,  | 
431  | 0  |                 info_ptr->text[i].text, 0);  | 
432  |  |             /* Mark this chunk as written */  | 
433  | 0  |             info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;  | 
434  |  | #else  | 
435  |  |             png_warning(png_ptr, "Unable to write uncompressed text");  | 
436  |  | #endif  | 
437  | 0  |          }  | 
438  | 0  |       }  | 
439  | 0  | #endif  | 
440  |  | 
  | 
441  |  | #ifdef PNG_WRITE_eXIf_SUPPORTED  | 
442  |  |    if ((info_ptr->valid & PNG_INFO_eXIf) != 0)  | 
443  |  |       png_write_eXIf(png_ptr, info_ptr->exif, info_ptr->num_exif);  | 
444  |  | #endif  | 
445  |  | 
  | 
446  | 0  | #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED  | 
447  | 0  |       write_unknown_chunks(png_ptr, info_ptr, PNG_AFTER_IDAT);  | 
448  | 0  | #endif  | 
449  | 0  |    }  | 
450  |  | 
  | 
451  | 0  |    png_ptr->mode |= PNG_AFTER_IDAT;  | 
452  |  |  | 
453  |  |    /* Write end of PNG file */  | 
454  | 0  |    png_write_IEND(png_ptr);  | 
455  |  |  | 
456  |  |    /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,  | 
457  |  |     * and restored again in libpng-1.2.30, may cause some applications that  | 
458  |  |     * do not set png_ptr->output_flush_fn to crash.  If your application  | 
459  |  |     * experiences a problem, please try building libpng with  | 
460  |  |     * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to  | 
461  |  |     * png-mng-implement at lists.sf.net .  | 
462  |  |     */  | 
463  | 0  | #ifdef PNG_WRITE_FLUSH_SUPPORTED  | 
464  |  | #  ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED  | 
465  |  |    png_flush(png_ptr);  | 
466  |  | #  endif  | 
467  | 0  | #endif  | 
468  | 0  | }  | 
469  |  |  | 
470  |  | #ifdef PNG_CONVERT_tIME_SUPPORTED  | 
471  |  | void PNGAPI  | 
472  |  | png_convert_from_struct_tm(png_timep ptime, const struct tm * ttime)  | 
473  | 0  | { | 
474  | 0  |    png_debug(1, "in png_convert_from_struct_tm");  | 
475  |  | 
  | 
476  | 0  |    ptime->year = (png_uint_16)(1900 + ttime->tm_year);  | 
477  | 0  |    ptime->month = (png_byte)(ttime->tm_mon + 1);  | 
478  | 0  |    ptime->day = (png_byte)ttime->tm_mday;  | 
479  | 0  |    ptime->hour = (png_byte)ttime->tm_hour;  | 
480  | 0  |    ptime->minute = (png_byte)ttime->tm_min;  | 
481  | 0  |    ptime->second = (png_byte)ttime->tm_sec;  | 
482  | 0  | }  | 
483  |  |  | 
484  |  | void PNGAPI  | 
485  |  | png_convert_from_time_t(png_timep ptime, time_t ttime)  | 
486  | 0  | { | 
487  | 0  |    struct tm *tbuf;  | 
488  |  | 
  | 
489  | 0  |    png_debug(1, "in png_convert_from_time_t");  | 
490  |  | 
  | 
491  | 0  |    tbuf = gmtime(&ttime);  | 
492  | 0  |    if (tbuf == NULL)  | 
493  | 0  |    { | 
494  |  |       /* TODO: add a safe function which takes a png_ptr argument and raises  | 
495  |  |        * a png_error if the ttime argument is invalid and the call to gmtime  | 
496  |  |        * fails as a consequence.  | 
497  |  |        */  | 
498  | 0  |       memset(ptime, 0, sizeof(*ptime));  | 
499  | 0  |       return;  | 
500  | 0  |    }  | 
501  |  |  | 
502  | 0  |    png_convert_from_struct_tm(ptime, tbuf);  | 
503  | 0  | }  | 
504  |  | #endif  | 
505  |  |  | 
506  |  | /* Initialize png_ptr structure, and allocate any memory needed */  | 
507  |  | PNG_FUNCTION(png_structp,PNGAPI  | 
508  |  | png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,  | 
509  |  |     png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)  | 
510  | 0  | { | 
511  |  | #ifndef PNG_USER_MEM_SUPPORTED  | 
512  |  |    png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,  | 
513  |  |        error_fn, warn_fn, NULL, NULL, NULL);  | 
514  |  | #else  | 
515  | 0  |    return png_create_write_struct_2(user_png_ver, error_ptr, error_fn,  | 
516  | 0  |        warn_fn, NULL, NULL, NULL);  | 
517  | 0  | }  | 
518  |  |  | 
519  |  | /* Alternate initialize png_ptr structure, and allocate any memory needed */  | 
520  |  | PNG_FUNCTION(png_structp,PNGAPI  | 
521  |  | png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,  | 
522  |  |     png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,  | 
523  |  |     png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)  | 
524  | 0  | { | 
525  | 0  |    png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,  | 
526  | 0  |        error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);  | 
527  | 0  | #endif /* USER_MEM */  | 
528  | 0  |    if (png_ptr != NULL)  | 
529  | 0  |    { | 
530  |  |       /* Set the zlib control values to defaults; they can be overridden by the  | 
531  |  |        * application after the struct has been created.  | 
532  |  |        */  | 
533  | 0  |       png_ptr->zbuffer_size = PNG_ZBUF_SIZE;  | 
534  |  |  | 
535  |  |       /* The 'zlib_strategy' setting is irrelevant because png_default_claim in  | 
536  |  |        * pngwutil.c defaults it according to whether or not filters will be  | 
537  |  |        * used, and ignores this setting.  | 
538  |  |        */  | 
539  | 0  |       png_ptr->zlib_strategy = PNG_Z_DEFAULT_STRATEGY;  | 
540  | 0  |       png_ptr->zlib_level = PNG_Z_DEFAULT_COMPRESSION;  | 
541  | 0  |       png_ptr->zlib_mem_level = 8;  | 
542  | 0  |       png_ptr->zlib_window_bits = 15;  | 
543  | 0  |       png_ptr->zlib_method = 8;  | 
544  |  | 
  | 
545  | 0  | #ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED  | 
546  | 0  |       png_ptr->zlib_text_strategy = PNG_TEXT_Z_DEFAULT_STRATEGY;  | 
547  | 0  |       png_ptr->zlib_text_level = PNG_TEXT_Z_DEFAULT_COMPRESSION;  | 
548  | 0  |       png_ptr->zlib_text_mem_level = 8;  | 
549  | 0  |       png_ptr->zlib_text_window_bits = 15;  | 
550  | 0  |       png_ptr->zlib_text_method = 8;  | 
551  | 0  | #endif /* WRITE_COMPRESSED_TEXT */  | 
552  |  |  | 
553  |  |       /* This is a highly dubious configuration option; by default it is off,  | 
554  |  |        * but it may be appropriate for private builds that are testing  | 
555  |  |        * extensions not conformant to the current specification, or of  | 
556  |  |        * applications that must not fail to write at all costs!  | 
557  |  |        */  | 
558  |  | #ifdef PNG_BENIGN_WRITE_ERRORS_SUPPORTED  | 
559  |  |       /* In stable builds only warn if an application error can be completely  | 
560  |  |        * handled.  | 
561  |  |        */  | 
562  |  |       png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;  | 
563  |  | #endif  | 
564  |  |  | 
565  |  |       /* App warnings are warnings in release (or release candidate) builds but  | 
566  |  |        * are errors during development.  | 
567  |  |        */  | 
568  | 0  | #if PNG_RELEASE_BUILD  | 
569  | 0  |       png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;  | 
570  | 0  | #endif  | 
571  |  |  | 
572  |  |       /* TODO: delay this, it can be done in png_init_io() (if the app doesn't  | 
573  |  |        * do it itself) avoiding setting the default function if it is not  | 
574  |  |        * required.  | 
575  |  |        */  | 
576  | 0  |       png_set_write_fn(png_ptr, NULL, NULL, NULL);  | 
577  | 0  |    }  | 
578  |  | 
  | 
579  | 0  |    return png_ptr;  | 
580  | 0  | }  | 
581  |  |  | 
582  |  |  | 
583  |  | /* Write a few rows of image data.  If the image is interlaced,  | 
584  |  |  * either you will have to write the 7 sub images, or, if you  | 
585  |  |  * have called png_set_interlace_handling(), you will have to  | 
586  |  |  * "write" the image seven times.  | 
587  |  |  */  | 
588  |  | void PNGAPI  | 
589  |  | png_write_rows(png_structrp png_ptr, png_bytepp row,  | 
590  |  |     png_uint_32 num_rows)  | 
591  | 0  | { | 
592  | 0  |    png_uint_32 i; /* row counter */  | 
593  | 0  |    png_bytepp rp; /* row pointer */  | 
594  |  | 
  | 
595  | 0  |    png_debug(1, "in png_write_rows");  | 
596  |  | 
  | 
597  | 0  |    if (png_ptr == NULL)  | 
598  | 0  |       return;  | 
599  |  |  | 
600  |  |    /* Loop through the rows */  | 
601  | 0  |    for (i = 0, rp = row; i < num_rows; i++, rp++)  | 
602  | 0  |    { | 
603  | 0  |       png_write_row(png_ptr, *rp);  | 
604  | 0  |    }  | 
605  | 0  | }  | 
606  |  |  | 
607  |  | /* Write the image.  You only need to call this function once, even  | 
608  |  |  * if you are writing an interlaced image.  | 
609  |  |  */  | 
610  |  | void PNGAPI  | 
611  |  | png_write_image(png_structrp png_ptr, png_bytepp image)  | 
612  | 0  | { | 
613  | 0  |    png_uint_32 i; /* row index */  | 
614  | 0  |    int pass, num_pass; /* pass variables */  | 
615  | 0  |    png_bytepp rp; /* points to current row */  | 
616  |  | 
  | 
617  | 0  |    if (png_ptr == NULL)  | 
618  | 0  |       return;  | 
619  |  |  | 
620  | 0  |    png_debug(1, "in png_write_image");  | 
621  |  | 
  | 
622  | 0  | #ifdef PNG_WRITE_INTERLACING_SUPPORTED  | 
623  |  |    /* Initialize interlace handling.  If image is not interlaced,  | 
624  |  |     * this will set pass to 1  | 
625  |  |     */  | 
626  | 0  |    num_pass = png_set_interlace_handling(png_ptr);  | 
627  |  | #else  | 
628  |  |    num_pass = 1;  | 
629  |  | #endif  | 
630  |  |    /* Loop through passes */  | 
631  | 0  |    for (pass = 0; pass < num_pass; pass++)  | 
632  | 0  |    { | 
633  |  |       /* Loop through image */  | 
634  | 0  |       for (i = 0, rp = image; i < png_ptr->height; i++, rp++)  | 
635  | 0  |       { | 
636  | 0  |          png_write_row(png_ptr, *rp);  | 
637  | 0  |       }  | 
638  | 0  |    }  | 
639  | 0  | }  | 
640  |  |  | 
641  |  | #ifdef PNG_MNG_FEATURES_SUPPORTED  | 
642  |  | /* Performs intrapixel differencing  */  | 
643  |  | static void  | 
644  |  | png_do_write_intrapixel(png_row_infop row_info, png_bytep row)  | 
645  | 0  | { | 
646  | 0  |    png_debug(1, "in png_do_write_intrapixel");  | 
647  |  | 
  | 
648  | 0  |    if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)  | 
649  | 0  |    { | 
650  | 0  |       int bytes_per_pixel;  | 
651  | 0  |       png_uint_32 row_width = row_info->width;  | 
652  | 0  |       if (row_info->bit_depth == 8)  | 
653  | 0  |       { | 
654  | 0  |          png_bytep rp;  | 
655  | 0  |          png_uint_32 i;  | 
656  |  | 
  | 
657  | 0  |          if (row_info->color_type == PNG_COLOR_TYPE_RGB)  | 
658  | 0  |             bytes_per_pixel = 3;  | 
659  |  |  | 
660  | 0  |          else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)  | 
661  | 0  |             bytes_per_pixel = 4;  | 
662  |  |  | 
663  | 0  |          else  | 
664  | 0  |             return;  | 
665  |  |  | 
666  | 0  |          for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)  | 
667  | 0  |          { | 
668  | 0  |             *(rp)     = (png_byte)(*rp       - *(rp + 1));  | 
669  | 0  |             *(rp + 2) = (png_byte)(*(rp + 2) - *(rp + 1));  | 
670  | 0  |          }  | 
671  | 0  |       }  | 
672  |  |  | 
673  | 0  | #ifdef PNG_WRITE_16BIT_SUPPORTED  | 
674  | 0  |       else if (row_info->bit_depth == 16)  | 
675  | 0  |       { | 
676  | 0  |          png_bytep rp;  | 
677  | 0  |          png_uint_32 i;  | 
678  |  | 
  | 
679  | 0  |          if (row_info->color_type == PNG_COLOR_TYPE_RGB)  | 
680  | 0  |             bytes_per_pixel = 6;  | 
681  |  |  | 
682  | 0  |          else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)  | 
683  | 0  |             bytes_per_pixel = 8;  | 
684  |  |  | 
685  | 0  |          else  | 
686  | 0  |             return;  | 
687  |  |  | 
688  | 0  |          for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)  | 
689  | 0  |          { | 
690  | 0  |             png_uint_32 s0   = (png_uint_32)(*(rp    ) << 8) | *(rp + 1);  | 
691  | 0  |             png_uint_32 s1   = (png_uint_32)(*(rp + 2) << 8) | *(rp + 3);  | 
692  | 0  |             png_uint_32 s2   = (png_uint_32)(*(rp + 4) << 8) | *(rp + 5);  | 
693  | 0  |             png_uint_32 red  = (png_uint_32)((s0 - s1) & 0xffffL);  | 
694  | 0  |             png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);  | 
695  | 0  |             *(rp    ) = (png_byte)(red >> 8);  | 
696  | 0  |             *(rp + 1) = (png_byte)red;  | 
697  | 0  |             *(rp + 4) = (png_byte)(blue >> 8);  | 
698  | 0  |             *(rp + 5) = (png_byte)blue;  | 
699  | 0  |          }  | 
700  | 0  |       }  | 
701  | 0  | #endif /* WRITE_16BIT */  | 
702  | 0  |    }  | 
703  | 0  | }  | 
704  |  | #endif /* MNG_FEATURES */  | 
705  |  |  | 
706  |  | /* Called by user to write a row of image data */  | 
707  |  | void PNGAPI  | 
708  |  | png_write_row(png_structrp png_ptr, png_const_bytep row)  | 
709  | 0  | { | 
710  |  |    /* 1.5.6: moved from png_struct to be a local structure: */  | 
711  | 0  |    png_row_info row_info;  | 
712  |  | 
  | 
713  | 0  |    if (png_ptr == NULL)  | 
714  | 0  |       return;  | 
715  |  |  | 
716  | 0  |    png_debug2(1, "in png_write_row (row %u, pass %d)",  | 
717  | 0  |        png_ptr->row_number, png_ptr->pass);  | 
718  |  |  | 
719  |  |    /* Initialize transformations and other stuff if first time */  | 
720  | 0  |    if (png_ptr->row_number == 0 && png_ptr->pass == 0)  | 
721  | 0  |    { | 
722  |  |       /* Make sure we wrote the header info */  | 
723  | 0  |       if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0)  | 
724  | 0  |          png_error(png_ptr,  | 
725  | 0  |              "png_write_info was never called before png_write_row");  | 
726  |  |  | 
727  |  |       /* Check for transforms that have been set but were defined out */  | 
728  |  | #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)  | 
729  |  |       if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)  | 
730  |  |          png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");  | 
731  |  | #endif  | 
732  |  |  | 
733  |  | #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)  | 
734  |  |       if ((png_ptr->transformations & PNG_FILLER) != 0)  | 
735  |  |          png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");  | 
736  |  | #endif  | 
737  |  | #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \  | 
738  |  |     defined(PNG_READ_PACKSWAP_SUPPORTED)  | 
739  |  |       if ((png_ptr->transformations & PNG_PACKSWAP) != 0)  | 
740  |  |          png_warning(png_ptr,  | 
741  |  |              "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");  | 
742  |  | #endif  | 
743  |  |  | 
744  |  | #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)  | 
745  |  |       if ((png_ptr->transformations & PNG_PACK) != 0)  | 
746  |  |          png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");  | 
747  |  | #endif  | 
748  |  |  | 
749  |  | #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)  | 
750  |  |       if ((png_ptr->transformations & PNG_SHIFT) != 0)  | 
751  |  |          png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");  | 
752  |  | #endif  | 
753  |  |  | 
754  |  | #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)  | 
755  |  |       if ((png_ptr->transformations & PNG_BGR) != 0)  | 
756  |  |          png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");  | 
757  |  | #endif  | 
758  |  |  | 
759  |  | #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)  | 
760  |  |       if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)  | 
761  |  |          png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");  | 
762  |  | #endif  | 
763  |  |  | 
764  | 0  |       png_write_start_row(png_ptr);  | 
765  | 0  |    }  | 
766  |  |  | 
767  | 0  | #ifdef PNG_WRITE_INTERLACING_SUPPORTED  | 
768  |  |    /* If interlaced and not interested in row, return */  | 
769  | 0  |    if (png_ptr->interlaced != 0 &&  | 
770  | 0  |        (png_ptr->transformations & PNG_INTERLACE) != 0)  | 
771  | 0  |    { | 
772  | 0  |       switch (png_ptr->pass)  | 
773  | 0  |       { | 
774  | 0  |          case 0:  | 
775  | 0  |             if ((png_ptr->row_number & 0x07) != 0)  | 
776  | 0  |             { | 
777  | 0  |                png_write_finish_row(png_ptr);  | 
778  | 0  |                return;  | 
779  | 0  |             }  | 
780  | 0  |             break;  | 
781  |  |  | 
782  | 0  |          case 1:  | 
783  | 0  |             if ((png_ptr->row_number & 0x07) != 0 || png_ptr->width < 5)  | 
784  | 0  |             { | 
785  | 0  |                png_write_finish_row(png_ptr);  | 
786  | 0  |                return;  | 
787  | 0  |             }  | 
788  | 0  |             break;  | 
789  |  |  | 
790  | 0  |          case 2:  | 
791  | 0  |             if ((png_ptr->row_number & 0x07) != 4)  | 
792  | 0  |             { | 
793  | 0  |                png_write_finish_row(png_ptr);  | 
794  | 0  |                return;  | 
795  | 0  |             }  | 
796  | 0  |             break;  | 
797  |  |  | 
798  | 0  |          case 3:  | 
799  | 0  |             if ((png_ptr->row_number & 0x03) != 0 || png_ptr->width < 3)  | 
800  | 0  |             { | 
801  | 0  |                png_write_finish_row(png_ptr);  | 
802  | 0  |                return;  | 
803  | 0  |             }  | 
804  | 0  |             break;  | 
805  |  |  | 
806  | 0  |          case 4:  | 
807  | 0  |             if ((png_ptr->row_number & 0x03) != 2)  | 
808  | 0  |             { | 
809  | 0  |                png_write_finish_row(png_ptr);  | 
810  | 0  |                return;  | 
811  | 0  |             }  | 
812  | 0  |             break;  | 
813  |  |  | 
814  | 0  |          case 5:  | 
815  | 0  |             if ((png_ptr->row_number & 0x01) != 0 || png_ptr->width < 2)  | 
816  | 0  |             { | 
817  | 0  |                png_write_finish_row(png_ptr);  | 
818  | 0  |                return;  | 
819  | 0  |             }  | 
820  | 0  |             break;  | 
821  |  |  | 
822  | 0  |          case 6:  | 
823  | 0  |             if ((png_ptr->row_number & 0x01) == 0)  | 
824  | 0  |             { | 
825  | 0  |                png_write_finish_row(png_ptr);  | 
826  | 0  |                return;  | 
827  | 0  |             }  | 
828  | 0  |             break;  | 
829  |  |  | 
830  | 0  |          default: /* error: ignore it */  | 
831  | 0  |             break;  | 
832  | 0  |       }  | 
833  | 0  |    }  | 
834  | 0  | #endif  | 
835  |  |  | 
836  |  |    /* Set up row info for transformations */  | 
837  | 0  |    row_info.color_type = png_ptr->color_type;  | 
838  | 0  |    row_info.width = png_ptr->usr_width;  | 
839  | 0  |    row_info.channels = png_ptr->usr_channels;  | 
840  | 0  |    row_info.bit_depth = png_ptr->usr_bit_depth;  | 
841  | 0  |    row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels);  | 
842  | 0  |    row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);  | 
843  |  | 
  | 
844  | 0  |    png_debug1(3, "row_info->color_type = %d", row_info.color_type);  | 
845  | 0  |    png_debug1(3, "row_info->width = %u", row_info.width);  | 
846  | 0  |    png_debug1(3, "row_info->channels = %d", row_info.channels);  | 
847  | 0  |    png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth);  | 
848  | 0  |    png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth);  | 
849  | 0  |    png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes);  | 
850  |  |  | 
851  |  |    /* Copy user's row into buffer, leaving room for filter byte. */  | 
852  | 0  |    memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);  | 
853  |  | 
  | 
854  | 0  | #ifdef PNG_WRITE_INTERLACING_SUPPORTED  | 
855  |  |    /* Handle interlacing */  | 
856  | 0  |    if (png_ptr->interlaced && png_ptr->pass < 6 &&  | 
857  | 0  |        (png_ptr->transformations & PNG_INTERLACE) != 0)  | 
858  | 0  |    { | 
859  | 0  |       png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass);  | 
860  |  |       /* This should always get caught above, but still ... */  | 
861  | 0  |       if (row_info.width == 0)  | 
862  | 0  |       { | 
863  | 0  |          png_write_finish_row(png_ptr);  | 
864  | 0  |          return;  | 
865  | 0  |       }  | 
866  | 0  |    }  | 
867  | 0  | #endif  | 
868  |  |  | 
869  | 0  | #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED  | 
870  |  |    /* Handle other transformations */  | 
871  | 0  |    if (png_ptr->transformations != 0)  | 
872  | 0  |       png_do_write_transformations(png_ptr, &row_info);  | 
873  | 0  | #endif  | 
874  |  |  | 
875  |  |    /* At this point the row_info pixel depth must match the 'transformed' depth,  | 
876  |  |     * which is also the output depth.  | 
877  |  |     */  | 
878  | 0  |    if (row_info.pixel_depth != png_ptr->pixel_depth ||  | 
879  | 0  |        row_info.pixel_depth != png_ptr->transformed_pixel_depth)  | 
880  | 0  |       png_error(png_ptr, "internal write transform logic error");  | 
881  |  |  | 
882  | 0  | #ifdef PNG_MNG_FEATURES_SUPPORTED  | 
883  |  |    /* Write filter_method 64 (intrapixel differencing) only if  | 
884  |  |     * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and  | 
885  |  |     * 2. Libpng did not write a PNG signature (this filter_method is only  | 
886  |  |     *    used in PNG datastreams that are embedded in MNG datastreams) and  | 
887  |  |     * 3. The application called png_permit_mng_features with a mask that  | 
888  |  |     *    included PNG_FLAG_MNG_FILTER_64 and  | 
889  |  |     * 4. The filter_method is 64 and  | 
890  |  |     * 5. The color_type is RGB or RGBA  | 
891  |  |     */  | 
892  | 0  |    if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&  | 
893  | 0  |        (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))  | 
894  | 0  |    { | 
895  |  |       /* Intrapixel differencing */  | 
896  | 0  |       png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1);  | 
897  | 0  |    }  | 
898  | 0  | #endif  | 
899  |  |  | 
900  |  | /* Added at libpng-1.5.10 */  | 
901  | 0  | #ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED  | 
902  |  |    /* Check for out-of-range palette index */  | 
903  | 0  |    if (row_info.color_type == PNG_COLOR_TYPE_PALETTE &&  | 
904  | 0  |        png_ptr->num_palette_max >= 0)  | 
905  | 0  |       png_do_check_palette_indexes(png_ptr, &row_info);  | 
906  | 0  | #endif  | 
907  |  |  | 
908  |  |    /* Find a filter if necessary, filter the row and write it out. */  | 
909  | 0  |    png_write_find_filter(png_ptr, &row_info);  | 
910  |  | 
  | 
911  | 0  |    if (png_ptr->write_row_fn != NULL)  | 
912  | 0  |       (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);  | 
913  | 0  | }  | 
914  |  |  | 
915  |  | #ifdef PNG_WRITE_FLUSH_SUPPORTED  | 
916  |  | /* Set the automatic flush interval or 0 to turn flushing off */  | 
917  |  | void PNGAPI  | 
918  |  | png_set_flush(png_structrp png_ptr, int nrows)  | 
919  | 0  | { | 
920  | 0  |    png_debug(1, "in png_set_flush");  | 
921  |  | 
  | 
922  | 0  |    if (png_ptr == NULL)  | 
923  | 0  |       return;  | 
924  |  |  | 
925  | 0  |    png_ptr->flush_dist = (nrows < 0 ? 0 : (png_uint_32)nrows);  | 
926  | 0  | }  | 
927  |  |  | 
928  |  | /* Flush the current output buffers now */  | 
929  |  | void PNGAPI  | 
930  |  | png_write_flush(png_structrp png_ptr)  | 
931  | 0  | { | 
932  | 0  |    png_debug(1, "in png_write_flush");  | 
933  |  | 
  | 
934  | 0  |    if (png_ptr == NULL)  | 
935  | 0  |       return;  | 
936  |  |  | 
937  |  |    /* We have already written out all of the data */  | 
938  | 0  |    if (png_ptr->row_number >= png_ptr->num_rows)  | 
939  | 0  |       return;  | 
940  |  |  | 
941  | 0  |    png_compress_IDAT(png_ptr, NULL, 0, Z_SYNC_FLUSH);  | 
942  | 0  |    png_ptr->flush_rows = 0;  | 
943  | 0  |    png_flush(png_ptr);  | 
944  | 0  | }  | 
945  |  | #endif /* WRITE_FLUSH */  | 
946  |  |  | 
947  |  | /* Free any memory used in png_ptr struct without freeing the struct itself. */  | 
948  |  | static void  | 
949  |  | png_write_destroy(png_structrp png_ptr)  | 
950  | 0  | { | 
951  | 0  |    png_debug(1, "in png_write_destroy");  | 
952  |  |  | 
953  |  |    /* Free any memory zlib uses */  | 
954  | 0  |    if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)  | 
955  | 0  |       deflateEnd(&png_ptr->zstream);  | 
956  |  |  | 
957  |  |    /* Free our memory.  png_free checks NULL for us. */  | 
958  | 0  |    png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);  | 
959  | 0  |    png_free(png_ptr, png_ptr->row_buf);  | 
960  | 0  |    png_ptr->row_buf = NULL;  | 
961  | 0  | #ifdef PNG_WRITE_FILTER_SUPPORTED  | 
962  | 0  |    png_free(png_ptr, png_ptr->prev_row);  | 
963  | 0  |    png_free(png_ptr, png_ptr->try_row);  | 
964  | 0  |    png_free(png_ptr, png_ptr->tst_row);  | 
965  | 0  |    png_ptr->prev_row = NULL;  | 
966  | 0  |    png_ptr->try_row = NULL;  | 
967  | 0  |    png_ptr->tst_row = NULL;  | 
968  | 0  | #endif  | 
969  |  | 
  | 
970  | 0  | #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED  | 
971  | 0  |    png_free(png_ptr, png_ptr->chunk_list);  | 
972  | 0  |    png_ptr->chunk_list = NULL;  | 
973  | 0  | #endif  | 
974  |  |  | 
975  |  |    /* The error handling and memory handling information is left intact at this  | 
976  |  |     * point: the jmp_buf may still have to be freed.  See png_destroy_png_struct  | 
977  |  |     * for how this happens.  | 
978  |  |     */  | 
979  | 0  | }  | 
980  |  |  | 
981  |  | /* Free all memory used by the write.  | 
982  |  |  * In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for  | 
983  |  |  * *png_ptr_ptr.  Prior to 1.6.0 it would accept such a value and it would free  | 
984  |  |  * the passed in info_structs but it would quietly fail to free any of the data  | 
985  |  |  * inside them.  In 1.6.0 it quietly does nothing (it has to be quiet because it  | 
986  |  |  * has no png_ptr.)  | 
987  |  |  */  | 
988  |  | void PNGAPI  | 
989  |  | png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)  | 
990  | 0  | { | 
991  | 0  |    png_debug(1, "in png_destroy_write_struct");  | 
992  |  | 
  | 
993  | 0  |    if (png_ptr_ptr != NULL)  | 
994  | 0  |    { | 
995  | 0  |       png_structrp png_ptr = *png_ptr_ptr;  | 
996  |  | 
  | 
997  | 0  |       if (png_ptr != NULL) /* added in libpng 1.6.0 */  | 
998  | 0  |       { | 
999  | 0  |          png_destroy_info_struct(png_ptr, info_ptr_ptr);  | 
1000  |  | 
  | 
1001  | 0  |          *png_ptr_ptr = NULL;  | 
1002  | 0  |          png_write_destroy(png_ptr);  | 
1003  | 0  |          png_destroy_png_struct(png_ptr);  | 
1004  | 0  |       }  | 
1005  | 0  |    }  | 
1006  | 0  | }  | 
1007  |  |  | 
1008  |  | /* Allow the application to select one or more row filters to use. */  | 
1009  |  | void PNGAPI  | 
1010  |  | png_set_filter(png_structrp png_ptr, int method, int filters)  | 
1011  | 0  | { | 
1012  | 0  |    png_debug(1, "in png_set_filter");  | 
1013  |  | 
  | 
1014  | 0  |    if (png_ptr == NULL)  | 
1015  | 0  |       return;  | 
1016  |  |  | 
1017  | 0  | #ifdef PNG_MNG_FEATURES_SUPPORTED  | 
1018  | 0  |    if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&  | 
1019  | 0  |        (method == PNG_INTRAPIXEL_DIFFERENCING))  | 
1020  | 0  |       method = PNG_FILTER_TYPE_BASE;  | 
1021  |  | 
  | 
1022  | 0  | #endif  | 
1023  | 0  |    if (method == PNG_FILTER_TYPE_BASE)  | 
1024  | 0  |    { | 
1025  | 0  |       switch (filters & (PNG_ALL_FILTERS | 0x07))  | 
1026  | 0  |       { | 
1027  | 0  | #ifdef PNG_WRITE_FILTER_SUPPORTED  | 
1028  | 0  |          case 5:  | 
1029  | 0  |          case 6:  | 
1030  | 0  |          case 7: png_app_error(png_ptr, "Unknown row filter for method 0");  | 
1031  | 0  | #endif /* WRITE_FILTER */  | 
1032  |  |             /* FALLTHROUGH */  | 
1033  | 0  |          case PNG_FILTER_VALUE_NONE:  | 
1034  | 0  |             png_ptr->do_filter = PNG_FILTER_NONE; break;  | 
1035  |  |  | 
1036  | 0  | #ifdef PNG_WRITE_FILTER_SUPPORTED  | 
1037  | 0  |          case PNG_FILTER_VALUE_SUB:  | 
1038  | 0  |             png_ptr->do_filter = PNG_FILTER_SUB; break;  | 
1039  |  |  | 
1040  | 0  |          case PNG_FILTER_VALUE_UP:  | 
1041  | 0  |             png_ptr->do_filter = PNG_FILTER_UP; break;  | 
1042  |  |  | 
1043  | 0  |          case PNG_FILTER_VALUE_AVG:  | 
1044  | 0  |             png_ptr->do_filter = PNG_FILTER_AVG; break;  | 
1045  |  |  | 
1046  | 0  |          case PNG_FILTER_VALUE_PAETH:  | 
1047  | 0  |             png_ptr->do_filter = PNG_FILTER_PAETH; break;  | 
1048  |  |  | 
1049  | 0  |          default:  | 
1050  | 0  |             png_ptr->do_filter = (png_byte)filters; break;  | 
1051  |  | #else  | 
1052  |  |          default:  | 
1053  |  |             png_app_error(png_ptr, "Unknown row filter for method 0");  | 
1054  |  | #endif /* WRITE_FILTER */  | 
1055  | 0  |       }  | 
1056  |  |  | 
1057  | 0  | #ifdef PNG_WRITE_FILTER_SUPPORTED  | 
1058  |  |       /* If we have allocated the row_buf, this means we have already started  | 
1059  |  |        * with the image and we should have allocated all of the filter buffers  | 
1060  |  |        * that have been selected.  If prev_row isn't already allocated, then  | 
1061  |  |        * it is too late to start using the filters that need it, since we  | 
1062  |  |        * will be missing the data in the previous row.  If an application  | 
1063  |  |        * wants to start and stop using particular filters during compression,  | 
1064  |  |        * it should start out with all of the filters, and then remove them  | 
1065  |  |        * or add them back after the start of compression.  | 
1066  |  |        *  | 
1067  |  |        * NOTE: this is a nasty constraint on the code, because it means that the  | 
1068  |  |        * prev_row buffer must be maintained even if there are currently no  | 
1069  |  |        * 'prev_row' requiring filters active.  | 
1070  |  |        */  | 
1071  | 0  |       if (png_ptr->row_buf != NULL)  | 
1072  | 0  |       { | 
1073  | 0  |          int num_filters;  | 
1074  | 0  |          png_alloc_size_t buf_size;  | 
1075  |  |  | 
1076  |  |          /* Repeat the checks in png_write_start_row; 1 pixel high or wide  | 
1077  |  |           * images cannot benefit from certain filters.  If this isn't done here  | 
1078  |  |           * the check below will fire on 1 pixel high images.  | 
1079  |  |           */  | 
1080  | 0  |          if (png_ptr->height == 1)  | 
1081  | 0  |             filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);  | 
1082  |  | 
  | 
1083  | 0  |          if (png_ptr->width == 1)  | 
1084  | 0  |             filters &= ~(PNG_FILTER_SUB|PNG_FILTER_AVG|PNG_FILTER_PAETH);  | 
1085  |  | 
  | 
1086  | 0  |          if ((filters & (PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH)) != 0  | 
1087  | 0  |             && png_ptr->prev_row == NULL)  | 
1088  | 0  |          { | 
1089  |  |             /* This is the error case, however it is benign - the previous row  | 
1090  |  |              * is not available so the filter can't be used.  Just warn here.  | 
1091  |  |              */  | 
1092  | 0  |             png_app_warning(png_ptr,  | 
1093  | 0  |                 "png_set_filter: UP/AVG/PAETH cannot be added after start");  | 
1094  | 0  |             filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);  | 
1095  | 0  |          }  | 
1096  |  | 
  | 
1097  | 0  |          num_filters = 0;  | 
1098  |  | 
  | 
1099  | 0  |          if (filters & PNG_FILTER_SUB)  | 
1100  | 0  |             num_filters++;  | 
1101  |  | 
  | 
1102  | 0  |          if (filters & PNG_FILTER_UP)  | 
1103  | 0  |             num_filters++;  | 
1104  |  | 
  | 
1105  | 0  |          if (filters & PNG_FILTER_AVG)  | 
1106  | 0  |             num_filters++;  | 
1107  |  | 
  | 
1108  | 0  |          if (filters & PNG_FILTER_PAETH)  | 
1109  | 0  |             num_filters++;  | 
1110  |  |  | 
1111  |  |          /* Allocate needed row buffers if they have not already been  | 
1112  |  |           * allocated.  | 
1113  |  |           */  | 
1114  | 0  |          buf_size = PNG_ROWBYTES(png_ptr->usr_channels * png_ptr->usr_bit_depth,  | 
1115  | 0  |              png_ptr->width) + 1;  | 
1116  |  | 
  | 
1117  | 0  |          if (png_ptr->try_row == NULL)  | 
1118  | 0  |             png_ptr->try_row = png_voidcast(png_bytep,  | 
1119  | 0  |                 png_malloc(png_ptr, buf_size));  | 
1120  |  | 
  | 
1121  | 0  |          if (num_filters > 1)  | 
1122  | 0  |          { | 
1123  | 0  |             if (png_ptr->tst_row == NULL)  | 
1124  | 0  |                png_ptr->tst_row = png_voidcast(png_bytep,  | 
1125  | 0  |                    png_malloc(png_ptr, buf_size));  | 
1126  | 0  |          }  | 
1127  | 0  |       }  | 
1128  | 0  |       png_ptr->do_filter = (png_byte)filters;  | 
1129  | 0  | #endif  | 
1130  | 0  |    }  | 
1131  | 0  |    else  | 
1132  | 0  |       png_error(png_ptr, "Unknown custom filter method");  | 
1133  | 0  | }  | 
1134  |  |  | 
1135  |  | #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* DEPRECATED */  | 
1136  |  | /* Provide floating and fixed point APIs */  | 
1137  |  | #ifdef PNG_FLOATING_POINT_SUPPORTED  | 
1138  |  | void PNGAPI  | 
1139  |  | png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method,  | 
1140  |  |     int num_weights, png_const_doublep filter_weights,  | 
1141  |  |     png_const_doublep filter_costs)  | 
1142  | 0  | { | 
1143  | 0  |    PNG_UNUSED(png_ptr)  | 
1144  | 0  |    PNG_UNUSED(heuristic_method)  | 
1145  | 0  |    PNG_UNUSED(num_weights)  | 
1146  | 0  |    PNG_UNUSED(filter_weights)  | 
1147  | 0  |    PNG_UNUSED(filter_costs)  | 
1148  | 0  | }  | 
1149  |  | #endif /* FLOATING_POINT */  | 
1150  |  |  | 
1151  |  | #ifdef PNG_FIXED_POINT_SUPPORTED  | 
1152  |  | void PNGAPI  | 
1153  |  | png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method,  | 
1154  |  |     int num_weights, png_const_fixed_point_p filter_weights,  | 
1155  |  |     png_const_fixed_point_p filter_costs)  | 
1156  | 0  | { | 
1157  | 0  |    PNG_UNUSED(png_ptr)  | 
1158  | 0  |    PNG_UNUSED(heuristic_method)  | 
1159  | 0  |    PNG_UNUSED(num_weights)  | 
1160  | 0  |    PNG_UNUSED(filter_weights)  | 
1161  | 0  |    PNG_UNUSED(filter_costs)  | 
1162  | 0  | }  | 
1163  |  | #endif /* FIXED_POINT */  | 
1164  |  | #endif /* WRITE_WEIGHTED_FILTER */  | 
1165  |  |  | 
1166  |  | #ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED  | 
1167  |  | void PNGAPI  | 
1168  |  | png_set_compression_level(png_structrp png_ptr, int level)  | 
1169  | 0  | { | 
1170  | 0  |    png_debug(1, "in png_set_compression_level");  | 
1171  |  | 
  | 
1172  | 0  |    if (png_ptr == NULL)  | 
1173  | 0  |       return;  | 
1174  |  |  | 
1175  | 0  |    png_ptr->zlib_level = level;  | 
1176  | 0  | }  | 
1177  |  |  | 
1178  |  | void PNGAPI  | 
1179  |  | png_set_compression_mem_level(png_structrp png_ptr, int mem_level)  | 
1180  | 0  | { | 
1181  | 0  |    png_debug(1, "in png_set_compression_mem_level");  | 
1182  |  | 
  | 
1183  | 0  |    if (png_ptr == NULL)  | 
1184  | 0  |       return;  | 
1185  |  |  | 
1186  | 0  |    png_ptr->zlib_mem_level = mem_level;  | 
1187  | 0  | }  | 
1188  |  |  | 
1189  |  | void PNGAPI  | 
1190  |  | png_set_compression_strategy(png_structrp png_ptr, int strategy)  | 
1191  | 0  | { | 
1192  | 0  |    png_debug(1, "in png_set_compression_strategy");  | 
1193  |  | 
  | 
1194  | 0  |    if (png_ptr == NULL)  | 
1195  | 0  |       return;  | 
1196  |  |  | 
1197  |  |    /* The flag setting here prevents the libpng dynamic selection of strategy.  | 
1198  |  |     */  | 
1199  | 0  |    png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;  | 
1200  | 0  |    png_ptr->zlib_strategy = strategy;  | 
1201  | 0  | }  | 
1202  |  |  | 
1203  |  | /* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a  | 
1204  |  |  * smaller value of window_bits if it can do so safely.  | 
1205  |  |  */  | 
1206  |  | void PNGAPI  | 
1207  |  | png_set_compression_window_bits(png_structrp png_ptr, int window_bits)  | 
1208  | 0  | { | 
1209  | 0  |    if (png_ptr == NULL)  | 
1210  | 0  |       return;  | 
1211  |  |  | 
1212  |  |    /* Prior to 1.6.0 this would warn but then set the window_bits value. This  | 
1213  |  |     * meant that negative window bits values could be selected that would cause  | 
1214  |  |     * libpng to write a non-standard PNG file with raw deflate or gzip  | 
1215  |  |     * compressed IDAT or ancillary chunks.  Such files can be read and there is  | 
1216  |  |     * no warning on read, so this seems like a very bad idea.  | 
1217  |  |     */  | 
1218  | 0  |    if (window_bits > 15)  | 
1219  | 0  |    { | 
1220  | 0  |       png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");  | 
1221  | 0  |       window_bits = 15;  | 
1222  | 0  |    }  | 
1223  |  |  | 
1224  | 0  |    else if (window_bits < 8)  | 
1225  | 0  |    { | 
1226  | 0  |       png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");  | 
1227  | 0  |       window_bits = 8;  | 
1228  | 0  |    }  | 
1229  |  | 
  | 
1230  | 0  |    png_ptr->zlib_window_bits = window_bits;  | 
1231  | 0  | }  | 
1232  |  |  | 
1233  |  | void PNGAPI  | 
1234  |  | png_set_compression_method(png_structrp png_ptr, int method)  | 
1235  | 0  | { | 
1236  | 0  |    png_debug(1, "in png_set_compression_method");  | 
1237  |  | 
  | 
1238  | 0  |    if (png_ptr == NULL)  | 
1239  | 0  |       return;  | 
1240  |  |  | 
1241  |  |    /* This would produce an invalid PNG file if it worked, but it doesn't and  | 
1242  |  |     * deflate will fault it, so it is harmless to just warn here.  | 
1243  |  |     */  | 
1244  | 0  |    if (method != 8)  | 
1245  | 0  |       png_warning(png_ptr, "Only compression method 8 is supported by PNG");  | 
1246  |  | 
  | 
1247  | 0  |    png_ptr->zlib_method = method;  | 
1248  | 0  | }  | 
1249  |  | #endif /* WRITE_CUSTOMIZE_COMPRESSION */  | 
1250  |  |  | 
1251  |  | /* The following were added to libpng-1.5.4 */  | 
1252  |  | #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED  | 
1253  |  | void PNGAPI  | 
1254  |  | png_set_text_compression_level(png_structrp png_ptr, int level)  | 
1255  | 0  | { | 
1256  | 0  |    png_debug(1, "in png_set_text_compression_level");  | 
1257  |  | 
  | 
1258  | 0  |    if (png_ptr == NULL)  | 
1259  | 0  |       return;  | 
1260  |  |  | 
1261  | 0  |    png_ptr->zlib_text_level = level;  | 
1262  | 0  | }  | 
1263  |  |  | 
1264  |  | void PNGAPI  | 
1265  |  | png_set_text_compression_mem_level(png_structrp png_ptr, int mem_level)  | 
1266  | 0  | { | 
1267  | 0  |    png_debug(1, "in png_set_text_compression_mem_level");  | 
1268  |  | 
  | 
1269  | 0  |    if (png_ptr == NULL)  | 
1270  | 0  |       return;  | 
1271  |  |  | 
1272  | 0  |    png_ptr->zlib_text_mem_level = mem_level;  | 
1273  | 0  | }  | 
1274  |  |  | 
1275  |  | void PNGAPI  | 
1276  |  | png_set_text_compression_strategy(png_structrp png_ptr, int strategy)  | 
1277  | 0  | { | 
1278  | 0  |    png_debug(1, "in png_set_text_compression_strategy");  | 
1279  |  | 
  | 
1280  | 0  |    if (png_ptr == NULL)  | 
1281  | 0  |       return;  | 
1282  |  |  | 
1283  | 0  |    png_ptr->zlib_text_strategy = strategy;  | 
1284  | 0  | }  | 
1285  |  |  | 
1286  |  | /* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a  | 
1287  |  |  * smaller value of window_bits if it can do so safely.  | 
1288  |  |  */  | 
1289  |  | void PNGAPI  | 
1290  |  | png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits)  | 
1291  | 0  | { | 
1292  | 0  |    if (png_ptr == NULL)  | 
1293  | 0  |       return;  | 
1294  |  |  | 
1295  | 0  |    if (window_bits > 15)  | 
1296  | 0  |    { | 
1297  | 0  |       png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");  | 
1298  | 0  |       window_bits = 15;  | 
1299  | 0  |    }  | 
1300  |  |  | 
1301  | 0  |    else if (window_bits < 8)  | 
1302  | 0  |    { | 
1303  | 0  |       png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");  | 
1304  | 0  |       window_bits = 8;  | 
1305  | 0  |    }  | 
1306  |  | 
  | 
1307  | 0  |    png_ptr->zlib_text_window_bits = window_bits;  | 
1308  | 0  | }  | 
1309  |  |  | 
1310  |  | void PNGAPI  | 
1311  |  | png_set_text_compression_method(png_structrp png_ptr, int method)  | 
1312  | 0  | { | 
1313  | 0  |    png_debug(1, "in png_set_text_compression_method");  | 
1314  |  | 
  | 
1315  | 0  |    if (png_ptr == NULL)  | 
1316  | 0  |       return;  | 
1317  |  |  | 
1318  | 0  |    if (method != 8)  | 
1319  | 0  |       png_warning(png_ptr, "Only compression method 8 is supported by PNG");  | 
1320  |  | 
  | 
1321  | 0  |    png_ptr->zlib_text_method = method;  | 
1322  | 0  | }  | 
1323  |  | #endif /* WRITE_CUSTOMIZE_ZTXT_COMPRESSION */  | 
1324  |  | /* end of API added to libpng-1.5.4 */  | 
1325  |  |  | 
1326  |  | void PNGAPI  | 
1327  |  | png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn)  | 
1328  | 0  | { | 
1329  | 0  |    if (png_ptr == NULL)  | 
1330  | 0  |       return;  | 
1331  |  |  | 
1332  | 0  |    png_ptr->write_row_fn = write_row_fn;  | 
1333  | 0  | }  | 
1334  |  |  | 
1335  |  | #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED  | 
1336  |  | void PNGAPI  | 
1337  |  | png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr  | 
1338  |  |     write_user_transform_fn)  | 
1339  | 0  | { | 
1340  | 0  |    png_debug(1, "in png_set_write_user_transform_fn");  | 
1341  |  | 
  | 
1342  | 0  |    if (png_ptr == NULL)  | 
1343  | 0  |       return;  | 
1344  |  |  | 
1345  | 0  |    png_ptr->transformations |= PNG_USER_TRANSFORM;  | 
1346  | 0  |    png_ptr->write_user_transform_fn = write_user_transform_fn;  | 
1347  | 0  | }  | 
1348  |  | #endif  | 
1349  |  |  | 
1350  |  |  | 
1351  |  | #ifdef PNG_INFO_IMAGE_SUPPORTED  | 
1352  |  | void PNGAPI  | 
1353  |  | png_write_png(png_structrp png_ptr, png_inforp info_ptr,  | 
1354  |  |     int transforms, voidp params)  | 
1355  | 0  | { | 
1356  | 0  |    if (png_ptr == NULL || info_ptr == NULL)  | 
1357  | 0  |       return;  | 
1358  |  |  | 
1359  | 0  |    if ((info_ptr->valid & PNG_INFO_IDAT) == 0)  | 
1360  | 0  |    { | 
1361  | 0  |       png_app_error(png_ptr, "no rows for png_write_image to write");  | 
1362  | 0  |       return;  | 
1363  | 0  |    }  | 
1364  |  |  | 
1365  |  |    /* Write the file header information. */  | 
1366  | 0  |    png_write_info(png_ptr, info_ptr);  | 
1367  |  |  | 
1368  |  |    /* ------ these transformations don't touch the info structure ------- */  | 
1369  |  |  | 
1370  |  |    /* Invert monochrome pixels */  | 
1371  | 0  |    if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0)  | 
1372  | 0  | #ifdef PNG_WRITE_INVERT_SUPPORTED  | 
1373  | 0  |       png_set_invert_mono(png_ptr);  | 
1374  |  | #else  | 
1375  |  |       png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");  | 
1376  |  | #endif  | 
1377  |  |  | 
1378  |  |    /* Shift the pixels up to a legal bit depth and fill in  | 
1379  |  |     * as appropriate to correctly scale the image.  | 
1380  |  |     */  | 
1381  | 0  |    if ((transforms & PNG_TRANSFORM_SHIFT) != 0)  | 
1382  | 0  | #ifdef PNG_WRITE_SHIFT_SUPPORTED  | 
1383  | 0  |       if ((info_ptr->valid & PNG_INFO_sBIT) != 0)  | 
1384  | 0  |          png_set_shift(png_ptr, &info_ptr->sig_bit);  | 
1385  |  | #else  | 
1386  |  |       png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");  | 
1387  |  | #endif  | 
1388  |  |  | 
1389  |  |    /* Pack pixels into bytes */  | 
1390  | 0  |    if ((transforms & PNG_TRANSFORM_PACKING) != 0)  | 
1391  | 0  | #ifdef PNG_WRITE_PACK_SUPPORTED  | 
1392  | 0  |       png_set_packing(png_ptr);  | 
1393  |  | #else  | 
1394  |  |       png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");  | 
1395  |  | #endif  | 
1396  |  |  | 
1397  |  |    /* Swap location of alpha bytes from ARGB to RGBA */  | 
1398  | 0  |    if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0)  | 
1399  | 0  | #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED  | 
1400  | 0  |       png_set_swap_alpha(png_ptr);  | 
1401  |  | #else  | 
1402  |  |       png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");  | 
1403  |  | #endif  | 
1404  |  |  | 
1405  |  |    /* Remove a filler (X) from XRGB/RGBX/AG/GA into to convert it into  | 
1406  |  |     * RGB, note that the code expects the input color type to be G or RGB; no  | 
1407  |  |     * alpha channel.  | 
1408  |  |     */  | 
1409  | 0  |    if ((transforms & (PNG_TRANSFORM_STRIP_FILLER_AFTER|  | 
1410  | 0  |        PNG_TRANSFORM_STRIP_FILLER_BEFORE)) != 0)  | 
1411  | 0  |    { | 
1412  | 0  | #ifdef PNG_WRITE_FILLER_SUPPORTED  | 
1413  | 0  |       if ((transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER) != 0)  | 
1414  | 0  |       { | 
1415  | 0  |          if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0)  | 
1416  | 0  |             png_app_error(png_ptr,  | 
1417  | 0  |                 "PNG_TRANSFORM_STRIP_FILLER: BEFORE+AFTER not supported");  | 
1418  |  |  | 
1419  |  |          /* Continue if ignored - this is the pre-1.6.10 behavior */  | 
1420  | 0  |          png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);  | 
1421  | 0  |       }  | 
1422  |  |  | 
1423  | 0  |       else if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0)  | 
1424  | 0  |          png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);  | 
1425  |  | #else  | 
1426  |  |       png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_FILLER not supported");  | 
1427  |  | #endif  | 
1428  | 0  |    }  | 
1429  |  |  | 
1430  |  |    /* Flip BGR pixels to RGB */  | 
1431  | 0  |    if ((transforms & PNG_TRANSFORM_BGR) != 0)  | 
1432  | 0  | #ifdef PNG_WRITE_BGR_SUPPORTED  | 
1433  | 0  |       png_set_bgr(png_ptr);  | 
1434  |  | #else  | 
1435  |  |       png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");  | 
1436  |  | #endif  | 
1437  |  |  | 
1438  |  |    /* Swap bytes of 16-bit files to most significant byte first */  | 
1439  | 0  |    if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0)  | 
1440  | 0  | #ifdef PNG_WRITE_SWAP_SUPPORTED  | 
1441  | 0  |       png_set_swap(png_ptr);  | 
1442  |  | #else  | 
1443  |  |       png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");  | 
1444  |  | #endif  | 
1445  |  |  | 
1446  |  |    /* Swap bits of 1-bit, 2-bit, 4-bit packed pixel formats */  | 
1447  | 0  |    if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)  | 
1448  | 0  | #ifdef PNG_WRITE_PACKSWAP_SUPPORTED  | 
1449  | 0  |       png_set_packswap(png_ptr);  | 
1450  |  | #else  | 
1451  |  |       png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");  | 
1452  |  | #endif  | 
1453  |  |  | 
1454  |  |    /* Invert the alpha channel from opacity to transparency */  | 
1455  | 0  |    if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0)  | 
1456  | 0  | #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED  | 
1457  | 0  |       png_set_invert_alpha(png_ptr);  | 
1458  |  | #else  | 
1459  |  |       png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");  | 
1460  |  | #endif  | 
1461  |  |  | 
1462  |  |    /* ----------------------- end of transformations ------------------- */  | 
1463  |  |  | 
1464  |  |    /* Write the bits */  | 
1465  | 0  |    png_write_image(png_ptr, info_ptr->row_pointers);  | 
1466  |  |  | 
1467  |  |    /* It is REQUIRED to call this to finish writing the rest of the file */  | 
1468  | 0  |    png_write_end(png_ptr, info_ptr);  | 
1469  |  | 
  | 
1470  | 0  |    PNG_UNUSED(params)  | 
1471  | 0  | }  | 
1472  |  | #endif  | 
1473  |  |  | 
1474  |  |  | 
1475  |  | #ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED  | 
1476  |  | /* Initialize the write structure - general purpose utility. */  | 
1477  |  | static int  | 
1478  |  | png_image_write_init(png_imagep image)  | 
1479  | 0  | { | 
1480  | 0  |    png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image,  | 
1481  | 0  |        png_safe_error, png_safe_warning);  | 
1482  |  | 
  | 
1483  | 0  |    if (png_ptr != NULL)  | 
1484  | 0  |    { | 
1485  | 0  |       png_infop info_ptr = png_create_info_struct(png_ptr);  | 
1486  |  | 
  | 
1487  | 0  |       if (info_ptr != NULL)  | 
1488  | 0  |       { | 
1489  | 0  |          png_controlp control = png_voidcast(png_controlp,  | 
1490  | 0  |              png_malloc_warn(png_ptr, (sizeof *control)));  | 
1491  |  | 
  | 
1492  | 0  |          if (control != NULL)  | 
1493  | 0  |          { | 
1494  | 0  |             memset(control, 0, (sizeof *control));  | 
1495  |  | 
  | 
1496  | 0  |             control->png_ptr = png_ptr;  | 
1497  | 0  |             control->info_ptr = info_ptr;  | 
1498  | 0  |             control->for_write = 1;  | 
1499  |  | 
  | 
1500  | 0  |             image->opaque = control;  | 
1501  | 0  |             return 1;  | 
1502  | 0  |          }  | 
1503  |  |  | 
1504  |  |          /* Error clean up */  | 
1505  | 0  |          png_destroy_info_struct(png_ptr, &info_ptr);  | 
1506  | 0  |       }  | 
1507  |  |  | 
1508  | 0  |       png_destroy_write_struct(&png_ptr, NULL);  | 
1509  | 0  |    }  | 
1510  |  |  | 
1511  | 0  |    return png_image_error(image, "png_image_write_: out of memory");  | 
1512  | 0  | }  | 
1513  |  |  | 
1514  |  | /* Arguments to png_image_write_main: */  | 
1515  |  | typedef struct  | 
1516  |  | { | 
1517  |  |    /* Arguments: */  | 
1518  |  |    png_imagep      image;  | 
1519  |  |    png_const_voidp buffer;  | 
1520  |  |    png_int_32      row_stride;  | 
1521  |  |    png_const_voidp colormap;  | 
1522  |  |    int             convert_to_8bit;  | 
1523  |  |    /* Local variables: */  | 
1524  |  |    png_const_voidp first_row;  | 
1525  |  |    ptrdiff_t       row_bytes;  | 
1526  |  |    png_voidp       local_row;  | 
1527  |  |    /* Byte count for memory writing */  | 
1528  |  |    png_bytep        memory;  | 
1529  |  |    png_alloc_size_t memory_bytes; /* not used for STDIO */  | 
1530  |  |    png_alloc_size_t output_bytes; /* running total */  | 
1531  |  | } png_image_write_control;  | 
1532  |  |  | 
1533  |  | /* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to  | 
1534  |  |  * do any necessary byte swapping.  The component order is defined by the  | 
1535  |  |  * png_image format value.  | 
1536  |  |  */  | 
1537  |  | static int  | 
1538  |  | png_write_image_16bit(png_voidp argument)  | 
1539  | 0  | { | 
1540  | 0  |    png_image_write_control *display = png_voidcast(png_image_write_control*,  | 
1541  | 0  |        argument);  | 
1542  | 0  |    png_imagep image = display->image;  | 
1543  | 0  |    png_structrp png_ptr = image->opaque->png_ptr;  | 
1544  |  | 
  | 
1545  | 0  |    png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,  | 
1546  | 0  |        display->first_row);  | 
1547  | 0  |    png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row);  | 
1548  | 0  |    png_uint_16p row_end;  | 
1549  | 0  |    unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ?  | 
1550  | 0  |        3 : 1;  | 
1551  | 0  |    int aindex = 0;  | 
1552  | 0  |    png_uint_32 y = image->height;  | 
1553  |  | 
  | 
1554  | 0  |    if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)  | 
1555  | 0  |    { | 
1556  | 0  | #   ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED  | 
1557  | 0  |       if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0)  | 
1558  | 0  |       { | 
1559  | 0  |          aindex = -1;  | 
1560  | 0  |          ++input_row; /* To point to the first component */  | 
1561  | 0  |          ++output_row;  | 
1562  | 0  |       }  | 
1563  | 0  |          else  | 
1564  | 0  |             aindex = (int)channels;  | 
1565  |  | #     else  | 
1566  |  |          aindex = (int)channels;  | 
1567  |  | #     endif  | 
1568  | 0  |    }  | 
1569  |  |  | 
1570  | 0  |    else  | 
1571  | 0  |       png_error(png_ptr, "png_write_image: internal call error");  | 
1572  |  |  | 
1573  |  |    /* Work out the output row end and count over this, note that the increment  | 
1574  |  |     * above to 'row' means that row_end can actually be beyond the end of the  | 
1575  |  |     * row; this is correct.  | 
1576  |  |     */  | 
1577  | 0  |    row_end = output_row + image->width * (channels+1);  | 
1578  |  | 
  | 
1579  | 0  |    for (; y > 0; --y)  | 
1580  | 0  |    { | 
1581  | 0  |       png_const_uint_16p in_ptr = input_row;  | 
1582  | 0  |       png_uint_16p out_ptr = output_row;  | 
1583  |  | 
  | 
1584  | 0  |       while (out_ptr < row_end)  | 
1585  | 0  |       { | 
1586  | 0  |          png_uint_16 alpha = in_ptr[aindex];  | 
1587  | 0  |          png_uint_32 reciprocal = 0;  | 
1588  | 0  |          int c;  | 
1589  |  | 
  | 
1590  | 0  |          out_ptr[aindex] = alpha;  | 
1591  |  |  | 
1592  |  |          /* Calculate a reciprocal.  The correct calculation is simply  | 
1593  |  |           * component/alpha*65535 << 15. (I.e. 15 bits of precision); this  | 
1594  |  |           * allows correct rounding by adding .5 before the shift.  'reciprocal'  | 
1595  |  |           * is only initialized when required.  | 
1596  |  |           */  | 
1597  | 0  |          if (alpha > 0 && alpha < 65535)  | 
1598  | 0  |             reciprocal = ((0xffff<<15)+(alpha>>1))/alpha;  | 
1599  |  | 
  | 
1600  | 0  |          c = (int)channels;  | 
1601  | 0  |          do /* always at least one channel */  | 
1602  | 0  |          { | 
1603  | 0  |             png_uint_16 component = *in_ptr++;  | 
1604  |  |  | 
1605  |  |             /* The following gives 65535 for an alpha of 0, which is fine,  | 
1606  |  |              * otherwise if 0/0 is represented as some other value there is more  | 
1607  |  |              * likely to be a discontinuity which will probably damage  | 
1608  |  |              * compression when moving from a fully transparent area to a  | 
1609  |  |              * nearly transparent one.  (The assumption here is that opaque  | 
1610  |  |              * areas tend not to be 0 intensity.)  | 
1611  |  |              */  | 
1612  | 0  |             if (component >= alpha)  | 
1613  | 0  |                component = 65535;  | 
1614  |  |  | 
1615  |  |             /* component<alpha, so component/alpha is less than one and  | 
1616  |  |              * component*reciprocal is less than 2^31.  | 
1617  |  |              */  | 
1618  | 0  |             else if (component > 0 && alpha < 65535)  | 
1619  | 0  |             { | 
1620  | 0  |                png_uint_32 calc = component * reciprocal;  | 
1621  | 0  |                calc += 16384; /* round to nearest */  | 
1622  | 0  |                component = (png_uint_16)(calc >> 15);  | 
1623  | 0  |             }  | 
1624  |  | 
  | 
1625  | 0  |             *out_ptr++ = component;  | 
1626  | 0  |          }  | 
1627  | 0  |          while (--c > 0);  | 
1628  |  |  | 
1629  |  |          /* Skip to next component (skip the intervening alpha channel) */  | 
1630  | 0  |          ++in_ptr;  | 
1631  | 0  |          ++out_ptr;  | 
1632  | 0  |       }  | 
1633  |  | 
  | 
1634  | 0  |       png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row));  | 
1635  | 0  |       input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16));  | 
1636  | 0  |    }  | 
1637  |  | 
  | 
1638  | 0  |    return 1;  | 
1639  | 0  | }  | 
1640  |  |  | 
1641  |  | /* Given 16-bit input (1 to 4 channels) write 8-bit output.  If an alpha channel  | 
1642  |  |  * is present it must be removed from the components, the components are then  | 
1643  |  |  * written in sRGB encoding.  No components are added or removed.  | 
1644  |  |  *  | 
1645  |  |  * Calculate an alpha reciprocal to reverse pre-multiplication.  As above the  | 
1646  |  |  * calculation can be done to 15 bits of accuracy; however, the output needs to  | 
1647  |  |  * be scaled in the range 0..255*65535, so include that scaling here.  | 
1648  |  |  */  | 
1649  | 0  | #   define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+((alpha)>>1))/(alpha))  | 
1650  |  |  | 
1651  |  | static png_byte  | 
1652  |  | png_unpremultiply(png_uint_32 component, png_uint_32 alpha,  | 
1653  |  |     png_uint_32 reciprocal/*from the above macro*/)  | 
1654  | 0  | { | 
1655  |  |    /* The following gives 1.0 for an alpha of 0, which is fine, otherwise if 0/0  | 
1656  |  |     * is represented as some other value there is more likely to be a  | 
1657  |  |     * discontinuity which will probably damage compression when moving from a  | 
1658  |  |     * fully transparent area to a nearly transparent one.  (The assumption here  | 
1659  |  |     * is that opaque areas tend not to be 0 intensity.)  | 
1660  |  |     *  | 
1661  |  |     * There is a rounding problem here; if alpha is less than 128 it will end up  | 
1662  |  |     * as 0 when scaled to 8 bits.  To avoid introducing spurious colors into the  | 
1663  |  |     * output change for this too.  | 
1664  |  |     */  | 
1665  | 0  |    if (component >= alpha || alpha < 128)  | 
1666  | 0  |       return 255;  | 
1667  |  |  | 
1668  |  |    /* component<alpha, so component/alpha is less than one and  | 
1669  |  |     * component*reciprocal is less than 2^31.  | 
1670  |  |     */  | 
1671  | 0  |    else if (component > 0)  | 
1672  | 0  |    { | 
1673  |  |       /* The test is that alpha/257 (rounded) is less than 255, the first value  | 
1674  |  |        * that becomes 255 is 65407.  | 
1675  |  |        * NOTE: this must agree with the PNG_DIV257 macro (which must, therefore,  | 
1676  |  |        * be exact!)  [Could also test reciprocal != 0]  | 
1677  |  |        */  | 
1678  | 0  |       if (alpha < 65407)  | 
1679  | 0  |       { | 
1680  | 0  |          component *= reciprocal;  | 
1681  | 0  |          component += 64; /* round to nearest */  | 
1682  | 0  |          component >>= 7;  | 
1683  | 0  |       }  | 
1684  |  |  | 
1685  | 0  |       else  | 
1686  | 0  |          component *= 255;  | 
1687  |  |  | 
1688  |  |       /* Convert the component to sRGB. */  | 
1689  | 0  |       return (png_byte)PNG_sRGB_FROM_LINEAR(component);  | 
1690  | 0  |    }  | 
1691  |  |  | 
1692  | 0  |    else  | 
1693  | 0  |       return 0;  | 
1694  | 0  | }  | 
1695  |  |  | 
1696  |  | static int  | 
1697  |  | png_write_image_8bit(png_voidp argument)  | 
1698  | 0  | { | 
1699  | 0  |    png_image_write_control *display = png_voidcast(png_image_write_control*,  | 
1700  | 0  |        argument);  | 
1701  | 0  |    png_imagep image = display->image;  | 
1702  | 0  |    png_structrp png_ptr = image->opaque->png_ptr;  | 
1703  |  | 
  | 
1704  | 0  |    png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,  | 
1705  | 0  |        display->first_row);  | 
1706  | 0  |    png_bytep output_row = png_voidcast(png_bytep, display->local_row);  | 
1707  | 0  |    png_uint_32 y = image->height;  | 
1708  | 0  |    unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ?  | 
1709  | 0  |        3 : 1;  | 
1710  |  | 
  | 
1711  | 0  |    if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)  | 
1712  | 0  |    { | 
1713  | 0  |       png_bytep row_end;  | 
1714  | 0  |       int aindex;  | 
1715  |  | 
  | 
1716  | 0  | #   ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED  | 
1717  | 0  |       if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0)  | 
1718  | 0  |       { | 
1719  | 0  |          aindex = -1;  | 
1720  | 0  |          ++input_row; /* To point to the first component */  | 
1721  | 0  |          ++output_row;  | 
1722  | 0  |       }  | 
1723  |  |  | 
1724  | 0  |       else  | 
1725  | 0  | #   endif  | 
1726  | 0  |       aindex = (int)channels;  | 
1727  |  |  | 
1728  |  |       /* Use row_end in place of a loop counter: */  | 
1729  | 0  |       row_end = output_row + image->width * (channels+1);  | 
1730  |  | 
  | 
1731  | 0  |       for (; y > 0; --y)  | 
1732  | 0  |       { | 
1733  | 0  |          png_const_uint_16p in_ptr = input_row;  | 
1734  | 0  |          png_bytep out_ptr = output_row;  | 
1735  |  | 
  | 
1736  | 0  |          while (out_ptr < row_end)  | 
1737  | 0  |          { | 
1738  | 0  |             png_uint_16 alpha = in_ptr[aindex];  | 
1739  | 0  |             png_byte alphabyte = (png_byte)PNG_DIV257(alpha);  | 
1740  | 0  |             png_uint_32 reciprocal = 0;  | 
1741  | 0  |             int c;  | 
1742  |  |  | 
1743  |  |             /* Scale and write the alpha channel. */  | 
1744  | 0  |             out_ptr[aindex] = alphabyte;  | 
1745  |  | 
  | 
1746  | 0  |             if (alphabyte > 0 && alphabyte < 255)  | 
1747  | 0  |                reciprocal = UNP_RECIPROCAL(alpha);  | 
1748  |  | 
  | 
1749  | 0  |             c = (int)channels;  | 
1750  | 0  |             do /* always at least one channel */  | 
1751  | 0  |                *out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal);  | 
1752  | 0  |             while (--c > 0);  | 
1753  |  |  | 
1754  |  |             /* Skip to next component (skip the intervening alpha channel) */  | 
1755  | 0  |             ++in_ptr;  | 
1756  | 0  |             ++out_ptr;  | 
1757  | 0  |          } /* while out_ptr < row_end */  | 
1758  |  | 
  | 
1759  | 0  |          png_write_row(png_ptr, png_voidcast(png_const_bytep,  | 
1760  | 0  |              display->local_row));  | 
1761  | 0  |          input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16));  | 
1762  | 0  |       } /* while y */  | 
1763  | 0  |    }  | 
1764  |  |  | 
1765  | 0  |    else  | 
1766  | 0  |    { | 
1767  |  |       /* No alpha channel, so the row_end really is the end of the row and it  | 
1768  |  |        * is sufficient to loop over the components one by one.  | 
1769  |  |        */  | 
1770  | 0  |       png_bytep row_end = output_row + image->width * channels;  | 
1771  |  | 
  | 
1772  | 0  |       for (; y > 0; --y)  | 
1773  | 0  |       { | 
1774  | 0  |          png_const_uint_16p in_ptr = input_row;  | 
1775  | 0  |          png_bytep out_ptr = output_row;  | 
1776  |  | 
  | 
1777  | 0  |          while (out_ptr < row_end)  | 
1778  | 0  |          { | 
1779  | 0  |             png_uint_32 component = *in_ptr++;  | 
1780  |  | 
  | 
1781  | 0  |             component *= 255;  | 
1782  | 0  |             *out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component);  | 
1783  | 0  |          }  | 
1784  |  | 
  | 
1785  | 0  |          png_write_row(png_ptr, output_row);  | 
1786  | 0  |          input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16));  | 
1787  | 0  |       }  | 
1788  | 0  |    }  | 
1789  |  | 
  | 
1790  | 0  |    return 1;  | 
1791  | 0  | }  | 
1792  |  |  | 
1793  |  | static void  | 
1794  |  | png_image_set_PLTE(png_image_write_control *display)  | 
1795  | 0  | { | 
1796  | 0  |    png_imagep image = display->image;  | 
1797  | 0  |    const void *cmap = display->colormap;  | 
1798  | 0  |    int entries = image->colormap_entries > 256 ? 256 :  | 
1799  | 0  |        (int)image->colormap_entries;  | 
1800  |  |  | 
1801  |  |    /* NOTE: the caller must check for cmap != NULL and entries != 0 */  | 
1802  | 0  |    png_uint_32 format = image->format;  | 
1803  | 0  |    unsigned int channels = PNG_IMAGE_SAMPLE_CHANNELS(format);  | 
1804  |  | 
  | 
1805  | 0  | #   if defined(PNG_FORMAT_BGR_SUPPORTED) &&\  | 
1806  | 0  |       defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED)  | 
1807  | 0  |       int afirst = (format & PNG_FORMAT_FLAG_AFIRST) != 0 &&  | 
1808  | 0  |           (format & PNG_FORMAT_FLAG_ALPHA) != 0;  | 
1809  |  | #   else  | 
1810  |  | #     define afirst 0  | 
1811  |  | #   endif  | 
1812  |  | 
  | 
1813  | 0  | #   ifdef PNG_FORMAT_BGR_SUPPORTED  | 
1814  | 0  |       int bgr = (format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;  | 
1815  |  | #   else  | 
1816  |  | #     define bgr 0  | 
1817  |  | #   endif  | 
1818  |  | 
  | 
1819  | 0  |    int i, num_trans;  | 
1820  | 0  |    png_color palette[256];  | 
1821  | 0  |    png_byte tRNS[256];  | 
1822  |  | 
  | 
1823  | 0  |    memset(tRNS, 255, (sizeof tRNS));  | 
1824  | 0  |    memset(palette, 0, (sizeof palette));  | 
1825  |  | 
  | 
1826  | 0  |    for (i=num_trans=0; i<entries; ++i)  | 
1827  | 0  |    { | 
1828  |  |       /* This gets automatically converted to sRGB with reversal of the  | 
1829  |  |        * pre-multiplication if the color-map has an alpha channel.  | 
1830  |  |        */  | 
1831  | 0  |       if ((format & PNG_FORMAT_FLAG_LINEAR) != 0)  | 
1832  | 0  |       { | 
1833  | 0  |          png_const_uint_16p entry = png_voidcast(png_const_uint_16p, cmap);  | 
1834  |  | 
  | 
1835  | 0  |          entry += (unsigned int)i * channels;  | 
1836  |  | 
  | 
1837  | 0  |          if ((channels & 1) != 0) /* no alpha */  | 
1838  | 0  |          { | 
1839  | 0  |             if (channels >= 3) /* RGB */  | 
1840  | 0  |             { | 
1841  | 0  |                palette[i].blue = (png_byte)PNG_sRGB_FROM_LINEAR(255 *  | 
1842  | 0  |                    entry[(2 ^ bgr)]);  | 
1843  | 0  |                palette[i].green = (png_byte)PNG_sRGB_FROM_LINEAR(255 *  | 
1844  | 0  |                    entry[1]);  | 
1845  | 0  |                palette[i].red = (png_byte)PNG_sRGB_FROM_LINEAR(255 *  | 
1846  | 0  |                    entry[bgr]);  | 
1847  | 0  |             }  | 
1848  |  |  | 
1849  | 0  |             else /* Gray */  | 
1850  | 0  |                palette[i].blue = palette[i].red = palette[i].green =  | 
1851  | 0  |                   (png_byte)PNG_sRGB_FROM_LINEAR(255 * *entry);  | 
1852  | 0  |          }  | 
1853  |  |  | 
1854  | 0  |          else /* alpha */  | 
1855  | 0  |          { | 
1856  | 0  |             png_uint_16 alpha = entry[afirst ? 0 : channels-1];  | 
1857  | 0  |             png_byte alphabyte = (png_byte)PNG_DIV257(alpha);  | 
1858  | 0  |             png_uint_32 reciprocal = 0;  | 
1859  |  |  | 
1860  |  |             /* Calculate a reciprocal, as in the png_write_image_8bit code above  | 
1861  |  |              * this is designed to produce a value scaled to 255*65535 when  | 
1862  |  |              * divided by 128 (i.e. asr 7).  | 
1863  |  |              */  | 
1864  | 0  |             if (alphabyte > 0 && alphabyte < 255)  | 
1865  | 0  |                reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha;  | 
1866  |  | 
  | 
1867  | 0  |             tRNS[i] = alphabyte;  | 
1868  | 0  |             if (alphabyte < 255)  | 
1869  | 0  |                num_trans = i+1;  | 
1870  |  | 
  | 
1871  | 0  |             if (channels >= 3) /* RGB */  | 
1872  | 0  |             { | 
1873  | 0  |                palette[i].blue = png_unpremultiply(entry[afirst + (2 ^ bgr)],  | 
1874  | 0  |                    alpha, reciprocal);  | 
1875  | 0  |                palette[i].green = png_unpremultiply(entry[afirst + 1], alpha,  | 
1876  | 0  |                    reciprocal);  | 
1877  | 0  |                palette[i].red = png_unpremultiply(entry[afirst + bgr], alpha,  | 
1878  | 0  |                    reciprocal);  | 
1879  | 0  |             }  | 
1880  |  |  | 
1881  | 0  |             else /* gray */  | 
1882  | 0  |                palette[i].blue = palette[i].red = palette[i].green =  | 
1883  | 0  |                    png_unpremultiply(entry[afirst], alpha, reciprocal);  | 
1884  | 0  |          }  | 
1885  | 0  |       }  | 
1886  |  |  | 
1887  | 0  |       else /* Color-map has sRGB values */  | 
1888  | 0  |       { | 
1889  | 0  |          png_const_bytep entry = png_voidcast(png_const_bytep, cmap);  | 
1890  |  | 
  | 
1891  | 0  |          entry += (unsigned int)i * channels;  | 
1892  |  | 
  | 
1893  | 0  |          switch (channels)  | 
1894  | 0  |          { | 
1895  | 0  |             case 4:  | 
1896  | 0  |                tRNS[i] = entry[afirst ? 0 : 3];  | 
1897  | 0  |                if (tRNS[i] < 255)  | 
1898  | 0  |                   num_trans = i+1;  | 
1899  |  |                /* FALLTHROUGH */  | 
1900  | 0  |             case 3:  | 
1901  | 0  |                palette[i].blue = entry[afirst + (2 ^ bgr)];  | 
1902  | 0  |                palette[i].green = entry[afirst + 1];  | 
1903  | 0  |                palette[i].red = entry[afirst + bgr];  | 
1904  | 0  |                break;  | 
1905  |  |  | 
1906  | 0  |             case 2:  | 
1907  | 0  |                tRNS[i] = entry[1 ^ afirst];  | 
1908  | 0  |                if (tRNS[i] < 255)  | 
1909  | 0  |                   num_trans = i+1;  | 
1910  |  |                /* FALLTHROUGH */  | 
1911  | 0  |             case 1:  | 
1912  | 0  |                palette[i].blue = palette[i].red = palette[i].green =  | 
1913  | 0  |                   entry[afirst];  | 
1914  | 0  |                break;  | 
1915  |  |  | 
1916  | 0  |             default:  | 
1917  | 0  |                break;  | 
1918  | 0  |          }  | 
1919  | 0  |       }  | 
1920  | 0  |    }  | 
1921  |  |  | 
1922  |  | #   ifdef afirst  | 
1923  |  | #     undef afirst  | 
1924  |  | #   endif  | 
1925  |  | #   ifdef bgr  | 
1926  |  | #     undef bgr  | 
1927  |  | #   endif  | 
1928  |  |  | 
1929  | 0  |    png_set_PLTE(image->opaque->png_ptr, image->opaque->info_ptr, palette,  | 
1930  | 0  |        entries);  | 
1931  |  | 
  | 
1932  | 0  |    if (num_trans > 0)  | 
1933  | 0  |       png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS,  | 
1934  | 0  |           num_trans, NULL);  | 
1935  |  | 
  | 
1936  | 0  |    image->colormap_entries = (png_uint_32)entries;  | 
1937  | 0  | }  | 
1938  |  |  | 
1939  |  | static int  | 
1940  |  | png_image_write_main(png_voidp argument)  | 
1941  | 0  | { | 
1942  | 0  |    png_image_write_control *display = png_voidcast(png_image_write_control*,  | 
1943  | 0  |        argument);  | 
1944  | 0  |    png_imagep image = display->image;  | 
1945  | 0  |    png_structrp png_ptr = image->opaque->png_ptr;  | 
1946  | 0  |    png_inforp info_ptr = image->opaque->info_ptr;  | 
1947  | 0  |    png_uint_32 format = image->format;  | 
1948  |  |  | 
1949  |  |    /* The following four ints are actually booleans */  | 
1950  | 0  |    int colormap = (format & PNG_FORMAT_FLAG_COLORMAP);  | 
1951  | 0  |    int linear = !colormap && (format & PNG_FORMAT_FLAG_LINEAR); /* input */  | 
1952  | 0  |    int alpha = !colormap && (format & PNG_FORMAT_FLAG_ALPHA);  | 
1953  | 0  |    int write_16bit = linear && (display->convert_to_8bit == 0);  | 
1954  |  | 
  | 
1955  | 0  | #   ifdef PNG_BENIGN_ERRORS_SUPPORTED  | 
1956  |  |       /* Make sure we error out on any bad situation */  | 
1957  | 0  |       png_set_benign_errors(png_ptr, 0/*error*/);  | 
1958  | 0  | #   endif  | 
1959  |  |  | 
1960  |  |    /* Default the 'row_stride' parameter if required, also check the row stride  | 
1961  |  |     * and total image size to ensure that they are within the system limits.  | 
1962  |  |     */  | 
1963  | 0  |    { | 
1964  | 0  |       unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format);  | 
1965  |  | 
  | 
1966  | 0  |       if (image->width <= 0x7fffffffU/channels) /* no overflow */  | 
1967  | 0  |       { | 
1968  | 0  |          png_uint_32 check;  | 
1969  | 0  |          png_uint_32 png_row_stride = image->width * channels;  | 
1970  |  | 
  | 
1971  | 0  |          if (display->row_stride == 0)  | 
1972  | 0  |             display->row_stride = (png_int_32)/*SAFE*/png_row_stride;  | 
1973  |  | 
  | 
1974  | 0  |          if (display->row_stride < 0)  | 
1975  | 0  |             check = (png_uint_32)(-display->row_stride);  | 
1976  |  |  | 
1977  | 0  |          else  | 
1978  | 0  |             check = (png_uint_32)display->row_stride;  | 
1979  |  | 
  | 
1980  | 0  |          if (check >= png_row_stride)  | 
1981  | 0  |          { | 
1982  |  |             /* Now check for overflow of the image buffer calculation; this  | 
1983  |  |              * limits the whole image size to 32 bits for API compatibility with  | 
1984  |  |              * the current, 32-bit, PNG_IMAGE_BUFFER_SIZE macro.  | 
1985  |  |              */  | 
1986  | 0  |             if (image->height > 0xffffffffU/png_row_stride)  | 
1987  | 0  |                png_error(image->opaque->png_ptr, "memory image too large");  | 
1988  | 0  |          }  | 
1989  |  |  | 
1990  | 0  |          else  | 
1991  | 0  |             png_error(image->opaque->png_ptr, "supplied row stride too small");  | 
1992  | 0  |       }  | 
1993  |  |  | 
1994  | 0  |       else  | 
1995  | 0  |          png_error(image->opaque->png_ptr, "image row stride too large");  | 
1996  | 0  |    }  | 
1997  |  |  | 
1998  |  |    /* Set the required transforms then write the rows in the correct order. */  | 
1999  | 0  |    if ((format & PNG_FORMAT_FLAG_COLORMAP) != 0)  | 
2000  | 0  |    { | 
2001  | 0  |       if (display->colormap != NULL && image->colormap_entries > 0)  | 
2002  | 0  |       { | 
2003  | 0  |          png_uint_32 entries = image->colormap_entries;  | 
2004  |  | 
  | 
2005  | 0  |          png_set_IHDR(png_ptr, info_ptr, image->width, image->height,  | 
2006  | 0  |              entries > 16 ? 8 : (entries > 4 ? 4 : (entries > 2 ? 2 : 1)),  | 
2007  | 0  |              PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,  | 
2008  | 0  |              PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);  | 
2009  |  | 
  | 
2010  | 0  |          png_image_set_PLTE(display);  | 
2011  | 0  |       }  | 
2012  |  |  | 
2013  | 0  |       else  | 
2014  | 0  |          png_error(image->opaque->png_ptr,  | 
2015  | 0  |              "no color-map for color-mapped image");  | 
2016  | 0  |    }  | 
2017  |  |  | 
2018  | 0  |    else  | 
2019  | 0  |       png_set_IHDR(png_ptr, info_ptr, image->width, image->height,  | 
2020  | 0  |           write_16bit ? 16 : 8,  | 
2021  | 0  |           ((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) +  | 
2022  | 0  |           ((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0),  | 
2023  | 0  |           PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);  | 
2024  |  |  | 
2025  |  |    /* Counter-intuitively the data transformations must be called *after*  | 
2026  |  |     * png_write_info, not before as in the read code, but the 'set' functions  | 
2027  |  |     * must still be called before.  Just set the color space information, never  | 
2028  |  |     * write an interlaced image.  | 
2029  |  |     */  | 
2030  |  |  | 
2031  | 0  |    if (write_16bit != 0)  | 
2032  | 0  |    { | 
2033  |  |       /* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */  | 
2034  | 0  |       png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR);  | 
2035  |  | 
  | 
2036  | 0  |       if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0)  | 
2037  | 0  |          png_set_cHRM_fixed(png_ptr, info_ptr,  | 
2038  |  |              /* color      x       y */  | 
2039  | 0  |              /* white */ 31270, 32900,  | 
2040  | 0  |              /* red   */ 64000, 33000,  | 
2041  | 0  |              /* green */ 30000, 60000,  | 
2042  | 0  |              /* blue  */ 15000,  6000  | 
2043  | 0  |          );  | 
2044  | 0  |    }  | 
2045  |  |  | 
2046  | 0  |    else if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0)  | 
2047  | 0  |       png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL);  | 
2048  |  |  | 
2049  |  |    /* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit  | 
2050  |  |     * space must still be gamma encoded.  | 
2051  |  |     */  | 
2052  | 0  |    else  | 
2053  | 0  |       png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);  | 
2054  |  |  | 
2055  |  |    /* Write the file header. */  | 
2056  | 0  |    png_write_info(png_ptr, info_ptr);  | 
2057  |  |  | 
2058  |  |    /* Now set up the data transformations (*after* the header is written),  | 
2059  |  |     * remove the handled transformations from the 'format' flags for checking.  | 
2060  |  |     *  | 
2061  |  |     * First check for a little endian system if writing 16-bit files.  | 
2062  |  |     */  | 
2063  | 0  |    if (write_16bit != 0)  | 
2064  | 0  |    { | 
2065  | 0  |       png_uint_16 le = 0x0001;  | 
2066  |  | 
  | 
2067  | 0  |       if ((*(png_const_bytep) & le) != 0)  | 
2068  | 0  |          png_set_swap(png_ptr);  | 
2069  | 0  |    }  | 
2070  |  | 
  | 
2071  | 0  | #   ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED  | 
2072  | 0  |       if ((format & PNG_FORMAT_FLAG_BGR) != 0)  | 
2073  | 0  |       { | 
2074  | 0  |          if (colormap == 0 && (format & PNG_FORMAT_FLAG_COLOR) != 0)  | 
2075  | 0  |             png_set_bgr(png_ptr);  | 
2076  | 0  |          format &= ~PNG_FORMAT_FLAG_BGR;  | 
2077  | 0  |       }  | 
2078  | 0  | #   endif  | 
2079  |  | 
  | 
2080  | 0  | #   ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED  | 
2081  | 0  |       if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)  | 
2082  | 0  |       { | 
2083  | 0  |          if (colormap == 0 && (format & PNG_FORMAT_FLAG_ALPHA) != 0)  | 
2084  | 0  |             png_set_swap_alpha(png_ptr);  | 
2085  | 0  |          format &= ~PNG_FORMAT_FLAG_AFIRST;  | 
2086  | 0  |       }  | 
2087  | 0  | #   endif  | 
2088  |  |  | 
2089  |  |    /* If there are 16 or fewer color-map entries we wrote a lower bit depth  | 
2090  |  |     * above, but the application data is still byte packed.  | 
2091  |  |     */  | 
2092  | 0  |    if (colormap != 0 && image->colormap_entries <= 16)  | 
2093  | 0  |       png_set_packing(png_ptr);  | 
2094  |  |  | 
2095  |  |    /* That should have handled all (both) the transforms. */  | 
2096  | 0  |    if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR |  | 
2097  | 0  |          PNG_FORMAT_FLAG_ALPHA | PNG_FORMAT_FLAG_COLORMAP)) != 0)  | 
2098  | 0  |       png_error(png_ptr, "png_write_image: unsupported transformation");  | 
2099  |  |  | 
2100  | 0  |    { | 
2101  | 0  |       png_const_bytep row = png_voidcast(png_const_bytep, display->buffer);  | 
2102  | 0  |       ptrdiff_t row_bytes = display->row_stride;  | 
2103  |  | 
  | 
2104  | 0  |       if (linear != 0)  | 
2105  | 0  |          row_bytes *= (sizeof (png_uint_16));  | 
2106  |  | 
  | 
2107  | 0  |       if (row_bytes < 0)  | 
2108  | 0  |          row += (image->height-1) * (-row_bytes);  | 
2109  |  | 
  | 
2110  | 0  |       display->first_row = row;  | 
2111  | 0  |       display->row_bytes = row_bytes;  | 
2112  | 0  |    }  | 
2113  |  |  | 
2114  |  |    /* Apply 'fast' options if the flag is set. */  | 
2115  | 0  |    if ((image->flags & PNG_IMAGE_FLAG_FAST) != 0)  | 
2116  | 0  |    { | 
2117  | 0  |       png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_NO_FILTERS);  | 
2118  |  |       /* NOTE: determined by experiment using pngstest, this reflects some  | 
2119  |  |        * balance between the time to write the image once and the time to read  | 
2120  |  |        * it about 50 times.  The speed-up in pngstest was about 10-20% of the  | 
2121  |  |        * total (user) time on a heavily loaded system.  | 
2122  |  |        */  | 
2123  | 0  | #   ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED  | 
2124  | 0  |       png_set_compression_level(png_ptr, 3);  | 
2125  | 0  | #   endif  | 
2126  | 0  |    }  | 
2127  |  |  | 
2128  |  |    /* Check for the cases that currently require a pre-transform on the row  | 
2129  |  |     * before it is written.  This only applies when the input is 16-bit and  | 
2130  |  |     * either there is an alpha channel or it is converted to 8-bit.  | 
2131  |  |     */  | 
2132  | 0  |    if ((linear != 0 && alpha != 0 ) ||  | 
2133  | 0  |        (colormap == 0 && display->convert_to_8bit != 0))  | 
2134  | 0  |    { | 
2135  | 0  |       png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,  | 
2136  | 0  |           png_get_rowbytes(png_ptr, info_ptr)));  | 
2137  | 0  |       int result;  | 
2138  |  | 
  | 
2139  | 0  |       display->local_row = row;  | 
2140  | 0  |       if (write_16bit != 0)  | 
2141  | 0  |          result = png_safe_execute(image, png_write_image_16bit, display);  | 
2142  | 0  |       else  | 
2143  | 0  |          result = png_safe_execute(image, png_write_image_8bit, display);  | 
2144  | 0  |       display->local_row = NULL;  | 
2145  |  | 
  | 
2146  | 0  |       png_free(png_ptr, row);  | 
2147  |  |  | 
2148  |  |       /* Skip the 'write_end' on error: */  | 
2149  | 0  |       if (result == 0)  | 
2150  | 0  |          return 0;  | 
2151  | 0  |    }  | 
2152  |  |  | 
2153  |  |    /* Otherwise this is the case where the input is in a format currently  | 
2154  |  |     * supported by the rest of the libpng write code; call it directly.  | 
2155  |  |     */  | 
2156  | 0  |    else  | 
2157  | 0  |    { | 
2158  | 0  |       png_const_bytep row = png_voidcast(png_const_bytep, display->first_row);  | 
2159  | 0  |       ptrdiff_t row_bytes = display->row_bytes;  | 
2160  | 0  |       png_uint_32 y = image->height;  | 
2161  |  | 
  | 
2162  | 0  |       for (; y > 0; --y)  | 
2163  | 0  |       { | 
2164  | 0  |          png_write_row(png_ptr, row);  | 
2165  | 0  |          row += row_bytes;  | 
2166  | 0  |       }  | 
2167  | 0  |    }  | 
2168  |  |  | 
2169  | 0  |    png_write_end(png_ptr, info_ptr);  | 
2170  | 0  |    return 1;  | 
2171  | 0  | }  | 
2172  |  |  | 
2173  |  |  | 
2174  |  | static void (PNGCBAPI  | 
2175  |  | image_memory_write)(png_structp png_ptr, png_bytep/*const*/ data, size_t size)  | 
2176  | 0  | { | 
2177  | 0  |    png_image_write_control *display = png_voidcast(png_image_write_control*,  | 
2178  | 0  |        png_ptr->io_ptr/*backdoor: png_get_io_ptr(png_ptr)*/);  | 
2179  | 0  |    png_alloc_size_t ob = display->output_bytes;  | 
2180  |  |  | 
2181  |  |    /* Check for overflow; this should never happen: */  | 
2182  | 0  |    if (size <= ((png_alloc_size_t)-1) - ob)  | 
2183  | 0  |    { | 
2184  |  |       /* I don't think libpng ever does this, but just in case: */  | 
2185  | 0  |       if (size > 0)  | 
2186  | 0  |       { | 
2187  | 0  |          if (display->memory_bytes >= ob+size) /* writing */  | 
2188  | 0  |             memcpy(display->memory+ob, data, size);  | 
2189  |  |  | 
2190  |  |          /* Always update the size: */  | 
2191  | 0  |          display->output_bytes = ob+size;  | 
2192  | 0  |       }  | 
2193  | 0  |    }  | 
2194  |  |  | 
2195  | 0  |    else  | 
2196  | 0  |       png_error(png_ptr, "png_image_write_to_memory: PNG too big");  | 
2197  | 0  | }  | 
2198  |  |  | 
2199  |  | static void (PNGCBAPI  | 
2200  |  | image_memory_flush)(png_structp png_ptr)  | 
2201  | 0  | { | 
2202  | 0  |    PNG_UNUSED(png_ptr)  | 
2203  | 0  | }  | 
2204  |  |  | 
2205  |  | static int  | 
2206  |  | png_image_write_memory(png_voidp argument)  | 
2207  | 0  | { | 
2208  | 0  |    png_image_write_control *display = png_voidcast(png_image_write_control*,  | 
2209  | 0  |        argument);  | 
2210  |  |  | 
2211  |  |    /* The rest of the memory-specific init and write_main in an error protected  | 
2212  |  |     * environment.  This case needs to use callbacks for the write operations  | 
2213  |  |     * since libpng has no built in support for writing to memory.  | 
2214  |  |     */  | 
2215  | 0  |    png_set_write_fn(display->image->opaque->png_ptr, display/*io_ptr*/,  | 
2216  | 0  |        image_memory_write, image_memory_flush);  | 
2217  |  | 
  | 
2218  | 0  |    return png_image_write_main(display);  | 
2219  | 0  | }  | 
2220  |  |  | 
2221  |  | int PNGAPI  | 
2222  |  | png_image_write_to_memory(png_imagep image, void *memory,  | 
2223  |  |     png_alloc_size_t * PNG_RESTRICT memory_bytes, int convert_to_8bit,  | 
2224  |  |     const void *buffer, png_int_32 row_stride, const void *colormap)  | 
2225  | 0  | { | 
2226  |  |    /* Write the image to the given buffer, or count the bytes if it is NULL */  | 
2227  | 0  |    if (image != NULL && image->version == PNG_IMAGE_VERSION)  | 
2228  | 0  |    { | 
2229  | 0  |       if (memory_bytes != NULL && buffer != NULL)  | 
2230  | 0  |       { | 
2231  |  |          /* This is to give the caller an easier error detection in the NULL  | 
2232  |  |           * case and guard against uninitialized variable problems:  | 
2233  |  |           */  | 
2234  | 0  |          if (memory == NULL)  | 
2235  | 0  |             *memory_bytes = 0;  | 
2236  |  | 
  | 
2237  | 0  |          if (png_image_write_init(image) != 0)  | 
2238  | 0  |          { | 
2239  | 0  |             png_image_write_control display;  | 
2240  | 0  |             int result;  | 
2241  |  | 
  | 
2242  | 0  |             memset(&display, 0, (sizeof display));  | 
2243  | 0  |             display.image = image;  | 
2244  | 0  |             display.buffer = buffer;  | 
2245  | 0  |             display.row_stride = row_stride;  | 
2246  | 0  |             display.colormap = colormap;  | 
2247  | 0  |             display.convert_to_8bit = convert_to_8bit;  | 
2248  | 0  |             display.memory = png_voidcast(png_bytep, memory);  | 
2249  | 0  |             display.memory_bytes = *memory_bytes;  | 
2250  | 0  |             display.output_bytes = 0;  | 
2251  |  | 
  | 
2252  | 0  |             result = png_safe_execute(image, png_image_write_memory, &display);  | 
2253  | 0  |             png_image_free(image);  | 
2254  |  |  | 
2255  |  |             /* write_memory returns true even if we ran out of buffer. */  | 
2256  | 0  |             if (result)  | 
2257  | 0  |             { | 
2258  |  |                /* On out-of-buffer this function returns '0' but still updates  | 
2259  |  |                 * memory_bytes:  | 
2260  |  |                 */  | 
2261  | 0  |                if (memory != NULL && display.output_bytes > *memory_bytes)  | 
2262  | 0  |                   result = 0;  | 
2263  |  | 
  | 
2264  | 0  |                *memory_bytes = display.output_bytes;  | 
2265  | 0  |             }  | 
2266  |  | 
  | 
2267  | 0  |             return result;  | 
2268  | 0  |          }  | 
2269  |  |  | 
2270  | 0  |          else  | 
2271  | 0  |             return 0;  | 
2272  | 0  |       }  | 
2273  |  |  | 
2274  | 0  |       else  | 
2275  | 0  |          return png_image_error(image,  | 
2276  | 0  |              "png_image_write_to_memory: invalid argument");  | 
2277  | 0  |    }  | 
2278  |  |  | 
2279  | 0  |    else if (image != NULL)  | 
2280  | 0  |       return png_image_error(image,  | 
2281  | 0  |           "png_image_write_to_memory: incorrect PNG_IMAGE_VERSION");  | 
2282  |  |  | 
2283  | 0  |    else  | 
2284  | 0  |       return 0;  | 
2285  | 0  | }  | 
2286  |  |  | 
2287  |  | #ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED  | 
2288  |  | int PNGAPI  | 
2289  |  | png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit,  | 
2290  |  |     const void *buffer, png_int_32 row_stride, const void *colormap)  | 
2291  |  | { | 
2292  |  |    /* Write the image to the given (FILE*). */  | 
2293  |  |    if (image != NULL && image->version == PNG_IMAGE_VERSION)  | 
2294  |  |    { | 
2295  |  |       if (file != NULL && buffer != NULL)  | 
2296  |  |       { | 
2297  |  |          if (png_image_write_init(image) != 0)  | 
2298  |  |          { | 
2299  |  |             png_image_write_control display;  | 
2300  |  |             int result;  | 
2301  |  |  | 
2302  |  |             /* This is slightly evil, but png_init_io doesn't do anything other  | 
2303  |  |              * than this and we haven't changed the standard IO functions so  | 
2304  |  |              * this saves a 'safe' function.  | 
2305  |  |              */  | 
2306  |  |             image->opaque->png_ptr->io_ptr = file;  | 
2307  |  |  | 
2308  |  |             memset(&display, 0, (sizeof display));  | 
2309  |  |             display.image = image;  | 
2310  |  |             display.buffer = buffer;  | 
2311  |  |             display.row_stride = row_stride;  | 
2312  |  |             display.colormap = colormap;  | 
2313  |  |             display.convert_to_8bit = convert_to_8bit;  | 
2314  |  |  | 
2315  |  |             result = png_safe_execute(image, png_image_write_main, &display);  | 
2316  |  |             png_image_free(image);  | 
2317  |  |             return result;  | 
2318  |  |          }  | 
2319  |  |  | 
2320  |  |          else  | 
2321  |  |             return 0;  | 
2322  |  |       }  | 
2323  |  |  | 
2324  |  |       else  | 
2325  |  |          return png_image_error(image,  | 
2326  |  |              "png_image_write_to_stdio: invalid argument");  | 
2327  |  |    }  | 
2328  |  |  | 
2329  |  |    else if (image != NULL)  | 
2330  |  |       return png_image_error(image,  | 
2331  |  |           "png_image_write_to_stdio: incorrect PNG_IMAGE_VERSION");  | 
2332  |  |  | 
2333  |  |    else  | 
2334  |  |       return 0;  | 
2335  |  | }  | 
2336  |  |  | 
2337  |  | int PNGAPI  | 
2338  |  | png_image_write_to_file(png_imagep image, const char *file_name,  | 
2339  |  |     int convert_to_8bit, const void *buffer, png_int_32 row_stride,  | 
2340  |  |     const void *colormap)  | 
2341  |  | { | 
2342  |  |    /* Write the image to the named file. */  | 
2343  |  |    if (image != NULL && image->version == PNG_IMAGE_VERSION)  | 
2344  |  |    { | 
2345  |  |       if (file_name != NULL && buffer != NULL)  | 
2346  |  |       { | 
2347  |  |          FILE *fp = fopen(file_name, "wb");  | 
2348  |  |  | 
2349  |  |          if (fp != NULL)  | 
2350  |  |          { | 
2351  |  |             if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer,  | 
2352  |  |                 row_stride, colormap) != 0)  | 
2353  |  |             { | 
2354  |  |                int error; /* from fflush/fclose */  | 
2355  |  |  | 
2356  |  |                /* Make sure the file is flushed correctly. */  | 
2357  |  |                if (fflush(fp) == 0 && ferror(fp) == 0)  | 
2358  |  |                { | 
2359  |  |                   if (fclose(fp) == 0)  | 
2360  |  |                      return 1;  | 
2361  |  |  | 
2362  |  |                   error = errno; /* from fclose */  | 
2363  |  |                }  | 
2364  |  |  | 
2365  |  |                else  | 
2366  |  |                { | 
2367  |  |                   error = errno; /* from fflush or ferror */  | 
2368  |  |                   (void)fclose(fp);  | 
2369  |  |                }  | 
2370  |  |  | 
2371  |  |                (void)remove(file_name);  | 
2372  |  |                /* The image has already been cleaned up; this is just used to  | 
2373  |  |                 * set the error (because the original write succeeded).  | 
2374  |  |                 */  | 
2375  |  |                return png_image_error(image, strerror(error));  | 
2376  |  |             }  | 
2377  |  |  | 
2378  |  |             else  | 
2379  |  |             { | 
2380  |  |                /* Clean up: just the opened file. */  | 
2381  |  |                (void)fclose(fp);  | 
2382  |  |                (void)remove(file_name);  | 
2383  |  |                return 0;  | 
2384  |  |             }  | 
2385  |  |          }  | 
2386  |  |  | 
2387  |  |          else  | 
2388  |  |             return png_image_error(image, strerror(errno));  | 
2389  |  |       }  | 
2390  |  |  | 
2391  |  |       else  | 
2392  |  |          return png_image_error(image,  | 
2393  |  |              "png_image_write_to_file: invalid argument");  | 
2394  |  |    }  | 
2395  |  |  | 
2396  |  |    else if (image != NULL)  | 
2397  |  |       return png_image_error(image,  | 
2398  |  |           "png_image_write_to_file: incorrect PNG_IMAGE_VERSION");  | 
2399  |  |  | 
2400  |  |    else  | 
2401  |  |       return 0;  | 
2402  |  | }  | 
2403  |  | #endif /* SIMPLIFIED_WRITE_STDIO */  | 
2404  |  | #endif /* SIMPLIFIED_WRITE */  | 
2405  |  | #endif /* WRITE */  |