blob: 0987612d1f3ab2f1dd3e312a44b8283fd63e7f0f [file] [log] [blame]
The Android Open Source Project893912b2009-03-03 19:30:05 -08001
2/* pngwrite.c - general routines to write a PNG file
3 *
Patrick Scotta0bb96c2009-07-22 11:50:02 -04004 * Last changed in libpng 1.2.37 [June 4, 2009]
5 * Copyright (c) 1998-2009 Glenn Randers-Pehrson
The Android Open Source Project893912b2009-03-03 19:30:05 -08006 * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
7 * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04008 *
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
The Android Open Source Project893912b2009-03-03 19:30:05 -080012 */
13
Patrick Scotta0bb96c2009-07-22 11:50:02 -040014/* Get internal access to png.h */
The Android Open Source Project893912b2009-03-03 19:30:05 -080015#define PNG_INTERNAL
16#include "png.h"
17#ifdef PNG_WRITE_SUPPORTED
18
19/* Writes all the PNG information. This is the suggested way to use the
20 * library. If you have a new chunk to add, make a function to write it,
21 * and put it in the correct location here. If you want the chunk written
22 * after the image data, put it in png_write_end(). I strongly encourage
23 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
24 * the chunk, as that will keep the code from breaking if you want to just
25 * write a plain PNG file. If you have long comments, I suggest writing
26 * them in png_write_end(), and compressing them.
27 */
28void PNGAPI
29png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
30{
The Android Open Source Project4215dd12009-03-09 11:52:12 -070031 png_debug(1, "in png_write_info_before_PLTE");
The Android Open Source Project893912b2009-03-03 19:30:05 -080032 if (png_ptr == NULL || info_ptr == NULL)
33 return;
34 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
35 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -040036 png_write_sig(png_ptr); /* Write PNG signature */
The Android Open Source Project893912b2009-03-03 19:30:05 -080037#if defined(PNG_MNG_FEATURES_SUPPORTED)
The Android Open Source Project4215dd12009-03-09 11:52:12 -070038 if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE)&&(png_ptr->mng_features_permitted))
The Android Open Source Project893912b2009-03-03 19:30:05 -080039 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -070040 png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
The Android Open Source Project893912b2009-03-03 19:30:05 -080041 png_ptr->mng_features_permitted=0;
42 }
43#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -040044 /* Write IHDR information. */
The Android Open Source Project893912b2009-03-03 19:30:05 -080045 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
46 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
47 info_ptr->filter_type,
48#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
49 info_ptr->interlace_type);
50#else
51 0);
52#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -040053 /* The rest of these check to see if the valid field has the appropriate
54 * flag set, and if it does, writes the chunk.
55 */
The Android Open Source Project893912b2009-03-03 19:30:05 -080056#if defined(PNG_WRITE_gAMA_SUPPORTED)
57 if (info_ptr->valid & PNG_INFO_gAMA)
58 {
59# ifdef PNG_FLOATING_POINT_SUPPORTED
60 png_write_gAMA(png_ptr, info_ptr->gamma);
61#else
62#ifdef PNG_FIXED_POINT_SUPPORTED
63 png_write_gAMA_fixed(png_ptr, info_ptr->int_gamma);
64# endif
65#endif
66 }
67#endif
68#if defined(PNG_WRITE_sRGB_SUPPORTED)
69 if (info_ptr->valid & PNG_INFO_sRGB)
70 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
71#endif
72#if defined(PNG_WRITE_iCCP_SUPPORTED)
73 if (info_ptr->valid & PNG_INFO_iCCP)
74 png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
75 info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
76#endif
77#if defined(PNG_WRITE_sBIT_SUPPORTED)
78 if (info_ptr->valid & PNG_INFO_sBIT)
79 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
80#endif
81#if defined(PNG_WRITE_cHRM_SUPPORTED)
82 if (info_ptr->valid & PNG_INFO_cHRM)
83 {
84#ifdef PNG_FLOATING_POINT_SUPPORTED
85 png_write_cHRM(png_ptr,
86 info_ptr->x_white, info_ptr->y_white,
87 info_ptr->x_red, info_ptr->y_red,
88 info_ptr->x_green, info_ptr->y_green,
89 info_ptr->x_blue, info_ptr->y_blue);
90#else
91# ifdef PNG_FIXED_POINT_SUPPORTED
92 png_write_cHRM_fixed(png_ptr,
93 info_ptr->int_x_white, info_ptr->int_y_white,
94 info_ptr->int_x_red, info_ptr->int_y_red,
95 info_ptr->int_x_green, info_ptr->int_y_green,
96 info_ptr->int_x_blue, info_ptr->int_y_blue);
97# endif
98#endif
99 }
100#endif
101#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
102 if (info_ptr->unknown_chunks_num)
103 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400104 png_unknown_chunk *up;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800105
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400106 png_debug(5, "writing extra chunks");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800107
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400108 for (up = info_ptr->unknown_chunks;
109 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
110 up++)
111 {
The Android Open Source Project893912b2009-03-03 19:30:05 -0800112 int keep=png_handle_as_unknown(png_ptr, up->name);
113 if (keep != PNG_HANDLE_CHUNK_NEVER &&
114 up->location && !(up->location & PNG_HAVE_PLTE) &&
115 !(up->location & PNG_HAVE_IDAT) &&
116 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
117 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
118 {
119 if (up->size == 0)
120 png_warning(png_ptr, "Writing zero-length unknown chunk");
121 png_write_chunk(png_ptr, up->name, up->data, up->size);
122 }
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400123 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800124 }
125#endif
126 png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
127 }
128}
129
130void PNGAPI
131png_write_info(png_structp png_ptr, png_infop info_ptr)
132{
133#if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
134 int i;
135#endif
136
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700137 png_debug(1, "in png_write_info");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800138
139 if (png_ptr == NULL || info_ptr == NULL)
140 return;
141
142 png_write_info_before_PLTE(png_ptr, info_ptr);
143
144 if (info_ptr->valid & PNG_INFO_PLTE)
145 png_write_PLTE(png_ptr, info_ptr->palette,
146 (png_uint_32)info_ptr->num_palette);
147 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
148 png_error(png_ptr, "Valid palette required for paletted images");
149
150#if defined(PNG_WRITE_tRNS_SUPPORTED)
151 if (info_ptr->valid & PNG_INFO_tRNS)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400152 {
The Android Open Source Project893912b2009-03-03 19:30:05 -0800153#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400154 /* Invert the alpha channel (in tRNS) */
155 if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
156 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
157 {
158 int j;
159 for (j=0; j<(int)info_ptr->num_trans; j++)
160 info_ptr->trans[j] = (png_byte)(255 - info_ptr->trans[j]);
161 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800162#endif
163 png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
164 info_ptr->num_trans, info_ptr->color_type);
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400165 }
The Android Open Source Project893912b2009-03-03 19:30:05 -0800166#endif
167#if defined(PNG_WRITE_bKGD_SUPPORTED)
168 if (info_ptr->valid & PNG_INFO_bKGD)
169 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
170#endif
171#if defined(PNG_WRITE_hIST_SUPPORTED)
172 if (info_ptr->valid & PNG_INFO_hIST)
173 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
174#endif
175#if defined(PNG_WRITE_oFFs_SUPPORTED)
176 if (info_ptr->valid & PNG_INFO_oFFs)
177 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
178 info_ptr->offset_unit_type);
179#endif
180#if defined(PNG_WRITE_pCAL_SUPPORTED)
181 if (info_ptr->valid & PNG_INFO_pCAL)
182 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
183 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
184 info_ptr->pcal_units, info_ptr->pcal_params);
185#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400186
187#if defined(PNG_sCAL_SUPPORTED)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800188 if (info_ptr->valid & PNG_INFO_sCAL)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400189#if defined(PNG_WRITE_sCAL_SUPPORTED)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800190#if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
191 png_write_sCAL(png_ptr, (int)info_ptr->scal_unit,
192 info_ptr->scal_pixel_width, info_ptr->scal_pixel_height);
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400193#else /* !FLOATING_POINT */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800194#ifdef PNG_FIXED_POINT_SUPPORTED
195 png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
196 info_ptr->scal_s_width, info_ptr->scal_s_height);
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400197#endif /* FIXED_POINT */
198#endif /* FLOATING_POINT */
199#else /* !WRITE_sCAL */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800200 png_warning(png_ptr,
201 "png_write_sCAL not supported; sCAL chunk not written.");
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400202#endif /* WRITE_sCAL */
203#endif /* sCAL */
204
The Android Open Source Project893912b2009-03-03 19:30:05 -0800205#if defined(PNG_WRITE_pHYs_SUPPORTED)
206 if (info_ptr->valid & PNG_INFO_pHYs)
207 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
208 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400209#endif /* pHYs */
210
The Android Open Source Project893912b2009-03-03 19:30:05 -0800211#if defined(PNG_WRITE_tIME_SUPPORTED)
212 if (info_ptr->valid & PNG_INFO_tIME)
213 {
214 png_write_tIME(png_ptr, &(info_ptr->mod_time));
215 png_ptr->mode |= PNG_WROTE_tIME;
216 }
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400217#endif /* tIME */
218
The Android Open Source Project893912b2009-03-03 19:30:05 -0800219#if defined(PNG_WRITE_sPLT_SUPPORTED)
220 if (info_ptr->valid & PNG_INFO_sPLT)
221 for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
222 png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400223#endif /* sPLT */
224
The Android Open Source Project893912b2009-03-03 19:30:05 -0800225#if defined(PNG_WRITE_TEXT_SUPPORTED)
226 /* Check to see if we need to write text chunks */
227 for (i = 0; i < info_ptr->num_text; i++)
228 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700229 png_debug2(2, "Writing header text chunk %d, type %d", i,
The Android Open Source Project893912b2009-03-03 19:30:05 -0800230 info_ptr->text[i].compression);
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400231 /* An internationalized chunk? */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800232 if (info_ptr->text[i].compression > 0)
233 {
234#if defined(PNG_WRITE_iTXt_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400235 /* Write international chunk */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800236 png_write_iTXt(png_ptr,
237 info_ptr->text[i].compression,
238 info_ptr->text[i].key,
239 info_ptr->text[i].lang,
240 info_ptr->text[i].lang_key,
241 info_ptr->text[i].text);
242#else
243 png_warning(png_ptr, "Unable to write international text");
244#endif
245 /* Mark this chunk as written */
246 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
247 }
248 /* If we want a compressed text chunk */
249 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
250 {
251#if defined(PNG_WRITE_zTXt_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400252 /* Write compressed chunk */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800253 png_write_zTXt(png_ptr, info_ptr->text[i].key,
254 info_ptr->text[i].text, 0,
255 info_ptr->text[i].compression);
256#else
257 png_warning(png_ptr, "Unable to write compressed text");
258#endif
259 /* Mark this chunk as written */
260 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
261 }
262 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
263 {
264#if defined(PNG_WRITE_tEXt_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400265 /* Write uncompressed chunk */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800266 png_write_tEXt(png_ptr, info_ptr->text[i].key,
267 info_ptr->text[i].text,
268 0);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800269 /* Mark this chunk as written */
270 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400271#else
272 /* Can't get here */
273 png_warning(png_ptr, "Unable to write uncompressed text");
274#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800275 }
276 }
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400277#endif /* tEXt */
278
The Android Open Source Project893912b2009-03-03 19:30:05 -0800279#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
280 if (info_ptr->unknown_chunks_num)
281 {
282 png_unknown_chunk *up;
283
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700284 png_debug(5, "writing extra chunks");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800285
286 for (up = info_ptr->unknown_chunks;
287 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
288 up++)
289 {
290 int keep=png_handle_as_unknown(png_ptr, up->name);
291 if (keep != PNG_HANDLE_CHUNK_NEVER &&
292 up->location && (up->location & PNG_HAVE_PLTE) &&
293 !(up->location & PNG_HAVE_IDAT) &&
294 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
295 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
296 {
297 png_write_chunk(png_ptr, up->name, up->data, up->size);
298 }
299 }
300 }
301#endif
302}
303
304/* Writes the end of the PNG file. If you don't want to write comments or
305 * time information, you can pass NULL for info. If you already wrote these
306 * in png_write_info(), do not write them again here. If you have long
307 * comments, I suggest writing them here, and compressing them.
308 */
309void PNGAPI
310png_write_end(png_structp png_ptr, png_infop info_ptr)
311{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700312 png_debug(1, "in png_write_end");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800313 if (png_ptr == NULL)
314 return;
315 if (!(png_ptr->mode & PNG_HAVE_IDAT))
316 png_error(png_ptr, "No IDATs written into file");
317
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400318 /* See if user wants us to write information chunks */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800319 if (info_ptr != NULL)
320 {
321#if defined(PNG_WRITE_TEXT_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400322 int i; /* Local index variable */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800323#endif
324#if defined(PNG_WRITE_tIME_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400325 /* Check to see if user has supplied a time chunk */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800326 if ((info_ptr->valid & PNG_INFO_tIME) &&
327 !(png_ptr->mode & PNG_WROTE_tIME))
328 png_write_tIME(png_ptr, &(info_ptr->mod_time));
329#endif
330#if defined(PNG_WRITE_TEXT_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400331 /* Loop through comment chunks */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800332 for (i = 0; i < info_ptr->num_text; i++)
333 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700334 png_debug2(2, "Writing trailer text chunk %d, type %d", i,
The Android Open Source Project893912b2009-03-03 19:30:05 -0800335 info_ptr->text[i].compression);
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400336 /* An internationalized chunk? */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800337 if (info_ptr->text[i].compression > 0)
338 {
339#if defined(PNG_WRITE_iTXt_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400340 /* Write international chunk */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800341 png_write_iTXt(png_ptr,
342 info_ptr->text[i].compression,
343 info_ptr->text[i].key,
344 info_ptr->text[i].lang,
345 info_ptr->text[i].lang_key,
346 info_ptr->text[i].text);
347#else
348 png_warning(png_ptr, "Unable to write international text");
349#endif
350 /* Mark this chunk as written */
351 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
352 }
353 else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
354 {
355#if defined(PNG_WRITE_zTXt_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400356 /* Write compressed chunk */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800357 png_write_zTXt(png_ptr, info_ptr->text[i].key,
358 info_ptr->text[i].text, 0,
359 info_ptr->text[i].compression);
360#else
361 png_warning(png_ptr, "Unable to write compressed text");
362#endif
363 /* Mark this chunk as written */
364 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
365 }
366 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
367 {
368#if defined(PNG_WRITE_tEXt_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400369 /* Write uncompressed chunk */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800370 png_write_tEXt(png_ptr, info_ptr->text[i].key,
371 info_ptr->text[i].text, 0);
372#else
373 png_warning(png_ptr, "Unable to write uncompressed text");
374#endif
375
376 /* Mark this chunk as written */
377 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
378 }
379 }
380#endif
381#if defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED)
382 if (info_ptr->unknown_chunks_num)
383 {
384 png_unknown_chunk *up;
385
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700386 png_debug(5, "writing extra chunks");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800387
388 for (up = info_ptr->unknown_chunks;
389 up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
390 up++)
391 {
392 int keep=png_handle_as_unknown(png_ptr, up->name);
393 if (keep != PNG_HANDLE_CHUNK_NEVER &&
394 up->location && (up->location & PNG_AFTER_IDAT) &&
395 ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
396 (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
397 {
398 png_write_chunk(png_ptr, up->name, up->data, up->size);
399 }
400 }
401 }
402#endif
403 }
404
405 png_ptr->mode |= PNG_AFTER_IDAT;
406
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400407 /* Write end of PNG file */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800408 png_write_IEND(png_ptr);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700409 /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
410 * and restored again in libpng-1.2.30, may cause some applications that
411 * do not set png_ptr->output_flush_fn to crash. If your application
412 * experiences a problem, please try building libpng with
413 * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
414 * png-mng-implement at lists.sf.net . This kludge will be removed
415 * from libpng-1.4.0.
416 */
417#if defined(PNG_WRITE_FLUSH_SUPPORTED) && \
418 defined(PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED)
419 png_flush(png_ptr);
420#endif
The Android Open Source Project893912b2009-03-03 19:30:05 -0800421}
422
423#if defined(PNG_WRITE_tIME_SUPPORTED)
424#if !defined(_WIN32_WCE)
425/* "time.h" functions are not supported on WindowsCE */
426void PNGAPI
427png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
428{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700429 png_debug(1, "in png_convert_from_struct_tm");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800430 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
431 ptime->month = (png_byte)(ttime->tm_mon + 1);
432 ptime->day = (png_byte)ttime->tm_mday;
433 ptime->hour = (png_byte)ttime->tm_hour;
434 ptime->minute = (png_byte)ttime->tm_min;
435 ptime->second = (png_byte)ttime->tm_sec;
436}
437
438void PNGAPI
439png_convert_from_time_t(png_timep ptime, time_t ttime)
440{
441 struct tm *tbuf;
442
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700443 png_debug(1, "in png_convert_from_time_t");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800444 tbuf = gmtime(&ttime);
445 png_convert_from_struct_tm(ptime, tbuf);
446}
447#endif
448#endif
449
450/* Initialize png_ptr structure, and allocate any memory needed */
451png_structp PNGAPI
452png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
453 png_error_ptr error_fn, png_error_ptr warn_fn)
454{
455#ifdef PNG_USER_MEM_SUPPORTED
456 return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
457 warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
458}
459
460/* Alternate initialize png_ptr structure, and allocate any memory needed */
461png_structp PNGAPI
462png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
463 png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
464 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
465{
466#endif /* PNG_USER_MEM_SUPPORTED */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700467#ifdef PNG_SETJMP_SUPPORTED
468 volatile
469#endif
470 png_structp png_ptr;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800471#ifdef PNG_SETJMP_SUPPORTED
472#ifdef USE_FAR_KEYWORD
473 jmp_buf jmpbuf;
474#endif
475#endif
476 int i;
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700477 png_debug(1, "in png_create_write_struct");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800478#ifdef PNG_USER_MEM_SUPPORTED
479 png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
480 (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
481#else
482 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
483#endif /* PNG_USER_MEM_SUPPORTED */
484 if (png_ptr == NULL)
485 return (NULL);
486
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400487 /* Added at libpng-1.2.6 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800488#ifdef PNG_SET_USER_LIMITS_SUPPORTED
489 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
490 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
491#endif
492
493#ifdef PNG_SETJMP_SUPPORTED
494#ifdef USE_FAR_KEYWORD
495 if (setjmp(jmpbuf))
496#else
497 if (setjmp(png_ptr->jmpbuf))
498#endif
499 {
500 png_free(png_ptr, png_ptr->zbuf);
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700501 png_ptr->zbuf=NULL;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800502 png_destroy_struct(png_ptr);
503 return (NULL);
504 }
505#ifdef USE_FAR_KEYWORD
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700506 png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800507#endif
508#endif
509
510#ifdef PNG_USER_MEM_SUPPORTED
511 png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
512#endif /* PNG_USER_MEM_SUPPORTED */
513 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
514
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700515 if (user_png_ver)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800516 {
517 i=0;
518 do
519 {
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700520 if (user_png_ver[i] != png_libpng_ver[i])
The Android Open Source Project893912b2009-03-03 19:30:05 -0800521 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
522 } while (png_libpng_ver[i++]);
523 }
524
525 if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
526 {
527 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
528 * we must recompile any applications that use any older library version.
529 * For versions after libpng 1.0, we will be compatible, so we need
530 * only check the first digit.
531 */
532 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
533 (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
534 (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
535 {
536#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
537 char msg[80];
538 if (user_png_ver)
539 {
540 png_snprintf(msg, 80,
541 "Application was compiled with png.h from libpng-%.20s",
542 user_png_ver);
543 png_warning(png_ptr, msg);
544 }
545 png_snprintf(msg, 80,
546 "Application is running with png.c from libpng-%.20s",
547 png_libpng_ver);
548 png_warning(png_ptr, msg);
549#endif
550#ifdef PNG_ERROR_NUMBERS_SUPPORTED
551 png_ptr->flags=0;
552#endif
553 png_error(png_ptr,
554 "Incompatible libpng version in application and library");
555 }
556 }
557
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400558 /* Initialize zbuf - compression buffer */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800559 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
560 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
561 (png_uint_32)png_ptr->zbuf_size);
562
563 png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
564 png_flush_ptr_NULL);
565
566#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
567 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
568 1, png_doublep_NULL, png_doublep_NULL);
569#endif
570
571#ifdef PNG_SETJMP_SUPPORTED
572/* Applications that neglect to set up their own setjmp() and then encounter
573 a png_error() will longjmp here. Since the jmpbuf is then meaningless we
574 abort instead of returning. */
575#ifdef USE_FAR_KEYWORD
576 if (setjmp(jmpbuf))
577 PNG_ABORT();
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700578 png_memcpy(png_ptr->jmpbuf, jmpbuf, png_sizeof(jmp_buf));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800579#else
580 if (setjmp(png_ptr->jmpbuf))
581 PNG_ABORT();
582#endif
583#endif
584 return (png_ptr);
585}
586
587/* Initialize png_ptr structure, and allocate any memory needed */
588#if defined(PNG_1_0_X) || defined(PNG_1_2_X)
589/* Deprecated. */
590#undef png_write_init
591void PNGAPI
592png_write_init(png_structp png_ptr)
593{
594 /* We only come here via pre-1.0.7-compiled applications */
595 png_write_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
596}
597
598void PNGAPI
599png_write_init_2(png_structp png_ptr, png_const_charp user_png_ver,
600 png_size_t png_struct_size, png_size_t png_info_size)
601{
602 /* We only come here via pre-1.0.12-compiled applications */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700603 if (png_ptr == NULL) return;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800604#if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700605 if (png_sizeof(png_struct) > png_struct_size ||
The Android Open Source Project893912b2009-03-03 19:30:05 -0800606 png_sizeof(png_info) > png_info_size)
607 {
608 char msg[80];
609 png_ptr->warning_fn=NULL;
610 if (user_png_ver)
611 {
612 png_snprintf(msg, 80,
613 "Application was compiled with png.h from libpng-%.20s",
614 user_png_ver);
615 png_warning(png_ptr, msg);
616 }
617 png_snprintf(msg, 80,
618 "Application is running with png.c from libpng-%.20s",
619 png_libpng_ver);
620 png_warning(png_ptr, msg);
621 }
622#endif
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700623 if (png_sizeof(png_struct) > png_struct_size)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800624 {
625 png_ptr->error_fn=NULL;
626#ifdef PNG_ERROR_NUMBERS_SUPPORTED
627 png_ptr->flags=0;
628#endif
629 png_error(png_ptr,
630 "The png struct allocated by the application for writing is too small.");
631 }
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700632 if (png_sizeof(png_info) > png_info_size)
The Android Open Source Project893912b2009-03-03 19:30:05 -0800633 {
634 png_ptr->error_fn=NULL;
635#ifdef PNG_ERROR_NUMBERS_SUPPORTED
636 png_ptr->flags=0;
637#endif
638 png_error(png_ptr,
639 "The info struct allocated by the application for writing is too small.");
640 }
641 png_write_init_3(&png_ptr, user_png_ver, png_struct_size);
642}
643#endif /* PNG_1_0_X || PNG_1_2_X */
644
645
646void PNGAPI
647png_write_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
648 png_size_t png_struct_size)
649{
650 png_structp png_ptr=*ptr_ptr;
651#ifdef PNG_SETJMP_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400652 jmp_buf tmp_jmp; /* To save current jump buffer */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800653#endif
654
655 int i = 0;
656
657 if (png_ptr == NULL)
658 return;
659
660 do
661 {
662 if (user_png_ver[i] != png_libpng_ver[i])
663 {
664#ifdef PNG_LEGACY_SUPPORTED
665 png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
666#else
667 png_ptr->warning_fn=NULL;
668 png_warning(png_ptr,
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700669 "Application uses deprecated png_write_init() and should be recompiled.");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800670 break;
671#endif
672 }
673 } while (png_libpng_ver[i++]);
674
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700675 png_debug(1, "in png_write_init_3");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800676
677#ifdef PNG_SETJMP_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400678 /* Save jump buffer and error functions */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700679 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800680#endif
681
682 if (png_sizeof(png_struct) > png_struct_size)
683 {
684 png_destroy_struct(png_ptr);
685 png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
686 *ptr_ptr = png_ptr;
687 }
688
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400689 /* Reset all variables to 0 */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700690 png_memset(png_ptr, 0, png_sizeof(png_struct));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800691
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400692 /* Added at libpng-1.2.6 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800693#ifdef PNG_SET_USER_LIMITS_SUPPORTED
694 png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
695 png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
696#endif
697
698#ifdef PNG_SETJMP_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400699 /* Restore jump buffer */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700700 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
The Android Open Source Project893912b2009-03-03 19:30:05 -0800701#endif
702
703 png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
704 png_flush_ptr_NULL);
705
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400706 /* Initialize zbuf - compression buffer */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800707 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
708 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
709 (png_uint_32)png_ptr->zbuf_size);
710
711#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
712 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
713 1, png_doublep_NULL, png_doublep_NULL);
714#endif
715}
716
717/* Write a few rows of image data. If the image is interlaced,
718 * either you will have to write the 7 sub images, or, if you
719 * have called png_set_interlace_handling(), you will have to
720 * "write" the image seven times.
721 */
722void PNGAPI
723png_write_rows(png_structp png_ptr, png_bytepp row,
724 png_uint_32 num_rows)
725{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400726 png_uint_32 i; /* Row counter */
727 png_bytepp rp; /* Row pointer */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800728
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700729 png_debug(1, "in png_write_rows");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800730
731 if (png_ptr == NULL)
732 return;
733
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400734 /* Loop through the rows */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800735 for (i = 0, rp = row; i < num_rows; i++, rp++)
736 {
737 png_write_row(png_ptr, *rp);
738 }
739}
740
741/* Write the image. You only need to call this function once, even
742 * if you are writing an interlaced image.
743 */
744void PNGAPI
745png_write_image(png_structp png_ptr, png_bytepp image)
746{
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400747 png_uint_32 i; /* Row index */
748 int pass, num_pass; /* Pass variables */
749 png_bytepp rp; /* Points to current row */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800750
751 if (png_ptr == NULL)
752 return;
753
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700754 png_debug(1, "in png_write_image");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800755#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400756 /* Initialize interlace handling. If image is not interlaced,
757 * this will set pass to 1
758 */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800759 num_pass = png_set_interlace_handling(png_ptr);
760#else
761 num_pass = 1;
762#endif
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400763 /* Loop through passes */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800764 for (pass = 0; pass < num_pass; pass++)
765 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400766 /* Loop through image */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800767 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
768 {
769 png_write_row(png_ptr, *rp);
770 }
771 }
772}
773
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400774/* Called by user to write a row of image data */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800775void PNGAPI
776png_write_row(png_structp png_ptr, png_bytep row)
777{
778 if (png_ptr == NULL)
779 return;
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700780 png_debug2(1, "in png_write_row (row %ld, pass %d)",
The Android Open Source Project893912b2009-03-03 19:30:05 -0800781 png_ptr->row_number, png_ptr->pass);
782
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400783 /* Initialize transformations and other stuff if first time */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800784 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
785 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400786 /* Make sure we wrote the header info */
787 if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
788 png_error(png_ptr,
789 "png_write_info was never called before png_write_row.");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800790
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400791 /* Check for transforms that have been set but were defined out */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800792#if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400793 if (png_ptr->transformations & PNG_INVERT_MONO)
794 png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800795#endif
796#if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400797 if (png_ptr->transformations & PNG_FILLER)
798 png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800799#endif
800#if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400801 if (png_ptr->transformations & PNG_PACKSWAP)
802 png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800803#endif
804#if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400805 if (png_ptr->transformations & PNG_PACK)
806 png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined.");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800807#endif
808#if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400809 if (png_ptr->transformations & PNG_SHIFT)
810 png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800811#endif
812#if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400813 if (png_ptr->transformations & PNG_BGR)
814 png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined.");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800815#endif
816#if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400817 if (png_ptr->transformations & PNG_SWAP_BYTES)
818 png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800819#endif
820
821 png_write_start_row(png_ptr);
822 }
823
824#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400825 /* If interlaced and not interested in row, return */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800826 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
827 {
828 switch (png_ptr->pass)
829 {
830 case 0:
831 if (png_ptr->row_number & 0x07)
832 {
833 png_write_finish_row(png_ptr);
834 return;
835 }
836 break;
837 case 1:
838 if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
839 {
840 png_write_finish_row(png_ptr);
841 return;
842 }
843 break;
844 case 2:
845 if ((png_ptr->row_number & 0x07) != 4)
846 {
847 png_write_finish_row(png_ptr);
848 return;
849 }
850 break;
851 case 3:
852 if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
853 {
854 png_write_finish_row(png_ptr);
855 return;
856 }
857 break;
858 case 4:
859 if ((png_ptr->row_number & 0x03) != 2)
860 {
861 png_write_finish_row(png_ptr);
862 return;
863 }
864 break;
865 case 5:
866 if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
867 {
868 png_write_finish_row(png_ptr);
869 return;
870 }
871 break;
872 case 6:
873 if (!(png_ptr->row_number & 0x01))
874 {
875 png_write_finish_row(png_ptr);
876 return;
877 }
878 break;
879 }
880 }
881#endif
882
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400883 /* Set up row info for transformations */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800884 png_ptr->row_info.color_type = png_ptr->color_type;
885 png_ptr->row_info.width = png_ptr->usr_width;
886 png_ptr->row_info.channels = png_ptr->usr_channels;
887 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
888 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
889 png_ptr->row_info.channels);
890
891 png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
892 png_ptr->row_info.width);
893
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700894 png_debug1(3, "row_info->color_type = %d", png_ptr->row_info.color_type);
895 png_debug1(3, "row_info->width = %lu", png_ptr->row_info.width);
896 png_debug1(3, "row_info->channels = %d", png_ptr->row_info.channels);
897 png_debug1(3, "row_info->bit_depth = %d", png_ptr->row_info.bit_depth);
898 png_debug1(3, "row_info->pixel_depth = %d", png_ptr->row_info.pixel_depth);
899 png_debug1(3, "row_info->rowbytes = %lu", png_ptr->row_info.rowbytes);
The Android Open Source Project893912b2009-03-03 19:30:05 -0800900
901 /* Copy user's row into buffer, leaving room for filter byte. */
902 png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
903 png_ptr->row_info.rowbytes);
904
905#if defined(PNG_WRITE_INTERLACING_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400906 /* Handle interlacing */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800907 if (png_ptr->interlaced && png_ptr->pass < 6 &&
908 (png_ptr->transformations & PNG_INTERLACE))
909 {
910 png_do_write_interlace(&(png_ptr->row_info),
911 png_ptr->row_buf + 1, png_ptr->pass);
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400912 /* This should always get caught above, but still ... */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800913 if (!(png_ptr->row_info.width))
914 {
915 png_write_finish_row(png_ptr);
916 return;
917 }
918 }
919#endif
920
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400921 /* Handle other transformations */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800922 if (png_ptr->transformations)
923 png_do_write_transformations(png_ptr);
924
925#if defined(PNG_MNG_FEATURES_SUPPORTED)
926 /* Write filter_method 64 (intrapixel differencing) only if
927 * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
928 * 2. Libpng did not write a PNG signature (this filter_method is only
929 * used in PNG datastreams that are embedded in MNG datastreams) and
930 * 3. The application called png_permit_mng_features with a mask that
931 * included PNG_FLAG_MNG_FILTER_64 and
932 * 4. The filter_method is 64 and
933 * 5. The color_type is RGB or RGBA
934 */
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700935 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
The Android Open Source Project893912b2009-03-03 19:30:05 -0800936 (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
937 {
938 /* Intrapixel differencing */
939 png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
940 }
941#endif
942
943 /* Find a filter if necessary, filter the row and write it out. */
944 png_write_find_filter(png_ptr, &(png_ptr->row_info));
945
946 if (png_ptr->write_row_fn != NULL)
947 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
948}
949
950#if defined(PNG_WRITE_FLUSH_SUPPORTED)
951/* Set the automatic flush interval or 0 to turn flushing off */
952void PNGAPI
953png_set_flush(png_structp png_ptr, int nrows)
954{
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700955 png_debug(1, "in png_set_flush");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800956 if (png_ptr == NULL)
957 return;
958 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
959}
960
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400961/* Flush the current output buffers now */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800962void PNGAPI
963png_write_flush(png_structp png_ptr)
964{
965 int wrote_IDAT;
966
The Android Open Source Project4215dd12009-03-09 11:52:12 -0700967 png_debug(1, "in png_write_flush");
The Android Open Source Project893912b2009-03-03 19:30:05 -0800968 if (png_ptr == NULL)
969 return;
970 /* We have already written out all of the data */
971 if (png_ptr->row_number >= png_ptr->num_rows)
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400972 return;
The Android Open Source Project893912b2009-03-03 19:30:05 -0800973
974 do
975 {
976 int ret;
977
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400978 /* Compress the data */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800979 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
980 wrote_IDAT = 0;
981
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400982 /* Check for compression errors */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800983 if (ret != Z_OK)
984 {
985 if (png_ptr->zstream.msg != NULL)
986 png_error(png_ptr, png_ptr->zstream.msg);
987 else
988 png_error(png_ptr, "zlib error");
989 }
990
991 if (!(png_ptr->zstream.avail_out))
992 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -0400993 /* Write the IDAT and reset the zlib output buffer */
The Android Open Source Project893912b2009-03-03 19:30:05 -0800994 png_write_IDAT(png_ptr, png_ptr->zbuf,
995 png_ptr->zbuf_size);
996 png_ptr->zstream.next_out = png_ptr->zbuf;
997 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
998 wrote_IDAT = 1;
999 }
1000 } while(wrote_IDAT == 1);
1001
1002 /* If there is any data left to be output, write it into a new IDAT */
1003 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
1004 {
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001005 /* Write the IDAT and reset the zlib output buffer */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001006 png_write_IDAT(png_ptr, png_ptr->zbuf,
1007 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
1008 png_ptr->zstream.next_out = png_ptr->zbuf;
1009 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
1010 }
1011 png_ptr->flush_rows = 0;
1012 png_flush(png_ptr);
1013}
1014#endif /* PNG_WRITE_FLUSH_SUPPORTED */
1015
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001016/* Free all memory used by the write */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001017void PNGAPI
1018png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
1019{
1020 png_structp png_ptr = NULL;
1021 png_infop info_ptr = NULL;
1022#ifdef PNG_USER_MEM_SUPPORTED
1023 png_free_ptr free_fn = NULL;
1024 png_voidp mem_ptr = NULL;
1025#endif
1026
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001027 png_debug(1, "in png_destroy_write_struct");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001028 if (png_ptr_ptr != NULL)
1029 {
1030 png_ptr = *png_ptr_ptr;
1031#ifdef PNG_USER_MEM_SUPPORTED
1032 free_fn = png_ptr->free_fn;
1033 mem_ptr = png_ptr->mem_ptr;
1034#endif
1035 }
1036
1037#ifdef PNG_USER_MEM_SUPPORTED
1038 if (png_ptr != NULL)
1039 {
1040 free_fn = png_ptr->free_fn;
1041 mem_ptr = png_ptr->mem_ptr;
1042 }
1043#endif
1044
1045 if (info_ptr_ptr != NULL)
1046 info_ptr = *info_ptr_ptr;
1047
1048 if (info_ptr != NULL)
1049 {
1050 if (png_ptr != NULL)
1051 {
1052 png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
1053
1054#if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
1055 if (png_ptr->num_chunk_list)
1056 {
1057 png_free(png_ptr, png_ptr->chunk_list);
1058 png_ptr->chunk_list=NULL;
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001059 png_ptr->num_chunk_list = 0;
The Android Open Source Project893912b2009-03-03 19:30:05 -08001060 }
1061#endif
1062 }
1063
1064#ifdef PNG_USER_MEM_SUPPORTED
1065 png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
1066 (png_voidp)mem_ptr);
1067#else
1068 png_destroy_struct((png_voidp)info_ptr);
1069#endif
1070 *info_ptr_ptr = NULL;
1071 }
1072
1073 if (png_ptr != NULL)
1074 {
1075 png_write_destroy(png_ptr);
1076#ifdef PNG_USER_MEM_SUPPORTED
1077 png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
1078 (png_voidp)mem_ptr);
1079#else
1080 png_destroy_struct((png_voidp)png_ptr);
1081#endif
1082 *png_ptr_ptr = NULL;
1083 }
1084}
1085
1086
1087/* Free any memory used in png_ptr struct (old method) */
1088void /* PRIVATE */
1089png_write_destroy(png_structp png_ptr)
1090{
1091#ifdef PNG_SETJMP_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001092 jmp_buf tmp_jmp; /* Save jump buffer */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001093#endif
1094 png_error_ptr error_fn;
1095 png_error_ptr warning_fn;
1096 png_voidp error_ptr;
1097#ifdef PNG_USER_MEM_SUPPORTED
1098 png_free_ptr free_fn;
1099#endif
1100
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001101 png_debug(1, "in png_write_destroy");
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001102 /* Free any memory zlib uses */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001103 deflateEnd(&png_ptr->zstream);
1104
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001105 /* Free our memory. png_free checks NULL for us. */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001106 png_free(png_ptr, png_ptr->zbuf);
1107 png_free(png_ptr, png_ptr->row_buf);
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001108#ifndef PNG_NO_WRITE_FILTER
The Android Open Source Project893912b2009-03-03 19:30:05 -08001109 png_free(png_ptr, png_ptr->prev_row);
1110 png_free(png_ptr, png_ptr->sub_row);
1111 png_free(png_ptr, png_ptr->up_row);
1112 png_free(png_ptr, png_ptr->avg_row);
1113 png_free(png_ptr, png_ptr->paeth_row);
1114#endif
1115
1116#if defined(PNG_TIME_RFC1123_SUPPORTED)
1117 png_free(png_ptr, png_ptr->time_buffer);
1118#endif
1119
1120#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
1121 png_free(png_ptr, png_ptr->prev_filters);
1122 png_free(png_ptr, png_ptr->filter_weights);
1123 png_free(png_ptr, png_ptr->inv_filter_weights);
1124 png_free(png_ptr, png_ptr->filter_costs);
1125 png_free(png_ptr, png_ptr->inv_filter_costs);
1126#endif
1127
1128#ifdef PNG_SETJMP_SUPPORTED
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001129 /* Reset structure */
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001130 png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof(jmp_buf));
The Android Open Source Project893912b2009-03-03 19:30:05 -08001131#endif
1132
1133 error_fn = png_ptr->error_fn;
1134 warning_fn = png_ptr->warning_fn;
1135 error_ptr = png_ptr->error_ptr;
1136#ifdef PNG_USER_MEM_SUPPORTED
1137 free_fn = png_ptr->free_fn;
1138#endif
1139
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001140 png_memset(png_ptr, 0, png_sizeof(png_struct));
The Android Open Source Project893912b2009-03-03 19:30:05 -08001141
1142 png_ptr->error_fn = error_fn;
1143 png_ptr->warning_fn = warning_fn;
1144 png_ptr->error_ptr = error_ptr;
1145#ifdef PNG_USER_MEM_SUPPORTED
1146 png_ptr->free_fn = free_fn;
1147#endif
1148
1149#ifdef PNG_SETJMP_SUPPORTED
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001150 png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
The Android Open Source Project893912b2009-03-03 19:30:05 -08001151#endif
1152}
1153
1154/* Allow the application to select one or more row filters to use. */
1155void PNGAPI
1156png_set_filter(png_structp png_ptr, int method, int filters)
1157{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001158 png_debug(1, "in png_set_filter");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001159 if (png_ptr == NULL)
1160 return;
1161#if defined(PNG_MNG_FEATURES_SUPPORTED)
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001162 if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
The Android Open Source Project893912b2009-03-03 19:30:05 -08001163 (method == PNG_INTRAPIXEL_DIFFERENCING))
1164 method = PNG_FILTER_TYPE_BASE;
1165#endif
1166 if (method == PNG_FILTER_TYPE_BASE)
1167 {
1168 switch (filters & (PNG_ALL_FILTERS | 0x07))
1169 {
1170#ifndef PNG_NO_WRITE_FILTER
1171 case 5:
1172 case 6:
1173 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
1174#endif /* PNG_NO_WRITE_FILTER */
1175 case PNG_FILTER_VALUE_NONE:
1176 png_ptr->do_filter=PNG_FILTER_NONE; break;
1177#ifndef PNG_NO_WRITE_FILTER
1178 case PNG_FILTER_VALUE_SUB:
1179 png_ptr->do_filter=PNG_FILTER_SUB; break;
1180 case PNG_FILTER_VALUE_UP:
1181 png_ptr->do_filter=PNG_FILTER_UP; break;
1182 case PNG_FILTER_VALUE_AVG:
1183 png_ptr->do_filter=PNG_FILTER_AVG; break;
1184 case PNG_FILTER_VALUE_PAETH:
1185 png_ptr->do_filter=PNG_FILTER_PAETH; break;
1186 default: png_ptr->do_filter = (png_byte)filters; break;
1187#else
1188 default: png_warning(png_ptr, "Unknown row filter for method 0");
1189#endif /* PNG_NO_WRITE_FILTER */
1190 }
1191
1192 /* If we have allocated the row_buf, this means we have already started
1193 * with the image and we should have allocated all of the filter buffers
1194 * that have been selected. If prev_row isn't already allocated, then
1195 * it is too late to start using the filters that need it, since we
1196 * will be missing the data in the previous row. If an application
1197 * wants to start and stop using particular filters during compression,
1198 * it should start out with all of the filters, and then add and
1199 * remove them after the start of compression.
1200 */
1201 if (png_ptr->row_buf != NULL)
1202 {
1203#ifndef PNG_NO_WRITE_FILTER
1204 if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
1205 {
1206 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
1207 (png_ptr->rowbytes + 1));
1208 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
1209 }
1210
1211 if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
1212 {
1213 if (png_ptr->prev_row == NULL)
1214 {
1215 png_warning(png_ptr, "Can't add Up filter after starting");
1216 png_ptr->do_filter &= ~PNG_FILTER_UP;
1217 }
1218 else
1219 {
1220 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
1221 (png_ptr->rowbytes + 1));
1222 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
1223 }
1224 }
1225
1226 if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
1227 {
1228 if (png_ptr->prev_row == NULL)
1229 {
1230 png_warning(png_ptr, "Can't add Average filter after starting");
1231 png_ptr->do_filter &= ~PNG_FILTER_AVG;
1232 }
1233 else
1234 {
1235 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
1236 (png_ptr->rowbytes + 1));
1237 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
1238 }
1239 }
1240
1241 if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
1242 png_ptr->paeth_row == NULL)
1243 {
1244 if (png_ptr->prev_row == NULL)
1245 {
1246 png_warning(png_ptr, "Can't add Paeth filter after starting");
1247 png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
1248 }
1249 else
1250 {
1251 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
1252 (png_ptr->rowbytes + 1));
1253 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
1254 }
1255 }
1256
1257 if (png_ptr->do_filter == PNG_NO_FILTERS)
1258#endif /* PNG_NO_WRITE_FILTER */
1259 png_ptr->do_filter = PNG_FILTER_NONE;
1260 }
1261 }
1262 else
1263 png_error(png_ptr, "Unknown custom filter method");
1264}
1265
1266/* This allows us to influence the way in which libpng chooses the "best"
1267 * filter for the current scanline. While the "minimum-sum-of-absolute-
1268 * differences metric is relatively fast and effective, there is some
1269 * question as to whether it can be improved upon by trying to keep the
1270 * filtered data going to zlib more consistent, hopefully resulting in
1271 * better compression.
1272 */
1273#if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
1274void PNGAPI
1275png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
1276 int num_weights, png_doublep filter_weights,
1277 png_doublep filter_costs)
1278{
1279 int i;
1280
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001281 png_debug(1, "in png_set_filter_heuristics");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001282 if (png_ptr == NULL)
1283 return;
1284 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
1285 {
1286 png_warning(png_ptr, "Unknown filter heuristic method");
1287 return;
1288 }
1289
1290 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
1291 {
1292 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
1293 }
1294
1295 if (num_weights < 0 || filter_weights == NULL ||
1296 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
1297 {
1298 num_weights = 0;
1299 }
1300
1301 png_ptr->num_prev_filters = (png_byte)num_weights;
1302 png_ptr->heuristic_method = (png_byte)heuristic_method;
1303
1304 if (num_weights > 0)
1305 {
1306 if (png_ptr->prev_filters == NULL)
1307 {
1308 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
1309 (png_uint_32)(png_sizeof(png_byte) * num_weights));
1310
1311 /* To make sure that the weighting starts out fairly */
1312 for (i = 0; i < num_weights; i++)
1313 {
1314 png_ptr->prev_filters[i] = 255;
1315 }
1316 }
1317
1318 if (png_ptr->filter_weights == NULL)
1319 {
1320 png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
1321 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
1322
1323 png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
1324 (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
1325 for (i = 0; i < num_weights; i++)
1326 {
1327 png_ptr->inv_filter_weights[i] =
1328 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1329 }
1330 }
1331
1332 for (i = 0; i < num_weights; i++)
1333 {
1334 if (filter_weights[i] < 0.0)
1335 {
1336 png_ptr->inv_filter_weights[i] =
1337 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
1338 }
1339 else
1340 {
1341 png_ptr->inv_filter_weights[i] =
1342 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
1343 png_ptr->filter_weights[i] =
1344 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
1345 }
1346 }
1347 }
1348
1349 /* If, in the future, there are other filter methods, this would
1350 * need to be based on png_ptr->filter.
1351 */
1352 if (png_ptr->filter_costs == NULL)
1353 {
1354 png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
1355 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
1356
1357 png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
1358 (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
1359
1360 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1361 {
1362 png_ptr->inv_filter_costs[i] =
1363 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1364 }
1365 }
1366
1367 /* Here is where we set the relative costs of the different filters. We
1368 * should take the desired compression level into account when setting
1369 * the costs, so that Paeth, for instance, has a high relative cost at low
1370 * compression levels, while it has a lower relative cost at higher
1371 * compression settings. The filter types are in order of increasing
1372 * relative cost, so it would be possible to do this with an algorithm.
1373 */
1374 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
1375 {
1376 if (filter_costs == NULL || filter_costs[i] < 0.0)
1377 {
1378 png_ptr->inv_filter_costs[i] =
1379 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
1380 }
1381 else if (filter_costs[i] >= 1.0)
1382 {
1383 png_ptr->inv_filter_costs[i] =
1384 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
1385 png_ptr->filter_costs[i] =
1386 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
1387 }
1388 }
1389}
1390#endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
1391
1392void PNGAPI
1393png_set_compression_level(png_structp png_ptr, int level)
1394{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001395 png_debug(1, "in png_set_compression_level");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001396 if (png_ptr == NULL)
1397 return;
1398 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
1399 png_ptr->zlib_level = level;
1400}
1401
1402void PNGAPI
1403png_set_compression_mem_level(png_structp png_ptr, int mem_level)
1404{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001405 png_debug(1, "in png_set_compression_mem_level");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001406 if (png_ptr == NULL)
1407 return;
1408 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
1409 png_ptr->zlib_mem_level = mem_level;
1410}
1411
1412void PNGAPI
1413png_set_compression_strategy(png_structp png_ptr, int strategy)
1414{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001415 png_debug(1, "in png_set_compression_strategy");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001416 if (png_ptr == NULL)
1417 return;
1418 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
1419 png_ptr->zlib_strategy = strategy;
1420}
1421
1422void PNGAPI
1423png_set_compression_window_bits(png_structp png_ptr, int window_bits)
1424{
1425 if (png_ptr == NULL)
1426 return;
1427 if (window_bits > 15)
1428 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
1429 else if (window_bits < 8)
1430 png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
1431#ifndef WBITS_8_OK
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001432 /* Avoid libpng bug with 256-byte windows */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001433 if (window_bits == 8)
1434 {
1435 png_warning(png_ptr, "Compression window is being reset to 512");
1436 window_bits=9;
1437 }
1438#endif
1439 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
1440 png_ptr->zlib_window_bits = window_bits;
1441}
1442
1443void PNGAPI
1444png_set_compression_method(png_structp png_ptr, int method)
1445{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001446 png_debug(1, "in png_set_compression_method");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001447 if (png_ptr == NULL)
1448 return;
1449 if (method != 8)
1450 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
1451 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
1452 png_ptr->zlib_method = method;
1453}
1454
1455void PNGAPI
1456png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
1457{
1458 if (png_ptr == NULL)
1459 return;
1460 png_ptr->write_row_fn = write_row_fn;
1461}
1462
1463#if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
1464void PNGAPI
1465png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
1466 write_user_transform_fn)
1467{
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001468 png_debug(1, "in png_set_write_user_transform_fn");
The Android Open Source Project893912b2009-03-03 19:30:05 -08001469 if (png_ptr == NULL)
1470 return;
1471 png_ptr->transformations |= PNG_USER_TRANSFORM;
1472 png_ptr->write_user_transform_fn = write_user_transform_fn;
1473}
1474#endif
1475
1476
1477#if defined(PNG_INFO_IMAGE_SUPPORTED)
1478void PNGAPI
1479png_write_png(png_structp png_ptr, png_infop info_ptr,
1480 int transforms, voidp params)
1481{
1482 if (png_ptr == NULL || info_ptr == NULL)
1483 return;
1484#if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001485 /* Invert the alpha channel from opacity to transparency */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001486 if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001487 png_set_invert_alpha(png_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001488#endif
1489
1490 /* Write the file header information. */
1491 png_write_info(png_ptr, info_ptr);
1492
1493 /* ------ these transformations don't touch the info structure ------- */
1494
1495#if defined(PNG_WRITE_INVERT_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001496 /* Invert monochrome pixels */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001497 if (transforms & PNG_TRANSFORM_INVERT_MONO)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001498 png_set_invert_mono(png_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001499#endif
1500
1501#if defined(PNG_WRITE_SHIFT_SUPPORTED)
1502 /* Shift the pixels up to a legal bit depth and fill in
1503 * as appropriate to correctly scale the image.
1504 */
1505 if ((transforms & PNG_TRANSFORM_SHIFT)
1506 && (info_ptr->valid & PNG_INFO_sBIT))
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001507 png_set_shift(png_ptr, &info_ptr->sig_bit);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001508#endif
1509
1510#if defined(PNG_WRITE_PACK_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001511 /* Pack pixels into bytes */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001512 if (transforms & PNG_TRANSFORM_PACKING)
1513 png_set_packing(png_ptr);
1514#endif
1515
1516#if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001517 /* Swap location of alpha bytes from ARGB to RGBA */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001518 if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001519 png_set_swap_alpha(png_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001520#endif
1521
1522#if defined(PNG_WRITE_FILLER_SUPPORTED)
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001523 /* Pack XRGB/RGBX/ARGB/RGBA into * RGB (4 channels -> 3 channels) */
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001524 if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001525 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001526 else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
The Android Open Source Project4215dd12009-03-09 11:52:12 -07001527 png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001528#endif
1529
1530#if defined(PNG_WRITE_BGR_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001531 /* Flip BGR pixels to RGB */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001532 if (transforms & PNG_TRANSFORM_BGR)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001533 png_set_bgr(png_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001534#endif
1535
1536#if defined(PNG_WRITE_SWAP_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001537 /* Swap bytes of 16-bit files to most significant byte first */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001538 if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001539 png_set_swap(png_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001540#endif
1541
1542#if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001543 /* Swap bits of 1, 2, 4 bit packed pixel formats */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001544 if (transforms & PNG_TRANSFORM_PACKSWAP)
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001545 png_set_packswap(png_ptr);
The Android Open Source Project893912b2009-03-03 19:30:05 -08001546#endif
1547
1548 /* ----------------------- end of transformations ------------------- */
1549
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001550 /* Write the bits */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001551 if (info_ptr->valid & PNG_INFO_IDAT)
1552 png_write_image(png_ptr, info_ptr->row_pointers);
1553
1554 /* It is REQUIRED to call this to finish writing the rest of the file */
1555 png_write_end(png_ptr, info_ptr);
1556
Patrick Scotta0bb96c2009-07-22 11:50:02 -04001557 transforms = transforms; /* Quiet compiler warnings */
The Android Open Source Project893912b2009-03-03 19:30:05 -08001558 params = params;
1559}
1560#endif
1561#endif /* PNG_WRITE_SUPPORTED */