pngwrite.c 75 KB

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