pngwrite.c 49 KB

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