pngwrite.c 76 KB

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