The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2793 lines
85KB

  1. /* pngwutil.c - utilities to write a PNG file
  2. *
  3. * Last changed in libpng 1.2.20 Septhember 3, 2007
  4. * For conditions of distribution and use, see copyright notice in png.h
  5. * Copyright (c) 1998-2007 Glenn Randers-Pehrson
  6. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  7. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  8. */
  9. #define PNG_INTERNAL
  10. #include "png.h"
  11. #ifdef PNG_WRITE_SUPPORTED
  12. /* Place a 32-bit number into a buffer in PNG byte order. We work
  13. * with unsigned numbers for convenience, although one supported
  14. * ancillary chunk uses signed (two's complement) numbers.
  15. */
  16. void PNGAPI
  17. png_save_uint_32(png_bytep buf, png_uint_32 i)
  18. {
  19. buf[0] = (png_byte)((i >> 24) & 0xff);
  20. buf[1] = (png_byte)((i >> 16) & 0xff);
  21. buf[2] = (png_byte)((i >> 8) & 0xff);
  22. buf[3] = (png_byte)(i & 0xff);
  23. }
  24. /* The png_save_int_32 function assumes integers are stored in two's
  25. * complement format. If this isn't the case, then this routine needs to
  26. * be modified to write data in two's complement format.
  27. */
  28. void PNGAPI
  29. png_save_int_32(png_bytep buf, png_int_32 i)
  30. {
  31. buf[0] = (png_byte)((i >> 24) & 0xff);
  32. buf[1] = (png_byte)((i >> 16) & 0xff);
  33. buf[2] = (png_byte)((i >> 8) & 0xff);
  34. buf[3] = (png_byte)(i & 0xff);
  35. }
  36. /* Place a 16-bit number into a buffer in PNG byte order.
  37. * The parameter is declared unsigned int, not png_uint_16,
  38. * just to avoid potential problems on pre-ANSI C compilers.
  39. */
  40. void PNGAPI
  41. png_save_uint_16(png_bytep buf, unsigned int i)
  42. {
  43. buf[0] = (png_byte)((i >> 8) & 0xff);
  44. buf[1] = (png_byte)(i & 0xff);
  45. }
  46. /* Write a PNG chunk all at once. The type is an array of ASCII characters
  47. * representing the chunk name. The array must be at least 4 bytes in
  48. * length, and does not need to be null terminated. To be safe, pass the
  49. * pre-defined chunk names here, and if you need a new one, define it
  50. * where the others are defined. The length is the length of the data.
  51. * All the data must be present. If that is not possible, use the
  52. * png_write_chunk_start(), png_write_chunk_data(), and png_write_chunk_end()
  53. * functions instead.
  54. */
  55. void PNGAPI
  56. png_write_chunk(png_structp png_ptr, png_bytep chunk_name,
  57. png_bytep data, png_size_t length)
  58. {
  59. if(png_ptr == NULL) return;
  60. png_write_chunk_start(png_ptr, chunk_name, (png_uint_32)length);
  61. png_write_chunk_data(png_ptr, data, length);
  62. png_write_chunk_end(png_ptr);
  63. }
  64. /* Write the start of a PNG chunk. The type is the chunk type.
  65. * The total_length is the sum of the lengths of all the data you will be
  66. * passing in png_write_chunk_data().
  67. */
  68. void PNGAPI
  69. png_write_chunk_start(png_structp png_ptr, png_bytep chunk_name,
  70. png_uint_32 length)
  71. {
  72. png_byte buf[4];
  73. png_debug2(0, "Writing %s chunk (%lu bytes)\n", chunk_name, length);
  74. if(png_ptr == NULL) return;
  75. /* write the length */
  76. png_save_uint_32(buf, length);
  77. png_write_data(png_ptr, buf, (png_size_t)4);
  78. /* write the chunk name */
  79. png_write_data(png_ptr, chunk_name, (png_size_t)4);
  80. /* reset the crc and run it over the chunk name */
  81. png_reset_crc(png_ptr);
  82. png_calculate_crc(png_ptr, chunk_name, (png_size_t)4);
  83. }
  84. /* Write the data of a PNG chunk started with png_write_chunk_start().
  85. * Note that multiple calls to this function are allowed, and that the
  86. * sum of the lengths from these calls *must* add up to the total_length
  87. * given to png_write_chunk_start().
  88. */
  89. void PNGAPI
  90. png_write_chunk_data(png_structp png_ptr, png_bytep data, png_size_t length)
  91. {
  92. /* write the data, and run the CRC over it */
  93. if(png_ptr == NULL) return;
  94. if (data != NULL && length > 0)
  95. {
  96. png_calculate_crc(png_ptr, data, length);
  97. png_write_data(png_ptr, data, length);
  98. }
  99. }
  100. /* Finish a chunk started with png_write_chunk_start(). */
  101. void PNGAPI
  102. png_write_chunk_end(png_structp png_ptr)
  103. {
  104. png_byte buf[4];
  105. if(png_ptr == NULL) return;
  106. /* write the crc */
  107. png_save_uint_32(buf, png_ptr->crc);
  108. png_write_data(png_ptr, buf, (png_size_t)4);
  109. }
  110. /* Simple function to write the signature. If we have already written
  111. * the magic bytes of the signature, or more likely, the PNG stream is
  112. * being embedded into another stream and doesn't need its own signature,
  113. * we should call png_set_sig_bytes() to tell libpng how many of the
  114. * bytes have already been written.
  115. */
  116. void /* PRIVATE */
  117. png_write_sig(png_structp png_ptr)
  118. {
  119. png_byte png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
  120. /* write the rest of the 8 byte signature */
  121. png_write_data(png_ptr, &png_signature[png_ptr->sig_bytes],
  122. (png_size_t)8 - png_ptr->sig_bytes);
  123. if(png_ptr->sig_bytes < 3)
  124. png_ptr->mode |= PNG_HAVE_PNG_SIGNATURE;
  125. }
  126. #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_iCCP_SUPPORTED)
  127. /*
  128. * This pair of functions encapsulates the operation of (a) compressing a
  129. * text string, and (b) issuing it later as a series of chunk data writes.
  130. * The compression_state structure is shared context for these functions
  131. * set up by the caller in order to make the whole mess thread-safe.
  132. */
  133. typedef struct
  134. {
  135. char *input; /* the uncompressed input data */
  136. int input_len; /* its length */
  137. int num_output_ptr; /* number of output pointers used */
  138. int max_output_ptr; /* size of output_ptr */
  139. png_charpp output_ptr; /* array of pointers to output */
  140. } compression_state;
  141. /* compress given text into storage in the png_ptr structure */
  142. static int /* PRIVATE */
  143. png_text_compress(png_structp png_ptr,
  144. png_charp text, png_size_t text_len, int compression,
  145. compression_state *comp)
  146. {
  147. int ret;
  148. comp->num_output_ptr = 0;
  149. comp->max_output_ptr = 0;
  150. comp->output_ptr = NULL;
  151. comp->input = NULL;
  152. comp->input_len = 0;
  153. /* we may just want to pass the text right through */
  154. if (compression == PNG_TEXT_COMPRESSION_NONE)
  155. {
  156. comp->input = text;
  157. comp->input_len = text_len;
  158. return((int)text_len);
  159. }
  160. if (compression >= PNG_TEXT_COMPRESSION_LAST)
  161. {
  162. #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  163. char msg[50];
  164. png_snprintf(msg, 50, "Unknown compression type %d", compression);
  165. png_warning(png_ptr, msg);
  166. #else
  167. png_warning(png_ptr, "Unknown compression type");
  168. #endif
  169. }
  170. /* We can't write the chunk until we find out how much data we have,
  171. * which means we need to run the compressor first and save the
  172. * output. This shouldn't be a problem, as the vast majority of
  173. * comments should be reasonable, but we will set up an array of
  174. * malloc'd pointers to be sure.
  175. *
  176. * If we knew the application was well behaved, we could simplify this
  177. * greatly by assuming we can always malloc an output buffer large
  178. * enough to hold the compressed text ((1001 * text_len / 1000) + 12)
  179. * and malloc this directly. The only time this would be a bad idea is
  180. * if we can't malloc more than 64K and we have 64K of random input
  181. * data, or if the input string is incredibly large (although this
  182. * wouldn't cause a failure, just a slowdown due to swapping).
  183. */
  184. /* set up the compression buffers */
  185. png_ptr->zstream.avail_in = (uInt)text_len;
  186. png_ptr->zstream.next_in = (Bytef *)text;
  187. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  188. png_ptr->zstream.next_out = (Bytef *)png_ptr->zbuf;
  189. /* this is the same compression loop as in png_write_row() */
  190. do
  191. {
  192. /* compress the data */
  193. ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
  194. if (ret != Z_OK)
  195. {
  196. /* error */
  197. if (png_ptr->zstream.msg != NULL)
  198. png_error(png_ptr, png_ptr->zstream.msg);
  199. else
  200. png_error(png_ptr, "zlib error");
  201. }
  202. /* check to see if we need more room */
  203. if (!(png_ptr->zstream.avail_out))
  204. {
  205. /* make sure the output array has room */
  206. if (comp->num_output_ptr >= comp->max_output_ptr)
  207. {
  208. int old_max;
  209. old_max = comp->max_output_ptr;
  210. comp->max_output_ptr = comp->num_output_ptr + 4;
  211. if (comp->output_ptr != NULL)
  212. {
  213. png_charpp old_ptr;
  214. old_ptr = comp->output_ptr;
  215. comp->output_ptr = (png_charpp)png_malloc(png_ptr,
  216. (png_uint_32)(comp->max_output_ptr *
  217. png_sizeof (png_charpp)));
  218. png_memcpy(comp->output_ptr, old_ptr, old_max
  219. * png_sizeof (png_charp));
  220. png_free(png_ptr, old_ptr);
  221. }
  222. else
  223. comp->output_ptr = (png_charpp)png_malloc(png_ptr,
  224. (png_uint_32)(comp->max_output_ptr *
  225. png_sizeof (png_charp)));
  226. }
  227. /* save the data */
  228. comp->output_ptr[comp->num_output_ptr] = (png_charp)png_malloc(png_ptr,
  229. (png_uint_32)png_ptr->zbuf_size);
  230. png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
  231. png_ptr->zbuf_size);
  232. comp->num_output_ptr++;
  233. /* and reset the buffer */
  234. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  235. png_ptr->zstream.next_out = png_ptr->zbuf;
  236. }
  237. /* continue until we don't have any more to compress */
  238. } while (png_ptr->zstream.avail_in);
  239. /* finish the compression */
  240. do
  241. {
  242. /* tell zlib we are finished */
  243. ret = deflate(&png_ptr->zstream, Z_FINISH);
  244. if (ret == Z_OK)
  245. {
  246. /* check to see if we need more room */
  247. if (!(png_ptr->zstream.avail_out))
  248. {
  249. /* check to make sure our output array has room */
  250. if (comp->num_output_ptr >= comp->max_output_ptr)
  251. {
  252. int old_max;
  253. old_max = comp->max_output_ptr;
  254. comp->max_output_ptr = comp->num_output_ptr + 4;
  255. if (comp->output_ptr != NULL)
  256. {
  257. png_charpp old_ptr;
  258. old_ptr = comp->output_ptr;
  259. /* This could be optimized to realloc() */
  260. comp->output_ptr = (png_charpp)png_malloc(png_ptr,
  261. (png_uint_32)(comp->max_output_ptr *
  262. png_sizeof (png_charpp)));
  263. png_memcpy(comp->output_ptr, old_ptr,
  264. old_max * png_sizeof (png_charp));
  265. png_free(png_ptr, old_ptr);
  266. }
  267. else
  268. comp->output_ptr = (png_charpp)png_malloc(png_ptr,
  269. (png_uint_32)(comp->max_output_ptr *
  270. png_sizeof (png_charp)));
  271. }
  272. /* save off the data */
  273. comp->output_ptr[comp->num_output_ptr] =
  274. (png_charp)png_malloc(png_ptr, (png_uint_32)png_ptr->zbuf_size);
  275. png_memcpy(comp->output_ptr[comp->num_output_ptr], png_ptr->zbuf,
  276. png_ptr->zbuf_size);
  277. comp->num_output_ptr++;
  278. /* and reset the buffer pointers */
  279. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  280. png_ptr->zstream.next_out = png_ptr->zbuf;
  281. }
  282. }
  283. else if (ret != Z_STREAM_END)
  284. {
  285. /* we got an error */
  286. if (png_ptr->zstream.msg != NULL)
  287. png_error(png_ptr, png_ptr->zstream.msg);
  288. else
  289. png_error(png_ptr, "zlib error");
  290. }
  291. } while (ret != Z_STREAM_END);
  292. /* text length is number of buffers plus last buffer */
  293. text_len = png_ptr->zbuf_size * comp->num_output_ptr;
  294. if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
  295. text_len += png_ptr->zbuf_size - (png_size_t)png_ptr->zstream.avail_out;
  296. return((int)text_len);
  297. }
  298. /* ship the compressed text out via chunk writes */
  299. static void /* PRIVATE */
  300. png_write_compressed_data_out(png_structp png_ptr, compression_state *comp)
  301. {
  302. int i;
  303. /* handle the no-compression case */
  304. if (comp->input)
  305. {
  306. png_write_chunk_data(png_ptr, (png_bytep)comp->input,
  307. (png_size_t)comp->input_len);
  308. return;
  309. }
  310. /* write saved output buffers, if any */
  311. for (i = 0; i < comp->num_output_ptr; i++)
  312. {
  313. png_write_chunk_data(png_ptr,(png_bytep)comp->output_ptr[i],
  314. png_ptr->zbuf_size);
  315. png_free(png_ptr, comp->output_ptr[i]);
  316. comp->output_ptr[i]=NULL;
  317. }
  318. if (comp->max_output_ptr != 0)
  319. png_free(png_ptr, comp->output_ptr);
  320. comp->output_ptr=NULL;
  321. /* write anything left in zbuf */
  322. if (png_ptr->zstream.avail_out < (png_uint_32)png_ptr->zbuf_size)
  323. png_write_chunk_data(png_ptr, png_ptr->zbuf,
  324. png_ptr->zbuf_size - png_ptr->zstream.avail_out);
  325. /* reset zlib for another zTXt/iTXt or image data */
  326. deflateReset(&png_ptr->zstream);
  327. png_ptr->zstream.data_type = Z_BINARY;
  328. }
  329. #endif
  330. /* Write the IHDR chunk, and update the png_struct with the necessary
  331. * information. Note that the rest of this code depends upon this
  332. * information being correct.
  333. */
  334. void /* PRIVATE */
  335. png_write_IHDR(png_structp png_ptr, png_uint_32 width, png_uint_32 height,
  336. int bit_depth, int color_type, int compression_type, int filter_type,
  337. int interlace_type)
  338. {
  339. #ifdef PNG_USE_LOCAL_ARRAYS
  340. PNG_IHDR;
  341. #endif
  342. png_byte buf[13]; /* buffer to store the IHDR info */
  343. png_debug(1, "in png_write_IHDR\n");
  344. /* Check that we have valid input data from the application info */
  345. switch (color_type)
  346. {
  347. case PNG_COLOR_TYPE_GRAY:
  348. switch (bit_depth)
  349. {
  350. case 1:
  351. case 2:
  352. case 4:
  353. case 8:
  354. case 16: png_ptr->channels = 1; break;
  355. default: png_error(png_ptr,"Invalid bit depth for grayscale image");
  356. }
  357. break;
  358. case PNG_COLOR_TYPE_RGB:
  359. if (bit_depth != 8 && bit_depth != 16)
  360. png_error(png_ptr, "Invalid bit depth for RGB image");
  361. png_ptr->channels = 3;
  362. break;
  363. case PNG_COLOR_TYPE_PALETTE:
  364. switch (bit_depth)
  365. {
  366. case 1:
  367. case 2:
  368. case 4:
  369. case 8: png_ptr->channels = 1; break;
  370. default: png_error(png_ptr, "Invalid bit depth for paletted image");
  371. }
  372. break;
  373. case PNG_COLOR_TYPE_GRAY_ALPHA:
  374. if (bit_depth != 8 && bit_depth != 16)
  375. png_error(png_ptr, "Invalid bit depth for grayscale+alpha image");
  376. png_ptr->channels = 2;
  377. break;
  378. case PNG_COLOR_TYPE_RGB_ALPHA:
  379. if (bit_depth != 8 && bit_depth != 16)
  380. png_error(png_ptr, "Invalid bit depth for RGBA image");
  381. png_ptr->channels = 4;
  382. break;
  383. default:
  384. png_error(png_ptr, "Invalid image color type specified");
  385. }
  386. if (compression_type != PNG_COMPRESSION_TYPE_BASE)
  387. {
  388. png_warning(png_ptr, "Invalid compression type specified");
  389. compression_type = PNG_COMPRESSION_TYPE_BASE;
  390. }
  391. /* Write filter_method 64 (intrapixel differencing) only if
  392. * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
  393. * 2. Libpng did not write a PNG signature (this filter_method is only
  394. * used in PNG datastreams that are embedded in MNG datastreams) and
  395. * 3. The application called png_permit_mng_features with a mask that
  396. * included PNG_FLAG_MNG_FILTER_64 and
  397. * 4. The filter_method is 64 and
  398. * 5. The color_type is RGB or RGBA
  399. */
  400. if (
  401. #if defined(PNG_MNG_FEATURES_SUPPORTED)
  402. !((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  403. ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) == 0) &&
  404. (color_type == PNG_COLOR_TYPE_RGB ||
  405. color_type == PNG_COLOR_TYPE_RGB_ALPHA) &&
  406. (filter_type == PNG_INTRAPIXEL_DIFFERENCING)) &&
  407. #endif
  408. filter_type != PNG_FILTER_TYPE_BASE)
  409. {
  410. png_warning(png_ptr, "Invalid filter type specified");
  411. filter_type = PNG_FILTER_TYPE_BASE;
  412. }
  413. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  414. if (interlace_type != PNG_INTERLACE_NONE &&
  415. interlace_type != PNG_INTERLACE_ADAM7)
  416. {
  417. png_warning(png_ptr, "Invalid interlace type specified");
  418. interlace_type = PNG_INTERLACE_ADAM7;
  419. }
  420. #else
  421. interlace_type=PNG_INTERLACE_NONE;
  422. #endif
  423. /* save off the relevent information */
  424. png_ptr->bit_depth = (png_byte)bit_depth;
  425. png_ptr->color_type = (png_byte)color_type;
  426. png_ptr->interlaced = (png_byte)interlace_type;
  427. #if defined(PNG_MNG_FEATURES_SUPPORTED)
  428. png_ptr->filter_type = (png_byte)filter_type;
  429. #endif
  430. png_ptr->compression_type = (png_byte)compression_type;
  431. png_ptr->width = width;
  432. png_ptr->height = height;
  433. png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
  434. png_ptr->rowbytes = PNG_ROWBYTES(png_ptr->pixel_depth, width);
  435. /* set the usr info, so any transformations can modify it */
  436. png_ptr->usr_width = png_ptr->width;
  437. png_ptr->usr_bit_depth = png_ptr->bit_depth;
  438. png_ptr->usr_channels = png_ptr->channels;
  439. /* pack the header information into the buffer */
  440. png_save_uint_32(buf, width);
  441. png_save_uint_32(buf + 4, height);
  442. buf[8] = (png_byte)bit_depth;
  443. buf[9] = (png_byte)color_type;
  444. buf[10] = (png_byte)compression_type;
  445. buf[11] = (png_byte)filter_type;
  446. buf[12] = (png_byte)interlace_type;
  447. /* write the chunk */
  448. png_write_chunk(png_ptr, png_IHDR, buf, (png_size_t)13);
  449. /* initialize zlib with PNG info */
  450. png_ptr->zstream.zalloc = png_zalloc;
  451. png_ptr->zstream.zfree = png_zfree;
  452. png_ptr->zstream.opaque = (voidpf)png_ptr;
  453. if (!(png_ptr->do_filter))
  454. {
  455. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
  456. png_ptr->bit_depth < 8)
  457. png_ptr->do_filter = PNG_FILTER_NONE;
  458. else
  459. png_ptr->do_filter = PNG_ALL_FILTERS;
  460. }
  461. if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_STRATEGY))
  462. {
  463. if (png_ptr->do_filter != PNG_FILTER_NONE)
  464. png_ptr->zlib_strategy = Z_FILTERED;
  465. else
  466. png_ptr->zlib_strategy = Z_DEFAULT_STRATEGY;
  467. }
  468. if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_LEVEL))
  469. png_ptr->zlib_level = Z_DEFAULT_COMPRESSION;
  470. if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL))
  471. png_ptr->zlib_mem_level = 8;
  472. if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS))
  473. png_ptr->zlib_window_bits = 15;
  474. if (!(png_ptr->flags & PNG_FLAG_ZLIB_CUSTOM_METHOD))
  475. png_ptr->zlib_method = 8;
  476. if (deflateInit2(&png_ptr->zstream, png_ptr->zlib_level,
  477. png_ptr->zlib_method, png_ptr->zlib_window_bits,
  478. png_ptr->zlib_mem_level, png_ptr->zlib_strategy) != Z_OK)
  479. png_error(png_ptr, "zlib failed to initialize compressor");
  480. png_ptr->zstream.next_out = png_ptr->zbuf;
  481. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  482. /* libpng is not interested in zstream.data_type */
  483. /* set it to a predefined value, to avoid its evaluation inside zlib */
  484. png_ptr->zstream.data_type = Z_BINARY;
  485. png_ptr->mode = PNG_HAVE_IHDR;
  486. }
  487. /* write the palette. We are careful not to trust png_color to be in the
  488. * correct order for PNG, so people can redefine it to any convenient
  489. * structure.
  490. */
  491. void /* PRIVATE */
  492. png_write_PLTE(png_structp png_ptr, png_colorp palette, png_uint_32 num_pal)
  493. {
  494. #ifdef PNG_USE_LOCAL_ARRAYS
  495. PNG_PLTE;
  496. #endif
  497. png_uint_32 i;
  498. png_colorp pal_ptr;
  499. png_byte buf[3];
  500. png_debug(1, "in png_write_PLTE\n");
  501. if ((
  502. #if defined(PNG_MNG_FEATURES_SUPPORTED)
  503. !(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) &&
  504. #endif
  505. num_pal == 0) || num_pal > 256)
  506. {
  507. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  508. {
  509. png_error(png_ptr, "Invalid number of colors in palette");
  510. }
  511. else
  512. {
  513. png_warning(png_ptr, "Invalid number of colors in palette");
  514. return;
  515. }
  516. }
  517. if (!(png_ptr->color_type&PNG_COLOR_MASK_COLOR))
  518. {
  519. png_warning(png_ptr,
  520. "Ignoring request to write a PLTE chunk in grayscale PNG");
  521. return;
  522. }
  523. png_ptr->num_palette = (png_uint_16)num_pal;
  524. png_debug1(3, "num_palette = %d\n", png_ptr->num_palette);
  525. png_write_chunk_start(png_ptr, png_PLTE, num_pal * 3);
  526. #ifndef PNG_NO_POINTER_INDEXING
  527. for (i = 0, pal_ptr = palette; i < num_pal; i++, pal_ptr++)
  528. {
  529. buf[0] = pal_ptr->red;
  530. buf[1] = pal_ptr->green;
  531. buf[2] = pal_ptr->blue;
  532. png_write_chunk_data(png_ptr, buf, (png_size_t)3);
  533. }
  534. #else
  535. /* This is a little slower but some buggy compilers need to do this instead */
  536. pal_ptr=palette;
  537. for (i = 0; i < num_pal; i++)
  538. {
  539. buf[0] = pal_ptr[i].red;
  540. buf[1] = pal_ptr[i].green;
  541. buf[2] = pal_ptr[i].blue;
  542. png_write_chunk_data(png_ptr, buf, (png_size_t)3);
  543. }
  544. #endif
  545. png_write_chunk_end(png_ptr);
  546. png_ptr->mode |= PNG_HAVE_PLTE;
  547. }
  548. /* write an IDAT chunk */
  549. void /* PRIVATE */
  550. png_write_IDAT(png_structp png_ptr, png_bytep data, png_size_t length)
  551. {
  552. #ifdef PNG_USE_LOCAL_ARRAYS
  553. PNG_IDAT;
  554. #endif
  555. png_debug(1, "in png_write_IDAT\n");
  556. /* Optimize the CMF field in the zlib stream. */
  557. /* This hack of the zlib stream is compliant to the stream specification. */
  558. if (!(png_ptr->mode & PNG_HAVE_IDAT) &&
  559. png_ptr->compression_type == PNG_COMPRESSION_TYPE_BASE)
  560. {
  561. unsigned int z_cmf = data[0]; /* zlib compression method and flags */
  562. if ((z_cmf & 0x0f) == 8 && (z_cmf & 0xf0) <= 0x70)
  563. {
  564. /* Avoid memory underflows and multiplication overflows. */
  565. /* The conditions below are practically always satisfied;
  566. however, they still must be checked. */
  567. if (length >= 2 &&
  568. png_ptr->height < 16384 && png_ptr->width < 16384)
  569. {
  570. png_uint_32 uncompressed_idat_size = png_ptr->height *
  571. ((png_ptr->width *
  572. png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
  573. unsigned int z_cinfo = z_cmf >> 4;
  574. unsigned int half_z_window_size = 1 << (z_cinfo + 7);
  575. while (uncompressed_idat_size <= half_z_window_size &&
  576. half_z_window_size >= 256)
  577. {
  578. z_cinfo--;
  579. half_z_window_size >>= 1;
  580. }
  581. z_cmf = (z_cmf & 0x0f) | (z_cinfo << 4);
  582. if (data[0] != (png_byte)z_cmf)
  583. {
  584. data[0] = (png_byte)z_cmf;
  585. data[1] &= 0xe0;
  586. data[1] += (png_byte)(0x1f - ((z_cmf << 8) + data[1]) % 0x1f);
  587. }
  588. }
  589. }
  590. else
  591. png_error(png_ptr,
  592. "Invalid zlib compression method or flags in IDAT");
  593. }
  594. png_write_chunk(png_ptr, png_IDAT, data, length);
  595. png_ptr->mode |= PNG_HAVE_IDAT;
  596. }
  597. /* write an IEND chunk */
  598. void /* PRIVATE */
  599. png_write_IEND(png_structp png_ptr)
  600. {
  601. #ifdef PNG_USE_LOCAL_ARRAYS
  602. PNG_IEND;
  603. #endif
  604. png_debug(1, "in png_write_IEND\n");
  605. png_write_chunk(png_ptr, png_IEND, png_bytep_NULL,
  606. (png_size_t)0);
  607. png_ptr->mode |= PNG_HAVE_IEND;
  608. }
  609. #if defined(PNG_WRITE_gAMA_SUPPORTED)
  610. /* write a gAMA chunk */
  611. #ifdef PNG_FLOATING_POINT_SUPPORTED
  612. void /* PRIVATE */
  613. png_write_gAMA(png_structp png_ptr, double file_gamma)
  614. {
  615. #ifdef PNG_USE_LOCAL_ARRAYS
  616. PNG_gAMA;
  617. #endif
  618. png_uint_32 igamma;
  619. png_byte buf[4];
  620. png_debug(1, "in png_write_gAMA\n");
  621. /* file_gamma is saved in 1/100,000ths */
  622. igamma = (png_uint_32)(file_gamma * 100000.0 + 0.5);
  623. png_save_uint_32(buf, igamma);
  624. png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
  625. }
  626. #endif
  627. #ifdef PNG_FIXED_POINT_SUPPORTED
  628. void /* PRIVATE */
  629. png_write_gAMA_fixed(png_structp png_ptr, png_fixed_point file_gamma)
  630. {
  631. #ifdef PNG_USE_LOCAL_ARRAYS
  632. PNG_gAMA;
  633. #endif
  634. png_byte buf[4];
  635. png_debug(1, "in png_write_gAMA\n");
  636. /* file_gamma is saved in 1/100,000ths */
  637. png_save_uint_32(buf, (png_uint_32)file_gamma);
  638. png_write_chunk(png_ptr, png_gAMA, buf, (png_size_t)4);
  639. }
  640. #endif
  641. #endif
  642. #if defined(PNG_WRITE_sRGB_SUPPORTED)
  643. /* write a sRGB chunk */
  644. void /* PRIVATE */
  645. png_write_sRGB(png_structp png_ptr, int srgb_intent)
  646. {
  647. #ifdef PNG_USE_LOCAL_ARRAYS
  648. PNG_sRGB;
  649. #endif
  650. png_byte buf[1];
  651. png_debug(1, "in png_write_sRGB\n");
  652. if(srgb_intent >= PNG_sRGB_INTENT_LAST)
  653. png_warning(png_ptr,
  654. "Invalid sRGB rendering intent specified");
  655. buf[0]=(png_byte)srgb_intent;
  656. png_write_chunk(png_ptr, png_sRGB, buf, (png_size_t)1);
  657. }
  658. #endif
  659. #if defined(PNG_WRITE_iCCP_SUPPORTED)
  660. /* write an iCCP chunk */
  661. void /* PRIVATE */
  662. png_write_iCCP(png_structp png_ptr, png_charp name, int compression_type,
  663. png_charp profile, int profile_len)
  664. {
  665. #ifdef PNG_USE_LOCAL_ARRAYS
  666. PNG_iCCP;
  667. #endif
  668. png_size_t name_len;
  669. png_charp new_name;
  670. compression_state comp;
  671. int embedded_profile_len = 0;
  672. png_debug(1, "in png_write_iCCP\n");
  673. comp.num_output_ptr = 0;
  674. comp.max_output_ptr = 0;
  675. comp.output_ptr = NULL;
  676. comp.input = NULL;
  677. comp.input_len = 0;
  678. if (name == NULL || (name_len = png_check_keyword(png_ptr, name,
  679. &new_name)) == 0)
  680. {
  681. png_warning(png_ptr, "Empty keyword in iCCP chunk");
  682. return;
  683. }
  684. if (compression_type != PNG_COMPRESSION_TYPE_BASE)
  685. png_warning(png_ptr, "Unknown compression type in iCCP chunk");
  686. if (profile == NULL)
  687. profile_len = 0;
  688. if (profile_len > 3)
  689. embedded_profile_len =
  690. ((*( (png_bytep)profile ))<<24) |
  691. ((*( (png_bytep)profile+1))<<16) |
  692. ((*( (png_bytep)profile+2))<< 8) |
  693. ((*( (png_bytep)profile+3)) );
  694. if (profile_len < embedded_profile_len)
  695. {
  696. png_warning(png_ptr,
  697. "Embedded profile length too large in iCCP chunk");
  698. return;
  699. }
  700. if (profile_len > embedded_profile_len)
  701. {
  702. png_warning(png_ptr,
  703. "Truncating profile to actual length in iCCP chunk");
  704. profile_len = embedded_profile_len;
  705. }
  706. if (profile_len)
  707. profile_len = png_text_compress(png_ptr, profile, (png_size_t)profile_len,
  708. PNG_COMPRESSION_TYPE_BASE, &comp);
  709. /* make sure we include the NULL after the name and the compression type */
  710. png_write_chunk_start(png_ptr, png_iCCP,
  711. (png_uint_32)name_len+profile_len+2);
  712. new_name[name_len+1]=0x00;
  713. png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 2);
  714. if (profile_len)
  715. png_write_compressed_data_out(png_ptr, &comp);
  716. png_write_chunk_end(png_ptr);
  717. png_free(png_ptr, new_name);
  718. }
  719. #endif
  720. #if defined(PNG_WRITE_sPLT_SUPPORTED)
  721. /* write a sPLT chunk */
  722. void /* PRIVATE */
  723. png_write_sPLT(png_structp png_ptr, png_sPLT_tp spalette)
  724. {
  725. #ifdef PNG_USE_LOCAL_ARRAYS
  726. PNG_sPLT;
  727. #endif
  728. png_size_t name_len;
  729. png_charp new_name;
  730. png_byte entrybuf[10];
  731. int entry_size = (spalette->depth == 8 ? 6 : 10);
  732. int palette_size = entry_size * spalette->nentries;
  733. png_sPLT_entryp ep;
  734. #ifdef PNG_NO_POINTER_INDEXING
  735. int i;
  736. #endif
  737. png_debug(1, "in png_write_sPLT\n");
  738. if (spalette->name == NULL || (name_len = png_check_keyword(png_ptr,
  739. spalette->name, &new_name))==0)
  740. {
  741. png_warning(png_ptr, "Empty keyword in sPLT chunk");
  742. return;
  743. }
  744. /* make sure we include the NULL after the name */
  745. png_write_chunk_start(png_ptr, png_sPLT,
  746. (png_uint_32)(name_len + 2 + palette_size));
  747. png_write_chunk_data(png_ptr, (png_bytep)new_name, name_len + 1);
  748. png_write_chunk_data(png_ptr, (png_bytep)&spalette->depth, 1);
  749. /* loop through each palette entry, writing appropriately */
  750. #ifndef PNG_NO_POINTER_INDEXING
  751. for (ep = spalette->entries; ep<spalette->entries+spalette->nentries; ep++)
  752. {
  753. if (spalette->depth == 8)
  754. {
  755. entrybuf[0] = (png_byte)ep->red;
  756. entrybuf[1] = (png_byte)ep->green;
  757. entrybuf[2] = (png_byte)ep->blue;
  758. entrybuf[3] = (png_byte)ep->alpha;
  759. png_save_uint_16(entrybuf + 4, ep->frequency);
  760. }
  761. else
  762. {
  763. png_save_uint_16(entrybuf + 0, ep->red);
  764. png_save_uint_16(entrybuf + 2, ep->green);
  765. png_save_uint_16(entrybuf + 4, ep->blue);
  766. png_save_uint_16(entrybuf + 6, ep->alpha);
  767. png_save_uint_16(entrybuf + 8, ep->frequency);
  768. }
  769. png_write_chunk_data(png_ptr, entrybuf, (png_size_t)entry_size);
  770. }
  771. #else
  772. ep=spalette->entries;
  773. for (i=0; i>spalette->nentries; i++)
  774. {
  775. if (spalette->depth == 8)
  776. {
  777. entrybuf[0] = (png_byte)ep[i].red;
  778. entrybuf[1] = (png_byte)ep[i].green;
  779. entrybuf[2] = (png_byte)ep[i].blue;
  780. entrybuf[3] = (png_byte)ep[i].alpha;
  781. png_save_uint_16(entrybuf + 4, ep[i].frequency);
  782. }
  783. else
  784. {
  785. png_save_uint_16(entrybuf + 0, ep[i].red);
  786. png_save_uint_16(entrybuf + 2, ep[i].green);
  787. png_save_uint_16(entrybuf + 4, ep[i].blue);
  788. png_save_uint_16(entrybuf + 6, ep[i].alpha);
  789. png_save_uint_16(entrybuf + 8, ep[i].frequency);
  790. }
  791. png_write_chunk_data(png_ptr, entrybuf, entry_size);
  792. }
  793. #endif
  794. png_write_chunk_end(png_ptr);
  795. png_free(png_ptr, new_name);
  796. }
  797. #endif
  798. #if defined(PNG_WRITE_sBIT_SUPPORTED)
  799. /* write the sBIT chunk */
  800. void /* PRIVATE */
  801. png_write_sBIT(png_structp png_ptr, png_color_8p sbit, int color_type)
  802. {
  803. #ifdef PNG_USE_LOCAL_ARRAYS
  804. PNG_sBIT;
  805. #endif
  806. png_byte buf[4];
  807. png_size_t size;
  808. png_debug(1, "in png_write_sBIT\n");
  809. /* make sure we don't depend upon the order of PNG_COLOR_8 */
  810. if (color_type & PNG_COLOR_MASK_COLOR)
  811. {
  812. png_byte maxbits;
  813. maxbits = (png_byte)(color_type==PNG_COLOR_TYPE_PALETTE ? 8 :
  814. png_ptr->usr_bit_depth);
  815. if (sbit->red == 0 || sbit->red > maxbits ||
  816. sbit->green == 0 || sbit->green > maxbits ||
  817. sbit->blue == 0 || sbit->blue > maxbits)
  818. {
  819. png_warning(png_ptr, "Invalid sBIT depth specified");
  820. return;
  821. }
  822. buf[0] = sbit->red;
  823. buf[1] = sbit->green;
  824. buf[2] = sbit->blue;
  825. size = 3;
  826. }
  827. else
  828. {
  829. if (sbit->gray == 0 || sbit->gray > png_ptr->usr_bit_depth)
  830. {
  831. png_warning(png_ptr, "Invalid sBIT depth specified");
  832. return;
  833. }
  834. buf[0] = sbit->gray;
  835. size = 1;
  836. }
  837. if (color_type & PNG_COLOR_MASK_ALPHA)
  838. {
  839. if (sbit->alpha == 0 || sbit->alpha > png_ptr->usr_bit_depth)
  840. {
  841. png_warning(png_ptr, "Invalid sBIT depth specified");
  842. return;
  843. }
  844. buf[size++] = sbit->alpha;
  845. }
  846. png_write_chunk(png_ptr, png_sBIT, buf, size);
  847. }
  848. #endif
  849. #if defined(PNG_WRITE_cHRM_SUPPORTED)
  850. /* write the cHRM chunk */
  851. #ifdef PNG_FLOATING_POINT_SUPPORTED
  852. void /* PRIVATE */
  853. png_write_cHRM(png_structp png_ptr, double white_x, double white_y,
  854. double red_x, double red_y, double green_x, double green_y,
  855. double blue_x, double blue_y)
  856. {
  857. #ifdef PNG_USE_LOCAL_ARRAYS
  858. PNG_cHRM;
  859. #endif
  860. png_byte buf[32];
  861. png_uint_32 itemp;
  862. png_debug(1, "in png_write_cHRM\n");
  863. /* each value is saved in 1/100,000ths */
  864. if (white_x < 0 || white_x > 0.8 || white_y < 0 || white_y > 0.8 ||
  865. white_x + white_y > 1.0)
  866. {
  867. png_warning(png_ptr, "Invalid cHRM white point specified");
  868. #if !defined(PNG_NO_CONSOLE_IO)
  869. fprintf(stderr,"white_x=%f, white_y=%f\n",white_x, white_y);
  870. #endif
  871. return;
  872. }
  873. itemp = (png_uint_32)(white_x * 100000.0 + 0.5);
  874. png_save_uint_32(buf, itemp);
  875. itemp = (png_uint_32)(white_y * 100000.0 + 0.5);
  876. png_save_uint_32(buf + 4, itemp);
  877. if (red_x < 0 || red_y < 0 || red_x + red_y > 1.0)
  878. {
  879. png_warning(png_ptr, "Invalid cHRM red point specified");
  880. return;
  881. }
  882. itemp = (png_uint_32)(red_x * 100000.0 + 0.5);
  883. png_save_uint_32(buf + 8, itemp);
  884. itemp = (png_uint_32)(red_y * 100000.0 + 0.5);
  885. png_save_uint_32(buf + 12, itemp);
  886. if (green_x < 0 || green_y < 0 || green_x + green_y > 1.0)
  887. {
  888. png_warning(png_ptr, "Invalid cHRM green point specified");
  889. return;
  890. }
  891. itemp = (png_uint_32)(green_x * 100000.0 + 0.5);
  892. png_save_uint_32(buf + 16, itemp);
  893. itemp = (png_uint_32)(green_y * 100000.0 + 0.5);
  894. png_save_uint_32(buf + 20, itemp);
  895. if (blue_x < 0 || blue_y < 0 || blue_x + blue_y > 1.0)
  896. {
  897. png_warning(png_ptr, "Invalid cHRM blue point specified");
  898. return;
  899. }
  900. itemp = (png_uint_32)(blue_x * 100000.0 + 0.5);
  901. png_save_uint_32(buf + 24, itemp);
  902. itemp = (png_uint_32)(blue_y * 100000.0 + 0.5);
  903. png_save_uint_32(buf + 28, itemp);
  904. png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
  905. }
  906. #endif
  907. #ifdef PNG_FIXED_POINT_SUPPORTED
  908. void /* PRIVATE */
  909. png_write_cHRM_fixed(png_structp png_ptr, png_fixed_point white_x,
  910. png_fixed_point white_y, png_fixed_point red_x, png_fixed_point red_y,
  911. png_fixed_point green_x, png_fixed_point green_y, png_fixed_point blue_x,
  912. png_fixed_point blue_y)
  913. {
  914. #ifdef PNG_USE_LOCAL_ARRAYS
  915. PNG_cHRM;
  916. #endif
  917. png_byte buf[32];
  918. png_debug(1, "in png_write_cHRM\n");
  919. /* each value is saved in 1/100,000ths */
  920. if (white_x > 80000L || white_y > 80000L || white_x + white_y > 100000L)
  921. {
  922. png_warning(png_ptr, "Invalid fixed cHRM white point specified");
  923. #if !defined(PNG_NO_CONSOLE_IO)
  924. fprintf(stderr,"white_x=%ld, white_y=%ld\n",white_x, white_y);
  925. #endif
  926. return;
  927. }
  928. png_save_uint_32(buf, (png_uint_32)white_x);
  929. png_save_uint_32(buf + 4, (png_uint_32)white_y);
  930. if (red_x + red_y > 100000L)
  931. {
  932. png_warning(png_ptr, "Invalid cHRM fixed red point specified");
  933. return;
  934. }
  935. png_save_uint_32(buf + 8, (png_uint_32)red_x);
  936. png_save_uint_32(buf + 12, (png_uint_32)red_y);
  937. if (green_x + green_y > 100000L)
  938. {
  939. png_warning(png_ptr, "Invalid fixed cHRM green point specified");
  940. return;
  941. }
  942. png_save_uint_32(buf + 16, (png_uint_32)green_x);
  943. png_save_uint_32(buf + 20, (png_uint_32)green_y);
  944. if (blue_x + blue_y > 100000L)
  945. {
  946. png_warning(png_ptr, "Invalid fixed cHRM blue point specified");
  947. return;
  948. }
  949. png_save_uint_32(buf + 24, (png_uint_32)blue_x);
  950. png_save_uint_32(buf + 28, (png_uint_32)blue_y);
  951. png_write_chunk(png_ptr, png_cHRM, buf, (png_size_t)32);
  952. }
  953. #endif
  954. #endif
  955. #if defined(PNG_WRITE_tRNS_SUPPORTED)
  956. /* write the tRNS chunk */
  957. void /* PRIVATE */
  958. png_write_tRNS(png_structp png_ptr, png_bytep trans, png_color_16p tran,
  959. int num_trans, int color_type)
  960. {
  961. #ifdef PNG_USE_LOCAL_ARRAYS
  962. PNG_tRNS;
  963. #endif
  964. png_byte buf[6];
  965. png_debug(1, "in png_write_tRNS\n");
  966. if (color_type == PNG_COLOR_TYPE_PALETTE)
  967. {
  968. if (num_trans <= 0 || num_trans > (int)png_ptr->num_palette)
  969. {
  970. png_warning(png_ptr,"Invalid number of transparent colors specified");
  971. return;
  972. }
  973. /* write the chunk out as it is */
  974. png_write_chunk(png_ptr, png_tRNS, trans, (png_size_t)num_trans);
  975. }
  976. else if (color_type == PNG_COLOR_TYPE_GRAY)
  977. {
  978. /* one 16 bit value */
  979. if(tran->gray >= (1 << png_ptr->bit_depth))
  980. {
  981. png_warning(png_ptr,
  982. "Ignoring attempt to write tRNS chunk out-of-range for bit_depth");
  983. return;
  984. }
  985. png_save_uint_16(buf, tran->gray);
  986. png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)2);
  987. }
  988. else if (color_type == PNG_COLOR_TYPE_RGB)
  989. {
  990. /* three 16 bit values */
  991. png_save_uint_16(buf, tran->red);
  992. png_save_uint_16(buf + 2, tran->green);
  993. png_save_uint_16(buf + 4, tran->blue);
  994. if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
  995. {
  996. png_warning(png_ptr,
  997. "Ignoring attempt to write 16-bit tRNS chunk when bit_depth is 8");
  998. return;
  999. }
  1000. png_write_chunk(png_ptr, png_tRNS, buf, (png_size_t)6);
  1001. }
  1002. else
  1003. {
  1004. png_warning(png_ptr, "Can't write tRNS with an alpha channel");
  1005. }
  1006. }
  1007. #endif
  1008. #if defined(PNG_WRITE_bKGD_SUPPORTED)
  1009. /* write the background chunk */
  1010. void /* PRIVATE */
  1011. png_write_bKGD(png_structp png_ptr, png_color_16p back, int color_type)
  1012. {
  1013. #ifdef PNG_USE_LOCAL_ARRAYS
  1014. PNG_bKGD;
  1015. #endif
  1016. png_byte buf[6];
  1017. png_debug(1, "in png_write_bKGD\n");
  1018. if (color_type == PNG_COLOR_TYPE_PALETTE)
  1019. {
  1020. if (
  1021. #if defined(PNG_MNG_FEATURES_SUPPORTED)
  1022. (png_ptr->num_palette ||
  1023. (!(png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE))) &&
  1024. #endif
  1025. back->index > png_ptr->num_palette)
  1026. {
  1027. png_warning(png_ptr, "Invalid background palette index");
  1028. return;
  1029. }
  1030. buf[0] = back->index;
  1031. png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)1);
  1032. }
  1033. else if (color_type & PNG_COLOR_MASK_COLOR)
  1034. {
  1035. png_save_uint_16(buf, back->red);
  1036. png_save_uint_16(buf + 2, back->green);
  1037. png_save_uint_16(buf + 4, back->blue);
  1038. if(png_ptr->bit_depth == 8 && (buf[0] | buf[2] | buf[4]))
  1039. {
  1040. png_warning(png_ptr,
  1041. "Ignoring attempt to write 16-bit bKGD chunk when bit_depth is 8");
  1042. return;
  1043. }
  1044. png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)6);
  1045. }
  1046. else
  1047. {
  1048. if(back->gray >= (1 << png_ptr->bit_depth))
  1049. {
  1050. png_warning(png_ptr,
  1051. "Ignoring attempt to write bKGD chunk out-of-range for bit_depth");
  1052. return;
  1053. }
  1054. png_save_uint_16(buf, back->gray);
  1055. png_write_chunk(png_ptr, png_bKGD, buf, (png_size_t)2);
  1056. }
  1057. }
  1058. #endif
  1059. #if defined(PNG_WRITE_hIST_SUPPORTED)
  1060. /* write the histogram */
  1061. void /* PRIVATE */
  1062. png_write_hIST(png_structp png_ptr, png_uint_16p hist, int num_hist)
  1063. {
  1064. #ifdef PNG_USE_LOCAL_ARRAYS
  1065. PNG_hIST;
  1066. #endif
  1067. int i;
  1068. png_byte buf[3];
  1069. png_debug(1, "in png_write_hIST\n");
  1070. if (num_hist > (int)png_ptr->num_palette)
  1071. {
  1072. png_debug2(3, "num_hist = %d, num_palette = %d\n", num_hist,
  1073. png_ptr->num_palette);
  1074. png_warning(png_ptr, "Invalid number of histogram entries specified");
  1075. return;
  1076. }
  1077. png_write_chunk_start(png_ptr, png_hIST, (png_uint_32)(num_hist * 2));
  1078. for (i = 0; i < num_hist; i++)
  1079. {
  1080. png_save_uint_16(buf, hist[i]);
  1081. png_write_chunk_data(png_ptr, buf, (png_size_t)2);
  1082. }
  1083. png_write_chunk_end(png_ptr);
  1084. }
  1085. #endif
  1086. #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_pCAL_SUPPORTED) || \
  1087. defined(PNG_WRITE_iCCP_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
  1088. /* Check that the tEXt or zTXt keyword is valid per PNG 1.0 specification,
  1089. * and if invalid, correct the keyword rather than discarding the entire
  1090. * chunk. The PNG 1.0 specification requires keywords 1-79 characters in
  1091. * length, forbids leading or trailing whitespace, multiple internal spaces,
  1092. * and the non-break space (0x80) from ISO 8859-1. Returns keyword length.
  1093. *
  1094. * The new_key is allocated to hold the corrected keyword and must be freed
  1095. * by the calling routine. This avoids problems with trying to write to
  1096. * static keywords without having to have duplicate copies of the strings.
  1097. */
  1098. png_size_t /* PRIVATE */
  1099. png_check_keyword(png_structp png_ptr, png_charp key, png_charpp new_key)
  1100. {
  1101. png_size_t key_len;
  1102. png_charp kp, dp;
  1103. int kflag;
  1104. int kwarn=0;
  1105. png_debug(1, "in png_check_keyword\n");
  1106. *new_key = NULL;
  1107. if (key == NULL || (key_len = png_strlen(key)) == 0)
  1108. {
  1109. png_warning(png_ptr, "zero length keyword");
  1110. return ((png_size_t)0);
  1111. }
  1112. png_debug1(2, "Keyword to be checked is '%s'\n", key);
  1113. *new_key = (png_charp)png_malloc_warn(png_ptr, (png_uint_32)(key_len + 2));
  1114. if (*new_key == NULL)
  1115. {
  1116. png_warning(png_ptr, "Out of memory while procesing keyword");
  1117. return ((png_size_t)0);
  1118. }
  1119. /* Replace non-printing characters with a blank and print a warning */
  1120. for (kp = key, dp = *new_key; *kp != '\0'; kp++, dp++)
  1121. {
  1122. if ((png_byte)*kp < 0x20 ||
  1123. ((png_byte)*kp > 0x7E && (png_byte)*kp < 0xA1))
  1124. {
  1125. #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  1126. char msg[40];
  1127. png_snprintf(msg, 40,
  1128. "invalid keyword character 0x%02X", (png_byte)*kp);
  1129. png_warning(png_ptr, msg);
  1130. #else
  1131. png_warning(png_ptr, "invalid character in keyword");
  1132. #endif
  1133. *dp = ' ';
  1134. }
  1135. else
  1136. {
  1137. *dp = *kp;
  1138. }
  1139. }
  1140. *dp = '\0';
  1141. /* Remove any trailing white space. */
  1142. kp = *new_key + key_len - 1;
  1143. if (*kp == ' ')
  1144. {
  1145. png_warning(png_ptr, "trailing spaces removed from keyword");
  1146. while (*kp == ' ')
  1147. {
  1148. *(kp--) = '\0';
  1149. key_len--;
  1150. }
  1151. }
  1152. /* Remove any leading white space. */
  1153. kp = *new_key;
  1154. if (*kp == ' ')
  1155. {
  1156. png_warning(png_ptr, "leading spaces removed from keyword");
  1157. while (*kp == ' ')
  1158. {
  1159. kp++;
  1160. key_len--;
  1161. }
  1162. }
  1163. png_debug1(2, "Checking for multiple internal spaces in '%s'\n", kp);
  1164. /* Remove multiple internal spaces. */
  1165. for (kflag = 0, dp = *new_key; *kp != '\0'; kp++)
  1166. {
  1167. if (*kp == ' ' && kflag == 0)
  1168. {
  1169. *(dp++) = *kp;
  1170. kflag = 1;
  1171. }
  1172. else if (*kp == ' ')
  1173. {
  1174. key_len--;
  1175. kwarn=1;
  1176. }
  1177. else
  1178. {
  1179. *(dp++) = *kp;
  1180. kflag = 0;
  1181. }
  1182. }
  1183. *dp = '\0';
  1184. if(kwarn)
  1185. png_warning(png_ptr, "extra interior spaces removed from keyword");
  1186. if (key_len == 0)
  1187. {
  1188. png_free(png_ptr, *new_key);
  1189. *new_key=NULL;
  1190. png_warning(png_ptr, "Zero length keyword");
  1191. }
  1192. if (key_len > 79)
  1193. {
  1194. png_warning(png_ptr, "keyword length must be 1 - 79 characters");
  1195. new_key[79] = '\0';
  1196. key_len = 79;
  1197. }
  1198. return (key_len);
  1199. }
  1200. #endif
  1201. #if defined(PNG_WRITE_tEXt_SUPPORTED)
  1202. /* write a tEXt chunk */
  1203. void /* PRIVATE */
  1204. png_write_tEXt(png_structp png_ptr, png_charp key, png_charp text,
  1205. png_size_t text_len)
  1206. {
  1207. #ifdef PNG_USE_LOCAL_ARRAYS
  1208. PNG_tEXt;
  1209. #endif
  1210. png_size_t key_len;
  1211. png_charp new_key;
  1212. png_debug(1, "in png_write_tEXt\n");
  1213. if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
  1214. {
  1215. png_warning(png_ptr, "Empty keyword in tEXt chunk");
  1216. return;
  1217. }
  1218. if (text == NULL || *text == '\0')
  1219. text_len = 0;
  1220. else
  1221. text_len = png_strlen(text);
  1222. /* make sure we include the 0 after the key */
  1223. png_write_chunk_start(png_ptr, png_tEXt, (png_uint_32)key_len+text_len+1);
  1224. /*
  1225. * We leave it to the application to meet PNG-1.0 requirements on the
  1226. * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
  1227. * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
  1228. * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
  1229. */
  1230. png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
  1231. if (text_len)
  1232. png_write_chunk_data(png_ptr, (png_bytep)text, text_len);
  1233. png_write_chunk_end(png_ptr);
  1234. png_free(png_ptr, new_key);
  1235. }
  1236. #endif
  1237. #if defined(PNG_WRITE_zTXt_SUPPORTED)
  1238. /* write a compressed text chunk */
  1239. void /* PRIVATE */
  1240. png_write_zTXt(png_structp png_ptr, png_charp key, png_charp text,
  1241. png_size_t text_len, int compression)
  1242. {
  1243. #ifdef PNG_USE_LOCAL_ARRAYS
  1244. PNG_zTXt;
  1245. #endif
  1246. png_size_t key_len;
  1247. char buf[1];
  1248. png_charp new_key;
  1249. compression_state comp;
  1250. png_debug(1, "in png_write_zTXt\n");
  1251. comp.num_output_ptr = 0;
  1252. comp.max_output_ptr = 0;
  1253. comp.output_ptr = NULL;
  1254. comp.input = NULL;
  1255. comp.input_len = 0;
  1256. if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
  1257. {
  1258. png_warning(png_ptr, "Empty keyword in zTXt chunk");
  1259. return;
  1260. }
  1261. if (text == NULL || *text == '\0' || compression==PNG_TEXT_COMPRESSION_NONE)
  1262. {
  1263. png_write_tEXt(png_ptr, new_key, text, (png_size_t)0);
  1264. png_free(png_ptr, new_key);
  1265. return;
  1266. }
  1267. text_len = png_strlen(text);
  1268. /* compute the compressed data; do it now for the length */
  1269. text_len = png_text_compress(png_ptr, text, text_len, compression,
  1270. &comp);
  1271. /* write start of chunk */
  1272. png_write_chunk_start(png_ptr, png_zTXt, (png_uint_32)
  1273. (key_len+text_len+2));
  1274. /* write key */
  1275. png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
  1276. png_free(png_ptr, new_key);
  1277. buf[0] = (png_byte)compression;
  1278. /* write compression */
  1279. png_write_chunk_data(png_ptr, (png_bytep)buf, (png_size_t)1);
  1280. /* write the compressed data */
  1281. png_write_compressed_data_out(png_ptr, &comp);
  1282. /* close the chunk */
  1283. png_write_chunk_end(png_ptr);
  1284. }
  1285. #endif
  1286. #if defined(PNG_WRITE_iTXt_SUPPORTED)
  1287. /* write an iTXt chunk */
  1288. void /* PRIVATE */
  1289. png_write_iTXt(png_structp png_ptr, int compression, png_charp key,
  1290. png_charp lang, png_charp lang_key, png_charp text)
  1291. {
  1292. #ifdef PNG_USE_LOCAL_ARRAYS
  1293. PNG_iTXt;
  1294. #endif
  1295. png_size_t lang_len, key_len, lang_key_len, text_len;
  1296. png_charp new_lang, new_key;
  1297. png_byte cbuf[2];
  1298. compression_state comp;
  1299. png_debug(1, "in png_write_iTXt\n");
  1300. comp.num_output_ptr = 0;
  1301. comp.max_output_ptr = 0;
  1302. comp.output_ptr = NULL;
  1303. comp.input = NULL;
  1304. if (key == NULL || (key_len = png_check_keyword(png_ptr, key, &new_key))==0)
  1305. {
  1306. png_warning(png_ptr, "Empty keyword in iTXt chunk");
  1307. return;
  1308. }
  1309. if (lang == NULL || (lang_len = png_check_keyword(png_ptr, lang, &new_lang))==0)
  1310. {
  1311. png_warning(png_ptr, "Empty language field in iTXt chunk");
  1312. new_lang = NULL;
  1313. lang_len = 0;
  1314. }
  1315. if (lang_key == NULL)
  1316. lang_key_len = 0;
  1317. else
  1318. lang_key_len = png_strlen(lang_key);
  1319. if (text == NULL)
  1320. text_len = 0;
  1321. else
  1322. text_len = png_strlen(text);
  1323. /* compute the compressed data; do it now for the length */
  1324. text_len = png_text_compress(png_ptr, text, text_len, compression-2,
  1325. &comp);
  1326. /* make sure we include the compression flag, the compression byte,
  1327. * and the NULs after the key, lang, and lang_key parts */
  1328. png_write_chunk_start(png_ptr, png_iTXt,
  1329. (png_uint_32)(
  1330. 5 /* comp byte, comp flag, terminators for key, lang and lang_key */
  1331. + key_len
  1332. + lang_len
  1333. + lang_key_len
  1334. + text_len));
  1335. /*
  1336. * We leave it to the application to meet PNG-1.0 requirements on the
  1337. * contents of the text. PNG-1.0 through PNG-1.2 discourage the use of
  1338. * any non-Latin-1 characters except for NEWLINE. ISO PNG will forbid them.
  1339. * The NUL character is forbidden by PNG-1.0 through PNG-1.2 and ISO PNG.
  1340. */
  1341. png_write_chunk_data(png_ptr, (png_bytep)new_key, key_len + 1);
  1342. /* set the compression flag */
  1343. if (compression == PNG_ITXT_COMPRESSION_NONE || \
  1344. compression == PNG_TEXT_COMPRESSION_NONE)
  1345. cbuf[0] = 0;
  1346. else /* compression == PNG_ITXT_COMPRESSION_zTXt */
  1347. cbuf[0] = 1;
  1348. /* set the compression method */
  1349. cbuf[1] = 0;
  1350. png_write_chunk_data(png_ptr, cbuf, 2);
  1351. cbuf[0] = 0;
  1352. png_write_chunk_data(png_ptr, (new_lang ? (png_bytep)new_lang : cbuf), lang_len + 1);
  1353. png_write_chunk_data(png_ptr, (lang_key ? (png_bytep)lang_key : cbuf), lang_key_len + 1);
  1354. png_write_compressed_data_out(png_ptr, &comp);
  1355. png_write_chunk_end(png_ptr);
  1356. png_free(png_ptr, new_key);
  1357. if (new_lang)
  1358. png_free(png_ptr, new_lang);
  1359. }
  1360. #endif
  1361. #if defined(PNG_WRITE_oFFs_SUPPORTED)
  1362. /* write the oFFs chunk */
  1363. void /* PRIVATE */
  1364. png_write_oFFs(png_structp png_ptr, png_int_32 x_offset, png_int_32 y_offset,
  1365. int unit_type)
  1366. {
  1367. #ifdef PNG_USE_LOCAL_ARRAYS
  1368. PNG_oFFs;
  1369. #endif
  1370. png_byte buf[9];
  1371. png_debug(1, "in png_write_oFFs\n");
  1372. if (unit_type >= PNG_OFFSET_LAST)
  1373. png_warning(png_ptr, "Unrecognized unit type for oFFs chunk");
  1374. png_save_int_32(buf, x_offset);
  1375. png_save_int_32(buf + 4, y_offset);
  1376. buf[8] = (png_byte)unit_type;
  1377. png_write_chunk(png_ptr, png_oFFs, buf, (png_size_t)9);
  1378. }
  1379. #endif
  1380. #if defined(PNG_WRITE_pCAL_SUPPORTED)
  1381. /* write the pCAL chunk (described in the PNG extensions document) */
  1382. void /* PRIVATE */
  1383. png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
  1384. png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
  1385. {
  1386. #ifdef PNG_USE_LOCAL_ARRAYS
  1387. PNG_pCAL;
  1388. #endif
  1389. png_size_t purpose_len, units_len, total_len;
  1390. png_uint_32p params_len;
  1391. png_byte buf[10];
  1392. png_charp new_purpose;
  1393. int i;
  1394. png_debug1(1, "in png_write_pCAL (%d parameters)\n", nparams);
  1395. if (type >= PNG_EQUATION_LAST)
  1396. png_warning(png_ptr, "Unrecognized equation type for pCAL chunk");
  1397. purpose_len = png_check_keyword(png_ptr, purpose, &new_purpose) + 1;
  1398. png_debug1(3, "pCAL purpose length = %d\n", (int)purpose_len);
  1399. units_len = png_strlen(units) + (nparams == 0 ? 0 : 1);
  1400. png_debug1(3, "pCAL units length = %d\n", (int)units_len);
  1401. total_len = purpose_len + units_len + 10;
  1402. params_len = (png_uint_32p)png_malloc(png_ptr, (png_uint_32)(nparams
  1403. *png_sizeof(png_uint_32)));
  1404. /* Find the length of each parameter, making sure we don't count the
  1405. null terminator for the last parameter. */
  1406. for (i = 0; i < nparams; i++)
  1407. {
  1408. params_len[i] = png_strlen(params[i]) + (i == nparams - 1 ? 0 : 1);
  1409. png_debug2(3, "pCAL parameter %d length = %lu\n", i, params_len[i]);
  1410. total_len += (png_size_t)params_len[i];
  1411. }
  1412. png_debug1(3, "pCAL total length = %d\n", (int)total_len);
  1413. png_write_chunk_start(png_ptr, png_pCAL, (png_uint_32)total_len);
  1414. png_write_chunk_data(png_ptr, (png_bytep)new_purpose, purpose_len);
  1415. png_save_int_32(buf, X0);
  1416. png_save_int_32(buf + 4, X1);
  1417. buf[8] = (png_byte)type;
  1418. buf[9] = (png_byte)nparams;
  1419. png_write_chunk_data(png_ptr, buf, (png_size_t)10);
  1420. png_write_chunk_data(png_ptr, (png_bytep)units, (png_size_t)units_len);
  1421. png_free(png_ptr, new_purpose);
  1422. for (i = 0; i < nparams; i++)
  1423. {
  1424. png_write_chunk_data(png_ptr, (png_bytep)params[i],
  1425. (png_size_t)params_len[i]);
  1426. }
  1427. png_free(png_ptr, params_len);
  1428. png_write_chunk_end(png_ptr);
  1429. }
  1430. #endif
  1431. #if defined(PNG_WRITE_sCAL_SUPPORTED)
  1432. /* write the sCAL chunk */
  1433. #if defined(PNG_FLOATING_POINT_SUPPORTED) && !defined(PNG_NO_STDIO)
  1434. void /* PRIVATE */
  1435. png_write_sCAL(png_structp png_ptr, int unit, double width, double height)
  1436. {
  1437. #ifdef PNG_USE_LOCAL_ARRAYS
  1438. PNG_sCAL;
  1439. #endif
  1440. char buf[64];
  1441. png_size_t total_len;
  1442. png_debug(1, "in png_write_sCAL\n");
  1443. buf[0] = (char)unit;
  1444. #if defined(_WIN32_WCE)
  1445. /* sprintf() function is not supported on WindowsCE */
  1446. {
  1447. wchar_t wc_buf[32];
  1448. size_t wc_len;
  1449. swprintf(wc_buf, TEXT("%12.12e"), width);
  1450. wc_len = wcslen(wc_buf);
  1451. WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + 1, wc_len, NULL, NULL);
  1452. total_len = wc_len + 2;
  1453. swprintf(wc_buf, TEXT("%12.12e"), height);
  1454. wc_len = wcslen(wc_buf);
  1455. WideCharToMultiByte(CP_ACP, 0, wc_buf, -1, buf + total_len, wc_len,
  1456. NULL, NULL);
  1457. total_len += wc_len;
  1458. }
  1459. #else
  1460. png_snprintf(buf + 1, 63, "%12.12e", width);
  1461. total_len = 1 + png_strlen(buf + 1) + 1;
  1462. png_snprintf(buf + total_len, 64-total_len, "%12.12e", height);
  1463. total_len += png_strlen(buf + total_len);
  1464. #endif
  1465. png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
  1466. png_write_chunk(png_ptr, png_sCAL, (png_bytep)buf, total_len);
  1467. }
  1468. #else
  1469. #ifdef PNG_FIXED_POINT_SUPPORTED
  1470. void /* PRIVATE */
  1471. png_write_sCAL_s(png_structp png_ptr, int unit, png_charp width,
  1472. png_charp height)
  1473. {
  1474. #ifdef PNG_USE_LOCAL_ARRAYS
  1475. PNG_sCAL;
  1476. #endif
  1477. png_byte buf[64];
  1478. png_size_t wlen, hlen, total_len;
  1479. png_debug(1, "in png_write_sCAL_s\n");
  1480. wlen = png_strlen(width);
  1481. hlen = png_strlen(height);
  1482. total_len = wlen + hlen + 2;
  1483. if (total_len > 64)
  1484. {
  1485. png_warning(png_ptr, "Can't write sCAL (buffer too small)");
  1486. return;
  1487. }
  1488. buf[0] = (png_byte)unit;
  1489. png_memcpy(buf + 1, width, wlen + 1); /* append the '\0' here */
  1490. png_memcpy(buf + wlen + 2, height, hlen); /* do NOT append the '\0' here */
  1491. png_debug1(3, "sCAL total length = %u\n", (unsigned int)total_len);
  1492. png_write_chunk(png_ptr, png_sCAL, buf, total_len);
  1493. }
  1494. #endif
  1495. #endif
  1496. #endif
  1497. #if defined(PNG_WRITE_pHYs_SUPPORTED)
  1498. /* write the pHYs chunk */
  1499. void /* PRIVATE */
  1500. png_write_pHYs(png_structp png_ptr, png_uint_32 x_pixels_per_unit,
  1501. png_uint_32 y_pixels_per_unit,
  1502. int unit_type)
  1503. {
  1504. #ifdef PNG_USE_LOCAL_ARRAYS
  1505. PNG_pHYs;
  1506. #endif
  1507. png_byte buf[9];
  1508. png_debug(1, "in png_write_pHYs\n");
  1509. if (unit_type >= PNG_RESOLUTION_LAST)
  1510. png_warning(png_ptr, "Unrecognized unit type for pHYs chunk");
  1511. png_save_uint_32(buf, x_pixels_per_unit);
  1512. png_save_uint_32(buf + 4, y_pixels_per_unit);
  1513. buf[8] = (png_byte)unit_type;
  1514. png_write_chunk(png_ptr, png_pHYs, buf, (png_size_t)9);
  1515. }
  1516. #endif
  1517. #if defined(PNG_WRITE_tIME_SUPPORTED)
  1518. /* Write the tIME chunk. Use either png_convert_from_struct_tm()
  1519. * or png_convert_from_time_t(), or fill in the structure yourself.
  1520. */
  1521. void /* PRIVATE */
  1522. png_write_tIME(png_structp png_ptr, png_timep mod_time)
  1523. {
  1524. #ifdef PNG_USE_LOCAL_ARRAYS
  1525. PNG_tIME;
  1526. #endif
  1527. png_byte buf[7];
  1528. png_debug(1, "in png_write_tIME\n");
  1529. if (mod_time->month > 12 || mod_time->month < 1 ||
  1530. mod_time->day > 31 || mod_time->day < 1 ||
  1531. mod_time->hour > 23 || mod_time->second > 60)
  1532. {
  1533. png_warning(png_ptr, "Invalid time specified for tIME chunk");
  1534. return;
  1535. }
  1536. png_save_uint_16(buf, mod_time->year);
  1537. buf[2] = mod_time->month;
  1538. buf[3] = mod_time->day;
  1539. buf[4] = mod_time->hour;
  1540. buf[5] = mod_time->minute;
  1541. buf[6] = mod_time->second;
  1542. png_write_chunk(png_ptr, png_tIME, buf, (png_size_t)7);
  1543. }
  1544. #endif
  1545. /* initializes the row writing capability of libpng */
  1546. void /* PRIVATE */
  1547. png_write_start_row(png_structp png_ptr)
  1548. {
  1549. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  1550. #ifdef PNG_USE_LOCAL_ARRAYS
  1551. /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  1552. /* start of interlace block */
  1553. int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  1554. /* offset to next interlace block */
  1555. int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  1556. /* start of interlace block in the y direction */
  1557. int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  1558. /* offset to next interlace block in the y direction */
  1559. int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  1560. #endif
  1561. #endif
  1562. png_size_t buf_size;
  1563. png_debug(1, "in png_write_start_row\n");
  1564. buf_size = (png_size_t)(PNG_ROWBYTES(
  1565. png_ptr->usr_channels*png_ptr->usr_bit_depth,png_ptr->width)+1);
  1566. /* set up row buffer */
  1567. png_ptr->row_buf = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
  1568. png_ptr->row_buf[0] = PNG_FILTER_VALUE_NONE;
  1569. #ifndef PNG_NO_WRITE_FILTERING
  1570. /* set up filtering buffer, if using this filter */
  1571. if (png_ptr->do_filter & PNG_FILTER_SUB)
  1572. {
  1573. png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
  1574. (png_ptr->rowbytes + 1));
  1575. png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
  1576. }
  1577. /* We only need to keep the previous row if we are using one of these. */
  1578. if (png_ptr->do_filter & (PNG_FILTER_AVG | PNG_FILTER_UP | PNG_FILTER_PAETH))
  1579. {
  1580. /* set up previous row buffer */
  1581. png_ptr->prev_row = (png_bytep)png_malloc(png_ptr, (png_uint_32)buf_size);
  1582. png_memset(png_ptr->prev_row, 0, buf_size);
  1583. if (png_ptr->do_filter & PNG_FILTER_UP)
  1584. {
  1585. png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
  1586. (png_ptr->rowbytes + 1));
  1587. png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
  1588. }
  1589. if (png_ptr->do_filter & PNG_FILTER_AVG)
  1590. {
  1591. png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
  1592. (png_ptr->rowbytes + 1));
  1593. png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
  1594. }
  1595. if (png_ptr->do_filter & PNG_FILTER_PAETH)
  1596. {
  1597. png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
  1598. (png_ptr->rowbytes + 1));
  1599. png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
  1600. }
  1601. #endif /* PNG_NO_WRITE_FILTERING */
  1602. }
  1603. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  1604. /* if interlaced, we need to set up width and height of pass */
  1605. if (png_ptr->interlaced)
  1606. {
  1607. if (!(png_ptr->transformations & PNG_INTERLACE))
  1608. {
  1609. png_ptr->num_rows = (png_ptr->height + png_pass_yinc[0] - 1 -
  1610. png_pass_ystart[0]) / png_pass_yinc[0];
  1611. png_ptr->usr_width = (png_ptr->width + png_pass_inc[0] - 1 -
  1612. png_pass_start[0]) / png_pass_inc[0];
  1613. }
  1614. else
  1615. {
  1616. png_ptr->num_rows = png_ptr->height;
  1617. png_ptr->usr_width = png_ptr->width;
  1618. }
  1619. }
  1620. else
  1621. #endif
  1622. {
  1623. png_ptr->num_rows = png_ptr->height;
  1624. png_ptr->usr_width = png_ptr->width;
  1625. }
  1626. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  1627. png_ptr->zstream.next_out = png_ptr->zbuf;
  1628. }
  1629. /* Internal use only. Called when finished processing a row of data. */
  1630. void /* PRIVATE */
  1631. png_write_finish_row(png_structp png_ptr)
  1632. {
  1633. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  1634. #ifdef PNG_USE_LOCAL_ARRAYS
  1635. /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  1636. /* start of interlace block */
  1637. int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  1638. /* offset to next interlace block */
  1639. int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  1640. /* start of interlace block in the y direction */
  1641. int png_pass_ystart[7] = {0, 0, 4, 0, 2, 0, 1};
  1642. /* offset to next interlace block in the y direction */
  1643. int png_pass_yinc[7] = {8, 8, 8, 4, 4, 2, 2};
  1644. #endif
  1645. #endif
  1646. int ret;
  1647. png_debug(1, "in png_write_finish_row\n");
  1648. /* next row */
  1649. png_ptr->row_number++;
  1650. /* see if we are done */
  1651. if (png_ptr->row_number < png_ptr->num_rows)
  1652. return;
  1653. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  1654. /* if interlaced, go to next pass */
  1655. if (png_ptr->interlaced)
  1656. {
  1657. png_ptr->row_number = 0;
  1658. if (png_ptr->transformations & PNG_INTERLACE)
  1659. {
  1660. png_ptr->pass++;
  1661. }
  1662. else
  1663. {
  1664. /* loop until we find a non-zero width or height pass */
  1665. do
  1666. {
  1667. png_ptr->pass++;
  1668. if (png_ptr->pass >= 7)
  1669. break;
  1670. png_ptr->usr_width = (png_ptr->width +
  1671. png_pass_inc[png_ptr->pass] - 1 -
  1672. png_pass_start[png_ptr->pass]) /
  1673. png_pass_inc[png_ptr->pass];
  1674. png_ptr->num_rows = (png_ptr->height +
  1675. png_pass_yinc[png_ptr->pass] - 1 -
  1676. png_pass_ystart[png_ptr->pass]) /
  1677. png_pass_yinc[png_ptr->pass];
  1678. if (png_ptr->transformations & PNG_INTERLACE)
  1679. break;
  1680. } while (png_ptr->usr_width == 0 || png_ptr->num_rows == 0);
  1681. }
  1682. /* reset the row above the image for the next pass */
  1683. if (png_ptr->pass < 7)
  1684. {
  1685. if (png_ptr->prev_row != NULL)
  1686. png_memset(png_ptr->prev_row, 0,
  1687. (png_size_t)(PNG_ROWBYTES(png_ptr->usr_channels*
  1688. png_ptr->usr_bit_depth,png_ptr->width))+1);
  1689. return;
  1690. }
  1691. }
  1692. #endif
  1693. /* if we get here, we've just written the last row, so we need
  1694. to flush the compressor */
  1695. do
  1696. {
  1697. /* tell the compressor we are done */
  1698. ret = deflate(&png_ptr->zstream, Z_FINISH);
  1699. /* check for an error */
  1700. if (ret == Z_OK)
  1701. {
  1702. /* check to see if we need more room */
  1703. if (!(png_ptr->zstream.avail_out))
  1704. {
  1705. png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  1706. png_ptr->zstream.next_out = png_ptr->zbuf;
  1707. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  1708. }
  1709. }
  1710. else if (ret != Z_STREAM_END)
  1711. {
  1712. if (png_ptr->zstream.msg != NULL)
  1713. png_error(png_ptr, png_ptr->zstream.msg);
  1714. else
  1715. png_error(png_ptr, "zlib error");
  1716. }
  1717. } while (ret != Z_STREAM_END);
  1718. /* write any extra space */
  1719. if (png_ptr->zstream.avail_out < png_ptr->zbuf_size)
  1720. {
  1721. png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size -
  1722. png_ptr->zstream.avail_out);
  1723. }
  1724. deflateReset(&png_ptr->zstream);
  1725. png_ptr->zstream.data_type = Z_BINARY;
  1726. }
  1727. #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
  1728. /* Pick out the correct pixels for the interlace pass.
  1729. * The basic idea here is to go through the row with a source
  1730. * pointer and a destination pointer (sp and dp), and copy the
  1731. * correct pixels for the pass. As the row gets compacted,
  1732. * sp will always be >= dp, so we should never overwrite anything.
  1733. * See the default: case for the easiest code to understand.
  1734. */
  1735. void /* PRIVATE */
  1736. png_do_write_interlace(png_row_infop row_info, png_bytep row, int pass)
  1737. {
  1738. #ifdef PNG_USE_LOCAL_ARRAYS
  1739. /* arrays to facilitate easy interlacing - use pass (0 - 6) as index */
  1740. /* start of interlace block */
  1741. int png_pass_start[7] = {0, 4, 0, 2, 0, 1, 0};
  1742. /* offset to next interlace block */
  1743. int png_pass_inc[7] = {8, 8, 4, 4, 2, 2, 1};
  1744. #endif
  1745. png_debug(1, "in png_do_write_interlace\n");
  1746. /* we don't have to do anything on the last pass (6) */
  1747. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  1748. if (row != NULL && row_info != NULL && pass < 6)
  1749. #else
  1750. if (pass < 6)
  1751. #endif
  1752. {
  1753. /* each pixel depth is handled separately */
  1754. switch (row_info->pixel_depth)
  1755. {
  1756. case 1:
  1757. {
  1758. png_bytep sp;
  1759. png_bytep dp;
  1760. int shift;
  1761. int d;
  1762. int value;
  1763. png_uint_32 i;
  1764. png_uint_32 row_width = row_info->width;
  1765. dp = row;
  1766. d = 0;
  1767. shift = 7;
  1768. for (i = png_pass_start[pass]; i < row_width;
  1769. i += png_pass_inc[pass])
  1770. {
  1771. sp = row + (png_size_t)(i >> 3);
  1772. value = (int)(*sp >> (7 - (int)(i & 0x07))) & 0x01;
  1773. d |= (value << shift);
  1774. if (shift == 0)
  1775. {
  1776. shift = 7;
  1777. *dp++ = (png_byte)d;
  1778. d = 0;
  1779. }
  1780. else
  1781. shift--;
  1782. }
  1783. if (shift != 7)
  1784. *dp = (png_byte)d;
  1785. break;
  1786. }
  1787. case 2:
  1788. {
  1789. png_bytep sp;
  1790. png_bytep dp;
  1791. int shift;
  1792. int d;
  1793. int value;
  1794. png_uint_32 i;
  1795. png_uint_32 row_width = row_info->width;
  1796. dp = row;
  1797. shift = 6;
  1798. d = 0;
  1799. for (i = png_pass_start[pass]; i < row_width;
  1800. i += png_pass_inc[pass])
  1801. {
  1802. sp = row + (png_size_t)(i >> 2);
  1803. value = (*sp >> ((3 - (int)(i & 0x03)) << 1)) & 0x03;
  1804. d |= (value << shift);
  1805. if (shift == 0)
  1806. {
  1807. shift = 6;
  1808. *dp++ = (png_byte)d;
  1809. d = 0;
  1810. }
  1811. else
  1812. shift -= 2;
  1813. }
  1814. if (shift != 6)
  1815. *dp = (png_byte)d;
  1816. break;
  1817. }
  1818. case 4:
  1819. {
  1820. png_bytep sp;
  1821. png_bytep dp;
  1822. int shift;
  1823. int d;
  1824. int value;
  1825. png_uint_32 i;
  1826. png_uint_32 row_width = row_info->width;
  1827. dp = row;
  1828. shift = 4;
  1829. d = 0;
  1830. for (i = png_pass_start[pass]; i < row_width;
  1831. i += png_pass_inc[pass])
  1832. {
  1833. sp = row + (png_size_t)(i >> 1);
  1834. value = (*sp >> ((1 - (int)(i & 0x01)) << 2)) & 0x0f;
  1835. d |= (value << shift);
  1836. if (shift == 0)
  1837. {
  1838. shift = 4;
  1839. *dp++ = (png_byte)d;
  1840. d = 0;
  1841. }
  1842. else
  1843. shift -= 4;
  1844. }
  1845. if (shift != 4)
  1846. *dp = (png_byte)d;
  1847. break;
  1848. }
  1849. default:
  1850. {
  1851. png_bytep sp;
  1852. png_bytep dp;
  1853. png_uint_32 i;
  1854. png_uint_32 row_width = row_info->width;
  1855. png_size_t pixel_bytes;
  1856. /* start at the beginning */
  1857. dp = row;
  1858. /* find out how many bytes each pixel takes up */
  1859. pixel_bytes = (row_info->pixel_depth >> 3);
  1860. /* loop through the row, only looking at the pixels that
  1861. matter */
  1862. for (i = png_pass_start[pass]; i < row_width;
  1863. i += png_pass_inc[pass])
  1864. {
  1865. /* find out where the original pixel is */
  1866. sp = row + (png_size_t)i * pixel_bytes;
  1867. /* move the pixel */
  1868. if (dp != sp)
  1869. png_memcpy(dp, sp, pixel_bytes);
  1870. /* next pixel */
  1871. dp += pixel_bytes;
  1872. }
  1873. break;
  1874. }
  1875. }
  1876. /* set new row width */
  1877. row_info->width = (row_info->width +
  1878. png_pass_inc[pass] - 1 -
  1879. png_pass_start[pass]) /
  1880. png_pass_inc[pass];
  1881. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
  1882. row_info->width);
  1883. }
  1884. }
  1885. #endif
  1886. /* This filters the row, chooses which filter to use, if it has not already
  1887. * been specified by the application, and then writes the row out with the
  1888. * chosen filter.
  1889. */
  1890. #define PNG_MAXSUM (((png_uint_32)(-1)) >> 1)
  1891. #define PNG_HISHIFT 10
  1892. #define PNG_LOMASK ((png_uint_32)0xffffL)
  1893. #define PNG_HIMASK ((png_uint_32)(~PNG_LOMASK >> PNG_HISHIFT))
  1894. void /* PRIVATE */
  1895. png_write_find_filter(png_structp png_ptr, png_row_infop row_info)
  1896. {
  1897. png_bytep best_row;
  1898. #ifndef PNG_NO_WRITE_FILTER
  1899. png_bytep prev_row, row_buf;
  1900. png_uint_32 mins, bpp;
  1901. png_byte filter_to_do = png_ptr->do_filter;
  1902. png_uint_32 row_bytes = row_info->rowbytes;
  1903. #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
  1904. int num_p_filters = (int)png_ptr->num_prev_filters;
  1905. #endif
  1906. png_debug(1, "in png_write_find_filter\n");
  1907. /* find out how many bytes offset each pixel is */
  1908. bpp = (row_info->pixel_depth + 7) >> 3;
  1909. prev_row = png_ptr->prev_row;
  1910. #endif
  1911. best_row = png_ptr->row_buf;
  1912. #ifndef PNG_NO_WRITE_FILTER
  1913. row_buf = best_row;
  1914. mins = PNG_MAXSUM;
  1915. /* The prediction method we use is to find which method provides the
  1916. * smallest value when summing the absolute values of the distances
  1917. * from zero, using anything >= 128 as negative numbers. This is known
  1918. * as the "minimum sum of absolute differences" heuristic. Other
  1919. * heuristics are the "weighted minimum sum of absolute differences"
  1920. * (experimental and can in theory improve compression), and the "zlib
  1921. * predictive" method (not implemented yet), which does test compressions
  1922. * of lines using different filter methods, and then chooses the
  1923. * (series of) filter(s) that give minimum compressed data size (VERY
  1924. * computationally expensive).
  1925. *
  1926. * GRR 980525: consider also
  1927. * (1) minimum sum of absolute differences from running average (i.e.,
  1928. * keep running sum of non-absolute differences & count of bytes)
  1929. * [track dispersion, too? restart average if dispersion too large?]
  1930. * (1b) minimum sum of absolute differences from sliding average, probably
  1931. * with window size <= deflate window (usually 32K)
  1932. * (2) minimum sum of squared differences from zero or running average
  1933. * (i.e., ~ root-mean-square approach)
  1934. */
  1935. /* We don't need to test the 'no filter' case if this is the only filter
  1936. * that has been chosen, as it doesn't actually do anything to the data.
  1937. */
  1938. if ((filter_to_do & PNG_FILTER_NONE) &&
  1939. filter_to_do != PNG_FILTER_NONE)
  1940. {
  1941. png_bytep rp;
  1942. png_uint_32 sum = 0;
  1943. png_uint_32 i;
  1944. int v;
  1945. for (i = 0, rp = row_buf + 1; i < row_bytes; i++, rp++)
  1946. {
  1947. v = *rp;
  1948. sum += (v < 128) ? v : 256 - v;
  1949. }
  1950. #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
  1951. if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  1952. {
  1953. png_uint_32 sumhi, sumlo;
  1954. int j;
  1955. sumlo = sum & PNG_LOMASK;
  1956. sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK; /* Gives us some footroom */
  1957. /* Reduce the sum if we match any of the previous rows */
  1958. for (j = 0; j < num_p_filters; j++)
  1959. {
  1960. if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
  1961. {
  1962. sumlo = (sumlo * png_ptr->filter_weights[j]) >>
  1963. PNG_WEIGHT_SHIFT;
  1964. sumhi = (sumhi * png_ptr->filter_weights[j]) >>
  1965. PNG_WEIGHT_SHIFT;
  1966. }
  1967. }
  1968. /* Factor in the cost of this filter (this is here for completeness,
  1969. * but it makes no sense to have a "cost" for the NONE filter, as
  1970. * it has the minimum possible computational cost - none).
  1971. */
  1972. sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
  1973. PNG_COST_SHIFT;
  1974. sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_NONE]) >>
  1975. PNG_COST_SHIFT;
  1976. if (sumhi > PNG_HIMASK)
  1977. sum = PNG_MAXSUM;
  1978. else
  1979. sum = (sumhi << PNG_HISHIFT) + sumlo;
  1980. }
  1981. #endif
  1982. mins = sum;
  1983. }
  1984. /* sub filter */
  1985. if (filter_to_do == PNG_FILTER_SUB)
  1986. /* it's the only filter so no testing is needed */
  1987. {
  1988. png_bytep rp, lp, dp;
  1989. png_uint_32 i;
  1990. for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
  1991. i++, rp++, dp++)
  1992. {
  1993. *dp = *rp;
  1994. }
  1995. for (lp = row_buf + 1; i < row_bytes;
  1996. i++, rp++, lp++, dp++)
  1997. {
  1998. *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
  1999. }
  2000. best_row = png_ptr->sub_row;
  2001. }
  2002. else if (filter_to_do & PNG_FILTER_SUB)
  2003. {
  2004. png_bytep rp, dp, lp;
  2005. png_uint_32 sum = 0, lmins = mins;
  2006. png_uint_32 i;
  2007. int v;
  2008. #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
  2009. /* We temporarily increase the "minimum sum" by the factor we
  2010. * would reduce the sum of this filter, so that we can do the
  2011. * early exit comparison without scaling the sum each time.
  2012. */
  2013. if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  2014. {
  2015. int j;
  2016. png_uint_32 lmhi, lmlo;
  2017. lmlo = lmins & PNG_LOMASK;
  2018. lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
  2019. for (j = 0; j < num_p_filters; j++)
  2020. {
  2021. if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
  2022. {
  2023. lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
  2024. PNG_WEIGHT_SHIFT;
  2025. lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
  2026. PNG_WEIGHT_SHIFT;
  2027. }
  2028. }
  2029. lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
  2030. PNG_COST_SHIFT;
  2031. lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
  2032. PNG_COST_SHIFT;
  2033. if (lmhi > PNG_HIMASK)
  2034. lmins = PNG_MAXSUM;
  2035. else
  2036. lmins = (lmhi << PNG_HISHIFT) + lmlo;
  2037. }
  2038. #endif
  2039. for (i = 0, rp = row_buf + 1, dp = png_ptr->sub_row + 1; i < bpp;
  2040. i++, rp++, dp++)
  2041. {
  2042. v = *dp = *rp;
  2043. sum += (v < 128) ? v : 256 - v;
  2044. }
  2045. for (lp = row_buf + 1; i < row_bytes;
  2046. i++, rp++, lp++, dp++)
  2047. {
  2048. v = *dp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
  2049. sum += (v < 128) ? v : 256 - v;
  2050. if (sum > lmins) /* We are already worse, don't continue. */
  2051. break;
  2052. }
  2053. #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
  2054. if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  2055. {
  2056. int j;
  2057. png_uint_32 sumhi, sumlo;
  2058. sumlo = sum & PNG_LOMASK;
  2059. sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
  2060. for (j = 0; j < num_p_filters; j++)
  2061. {
  2062. if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_SUB)
  2063. {
  2064. sumlo = (sumlo * png_ptr->inv_filter_weights[j]) >>
  2065. PNG_WEIGHT_SHIFT;
  2066. sumhi = (sumhi * png_ptr->inv_filter_weights[j]) >>
  2067. PNG_WEIGHT_SHIFT;
  2068. }
  2069. }
  2070. sumlo = (sumlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
  2071. PNG_COST_SHIFT;
  2072. sumhi = (sumhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_SUB]) >>
  2073. PNG_COST_SHIFT;
  2074. if (sumhi > PNG_HIMASK)
  2075. sum = PNG_MAXSUM;
  2076. else
  2077. sum = (sumhi << PNG_HISHIFT) + sumlo;
  2078. }
  2079. #endif
  2080. if (sum < mins)
  2081. {
  2082. mins = sum;
  2083. best_row = png_ptr->sub_row;
  2084. }
  2085. }
  2086. /* up filter */
  2087. if (filter_to_do == PNG_FILTER_UP)
  2088. {
  2089. png_bytep rp, dp, pp;
  2090. png_uint_32 i;
  2091. for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
  2092. pp = prev_row + 1; i < row_bytes;
  2093. i++, rp++, pp++, dp++)
  2094. {
  2095. *dp = (png_byte)(((int)*rp - (int)*pp) & 0xff);
  2096. }
  2097. best_row = png_ptr->up_row;
  2098. }
  2099. else if (filter_to_do & PNG_FILTER_UP)
  2100. {
  2101. png_bytep rp, dp, pp;
  2102. png_uint_32 sum = 0, lmins = mins;
  2103. png_uint_32 i;
  2104. int v;
  2105. #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
  2106. if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  2107. {
  2108. int j;
  2109. png_uint_32 lmhi, lmlo;
  2110. lmlo = lmins & PNG_LOMASK;
  2111. lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
  2112. for (j = 0; j < num_p_filters; j++)
  2113. {
  2114. if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
  2115. {
  2116. lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
  2117. PNG_WEIGHT_SHIFT;
  2118. lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
  2119. PNG_WEIGHT_SHIFT;
  2120. }
  2121. }
  2122. lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
  2123. PNG_COST_SHIFT;
  2124. lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_UP]) >>
  2125. PNG_COST_SHIFT;
  2126. if (lmhi > PNG_HIMASK)
  2127. lmins = PNG_MAXSUM;
  2128. else
  2129. lmins = (lmhi << PNG_HISHIFT) + lmlo;
  2130. }
  2131. #endif
  2132. for (i = 0, rp = row_buf + 1, dp = png_ptr->up_row + 1,
  2133. pp = prev_row + 1; i < row_bytes; i++)
  2134. {
  2135. v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
  2136. sum += (v < 128) ? v : 256 - v;
  2137. if (sum > lmins) /* We are already worse, don't continue. */
  2138. break;
  2139. }
  2140. #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
  2141. if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  2142. {
  2143. int j;
  2144. png_uint_32 sumhi, sumlo;
  2145. sumlo = sum & PNG_LOMASK;
  2146. sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
  2147. for (j = 0; j < num_p_filters; j++)
  2148. {
  2149. if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_UP)
  2150. {
  2151. sumlo = (sumlo * png_ptr->filter_weights[j]) >>
  2152. PNG_WEIGHT_SHIFT;
  2153. sumhi = (sumhi * png_ptr->filter_weights[j]) >>
  2154. PNG_WEIGHT_SHIFT;
  2155. }
  2156. }
  2157. sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
  2158. PNG_COST_SHIFT;
  2159. sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_UP]) >>
  2160. PNG_COST_SHIFT;
  2161. if (sumhi > PNG_HIMASK)
  2162. sum = PNG_MAXSUM;
  2163. else
  2164. sum = (sumhi << PNG_HISHIFT) + sumlo;
  2165. }
  2166. #endif
  2167. if (sum < mins)
  2168. {
  2169. mins = sum;
  2170. best_row = png_ptr->up_row;
  2171. }
  2172. }
  2173. /* avg filter */
  2174. if (filter_to_do == PNG_FILTER_AVG)
  2175. {
  2176. png_bytep rp, dp, pp, lp;
  2177. png_uint_32 i;
  2178. for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
  2179. pp = prev_row + 1; i < bpp; i++)
  2180. {
  2181. *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
  2182. }
  2183. for (lp = row_buf + 1; i < row_bytes; i++)
  2184. {
  2185. *dp++ = (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2))
  2186. & 0xff);
  2187. }
  2188. best_row = png_ptr->avg_row;
  2189. }
  2190. else if (filter_to_do & PNG_FILTER_AVG)
  2191. {
  2192. png_bytep rp, dp, pp, lp;
  2193. png_uint_32 sum = 0, lmins = mins;
  2194. png_uint_32 i;
  2195. int v;
  2196. #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
  2197. if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  2198. {
  2199. int j;
  2200. png_uint_32 lmhi, lmlo;
  2201. lmlo = lmins & PNG_LOMASK;
  2202. lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
  2203. for (j = 0; j < num_p_filters; j++)
  2204. {
  2205. if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_AVG)
  2206. {
  2207. lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
  2208. PNG_WEIGHT_SHIFT;
  2209. lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
  2210. PNG_WEIGHT_SHIFT;
  2211. }
  2212. }
  2213. lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
  2214. PNG_COST_SHIFT;
  2215. lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_AVG]) >>
  2216. PNG_COST_SHIFT;
  2217. if (lmhi > PNG_HIMASK)
  2218. lmins = PNG_MAXSUM;
  2219. else
  2220. lmins = (lmhi << PNG_HISHIFT) + lmlo;
  2221. }
  2222. #endif
  2223. for (i = 0, rp = row_buf + 1, dp = png_ptr->avg_row + 1,
  2224. pp = prev_row + 1; i < bpp; i++)
  2225. {
  2226. v = *dp++ = (png_byte)(((int)*rp++ - ((int)*pp++ / 2)) & 0xff);
  2227. sum += (v < 128) ? v : 256 - v;
  2228. }
  2229. for (lp = row_buf + 1; i < row_bytes; i++)
  2230. {
  2231. v = *dp++ =
  2232. (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
  2233. sum += (v < 128) ? v : 256 - v;
  2234. if (sum > lmins) /* We are already worse, don't continue. */
  2235. break;
  2236. }
  2237. #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
  2238. if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  2239. {
  2240. int j;
  2241. png_uint_32 sumhi, sumlo;
  2242. sumlo = sum & PNG_LOMASK;
  2243. sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
  2244. for (j = 0; j < num_p_filters; j++)
  2245. {
  2246. if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_NONE)
  2247. {
  2248. sumlo = (sumlo * png_ptr->filter_weights[j]) >>
  2249. PNG_WEIGHT_SHIFT;
  2250. sumhi = (sumhi * png_ptr->filter_weights[j]) >>
  2251. PNG_WEIGHT_SHIFT;
  2252. }
  2253. }
  2254. sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
  2255. PNG_COST_SHIFT;
  2256. sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_AVG]) >>
  2257. PNG_COST_SHIFT;
  2258. if (sumhi > PNG_HIMASK)
  2259. sum = PNG_MAXSUM;
  2260. else
  2261. sum = (sumhi << PNG_HISHIFT) + sumlo;
  2262. }
  2263. #endif
  2264. if (sum < mins)
  2265. {
  2266. mins = sum;
  2267. best_row = png_ptr->avg_row;
  2268. }
  2269. }
  2270. /* Paeth filter */
  2271. if (filter_to_do == PNG_FILTER_PAETH)
  2272. {
  2273. png_bytep rp, dp, pp, cp, lp;
  2274. png_uint_32 i;
  2275. for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
  2276. pp = prev_row + 1; i < bpp; i++)
  2277. {
  2278. *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
  2279. }
  2280. for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
  2281. {
  2282. int a, b, c, pa, pb, pc, p;
  2283. b = *pp++;
  2284. c = *cp++;
  2285. a = *lp++;
  2286. p = b - c;
  2287. pc = a - c;
  2288. #ifdef PNG_USE_ABS
  2289. pa = abs(p);
  2290. pb = abs(pc);
  2291. pc = abs(p + pc);
  2292. #else
  2293. pa = p < 0 ? -p : p;
  2294. pb = pc < 0 ? -pc : pc;
  2295. pc = (p + pc) < 0 ? -(p + pc) : p + pc;
  2296. #endif
  2297. p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
  2298. *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
  2299. }
  2300. best_row = png_ptr->paeth_row;
  2301. }
  2302. else if (filter_to_do & PNG_FILTER_PAETH)
  2303. {
  2304. png_bytep rp, dp, pp, cp, lp;
  2305. png_uint_32 sum = 0, lmins = mins;
  2306. png_uint_32 i;
  2307. int v;
  2308. #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
  2309. if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  2310. {
  2311. int j;
  2312. png_uint_32 lmhi, lmlo;
  2313. lmlo = lmins & PNG_LOMASK;
  2314. lmhi = (lmins >> PNG_HISHIFT) & PNG_HIMASK;
  2315. for (j = 0; j < num_p_filters; j++)
  2316. {
  2317. if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
  2318. {
  2319. lmlo = (lmlo * png_ptr->inv_filter_weights[j]) >>
  2320. PNG_WEIGHT_SHIFT;
  2321. lmhi = (lmhi * png_ptr->inv_filter_weights[j]) >>
  2322. PNG_WEIGHT_SHIFT;
  2323. }
  2324. }
  2325. lmlo = (lmlo * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
  2326. PNG_COST_SHIFT;
  2327. lmhi = (lmhi * png_ptr->inv_filter_costs[PNG_FILTER_VALUE_PAETH]) >>
  2328. PNG_COST_SHIFT;
  2329. if (lmhi > PNG_HIMASK)
  2330. lmins = PNG_MAXSUM;
  2331. else
  2332. lmins = (lmhi << PNG_HISHIFT) + lmlo;
  2333. }
  2334. #endif
  2335. for (i = 0, rp = row_buf + 1, dp = png_ptr->paeth_row + 1,
  2336. pp = prev_row + 1; i < bpp; i++)
  2337. {
  2338. v = *dp++ = (png_byte)(((int)*rp++ - (int)*pp++) & 0xff);
  2339. sum += (v < 128) ? v : 256 - v;
  2340. }
  2341. for (lp = row_buf + 1, cp = prev_row + 1; i < row_bytes; i++)
  2342. {
  2343. int a, b, c, pa, pb, pc, p;
  2344. b = *pp++;
  2345. c = *cp++;
  2346. a = *lp++;
  2347. #ifndef PNG_SLOW_PAETH
  2348. p = b - c;
  2349. pc = a - c;
  2350. #ifdef PNG_USE_ABS
  2351. pa = abs(p);
  2352. pb = abs(pc);
  2353. pc = abs(p + pc);
  2354. #else
  2355. pa = p < 0 ? -p : p;
  2356. pb = pc < 0 ? -pc : pc;
  2357. pc = (p + pc) < 0 ? -(p + pc) : p + pc;
  2358. #endif
  2359. p = (pa <= pb && pa <=pc) ? a : (pb <= pc) ? b : c;
  2360. #else /* PNG_SLOW_PAETH */
  2361. p = a + b - c;
  2362. pa = abs(p - a);
  2363. pb = abs(p - b);
  2364. pc = abs(p - c);
  2365. if (pa <= pb && pa <= pc)
  2366. p = a;
  2367. else if (pb <= pc)
  2368. p = b;
  2369. else
  2370. p = c;
  2371. #endif /* PNG_SLOW_PAETH */
  2372. v = *dp++ = (png_byte)(((int)*rp++ - p) & 0xff);
  2373. sum += (v < 128) ? v : 256 - v;
  2374. if (sum > lmins) /* We are already worse, don't continue. */
  2375. break;
  2376. }
  2377. #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
  2378. if (png_ptr->heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  2379. {
  2380. int j;
  2381. png_uint_32 sumhi, sumlo;
  2382. sumlo = sum & PNG_LOMASK;
  2383. sumhi = (sum >> PNG_HISHIFT) & PNG_HIMASK;
  2384. for (j = 0; j < num_p_filters; j++)
  2385. {
  2386. if (png_ptr->prev_filters[j] == PNG_FILTER_VALUE_PAETH)
  2387. {
  2388. sumlo = (sumlo * png_ptr->filter_weights[j]) >>
  2389. PNG_WEIGHT_SHIFT;
  2390. sumhi = (sumhi * png_ptr->filter_weights[j]) >>
  2391. PNG_WEIGHT_SHIFT;
  2392. }
  2393. }
  2394. sumlo = (sumlo * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
  2395. PNG_COST_SHIFT;
  2396. sumhi = (sumhi * png_ptr->filter_costs[PNG_FILTER_VALUE_PAETH]) >>
  2397. PNG_COST_SHIFT;
  2398. if (sumhi > PNG_HIMASK)
  2399. sum = PNG_MAXSUM;
  2400. else
  2401. sum = (sumhi << PNG_HISHIFT) + sumlo;
  2402. }
  2403. #endif
  2404. if (sum < mins)
  2405. {
  2406. best_row = png_ptr->paeth_row;
  2407. }
  2408. }
  2409. #endif /* PNG_NO_WRITE_FILTER */
  2410. /* Do the actual writing of the filtered row data from the chosen filter. */
  2411. png_write_filtered_row(png_ptr, best_row);
  2412. #ifndef PNG_NO_WRITE_FILTER
  2413. #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
  2414. /* Save the type of filter we picked this time for future calculations */
  2415. if (png_ptr->num_prev_filters > 0)
  2416. {
  2417. int j;
  2418. for (j = 1; j < num_p_filters; j++)
  2419. {
  2420. png_ptr->prev_filters[j] = png_ptr->prev_filters[j - 1];
  2421. }
  2422. png_ptr->prev_filters[j] = best_row[0];
  2423. }
  2424. #endif
  2425. #endif /* PNG_NO_WRITE_FILTER */
  2426. }
  2427. /* Do the actual writing of a previously filtered row. */
  2428. void /* PRIVATE */
  2429. png_write_filtered_row(png_structp png_ptr, png_bytep filtered_row)
  2430. {
  2431. png_debug(1, "in png_write_filtered_row\n");
  2432. png_debug1(2, "filter = %d\n", filtered_row[0]);
  2433. /* set up the zlib input buffer */
  2434. png_ptr->zstream.next_in = filtered_row;
  2435. png_ptr->zstream.avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
  2436. /* repeat until we have compressed all the data */
  2437. do
  2438. {
  2439. int ret; /* return of zlib */
  2440. /* compress the data */
  2441. ret = deflate(&png_ptr->zstream, Z_NO_FLUSH);
  2442. /* check for compression errors */
  2443. if (ret != Z_OK)
  2444. {
  2445. if (png_ptr->zstream.msg != NULL)
  2446. png_error(png_ptr, png_ptr->zstream.msg);
  2447. else
  2448. png_error(png_ptr, "zlib error");
  2449. }
  2450. /* see if it is time to write another IDAT */
  2451. if (!(png_ptr->zstream.avail_out))
  2452. {
  2453. /* write the IDAT and reset the zlib output buffer */
  2454. png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  2455. png_ptr->zstream.next_out = png_ptr->zbuf;
  2456. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  2457. }
  2458. /* repeat until all data has been compressed */
  2459. } while (png_ptr->zstream.avail_in);
  2460. /* swap the current and previous rows */
  2461. if (png_ptr->prev_row != NULL)
  2462. {
  2463. png_bytep tptr;
  2464. tptr = png_ptr->prev_row;
  2465. png_ptr->prev_row = png_ptr->row_buf;
  2466. png_ptr->row_buf = tptr;
  2467. }
  2468. /* finish row - updates counters and flushes zlib if last row */
  2469. png_write_finish_row(png_ptr);
  2470. #if defined(PNG_WRITE_FLUSH_SUPPORTED)
  2471. png_ptr->flush_rows++;
  2472. if (png_ptr->flush_dist > 0 &&
  2473. png_ptr->flush_rows >= png_ptr->flush_dist)
  2474. {
  2475. png_write_flush(png_ptr);
  2476. }
  2477. #endif
  2478. }
  2479. #endif /* PNG_WRITE_SUPPORTED */