pngwrite.c 76 KB

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