pngwrite.c 75 KB

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