pngwrite.c 48 KB

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