pngwrite.c 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668
  1. /* pngwrite.c - general routines to write a PNG file
  2. *
  3. * Last changed in libpng 1.5.11 [June 14, 2012]
  4. * Copyright (c) 1998-2012 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. /* "tm" structure is not supported on WindowsCE */
  376. void PNGAPI
  377. png_convert_from_struct_tm(png_timep ptime, PNG_CONST struct tm FAR * ttime)
  378. {
  379. png_debug(1, "in png_convert_from_struct_tm");
  380. ptime->year = (png_uint_16)(1900 + ttime->tm_year);
  381. ptime->month = (png_byte)(ttime->tm_mon + 1);
  382. ptime->day = (png_byte)ttime->tm_mday;
  383. ptime->hour = (png_byte)ttime->tm_hour;
  384. ptime->minute = (png_byte)ttime->tm_min;
  385. ptime->second = (png_byte)ttime->tm_sec;
  386. }
  387. void PNGAPI
  388. png_convert_from_time_t(png_timep ptime, time_t ttime)
  389. {
  390. struct tm *tbuf;
  391. png_debug(1, "in png_convert_from_time_t");
  392. tbuf = gmtime(&ttime);
  393. png_convert_from_struct_tm(ptime, tbuf);
  394. }
  395. #endif
  396. /* Initialize png_ptr structure, and allocate any memory needed */
  397. PNG_FUNCTION(png_structp,PNGAPI
  398. png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
  399. png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
  400. {
  401. #ifdef PNG_USER_MEM_SUPPORTED
  402. return (png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
  403. warn_fn, NULL, NULL, NULL));
  404. }
  405. /* Alternate initialize png_ptr structure, and allocate any memory needed */
  406. static void png_reset_filter_heuristics(png_structp png_ptr); /* forward decl */
  407. PNG_FUNCTION(png_structp,PNGAPI
  408. png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
  409. png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
  410. png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
  411. {
  412. #endif /* PNG_USER_MEM_SUPPORTED */
  413. volatile int png_cleanup_needed = 0;
  414. #ifdef PNG_SETJMP_SUPPORTED
  415. volatile
  416. #endif
  417. png_structp png_ptr;
  418. #ifdef PNG_SETJMP_SUPPORTED
  419. #ifdef USE_FAR_KEYWORD
  420. jmp_buf tmp_jmpbuf;
  421. #endif
  422. #endif
  423. png_debug(1, "in png_create_write_struct");
  424. #ifdef PNG_USER_MEM_SUPPORTED
  425. png_ptr = (png_structp)png_create_struct_2(PNG_STRUCT_PNG,
  426. (png_malloc_ptr)malloc_fn, (png_voidp)mem_ptr);
  427. #else
  428. png_ptr = (png_structp)png_create_struct(PNG_STRUCT_PNG);
  429. #endif /* PNG_USER_MEM_SUPPORTED */
  430. if (png_ptr == NULL)
  431. return (NULL);
  432. /* Added at libpng-1.2.6 */
  433. #ifdef PNG_SET_USER_LIMITS_SUPPORTED
  434. png_ptr->user_width_max = PNG_USER_WIDTH_MAX;
  435. png_ptr->user_height_max = PNG_USER_HEIGHT_MAX;
  436. #endif
  437. #ifdef PNG_SETJMP_SUPPORTED
  438. /* Applications that neglect to set up their own setjmp() and then
  439. * encounter a png_error() will longjmp here. Since the jmpbuf is
  440. * then meaningless we abort instead of returning.
  441. */
  442. #ifdef USE_FAR_KEYWORD
  443. if (setjmp(tmp_jmpbuf))
  444. #else
  445. if (setjmp(png_jmpbuf(png_ptr))) /* sets longjmp to match setjmp */
  446. #endif
  447. #ifdef USE_FAR_KEYWORD
  448. png_memcpy(png_jmpbuf(png_ptr), tmp_jmpbuf, png_sizeof(jmp_buf));
  449. #endif
  450. PNG_ABORT();
  451. #endif
  452. #ifdef PNG_USER_MEM_SUPPORTED
  453. png_set_mem_fn(png_ptr, mem_ptr, malloc_fn, free_fn);
  454. #endif /* PNG_USER_MEM_SUPPORTED */
  455. png_set_error_fn(png_ptr, error_ptr, error_fn, warn_fn);
  456. if (!png_user_version_check(png_ptr, user_png_ver))
  457. png_cleanup_needed = 1;
  458. /* Initialize zbuf - compression buffer */
  459. png_ptr->zbuf_size = PNG_ZBUF_SIZE;
  460. if (!png_cleanup_needed)
  461. {
  462. png_ptr->zbuf = (png_bytep)png_malloc_warn(png_ptr,
  463. png_ptr->zbuf_size);
  464. if (png_ptr->zbuf == NULL)
  465. png_cleanup_needed = 1;
  466. }
  467. if (png_cleanup_needed)
  468. {
  469. /* Clean up PNG structure and deallocate any memory. */
  470. png_free(png_ptr, png_ptr->zbuf);
  471. png_ptr->zbuf = NULL;
  472. #ifdef PNG_USER_MEM_SUPPORTED
  473. png_destroy_struct_2((png_voidp)png_ptr,
  474. (png_free_ptr)free_fn, (png_voidp)mem_ptr);
  475. #else
  476. png_destroy_struct((png_voidp)png_ptr);
  477. #endif
  478. return (NULL);
  479. }
  480. png_set_write_fn(png_ptr, NULL, NULL, NULL);
  481. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  482. png_reset_filter_heuristics(png_ptr);
  483. #endif
  484. return (png_ptr);
  485. }
  486. /* Write a few rows of image data. If the image is interlaced,
  487. * either you will have to write the 7 sub images, or, if you
  488. * have called png_set_interlace_handling(), you will have to
  489. * "write" the image seven times.
  490. */
  491. void PNGAPI
  492. png_write_rows(png_structp png_ptr, png_bytepp row,
  493. png_uint_32 num_rows)
  494. {
  495. png_uint_32 i; /* row counter */
  496. png_bytepp rp; /* row pointer */
  497. png_debug(1, "in png_write_rows");
  498. if (png_ptr == NULL)
  499. return;
  500. /* Loop through the rows */
  501. for (i = 0, rp = row; i < num_rows; i++, rp++)
  502. {
  503. png_write_row(png_ptr, *rp);
  504. }
  505. }
  506. /* Write the image. You only need to call this function once, even
  507. * if you are writing an interlaced image.
  508. */
  509. void PNGAPI
  510. png_write_image(png_structp png_ptr, png_bytepp image)
  511. {
  512. png_uint_32 i; /* row index */
  513. int pass, num_pass; /* pass variables */
  514. png_bytepp rp; /* points to current row */
  515. if (png_ptr == NULL)
  516. return;
  517. png_debug(1, "in png_write_image");
  518. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  519. /* Initialize interlace handling. If image is not interlaced,
  520. * this will set pass to 1
  521. */
  522. num_pass = png_set_interlace_handling(png_ptr);
  523. #else
  524. num_pass = 1;
  525. #endif
  526. /* Loop through passes */
  527. for (pass = 0; pass < num_pass; pass++)
  528. {
  529. /* Loop through image */
  530. for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
  531. {
  532. png_write_row(png_ptr, *rp);
  533. }
  534. }
  535. }
  536. /* Called by user to write a row of image data */
  537. void PNGAPI
  538. png_write_row(png_structp png_ptr, png_const_bytep row)
  539. {
  540. /* 1.5.6: moved from png_struct to be a local structure: */
  541. png_row_info row_info;
  542. if (png_ptr == NULL)
  543. return;
  544. png_debug2(1, "in png_write_row (row %u, pass %d)",
  545. png_ptr->row_number, png_ptr->pass);
  546. /* Initialize transformations and other stuff if first time */
  547. if (png_ptr->row_number == 0 && png_ptr->pass == 0)
  548. {
  549. /* Make sure we wrote the header info */
  550. if (!(png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE))
  551. png_error(png_ptr,
  552. "png_write_info was never called before png_write_row");
  553. /* Check for transforms that have been set but were defined out */
  554. #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
  555. if (png_ptr->transformations & PNG_INVERT_MONO)
  556. png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
  557. #endif
  558. #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
  559. if (png_ptr->transformations & PNG_FILLER)
  560. png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
  561. #endif
  562. #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
  563. defined(PNG_READ_PACKSWAP_SUPPORTED)
  564. if (png_ptr->transformations & PNG_PACKSWAP)
  565. png_warning(png_ptr,
  566. "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
  567. #endif
  568. #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
  569. if (png_ptr->transformations & PNG_PACK)
  570. png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
  571. #endif
  572. #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
  573. if (png_ptr->transformations & PNG_SHIFT)
  574. png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
  575. #endif
  576. #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
  577. if (png_ptr->transformations & PNG_BGR)
  578. png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
  579. #endif
  580. #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
  581. if (png_ptr->transformations & PNG_SWAP_BYTES)
  582. png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
  583. #endif
  584. png_write_start_row(png_ptr);
  585. }
  586. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  587. /* If interlaced and not interested in row, return */
  588. if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
  589. {
  590. switch (png_ptr->pass)
  591. {
  592. case 0:
  593. if (png_ptr->row_number & 0x07)
  594. {
  595. png_write_finish_row(png_ptr);
  596. return;
  597. }
  598. break;
  599. case 1:
  600. if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
  601. {
  602. png_write_finish_row(png_ptr);
  603. return;
  604. }
  605. break;
  606. case 2:
  607. if ((png_ptr->row_number & 0x07) != 4)
  608. {
  609. png_write_finish_row(png_ptr);
  610. return;
  611. }
  612. break;
  613. case 3:
  614. if ((png_ptr->row_number & 0x03) || png_ptr->width < 3)
  615. {
  616. png_write_finish_row(png_ptr);
  617. return;
  618. }
  619. break;
  620. case 4:
  621. if ((png_ptr->row_number & 0x03) != 2)
  622. {
  623. png_write_finish_row(png_ptr);
  624. return;
  625. }
  626. break;
  627. case 5:
  628. if ((png_ptr->row_number & 0x01) || png_ptr->width < 2)
  629. {
  630. png_write_finish_row(png_ptr);
  631. return;
  632. }
  633. break;
  634. case 6:
  635. if (!(png_ptr->row_number & 0x01))
  636. {
  637. png_write_finish_row(png_ptr);
  638. return;
  639. }
  640. break;
  641. default: /* error: ignore it */
  642. break;
  643. }
  644. }
  645. #endif
  646. /* Set up row info for transformations */
  647. row_info.color_type = png_ptr->color_type;
  648. row_info.width = png_ptr->usr_width;
  649. row_info.channels = png_ptr->usr_channels;
  650. row_info.bit_depth = png_ptr->usr_bit_depth;
  651. row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels);
  652. row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
  653. png_debug1(3, "row_info->color_type = %d", row_info.color_type);
  654. png_debug1(3, "row_info->width = %u", row_info.width);
  655. png_debug1(3, "row_info->channels = %d", row_info.channels);
  656. png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth);
  657. png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth);
  658. png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes);
  659. /* Copy user's row into buffer, leaving room for filter byte. */
  660. png_memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);
  661. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  662. /* Handle interlacing */
  663. if (png_ptr->interlaced && png_ptr->pass < 6 &&
  664. (png_ptr->transformations & PNG_INTERLACE))
  665. {
  666. png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass);
  667. /* This should always get caught above, but still ... */
  668. if (!(row_info.width))
  669. {
  670. png_write_finish_row(png_ptr);
  671. return;
  672. }
  673. }
  674. #endif
  675. #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
  676. /* Handle other transformations */
  677. if (png_ptr->transformations)
  678. png_do_write_transformations(png_ptr, &row_info);
  679. #endif
  680. /* At this point the row_info pixel depth must match the 'transformed' depth,
  681. * which is also the output depth.
  682. */
  683. if (row_info.pixel_depth != png_ptr->pixel_depth ||
  684. row_info.pixel_depth != png_ptr->transformed_pixel_depth)
  685. png_error(png_ptr, "internal write transform logic error");
  686. #ifdef PNG_MNG_FEATURES_SUPPORTED
  687. /* Write filter_method 64 (intrapixel differencing) only if
  688. * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
  689. * 2. Libpng did not write a PNG signature (this filter_method is only
  690. * used in PNG datastreams that are embedded in MNG datastreams) and
  691. * 3. The application called png_permit_mng_features with a mask that
  692. * included PNG_FLAG_MNG_FILTER_64 and
  693. * 4. The filter_method is 64 and
  694. * 5. The color_type is RGB or RGBA
  695. */
  696. if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  697. (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
  698. {
  699. /* Intrapixel differencing */
  700. png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1);
  701. }
  702. #endif
  703. /* Added at libpng-1.5.10 */
  704. #ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
  705. /* Check for out-of-range palette index */
  706. if (row_info.color_type == PNG_COLOR_TYPE_PALETTE &&
  707. png_ptr->num_palette_max >= 0)
  708. png_do_check_palette_indexes(png_ptr, &row_info);
  709. #endif
  710. /* Find a filter if necessary, filter the row and write it out. */
  711. png_write_find_filter(png_ptr, &row_info);
  712. if (png_ptr->write_row_fn != NULL)
  713. (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
  714. }
  715. #ifdef PNG_WRITE_FLUSH_SUPPORTED
  716. /* Set the automatic flush interval or 0 to turn flushing off */
  717. void PNGAPI
  718. png_set_flush(png_structp png_ptr, int nrows)
  719. {
  720. png_debug(1, "in png_set_flush");
  721. if (png_ptr == NULL)
  722. return;
  723. png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
  724. }
  725. /* Flush the current output buffers now */
  726. void PNGAPI
  727. png_write_flush(png_structp png_ptr)
  728. {
  729. int wrote_IDAT;
  730. png_debug(1, "in png_write_flush");
  731. if (png_ptr == NULL)
  732. return;
  733. /* We have already written out all of the data */
  734. if (png_ptr->row_number >= png_ptr->num_rows)
  735. return;
  736. do
  737. {
  738. int ret;
  739. /* Compress the data */
  740. ret = deflate(&png_ptr->zstream, Z_SYNC_FLUSH);
  741. wrote_IDAT = 0;
  742. /* Check for compression errors */
  743. if (ret != Z_OK)
  744. {
  745. if (png_ptr->zstream.msg != NULL)
  746. png_error(png_ptr, png_ptr->zstream.msg);
  747. else
  748. png_error(png_ptr, "zlib error");
  749. }
  750. if (!(png_ptr->zstream.avail_out))
  751. {
  752. /* Write the IDAT and reset the zlib output buffer */
  753. png_write_IDAT(png_ptr, png_ptr->zbuf, png_ptr->zbuf_size);
  754. wrote_IDAT = 1;
  755. }
  756. } while (wrote_IDAT == 1);
  757. /* If there is any data left to be output, write it into a new IDAT */
  758. if (png_ptr->zbuf_size != png_ptr->zstream.avail_out)
  759. {
  760. /* Write the IDAT and reset the zlib output buffer */
  761. png_write_IDAT(png_ptr, png_ptr->zbuf,
  762. png_ptr->zbuf_size - png_ptr->zstream.avail_out);
  763. }
  764. png_ptr->flush_rows = 0;
  765. png_flush(png_ptr);
  766. }
  767. #endif /* PNG_WRITE_FLUSH_SUPPORTED */
  768. /* Free all memory used by the write */
  769. void PNGAPI
  770. png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
  771. {
  772. png_structp png_ptr = NULL;
  773. png_infop info_ptr = NULL;
  774. #ifdef PNG_USER_MEM_SUPPORTED
  775. png_free_ptr free_fn = NULL;
  776. png_voidp mem_ptr = NULL;
  777. #endif
  778. png_debug(1, "in png_destroy_write_struct");
  779. if (png_ptr_ptr != NULL)
  780. png_ptr = *png_ptr_ptr;
  781. #ifdef PNG_USER_MEM_SUPPORTED
  782. if (png_ptr != NULL)
  783. {
  784. free_fn = png_ptr->free_fn;
  785. mem_ptr = png_ptr->mem_ptr;
  786. }
  787. #endif
  788. if (info_ptr_ptr != NULL)
  789. info_ptr = *info_ptr_ptr;
  790. if (info_ptr != NULL)
  791. {
  792. if (png_ptr != NULL)
  793. {
  794. png_free_data(png_ptr, info_ptr, PNG_FREE_ALL, -1);
  795. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  796. if (png_ptr->num_chunk_list)
  797. {
  798. png_free(png_ptr, png_ptr->chunk_list);
  799. png_ptr->num_chunk_list = 0;
  800. }
  801. #endif
  802. }
  803. #ifdef PNG_USER_MEM_SUPPORTED
  804. png_destroy_struct_2((png_voidp)info_ptr, (png_free_ptr)free_fn,
  805. (png_voidp)mem_ptr);
  806. #else
  807. png_destroy_struct((png_voidp)info_ptr);
  808. #endif
  809. *info_ptr_ptr = NULL;
  810. }
  811. if (png_ptr != NULL)
  812. {
  813. png_write_destroy(png_ptr);
  814. #ifdef PNG_USER_MEM_SUPPORTED
  815. png_destroy_struct_2((png_voidp)png_ptr, (png_free_ptr)free_fn,
  816. (png_voidp)mem_ptr);
  817. #else
  818. png_destroy_struct((png_voidp)png_ptr);
  819. #endif
  820. *png_ptr_ptr = NULL;
  821. }
  822. }
  823. /* Free any memory used in png_ptr struct (old method) */
  824. void /* PRIVATE */
  825. png_write_destroy(png_structp png_ptr)
  826. {
  827. #ifdef PNG_SETJMP_SUPPORTED
  828. jmp_buf tmp_jmp; /* Save jump buffer */
  829. #endif
  830. png_error_ptr error_fn;
  831. #ifdef PNG_WARNINGS_SUPPORTED
  832. png_error_ptr warning_fn;
  833. #endif
  834. png_voidp error_ptr;
  835. #ifdef PNG_USER_MEM_SUPPORTED
  836. png_free_ptr free_fn;
  837. #endif
  838. png_debug(1, "in png_write_destroy");
  839. /* Free any memory zlib uses */
  840. if (png_ptr->zlib_state != PNG_ZLIB_UNINITIALIZED)
  841. deflateEnd(&png_ptr->zstream);
  842. /* Free our memory. png_free checks NULL for us. */
  843. png_free(png_ptr, png_ptr->zbuf);
  844. png_free(png_ptr, png_ptr->row_buf);
  845. #ifdef PNG_WRITE_FILTER_SUPPORTED
  846. png_free(png_ptr, png_ptr->prev_row);
  847. png_free(png_ptr, png_ptr->sub_row);
  848. png_free(png_ptr, png_ptr->up_row);
  849. png_free(png_ptr, png_ptr->avg_row);
  850. png_free(png_ptr, png_ptr->paeth_row);
  851. #endif
  852. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
  853. /* Use this to save a little code space, it doesn't free the filter_costs */
  854. png_reset_filter_heuristics(png_ptr);
  855. png_free(png_ptr, png_ptr->filter_costs);
  856. png_free(png_ptr, png_ptr->inv_filter_costs);
  857. #endif
  858. #ifdef PNG_SETJMP_SUPPORTED
  859. /* Reset structure */
  860. png_memcpy(tmp_jmp, png_ptr->longjmp_buffer, png_sizeof(jmp_buf));
  861. #endif
  862. error_fn = png_ptr->error_fn;
  863. #ifdef PNG_WARNINGS_SUPPORTED
  864. warning_fn = png_ptr->warning_fn;
  865. #endif
  866. error_ptr = png_ptr->error_ptr;
  867. #ifdef PNG_USER_MEM_SUPPORTED
  868. free_fn = png_ptr->free_fn;
  869. #endif
  870. png_memset(png_ptr, 0, png_sizeof(png_struct));
  871. png_ptr->error_fn = error_fn;
  872. #ifdef PNG_WARNINGS_SUPPORTED
  873. png_ptr->warning_fn = warning_fn;
  874. #endif
  875. png_ptr->error_ptr = error_ptr;
  876. #ifdef PNG_USER_MEM_SUPPORTED
  877. png_ptr->free_fn = free_fn;
  878. #endif
  879. #ifdef PNG_SETJMP_SUPPORTED
  880. png_memcpy(png_ptr->longjmp_buffer, tmp_jmp, png_sizeof(jmp_buf));
  881. #endif
  882. }
  883. /* Allow the application to select one or more row filters to use. */
  884. void PNGAPI
  885. png_set_filter(png_structp png_ptr, int method, int filters)
  886. {
  887. png_debug(1, "in png_set_filter");
  888. if (png_ptr == NULL)
  889. return;
  890. #ifdef PNG_MNG_FEATURES_SUPPORTED
  891. if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) &&
  892. (method == PNG_INTRAPIXEL_DIFFERENCING))
  893. method = PNG_FILTER_TYPE_BASE;
  894. #endif
  895. if (method == PNG_FILTER_TYPE_BASE)
  896. {
  897. switch (filters & (PNG_ALL_FILTERS | 0x07))
  898. {
  899. #ifdef PNG_WRITE_FILTER_SUPPORTED
  900. case 5:
  901. case 6:
  902. case 7: png_warning(png_ptr, "Unknown row filter for method 0");
  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 */