pngwrite.c 50 KB

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