]> git.saurik.com Git - wxWidgets.git/blob - src/png/pngwrite.c
Mods for Gnu-Win32
[wxWidgets.git] / src / png / pngwrite.c
1
2 /* pngwrite.c - general routines to write a PNG file
3 *
4 * libpng 1.0.1
5 * For conditions of distribution and use, see copyright notice in png.h
6 * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
7 * Copyright (c) 1996, 1997 Andreas Dilger
8 * Copyright (c) 1998, Glenn Randers-Pehrson
9 * March 15, 1998
10 */
11
12 /* get internal access to png.h */
13 #define PNG_INTERNAL
14 #include "../png/png.h"
15
16 /* Writes all the PNG information. This is the suggested way to use the
17 * library. If you have a new chunk to add, make a function to write it,
18 * and put it in the correct location here. If you want the chunk written
19 * after the image data, put it in png_write_end(). I strongly encurage
20 * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
21 * the chunk, as that will keep the code from breaking if you want to just
22 * write a plain PNG file. If you have long comments, I suggest writing
23 * them in png_write_end(), and compressing them.
24 */
25 void
26 png_write_info(png_structp png_ptr, png_infop info_ptr)
27 {
28 #if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
29 int i;
30 #endif
31
32 png_debug(1, "in png_write_info\n");
33 png_write_sig(png_ptr); /* write PNG signature */
34 /* write IHDR information. */
35 png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
36 info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
37 info_ptr->filter_type,
38 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
39 info_ptr->interlace_type);
40 #else
41 0);
42 #endif
43 /* the rest of these check to see if the valid field has the appropriate
44 flag set, and if it does, writes the chunk. */
45 #if defined(PNG_WRITE_gAMA_SUPPORTED)
46 if (info_ptr->valid & PNG_INFO_gAMA)
47 png_write_gAMA(png_ptr, info_ptr->gamma);
48 #endif
49 #if defined(PNG_WRITE_sRGB_SUPPORTED)
50 if (info_ptr->valid & PNG_INFO_sRGB)
51 png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
52 #endif
53 #if defined(PNG_WRITE_sBIT_SUPPORTED)
54 if (info_ptr->valid & PNG_INFO_sBIT)
55 png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
56 #endif
57 #if defined(PNG_WRITE_cHRM_SUPPORTED)
58 if (info_ptr->valid & PNG_INFO_cHRM)
59 png_write_cHRM(png_ptr,
60 info_ptr->x_white, info_ptr->y_white,
61 info_ptr->x_red, info_ptr->y_red,
62 info_ptr->x_green, info_ptr->y_green,
63 info_ptr->x_blue, info_ptr->y_blue);
64 #endif
65 if (info_ptr->valid & PNG_INFO_PLTE)
66 png_write_PLTE(png_ptr, info_ptr->palette,
67 (png_uint_32)info_ptr->num_palette);
68 else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
69 png_error(png_ptr, "Valid palette required for paletted images\n");
70
71 #if defined(PNG_WRITE_tRNS_SUPPORTED)
72 if (info_ptr->valid & PNG_INFO_tRNS)
73 {
74 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
75 /* invert the alpha channel (in tRNS) */
76 if (png_ptr->transformations & PNG_INVERT_ALPHA &&
77 info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
78 {
79 int j;
80 for (j=0; j<(int)info_ptr->num_trans; j++)
81 info_ptr->trans[j] = 255 - info_ptr->trans[j];
82 }
83 #endif
84 png_write_tRNS(png_ptr, info_ptr->trans, &(info_ptr->trans_values),
85 info_ptr->num_trans, info_ptr->color_type);
86 }
87 #endif
88 #if defined(PNG_WRITE_bKGD_SUPPORTED)
89 if (info_ptr->valid & PNG_INFO_bKGD)
90 png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
91 #endif
92 #if defined(PNG_WRITE_hIST_SUPPORTED)
93 if (info_ptr->valid & PNG_INFO_hIST)
94 png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
95 #endif
96 #if defined(PNG_WRITE_oFFs_SUPPORTED)
97 if (info_ptr->valid & PNG_INFO_oFFs)
98 png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
99 info_ptr->offset_unit_type);
100 #endif
101 #if defined(PNG_WRITE_pCAL_SUPPORTED)
102 if (info_ptr->valid & PNG_INFO_pCAL)
103 png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
104 info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
105 info_ptr->pcal_units, info_ptr->pcal_params);
106 #endif
107 #if defined(PNG_WRITE_pHYs_SUPPORTED)
108 if (info_ptr->valid & PNG_INFO_pHYs)
109 png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
110 info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
111 #endif
112 #if defined(PNG_WRITE_tIME_SUPPORTED)
113 if (info_ptr->valid & PNG_INFO_tIME)
114 {
115 png_write_tIME(png_ptr, &(info_ptr->mod_time));
116 png_ptr->flags |= PNG_FLAG_WROTE_tIME;
117 }
118 #endif
119 #if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
120 /* Check to see if we need to write text chunks */
121 for (i = 0; i < info_ptr->num_text; i++)
122 {
123 png_debug2(2, "Writing header text chunk %d, type %d\n", i,
124 info_ptr->text[i].compression);
125 /* If we want a compressed text chunk */
126 if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
127 {
128 #if defined(PNG_WRITE_zTXt_SUPPORTED)
129 /* write compressed chunk */
130 png_write_zTXt(png_ptr, info_ptr->text[i].key,
131 info_ptr->text[i].text, info_ptr->text[i].text_length,
132 info_ptr->text[i].compression);
133 #else
134 png_warning(png_ptr, "Unable to write compressed text\n");
135 #endif
136 /* Mark this chunk as written */
137 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
138 }
139 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
140 {
141 #if defined(PNG_WRITE_tEXt_SUPPORTED)
142 /* write uncompressed chunk */
143 png_write_tEXt(png_ptr, info_ptr->text[i].key,
144 info_ptr->text[i].text, info_ptr->text[i].text_length);
145 #else
146 png_warning(png_ptr, "Unable to write uncompressed text\n");
147 #endif
148 /* Mark this chunk as written */
149 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
150 }
151 }
152 #endif
153 }
154
155 /* Writes the end of the PNG file. If you don't want to write comments or
156 * time information, you can pass NULL for info. If you already wrote these
157 * in png_write_info(), do not write them again here. If you have long
158 * comments, I suggest writing them here, and compressing them.
159 */
160 void
161 png_write_end(png_structp png_ptr, png_infop info_ptr)
162 {
163 png_debug(1, "in png_write_end\n");
164 if (!(png_ptr->mode & PNG_HAVE_IDAT))
165 png_error(png_ptr, "No IDATs written into file");
166
167 /* see if user wants us to write information chunks */
168 if (info_ptr != NULL)
169 {
170 #if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
171 int i; /* local index variable */
172 #endif
173 #if defined(PNG_WRITE_tIME_SUPPORTED)
174 /* check to see if user has supplied a time chunk */
175 if (info_ptr->valid & PNG_INFO_tIME &&
176 !(png_ptr->flags & PNG_FLAG_WROTE_tIME))
177 png_write_tIME(png_ptr, &(info_ptr->mod_time));
178 #endif
179 #if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
180 /* loop through comment chunks */
181 for (i = 0; i < info_ptr->num_text; i++)
182 {
183 png_debug2(2, "Writing trailer text chunk %d, type %d\n", i,
184 info_ptr->text[i].compression);
185 if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
186 {
187 #if defined(PNG_WRITE_zTXt_SUPPORTED)
188 /* write compressed chunk */
189 png_write_zTXt(png_ptr, info_ptr->text[i].key,
190 info_ptr->text[i].text, info_ptr->text[i].text_length,
191 info_ptr->text[i].compression);
192 #else
193 png_warning(png_ptr, "Unable to write compressed text\n");
194 #endif
195 /* Mark this chunk as written */
196 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
197 }
198 else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
199 {
200 #if defined(PNG_WRITE_tEXt_SUPPORTED)
201 /* write uncompressed chunk */
202 png_write_tEXt(png_ptr, info_ptr->text[i].key,
203 info_ptr->text[i].text, info_ptr->text[i].text_length);
204 #else
205 png_warning(png_ptr, "Unable to write uncompressed text\n");
206 #endif
207
208 /* Mark this chunk as written */
209 info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
210 }
211 }
212 #endif
213 }
214
215 png_ptr->mode |= PNG_AFTER_IDAT;
216
217 /* write end of PNG file */
218 png_write_IEND(png_ptr);
219 }
220
221 #if defined(PNG_TIME_RFC1123_SUPPORTED)
222 /* Convert the supplied time into an RFC 1123 string suitable for use in
223 * a "Creation Time" or other text-based time string.
224 */
225 png_charp
226 png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
227 {
228 static PNG_CONST char short_months[12][4] =
229 {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
230 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
231
232 if (png_ptr->time_buffer == NULL)
233 {
234 png_ptr->time_buffer = (png_charp)png_malloc(png_ptr, (png_uint_32)(29*
235 sizeof(char)));
236 }
237
238 #ifdef USE_FAR_KEYWORD
239 {
240 char near_time_buf[29];
241 sprintf(near_time_buf, "%d %s %d %02d:%02d:%02d +0000",
242 ptime->day % 31, short_months[ptime->month - 1],
243 ptime->year, ptime->hour % 24, ptime->minute % 60,
244 ptime->second % 61);
245 png_memcpy(png_ptr->time_buffer, near_time_buf,
246 29*sizeof(char));
247 }
248 #else
249 sprintf(png_ptr->time_buffer, "%d %s %d %02d:%02d:%02d +0000",
250 ptime->day % 31, short_months[ptime->month - 1],
251 ptime->year, ptime->hour % 24, ptime->minute % 60,
252 ptime->second % 61);
253 #endif
254 return ((png_charp)png_ptr->time_buffer);
255 }
256 #endif /* PNG_TIME_RFC1123_SUPPORTED */
257
258 #if defined(PNG_WRITE_tIME_SUPPORTED)
259 void
260 png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
261 {
262 png_debug(1, "in png_convert_from_struct_tm\n");
263 ptime->year = (png_uint_16)(1900 + ttime->tm_year);
264 ptime->month = (png_byte)(ttime->tm_mon + 1);
265 ptime->day = (png_byte)ttime->tm_mday;
266 ptime->hour = (png_byte)ttime->tm_hour;
267 ptime->minute = (png_byte)ttime->tm_min;
268 ptime->second = (png_byte)ttime->tm_sec;
269 }
270
271 void
272 png_convert_from_time_t(png_timep ptime, time_t ttime)
273 {
274 struct tm *tbuf;
275
276 png_debug(1, "in png_convert_from_time_t\n");
277 tbuf = gmtime(&ttime);
278 png_convert_from_struct_tm(ptime, tbuf);
279 }
280 #endif
281
282 /* Initialize png_ptr structure, and allocate any memory needed */
283 png_structp
284 png_create_write_struct(png_const_charp user_png_ver, voidp error_ptr,
285 png_error_ptr error_fn, png_error_ptr warn_fn)
286 {
287 png_structp png_ptr;
288 #ifdef USE_FAR_KEYWORD
289 jmp_buf jmpbuf;
290 #endif
291 png_debug(1, "in png_create_write_struct\n");
292 if ((png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG)) == NULL)
293 {
294 return ((png_structp)NULL);
295 }
296 #ifdef USE_FAR_KEYWORD
297 if (setjmp(jmpbuf))
298 #else
299 if (setjmp(png_ptr->jmpbuf))
300 #endif
301 {
302 png_free(png_ptr, png_ptr->zbuf);
303 png_destroy_struct(png_ptr);
304 return ((png_structp)NULL);
305 }
306 #ifdef USE_FAR_KEYWORD
307 png_memcpy(png_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
308 #endif
309 png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
310
311 /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
312 * we must recompile any applications that use any older library version.
313 * For versions after libpng 1.0, we will be compatible, so we need
314 * only check the first digit.
315 */
316 if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
317 (png_libpng_ver[0] == '0' && user_png_ver[2] < '9'))
318 {
319 png_error(png_ptr,
320 "Incompatible libpng version in application and library");
321 }
322
323 /* initialize zbuf - compression buffer */
324 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
325 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
326 (png_uint_32)png_ptr->zbuf_size);
327
328 png_set_write_fn(png_ptr, NULL, NULL, NULL);
329
330 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
331 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
332 1, NULL, NULL);
333 #endif
334
335 return ((png_structp)png_ptr);
336 }
337
338
339 /* Initialize png_ptr structure, and allocate any memory needed */
340 void
341 png_write_init(png_structp png_ptr)
342 {
343 jmp_buf tmp_jmp; /* to save current jump buffer */
344
345 png_debug(1, "in png_write_init\n");
346 /* save jump buffer and error functions */
347 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
348
349 /* reset all variables to 0 */
350 png_memset(png_ptr, 0, sizeof (png_struct));
351
352 /* restore jump buffer */
353 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
354
355 /* initialize zbuf - compression buffer */
356 png_ptr->zbuf_size = PNG_ZBUF_SIZE;
357 png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
358 (png_uint_32)png_ptr->zbuf_size);
359 png_set_write_fn(png_ptr, NULL, NULL, NULL);
360
361 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
362 png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
363 1, NULL, NULL);
364 #endif
365 }
366
367 /* Write a few rows of image data. If the image is interlaced,
368 * either you will have to write the 7 sub images, or, if you
369 * have called png_set_interlace_handling(), you will have to
370 * "write" the image seven times.
371 */
372 void
373 png_write_rows(png_structp png_ptr, png_bytepp row,
374 png_uint_32 num_rows)
375 {
376 png_uint_32 i; /* row counter */
377 png_bytepp rp; /* row pointer */
378
379 png_debug(1, "in png_write_rows\n");
380 /* loop through the rows */
381 for (i = 0, rp = row; i < num_rows; i++, rp++)
382 {
383 png_write_row(png_ptr, *rp);
384 }
385 }
386
387 /* Write the image. You only need to call this function once, even
388 * if you are writing an interlaced image.
389 */
390 void
391 png_write_image(png_structp png_ptr, png_bytepp image)
392 {
393 png_uint_32 i; /* row index */
394 int pass, num_pass; /* pass variables */
395 png_bytepp rp; /* points to current row */
396
397 png_debug(1, "in png_write_image\n");
398 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
399 /* intialize interlace handling. If image is not interlaced,
400 this will set pass to 1 */
401 num_pass = png_set_interlace_handling(png_ptr);
402 #else
403 num_pass = 1;
404 #endif
405 /* loop through passes */
406 for (pass = 0; pass < num_pass; pass++)
407 {
408 /* loop through image */
409 for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
410 {
411 png_write_row(png_ptr, *rp);
412 }
413 }
414 }
415
416 /* called by user to write a row of image data */
417 void
418 png_write_row(png_structp png_ptr, png_bytep row)
419 {
420 png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
421 png_ptr->row_number, png_ptr->pass);
422 /* initialize transformations and other stuff if first time */
423 if (png_ptr->row_number == 0 && png_ptr->pass == 0)
424 {
425 png_write_start_row(png_ptr);
426 }
427
428 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
429 /* if interlaced and not interested in row, return */
430 if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
431 {
432 switch (png_ptr->pass)
433 {
434 case 0:
435 if (png_ptr->row_number & 7)
436 {
437 png_write_finish_row(png_ptr);
438 return;
439 }
440 break;
441 case 1:
442 if ((png_ptr->row_number & 7) || png_ptr->width < 5)
443 {
444 png_write_finish_row(png_ptr);
445 return;
446 }
447 break;
448 case 2:
449 if ((png_ptr->row_number & 7) != 4)
450 {
451 png_write_finish_row(png_ptr);
452 return;
453 }
454 break;
455 case 3:
456 if ((png_ptr->row_number & 3) || png_ptr->width < 3)
457 {
458 png_write_finish_row(png_ptr);
459 return;
460 }
461 break;
462 case 4:
463 if ((png_ptr->row_number & 3) != 2)
464 {
465 png_write_finish_row(png_ptr);
466 return;
467 }
468 break;
469 case 5:
470 if ((png_ptr->row_number & 1) || png_ptr->width < 2)
471 {
472 png_write_finish_row(png_ptr);
473 return;
474 }
475 break;
476 case 6:
477 if (!(png_ptr->row_number & 1))
478 {
479 png_write_finish_row(png_ptr);
480 return;
481 }
482 break;
483 }
484 }
485 #endif
486
487 /* set up row info for transformations */
488 png_ptr->row_info.color_type = png_ptr->color_type;
489 png_ptr->row_info.width = png_ptr->usr_width;
490 png_ptr->row_info.channels = png_ptr->usr_channels;
491 png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
492 png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
493 png_ptr->row_info.channels);
494
495 png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
496 (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
497
498 png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
499 png_debug1(3, "row_info->width = %d\n", png_ptr->row_info.width);
500 png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
501 png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
502 png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
503 png_debug1(3, "row_info->rowbytes = %d\n", png_ptr->row_info.rowbytes);
504
505 /* Copy user's row into buffer, leaving room for filter byte. */
506 png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
507 png_ptr->row_info.rowbytes);
508
509 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
510 /* handle interlacing */
511 if (png_ptr->interlaced && png_ptr->pass < 6 &&
512 (png_ptr->transformations & PNG_INTERLACE))
513 {
514 png_do_write_interlace(&(png_ptr->row_info),
515 png_ptr->row_buf + 1, png_ptr->pass);
516 /* this should always get caught above, but still ... */
517 if (!(png_ptr->row_info.width))
518 {
519 png_write_finish_row(png_ptr);
520 return;
521 }
522 }
523 #endif
524
525 /* handle other transformations */
526 if (png_ptr->transformations)
527 png_do_write_transformations(png_ptr);
528
529 /* Find a filter if necessary, filter the row and write it out. */
530 png_write_find_filter(png_ptr, &(png_ptr->row_info));
531
532 if (png_ptr->write_row_fn != NULL)
533 (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
534 }
535
536 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
537 /* Set the automatic flush interval or 0 to turn flushing off */
538 void
539 png_set_flush(png_structp png_ptr, int nrows)
540 {
541 png_debug(1, "in png_set_flush\n");
542 png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
543 }
544
545 /* flush the current output buffers now */
546 void
547 png_write_flush(png_structp png_ptr)
548 {
549 int wrote_IDAT;
550
551 png_debug(1, "in png_write_flush\n");
552 /* We have already written out all of the data */
553 if (png_ptr->row_number >= png_ptr->num_rows)
554 return;
555
556 do
557 {
558 int ret;
559
560 /* compress the data */
561 ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
562 wrote_IDAT = 0;
563
564 /* check for compression errors */
565 if (ret != Z_OK)
566 {
567 if (png_ptr->zstream.msg != NULL)
568 png_error(png_ptr, png_ptr->zstream.msg);
569 else
570 png_error(png_ptr, "zlib error");
571 }
572
573 if (!(png_ptr->zstream.avail_out))
574 {
575 /* write the IDAT and reset the zlib output buffer */
576 png_write_IDAT(png_ptr, png_ptr->zbuf,
577 png_ptr->zbuf_size);
578 png_ptr->zstream.next_out = png_ptr->zbuf;
579 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
580 wrote_IDAT = 1;
581 }
582 } while(wrote_IDAT == 1);
583
584 /* If there is any data left to be output, write it into a new IDAT */
585 if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
586 {
587 /* write the IDAT and reset the zlib output buffer */
588 png_write_IDAT(png_ptr, png_ptr->zbuf,
589 png_ptr->zbuf_size - png_ptr->zstream.avail_out);
590 png_ptr->zstream.next_out = png_ptr->zbuf;
591 png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
592 }
593 png_ptr->flush_rows = 0;
594 png_flush(png_ptr);
595 }
596 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
597
598 /* free all memory used by the write */
599 void
600 png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
601 {
602 png_structp png_ptr = NULL;
603 png_infop info_ptr = NULL;
604
605 png_debug(1, "in png_destroy_write_struct\n");
606 if (png_ptr_ptr != NULL)
607 png_ptr = *png_ptr_ptr;
608
609 if (info_ptr_ptr != NULL)
610 info_ptr = *info_ptr_ptr;
611
612 if (info_ptr != NULL)
613 {
614 #ifdef PNG_WRITE_tEXt_SUPPORTED
615 png_free(png_ptr, info_ptr->text);
616 #endif
617 #if defined(PNG_READ_pCAL_SUPPORTED)
618 png_free(png_ptr, info_ptr->pcal_purpose);
619 png_free(png_ptr, info_ptr->pcal_units);
620 if (info_ptr->pcal_params != NULL)
621 {
622 int i;
623 for (i = 0; i < (int)info_ptr->pcal_nparams; i++)
624 {
625 png_free(png_ptr, info_ptr->pcal_params[i]);
626 }
627 png_free(png_ptr, info_ptr->pcal_params);
628 }
629 #endif
630 png_destroy_struct((png_voidp)info_ptr);
631 *info_ptr_ptr = (png_infop)NULL;
632 }
633
634 if (png_ptr != NULL)
635 {
636 png_write_destroy(png_ptr);
637 png_destroy_struct((png_voidp)png_ptr);
638 *png_ptr_ptr = (png_structp)NULL;
639 }
640 }
641
642
643 /* Free any memory used in png_ptr struct (old method) */
644 void
645 png_write_destroy(png_structp png_ptr)
646 {
647 jmp_buf tmp_jmp; /* save jump buffer */
648 png_error_ptr error_fn;
649 png_error_ptr warning_fn;
650 png_voidp error_ptr;
651
652 png_debug(1, "in png_write_destroy\n");
653 /* free any memory zlib uses */
654 deflateEnd(&png_ptr->zstream);
655
656 /* free our memory. png_free checks NULL for us. */
657 png_free(png_ptr, png_ptr->zbuf);
658 png_free(png_ptr, png_ptr->row_buf);
659 png_free(png_ptr, png_ptr->prev_row);
660 png_free(png_ptr, png_ptr->sub_row);
661 png_free(png_ptr, png_ptr->up_row);
662 png_free(png_ptr, png_ptr->avg_row);
663 png_free(png_ptr, png_ptr->paeth_row);
664 #if defined(PNG_TIME_RFC1123_SUPPORTED)
665 png_free(png_ptr, png_ptr->time_buffer);
666 #endif /* PNG_TIME_RFC1123_SUPPORTED */
667 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
668 png_free(png_ptr, png_ptr->prev_filters);
669 png_free(png_ptr, png_ptr->filter_weights);
670 png_free(png_ptr, png_ptr->inv_filter_weights);
671 png_free(png_ptr, png_ptr->filter_costs);
672 png_free(png_ptr, png_ptr->inv_filter_costs);
673 #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
674
675 /* reset structure */
676 png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
677
678 error_fn = png_ptr->error_fn;
679 warning_fn = png_ptr->warning_fn;
680 error_ptr = png_ptr->error_ptr;
681
682 png_memset(png_ptr, 0, sizeof (png_struct));
683
684 png_ptr->error_fn = error_fn;
685 png_ptr->warning_fn = warning_fn;
686 png_ptr->error_ptr = error_ptr;
687
688 png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
689 }
690
691 /* Allow the application to select one or more row filters to use. */
692 void
693 png_set_filter(png_structp png_ptr, int method, int filters)
694 {
695 png_debug(1, "in png_set_filter\n");
696 /* We allow 'method' only for future expansion of the base filter method. */
697 if (method == PNG_FILTER_TYPE_BASE)
698 {
699 switch (filters & (PNG_ALL_FILTERS | 0x07))
700 {
701 case 5:
702 case 6:
703 case 7: png_warning(png_ptr, "Unknown row filter for method 0");
704 case PNG_FILTER_VALUE_NONE: png_ptr->do_filter=PNG_FILTER_NONE; break;
705 case PNG_FILTER_VALUE_SUB: png_ptr->do_filter=PNG_FILTER_SUB; break;
706 case PNG_FILTER_VALUE_UP: png_ptr->do_filter=PNG_FILTER_UP; break;
707 case PNG_FILTER_VALUE_AVG: png_ptr->do_filter=PNG_FILTER_AVG; break;
708 case PNG_FILTER_VALUE_PAETH: png_ptr->do_filter=PNG_FILTER_PAETH;break;
709 default: png_ptr->do_filter = (png_byte)filters; break;
710 }
711
712 /* If we have allocated the row_buf, this means we have already started
713 * with the image and we should have allocated all of the filter buffers
714 * that have been selected. If prev_row isn't already allocated, then
715 * it is too late to start using the filters that need it, since we
716 * will be missing the data in the previous row. If an application
717 * wants to start and stop using particular filters during compression,
718 * it should start out with all of the filters, and then add and
719 * remove them after the start of compression.
720 */
721 if (png_ptr->row_buf != NULL)
722 {
723 if (png_ptr->do_filter & PNG_FILTER_SUB && png_ptr->sub_row == NULL)
724 {
725 png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
726 (png_ptr->rowbytes + 1));
727 png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
728 }
729
730 if (png_ptr->do_filter & PNG_FILTER_UP && png_ptr->up_row == NULL)
731 {
732 if (png_ptr->prev_row == NULL)
733 {
734 png_warning(png_ptr, "Can't add Up filter after starting");
735 png_ptr->do_filter &= ~PNG_FILTER_UP;
736 }
737 else
738 {
739 png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
740 (png_ptr->rowbytes + 1));
741 png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
742 }
743 }
744
745 if (png_ptr->do_filter & PNG_FILTER_AVG && png_ptr->avg_row == NULL)
746 {
747 if (png_ptr->prev_row == NULL)
748 {
749 png_warning(png_ptr, "Can't add Average filter after starting");
750 png_ptr->do_filter &= ~PNG_FILTER_AVG;
751 }
752 else
753 {
754 png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
755 (png_ptr->rowbytes + 1));
756 png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
757 }
758 }
759
760 if (png_ptr->do_filter & PNG_FILTER_PAETH &&
761 png_ptr->paeth_row == NULL)
762 {
763 if (png_ptr->prev_row == NULL)
764 {
765 png_warning(png_ptr, "Can't add Paeth filter after starting");
766 png_ptr->do_filter &= ~PNG_FILTER_PAETH;
767 }
768 else
769 {
770 png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
771 (png_ptr->rowbytes + 1));
772 png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
773 }
774 }
775
776 if (png_ptr->do_filter == PNG_NO_FILTERS)
777 png_ptr->do_filter = PNG_FILTER_NONE;
778 }
779 }
780 else
781 png_error(png_ptr, "Unknown custom filter method");
782 }
783
784 /* This allows us to influence the way in which libpng chooses the "best"
785 * filter for the current scanline. While the "minimum-sum-of-absolute-
786 * differences metric is relatively fast and effective, there is some
787 * question as to whether it can be improved upon by trying to keep the
788 * filtered data going to zlib more consistent, hopefully resulting in
789 * better compression.
790 */
791 #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
792 void
793 png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
794 int num_weights, png_doublep filter_weights,
795 png_doublep filter_costs)
796 {
797 #if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
798 int i;
799 #endif
800
801 png_debug(1, "in png_set_filter_heuristics\n");
802 if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
803 {
804 png_warning(png_ptr, "Unknown filter heuristic method");
805 return;
806 }
807
808 if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
809 {
810 heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
811 }
812
813 if (num_weights < 0 || filter_weights == NULL ||
814 heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
815 {
816 num_weights = 0;
817 }
818
819 png_ptr->num_prev_filters = num_weights;
820 png_ptr->heuristic_method = heuristic_method;
821
822 if (num_weights > 0)
823 {
824 if (png_ptr->prev_filters == NULL)
825 {
826 png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
827 (png_uint_32)(sizeof(png_byte) * num_weights));
828
829 /* To make sure that the weighting starts out fairly */
830 for (i = 0; i < num_weights; i++)
831 {
832 png_ptr->prev_filters[i] = 255;
833 }
834 }
835
836 if (png_ptr->filter_weights == NULL)
837 {
838 png_ptr->filter_weights = (png_uint_16p) png_malloc(png_ptr,
839 (png_uint_32)(sizeof(png_uint_16) * num_weights));
840
841 png_ptr->inv_filter_weights = (png_uint_16p) png_malloc(png_ptr,
842 (png_uint_32)(sizeof(png_uint_16) * num_weights));
843
844 for (i = 0; i < num_weights; i++)
845 {
846 png_ptr->inv_filter_weights[i] =
847 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
848 }
849 }
850
851 for (i = 0; i < num_weights; i++)
852 {
853 if (filter_weights[i] < 0.0)
854 {
855 png_ptr->inv_filter_weights[i] =
856 png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
857 }
858 else
859 {
860 png_ptr->inv_filter_weights[i] =
861 (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
862 png_ptr->filter_weights[i] =
863 (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
864 }
865 }
866 }
867
868 /* If, in the future, there are other filter methods, this would
869 * need to be based on png_ptr->filter.
870 */
871 if (png_ptr->filter_costs == NULL)
872 {
873 png_ptr->filter_costs = (png_uint_16p) png_malloc(png_ptr,
874 (png_uint_32)(sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
875
876 png_ptr->inv_filter_costs = (png_uint_16p) png_malloc(png_ptr,
877 (png_uint_32)(sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
878
879 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
880 {
881 png_ptr->inv_filter_costs[i] =
882 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
883 }
884 }
885
886 /* Here is where we set the relative costs of the different filters. We
887 * should take the desired compression level into account when setting
888 * the costs, so that Paeth, for instance, has a high relative cost at low
889 * compression levels, while it has a lower relative cost at higher
890 * compression settings. The filter types are in order of increasing
891 * relative cost, so it would be possible to do this with an algorithm.
892 */
893 for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
894 {
895 if (filter_costs == NULL || filter_costs[i] < 0.0)
896 {
897 png_ptr->inv_filter_costs[i] =
898 png_ptr->filter_costs[i] = PNG_COST_FACTOR;
899 }
900 else if (filter_costs[i] >= 1.0)
901 {
902 png_ptr->inv_filter_costs[i] =
903 (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
904 png_ptr->filter_costs[i] =
905 (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
906 }
907 }
908 }
909 #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
910
911 void
912 png_set_compression_level(png_structp png_ptr, int level)
913 {
914 png_debug(1, "in png_set_compression_level\n");
915 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
916 png_ptr->zlib_level = level;
917 }
918
919 void
920 png_set_compression_mem_level(png_structp png_ptr, int mem_level)
921 {
922 png_debug(1, "in png_set_compression_mem_level\n");
923 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
924 png_ptr->zlib_mem_level = mem_level;
925 }
926
927 void
928 png_set_compression_strategy(png_structp png_ptr, int strategy)
929 {
930 png_debug(1, "in png_set_compression_strategy\n");
931 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
932 png_ptr->zlib_strategy = strategy;
933 }
934
935 void
936 png_set_compression_window_bits(png_structp png_ptr, int window_bits)
937 {
938 if (window_bits > 15)
939 png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
940 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
941 png_ptr->zlib_window_bits = window_bits;
942 }
943
944 void
945 png_set_compression_method(png_structp png_ptr, int method)
946 {
947 png_debug(1, "in png_set_compression_method\n");
948 if (method != 8)
949 png_warning(png_ptr, "Only compression method 8 is supported by PNG");
950 png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
951 png_ptr->zlib_method = method;
952 }
953
954 void
955 png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
956 {
957 png_ptr->write_row_fn = write_row_fn;
958 }
959
960 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
961 void
962 png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
963 write_user_transform_fn)
964 {
965 png_debug(1, "in png_set_write_user_transform_fn\n");
966 png_ptr->transformations |= PNG_USER_TRANSFORM;
967 png_ptr->write_user_transform_fn = write_user_transform_fn;
968 }
969 #endif
970