pngwrite.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406
  1. /* pngwrite.c - general routines to write a PNG file
  2. *
  3. * Copyright (c) 2018-2022 Cosmin Truta
  4. * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
  5. * Copyright (c) 1996-1997 Andreas Dilger
  6. * 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_SIMPLIFIED_WRITE_STDIO_SUPPORTED
  14. # include <errno.h>
  15. #endif /* SIMPLIFIED_WRITE_STDIO */
  16. #ifdef PNG_WRITE_SUPPORTED
  17. #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
  18. /* Write out all the unknown chunks for the current given location */
  19. static void
  20. write_unknown_chunks(png_structrp png_ptr, png_const_inforp info_ptr,
  21. unsigned int where)
  22. {
  23. if (info_ptr->unknown_chunks_num != 0)
  24. {
  25. png_const_unknown_chunkp up;
  26. png_debug(5, "writing extra chunks");
  27. for (up = info_ptr->unknown_chunks;
  28. up < info_ptr->unknown_chunks + info_ptr->unknown_chunks_num;
  29. ++up)
  30. if ((up->location & where) != 0)
  31. {
  32. /* If per-chunk unknown chunk handling is enabled use it, otherwise
  33. * just write the chunks the application has set.
  34. */
  35. #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
  36. int keep = png_handle_as_unknown(png_ptr, up->name);
  37. /* NOTE: this code is radically different from the read side in the
  38. * matter of handling an ancillary unknown chunk. In the read side
  39. * the default behavior is to discard it, in the code below the default
  40. * behavior is to write it. Critical chunks are, however, only
  41. * written if explicitly listed or if the default is set to write all
  42. * unknown chunks.
  43. *
  44. * The default handling is also slightly weird - it is not possible to
  45. * stop the writing of all unsafe-to-copy chunks!
  46. *
  47. * TODO: REVIEW: this would seem to be a bug.
  48. */
  49. if (keep != PNG_HANDLE_CHUNK_NEVER &&
  50. ((up->name[3] & 0x20) /* safe-to-copy overrides everything */ ||
  51. keep == PNG_HANDLE_CHUNK_ALWAYS ||
  52. (keep == PNG_HANDLE_CHUNK_AS_DEFAULT &&
  53. png_ptr->unknown_default == PNG_HANDLE_CHUNK_ALWAYS)))
  54. #endif
  55. {
  56. /* TODO: review, what is wrong with a zero length unknown chunk? */
  57. if (up->size == 0)
  58. png_warning(png_ptr, "Writing zero-length unknown chunk");
  59. png_write_chunk(png_ptr, up->name, up->data, up->size);
  60. }
  61. }
  62. }
  63. }
  64. #endif /* WRITE_UNKNOWN_CHUNKS */
  65. /* Writes all the PNG information. This is the suggested way to use the
  66. * library. If you have a new chunk to add, make a function to write it,
  67. * and put it in the correct location here. If you want the chunk written
  68. * after the image data, put it in png_write_end(). I strongly encourage
  69. * you to supply a PNG_INFO_<chunk> flag, and check info_ptr->valid before
  70. * writing the chunk, as that will keep the code from breaking if you want
  71. * to just write a plain PNG file. If you have long comments, I suggest
  72. * writing them in png_write_end(), and compressing them.
  73. */
  74. void PNGAPI
  75. png_write_info_before_PLTE(png_structrp png_ptr, png_const_inforp info_ptr)
  76. {
  77. png_debug(1, "in png_write_info_before_PLTE");
  78. if (png_ptr == NULL || info_ptr == NULL)
  79. return;
  80. if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0)
  81. {
  82. /* Write PNG signature */
  83. png_write_sig(png_ptr);
  84. #ifdef PNG_MNG_FEATURES_SUPPORTED
  85. if ((png_ptr->mode & PNG_HAVE_PNG_SIGNATURE) != 0 && \
  86. png_ptr->mng_features_permitted != 0)
  87. {
  88. png_warning(png_ptr,
  89. "MNG features are not allowed in a PNG datastream");
  90. png_ptr->mng_features_permitted = 0;
  91. }
  92. #endif
  93. /* Write IHDR information. */
  94. png_write_IHDR(png_ptr, info_ptr->width, info_ptr->height,
  95. info_ptr->bit_depth, info_ptr->color_type, info_ptr->compression_type,
  96. info_ptr->filter_type,
  97. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  98. info_ptr->interlace_type
  99. #else
  100. 0
  101. #endif
  102. );
  103. /* The rest of these check to see if the valid field has the appropriate
  104. * flag set, and if it does, writes the chunk.
  105. *
  106. * 1.6.0: COLORSPACE support controls the writing of these chunks too, and
  107. * the chunks will be written if the WRITE routine is there and
  108. * information * is available in the COLORSPACE. (See
  109. * png_colorspace_sync_info in png.c for where the valid flags get set.)
  110. *
  111. * Under certain circumstances the colorspace can be invalidated without
  112. * syncing the info_struct 'valid' flags; this happens if libpng detects
  113. * an error and calls png_error while the color space is being set, yet
  114. * the application continues writing the PNG. So check the 'invalid'
  115. * flag here too.
  116. */
  117. #ifdef PNG_GAMMA_SUPPORTED
  118. # ifdef PNG_WRITE_gAMA_SUPPORTED
  119. if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
  120. (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_gAMA) != 0 &&
  121. (info_ptr->valid & PNG_INFO_gAMA) != 0)
  122. png_write_gAMA_fixed(png_ptr, info_ptr->colorspace.gamma);
  123. # endif
  124. #endif
  125. #ifdef PNG_COLORSPACE_SUPPORTED
  126. /* Write only one of sRGB or an ICC profile. If a profile was supplied
  127. * and it matches one of the known sRGB ones issue a warning.
  128. */
  129. # ifdef PNG_WRITE_iCCP_SUPPORTED
  130. if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
  131. (info_ptr->valid & PNG_INFO_iCCP) != 0)
  132. {
  133. # ifdef PNG_WRITE_sRGB_SUPPORTED
  134. if ((info_ptr->valid & PNG_INFO_sRGB) != 0)
  135. png_app_warning(png_ptr,
  136. "profile matches sRGB but writing iCCP instead");
  137. # endif
  138. png_write_iCCP(png_ptr, info_ptr->iccp_name,
  139. info_ptr->iccp_profile);
  140. }
  141. # ifdef PNG_WRITE_sRGB_SUPPORTED
  142. else
  143. # endif
  144. # endif
  145. # ifdef PNG_WRITE_sRGB_SUPPORTED
  146. if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
  147. (info_ptr->valid & PNG_INFO_sRGB) != 0)
  148. png_write_sRGB(png_ptr, info_ptr->colorspace.rendering_intent);
  149. # endif /* WRITE_sRGB */
  150. #endif /* COLORSPACE */
  151. #ifdef PNG_WRITE_sBIT_SUPPORTED
  152. if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
  153. png_write_sBIT(png_ptr, &(info_ptr->sig_bit), info_ptr->color_type);
  154. #endif
  155. #ifdef PNG_COLORSPACE_SUPPORTED
  156. # ifdef PNG_WRITE_cHRM_SUPPORTED
  157. if ((info_ptr->colorspace.flags & PNG_COLORSPACE_INVALID) == 0 &&
  158. (info_ptr->colorspace.flags & PNG_COLORSPACE_FROM_cHRM) != 0 &&
  159. (info_ptr->valid & PNG_INFO_cHRM) != 0)
  160. png_write_cHRM_fixed(png_ptr, &info_ptr->colorspace.end_points_xy);
  161. # endif
  162. #endif
  163. #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
  164. write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_IHDR);
  165. #endif
  166. png_ptr->mode |= PNG_WROTE_INFO_BEFORE_PLTE;
  167. }
  168. }
  169. void PNGAPI
  170. png_write_info(png_structrp png_ptr, png_const_inforp info_ptr)
  171. {
  172. #if defined(PNG_WRITE_TEXT_SUPPORTED) || defined(PNG_WRITE_sPLT_SUPPORTED)
  173. int i;
  174. #endif
  175. png_debug(1, "in png_write_info");
  176. if (png_ptr == NULL || info_ptr == NULL)
  177. return;
  178. png_write_info_before_PLTE(png_ptr, info_ptr);
  179. if ((info_ptr->valid & PNG_INFO_PLTE) != 0)
  180. png_write_PLTE(png_ptr, info_ptr->palette,
  181. (png_uint_32)info_ptr->num_palette);
  182. else if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  183. png_error(png_ptr, "Valid palette required for paletted images");
  184. #ifdef PNG_WRITE_tRNS_SUPPORTED
  185. if ((info_ptr->valid & PNG_INFO_tRNS) !=0)
  186. {
  187. #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
  188. /* Invert the alpha channel (in tRNS) */
  189. if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0 &&
  190. info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  191. {
  192. int j, jend;
  193. jend = info_ptr->num_trans;
  194. if (jend > PNG_MAX_PALETTE_LENGTH)
  195. jend = PNG_MAX_PALETTE_LENGTH;
  196. for (j = 0; j<jend; ++j)
  197. info_ptr->trans_alpha[j] =
  198. (png_byte)(255 - info_ptr->trans_alpha[j]);
  199. }
  200. #endif
  201. png_write_tRNS(png_ptr, info_ptr->trans_alpha, &(info_ptr->trans_color),
  202. info_ptr->num_trans, info_ptr->color_type);
  203. }
  204. #endif
  205. #ifdef PNG_WRITE_bKGD_SUPPORTED
  206. if ((info_ptr->valid & PNG_INFO_bKGD) != 0)
  207. png_write_bKGD(png_ptr, &(info_ptr->background), info_ptr->color_type);
  208. #endif
  209. #ifdef PNG_WRITE_eXIf_SUPPORTED
  210. if ((info_ptr->valid & PNG_INFO_eXIf) != 0)
  211. png_write_eXIf(png_ptr, info_ptr->exif, info_ptr->num_exif);
  212. #endif
  213. #ifdef PNG_WRITE_hIST_SUPPORTED
  214. if ((info_ptr->valid & PNG_INFO_hIST) != 0)
  215. png_write_hIST(png_ptr, info_ptr->hist, info_ptr->num_palette);
  216. #endif
  217. #ifdef PNG_WRITE_oFFs_SUPPORTED
  218. if ((info_ptr->valid & PNG_INFO_oFFs) != 0)
  219. png_write_oFFs(png_ptr, info_ptr->x_offset, info_ptr->y_offset,
  220. info_ptr->offset_unit_type);
  221. #endif
  222. #ifdef PNG_WRITE_pCAL_SUPPORTED
  223. if ((info_ptr->valid & PNG_INFO_pCAL) != 0)
  224. png_write_pCAL(png_ptr, info_ptr->pcal_purpose, info_ptr->pcal_X0,
  225. info_ptr->pcal_X1, info_ptr->pcal_type, info_ptr->pcal_nparams,
  226. info_ptr->pcal_units, info_ptr->pcal_params);
  227. #endif
  228. #ifdef PNG_WRITE_sCAL_SUPPORTED
  229. if ((info_ptr->valid & PNG_INFO_sCAL) != 0)
  230. png_write_sCAL_s(png_ptr, (int)info_ptr->scal_unit,
  231. info_ptr->scal_s_width, info_ptr->scal_s_height);
  232. #endif /* sCAL */
  233. #ifdef PNG_WRITE_pHYs_SUPPORTED
  234. if ((info_ptr->valid & PNG_INFO_pHYs) != 0)
  235. png_write_pHYs(png_ptr, info_ptr->x_pixels_per_unit,
  236. info_ptr->y_pixels_per_unit, info_ptr->phys_unit_type);
  237. #endif /* pHYs */
  238. #ifdef PNG_WRITE_tIME_SUPPORTED
  239. if ((info_ptr->valid & PNG_INFO_tIME) != 0)
  240. {
  241. png_write_tIME(png_ptr, &(info_ptr->mod_time));
  242. png_ptr->mode |= PNG_WROTE_tIME;
  243. }
  244. #endif /* tIME */
  245. #ifdef PNG_WRITE_sPLT_SUPPORTED
  246. if ((info_ptr->valid & PNG_INFO_sPLT) != 0)
  247. for (i = 0; i < (int)info_ptr->splt_palettes_num; i++)
  248. png_write_sPLT(png_ptr, info_ptr->splt_palettes + i);
  249. #endif /* sPLT */
  250. #ifdef PNG_WRITE_TEXT_SUPPORTED
  251. /* Check to see if we need to write text chunks */
  252. for (i = 0; i < info_ptr->num_text; i++)
  253. {
  254. png_debug2(2, "Writing header text chunk %d, type %d", i,
  255. info_ptr->text[i].compression);
  256. /* An internationalized chunk? */
  257. if (info_ptr->text[i].compression > 0)
  258. {
  259. #ifdef PNG_WRITE_iTXt_SUPPORTED
  260. /* Write international chunk */
  261. png_write_iTXt(png_ptr,
  262. info_ptr->text[i].compression,
  263. info_ptr->text[i].key,
  264. info_ptr->text[i].lang,
  265. info_ptr->text[i].lang_key,
  266. info_ptr->text[i].text);
  267. /* Mark this chunk as written */
  268. if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
  269. info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  270. else
  271. info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
  272. #else
  273. png_warning(png_ptr, "Unable to write international text");
  274. #endif
  275. }
  276. /* If we want a compressed text chunk */
  277. else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_zTXt)
  278. {
  279. #ifdef PNG_WRITE_zTXt_SUPPORTED
  280. /* Write compressed chunk */
  281. png_write_zTXt(png_ptr, info_ptr->text[i].key,
  282. info_ptr->text[i].text, info_ptr->text[i].compression);
  283. /* Mark this chunk as written */
  284. info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
  285. #else
  286. png_warning(png_ptr, "Unable to write compressed text");
  287. #endif
  288. }
  289. else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
  290. {
  291. #ifdef PNG_WRITE_tEXt_SUPPORTED
  292. /* Write uncompressed chunk */
  293. png_write_tEXt(png_ptr, info_ptr->text[i].key,
  294. info_ptr->text[i].text,
  295. 0);
  296. /* Mark this chunk as written */
  297. info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  298. #else
  299. /* Can't get here */
  300. png_warning(png_ptr, "Unable to write uncompressed text");
  301. #endif
  302. }
  303. }
  304. #endif /* tEXt */
  305. #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
  306. write_unknown_chunks(png_ptr, info_ptr, PNG_HAVE_PLTE);
  307. #endif
  308. }
  309. /* Writes the end of the PNG file. If you don't want to write comments or
  310. * time information, you can pass NULL for info. If you already wrote these
  311. * in png_write_info(), do not write them again here. If you have long
  312. * comments, I suggest writing them here, and compressing them.
  313. */
  314. void PNGAPI
  315. png_write_end(png_structrp png_ptr, png_inforp info_ptr)
  316. {
  317. png_debug(1, "in png_write_end");
  318. if (png_ptr == NULL)
  319. return;
  320. if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
  321. png_error(png_ptr, "No IDATs written into file");
  322. #ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
  323. if (png_ptr->num_palette_max > png_ptr->num_palette)
  324. png_benign_error(png_ptr, "Wrote palette index exceeding num_palette");
  325. #endif
  326. /* See if user wants us to write information chunks */
  327. if (info_ptr != NULL)
  328. {
  329. #ifdef PNG_WRITE_TEXT_SUPPORTED
  330. int i; /* local index variable */
  331. #endif
  332. #ifdef PNG_WRITE_tIME_SUPPORTED
  333. /* Check to see if user has supplied a time chunk */
  334. if ((info_ptr->valid & PNG_INFO_tIME) != 0 &&
  335. (png_ptr->mode & PNG_WROTE_tIME) == 0)
  336. png_write_tIME(png_ptr, &(info_ptr->mod_time));
  337. #endif
  338. #ifdef PNG_WRITE_TEXT_SUPPORTED
  339. /* Loop through comment chunks */
  340. for (i = 0; i < info_ptr->num_text; i++)
  341. {
  342. png_debug2(2, "Writing trailer text chunk %d, type %d", i,
  343. info_ptr->text[i].compression);
  344. /* An internationalized chunk? */
  345. if (info_ptr->text[i].compression > 0)
  346. {
  347. #ifdef PNG_WRITE_iTXt_SUPPORTED
  348. /* Write international chunk */
  349. png_write_iTXt(png_ptr,
  350. info_ptr->text[i].compression,
  351. info_ptr->text[i].key,
  352. info_ptr->text[i].lang,
  353. info_ptr->text[i].lang_key,
  354. info_ptr->text[i].text);
  355. /* Mark this chunk as written */
  356. if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
  357. info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  358. else
  359. info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
  360. #else
  361. png_warning(png_ptr, "Unable to write international text");
  362. #endif
  363. }
  364. else if (info_ptr->text[i].compression >= PNG_TEXT_COMPRESSION_zTXt)
  365. {
  366. #ifdef PNG_WRITE_zTXt_SUPPORTED
  367. /* Write compressed chunk */
  368. png_write_zTXt(png_ptr, info_ptr->text[i].key,
  369. info_ptr->text[i].text, info_ptr->text[i].compression);
  370. /* Mark this chunk as written */
  371. info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_zTXt_WR;
  372. #else
  373. png_warning(png_ptr, "Unable to write compressed text");
  374. #endif
  375. }
  376. else if (info_ptr->text[i].compression == PNG_TEXT_COMPRESSION_NONE)
  377. {
  378. #ifdef PNG_WRITE_tEXt_SUPPORTED
  379. /* Write uncompressed chunk */
  380. png_write_tEXt(png_ptr, info_ptr->text[i].key,
  381. info_ptr->text[i].text, 0);
  382. /* Mark this chunk as written */
  383. info_ptr->text[i].compression = PNG_TEXT_COMPRESSION_NONE_WR;
  384. #else
  385. png_warning(png_ptr, "Unable to write uncompressed text");
  386. #endif
  387. }
  388. }
  389. #endif
  390. #ifdef PNG_WRITE_eXIf_SUPPORTED
  391. if ((info_ptr->valid & PNG_INFO_eXIf) != 0)
  392. png_write_eXIf(png_ptr, info_ptr->exif, info_ptr->num_exif);
  393. #endif
  394. #ifdef PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
  395. write_unknown_chunks(png_ptr, info_ptr, PNG_AFTER_IDAT);
  396. #endif
  397. }
  398. png_ptr->mode |= PNG_AFTER_IDAT;
  399. /* Write end of PNG file */
  400. png_write_IEND(png_ptr);
  401. /* This flush, added in libpng-1.0.8, removed from libpng-1.0.9beta03,
  402. * and restored again in libpng-1.2.30, may cause some applications that
  403. * do not set png_ptr->output_flush_fn to crash. If your application
  404. * experiences a problem, please try building libpng with
  405. * PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED defined, and report the event to
  406. * png-mng-implement at lists.sf.net .
  407. */
  408. #ifdef PNG_WRITE_FLUSH_SUPPORTED
  409. # ifdef PNG_WRITE_FLUSH_AFTER_IEND_SUPPORTED
  410. png_flush(png_ptr);
  411. # endif
  412. #endif
  413. }
  414. #ifdef PNG_CONVERT_tIME_SUPPORTED
  415. void PNGAPI
  416. png_convert_from_struct_tm(png_timep ptime, const struct tm * ttime)
  417. {
  418. png_debug(1, "in png_convert_from_struct_tm");
  419. ptime->year = (png_uint_16)(1900 + ttime->tm_year);
  420. ptime->month = (png_byte)(ttime->tm_mon + 1);
  421. ptime->day = (png_byte)ttime->tm_mday;
  422. ptime->hour = (png_byte)ttime->tm_hour;
  423. ptime->minute = (png_byte)ttime->tm_min;
  424. ptime->second = (png_byte)ttime->tm_sec;
  425. }
  426. void PNGAPI
  427. png_convert_from_time_t(png_timep ptime, time_t ttime)
  428. {
  429. struct tm *tbuf;
  430. png_debug(1, "in png_convert_from_time_t");
  431. tbuf = gmtime(&ttime);
  432. if (tbuf == NULL)
  433. {
  434. /* TODO: add a safe function which takes a png_ptr argument and raises
  435. * a png_error if the ttime argument is invalid and the call to gmtime
  436. * fails as a consequence.
  437. */
  438. memset(ptime, 0, sizeof(*ptime));
  439. return;
  440. }
  441. png_convert_from_struct_tm(ptime, tbuf);
  442. }
  443. #endif
  444. /* Initialize png_ptr structure, and allocate any memory needed */
  445. PNG_FUNCTION(png_structp,PNGAPI
  446. png_create_write_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
  447. png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
  448. {
  449. #ifndef PNG_USER_MEM_SUPPORTED
  450. png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
  451. error_fn, warn_fn, NULL, NULL, NULL);
  452. #else
  453. return png_create_write_struct_2(user_png_ver, error_ptr, error_fn,
  454. warn_fn, NULL, NULL, NULL);
  455. }
  456. /* Alternate initialize png_ptr structure, and allocate any memory needed */
  457. PNG_FUNCTION(png_structp,PNGAPI
  458. png_create_write_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
  459. png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
  460. png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
  461. {
  462. png_structrp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
  463. error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
  464. #endif /* USER_MEM */
  465. if (png_ptr != NULL)
  466. {
  467. /* Set the zlib control values to defaults; they can be overridden by the
  468. * application after the struct has been created.
  469. */
  470. png_ptr->zbuffer_size = PNG_ZBUF_SIZE;
  471. /* The 'zlib_strategy' setting is irrelevant because png_default_claim in
  472. * pngwutil.c defaults it according to whether or not filters will be
  473. * used, and ignores this setting.
  474. */
  475. png_ptr->zlib_strategy = PNG_Z_DEFAULT_STRATEGY;
  476. png_ptr->zlib_level = PNG_Z_DEFAULT_COMPRESSION;
  477. png_ptr->zlib_mem_level = 8;
  478. png_ptr->zlib_window_bits = 15;
  479. png_ptr->zlib_method = 8;
  480. #ifdef PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
  481. png_ptr->zlib_text_strategy = PNG_TEXT_Z_DEFAULT_STRATEGY;
  482. png_ptr->zlib_text_level = PNG_TEXT_Z_DEFAULT_COMPRESSION;
  483. png_ptr->zlib_text_mem_level = 8;
  484. png_ptr->zlib_text_window_bits = 15;
  485. png_ptr->zlib_text_method = 8;
  486. #endif /* WRITE_COMPRESSED_TEXT */
  487. /* This is a highly dubious configuration option; by default it is off,
  488. * but it may be appropriate for private builds that are testing
  489. * extensions not conformant to the current specification, or of
  490. * applications that must not fail to write at all costs!
  491. */
  492. #ifdef PNG_BENIGN_WRITE_ERRORS_SUPPORTED
  493. /* In stable builds only warn if an application error can be completely
  494. * handled.
  495. */
  496. png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
  497. #endif
  498. /* App warnings are warnings in release (or release candidate) builds but
  499. * are errors during development.
  500. */
  501. #if PNG_RELEASE_BUILD
  502. png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
  503. #endif
  504. /* TODO: delay this, it can be done in png_init_io() (if the app doesn't
  505. * do it itself) avoiding setting the default function if it is not
  506. * required.
  507. */
  508. png_set_write_fn(png_ptr, NULL, NULL, NULL);
  509. }
  510. return png_ptr;
  511. }
  512. /* Write a few rows of image data. If the image is interlaced,
  513. * either you will have to write the 7 sub images, or, if you
  514. * have called png_set_interlace_handling(), you will have to
  515. * "write" the image seven times.
  516. */
  517. void PNGAPI
  518. png_write_rows(png_structrp png_ptr, png_bytepp row,
  519. png_uint_32 num_rows)
  520. {
  521. png_uint_32 i; /* row counter */
  522. png_bytepp rp; /* row pointer */
  523. png_debug(1, "in png_write_rows");
  524. if (png_ptr == NULL)
  525. return;
  526. /* Loop through the rows */
  527. for (i = 0, rp = row; i < num_rows; i++, rp++)
  528. {
  529. png_write_row(png_ptr, *rp);
  530. }
  531. }
  532. /* Write the image. You only need to call this function once, even
  533. * if you are writing an interlaced image.
  534. */
  535. void PNGAPI
  536. png_write_image(png_structrp png_ptr, png_bytepp image)
  537. {
  538. png_uint_32 i; /* row index */
  539. int pass, num_pass; /* pass variables */
  540. png_bytepp rp; /* points to current row */
  541. if (png_ptr == NULL)
  542. return;
  543. png_debug(1, "in png_write_image");
  544. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  545. /* Initialize interlace handling. If image is not interlaced,
  546. * this will set pass to 1
  547. */
  548. num_pass = png_set_interlace_handling(png_ptr);
  549. #else
  550. num_pass = 1;
  551. #endif
  552. /* Loop through passes */
  553. for (pass = 0; pass < num_pass; pass++)
  554. {
  555. /* Loop through image */
  556. for (i = 0, rp = image; i < png_ptr->height; i++, rp++)
  557. {
  558. png_write_row(png_ptr, *rp);
  559. }
  560. }
  561. }
  562. #ifdef PNG_MNG_FEATURES_SUPPORTED
  563. /* Performs intrapixel differencing */
  564. static void
  565. png_do_write_intrapixel(png_row_infop row_info, png_bytep row)
  566. {
  567. png_debug(1, "in png_do_write_intrapixel");
  568. if ((row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
  569. {
  570. int bytes_per_pixel;
  571. png_uint_32 row_width = row_info->width;
  572. if (row_info->bit_depth == 8)
  573. {
  574. png_bytep rp;
  575. png_uint_32 i;
  576. if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  577. bytes_per_pixel = 3;
  578. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  579. bytes_per_pixel = 4;
  580. else
  581. return;
  582. for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
  583. {
  584. *(rp) = (png_byte)(*rp - *(rp + 1));
  585. *(rp + 2) = (png_byte)(*(rp + 2) - *(rp + 1));
  586. }
  587. }
  588. #ifdef PNG_WRITE_16BIT_SUPPORTED
  589. else if (row_info->bit_depth == 16)
  590. {
  591. png_bytep rp;
  592. png_uint_32 i;
  593. if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  594. bytes_per_pixel = 6;
  595. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  596. bytes_per_pixel = 8;
  597. else
  598. return;
  599. for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
  600. {
  601. png_uint_32 s0 = (png_uint_32)(*(rp ) << 8) | *(rp + 1);
  602. png_uint_32 s1 = (png_uint_32)(*(rp + 2) << 8) | *(rp + 3);
  603. png_uint_32 s2 = (png_uint_32)(*(rp + 4) << 8) | *(rp + 5);
  604. png_uint_32 red = (png_uint_32)((s0 - s1) & 0xffffL);
  605. png_uint_32 blue = (png_uint_32)((s2 - s1) & 0xffffL);
  606. *(rp ) = (png_byte)(red >> 8);
  607. *(rp + 1) = (png_byte)red;
  608. *(rp + 4) = (png_byte)(blue >> 8);
  609. *(rp + 5) = (png_byte)blue;
  610. }
  611. }
  612. #endif /* WRITE_16BIT */
  613. }
  614. }
  615. #endif /* MNG_FEATURES */
  616. /* Called by user to write a row of image data */
  617. void PNGAPI
  618. png_write_row(png_structrp png_ptr, png_const_bytep row)
  619. {
  620. /* 1.5.6: moved from png_struct to be a local structure: */
  621. png_row_info row_info;
  622. if (png_ptr == NULL)
  623. return;
  624. png_debug2(1, "in png_write_row (row %u, pass %d)",
  625. png_ptr->row_number, png_ptr->pass);
  626. /* Initialize transformations and other stuff if first time */
  627. if (png_ptr->row_number == 0 && png_ptr->pass == 0)
  628. {
  629. /* Make sure we wrote the header info */
  630. if ((png_ptr->mode & PNG_WROTE_INFO_BEFORE_PLTE) == 0)
  631. png_error(png_ptr,
  632. "png_write_info was never called before png_write_row");
  633. /* Check for transforms that have been set but were defined out */
  634. #if !defined(PNG_WRITE_INVERT_SUPPORTED) && defined(PNG_READ_INVERT_SUPPORTED)
  635. if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
  636. png_warning(png_ptr, "PNG_WRITE_INVERT_SUPPORTED is not defined");
  637. #endif
  638. #if !defined(PNG_WRITE_FILLER_SUPPORTED) && defined(PNG_READ_FILLER_SUPPORTED)
  639. if ((png_ptr->transformations & PNG_FILLER) != 0)
  640. png_warning(png_ptr, "PNG_WRITE_FILLER_SUPPORTED is not defined");
  641. #endif
  642. #if !defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
  643. defined(PNG_READ_PACKSWAP_SUPPORTED)
  644. if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
  645. png_warning(png_ptr,
  646. "PNG_WRITE_PACKSWAP_SUPPORTED is not defined");
  647. #endif
  648. #if !defined(PNG_WRITE_PACK_SUPPORTED) && defined(PNG_READ_PACK_SUPPORTED)
  649. if ((png_ptr->transformations & PNG_PACK) != 0)
  650. png_warning(png_ptr, "PNG_WRITE_PACK_SUPPORTED is not defined");
  651. #endif
  652. #if !defined(PNG_WRITE_SHIFT_SUPPORTED) && defined(PNG_READ_SHIFT_SUPPORTED)
  653. if ((png_ptr->transformations & PNG_SHIFT) != 0)
  654. png_warning(png_ptr, "PNG_WRITE_SHIFT_SUPPORTED is not defined");
  655. #endif
  656. #if !defined(PNG_WRITE_BGR_SUPPORTED) && defined(PNG_READ_BGR_SUPPORTED)
  657. if ((png_ptr->transformations & PNG_BGR) != 0)
  658. png_warning(png_ptr, "PNG_WRITE_BGR_SUPPORTED is not defined");
  659. #endif
  660. #if !defined(PNG_WRITE_SWAP_SUPPORTED) && defined(PNG_READ_SWAP_SUPPORTED)
  661. if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
  662. png_warning(png_ptr, "PNG_WRITE_SWAP_SUPPORTED is not defined");
  663. #endif
  664. png_write_start_row(png_ptr);
  665. }
  666. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  667. /* If interlaced and not interested in row, return */
  668. if (png_ptr->interlaced != 0 &&
  669. (png_ptr->transformations & PNG_INTERLACE) != 0)
  670. {
  671. switch (png_ptr->pass)
  672. {
  673. case 0:
  674. if ((png_ptr->row_number & 0x07) != 0)
  675. {
  676. png_write_finish_row(png_ptr);
  677. return;
  678. }
  679. break;
  680. case 1:
  681. if ((png_ptr->row_number & 0x07) != 0 || png_ptr->width < 5)
  682. {
  683. png_write_finish_row(png_ptr);
  684. return;
  685. }
  686. break;
  687. case 2:
  688. if ((png_ptr->row_number & 0x07) != 4)
  689. {
  690. png_write_finish_row(png_ptr);
  691. return;
  692. }
  693. break;
  694. case 3:
  695. if ((png_ptr->row_number & 0x03) != 0 || png_ptr->width < 3)
  696. {
  697. png_write_finish_row(png_ptr);
  698. return;
  699. }
  700. break;
  701. case 4:
  702. if ((png_ptr->row_number & 0x03) != 2)
  703. {
  704. png_write_finish_row(png_ptr);
  705. return;
  706. }
  707. break;
  708. case 5:
  709. if ((png_ptr->row_number & 0x01) != 0 || png_ptr->width < 2)
  710. {
  711. png_write_finish_row(png_ptr);
  712. return;
  713. }
  714. break;
  715. case 6:
  716. if ((png_ptr->row_number & 0x01) == 0)
  717. {
  718. png_write_finish_row(png_ptr);
  719. return;
  720. }
  721. break;
  722. default: /* error: ignore it */
  723. break;
  724. }
  725. }
  726. #endif
  727. /* Set up row info for transformations */
  728. row_info.color_type = png_ptr->color_type;
  729. row_info.width = png_ptr->usr_width;
  730. row_info.channels = png_ptr->usr_channels;
  731. row_info.bit_depth = png_ptr->usr_bit_depth;
  732. row_info.pixel_depth = (png_byte)(row_info.bit_depth * row_info.channels);
  733. row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
  734. png_debug1(3, "row_info->color_type = %d", row_info.color_type);
  735. png_debug1(3, "row_info->width = %u", row_info.width);
  736. png_debug1(3, "row_info->channels = %d", row_info.channels);
  737. png_debug1(3, "row_info->bit_depth = %d", row_info.bit_depth);
  738. png_debug1(3, "row_info->pixel_depth = %d", row_info.pixel_depth);
  739. png_debug1(3, "row_info->rowbytes = %lu", (unsigned long)row_info.rowbytes);
  740. /* Copy user's row into buffer, leaving room for filter byte. */
  741. memcpy(png_ptr->row_buf + 1, row, row_info.rowbytes);
  742. #ifdef PNG_WRITE_INTERLACING_SUPPORTED
  743. /* Handle interlacing */
  744. if (png_ptr->interlaced && png_ptr->pass < 6 &&
  745. (png_ptr->transformations & PNG_INTERLACE) != 0)
  746. {
  747. png_do_write_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass);
  748. /* This should always get caught above, but still ... */
  749. if (row_info.width == 0)
  750. {
  751. png_write_finish_row(png_ptr);
  752. return;
  753. }
  754. }
  755. #endif
  756. #ifdef PNG_WRITE_TRANSFORMS_SUPPORTED
  757. /* Handle other transformations */
  758. if (png_ptr->transformations != 0)
  759. png_do_write_transformations(png_ptr, &row_info);
  760. #endif
  761. /* At this point the row_info pixel depth must match the 'transformed' depth,
  762. * which is also the output depth.
  763. */
  764. if (row_info.pixel_depth != png_ptr->pixel_depth ||
  765. row_info.pixel_depth != png_ptr->transformed_pixel_depth)
  766. png_error(png_ptr, "internal write transform logic error");
  767. #ifdef PNG_MNG_FEATURES_SUPPORTED
  768. /* Write filter_method 64 (intrapixel differencing) only if
  769. * 1. Libpng was compiled with PNG_MNG_FEATURES_SUPPORTED and
  770. * 2. Libpng did not write a PNG signature (this filter_method is only
  771. * used in PNG datastreams that are embedded in MNG datastreams) and
  772. * 3. The application called png_permit_mng_features with a mask that
  773. * included PNG_FLAG_MNG_FILTER_64 and
  774. * 4. The filter_method is 64 and
  775. * 5. The color_type is RGB or RGBA
  776. */
  777. if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
  778. (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
  779. {
  780. /* Intrapixel differencing */
  781. png_do_write_intrapixel(&row_info, png_ptr->row_buf + 1);
  782. }
  783. #endif
  784. /* Added at libpng-1.5.10 */
  785. #ifdef PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
  786. /* Check for out-of-range palette index */
  787. if (row_info.color_type == PNG_COLOR_TYPE_PALETTE &&
  788. png_ptr->num_palette_max >= 0)
  789. png_do_check_palette_indexes(png_ptr, &row_info);
  790. #endif
  791. /* Find a filter if necessary, filter the row and write it out. */
  792. png_write_find_filter(png_ptr, &row_info);
  793. if (png_ptr->write_row_fn != NULL)
  794. (*(png_ptr->write_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
  795. }
  796. #ifdef PNG_WRITE_FLUSH_SUPPORTED
  797. /* Set the automatic flush interval or 0 to turn flushing off */
  798. void PNGAPI
  799. png_set_flush(png_structrp png_ptr, int nrows)
  800. {
  801. png_debug(1, "in png_set_flush");
  802. if (png_ptr == NULL)
  803. return;
  804. png_ptr->flush_dist = (nrows < 0 ? 0 : (png_uint_32)nrows);
  805. }
  806. /* Flush the current output buffers now */
  807. void PNGAPI
  808. png_write_flush(png_structrp png_ptr)
  809. {
  810. png_debug(1, "in png_write_flush");
  811. if (png_ptr == NULL)
  812. return;
  813. /* We have already written out all of the data */
  814. if (png_ptr->row_number >= png_ptr->num_rows)
  815. return;
  816. png_compress_IDAT(png_ptr, NULL, 0, Z_SYNC_FLUSH);
  817. png_ptr->flush_rows = 0;
  818. png_flush(png_ptr);
  819. }
  820. #endif /* WRITE_FLUSH */
  821. /* Free any memory used in png_ptr struct without freeing the struct itself. */
  822. static void
  823. png_write_destroy(png_structrp png_ptr)
  824. {
  825. png_debug(1, "in png_write_destroy");
  826. /* Free any memory zlib uses */
  827. if ((png_ptr->flags & PNG_FLAG_ZSTREAM_INITIALIZED) != 0)
  828. deflateEnd(&png_ptr->zstream);
  829. /* Free our memory. png_free checks NULL for us. */
  830. png_free_buffer_list(png_ptr, &png_ptr->zbuffer_list);
  831. png_free(png_ptr, png_ptr->row_buf);
  832. png_ptr->row_buf = NULL;
  833. #ifdef PNG_WRITE_FILTER_SUPPORTED
  834. png_free(png_ptr, png_ptr->prev_row);
  835. png_free(png_ptr, png_ptr->try_row);
  836. png_free(png_ptr, png_ptr->tst_row);
  837. png_ptr->prev_row = NULL;
  838. png_ptr->try_row = NULL;
  839. png_ptr->tst_row = NULL;
  840. #endif
  841. #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
  842. png_free(png_ptr, png_ptr->chunk_list);
  843. png_ptr->chunk_list = NULL;
  844. #endif
  845. /* The error handling and memory handling information is left intact at this
  846. * point: the jmp_buf may still have to be freed. See png_destroy_png_struct
  847. * for how this happens.
  848. */
  849. }
  850. /* Free all memory used by the write.
  851. * In libpng 1.6.0 this API changed quietly to no longer accept a NULL value for
  852. * *png_ptr_ptr. Prior to 1.6.0 it would accept such a value and it would free
  853. * the passed in info_structs but it would quietly fail to free any of the data
  854. * inside them. In 1.6.0 it quietly does nothing (it has to be quiet because it
  855. * has no png_ptr.)
  856. */
  857. void PNGAPI
  858. png_destroy_write_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr)
  859. {
  860. png_debug(1, "in png_destroy_write_struct");
  861. if (png_ptr_ptr != NULL)
  862. {
  863. png_structrp png_ptr = *png_ptr_ptr;
  864. if (png_ptr != NULL) /* added in libpng 1.6.0 */
  865. {
  866. png_destroy_info_struct(png_ptr, info_ptr_ptr);
  867. *png_ptr_ptr = NULL;
  868. png_write_destroy(png_ptr);
  869. png_destroy_png_struct(png_ptr);
  870. }
  871. }
  872. }
  873. /* Allow the application to select one or more row filters to use. */
  874. void PNGAPI
  875. png_set_filter(png_structrp png_ptr, int method, int filters)
  876. {
  877. png_debug(1, "in png_set_filter");
  878. if (png_ptr == NULL)
  879. return;
  880. #ifdef PNG_MNG_FEATURES_SUPPORTED
  881. if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
  882. (method == PNG_INTRAPIXEL_DIFFERENCING))
  883. method = PNG_FILTER_TYPE_BASE;
  884. #endif
  885. if (method == PNG_FILTER_TYPE_BASE)
  886. {
  887. switch (filters & (PNG_ALL_FILTERS | 0x07))
  888. {
  889. #ifdef PNG_WRITE_FILTER_SUPPORTED
  890. case 5:
  891. case 6:
  892. case 7: png_app_error(png_ptr, "Unknown row filter for method 0");
  893. #endif /* WRITE_FILTER */
  894. /* FALLTHROUGH */
  895. case PNG_FILTER_VALUE_NONE:
  896. png_ptr->do_filter = PNG_FILTER_NONE; break;
  897. #ifdef PNG_WRITE_FILTER_SUPPORTED
  898. case PNG_FILTER_VALUE_SUB:
  899. png_ptr->do_filter = PNG_FILTER_SUB; break;
  900. case PNG_FILTER_VALUE_UP:
  901. png_ptr->do_filter = PNG_FILTER_UP; break;
  902. case PNG_FILTER_VALUE_AVG:
  903. png_ptr->do_filter = PNG_FILTER_AVG; break;
  904. case PNG_FILTER_VALUE_PAETH:
  905. png_ptr->do_filter = PNG_FILTER_PAETH; break;
  906. default:
  907. png_ptr->do_filter = (png_byte)filters; break;
  908. #else
  909. default:
  910. png_app_error(png_ptr, "Unknown row filter for method 0");
  911. #endif /* WRITE_FILTER */
  912. }
  913. #ifdef PNG_WRITE_FILTER_SUPPORTED
  914. /* If we have allocated the row_buf, this means we have already started
  915. * with the image and we should have allocated all of the filter buffers
  916. * that have been selected. If prev_row isn't already allocated, then
  917. * it is too late to start using the filters that need it, since we
  918. * will be missing the data in the previous row. If an application
  919. * wants to start and stop using particular filters during compression,
  920. * it should start out with all of the filters, and then remove them
  921. * or add them back after the start of compression.
  922. *
  923. * NOTE: this is a nasty constraint on the code, because it means that the
  924. * prev_row buffer must be maintained even if there are currently no
  925. * 'prev_row' requiring filters active.
  926. */
  927. if (png_ptr->row_buf != NULL)
  928. {
  929. int num_filters;
  930. png_alloc_size_t buf_size;
  931. /* Repeat the checks in png_write_start_row; 1 pixel high or wide
  932. * images cannot benefit from certain filters. If this isn't done here
  933. * the check below will fire on 1 pixel high images.
  934. */
  935. if (png_ptr->height == 1)
  936. filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);
  937. if (png_ptr->width == 1)
  938. filters &= ~(PNG_FILTER_SUB|PNG_FILTER_AVG|PNG_FILTER_PAETH);
  939. if ((filters & (PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH)) != 0
  940. && png_ptr->prev_row == NULL)
  941. {
  942. /* This is the error case, however it is benign - the previous row
  943. * is not available so the filter can't be used. Just warn here.
  944. */
  945. png_app_warning(png_ptr,
  946. "png_set_filter: UP/AVG/PAETH cannot be added after start");
  947. filters &= ~(PNG_FILTER_UP|PNG_FILTER_AVG|PNG_FILTER_PAETH);
  948. }
  949. num_filters = 0;
  950. if (filters & PNG_FILTER_SUB)
  951. num_filters++;
  952. if (filters & PNG_FILTER_UP)
  953. num_filters++;
  954. if (filters & PNG_FILTER_AVG)
  955. num_filters++;
  956. if (filters & PNG_FILTER_PAETH)
  957. num_filters++;
  958. /* Allocate needed row buffers if they have not already been
  959. * allocated.
  960. */
  961. buf_size = PNG_ROWBYTES(png_ptr->usr_channels * png_ptr->usr_bit_depth,
  962. png_ptr->width) + 1;
  963. if (png_ptr->try_row == NULL)
  964. png_ptr->try_row = png_voidcast(png_bytep,
  965. png_malloc(png_ptr, buf_size));
  966. if (num_filters > 1)
  967. {
  968. if (png_ptr->tst_row == NULL)
  969. png_ptr->tst_row = png_voidcast(png_bytep,
  970. png_malloc(png_ptr, buf_size));
  971. }
  972. }
  973. png_ptr->do_filter = (png_byte)filters;
  974. #endif
  975. }
  976. else
  977. png_error(png_ptr, "Unknown custom filter method");
  978. }
  979. #ifdef PNG_WRITE_WEIGHTED_FILTER_SUPPORTED /* DEPRECATED */
  980. /* Provide floating and fixed point APIs */
  981. #ifdef PNG_FLOATING_POINT_SUPPORTED
  982. void PNGAPI
  983. png_set_filter_heuristics(png_structrp png_ptr, int heuristic_method,
  984. int num_weights, png_const_doublep filter_weights,
  985. png_const_doublep filter_costs)
  986. {
  987. PNG_UNUSED(png_ptr)
  988. PNG_UNUSED(heuristic_method)
  989. PNG_UNUSED(num_weights)
  990. PNG_UNUSED(filter_weights)
  991. PNG_UNUSED(filter_costs)
  992. }
  993. #endif /* FLOATING_POINT */
  994. #ifdef PNG_FIXED_POINT_SUPPORTED
  995. void PNGAPI
  996. png_set_filter_heuristics_fixed(png_structrp png_ptr, int heuristic_method,
  997. int num_weights, png_const_fixed_point_p filter_weights,
  998. png_const_fixed_point_p filter_costs)
  999. {
  1000. PNG_UNUSED(png_ptr)
  1001. PNG_UNUSED(heuristic_method)
  1002. PNG_UNUSED(num_weights)
  1003. PNG_UNUSED(filter_weights)
  1004. PNG_UNUSED(filter_costs)
  1005. }
  1006. #endif /* FIXED_POINT */
  1007. #endif /* WRITE_WEIGHTED_FILTER */
  1008. #ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED
  1009. void PNGAPI
  1010. png_set_compression_level(png_structrp png_ptr, int level)
  1011. {
  1012. png_debug(1, "in png_set_compression_level");
  1013. if (png_ptr == NULL)
  1014. return;
  1015. png_ptr->zlib_level = level;
  1016. }
  1017. void PNGAPI
  1018. png_set_compression_mem_level(png_structrp png_ptr, int mem_level)
  1019. {
  1020. png_debug(1, "in png_set_compression_mem_level");
  1021. if (png_ptr == NULL)
  1022. return;
  1023. png_ptr->zlib_mem_level = mem_level;
  1024. }
  1025. void PNGAPI
  1026. png_set_compression_strategy(png_structrp png_ptr, int strategy)
  1027. {
  1028. png_debug(1, "in png_set_compression_strategy");
  1029. if (png_ptr == NULL)
  1030. return;
  1031. /* The flag setting here prevents the libpng dynamic selection of strategy.
  1032. */
  1033. png_ptr->flags |= PNG_FLAG_ZLIB_CUSTOM_STRATEGY;
  1034. png_ptr->zlib_strategy = strategy;
  1035. }
  1036. /* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
  1037. * smaller value of window_bits if it can do so safely.
  1038. */
  1039. void PNGAPI
  1040. png_set_compression_window_bits(png_structrp png_ptr, int window_bits)
  1041. {
  1042. if (png_ptr == NULL)
  1043. return;
  1044. /* Prior to 1.6.0 this would warn but then set the window_bits value. This
  1045. * meant that negative window bits values could be selected that would cause
  1046. * libpng to write a non-standard PNG file with raw deflate or gzip
  1047. * compressed IDAT or ancillary chunks. Such files can be read and there is
  1048. * no warning on read, so this seems like a very bad idea.
  1049. */
  1050. if (window_bits > 15)
  1051. {
  1052. png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
  1053. window_bits = 15;
  1054. }
  1055. else if (window_bits < 8)
  1056. {
  1057. png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
  1058. window_bits = 8;
  1059. }
  1060. png_ptr->zlib_window_bits = window_bits;
  1061. }
  1062. void PNGAPI
  1063. png_set_compression_method(png_structrp png_ptr, int method)
  1064. {
  1065. png_debug(1, "in png_set_compression_method");
  1066. if (png_ptr == NULL)
  1067. return;
  1068. /* This would produce an invalid PNG file if it worked, but it doesn't and
  1069. * deflate will fault it, so it is harmless to just warn here.
  1070. */
  1071. if (method != 8)
  1072. png_warning(png_ptr, "Only compression method 8 is supported by PNG");
  1073. png_ptr->zlib_method = method;
  1074. }
  1075. #endif /* WRITE_CUSTOMIZE_COMPRESSION */
  1076. /* The following were added to libpng-1.5.4 */
  1077. #ifdef PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
  1078. void PNGAPI
  1079. png_set_text_compression_level(png_structrp png_ptr, int level)
  1080. {
  1081. png_debug(1, "in png_set_text_compression_level");
  1082. if (png_ptr == NULL)
  1083. return;
  1084. png_ptr->zlib_text_level = level;
  1085. }
  1086. void PNGAPI
  1087. png_set_text_compression_mem_level(png_structrp png_ptr, int mem_level)
  1088. {
  1089. png_debug(1, "in png_set_text_compression_mem_level");
  1090. if (png_ptr == NULL)
  1091. return;
  1092. png_ptr->zlib_text_mem_level = mem_level;
  1093. }
  1094. void PNGAPI
  1095. png_set_text_compression_strategy(png_structrp png_ptr, int strategy)
  1096. {
  1097. png_debug(1, "in png_set_text_compression_strategy");
  1098. if (png_ptr == NULL)
  1099. return;
  1100. png_ptr->zlib_text_strategy = strategy;
  1101. }
  1102. /* If PNG_WRITE_OPTIMIZE_CMF_SUPPORTED is defined, libpng will use a
  1103. * smaller value of window_bits if it can do so safely.
  1104. */
  1105. void PNGAPI
  1106. png_set_text_compression_window_bits(png_structrp png_ptr, int window_bits)
  1107. {
  1108. if (png_ptr == NULL)
  1109. return;
  1110. if (window_bits > 15)
  1111. {
  1112. png_warning(png_ptr, "Only compression windows <= 32k supported by PNG");
  1113. window_bits = 15;
  1114. }
  1115. else if (window_bits < 8)
  1116. {
  1117. png_warning(png_ptr, "Only compression windows >= 256 supported by PNG");
  1118. window_bits = 8;
  1119. }
  1120. png_ptr->zlib_text_window_bits = window_bits;
  1121. }
  1122. void PNGAPI
  1123. png_set_text_compression_method(png_structrp png_ptr, int method)
  1124. {
  1125. png_debug(1, "in png_set_text_compression_method");
  1126. if (png_ptr == NULL)
  1127. return;
  1128. if (method != 8)
  1129. png_warning(png_ptr, "Only compression method 8 is supported by PNG");
  1130. png_ptr->zlib_text_method = method;
  1131. }
  1132. #endif /* WRITE_CUSTOMIZE_ZTXT_COMPRESSION */
  1133. /* end of API added to libpng-1.5.4 */
  1134. void PNGAPI
  1135. png_set_write_status_fn(png_structrp png_ptr, png_write_status_ptr write_row_fn)
  1136. {
  1137. if (png_ptr == NULL)
  1138. return;
  1139. png_ptr->write_row_fn = write_row_fn;
  1140. }
  1141. #ifdef PNG_WRITE_USER_TRANSFORM_SUPPORTED
  1142. void PNGAPI
  1143. png_set_write_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
  1144. write_user_transform_fn)
  1145. {
  1146. png_debug(1, "in png_set_write_user_transform_fn");
  1147. if (png_ptr == NULL)
  1148. return;
  1149. png_ptr->transformations |= PNG_USER_TRANSFORM;
  1150. png_ptr->write_user_transform_fn = write_user_transform_fn;
  1151. }
  1152. #endif
  1153. #ifdef PNG_INFO_IMAGE_SUPPORTED
  1154. void PNGAPI
  1155. png_write_png(png_structrp png_ptr, png_inforp info_ptr,
  1156. int transforms, voidp params)
  1157. {
  1158. if (png_ptr == NULL || info_ptr == NULL)
  1159. return;
  1160. if ((info_ptr->valid & PNG_INFO_IDAT) == 0)
  1161. {
  1162. png_app_error(png_ptr, "no rows for png_write_image to write");
  1163. return;
  1164. }
  1165. /* Write the file header information. */
  1166. png_write_info(png_ptr, info_ptr);
  1167. /* ------ these transformations don't touch the info structure ------- */
  1168. /* Invert monochrome pixels */
  1169. if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0)
  1170. #ifdef PNG_WRITE_INVERT_SUPPORTED
  1171. png_set_invert_mono(png_ptr);
  1172. #else
  1173. png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
  1174. #endif
  1175. /* Shift the pixels up to a legal bit depth and fill in
  1176. * as appropriate to correctly scale the image.
  1177. */
  1178. if ((transforms & PNG_TRANSFORM_SHIFT) != 0)
  1179. #ifdef PNG_WRITE_SHIFT_SUPPORTED
  1180. if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
  1181. png_set_shift(png_ptr, &info_ptr->sig_bit);
  1182. #else
  1183. png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
  1184. #endif
  1185. /* Pack pixels into bytes */
  1186. if ((transforms & PNG_TRANSFORM_PACKING) != 0)
  1187. #ifdef PNG_WRITE_PACK_SUPPORTED
  1188. png_set_packing(png_ptr);
  1189. #else
  1190. png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
  1191. #endif
  1192. /* Swap location of alpha bytes from ARGB to RGBA */
  1193. if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0)
  1194. #ifdef PNG_WRITE_SWAP_ALPHA_SUPPORTED
  1195. png_set_swap_alpha(png_ptr);
  1196. #else
  1197. png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
  1198. #endif
  1199. /* Remove a filler (X) from XRGB/RGBX/AG/GA into to convert it into
  1200. * RGB, note that the code expects the input color type to be G or RGB; no
  1201. * alpha channel.
  1202. */
  1203. if ((transforms & (PNG_TRANSFORM_STRIP_FILLER_AFTER|
  1204. PNG_TRANSFORM_STRIP_FILLER_BEFORE)) != 0)
  1205. {
  1206. #ifdef PNG_WRITE_FILLER_SUPPORTED
  1207. if ((transforms & PNG_TRANSFORM_STRIP_FILLER_AFTER) != 0)
  1208. {
  1209. if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0)
  1210. png_app_error(png_ptr,
  1211. "PNG_TRANSFORM_STRIP_FILLER: BEFORE+AFTER not supported");
  1212. /* Continue if ignored - this is the pre-1.6.10 behavior */
  1213. png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
  1214. }
  1215. else if ((transforms & PNG_TRANSFORM_STRIP_FILLER_BEFORE) != 0)
  1216. png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
  1217. #else
  1218. png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_FILLER not supported");
  1219. #endif
  1220. }
  1221. /* Flip BGR pixels to RGB */
  1222. if ((transforms & PNG_TRANSFORM_BGR) != 0)
  1223. #ifdef PNG_WRITE_BGR_SUPPORTED
  1224. png_set_bgr(png_ptr);
  1225. #else
  1226. png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
  1227. #endif
  1228. /* Swap bytes of 16-bit files to most significant byte first */
  1229. if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0)
  1230. #ifdef PNG_WRITE_SWAP_SUPPORTED
  1231. png_set_swap(png_ptr);
  1232. #else
  1233. png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
  1234. #endif
  1235. /* Swap bits of 1-bit, 2-bit, 4-bit packed pixel formats */
  1236. if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)
  1237. #ifdef PNG_WRITE_PACKSWAP_SUPPORTED
  1238. png_set_packswap(png_ptr);
  1239. #else
  1240. png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
  1241. #endif
  1242. /* Invert the alpha channel from opacity to transparency */
  1243. if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0)
  1244. #ifdef PNG_WRITE_INVERT_ALPHA_SUPPORTED
  1245. png_set_invert_alpha(png_ptr);
  1246. #else
  1247. png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
  1248. #endif
  1249. /* ----------------------- end of transformations ------------------- */
  1250. /* Write the bits */
  1251. png_write_image(png_ptr, info_ptr->row_pointers);
  1252. /* It is REQUIRED to call this to finish writing the rest of the file */
  1253. png_write_end(png_ptr, info_ptr);
  1254. PNG_UNUSED(params)
  1255. }
  1256. #endif
  1257. #ifdef PNG_SIMPLIFIED_WRITE_SUPPORTED
  1258. /* Initialize the write structure - general purpose utility. */
  1259. static int
  1260. png_image_write_init(png_imagep image)
  1261. {
  1262. png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, image,
  1263. png_safe_error, png_safe_warning);
  1264. if (png_ptr != NULL)
  1265. {
  1266. png_infop info_ptr = png_create_info_struct(png_ptr);
  1267. if (info_ptr != NULL)
  1268. {
  1269. png_controlp control = png_voidcast(png_controlp,
  1270. png_malloc_warn(png_ptr, (sizeof *control)));
  1271. if (control != NULL)
  1272. {
  1273. memset(control, 0, (sizeof *control));
  1274. control->png_ptr = png_ptr;
  1275. control->info_ptr = info_ptr;
  1276. control->for_write = 1;
  1277. image->opaque = control;
  1278. return 1;
  1279. }
  1280. /* Error clean up */
  1281. png_destroy_info_struct(png_ptr, &info_ptr);
  1282. }
  1283. png_destroy_write_struct(&png_ptr, NULL);
  1284. }
  1285. return png_image_error(image, "png_image_write_: out of memory");
  1286. }
  1287. /* Arguments to png_image_write_main: */
  1288. typedef struct
  1289. {
  1290. /* Arguments: */
  1291. png_imagep image;
  1292. png_const_voidp buffer;
  1293. png_int_32 row_stride;
  1294. png_const_voidp colormap;
  1295. int convert_to_8bit;
  1296. /* Local variables: */
  1297. png_const_voidp first_row;
  1298. ptrdiff_t row_bytes;
  1299. png_voidp local_row;
  1300. /* Byte count for memory writing */
  1301. png_bytep memory;
  1302. png_alloc_size_t memory_bytes; /* not used for STDIO */
  1303. png_alloc_size_t output_bytes; /* running total */
  1304. } png_image_write_control;
  1305. /* Write png_uint_16 input to a 16-bit PNG; the png_ptr has already been set to
  1306. * do any necessary byte swapping. The component order is defined by the
  1307. * png_image format value.
  1308. */
  1309. static int
  1310. png_write_image_16bit(png_voidp argument)
  1311. {
  1312. png_image_write_control *display = png_voidcast(png_image_write_control*,
  1313. argument);
  1314. png_imagep image = display->image;
  1315. png_structrp png_ptr = image->opaque->png_ptr;
  1316. png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
  1317. display->first_row);
  1318. png_uint_16p output_row = png_voidcast(png_uint_16p, display->local_row);
  1319. png_uint_16p row_end;
  1320. unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ?
  1321. 3 : 1;
  1322. int aindex = 0;
  1323. png_uint_32 y = image->height;
  1324. if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
  1325. {
  1326. # ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
  1327. if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
  1328. {
  1329. aindex = -1;
  1330. ++input_row; /* To point to the first component */
  1331. ++output_row;
  1332. }
  1333. else
  1334. aindex = (int)channels;
  1335. # else
  1336. aindex = (int)channels;
  1337. # endif
  1338. }
  1339. else
  1340. png_error(png_ptr, "png_write_image: internal call error");
  1341. /* Work out the output row end and count over this, note that the increment
  1342. * above to 'row' means that row_end can actually be beyond the end of the
  1343. * row; this is correct.
  1344. */
  1345. row_end = output_row + image->width * (channels+1);
  1346. for (; y > 0; --y)
  1347. {
  1348. png_const_uint_16p in_ptr = input_row;
  1349. png_uint_16p out_ptr = output_row;
  1350. while (out_ptr < row_end)
  1351. {
  1352. png_uint_16 alpha = in_ptr[aindex];
  1353. png_uint_32 reciprocal = 0;
  1354. int c;
  1355. out_ptr[aindex] = alpha;
  1356. /* Calculate a reciprocal. The correct calculation is simply
  1357. * component/alpha*65535 << 15. (I.e. 15 bits of precision); this
  1358. * allows correct rounding by adding .5 before the shift. 'reciprocal'
  1359. * is only initialized when required.
  1360. */
  1361. if (alpha > 0 && alpha < 65535)
  1362. reciprocal = ((0xffff<<15)+(alpha>>1))/alpha;
  1363. c = (int)channels;
  1364. do /* always at least one channel */
  1365. {
  1366. png_uint_16 component = *in_ptr++;
  1367. /* The following gives 65535 for an alpha of 0, which is fine,
  1368. * otherwise if 0/0 is represented as some other value there is more
  1369. * likely to be a discontinuity which will probably damage
  1370. * compression when moving from a fully transparent area to a
  1371. * nearly transparent one. (The assumption here is that opaque
  1372. * areas tend not to be 0 intensity.)
  1373. */
  1374. if (component >= alpha)
  1375. component = 65535;
  1376. /* component<alpha, so component/alpha is less than one and
  1377. * component*reciprocal is less than 2^31.
  1378. */
  1379. else if (component > 0 && alpha < 65535)
  1380. {
  1381. png_uint_32 calc = component * reciprocal;
  1382. calc += 16384; /* round to nearest */
  1383. component = (png_uint_16)(calc >> 15);
  1384. }
  1385. *out_ptr++ = component;
  1386. }
  1387. while (--c > 0);
  1388. /* Skip to next component (skip the intervening alpha channel) */
  1389. ++in_ptr;
  1390. ++out_ptr;
  1391. }
  1392. png_write_row(png_ptr, png_voidcast(png_const_bytep, display->local_row));
  1393. input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16));
  1394. }
  1395. return 1;
  1396. }
  1397. /* Given 16-bit input (1 to 4 channels) write 8-bit output. If an alpha channel
  1398. * is present it must be removed from the components, the components are then
  1399. * written in sRGB encoding. No components are added or removed.
  1400. *
  1401. * Calculate an alpha reciprocal to reverse pre-multiplication. As above the
  1402. * calculation can be done to 15 bits of accuracy; however, the output needs to
  1403. * be scaled in the range 0..255*65535, so include that scaling here.
  1404. */
  1405. # define UNP_RECIPROCAL(alpha) ((((0xffff*0xff)<<7)+((alpha)>>1))/(alpha))
  1406. static png_byte
  1407. png_unpremultiply(png_uint_32 component, png_uint_32 alpha,
  1408. png_uint_32 reciprocal/*from the above macro*/)
  1409. {
  1410. /* The following gives 1.0 for an alpha of 0, which is fine, otherwise if 0/0
  1411. * is represented as some other value there is more likely to be a
  1412. * discontinuity which will probably damage compression when moving from a
  1413. * fully transparent area to a nearly transparent one. (The assumption here
  1414. * is that opaque areas tend not to be 0 intensity.)
  1415. *
  1416. * There is a rounding problem here; if alpha is less than 128 it will end up
  1417. * as 0 when scaled to 8 bits. To avoid introducing spurious colors into the
  1418. * output change for this too.
  1419. */
  1420. if (component >= alpha || alpha < 128)
  1421. return 255;
  1422. /* component<alpha, so component/alpha is less than one and
  1423. * component*reciprocal is less than 2^31.
  1424. */
  1425. else if (component > 0)
  1426. {
  1427. /* The test is that alpha/257 (rounded) is less than 255, the first value
  1428. * that becomes 255 is 65407.
  1429. * NOTE: this must agree with the PNG_DIV257 macro (which must, therefore,
  1430. * be exact!) [Could also test reciprocal != 0]
  1431. */
  1432. if (alpha < 65407)
  1433. {
  1434. component *= reciprocal;
  1435. component += 64; /* round to nearest */
  1436. component >>= 7;
  1437. }
  1438. else
  1439. component *= 255;
  1440. /* Convert the component to sRGB. */
  1441. return (png_byte)PNG_sRGB_FROM_LINEAR(component);
  1442. }
  1443. else
  1444. return 0;
  1445. }
  1446. static int
  1447. png_write_image_8bit(png_voidp argument)
  1448. {
  1449. png_image_write_control *display = png_voidcast(png_image_write_control*,
  1450. argument);
  1451. png_imagep image = display->image;
  1452. png_structrp png_ptr = image->opaque->png_ptr;
  1453. png_const_uint_16p input_row = png_voidcast(png_const_uint_16p,
  1454. display->first_row);
  1455. png_bytep output_row = png_voidcast(png_bytep, display->local_row);
  1456. png_uint_32 y = image->height;
  1457. unsigned int channels = (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ?
  1458. 3 : 1;
  1459. if ((image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
  1460. {
  1461. png_bytep row_end;
  1462. int aindex;
  1463. # ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
  1464. if ((image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
  1465. {
  1466. aindex = -1;
  1467. ++input_row; /* To point to the first component */
  1468. ++output_row;
  1469. }
  1470. else
  1471. # endif
  1472. aindex = (int)channels;
  1473. /* Use row_end in place of a loop counter: */
  1474. row_end = output_row + image->width * (channels+1);
  1475. for (; y > 0; --y)
  1476. {
  1477. png_const_uint_16p in_ptr = input_row;
  1478. png_bytep out_ptr = output_row;
  1479. while (out_ptr < row_end)
  1480. {
  1481. png_uint_16 alpha = in_ptr[aindex];
  1482. png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
  1483. png_uint_32 reciprocal = 0;
  1484. int c;
  1485. /* Scale and write the alpha channel. */
  1486. out_ptr[aindex] = alphabyte;
  1487. if (alphabyte > 0 && alphabyte < 255)
  1488. reciprocal = UNP_RECIPROCAL(alpha);
  1489. c = (int)channels;
  1490. do /* always at least one channel */
  1491. *out_ptr++ = png_unpremultiply(*in_ptr++, alpha, reciprocal);
  1492. while (--c > 0);
  1493. /* Skip to next component (skip the intervening alpha channel) */
  1494. ++in_ptr;
  1495. ++out_ptr;
  1496. } /* while out_ptr < row_end */
  1497. png_write_row(png_ptr, png_voidcast(png_const_bytep,
  1498. display->local_row));
  1499. input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16));
  1500. } /* while y */
  1501. }
  1502. else
  1503. {
  1504. /* No alpha channel, so the row_end really is the end of the row and it
  1505. * is sufficient to loop over the components one by one.
  1506. */
  1507. png_bytep row_end = output_row + image->width * channels;
  1508. for (; y > 0; --y)
  1509. {
  1510. png_const_uint_16p in_ptr = input_row;
  1511. png_bytep out_ptr = output_row;
  1512. while (out_ptr < row_end)
  1513. {
  1514. png_uint_32 component = *in_ptr++;
  1515. component *= 255;
  1516. *out_ptr++ = (png_byte)PNG_sRGB_FROM_LINEAR(component);
  1517. }
  1518. png_write_row(png_ptr, output_row);
  1519. input_row += (png_uint_16)display->row_bytes/(sizeof (png_uint_16));
  1520. }
  1521. }
  1522. return 1;
  1523. }
  1524. static void
  1525. png_image_set_PLTE(png_image_write_control *display)
  1526. {
  1527. png_imagep image = display->image;
  1528. const void *cmap = display->colormap;
  1529. int entries = image->colormap_entries > 256 ? 256 :
  1530. (int)image->colormap_entries;
  1531. /* NOTE: the caller must check for cmap != NULL and entries != 0 */
  1532. png_uint_32 format = image->format;
  1533. unsigned int channels = PNG_IMAGE_SAMPLE_CHANNELS(format);
  1534. # if defined(PNG_FORMAT_BGR_SUPPORTED) &&\
  1535. defined(PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED)
  1536. int afirst = (format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
  1537. (format & PNG_FORMAT_FLAG_ALPHA) != 0;
  1538. # else
  1539. # define afirst 0
  1540. # endif
  1541. # ifdef PNG_FORMAT_BGR_SUPPORTED
  1542. int bgr = (format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
  1543. # else
  1544. # define bgr 0
  1545. # endif
  1546. int i, num_trans;
  1547. png_color palette[256];
  1548. png_byte tRNS[256];
  1549. memset(tRNS, 255, (sizeof tRNS));
  1550. memset(palette, 0, (sizeof palette));
  1551. for (i=num_trans=0; i<entries; ++i)
  1552. {
  1553. /* This gets automatically converted to sRGB with reversal of the
  1554. * pre-multiplication if the color-map has an alpha channel.
  1555. */
  1556. if ((format & PNG_FORMAT_FLAG_LINEAR) != 0)
  1557. {
  1558. png_const_uint_16p entry = png_voidcast(png_const_uint_16p, cmap);
  1559. entry += (unsigned int)i * channels;
  1560. if ((channels & 1) != 0) /* no alpha */
  1561. {
  1562. if (channels >= 3) /* RGB */
  1563. {
  1564. palette[i].blue = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
  1565. entry[(2 ^ bgr)]);
  1566. palette[i].green = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
  1567. entry[1]);
  1568. palette[i].red = (png_byte)PNG_sRGB_FROM_LINEAR(255 *
  1569. entry[bgr]);
  1570. }
  1571. else /* Gray */
  1572. palette[i].blue = palette[i].red = palette[i].green =
  1573. (png_byte)PNG_sRGB_FROM_LINEAR(255 * *entry);
  1574. }
  1575. else /* alpha */
  1576. {
  1577. png_uint_16 alpha = entry[afirst ? 0 : channels-1];
  1578. png_byte alphabyte = (png_byte)PNG_DIV257(alpha);
  1579. png_uint_32 reciprocal = 0;
  1580. /* Calculate a reciprocal, as in the png_write_image_8bit code above
  1581. * this is designed to produce a value scaled to 255*65535 when
  1582. * divided by 128 (i.e. asr 7).
  1583. */
  1584. if (alphabyte > 0 && alphabyte < 255)
  1585. reciprocal = (((0xffff*0xff)<<7)+(alpha>>1))/alpha;
  1586. tRNS[i] = alphabyte;
  1587. if (alphabyte < 255)
  1588. num_trans = i+1;
  1589. if (channels >= 3) /* RGB */
  1590. {
  1591. palette[i].blue = png_unpremultiply(entry[afirst + (2 ^ bgr)],
  1592. alpha, reciprocal);
  1593. palette[i].green = png_unpremultiply(entry[afirst + 1], alpha,
  1594. reciprocal);
  1595. palette[i].red = png_unpremultiply(entry[afirst + bgr], alpha,
  1596. reciprocal);
  1597. }
  1598. else /* gray */
  1599. palette[i].blue = palette[i].red = palette[i].green =
  1600. png_unpremultiply(entry[afirst], alpha, reciprocal);
  1601. }
  1602. }
  1603. else /* Color-map has sRGB values */
  1604. {
  1605. png_const_bytep entry = png_voidcast(png_const_bytep, cmap);
  1606. entry += (unsigned int)i * channels;
  1607. switch (channels)
  1608. {
  1609. case 4:
  1610. tRNS[i] = entry[afirst ? 0 : 3];
  1611. if (tRNS[i] < 255)
  1612. num_trans = i+1;
  1613. /* FALLTHROUGH */
  1614. case 3:
  1615. palette[i].blue = entry[afirst + (2 ^ bgr)];
  1616. palette[i].green = entry[afirst + 1];
  1617. palette[i].red = entry[afirst + bgr];
  1618. break;
  1619. case 2:
  1620. tRNS[i] = entry[1 ^ afirst];
  1621. if (tRNS[i] < 255)
  1622. num_trans = i+1;
  1623. /* FALLTHROUGH */
  1624. case 1:
  1625. palette[i].blue = palette[i].red = palette[i].green =
  1626. entry[afirst];
  1627. break;
  1628. default:
  1629. break;
  1630. }
  1631. }
  1632. }
  1633. # ifdef afirst
  1634. # undef afirst
  1635. # endif
  1636. # ifdef bgr
  1637. # undef bgr
  1638. # endif
  1639. png_set_PLTE(image->opaque->png_ptr, image->opaque->info_ptr, palette,
  1640. entries);
  1641. if (num_trans > 0)
  1642. png_set_tRNS(image->opaque->png_ptr, image->opaque->info_ptr, tRNS,
  1643. num_trans, NULL);
  1644. image->colormap_entries = (png_uint_32)entries;
  1645. }
  1646. static int
  1647. png_image_write_main(png_voidp argument)
  1648. {
  1649. png_image_write_control *display = png_voidcast(png_image_write_control*,
  1650. argument);
  1651. png_imagep image = display->image;
  1652. png_structrp png_ptr = image->opaque->png_ptr;
  1653. png_inforp info_ptr = image->opaque->info_ptr;
  1654. png_uint_32 format = image->format;
  1655. /* The following four ints are actually booleans */
  1656. int colormap = (format & PNG_FORMAT_FLAG_COLORMAP);
  1657. int linear = !colormap && (format & PNG_FORMAT_FLAG_LINEAR); /* input */
  1658. int alpha = !colormap && (format & PNG_FORMAT_FLAG_ALPHA);
  1659. int write_16bit = linear && (display->convert_to_8bit == 0);
  1660. # ifdef PNG_BENIGN_ERRORS_SUPPORTED
  1661. /* Make sure we error out on any bad situation */
  1662. png_set_benign_errors(png_ptr, 0/*error*/);
  1663. # endif
  1664. /* Default the 'row_stride' parameter if required, also check the row stride
  1665. * and total image size to ensure that they are within the system limits.
  1666. */
  1667. {
  1668. unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format);
  1669. if (image->width <= 0x7fffffffU/channels) /* no overflow */
  1670. {
  1671. png_uint_32 check;
  1672. png_uint_32 png_row_stride = image->width * channels;
  1673. if (display->row_stride == 0)
  1674. display->row_stride = (png_int_32)/*SAFE*/png_row_stride;
  1675. if (display->row_stride < 0)
  1676. check = (png_uint_32)(-display->row_stride);
  1677. else
  1678. check = (png_uint_32)display->row_stride;
  1679. if (check >= png_row_stride)
  1680. {
  1681. /* Now check for overflow of the image buffer calculation; this
  1682. * limits the whole image size to 32 bits for API compatibility with
  1683. * the current, 32-bit, PNG_IMAGE_BUFFER_SIZE macro.
  1684. */
  1685. if (image->height > 0xffffffffU/png_row_stride)
  1686. png_error(image->opaque->png_ptr, "memory image too large");
  1687. }
  1688. else
  1689. png_error(image->opaque->png_ptr, "supplied row stride too small");
  1690. }
  1691. else
  1692. png_error(image->opaque->png_ptr, "image row stride too large");
  1693. }
  1694. /* Set the required transforms then write the rows in the correct order. */
  1695. if ((format & PNG_FORMAT_FLAG_COLORMAP) != 0)
  1696. {
  1697. if (display->colormap != NULL && image->colormap_entries > 0)
  1698. {
  1699. png_uint_32 entries = image->colormap_entries;
  1700. png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
  1701. entries > 16 ? 8 : (entries > 4 ? 4 : (entries > 2 ? 2 : 1)),
  1702. PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
  1703. PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
  1704. png_image_set_PLTE(display);
  1705. }
  1706. else
  1707. png_error(image->opaque->png_ptr,
  1708. "no color-map for color-mapped image");
  1709. }
  1710. else
  1711. png_set_IHDR(png_ptr, info_ptr, image->width, image->height,
  1712. write_16bit ? 16 : 8,
  1713. ((format & PNG_FORMAT_FLAG_COLOR) ? PNG_COLOR_MASK_COLOR : 0) +
  1714. ((format & PNG_FORMAT_FLAG_ALPHA) ? PNG_COLOR_MASK_ALPHA : 0),
  1715. PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
  1716. /* Counter-intuitively the data transformations must be called *after*
  1717. * png_write_info, not before as in the read code, but the 'set' functions
  1718. * must still be called before. Just set the color space information, never
  1719. * write an interlaced image.
  1720. */
  1721. if (write_16bit != 0)
  1722. {
  1723. /* The gamma here is 1.0 (linear) and the cHRM chunk matches sRGB. */
  1724. png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_LINEAR);
  1725. if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0)
  1726. png_set_cHRM_fixed(png_ptr, info_ptr,
  1727. /* color x y */
  1728. /* white */ 31270, 32900,
  1729. /* red */ 64000, 33000,
  1730. /* green */ 30000, 60000,
  1731. /* blue */ 15000, 6000
  1732. );
  1733. }
  1734. else if ((image->flags & PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB) == 0)
  1735. png_set_sRGB(png_ptr, info_ptr, PNG_sRGB_INTENT_PERCEPTUAL);
  1736. /* Else writing an 8-bit file and the *colors* aren't sRGB, but the 8-bit
  1737. * space must still be gamma encoded.
  1738. */
  1739. else
  1740. png_set_gAMA_fixed(png_ptr, info_ptr, PNG_GAMMA_sRGB_INVERSE);
  1741. /* Write the file header. */
  1742. png_write_info(png_ptr, info_ptr);
  1743. /* Now set up the data transformations (*after* the header is written),
  1744. * remove the handled transformations from the 'format' flags for checking.
  1745. *
  1746. * First check for a little endian system if writing 16-bit files.
  1747. */
  1748. if (write_16bit != 0)
  1749. {
  1750. png_uint_16 le = 0x0001;
  1751. if ((*(png_const_bytep) & le) != 0)
  1752. png_set_swap(png_ptr);
  1753. }
  1754. # ifdef PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
  1755. if ((format & PNG_FORMAT_FLAG_BGR) != 0)
  1756. {
  1757. if (colormap == 0 && (format & PNG_FORMAT_FLAG_COLOR) != 0)
  1758. png_set_bgr(png_ptr);
  1759. format &= ~PNG_FORMAT_FLAG_BGR;
  1760. }
  1761. # endif
  1762. # ifdef PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
  1763. if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
  1764. {
  1765. if (colormap == 0 && (format & PNG_FORMAT_FLAG_ALPHA) != 0)
  1766. png_set_swap_alpha(png_ptr);
  1767. format &= ~PNG_FORMAT_FLAG_AFIRST;
  1768. }
  1769. # endif
  1770. /* If there are 16 or fewer color-map entries we wrote a lower bit depth
  1771. * above, but the application data is still byte packed.
  1772. */
  1773. if (colormap != 0 && image->colormap_entries <= 16)
  1774. png_set_packing(png_ptr);
  1775. /* That should have handled all (both) the transforms. */
  1776. if ((format & ~(png_uint_32)(PNG_FORMAT_FLAG_COLOR | PNG_FORMAT_FLAG_LINEAR |
  1777. PNG_FORMAT_FLAG_ALPHA | PNG_FORMAT_FLAG_COLORMAP)) != 0)
  1778. png_error(png_ptr, "png_write_image: unsupported transformation");
  1779. {
  1780. png_const_bytep row = png_voidcast(png_const_bytep, display->buffer);
  1781. ptrdiff_t row_bytes = display->row_stride;
  1782. if (linear != 0)
  1783. row_bytes *= (sizeof (png_uint_16));
  1784. if (row_bytes < 0)
  1785. row += (image->height-1) * (-row_bytes);
  1786. display->first_row = row;
  1787. display->row_bytes = row_bytes;
  1788. }
  1789. /* Apply 'fast' options if the flag is set. */
  1790. if ((image->flags & PNG_IMAGE_FLAG_FAST) != 0)
  1791. {
  1792. png_set_filter(png_ptr, PNG_FILTER_TYPE_BASE, PNG_NO_FILTERS);
  1793. /* NOTE: determined by experiment using pngstest, this reflects some
  1794. * balance between the time to write the image once and the time to read
  1795. * it about 50 times. The speed-up in pngstest was about 10-20% of the
  1796. * total (user) time on a heavily loaded system.
  1797. */
  1798. # ifdef PNG_WRITE_CUSTOMIZE_COMPRESSION_SUPPORTED
  1799. png_set_compression_level(png_ptr, 3);
  1800. # endif
  1801. }
  1802. /* Check for the cases that currently require a pre-transform on the row
  1803. * before it is written. This only applies when the input is 16-bit and
  1804. * either there is an alpha channel or it is converted to 8-bit.
  1805. */
  1806. if ((linear != 0 && alpha != 0 ) ||
  1807. (colormap == 0 && display->convert_to_8bit != 0))
  1808. {
  1809. png_bytep row = png_voidcast(png_bytep, png_malloc(png_ptr,
  1810. png_get_rowbytes(png_ptr, info_ptr)));
  1811. int result;
  1812. display->local_row = row;
  1813. if (write_16bit != 0)
  1814. result = png_safe_execute(image, png_write_image_16bit, display);
  1815. else
  1816. result = png_safe_execute(image, png_write_image_8bit, display);
  1817. display->local_row = NULL;
  1818. png_free(png_ptr, row);
  1819. /* Skip the 'write_end' on error: */
  1820. if (result == 0)
  1821. return 0;
  1822. }
  1823. /* Otherwise this is the case where the input is in a format currently
  1824. * supported by the rest of the libpng write code; call it directly.
  1825. */
  1826. else
  1827. {
  1828. png_const_bytep row = png_voidcast(png_const_bytep, display->first_row);
  1829. ptrdiff_t row_bytes = display->row_bytes;
  1830. png_uint_32 y = image->height;
  1831. for (; y > 0; --y)
  1832. {
  1833. png_write_row(png_ptr, row);
  1834. row += row_bytes;
  1835. }
  1836. }
  1837. png_write_end(png_ptr, info_ptr);
  1838. return 1;
  1839. }
  1840. static void (PNGCBAPI
  1841. image_memory_write)(png_structp png_ptr, png_bytep/*const*/ data, size_t size)
  1842. {
  1843. png_image_write_control *display = png_voidcast(png_image_write_control*,
  1844. png_ptr->io_ptr/*backdoor: png_get_io_ptr(png_ptr)*/);
  1845. png_alloc_size_t ob = display->output_bytes;
  1846. /* Check for overflow; this should never happen: */
  1847. if (size <= ((png_alloc_size_t)-1) - ob)
  1848. {
  1849. /* I don't think libpng ever does this, but just in case: */
  1850. if (size > 0)
  1851. {
  1852. if (display->memory_bytes >= ob+size) /* writing */
  1853. memcpy(display->memory+ob, data, size);
  1854. /* Always update the size: */
  1855. display->output_bytes = ob+size;
  1856. }
  1857. }
  1858. else
  1859. png_error(png_ptr, "png_image_write_to_memory: PNG too big");
  1860. }
  1861. static void (PNGCBAPI
  1862. image_memory_flush)(png_structp png_ptr)
  1863. {
  1864. PNG_UNUSED(png_ptr)
  1865. }
  1866. static int
  1867. png_image_write_memory(png_voidp argument)
  1868. {
  1869. png_image_write_control *display = png_voidcast(png_image_write_control*,
  1870. argument);
  1871. /* The rest of the memory-specific init and write_main in an error protected
  1872. * environment. This case needs to use callbacks for the write operations
  1873. * since libpng has no built in support for writing to memory.
  1874. */
  1875. png_set_write_fn(display->image->opaque->png_ptr, display/*io_ptr*/,
  1876. image_memory_write, image_memory_flush);
  1877. return png_image_write_main(display);
  1878. }
  1879. int PNGAPI
  1880. png_image_write_to_memory(png_imagep image, void *memory,
  1881. png_alloc_size_t * PNG_RESTRICT memory_bytes, int convert_to_8bit,
  1882. const void *buffer, png_int_32 row_stride, const void *colormap)
  1883. {
  1884. /* Write the image to the given buffer, or count the bytes if it is NULL */
  1885. if (image != NULL && image->version == PNG_IMAGE_VERSION)
  1886. {
  1887. if (memory_bytes != NULL && buffer != NULL)
  1888. {
  1889. /* This is to give the caller an easier error detection in the NULL
  1890. * case and guard against uninitialized variable problems:
  1891. */
  1892. if (memory == NULL)
  1893. *memory_bytes = 0;
  1894. if (png_image_write_init(image) != 0)
  1895. {
  1896. png_image_write_control display;
  1897. int result;
  1898. memset(&display, 0, (sizeof display));
  1899. display.image = image;
  1900. display.buffer = buffer;
  1901. display.row_stride = row_stride;
  1902. display.colormap = colormap;
  1903. display.convert_to_8bit = convert_to_8bit;
  1904. display.memory = png_voidcast(png_bytep, memory);
  1905. display.memory_bytes = *memory_bytes;
  1906. display.output_bytes = 0;
  1907. result = png_safe_execute(image, png_image_write_memory, &display);
  1908. png_image_free(image);
  1909. /* write_memory returns true even if we ran out of buffer. */
  1910. if (result)
  1911. {
  1912. /* On out-of-buffer this function returns '0' but still updates
  1913. * memory_bytes:
  1914. */
  1915. if (memory != NULL && display.output_bytes > *memory_bytes)
  1916. result = 0;
  1917. *memory_bytes = display.output_bytes;
  1918. }
  1919. return result;
  1920. }
  1921. else
  1922. return 0;
  1923. }
  1924. else
  1925. return png_image_error(image,
  1926. "png_image_write_to_memory: invalid argument");
  1927. }
  1928. else if (image != NULL)
  1929. return png_image_error(image,
  1930. "png_image_write_to_memory: incorrect PNG_IMAGE_VERSION");
  1931. else
  1932. return 0;
  1933. }
  1934. #ifdef PNG_SIMPLIFIED_WRITE_STDIO_SUPPORTED
  1935. int PNGAPI
  1936. png_image_write_to_stdio(png_imagep image, FILE *file, int convert_to_8bit,
  1937. const void *buffer, png_int_32 row_stride, const void *colormap)
  1938. {
  1939. /* Write the image to the given (FILE*). */
  1940. if (image != NULL && image->version == PNG_IMAGE_VERSION)
  1941. {
  1942. if (file != NULL && buffer != NULL)
  1943. {
  1944. if (png_image_write_init(image) != 0)
  1945. {
  1946. png_image_write_control display;
  1947. int result;
  1948. /* This is slightly evil, but png_init_io doesn't do anything other
  1949. * than this and we haven't changed the standard IO functions so
  1950. * this saves a 'safe' function.
  1951. */
  1952. image->opaque->png_ptr->io_ptr = file;
  1953. memset(&display, 0, (sizeof display));
  1954. display.image = image;
  1955. display.buffer = buffer;
  1956. display.row_stride = row_stride;
  1957. display.colormap = colormap;
  1958. display.convert_to_8bit = convert_to_8bit;
  1959. result = png_safe_execute(image, png_image_write_main, &display);
  1960. png_image_free(image);
  1961. return result;
  1962. }
  1963. else
  1964. return 0;
  1965. }
  1966. else
  1967. return png_image_error(image,
  1968. "png_image_write_to_stdio: invalid argument");
  1969. }
  1970. else if (image != NULL)
  1971. return png_image_error(image,
  1972. "png_image_write_to_stdio: incorrect PNG_IMAGE_VERSION");
  1973. else
  1974. return 0;
  1975. }
  1976. int PNGAPI
  1977. png_image_write_to_file(png_imagep image, const char *file_name,
  1978. int convert_to_8bit, const void *buffer, png_int_32 row_stride,
  1979. const void *colormap)
  1980. {
  1981. /* Write the image to the named file. */
  1982. if (image != NULL && image->version == PNG_IMAGE_VERSION)
  1983. {
  1984. if (file_name != NULL && buffer != NULL)
  1985. {
  1986. FILE *fp = fopen(file_name, "wb");
  1987. if (fp != NULL)
  1988. {
  1989. if (png_image_write_to_stdio(image, fp, convert_to_8bit, buffer,
  1990. row_stride, colormap) != 0)
  1991. {
  1992. int error; /* from fflush/fclose */
  1993. /* Make sure the file is flushed correctly. */
  1994. if (fflush(fp) == 0 && ferror(fp) == 0)
  1995. {
  1996. if (fclose(fp) == 0)
  1997. return 1;
  1998. error = errno; /* from fclose */
  1999. }
  2000. else
  2001. {
  2002. error = errno; /* from fflush or ferror */
  2003. (void)fclose(fp);
  2004. }
  2005. (void)remove(file_name);
  2006. /* The image has already been cleaned up; this is just used to
  2007. * set the error (because the original write succeeded).
  2008. */
  2009. return png_image_error(image, strerror(error));
  2010. }
  2011. else
  2012. {
  2013. /* Clean up: just the opened file. */
  2014. (void)fclose(fp);
  2015. (void)remove(file_name);
  2016. return 0;
  2017. }
  2018. }
  2019. else
  2020. return png_image_error(image, strerror(errno));
  2021. }
  2022. else
  2023. return png_image_error(image,
  2024. "png_image_write_to_file: invalid argument");
  2025. }
  2026. else if (image != NULL)
  2027. return png_image_error(image,
  2028. "png_image_write_to_file: incorrect PNG_IMAGE_VERSION");
  2029. else
  2030. return 0;
  2031. }
  2032. #endif /* SIMPLIFIED_WRITE_STDIO */
  2033. #endif /* SIMPLIFIED_WRITE */
  2034. #endif /* WRITE */