pngwrite.c 78 KB

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