pngwrite.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513
  1. /* pngwrite.c - general routines to write a PNG file
  2. *
  3. * Last changed in libpng 1.2.9 April 14, 2006
  4. * For conditions of distribution and use, see copyright notice in png.h
  5. * Copyright (c) 1998-2006 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. #if 0
  375. /* This flush, added in libpng-1.0.8, causes some applications to crash
  376. because they do not set png_ptr->output_flush_fn */
  377. png_flush(png_ptr);
  378. #endif
  379. }
  380. #if defined(PNG_WRITE_tIME_SUPPORTED)
  381. #if !defined(_WIN32_WCE)
  382. /* "time.h" functions are not supported on WindowsCE */
  383. void PNGAPI
  384. png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
  385. {
  386. png_debug(1, "in png_convert_from_struct_tm\n");
  387. ptime->year = (png_uint_16)(1900 + ttime->tm_year);
  388. ptime->month = (png_byte)(ttime->tm_mon + 1);
  389. ptime->day = (png_byte)ttime->tm_mday;
  390. ptime->hour = (png_byte)ttime->tm_hour;
  391. ptime->minute = (png_byte)ttime->tm_min;
  392. ptime->second = (png_byte)ttime->tm_sec;
  393. }
  394. void PNGAPI
  395. png_convert_from_time_t(png_timep ptime, time_t ttime)
  396. {
  397. struct tm *tbuf;
  398. png_debug(1, "in png_convert_from_time_t\n");
  399. tbuf = gmtime(&ttime);
  400. png_convert_from_struct_tm(ptime, tbuf);
  401. }
  402. #endif
  403. #endif
  404. /* Initialize png_ptr structure, and allocate any memory needed */
  405. png_structp PNGAPI
  406. png_create_write_struct(png_const_charp user_png_ver, png_voidp error_ptr,
  407. png_error_ptr error_fn, png_error_ptr warn_fn)
  408. {
  409. #ifdef PNG_USER_MEM_SUPPORTED
  410. return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
  411. warn_fn, png_voidp_NULL, png_malloc_ptr_NULL, png_free_ptr_NULL));
  412. }
  413. /* Alternate initialize png_ptr structure, and allocate any memory needed */
  414. png_structp PNGAPI
  415. png_create_write_struct_2(png_const_charp user_png_ver, png_voidp error_ptr,
  416. png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
  417. png_malloc_ptr malloc_fn, png_free_ptr free_fn)
  418. {
  419. #endif /* PNG_USER_MEM_SUPPORTED */
  420. png_structp png_ptr;
  421. #ifdef PNG_SETJMP_SUPPORTED
  422. #ifdef USE_FAR_KEYWORD
  423. jmp_buf jmpbuf;
  424. #endif
  425. #endif
  426. int i;
  427. png_debug(1, "in png_create_write_struct\n");
  428. #ifdef PNG_USER_MEM_SUPPORTED
  429. png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
  430. (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
  431. #else
  432. png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
  433. #endif /* PNG_USER_MEM_SUPPORTED */
  434. if (png_ptr == NULL)
  435. return (NULL);
  436. #if !defined(PNG_1_0_X)
  437. #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
  438. png_init_mmx_flags(png_ptr); /* 1.2.0 addition */
  439. #endif
  440. #endif /* PNG_1_0_X */
  441. /* added at libpng-1.2.6 */
  442. #ifdef PNG_SET_USER_LIMITS_SUPPORTED
  443. png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
  444. png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
  445. #endif
  446. #ifdef PNG_SETJMP_SUPPORTED
  447. #ifdef USE_FAR_KEYWORD
  448. if (setjmp(jmpbuf))
  449. #else
  450. if (setjmp(png_ptr->jmpbuf))
  451. #endif
  452. {
  453. png_free(png_ptr, png_ptr->zbuf);
  454. png_ptr->zbuf=NULL;
  455. png_destroy_struct(png_ptr);
  456. return (NULL);
  457. }
  458. #ifdef USE_FAR_KEYWORD
  459. png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
  460. #endif
  461. #endif
  462. #ifdef PNG_USER_MEM_SUPPORTED
  463. png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
  464. #endif /* PNG_USER_MEM_SUPPORTED */
  465. png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
  466. i=0;
  467. do
  468. {
  469. if(user_png_ver[i] != png_libpng_ver[i])
  470. png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  471. } while (png_libpng_ver[i++]);
  472. if (png_ptr->flags & PNG_FLAG_LIBRARY_MISMATCH)
  473. {
  474. /* Libpng 0.90 and later are binary incompatible with libpng 0.89, so
  475. * we must recompile any applications that use any older library version.
  476. * For versions after libpng 1.0, we will be compatible, so we need
  477. * only check the first digit.
  478. */
  479. if (user_png_ver == NULL || user_png_ver[0] != png_libpng_ver[0] ||
  480. (user_png_ver[0] == '1' && user_png_ver[2] != png_libpng_ver[2]) ||
  481. (user_png_ver[0] == '0' && user_png_ver[2] < '9'))
  482. {
  483. #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  484. char msg[80];
  485. if (user_png_ver)
  486. {
  487. sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
  488. user_png_ver);
  489. png_warning(png_ptr, msg);
  490. }
  491. sprintf(msg, "Application is running with png.c from libpng-%.20s",
  492. png_libpng_ver);
  493. png_warning(png_ptr, msg);
  494. #endif
  495. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  496. png_ptr->flags=0;
  497. #endif
  498. png_error(png_ptr,
  499. "Incompatible libpng version in application and library");
  500. }
  501. }
  502. /* initialize zbuf - compression buffer */
  503. png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  504. png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
  505. (png_uint_32)png_ptr->zbuf_size);
  506. png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
  507. png_flush_ptr_NULL);
  508. #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
  509. png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
  510. 1, png_doublep_NULL, png_doublep_NULL);
  511. #endif
  512. #ifdef PNG_SETJMP_SUPPORTED
  513. /* Applications that neglect to set up their own setjmp() and then encounter
  514. a png_error() will longjmp here. Since the jmpbuf is then meaningless we
  515. abort instead of returning. */
  516. #ifdef USE_FAR_KEYWORD
  517. if (setjmp(jmpbuf))
  518. PNG_ABORT();
  519. png_memcpy(png_ptr->jmpbuf,jmpbuf,png_sizeof(jmp_buf));
  520. #else
  521. if (setjmp(png_ptr->jmpbuf))
  522. PNG_ABORT();
  523. #endif
  524. #endif
  525. return (png_ptr);
  526. }
  527. /* Initialize png_ptr structure, and allocate any memory needed */
  528. #if defined(PNG_1_0_X) || defined(PNG_1_2_X)
  529. /* Deprecated. */
  530. #undef png_write_init
  531. void PNGAPI
  532. png_write_init(png_structp png_ptr)
  533. {
  534. /* We only come here via pre-1.0.7-compiled applications */
  535. png_write_init_2(png_ptr, "1.0.6 or earlier", 0, 0);
  536. }
  537. void PNGAPI
  538. png_write_init_2(png_structp png_ptr, png_const_charp user_png_ver,
  539. png_size_t png_struct_size, png_size_t png_info_size)
  540. {
  541. /* We only come here via pre-1.0.12-compiled applications */
  542. #if !defined(PNG_NO_STDIO) && !defined(_WIN32_WCE)
  543. if(png_sizeof(png_struct) > png_struct_size ||
  544. png_sizeof(png_info) > png_info_size)
  545. {
  546. char msg[80];
  547. png_ptr->warning_fn=NULL;
  548. if (user_png_ver)
  549. {
  550. sprintf(msg, "Application was compiled with png.h from libpng-%.20s",
  551. user_png_ver);
  552. png_warning(png_ptr, msg);
  553. }
  554. sprintf(msg, "Application is running with png.c from libpng-%.20s",
  555. png_libpng_ver);
  556. png_warning(png_ptr, msg);
  557. }
  558. #endif
  559. if(png_sizeof(png_struct) > png_struct_size)
  560. {
  561. png_ptr->error_fn=NULL;
  562. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  563. png_ptr->flags=0;
  564. #endif
  565. png_error(png_ptr,
  566. "The png struct allocated by the application for writing is too small.");
  567. }
  568. if(png_sizeof(png_info) > png_info_size)
  569. {
  570. png_ptr->error_fn=NULL;
  571. #ifdef PNG_ERROR_NUMBERS_SUPPORTED
  572. png_ptr->flags=0;
  573. #endif
  574. png_error(png_ptr,
  575. "The info struct allocated by the application for writing is too small.");
  576. }
  577. png_write_init_3(&png_ptr, user_png_ver, png_struct_size);
  578. }
  579. #endif /* PNG_1_0_X || PNG_1_2_X */
  580. void PNGAPI
  581. png_write_init_3(png_structpp ptr_ptr, png_const_charp user_png_ver,
  582. png_size_t png_struct_size)
  583. {
  584. png_structp png_ptr=*ptr_ptr;
  585. #ifdef PNG_SETJMP_SUPPORTED
  586. jmp_buf tmp_jmp; /* to save current jump buffer */
  587. #endif
  588. int i = 0;
  589. if (png_ptr == NULL)
  590. return;
  591. do
  592. {
  593. if (user_png_ver[i] != png_libpng_ver[i])
  594. {
  595. #ifdef PNG_LEGACY_SUPPORTED
  596. png_ptr->flags |= PNG_FLAG_LIBRARY_MISMATCH;
  597. #else
  598. png_ptr->warning_fn=NULL;
  599. png_warning(png_ptr,
  600. "Application uses deprecated png_write_init() and should be recompiled.");
  601. break;
  602. #endif
  603. }
  604. } while (png_libpng_ver[i++]);
  605. png_debug(1, "in png_write_init_3\n");
  606. #ifdef PNG_SETJMP_SUPPORTED
  607. /* save jump buffer and error functions */
  608. png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
  609. #endif
  610. if (png_sizeof(png_struct) > png_struct_size)
  611. {
  612. png_destroy_struct(png_ptr);
  613. png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
  614. *ptr_ptr = png_ptr;
  615. }
  616. /* reset all variables to 0 */
  617. png_memset(png_ptr, 0, png_sizeof (png_struct));
  618. /* added at libpng-1.2.6 */
  619. #ifdef PNG_SET_USER_LIMITS_SUPPORTED
  620. png_ptr->user_width_max=PNG_USER_WIDTH_MAX;
  621. png_ptr->user_height_max=PNG_USER_HEIGHT_MAX;
  622. #endif
  623. #if !defined(PNG_1_0_X)
  624. #ifdef PNG_ASSEMBLER_CODE_SUPPORTED
  625. png_init_mmx_flags(png_ptr); /* 1.2.0 addition */
  626. #endif
  627. #endif /* PNG_1_0_X */
  628. #ifdef PNG_SETJMP_SUPPORTED
  629. /* restore jump buffer */
  630. png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
  631. #endif
  632. png_set_write_fn(png_ptr, png_voidp_NULL, png_rw_ptr_NULL,
  633. png_flush_ptr_NULL);
  634. /* initialize zbuf - compression buffer */
  635. png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  636. png_ptr->zbuf = (png_bytep)png_malloc(png_ptr,
  637. (png_uint_32)png_ptr->zbuf_size);
  638. #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
  639. png_set_filter_heuristics(png_ptr, PNG_FILTER_HEURISTIC_DEFAULT,
  640. 1, png_doublep_NULL, png_doublep_NULL);
  641. #endif
  642. }
  643. /* Write a few rows of image data. If the image is interlaced,
  644. * either you will have to write the 7 sub images, or, if you
  645. * have called png_set_interlace_handling(), you will have to
  646. * "write" the image seven times.
  647. */
  648. void PNGAPI
  649. png_write_rows(png_structp png_ptr, png_bytepp row,
  650. png_uint_32 num_rows)
  651. {
  652. png_uint_32 i; /* row counter */
  653. png_bytepp rp; /* row pointer */
  654. png_debug(1, "in png_write_rows\n");
  655. if (png_ptr == NULL)
  656. return;
  657. /* loop through the rows */
  658. for (i = 0, rp = row; i < num_rows; i++, rp++)
  659. {
  660. png_write_row(png_ptr, *rp);
  661. }
  662. }
  663. /* Write the image. You only need to call this function once, even
  664. * if you are writing an interlaced image.
  665. */
  666. void PNGAPI
  667. png_write_image(png_structp png_ptr, png_bytepp image)
  668. {
  669. png_uint_32 i; /* row index */
  670. int pass, num_pass; /* pass variables */
  671. png_bytepp rp; /* points to current row */
  672. if (png_ptr == NULL)
  673. return;
  674. png_debug(1, "in png_write_image\n");
  675. #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
  676. /* intialize interlace handling. If image is not interlaced,
  677. this will set pass to 1 */
  678. num_pass = png_set_interlace_handling(png_ptr);
  679. #else
  680. num_pass = 1;
  681. #endif
  682. /* loop through passes */
  683. for (pass = 0; pass < num_pass; pass++)
  684. {
  685. /* loop through image */
  686. for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
  687. {
  688. png_write_row(png_ptr, *rp);
  689. }
  690. }
  691. }
  692. /* called by user to write a row of image data */
  693. void PNGAPI
  694. png_write_row(png_structp png_ptr, png_bytep row)
  695. {
  696. if (png_ptr == NULL)
  697. return;
  698. png_debug2(1, "in png_write_row (row %ld, pass %d)\n",
  699. png_ptr->row_number, png_ptr->pass);
  700. /* initialize transformations and other stuff if first time */
  701. if (png_ptr->row_number == 0 && png_ptr->pass == 0)
  702. {
  703. /* make sure we wrote the header info */
  704. if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
  705. png_error(png_ptr,
  706. "png_write_info was never called before png_write_row.");
  707. /* check for transforms that have been set but were defined out */
  708. #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
  709. if (png_ptr->transformations & PNG_INVERT_MONO)
  710. png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined.");
  711. #endif
  712. #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
  713. if (png_ptr->transformations & PNG_FILLER)
  714. png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined.");
  715. #endif
  716. #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && defined(PNG_READ_PACKSWAP_SUPPORTED)
  717. if (png_ptr->transformations & PNG_PACKSWAP)
  718. png_warning(png_ptr, "PNG_WRITE_PACKSWAP_SUPPORTED is not defined.");
  719. #endif
  720. #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
  721. if (png_ptr->transformations & PNG_PACK)
  722. png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined.");
  723. #endif
  724. #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
  725. if (png_ptr->transformations & PNG_SHIFT)
  726. png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined.");
  727. #endif
  728. #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
  729. if (png_ptr->transformations & PNG_BGR)
  730. png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined.");
  731. #endif
  732. #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
  733. if (png_ptr->transformations & PNG_SWAP_BYTES)
  734. png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined.");
  735. #endif
  736. png_write_start_row(png_ptr);
  737. }
  738. #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
  739. /* if interlaced and not interested in row, return */
  740. if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
  741. {
  742. switch (png_ptr->pass)
  743. {
  744. case 0:
  745. if (png_ptr->row_number & 0x07)
  746. {
  747. png_write_finish_row(png_ptr);
  748. return;
  749. }
  750. break;
  751. case 1:
  752. if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
  753. {
  754. png_write_finish_row(png_ptr);
  755. return;
  756. }
  757. break;
  758. case 2:
  759. if ((png_ptr->row_number & 0x07) != 4)
  760. {
  761. png_write_finish_row(png_ptr);
  762. return;
  763. }
  764. break;
  765. case 3:
  766. if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
  767. {
  768. png_write_finish_row(png_ptr);
  769. return;
  770. }
  771. break;
  772. case 4:
  773. if ((png_ptr->row_number & 0x03) != 2)
  774. {
  775. png_write_finish_row(png_ptr);
  776. return;
  777. }
  778. break;
  779. case 5:
  780. if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
  781. {
  782. png_write_finish_row(png_ptr);
  783. return;
  784. }
  785. break;
  786. case 6:
  787. if (!(png_ptr->row_number & 0x01))
  788. {
  789. png_write_finish_row(png_ptr);
  790. return;
  791. }
  792. break;
  793. }
  794. }
  795. #endif
  796. /* set up row info for transformations */
  797. png_ptr->row_info.color_type = png_ptr->color_type;
  798. png_ptr->row_info.width = png_ptr->usr_width;
  799. png_ptr->row_info.channels = png_ptr->usr_channels;
  800. png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
  801. png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
  802. png_ptr->row_info.channels);
  803. png_ptr->row_info.rowbytes = PNG_ROWBYTES(png_ptr->row_info.pixel_depth,
  804. png_ptr->row_info.width);
  805. png_debug1(3, "row_info->color_type = %d\n", png_ptr->row_info.color_type);
  806. png_debug1(3, "row_info->width = %lu\n", png_ptr->row_info.width);
  807. png_debug1(3, "row_info->channels = %d\n", png_ptr->row_info.channels);
  808. png_debug1(3, "row_info->bit_depth = %d\n", png_ptr->row_info.bit_depth);
  809. png_debug1(3, "row_info->pixel_depth = %d\n", png_ptr->row_info.pixel_depth);
  810. png_debug1(3, "row_info->rowbytes = %lu\n", png_ptr->row_info.rowbytes);
  811. /* Copy user's row into buffer, leaving room for filter byte. */
  812. png_memcpy_check(png_ptr, png_ptr->row_buf + 1, row,
  813. png_ptr->row_info.rowbytes);
  814. #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
  815. /* handle interlacing */
  816. if (png_ptr->interlaced && png_ptr->pass < 6 &&
  817. (png_ptr->transformations & PNG_INTERLACE))
  818. {
  819. png_do_write_interlace(&(png_ptr->row_info),
  820. png_ptr->row_buf + 1, png_ptr->pass);
  821. /* this should always get caught above, but still ... */
  822. if (!(png_ptr->row_info.width))
  823. {
  824. png_write_finish_row(png_ptr);
  825. return;
  826. }
  827. }
  828. #endif
  829. /* handle other transformations */
  830. if (png_ptr->transformations)
  831. png_do_write_transformations(png_ptr);
  832. #if defined(PNG_MNG_FEATURES_SUPPORTED)
  833. /* Write filter_method 64 (intrapixel differencing) only if
  834. * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
  835. * 2. Libpng did not write a PNG signature (this filter_method is only
  836. * used in PNG datastreams that are embedded in MNG datastreams) and
  837. * 3. The application called png_permit_mng_features with a mask that
  838. * included PNG_FLAG_MNG_FILTER_64 and
  839. * 4. The filter_method is 64 and
  840. * 5. The color_type is RGB or RGBA
  841. */
  842. if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  843. (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
  844. {
  845. /* Intrapixel differencing */
  846. png_do_write_intrapixel(&(png_ptr->row_info), png_ptr->row_buf + 1);
  847. }
  848. #endif
  849. /* Find a filter if necessary, filter the row and write it out. */
  850. png_write_find_filter(png_ptr, &(png_ptr->row_info));
  851. if (png_ptr->write_row_fn != NULL)
  852. (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
  853. }
  854. #if defined(PNG_WRITE_FLUSH_SUPPORTED)
  855. /* Set the automatic flush interval or 0 to turn flushing off */
  856. void PNGAPI
  857. png_set_flush(png_structp png_ptr, int nrows)
  858. {
  859. png_debug(1, "in png_set_flush\n");
  860. if (png_ptr == NULL)
  861. return;
  862. png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
  863. }
  864. /* flush the current output buffers now */
  865. void PNGAPI
  866. png_write_flush(png_structp png_ptr)
  867. {
  868. int wrote_IDAT;
  869. png_debug(1, "in png_write_flush\n");
  870. if (png_ptr == NULL)
  871. return;
  872. /* We have already written out all of the data */
  873. if (png_ptr->row_number >= png_ptr->num_rows)
  874. return;
  875. do
  876. {
  877. int ret;
  878. /* compress the data */
  879. ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
  880. wrote_IDAT = 0;
  881. /* check for compression errors */
  882. if (ret != Z_OK)
  883. {
  884. if (png_ptr->zstream.msg != NULL)
  885. png_error(png_ptr, png_ptr->zstream.msg);
  886. else
  887. png_error(png_ptr, "zlib error");
  888. }
  889. if (!(png_ptr->zstream.avail_out))
  890. {
  891. /* write the IDAT and reset the zlib output buffer */
  892. png_write_IDAT(png_ptr, png_ptr->zbuf,
  893. png_ptr->zbuf_size);
  894. png_ptr->zstream.next_out = png_ptr->zbuf;
  895. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  896. wrote_IDAT = 1;
  897. }
  898. } while(wrote_IDAT == 1);
  899. /* If there is any data left to be output, write it into a new IDAT */
  900. if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
  901. {
  902. /* write the IDAT and reset the zlib output buffer */
  903. png_write_IDAT(png_ptr, png_ptr->zbuf,
  904. png_ptr->zbuf_size - png_ptr->zstream.avail_out);
  905. png_ptr->zstream.next_out = png_ptr->zbuf;
  906. png_ptr->zstream.avail_out = (uInt)png_ptr->zbuf_size;
  907. }
  908. png_ptr->flush_rows = 0;
  909. png_flush(png_ptr);
  910. }
  911. #endif /* PNG_WRITE_FLUSH_SUPPORTED */
  912. /* free all memory used by the write */
  913. void PNGAPI
  914. png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
  915. {
  916. png_structp png_ptr = NULL;
  917. png_infop info_ptr = NULL;
  918. #ifdef PNG_USER_MEM_SUPPORTED
  919. png_free_ptr free_fn = NULL;
  920. png_voidp mem_ptr = NULL;
  921. #endif
  922. png_debug(1, "in png_destroy_write_struct\n");
  923. if (png_ptr_ptr != NULL)
  924. {
  925. png_ptr = *png_ptr_ptr;
  926. #ifdef PNG_USER_MEM_SUPPORTED
  927. free_fn = png_ptr->free_fn;
  928. mem_ptr = png_ptr->mem_ptr;
  929. #endif
  930. }
  931. if (info_ptr_ptr != NULL)
  932. info_ptr = *info_ptr_ptr;
  933. if (info_ptr != NULL)
  934. {
  935. png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
  936. #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
  937. if (png_ptr->num_chunk_list)
  938. {
  939. png_free(png_ptr, png_ptr->chunk_list);
  940. png_ptr->chunk_list=NULL;
  941. png_ptr->num_chunk_list=0;
  942. }
  943. #endif
  944. #ifdef PNG_USER_MEM_SUPPORTED
  945. png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
  946. (png_voidp)mem_ptr);
  947. #else
  948. png_destroy_struct((png_voidp)info_ptr);
  949. #endif
  950. *info_ptr_ptr = NULL;
  951. }
  952. if (png_ptr != NULL)
  953. {
  954. png_write_destroy(png_ptr);
  955. #ifdef PNG_USER_MEM_SUPPORTED
  956. png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
  957. (png_voidp)mem_ptr);
  958. #else
  959. png_destroy_struct((png_voidp)png_ptr);
  960. #endif
  961. *png_ptr_ptr = NULL;
  962. }
  963. }
  964. /* Free any memory used in png_ptr struct (old method) */
  965. void /* PRIVATE */
  966. png_write_destroy(png_structp png_ptr)
  967. {
  968. #ifdef PNG_SETJMP_SUPPORTED
  969. jmp_buf tmp_jmp; /* save jump buffer */
  970. #endif
  971. png_error_ptr error_fn;
  972. png_error_ptr warning_fn;
  973. png_voidp error_ptr;
  974. #ifdef PNG_USER_MEM_SUPPORTED
  975. png_free_ptr free_fn;
  976. #endif
  977. png_debug(1, "in png_write_destroy\n");
  978. /* free any memory zlib uses */
  979. deflateEnd(&png_ptr->zstream);
  980. /* free our memory. png_free checks NULL for us. */
  981. png_free(png_ptr, png_ptr->zbuf);
  982. png_free(png_ptr, png_ptr->row_buf);
  983. png_free(png_ptr, png_ptr->prev_row);
  984. png_free(png_ptr, png_ptr->sub_row);
  985. png_free(png_ptr, png_ptr->up_row);
  986. png_free(png_ptr, png_ptr->avg_row);
  987. png_free(png_ptr, png_ptr->paeth_row);
  988. #if defined(PNG_TIME_RFC1123_SUPPORTED)
  989. png_free(png_ptr, png_ptr->time_buffer);
  990. #endif
  991. #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED)
  992. png_free(png_ptr, png_ptr->prev_filters);
  993. png_free(png_ptr, png_ptr->filter_weights);
  994. png_free(png_ptr, png_ptr->inv_filter_weights);
  995. png_free(png_ptr, png_ptr->filter_costs);
  996. png_free(png_ptr, png_ptr->inv_filter_costs);
  997. #endif
  998. #ifdef PNG_SETJMP_SUPPORTED
  999. /* reset structure */
  1000. png_memcpy(tmp_jmp, png_ptr->jmpbuf, png_sizeof (jmp_buf));
  1001. #endif
  1002. error_fn = png_ptr->error_fn;
  1003. warning_fn = png_ptr->warning_fn;
  1004. error_ptr = png_ptr->error_ptr;
  1005. #ifdef PNG_USER_MEM_SUPPORTED
  1006. free_fn = png_ptr->free_fn;
  1007. #endif
  1008. png_memset(png_ptr, 0, png_sizeof (png_struct));
  1009. png_ptr->error_fn = error_fn;
  1010. png_ptr->warning_fn = warning_fn;
  1011. png_ptr->error_ptr = error_ptr;
  1012. #ifdef PNG_USER_MEM_SUPPORTED
  1013. png_ptr->free_fn = free_fn;
  1014. #endif
  1015. #ifdef PNG_SETJMP_SUPPORTED
  1016. png_memcpy(png_ptr->jmpbuf, tmp_jmp, png_sizeof (jmp_buf));
  1017. #endif
  1018. }
  1019. /* Allow the application to select one or more row filters to use. */
  1020. void PNGAPI
  1021. png_set_filter(png_structp png_ptr, int method, int filters)
  1022. {
  1023. png_debug(1, "in png_set_filter\n");
  1024. if (png_ptr == NULL)
  1025. return;
  1026. #if defined(PNG_MNG_FEATURES_SUPPORTED)
  1027. if((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  1028. (method == PNG_INTRAPIXEL_DIFFERENCING))
  1029. method = PNG_FILTER_TYPE_BASE;
  1030. #endif
  1031. if (method == PNG_FILTER_TYPE_BASE)
  1032. {
  1033. switch (filters & (PNG_ALL_FILTERS | 0x07))
  1034. {
  1035. case 5:
  1036. case 6:
  1037. case 7: png_warning(png_ptr, "Unknown row filter for method 0");
  1038. case PNG_FILTER_VALUE_NONE: png_ptr->do_filter=PNG_FILTER_NONE; break;
  1039. case PNG_FILTER_VALUE_SUB: png_ptr->do_filter=PNG_FILTER_SUB; break;
  1040. case PNG_FILTER_VALUE_UP: png_ptr->do_filter=PNG_FILTER_UP; break;
  1041. case PNG_FILTER_VALUE_AVG: png_ptr->do_filter=PNG_FILTER_AVG; break;
  1042. case PNG_FILTER_VALUE_PAETH: png_ptr->do_filter=PNG_FILTER_PAETH;break;
  1043. default: png_ptr->do_filter = (png_byte)filters; break;
  1044. }
  1045. /* If we have allocated the row_buf, this means we have already started
  1046. * with the image and we should have allocated all of the filter buffers
  1047. * that have been selected. If prev_row isn't already allocated, then
  1048. * it is too late to start using the filters that need it, since we
  1049. * will be missing the data in the previous row. If an application
  1050. * wants to start and stop using particular filters during compression,
  1051. * it should start out with all of the filters, and then add and
  1052. * remove them after the start of compression.
  1053. */
  1054. if (png_ptr->row_buf != NULL)
  1055. {
  1056. if ((png_ptr->do_filter & PNG_FILTER_SUB) && png_ptr->sub_row == NULL)
  1057. {
  1058. png_ptr->sub_row = (png_bytep)png_malloc(png_ptr,
  1059. (png_ptr->rowbytes + 1));
  1060. png_ptr->sub_row[0] = PNG_FILTER_VALUE_SUB;
  1061. }
  1062. if ((png_ptr->do_filter & PNG_FILTER_UP) && png_ptr->up_row == NULL)
  1063. {
  1064. if (png_ptr->prev_row == NULL)
  1065. {
  1066. png_warning(png_ptr, "Can't add Up filter after starting");
  1067. png_ptr->do_filter &= ~PNG_FILTER_UP;
  1068. }
  1069. else
  1070. {
  1071. png_ptr->up_row = (png_bytep)png_malloc(png_ptr,
  1072. (png_ptr->rowbytes + 1));
  1073. png_ptr->up_row[0] = PNG_FILTER_VALUE_UP;
  1074. }
  1075. }
  1076. if ((png_ptr->do_filter & PNG_FILTER_AVG) && png_ptr->avg_row == NULL)
  1077. {
  1078. if (png_ptr->prev_row == NULL)
  1079. {
  1080. png_warning(png_ptr, "Can't add Average filter after starting");
  1081. png_ptr->do_filter &= ~PNG_FILTER_AVG;
  1082. }
  1083. else
  1084. {
  1085. png_ptr->avg_row = (png_bytep)png_malloc(png_ptr,
  1086. (png_ptr->rowbytes + 1));
  1087. png_ptr->avg_row[0] = PNG_FILTER_VALUE_AVG;
  1088. }
  1089. }
  1090. if ((png_ptr->do_filter & PNG_FILTER_PAETH) &&
  1091. png_ptr->paeth_row == NULL)
  1092. {
  1093. if (png_ptr->prev_row == NULL)
  1094. {
  1095. png_warning(png_ptr, "Can't add Paeth filter after starting");
  1096. png_ptr->do_filter &= (png_byte)(~PNG_FILTER_PAETH);
  1097. }
  1098. else
  1099. {
  1100. png_ptr->paeth_row = (png_bytep)png_malloc(png_ptr,
  1101. (png_ptr->rowbytes + 1));
  1102. png_ptr->paeth_row[0] = PNG_FILTER_VALUE_PAETH;
  1103. }
  1104. }
  1105. if (png_ptr->do_filter == PNG_NO_FILTERS)
  1106. png_ptr->do_filter = PNG_FILTER_NONE;
  1107. }
  1108. }
  1109. else
  1110. png_error(png_ptr, "Unknown custom filter method");
  1111. }
  1112. /* This allows us to influence the way in which libpng chooses the "best"
  1113. * filter for the current scanline. While the "minimum-sum-of-absolute-
  1114. * differences metric is relatively fast and effective, there is some
  1115. * question as to whether it can be improved upon by trying to keep the
  1116. * filtered data going to zlib more consistent, hopefully resulting in
  1117. * better compression.
  1118. */
  1119. #if defined(PNG_WRITE_WEIGHTED_FILTER_SUPPORTED) /* GRR 970116 */
  1120. void PNGAPI
  1121. png_set_filter_heuristics(png_structp png_ptr, int heuristic_method,
  1122. int num_weights, png_doublep filter_weights,
  1123. png_doublep filter_costs)
  1124. {
  1125. int i;
  1126. png_debug(1, "in png_set_filter_heuristics\n");
  1127. if (png_ptr == NULL)
  1128. return;
  1129. if (heuristic_method >= PNG_FILTER_HEURISTIC_LAST)
  1130. {
  1131. png_warning(png_ptr, "Unknown filter heuristic method");
  1132. return;
  1133. }
  1134. if (heuristic_method == PNG_FILTER_HEURISTIC_DEFAULT)
  1135. {
  1136. heuristic_method = PNG_FILTER_HEURISTIC_UNWEIGHTED;
  1137. }
  1138. if (num_weights < 0 || filter_weights == NULL ||
  1139. heuristic_method == PNG_FILTER_HEURISTIC_UNWEIGHTED)
  1140. {
  1141. num_weights = 0;
  1142. }
  1143. png_ptr->num_prev_filters = (png_byte)num_weights;
  1144. png_ptr->heuristic_method = (png_byte)heuristic_method;
  1145. if (num_weights > 0)
  1146. {
  1147. if (png_ptr->prev_filters == NULL)
  1148. {
  1149. png_ptr->prev_filters = (png_bytep)png_malloc(png_ptr,
  1150. (png_uint_32)(png_sizeof(png_byte) * num_weights));
  1151. /* To make sure that the weighting starts out fairly */
  1152. for (i = 0; i < num_weights; i++)
  1153. {
  1154. png_ptr->prev_filters[i] = 255;
  1155. }
  1156. }
  1157. if (png_ptr->filter_weights == NULL)
  1158. {
  1159. png_ptr->filter_weights = (png_uint_16p)png_malloc(png_ptr,
  1160. (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
  1161. png_ptr->inv_filter_weights = (png_uint_16p)png_malloc(png_ptr,
  1162. (png_uint_32)(png_sizeof(png_uint_16) * num_weights));
  1163. for (i = 0; i < num_weights; i++)
  1164. {
  1165. png_ptr->inv_filter_weights[i] =
  1166. png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
  1167. }
  1168. }
  1169. for (i = 0; i < num_weights; i++)
  1170. {
  1171. if (filter_weights[i] < 0.0)
  1172. {
  1173. png_ptr->inv_filter_weights[i] =
  1174. png_ptr->filter_weights[i] = PNG_WEIGHT_FACTOR;
  1175. }
  1176. else
  1177. {
  1178. png_ptr->inv_filter_weights[i] =
  1179. (png_uint_16)((double)PNG_WEIGHT_FACTOR*filter_weights[i]+0.5);
  1180. png_ptr->filter_weights[i] =
  1181. (png_uint_16)((double)PNG_WEIGHT_FACTOR/filter_weights[i]+0.5);
  1182. }
  1183. }
  1184. }
  1185. /* If, in the future, there are other filter methods, this would
  1186. * need to be based on png_ptr->filter.
  1187. */
  1188. if (png_ptr->filter_costs == NULL)
  1189. {
  1190. png_ptr->filter_costs = (png_uint_16p)png_malloc(png_ptr,
  1191. (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
  1192. png_ptr->inv_filter_costs = (png_uint_16p)png_malloc(png_ptr,
  1193. (png_uint_32)(png_sizeof(png_uint_16) * PNG_FILTER_VALUE_LAST));
  1194. for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
  1195. {
  1196. png_ptr->inv_filter_costs[i] =
  1197. png_ptr->filter_costs[i] = PNG_COST_FACTOR;
  1198. }
  1199. }
  1200. /* Here is where we set the relative costs of the different filters. We
  1201. * should take the desired compression level into account when setting
  1202. * the costs, so that Paeth, for instance, has a high relative cost at low
  1203. * compression levels, while it has a lower relative cost at higher
  1204. * compression settings. The filter types are in order of increasing
  1205. * relative cost, so it would be possible to do this with an algorithm.
  1206. */
  1207. for (i = 0; i < PNG_FILTER_VALUE_LAST; i++)
  1208. {
  1209. if (filter_costs == NULL || filter_costs[i] < 0.0)
  1210. {
  1211. png_ptr->inv_filter_costs[i] =
  1212. png_ptr->filter_costs[i] = PNG_COST_FACTOR;
  1213. }
  1214. else if (filter_costs[i] >= 1.0)
  1215. {
  1216. png_ptr->inv_filter_costs[i] =
  1217. (png_uint_16)((double)PNG_COST_FACTOR / filter_costs[i] + 0.5);
  1218. png_ptr->filter_costs[i] =
  1219. (png_uint_16)((double)PNG_COST_FACTOR * filter_costs[i] + 0.5);
  1220. }
  1221. }
  1222. }
  1223. #endif /* PNG_WRITE_WEIGHTED_FILTER_SUPPORTED */
  1224. void PNGAPI
  1225. png_set_compression_level(png_structp png_ptr, int level)
  1226. {
  1227. png_debug(1, "in png_set_compression_level\n");
  1228. if (png_ptr == NULL)
  1229. return;
  1230. png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_LEVEL;
  1231. png_ptr->zlib_level = level;
  1232. }
  1233. void PNGAPI
  1234. png_set_compression_mem_level(png_structp png_ptr, int mem_level)
  1235. {
  1236. png_debug(1, "in png_set_compression_mem_level\n");
  1237. if (png_ptr == NULL)
  1238. return;
  1239. png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_MEM_LEVEL;
  1240. png_ptr->zlib_mem_level = mem_level;
  1241. }
  1242. void PNGAPI
  1243. png_set_compression_strategy(png_structp png_ptr, int strategy)
  1244. {
  1245. png_debug(1, "in png_set_compression_strategy\n");
  1246. if (png_ptr == NULL)
  1247. return;
  1248. png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
  1249. png_ptr->zlib_strategy = strategy;
  1250. }
  1251. void PNGAPI
  1252. png_set_compression_window_bits(png_structp png_ptr, int window_bits)
  1253. {
  1254. if (png_ptr == NULL)
  1255. return;
  1256. if (window_bits > 15)
  1257. png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
  1258. else if (window_bits < 8)
  1259. png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
  1260. #ifndef WBITS_8_OK
  1261. /* avoid libpng bug with 256-byte windows */
  1262. if (window_bits == 8)
  1263. {
  1264. png_warning(png_ptr, "Compression window is being reset to 512");
  1265. window_bits=9;
  1266. }
  1267. #endif
  1268. png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_WINDOW_BITS;
  1269. png_ptr->zlib_window_bits = window_bits;
  1270. }
  1271. void PNGAPI
  1272. png_set_compression_method(png_structp png_ptr, int method)
  1273. {
  1274. png_debug(1, "in png_set_compression_method\n");
  1275. if (png_ptr == NULL)
  1276. return;
  1277. if (method != 8)
  1278. png_warning(png_ptr, "Only compression method 8 is supported by PNG");
  1279. png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_METHOD;
  1280. png_ptr->zlib_method = method;
  1281. }
  1282. void PNGAPI
  1283. png_set_write_status_fn(png_structp png_ptr, png_write_status_ptr write_row_fn)
  1284. {
  1285. if (png_ptr == NULL)
  1286. return;
  1287. png_ptr->write_row_fn = write_row_fn;
  1288. }
  1289. #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
  1290. void PNGAPI
  1291. png_set_write_user_transform_fn(png_structp png_ptr, png_user_transform_ptr
  1292. write_user_transform_fn)
  1293. {
  1294. png_debug(1, "in png_set_write_user_transform_fn\n");
  1295. if (png_ptr == NULL)
  1296. return;
  1297. png_ptr->transformations |= PNG_USER_TRANSFORM;
  1298. png_ptr->write_user_transform_fn = write_user_transform_fn;
  1299. }
  1300. #endif
  1301. #if defined(PNG_INFO_IMAGE_SUPPORTED)
  1302. void PNGAPI
  1303. png_write_png(png_structp png_ptr, png_infop info_ptr,
  1304. int transforms, voidp params)
  1305. {
  1306. if (png_ptr == NULL || info_ptr == NULL)
  1307. return;
  1308. #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
  1309. /* invert the alpha channel from opacity to transparency */
  1310. if (transforms & PNG_TRANSFORM_INVERT_ALPHA)
  1311. png_set_invert_alpha(png_ptr);
  1312. #endif
  1313. /* Write the file header information. */
  1314. png_write_info(png_ptr, info_ptr);
  1315. /* ------ these transformations don't touch the info structure ------- */
  1316. #if defined(PNG_WRITE_INVERT_SUPPORTED)
  1317. /* invert monochrome pixels */
  1318. if (transforms & PNG_TRANSFORM_INVERT_MONO)
  1319. png_set_invert_mono(png_ptr);
  1320. #endif
  1321. #if defined(PNG_WRITE_SHIFT_SUPPORTED)
  1322. /* Shift the pixels up to a legal bit depth and fill in
  1323. * as appropriate to correctly scale the image.
  1324. */
  1325. if ((transforms & PNG_TRANSFORM_SHIFT)
  1326. && (info_ptr->valid & PNG_INFO_sBIT))
  1327. png_set_shift(png_ptr, &info_ptr->sig_bit);
  1328. #endif
  1329. #if defined(PNG_WRITE_PACK_SUPPORTED)
  1330. /* pack pixels into bytes */
  1331. if (transforms & PNG_TRANSFORM_PACKING)
  1332. png_set_packing(png_ptr);
  1333. #endif
  1334. #if defined(PNG_WRITE_SWAP_ALPHA_SUPPORTED)
  1335. /* swap location of alpha bytes from ARGB to RGBA */
  1336. if (transforms & PNG_TRANSFORM_SWAP_ALPHA)
  1337. png_set_swap_alpha(png_ptr);
  1338. #endif
  1339. #if defined(PNG_WRITE_FILLER_SUPPORTED)
  1340. /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
  1341. * RGB (4 channels -> 3 channels). The second parameter is not used.
  1342. */
  1343. if (transforms & PNG_TRANSFORM_STRIP_FILLER)
  1344. png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
  1345. #endif
  1346. #if defined(PNG_WRITE_BGR_SUPPORTED)
  1347. /* flip BGR pixels to RGB */
  1348. if (transforms & PNG_TRANSFORM_BGR)
  1349. png_set_bgr(png_ptr);
  1350. #endif
  1351. #if defined(PNG_WRITE_SWAP_SUPPORTED)
  1352. /* swap bytes of 16-bit files to most significant byte first */
  1353. if (transforms & PNG_TRANSFORM_SWAP_ENDIAN)
  1354. png_set_swap(png_ptr);
  1355. #endif
  1356. #if defined(PNG_WRITE_PACKSWAP_SUPPORTED)
  1357. /* swap bits of 1, 2, 4 bit packed pixel formats */
  1358. if (transforms & PNG_TRANSFORM_PACKSWAP)
  1359. png_set_packswap(png_ptr);
  1360. #endif
  1361. /* ----------------------- end of transformations ------------------- */
  1362. /* write the bits */
  1363. if (info_ptr->valid & PNG_INFO_IDAT)
  1364. png_write_image(png_ptr, info_ptr->row_pointers);
  1365. /* It is REQUIRED to call this to finish writing the rest of the file */
  1366. png_write_end(png_ptr, info_ptr);
  1367. if(transforms == 0 || params == NULL)
  1368. /* quiet compiler warnings */ return;
  1369. }
  1370. #endif
  1371. #endif /* PNG_WRITE_SUPPORTED */