pngwrite.c 77 KB

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