Counter Strike : Global Offensive Source Code
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.

1605 lines
48 KiB

  1. /* pngwrite.c - general routines to write a PNG file
  2. *
  3. * Last changed in libpng 1.5.1 [February 3, 2011]
  4. * Copyright (c) 1998-2011 Glenn Randers-Pehrson
  5. * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  6. * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. */
  12. #include "pngpriv.h"
  13. #ifdef PNG_WRITE_SUPPORTED
  14. /* Writes all the PNG information. This is the suggested way to use the
  15. * library. If you have a new chunk to add, make a function to write it,
  16. * and put it in the correct location here. If you want the chunk written
  17. * after the image data, put it in png_write_end(). I strongly encourage
  18. * you to supply a PNG_INFO_ flag, and check info_ptr->valid before writing
  19. * the chunk, as that will keep the code from breaking if you want to just
  20. * write a plain PNG file. If you have long comments, I suggest writing
  21. * them in png_write_end(), and compressing them.
  22. */
  23. void PNGAPI
  24. png_write_info_before_PLTE(png_structp png_ptr, png_infop info_ptr)
  25. {
  26. png_debug(1, "in png_write_info_before_PLTE");
  27. if (png_ptr == NULL || info_ptr == NULL)
  28. return;
  29. if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
  30. {
  31. /* Write PNG signature */
  32. png_write_sig(png_ptr);
  33. #ifdef PNG_MNG_FEATURES_SUPPORTED
  34. if ((png_ptr->mode&PNG_HAVE_PNG_SIGNATURE) && \
  35. (png_ptr->mng_features_permitted))
  36. {
  37. png_warning(png_ptr, "MNG features are not allowed in a PNG datastream");
  38. png_ptr->mng_features_permitted = 0;
  39. }
  40. #endif
  41. /* Write IHDR information. */
  42. png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
  43. info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
  44. info_ptr->filter_type,
  45. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  46. info_ptr->interlace_type);
  47. #else
  48. 0);
  49. #endif
  50. /* The rest of these check to see if the valid field has the appropriate
  51. * flag set, and if it does, writes the chunk.
  52. */
  53. #ifdef PNG_WRITE_gAMA_SUPPORTED
  54. if (info_ptr->valid & PNG_INFO_gAMA)
  55. png_write_gAMA_fixed(png_ptr, info_ptr->gamma);
  56. #endif
  57. #ifdef PNG_WRITE_sRGB_SUPPORTED
  58. if (info_ptr->valid & PNG_INFO_sRGB)
  59. png_write_sRGB(png_ptr, (int)info_ptr->srgb_intent);
  60. #endif
  61. #ifdef PNG_WRITE_iCCP_SUPPORTED
  62. if (info_ptr->valid & PNG_INFO_iCCP)
  63. png_write_iCCP(png_ptr, info_ptr->iccp_name, PNG_COMPRESSION_TYPE_BASE,
  64. (png_charp)info_ptr->iccp_profile, (int)info_ptr->iccp_proflen);
  65. #endif
  66. #ifdef PNG_WRITE_sBIT_SUPPORTED
  67. if (info_ptr->valid & PNG_INFO_sBIT)
  68. png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
  69. #endif
  70. #ifdef PNG_WRITE_cHRM_SUPPORTED
  71. if (info_ptr->valid & PNG_INFO_cHRM)
  72. png_write_cHRM_fixed(png_ptr,
  73. info_ptr->x_white, info_ptr->y_white,
  74. info_ptr->x_red, info_ptr->y_red,
  75. info_ptr->x_green, info_ptr->y_green,
  76. info_ptr->x_blue, info_ptr->y_blue);
  77. #endif
  78. #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
  79. if (info_ptr->unknown_chunks_num)
  80. {
  81. png_unknown_chunk *up;
  82. png_debug(5, "writing extra chunks");
  83. for (up = info_ptr->unknown_chunks;
  84. up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
  85. up++)
  86. {
  87. int keep = png_handle_as_unknown(png_ptr, up->name);
  88. if (keep != PNG_HANDLE_CHUNK_NEVER &&
  89. up->location && !(up->location & PNG_HAVE_PLTE) &&
  90. !(up->location & PNG_HAVE_IDAT) &&
  91. ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
  92. (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
  93. {
  94. if (up->size == 0)
  95. png_warning(png_ptr, "Writing zero-length unknown chunk");
  96. png_write_chunk(png_ptr, up->name, up->data, up->size);
  97. }
  98. }
  99. }
  100. #endif
  101. png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
  102. }
  103. }
  104. void PNGAPI
  105. png_write_info(png_structp png_ptr, png_infop info_ptr)
  106. {
  107. #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
  108. int i;
  109. #endif
  110. png_debug(1, "in png_write_info");
  111. if (png_ptr == NULL || info_ptr == NULL)
  112. return;
  113. png_write_info_before_PLTE(png_ptr, info_ptr);
  114. if (info_ptr->valid & PNG_INFO_PLTE)
  115. png_write_PLTE(png_ptr, info_ptr->palette,
  116. (png_uint_32)info_ptr->num_palette);
  117. else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  118. png_error(png_ptr, "Valid palette required for paletted images");
  119. #ifdef PNG_WRITE_tRNS_SUPPORTED
  120. if (info_ptr->valid & PNG_INFO_tRNS)
  121. {
  122. #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
  123. /* Invert the alpha channel (in tRNS) */
  124. if ((png_ptr->transformations & PNG_INVERT_ALPHA) &&
  125. info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  126. {
  127. int j;
  128. for (j = 0; j<(int)info_ptr->num_trans; j++)
  129. info_ptr->trans_alpha[j] =
  130. (png_byte)(255 - info_ptr->trans_alpha[j]);
  131. }
  132. #endif
  133. png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
  134. info_ptr->num_trans, info_ptr->color_type);
  135. }
  136. #endif
  137. #ifdef PNG_WRITE_bKGD_SUPPORTED
  138. if (info_ptr->valid & PNG_INFO_bKGD)
  139. png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
  140. #endif
  141. #ifdef PNG_WRITE_hIST_SUPPORTED
  142. if (info_ptr->valid & PNG_INFO_hIST)
  143. png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
  144. #endif
  145. #ifdef PNG_WRITE_oFFs_SUPPORTED
  146. if (info_ptr->valid & PNG_INFO_oFFs)
  147. png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
  148. info_ptr->offset_unit_type);
  149. #endif
  150. #ifdef PNG_WRITE_pCAL_SUPPORTED
  151. if (info_ptr->valid & PNG_INFO_pCAL)
  152. png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
  153. info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
  154. info_ptr->pcal_units, info_ptr->pcal_params);
  155. #endif
  156. #ifdef PNG_WRITE_sCAL_SUPPORTED
  157. if (info_ptr->valid & PNG_INFO_sCAL)
  158. png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
  159. info_ptr->scal_s_width, info_ptr->scal_s_height);
  160. #endif /* sCAL */
  161. #ifdef PNG_WRITE_pHYs_SUPPORTED
  162. if (info_ptr->valid & PNG_INFO_pHYs)
  163. png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
  164. info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
  165. #endif /* pHYs */
  166. #ifdef PNG_WRITE_tIME_SUPPORTED
  167. if (info_ptr->valid & PNG_INFO_tIME)
  168. {
  169. png_write_tIME(png_ptr, &(info_ptr->mod_time));
  170. png_ptr->mode |= PNG_WROTE_tIME;
  171. }
  172. #endif /* tIME */
  173. #ifdef PNG_WRITE_sPLT_SUPPORTED
  174. if (info_ptr->valid & PNG_INFO_sPLT)
  175. for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
  176. png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
  177. #endif /* sPLT */
  178. #ifdef PNG_WRITE_TEXT_SUPPORTED
  179. /* Check to see if we need to write text chunks */
  180. for (i = 0; i < info_ptr->num_text; i++)
  181. {
  182. png_debug2(2, "Writing header text chunk %d, type %d", i,
  183. info_ptr->text[i].compression);
  184. /* An internationalized chunk? */
  185. if (info_ptr->text[i].compression > 0)
  186. {
  187. #ifdef PNG_WRITE_iTXt_SUPPORTED
  188. /* Write international chunk */
  189. png_write_iTXt(png_ptr,
  190. info_ptr->text[i].compression,
  191. info_ptr->text[i].key,
  192. info_ptr->text[i].lang,
  193. info_ptr->text[i].lang_key,
  194. info_ptr->text[i].text);
  195. #else
  196. png_warning(png_ptr, "Unable to write international text");
  197. #endif
  198. /* Mark this chunk as written */
  199. info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  200. }
  201. /* If we want a compressed text chunk */
  202. else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
  203. {
  204. #ifdef PNG_WRITE_zTXt_SUPPORTED
  205. /* Write compressed chunk */
  206. png_write_zTXt(png_ptr, info_ptr->text[i].key,
  207. info_ptr->text[i].text, 0,
  208. info_ptr->text[i].compression);
  209. #else
  210. png_warning(png_ptr, "Unable to write compressed text");
  211. #endif
  212. /* Mark this chunk as written */
  213. info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
  214. }
  215. else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
  216. {
  217. #ifdef PNG_WRITE_tEXt_SUPPORTED
  218. /* Write uncompressed chunk */
  219. png_write_tEXt(png_ptr, info_ptr->text[i].key,
  220. info_ptr->text[i].text,
  221. 0);
  222. /* Mark this chunk as written */
  223. info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  224. #else
  225. /* Can't get here */
  226. png_warning(png_ptr, "Unable to write uncompressed text");
  227. #endif
  228. }
  229. }
  230. #endif /* tEXt */
  231. #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
  232. if (info_ptr->unknown_chunks_num)
  233. {
  234. png_unknown_chunk *up;
  235. png_debug(5, "writing extra chunks");
  236. for (up = info_ptr->unknown_chunks;
  237. up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
  238. up++)
  239. {
  240. int keep = png_handle_as_unknown(png_ptr, up->name);
  241. if (keep != PNG_HANDLE_CHUNK_NEVER &&
  242. up->location && (up->location & PNG_HAVE_PLTE) &&
  243. !(up->location & PNG_HAVE_IDAT) &&
  244. ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
  245. (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
  246. {
  247. png_write_chunk(png_ptr, up->name, up->data, up->size);
  248. }
  249. }
  250. }
  251. #endif
  252. }
  253. /* Writes the end of the PNG file. If you don't want to write comments or
  254. * time information, you can pass NULL for info. If you already wrote these
  255. * in png_write_info(), do not write them again here. If you have long
  256. * comments, I suggest writing them here, and compressing them.
  257. */
  258. void PNGAPI
  259. png_write_end(png_structp png_ptr, png_infop info_ptr)
  260. {
  261. png_debug(1, "in png_write_end");
  262. if (png_ptr == NULL)
  263. return;
  264. if (!(png_ptr->mode & PNG_HAVE_IDAT))
  265. png_error(png_ptr, "No IDATs written into file");
  266. /* See if user wants us to write information chunks */
  267. if (info_ptr != NULL)
  268. {
  269. #ifdef PNG_WRITE_TEXT_SUPPORTED
  270. int i; /* local index variable */
  271. #endif
  272. #ifdef PNG_WRITE_tIME_SUPPORTED
  273. /* Check to see if user has supplied a time chunk */
  274. if ((info_ptr->valid & PNG_INFO_tIME) &&
  275. !(png_ptr->mode & PNG_WROTE_tIME))
  276. png_write_tIME(png_ptr, &(info_ptr->mod_time));
  277. #endif
  278. #ifdef PNG_WRITE_TEXT_SUPPORTED
  279. /* Loop through comment chunks */
  280. for (i = 0; i < info_ptr->num_text; i++)
  281. {
  282. png_debug2(2, "Writing trailer text chunk %d, type %d", i,
  283. info_ptr->text[i].compression);
  284. /* An internationalized chunk? */
  285. if (info_ptr->text[i].compression > 0)
  286. {
  287. #ifdef PNG_WRITE_iTXt_SUPPORTED
  288. /* Write international chunk */
  289. png_write_iTXt(png_ptr,
  290. info_ptr->text[i].compression,
  291. info_ptr->text[i].key,
  292. info_ptr->text[i].lang,
  293. info_ptr->text[i].lang_key,
  294. info_ptr->text[i].text);
  295. #else
  296. png_warning(png_ptr, "Unable to write international text");
  297. #endif
  298. /* Mark this chunk as written */
  299. info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  300. }
  301. else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
  302. {
  303. #ifdef PNG_WRITE_zTXt_SUPPORTED
  304. /* Write compressed chunk */
  305. png_write_zTXt(png_ptr, info_ptr->text[i].key,
  306. info_ptr->text[i].text, 0,
  307. info_ptr->text[i].compression);
  308. #else
  309. png_warning(png_ptr, "Unable to write compressed text");
  310. #endif
  311. /* Mark this chunk as written */
  312. info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
  313. }
  314. else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
  315. {
  316. #ifdef PNG_WRITE_tEXt_SUPPORTED
  317. /* Write uncompressed chunk */
  318. png_write_tEXt(png_ptr, info_ptr->text[i].key,
  319. info_ptr->text[i].text, 0);
  320. #else
  321. png_warning(png_ptr, "Unable to write uncompressed text");
  322. #endif
  323. /* Mark this chunk as written */
  324. info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  325. }
  326. }
  327. #endif
  328. #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
  329. if (info_ptr->unknown_chunks_num)
  330. {
  331. png_unknown_chunk *up;
  332. png_debug(5, "writing extra chunks");
  333. for (up = info_ptr->unknown_chunks;
  334. up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
  335. up++)
  336. {
  337. int keep = png_handle_as_unknown(png_ptr, up->name);
  338. if (keep != PNG_HANDLE_CHUNK_NEVER &&
  339. up->location && (up->location & PNG_AFTER_IDAT) &&
  340. ((up->name[3] & 0x20) || keep == PNG_HANDLE_CHUNK_ALWAYS ||
  341. (png_ptr->flags & PNG_FLAG_KEEP_UNSAFE_CHUNKS)))
  342. {
  343. png_write_chunk(png_ptr, up->name, up->data, up->size);
  344. }
  345. }
  346. }
  347. #endif
  348. }
  349. png_ptr->mode |= PNG_AFTER_IDAT;
  350. /* Write end of PNG file */
  351. png_write_IEND(png_ptr);
  352. /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
  353. * and restored again in libpng-1.2.30, may cause some applications that
  354. * do not set png_ptr->output_flush_fn to crash. If your application
  355. * experiences a problem, please try building libpng with
  356. * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
  357. * png-mng-implement at lists.sf.net .
  358. */
  359. #ifdef PNG_WRITE_FLUSH_SUPPORTED
  360. # ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
  361. png_flush(png_ptr);
  362. # endif
  363. #endif
  364. }
  365. #ifdef PNG_CONVERT_tIME_SUPPORTED
  366. /* "tm" structure is not supported on WindowsCE */
  367. void PNGAPI
  368. png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm FAR * ttime)
  369. {
  370. png_debug(1, "in png_convert_from_struct_tm");
  371. ptime->year = (png_uint_16)(1900 + ttime->tm_year);
  372. ptime->month = (png_byte)(ttime->tm_mon + 1);
  373. ptime->day = (png_byte)ttime->tm_mday;
  374. ptime->hour = (png_byte)ttime->tm_hour;
  375. ptime->minute = (png_byte)ttime->tm_min;
  376. ptime->second = (png_byte)ttime->tm_sec;
  377. }
  378. void PNGAPI
  379. png_convert_from_time_t(png_timep ptime, time_t ttime)
  380. {
  381. struct tm *tbuf;
  382. png_debug(1, "in png_convert_from_time_t");
  383. tbuf = gmtime(&ttime);
  384. png_convert_from_struct_tm(ptime, tbuf);
  385. }
  386. #endif
  387. /* Initialize png_ptr structure, and allocate any memory needed */
  388. PNG_FUNCTION(png_structp,PNGAPI
  389. png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
  390. png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
  391. {
  392. #ifdef PNG_USER_MEM_SUPPORTED
  393. return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
  394. warn_fn, NULL, NULL, NULL));
  395. }
  396. /* Alternate initialize png_ptr structure, and allocate any memory needed */
  397. static void png_reset_filter_heuristics(png_structp png_ptr); /* forward decl */
  398. PNG_FUNCTION(png_structp,PNGAPI
  399. png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
  400. png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
  401. png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
  402. {
  403. #endif /* PNG_USER_MEM_SUPPORTED */
  404. volatile int png_cleanup_needed = 0;
  405. #ifdef PNG_SETJMP_SUPPORTED
  406. volatile
  407. #endif
  408. png_structp png_ptr;
  409. #ifdef PNG_SETJMP_SUPPORTED
  410. #ifdef USE_FAR_KEYWORD
  411. jmp_buf png_jmpbuf;
  412. #endif
  413. #endif
  414. int i;
  415. png_debug(1, "in png_create_write_struct");
  416. #ifdef PNG_USER_MEM_SUPPORTED
  417. png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
  418. (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
  419. #else
  420. png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
  421. #endif /* PNG_USER_MEM_SUPPORTED */
  422. if (png_ptr == NULL)
  423. return (NULL);
  424. /* Added at libpng-1.2.6 */
  425. #ifdef PNG_SET_USER_LIMITS_SUPPORTED
  426. png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
  427. png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
  428. #endif
  429. #ifdef PNG_SETJMP_SUPPORTED
  430. /* Applications that neglect to set up their own setjmp() and then
  431. encounter a png_error() will longjmp here. Since the jmpbuf is
  432. then meaningless we abort instead of returning. */
  433. #ifdef USE_FAR_KEYWORD
  434. if (setjmp(png_jmpbuf))
  435. #else
  436. if (setjmp(png_jmpbuf(png_ptr))) /* sets longjmp to match setjmp */
  437. #endif
  438. #ifdef USE_FAR_KEYWORD
  439. png_memcpy(png_jmpbuf(png_ptr), png_jmpbuf, png_sizeof(jmp_buf));
  440. #endif
  441. PNG_ABORT();
  442. #endif
  443. #ifdef PNG_USER_MEM_SUPPORTED
  444. png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
  445. #endif /* PNG_USER_MEM_SUPPORTED */
  446. png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
  447. if (user_png_ver)
  448. {
  449. i = 0;
  450. do
  451. {
  452. if (user_png_ver[i] != png_libpng_ver[i])
  453. png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  454. } while (png_libpng_ver[i++]);
  455. }
  456. if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
  457. {
  458. /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
  459. * we must recompile any applications that use any older library version.
  460. * For versions after libpng 1.0, we will be compatible, so we need
  461. * only check the first digit.
  462. */
  463. if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
  464. (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
  465. (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
  466. {
  467. #ifdef PNG_CONSOLE_IO_SUPPORTED
  468. char msg[80];
  469. if (user_png_ver)
  470. {
  471. png_snprintf2(msg, 80,
  472. "Application built with libpng-%.20s"
  473. " but running with %.20s",
  474. user_png_ver,
  475. png_libpng_ver);
  476. png_warning(png_ptr, msg);
  477. }
  478. #else
  479. png_warning(png_ptr,
  480. "Incompatible libpng version in application and library");
  481. #endif
  482. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  483. png_ptr->flags = 0;
  484. #endif
  485. png_cleanup_needed = 1;
  486. }
  487. }
  488. /* Initialize zbuf - compression buffer */
  489. png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  490. if (!png_cleanup_needed)
  491. {
  492. png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
  493. png_ptr->zbuf_size);
  494. if (png_ptr->zbuf == NULL)
  495. png_cleanup_needed = 1;
  496. }
  497. if (png_cleanup_needed)
  498. {
  499. /* Clean up PNG structure and deallocate any memory. */
  500. png_free(png_ptr, png_ptr->zbuf);
  501. png_ptr->zbuf = NULL;
  502. #ifdef PNG_USER_MEM_SUPPORTED
  503. png_destroy_struct_2((png_voidp)png_ptr,
  504. (png_free_ptr)free_fn, (png_voidp)mem_ptr);
  505. #else
  506. png_destroy_struct((png_voidp)png_ptr);
  507. #endif
  508. return (NULL);
  509. }
  510. png_set_write_fn(png_ptr, NULL, NULL, NULL);
  511. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  512. png_reset_filter_heuristics(png_ptr);
  513. #endif
  514. return (png_ptr);
  515. }
  516. /* Write a few rows of image data. If the image is interlaced,
  517. * either you will have to write the 7 sub images, or, if you
  518. * have called png_set_interlace_handling(), you will have to
  519. * "write" the image seven times.
  520. */
  521. void PNGAPI
  522. png_write_rows(png_structp png_ptr, png_bytepp row,
  523. png_uint_32 num_rows)
  524. {
  525. png_uint_32 i; /* row counter */
  526. png_bytepp rp; /* row pointer */
  527. png_debug(1, "in png_write_rows");
  528. if (png_ptr == NULL)
  529. return;
  530. /* Loop through the rows */
  531. for (i = 0, rp = row; i < num_rows; i++, rp++)
  532. {
  533. png_write_row(png_ptr, *rp);
  534. }
  535. }
  536. /* Write the image. You only need to call this function once, even
  537. * if you are writing an interlaced image.
  538. */
  539. void PNGAPI
  540. png_write_image(png_structp png_ptr, png_bytepp image)
  541. {
  542. png_uint_32 i; /* row index */
  543. int pass, num_pass; /* pass variables */
  544. png_bytepp rp; /* points to current row */
  545. if (png_ptr == NULL)
  546. return;
  547. png_debug(1, "in png_write_image");
  548. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  549. /* Initialize interlace handling. If image is not interlaced,
  550. * this will set pass to 1
  551. */
  552. num_pass = png_set_interlace_handling(png_ptr);
  553. #else
  554. num_pass = 1;
  555. #endif
  556. /* Loop through passes */
  557. for (pass = 0; pass < num_pass; pass++)
  558. {
  559. /* Loop through image */
  560. for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
  561. {
  562. png_write_row(png_ptr, *rp);
  563. }
  564. }
  565. }
  566. /* Called by user to write a row of image data */
  567. void PNGAPI
  568. png_write_row(png_structp png_ptr, png_const_bytep row)
  569. {
  570. if (png_ptr == NULL)
  571. return;
  572. png_debug2(1, "in png_write_row (row %u, pass %d)",
  573. png_ptr->row_number, png_ptr->pass);
  574. /* Initialize transformations and other stuff if first time */
  575. if (png_ptr->row_number == 0 && png_ptr->pass == 0)
  576. {
  577. /* Make sure we wrote the header info */
  578. if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
  579. png_error(png_ptr,
  580. "png_write_info was never called before png_write_row");
  581. /* Check for transforms that have been set but were defined out */
  582. #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
  583. if (png_ptr->transformations & PNG_INVERT_MONO)
  584. png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
  585. #endif
  586. #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
  587. if (png_ptr->transformations & PNG_FILLER)
  588. png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
  589. #endif
  590. #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
  591. defined(PNG_READ_PACKSWAP_SUPPORTED)
  592. if (png_ptr->transformations & PNG_PACKSWAP)
  593. png_warning(png_ptr,
  594. "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
  595. #endif
  596. #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
  597. if (png_ptr->transformations & PNG_PACK)
  598. png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
  599. #endif
  600. #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
  601. if (png_ptr->transformations & PNG_SHIFT)
  602. png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
  603. #endif
  604. #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
  605. if (png_ptr->transformations & PNG_BGR)
  606. png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
  607. #endif
  608. #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
  609. if (png_ptr->transformations & PNG_SWAP_BYTES)
  610. png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
  611. #endif
  612. png_write_start_row(png_ptr);
  613. }
  614. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  615. /* If interlaced and not interested in row, return */
  616. if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
  617. {
  618. switch (png_ptr->pass)
  619. {
  620. case 0:
  621. if (png_ptr->row_number & 0x07)
  622. {
  623. png_write_finish_row(png_ptr);
  624. return;
  625. }
  626. break;
  627. case 1:
  628. if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
  629. {
  630. png_write_finish_row(png_ptr);
  631. return;
  632. }
  633. break;
  634. case 2:
  635. if ((png_ptr->row_number & 0x07) != 4)
  636. {
  637. png_write_finish_row(png_ptr);
  638. return;
  639. }
  640. break;
  641. case 3:
  642. if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
  643. {
  644. png_write_finish_row(png_ptr);
  645. return;
  646. }
  647. break;
  648. case 4:
  649. if ((png_ptr->row_number & 0x03) != 2)
  650. {
  651. png_write_finish_row(png_ptr);
  652. return;
  653. }
  654. break;
  655. case 5:
  656. if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
  657. {
  658. png_write_finish_row(png_ptr);
  659. return;
  660. }
  661. break;
  662. case 6:
  663. if (!(png_ptr->row_number & 0x01))
  664. {
  665. png_write_finish_row(png_ptr);
  666. return;
  667. }
  668. break;
  669. default: /* error: ignore it */
  670. break;
  671. }
  672. }
  673. #endif
  674. /* Set up row info for transformations */
  675. png_ptr->row_info.color_type = png_ptr->color_type;
  676. png_ptr->row_info.width = png_ptr->usr_width;
  677. png_ptr->row_info.channels = png_ptr->usr_channels;
  678. png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
  679. png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
  680. png_ptr->row_info.channels);
  681. png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
  682. png_ptr->row_info.width);
  683. png_debug1(3, "row_info->color_type = %d", png_ptr->row_info.color_type);
  684. png_debug1(3, "row_info->width = %u", png_ptr->row_info.width);
  685. png_debug1(3, "row_info->channels = %d", png_ptr->row_info.channels);
  686. png_debug1(3, "row_info->bit_depth = %d", png_ptr->row_info.bit_depth);
  687. png_debug1(3, "row_info->pixel_depth = %d", png_ptr->row_info.pixel_depth);
  688. png_debug1(3, "row_info->rowbytes = %lu",
  689. (unsigned long)png_ptr->row_info.rowbytes);
  690. /* Copy user's row into buffer, leaving room for filter byte. */
  691. png_memcpy(png_ptr->row_buf + 1, row, png_ptr->row_info.rowbytes);
  692. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  693. /* Handle interlacing */
  694. if (png_ptr->interlaced && png_ptr->pass < 6 &&
  695. (png_ptr->transformations & PNG_INTERLACE))
  696. {
  697. png_do_write_interlace(&(png_ptr->row_info),
  698. png_ptr->row_buf + 1, png_ptr->pass);
  699. /* This should always get caught above, but still ... */
  700. if (!(png_ptr->row_info.width))
  701. {
  702. png_write_finish_row(png_ptr);
  703. return;
  704. }
  705. }
  706. #endif
  707. /* Handle other transformations */
  708. if (png_ptr->transformations)
  709. png_do_write_transformations(png_ptr);
  710. #ifdef PNG_MNG_FEATURES_SUPPORTED
  711. /* Write filter_method 64 (intrapixel differencing) only if
  712. * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
  713. * 2. Libpng did not write a PNG signature (this filter_method is only
  714. * used in PNG datastreams that are embedded in MNG datastreams) and
  715. * 3. The application called png_permit_mng_features with a mask that
  716. * included PNG_FLAG_MNG_FILTER_64 and
  717. * 4. The filter_method is 64 and
  718. * 5. The color_type is RGB or RGBA
  719. */
  720. if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  721. (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
  722. {
  723. /* Intrapixel differencing */
  724. png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
  725. }
  726. #endif
  727. /* Find a filter if necessary, filter the row and write it out. */
  728. png_write_find_filter(png_ptr, &(png_ptr->row_info));
  729. if (png_ptr->write_row_fn != NULL)
  730. (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
  731. }
  732. #ifdef PNG_WRITE_FLUSH_SUPPORTED
  733. /* Set the automatic flush interval or 0 to turn flushing off */
  734. void PNGAPI
  735. png_set_flush(png_structp png_ptr, int nrows)
  736. {
  737. png_debug(1, "in png_set_flush");
  738. if (png_ptr == NULL)
  739. return;
  740. png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
  741. }
  742. /* Flush the current output buffers now */
  743. void PNGAPI
  744. png_write_flush(png_structp png_ptr)
  745. {
  746. int wrote_IDAT;
  747. png_debug(1, "in png_write_flush");
  748. if (png_ptr == NULL)
  749. return;
  750. /* We have already written out all of the data */
  751. if (png_ptr->row_number >= png_ptr->num_rows)
  752. return;
  753. do
  754. {
  755. int ret;
  756. /* Compress the data */
  757. ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
  758. wrote_IDAT = 0;
  759. /* Check for compression errors */
  760. if (ret != Z_OK)
  761. {
  762. if (png_ptr->zstream.msg != NULL)
  763. png_error(png_ptr, png_ptr->zstream.msg);
  764. else
  765. png_error(png_ptr, "zlib error");
  766. }
  767. if (!(png_ptr->zstream.avail_out))
  768. {
  769. /* Write the IDAT and reset the zlib output buffer */
  770. png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  771. png_ptr->zstream.next_out = png_ptr->zbuf;
  772. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  773. wrote_IDAT = 1;
  774. }
  775. } while (wrote_IDAT == 1);
  776. /* If there is any data left to be output, write it into a new IDAT */
  777. if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
  778. {
  779. /* Write the IDAT and reset the zlib output buffer */
  780. png_write_IDAT(png_ptr, png_ptr->zbuf,
  781. png_ptr->zbuf_size - png_ptr->zstream.avail_out);
  782. png_ptr->zstream.next_out = png_ptr->zbuf;
  783. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  784. }
  785. png_ptr->flush_rows = 0;
  786. png_flush(png_ptr);
  787. }
  788. #endif /* PNG_WRITE_FLUSH_SUPPORTED */
  789. /* Free all memory used by the write */
  790. void PNGAPI
  791. png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
  792. {
  793. png_structp png_ptr = NULL;
  794. png_infop info_ptr = NULL;
  795. #ifdef PNG_USER_MEM_SUPPORTED
  796. png_free_ptr free_fn = NULL;
  797. png_voidp mem_ptr = NULL;
  798. #endif
  799. png_debug(1, "in png_destroy_write_struct");
  800. if (png_ptr_ptr != NULL)
  801. {
  802. png_ptr = *png_ptr_ptr;
  803. #ifdef PNG_USER_MEM_SUPPORTED
  804. free_fn = png_ptr->free_fn;
  805. mem_ptr = png_ptr->mem_ptr;
  806. #endif
  807. }
  808. #ifdef PNG_USER_MEM_SUPPORTED
  809. if (png_ptr != NULL)
  810. {
  811. free_fn = png_ptr->free_fn;
  812. mem_ptr = png_ptr->mem_ptr;
  813. }
  814. #endif
  815. if (info_ptr_ptr != NULL)
  816. info_ptr = *info_ptr_ptr;
  817. if (info_ptr != NULL)
  818. {
  819. if (png_ptr != NULL)
  820. {
  821. png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
  822. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  823. if (png_ptr->num_chunk_list)
  824. {
  825. png_free(png_ptr, png_ptr->chunk_list);
  826. png_ptr->num_chunk_list = 0;
  827. }
  828. #endif
  829. }
  830. #ifdef PNG_USER_MEM_SUPPORTED
  831. png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
  832. (png_voidp)mem_ptr);
  833. #else
  834. png_destroy_struct((png_voidp)info_ptr);
  835. #endif
  836. *info_ptr_ptr = NULL;
  837. }
  838. if (png_ptr != NULL)
  839. {
  840. png_write_destroy(png_ptr);
  841. #ifdef PNG_USER_MEM_SUPPORTED
  842. png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
  843. (png_voidp)mem_ptr);
  844. #else
  845. png_destroy_struct((png_voidp)png_ptr);
  846. #endif
  847. *png_ptr_ptr = NULL;
  848. }
  849. }
  850. /* Free any memory used in png_ptr struct (old method) */
  851. void /* PRIVATE */
  852. png_write_destroy(png_structp png_ptr)
  853. {
  854. #ifdef PNG_SETJMP_SUPPORTED
  855. jmp_buf tmp_jmp; /* Save jump buffer */
  856. #endif
  857. png_error_ptr error_fn;
  858. png_error_ptr warning_fn;
  859. png_voidp error_ptr;
  860. #ifdef PNG_USER_MEM_SUPPORTED
  861. png_free_ptr free_fn;
  862. #endif
  863. png_debug(1, "in png_write_destroy");
  864. /* Free any memory zlib uses */
  865. deflateEnd(&png_ptr->zstream);
  866. /* Free our memory. png_free checks NULL for us. */
  867. png_free(png_ptr, png_ptr->zbuf);
  868. png_free(png_ptr, png_ptr->row_buf);
  869. #ifdef PNG_WRITE_FILTER_SUPPORTED
  870. png_free(png_ptr, png_ptr->prev_row);
  871. png_free(png_ptr, png_ptr->sub_row);
  872. png_free(png_ptr, png_ptr->up_row);
  873. png_free(png_ptr, png_ptr->avg_row);
  874. png_free(png_ptr, png_ptr->paeth_row);
  875. #endif
  876. #ifdef PNG_TIME_RFC1123_SUPPORTED
  877. png_free(png_ptr, png_ptr->time_buffer);
  878. #endif
  879. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  880. /* Use this to save a little code space, it doesn't free the filter_costs */
  881. png_reset_filter_heuristics(png_ptr);
  882. png_free(png_ptr, png_ptr->filter_costs);
  883. png_free(png_ptr, png_ptr->inv_filter_costs);
  884. #endif
  885. #ifdef PNG_SETJMP_SUPPORTED
  886. /* Reset structure */
  887. png_memcpy(tmp_jmp, png_ptr->png_jmpbuf, png_sizeof(jmp_buf));
  888. #endif
  889. error_fn = png_ptr->error_fn;
  890. warning_fn = png_ptr->warning_fn;
  891. error_ptr = png_ptr->error_ptr;
  892. #ifdef PNG_USER_MEM_SUPPORTED
  893. free_fn = png_ptr->free_fn;
  894. #endif
  895. png_memset(png_ptr, 0, png_sizeof(png_struct));
  896. png_ptr->error_fn = error_fn;
  897. png_ptr->warning_fn = warning_fn;
  898. png_ptr->error_ptr = error_ptr;
  899. #ifdef PNG_USER_MEM_SUPPORTED
  900. png_ptr->free_fn = free_fn;
  901. #endif
  902. #ifdef PNG_SETJMP_SUPPORTED
  903. png_memcpy(png_ptr->png_jmpbuf, tmp_jmp, png_sizeof(jmp_buf));
  904. #endif
  905. }
  906. /* Allow the application to select one or more row filters to use. */
  907. void PNGAPI
  908. png_set_filter(png_structp png_ptr, int method, int filters)
  909. {
  910. png_debug(1, "in png_set_filter");
  911. if (png_ptr == NULL)
  912. return;
  913. #ifdef PNG_MNG_FEATURES_SUPPORTED
  914. if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  915. (method == PNG_INTRAPIXEL_DIFFERENCING))
  916. method = PNG_FILTER_TYPE_BASE;
  917. #endif
  918. if (method == PNG_FILTER_TYPE_BASE)
  919. {
  920. switch (filters & (PNG_ALL_FILTERS | 0x07))
  921. {
  922. #ifdef PNG_WRITE_FILTER_SUPPORTED
  923. case 5:
  924. case 6:
  925. case 7: png_warning(png_ptr, "Unknown row filter for method 0");
  926. #endif /* PNG_WRITE_FILTER_SUPPORTED */
  927. case PNG_FILTER_VALUE_NONE:
  928. png_ptr->do_filter = PNG_FILTER_NONE; break;
  929. #ifdef PNG_WRITE_FILTER_SUPPORTED
  930. case PNG_FILTER_VALUE_SUB:
  931. png_ptr->do_filter = PNG_FILTER_SUB; break;
  932. case PNG_FILTER_VALUE_UP:
  933. png_ptr->do_filter = PNG_FILTER_UP; break;
  934. case PNG_FILTER_VALUE_AVG:
  935. png_ptr->do_filter = PNG_FILTER_AVG; break;
  936. case PNG_FILTER_VALUE_PAETH:
  937. png_ptr->do_filter = PNG_FILTER_PAETH; break;
  938. default:
  939. png_ptr->do_filter = (png_byte)filters; break;
  940. #else
  941. default:
  942. png_warning(png_ptr, "Unknown row filter for method 0");
  943. #endif /* PNG_WRITE_FILTER_SUPPORTED */
  944. }
  945. /* If we have allocated the row_buf, this means we have already started
  946. * with the image and we should have allocated all of the filter buffers
  947. * that have been selected. If prev_row isn't already allocated, then
  948. * it is too late to start using the filters that need it, since we
  949. * will be missing the data in the previous row. If an application
  950. * wants to start and stop using particular filters during compression,
  951. * it should start out with all of the filters, and then add and
  952. * remove them after the start of compression.
  953. */
  954. if (png_ptr->row_buf != NULL)
  955. {
  956. #ifdef PNG_WRITE_FILTER_SUPPORTED
  957. if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
  958. {
  959. png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
  960. (png_ptr->rowbytes + 1));
  961. png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
  962. }
  963. if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
  964. {
  965. if (png_ptr->prev_row == NULL)
  966. {
  967. png_warning(png_ptr, "Can't add Up filter after starting");
  968. png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
  969. ~PNG_FILTER_UP);
  970. }
  971. else
  972. {
  973. png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
  974. (png_ptr->rowbytes + 1));
  975. png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
  976. }
  977. }
  978. if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
  979. {
  980. if (png_ptr->prev_row == NULL)
  981. {
  982. png_warning(png_ptr, "Can't add Average filter after starting");
  983. png_ptr->do_filter = (png_byte)(png_ptr->do_filter &
  984. ~PNG_FILTER_AVG);
  985. }
  986. else
  987. {
  988. png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
  989. (png_ptr->rowbytes + 1));
  990. png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
  991. }
  992. }
  993. if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
  994. png_ptr->paeth_row == NULL)
  995. {
  996. if (png_ptr->prev_row == NULL)
  997. {
  998. png_warning(png_ptr, "Can't add Paeth filter after starting");
  999. png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
  1000. }
  1001. else
  1002. {
  1003. png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
  1004. (png_ptr->rowbytes + 1));
  1005. png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
  1006. }
  1007. }
  1008. if (png_ptr->do_filter == PNG_NO_FILTERS)
  1009. #endif /* PNG_WRITE_FILTER_SUPPORTED */
  1010. png_ptr->do_filter = PNG_FILTER_NONE;
  1011. }
  1012. }
  1013. else
  1014. png_error(png_ptr, "Unknown custom filter method");
  1015. }
  1016. /* This allows us to influence the way in which libpng chooses the "best"
  1017. * filter for the current scanline. While the "minimum-sum-of-absolute-
  1018. * differences metric is relatively fast and effective, there is some
  1019. * question as to whether it can be improved upon by trying to keep the
  1020. * filtered data going to zlib more consistent, hopefully resulting in
  1021. * better compression.
  1022. */
  1023. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* GRR 970116 */
  1024. /* Convenience reset API. */
  1025. static void
  1026. png_reset_filter_heuristics(png_structp png_ptr)
  1027. {
  1028. /* Clear out any old values in the 'weights' - this must be done because if
  1029. * the app calls set_filter_heuristics multiple times with different
  1030. * 'num_weights' values we would otherwise potentially have wrong sized
  1031. * arrays.
  1032. */
  1033. png_ptr->num_prev_filters = 0;
  1034. png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
  1035. if (png_ptr->prev_filters != NULL)
  1036. {
  1037. png_bytep old = png_ptr->prev_filters;
  1038. png_ptr->prev_filters = NULL;
  1039. png_free(png_ptr, old);
  1040. }
  1041. if (png_ptr->filter_weights != NULL)
  1042. {
  1043. png_uint_16p old = png_ptr->filter_weights;
  1044. png_ptr->filter_weights = NULL;
  1045. png_free(png_ptr, old);
  1046. }
  1047. if (png_ptr->inv_filter_weights != NULL)
  1048. {
  1049. png_uint_16p old = png_ptr->inv_filter_weights;
  1050. png_ptr->inv_filter_weights = NULL;
  1051. png_free(png_ptr, old);
  1052. }
  1053. /* Leave the filter_costs - this array is fixed size. */
  1054. }
  1055. static int
  1056. png_init_filter_heuristics(png_structp png_ptr, int heuristic_method,
  1057. int num_weights)
  1058. {
  1059. if (png_ptr == NULL)
  1060. return 0;
  1061. /* Clear out the arrays */
  1062. png_reset_filter_heuristics(png_ptr);
  1063. /* Check arguments; the 'reset' function makes the correct settings for the
  1064. * unweighted case, but we must handle the weight case by initializing the
  1065. * arrays for the caller.
  1066. */
  1067. if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  1068. {
  1069. int i;
  1070. if (num_weights > 0)
  1071. {
  1072. png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
  1073. (png_uint_32)(png_sizeof(png_byte) * num_weights));
  1074. /* To make sure that the weighting starts out fairly */
  1075. for (i = 0; i < num_weights; i++)
  1076. {
  1077. png_ptr->prev_filters[i] = 255;
  1078. }
  1079. png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
  1080. (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
  1081. png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
  1082. (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
  1083. for (i = 0; i < num_weights; i++)
  1084. {
  1085. png_ptr->inv_filter_weights[i] =
  1086. png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
  1087. }
  1088. /* Safe to set this now */
  1089. png_ptr->num_prev_filters = (png_byte)num_weights;
  1090. }
  1091. /* If, in the future, there are other filter methods, this would
  1092. * need to be based on png_ptr->filter.
  1093. */
  1094. if (png_ptr->filter_costs == NULL)
  1095. {
  1096. png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
  1097. (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
  1098. png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
  1099. (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
  1100. }
  1101. for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
  1102. {
  1103. png_ptr->inv_filter_costs[i] =
  1104. png_ptr->filter_costs[i] = PNG_COST_FACTOR;
  1105. }
  1106. /* All the arrays are inited, safe to set this: */
  1107. png_ptr->heuristic_method = PNG_FILTER_HEURISTIC_WEIGHTED;
  1108. /* Return the 'ok' code. */
  1109. return 1;
  1110. }
  1111. else if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT ||
  1112. heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
  1113. {
  1114. return 1;
  1115. }
  1116. else
  1117. {
  1118. png_warning(png_ptr, "Unknown filter heuristic method");
  1119. return 0;
  1120. }
  1121. }
  1122. /* Provide floating and fixed point APIs */
  1123. #ifdef PNG_FLOATING_POINT_SUPPORTED
  1124. void PNGAPI
  1125. png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
  1126. int num_weights, png_const_doublep filter_weights,
  1127. png_const_doublep filter_costs)
  1128. {
  1129. png_debug(1, "in png_set_filter_heuristics");
  1130. /* The internal API allocates all the arrays and ensures that the elements of
  1131. * those arrays are set to the default value.
  1132. */
  1133. if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
  1134. return;
  1135. /* If using the weighted method copy in the weights. */
  1136. if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  1137. {
  1138. int i;
  1139. for (i = 0; i < num_weights; i++)
  1140. {
  1141. if (filter_weights[i] <= 0.0)
  1142. {
  1143. png_ptr->inv_filter_weights[i] =
  1144. png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
  1145. }
  1146. else
  1147. {
  1148. png_ptr->inv_filter_weights[i] =
  1149. (png_uint_16)(PNG_WEIGHT_FACTOR*filter_weights[i]+.5);
  1150. png_ptr->filter_weights[i] =
  1151. (png_uint_16)(PNG_WEIGHT_FACTOR/filter_weights[i]+.5);
  1152. }
  1153. }
  1154. /* Here is where we set the relative costs of the different filters. We
  1155. * should take the desired compression level into account when setting
  1156. * the costs, so that Paeth, for instance, has a high relative cost at low
  1157. * compression levels, while it has a lower relative cost at higher
  1158. * compression settings. The filter types are in order of increasing
  1159. * relative cost, so it would be possible to do this with an algorithm.
  1160. */
  1161. for (i = 0; i < PNG_FILTER_VALUE_LAST; i++) if (filter_costs[i] >= 1.0)
  1162. {
  1163. png_ptr->inv_filter_costs[i] =
  1164. (png_uint_16)(PNG_COST_FACTOR / filter_costs[i] + .5);
  1165. png_ptr->filter_costs[i] =
  1166. (png_uint_16)(PNG_COST_FACTOR * filter_costs[i] + .5);
  1167. }
  1168. }
  1169. }
  1170. #endif /* FLOATING_POINT */
  1171. #ifdef PNG_FIXED_POINT_SUPPORTED
  1172. void PNGAPI
  1173. png_set_filter_heuristics_fixed(png_structp png_ptr, int heuristic_method,
  1174. int num_weights, png_const_fixed_point_p filter_weights,
  1175. png_const_fixed_point_p filter_costs)
  1176. {
  1177. png_debug(1, "in png_set_filter_heuristics_fixed");
  1178. /* The internal API allocates all the arrays and ensures that the elements of
  1179. * those arrays are set to the default value.
  1180. */
  1181. if (!png_init_filter_heuristics(png_ptr, heuristic_method, num_weights))
  1182. return;
  1183. /* If using the weighted method copy in the weights. */
  1184. if (heuristic_method == PNG_FILTER_HEURISTIC_WEIGHTED)
  1185. {
  1186. int i;
  1187. for (i = 0; i < num_weights; i++)
  1188. {
  1189. if (filter_weights[i] <= 0)
  1190. {
  1191. png_ptr->inv_filter_weights[i] =
  1192. png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
  1193. }
  1194. else
  1195. {
  1196. png_ptr->inv_filter_weights[i] = (png_uint_16)
  1197. ((PNG_WEIGHT_FACTOR*filter_weights[i]+PNG_FP_HALF)/PNG_FP_1);
  1198. png_ptr->filter_weights[i] = (png_uint_16)((PNG_WEIGHT_FACTOR*
  1199. PNG_FP_1+(filter_weights[i]/2))/filter_weights[i]);
  1200. }
  1201. }
  1202. /* Here is where we set the relative costs of the different filters. We
  1203. * should take the desired compression level into account when setting
  1204. * the costs, so that Paeth, for instance, has a high relative cost at low
  1205. * compression levels, while it has a lower relative cost at higher
  1206. * compression settings. The filter types are in order of increasing
  1207. * relative cost, so it would be possible to do this with an algorithm.
  1208. */
  1209. for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
  1210. if (filter_costs[i] >= PNG_FP_1)
  1211. {
  1212. png_uint_32 tmp;
  1213. /* Use a 32 bit unsigned temporary here because otherwise the
  1214. * intermediate value will be a 32 bit *signed* integer (ANSI rules)
  1215. * and this will get the wrong answer on division.
  1216. */
  1217. tmp = PNG_COST_FACTOR*PNG_FP_1 + (filter_costs[i]/2);
  1218. tmp /= filter_costs[i];
  1219. png_ptr->inv_filter_costs[i] = (png_uint_16)tmp;
  1220. tmp = PNG_COST_FACTOR * filter_costs[i] + PNG_FP_HALF;
  1221. tmp /= PNG_FP_1;
  1222. png_ptr->filter_costs[i] = (png_uint_16)tmp;
  1223. }
  1224. }
  1225. }
  1226. #endif /* FIXED_POINT */
  1227. #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
  1228. void PNGAPI
  1229. png_set_compression_level(png_structp png_ptr, int level)
  1230. {
  1231. png_debug(1, "in png_set_compression_level");
  1232. if (png_ptr == NULL)
  1233. return;
  1234. png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
  1235. png_ptr->zlib_level = level;
  1236. }
  1237. void PNGAPI
  1238. png_set_compression_mem_level(png_structp png_ptr, int mem_level)
  1239. {
  1240. png_debug(1, "in png_set_compression_mem_level");
  1241. if (png_ptr == NULL)
  1242. return;
  1243. png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
  1244. png_ptr->zlib_mem_level = mem_level;
  1245. }
  1246. void PNGAPI
  1247. png_set_compression_strategy(png_structp png_ptr, int strategy)
  1248. {
  1249. png_debug(1, "in png_set_compression_strategy");
  1250. if (png_ptr == NULL)
  1251. return;
  1252. png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
  1253. png_ptr->zlib_strategy = strategy;
  1254. }
  1255. void PNGAPI
  1256. png_set_compression_window_bits(png_structp png_ptr, int window_bits)
  1257. {
  1258. if (png_ptr == NULL)
  1259. return;
  1260. if (window_bits > 15)
  1261. png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
  1262. else if (window_bits < 8)
  1263. png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
  1264. #ifndef WBITS_8_OK
  1265. /* Avoid libpng bug with 256-byte windows */
  1266. if (window_bits == 8)
  1267. {
  1268. png_warning(png_ptr, "Compression window is being reset to 512");
  1269. window_bits = 9;
  1270. }
  1271. #endif
  1272. png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
  1273. png_ptr->zlib_window_bits = window_bits;
  1274. }
  1275. void PNGAPI
  1276. png_set_compression_method(png_structp png_ptr, int method)
  1277. {
  1278. png_debug(1, "in png_set_compression_method");
  1279. if (png_ptr == NULL)
  1280. return;
  1281. if (method != 8)
  1282. png_warning(png_ptr, "Only compression method 8 is supported by PNG");
  1283. png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
  1284. png_ptr->zlib_method = method;
  1285. }
  1286. void PNGAPI
  1287. png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
  1288. {
  1289. if (png_ptr == NULL)
  1290. return;
  1291. png_ptr->write_row_fn = write_row_fn;
  1292. }
  1293. #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
  1294. void PNGAPI
  1295. png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
  1296. write_user_transform_fn)
  1297. {
  1298. png_debug(1, "in png_set_write_user_transform_fn");
  1299. if (png_ptr == NULL)
  1300. return;
  1301. png_ptr->transformations |= PNG_USER_TRANSFORM;
  1302. png_ptr->write_user_transform_fn = write_user_transform_fn;
  1303. }
  1304. #endif
  1305. #ifdef PNG_INFO_IMAGE_SUPPORTED
  1306. void PNGAPI
  1307. png_write_png(png_structp png_ptr, png_infop info_ptr,
  1308. int transforms, voidp params)
  1309. {
  1310. if (png_ptr == NULL || info_ptr == NULL)
  1311. return;
  1312. /* Write the file header information. */
  1313. png_write_info(png_ptr, info_ptr);
  1314. /* ------ these transformations don't touch the info structure ------- */
  1315. #ifdef PNG_WRITE_INVERT_SUPPORTED
  1316. /* Invert monochrome pixels */
  1317. if (transforms & PNG_TRANSFORM_INVERT_MONO)
  1318. png_set_invert_mono(png_ptr);
  1319. #endif
  1320. #ifdef PNG_WRITE_SHIFT_SUPPORTED
  1321. /* Shift the pixels up to a legal bit depth and fill in
  1322. * as appropriate to correctly scale the image.
  1323. */
  1324. if ((transforms & PNG_TRANSFORM_SHIFT)
  1325. && (info_ptr->valid & PNG_INFO_sBIT))
  1326. png_set_shift(png_ptr, &info_ptr->sig_bit);
  1327. #endif
  1328. #ifdef PNG_WRITE_PACK_SUPPORTED
  1329. /* Pack pixels into bytes */
  1330. if (transforms & PNG_TRANSFORM_PACKING)
  1331. png_set_packing(png_ptr);
  1332. #endif
  1333. #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
  1334. /* Swap location of alpha bytes from ARGB to RGBA */
  1335. if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
  1336. png_set_swap_alpha(png_ptr);
  1337. #endif
  1338. #ifdef PNG_WRITE_FILLER_SUPPORTED
  1339. /* Pack XRGB/RGBX/ARGB/RGBA into * RGB (4 channels -> 3 channels) */
  1340. if (transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER)
  1341. png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
  1342. else if (transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE)
  1343. png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
  1344. #endif
  1345. #ifdef PNG_WRITE_BGR_SUPPORTED
  1346. /* Flip BGR pixels to RGB */
  1347. if (transforms & PNG_TRANSFORM_BGR)
  1348. png_set_bgr(png_ptr);
  1349. #endif
  1350. #ifdef PNG_WRITE_SWAP_SUPPORTED
  1351. /* Swap bytes of 16-bit files to most significant byte first */
  1352. if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
  1353. png_set_swap(png_ptr);
  1354. #endif
  1355. #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
  1356. /* Swap bits of 1, 2, 4 bit packed pixel formats */
  1357. if (transforms & PNG_TRANSFORM_PACKSWAP)
  1358. png_set_packswap(png_ptr);
  1359. #endif
  1360. #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
  1361. /* Invert the alpha channel from opacity to transparency */
  1362. if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
  1363. png_set_invert_alpha(png_ptr);
  1364. #endif
  1365. /* ----------------------- end of transformations ------------------- */
  1366. /* Write the bits */
  1367. if (info_ptr->valid & PNG_INFO_IDAT)
  1368. png_write_image(png_ptr, info_ptr->row_pointers);
  1369. /* It is REQUIRED to call this to finish writing the rest of the file */
  1370. png_write_end(png_ptr, info_ptr);
  1371. PNG_UNUSED(transforms) /* Quiet compiler warnings */
  1372. PNG_UNUSED(params)
  1373. }
  1374. #endif
  1375. #endif /* PNG_WRITE_SUPPORTED */