pngwrite.c 77 KB

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