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.

1515 lines
49KB

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