pngwrite.c 69 KB

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