2
0

pngwrite.c 44 KB

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