pngread.c 140 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198
  1. /* pngread.c - read a PNG file
  2. *
  3. * Copyright (c) 2018-2025 Cosmin Truta
  4. * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
  5. * Copyright (c) 1996-1997 Andreas Dilger
  6. * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. *
  12. * This file contains routines that an application calls directly to
  13. * read a PNG file or stream.
  14. */
  15. #include "pngpriv.h"
  16. #if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
  17. # include <errno.h>
  18. #endif
  19. #ifdef PNG_READ_SUPPORTED
  20. /* Create a PNG structure for reading, and allocate any memory needed. */
  21. PNG_FUNCTION(png_structp,PNGAPI
  22. png_create_read_struct,(png_const_charp user_png_ver, png_voidp error_ptr,
  23. png_error_ptr error_fn, png_error_ptr warn_fn),PNG_ALLOCATED)
  24. {
  25. #ifndef PNG_USER_MEM_SUPPORTED
  26. png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
  27. error_fn, warn_fn, NULL, NULL, NULL);
  28. #else
  29. return png_create_read_struct_2(user_png_ver, error_ptr, error_fn,
  30. warn_fn, NULL, NULL, NULL);
  31. }
  32. /* Alternate create PNG structure for reading, and allocate any memory
  33. * needed.
  34. */
  35. PNG_FUNCTION(png_structp,PNGAPI
  36. png_create_read_struct_2,(png_const_charp user_png_ver, png_voidp error_ptr,
  37. png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
  38. png_malloc_ptr malloc_fn, png_free_ptr free_fn),PNG_ALLOCATED)
  39. {
  40. png_structp png_ptr = png_create_png_struct(user_png_ver, error_ptr,
  41. error_fn, warn_fn, mem_ptr, malloc_fn, free_fn);
  42. #endif /* USER_MEM */
  43. if (png_ptr != NULL)
  44. {
  45. png_ptr->mode = PNG_IS_READ_STRUCT;
  46. /* Added in libpng-1.6.0; this can be used to detect a read structure if
  47. * required (it will be zero in a write structure.)
  48. */
  49. # ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  50. png_ptr->IDAT_read_size = PNG_IDAT_READ_SIZE;
  51. # endif
  52. # ifdef PNG_BENIGN_READ_ERRORS_SUPPORTED
  53. png_ptr->flags |= PNG_FLAG_BENIGN_ERRORS_WARN;
  54. /* In stable builds only warn if an application error can be completely
  55. * handled.
  56. */
  57. # if PNG_RELEASE_BUILD
  58. png_ptr->flags |= PNG_FLAG_APP_WARNINGS_WARN;
  59. # endif
  60. # endif
  61. /* TODO: delay this, it can be done in png_init_io (if the app doesn't
  62. * do it itself) avoiding setting the default function if it is not
  63. * required.
  64. */
  65. png_set_read_fn(png_ptr, NULL, NULL);
  66. }
  67. return png_ptr;
  68. }
  69. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  70. /* Read the information before the actual image data. This has been
  71. * changed in v0.90 to allow reading a file that already has the magic
  72. * bytes read from the stream. You can tell libpng how many bytes have
  73. * been read from the beginning of the stream (up to the maximum of 8)
  74. * via png_set_sig_bytes(), and we will only check the remaining bytes
  75. * here. The application can then have access to the signature bytes we
  76. * read if it is determined that this isn't a valid PNG file.
  77. */
  78. void PNGAPI
  79. png_read_info(png_structrp png_ptr, png_inforp info_ptr)
  80. {
  81. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  82. int keep;
  83. #endif
  84. png_debug(1, "in png_read_info");
  85. if (png_ptr == NULL || info_ptr == NULL)
  86. return;
  87. /* Read and check the PNG file signature. */
  88. png_read_sig(png_ptr, info_ptr);
  89. for (;;)
  90. {
  91. png_uint_32 length = png_read_chunk_header(png_ptr);
  92. png_uint_32 chunk_name = png_ptr->chunk_name;
  93. /* IDAT logic needs to happen here to simplify getting the two flags
  94. * right.
  95. */
  96. if (chunk_name == png_IDAT)
  97. {
  98. if ((png_ptr->mode & PNG_HAVE_IHDR) == 0)
  99. png_chunk_error(png_ptr, "Missing IHDR before IDAT");
  100. else if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  101. (png_ptr->mode & PNG_HAVE_PLTE) == 0)
  102. png_chunk_error(png_ptr, "Missing PLTE before IDAT");
  103. else if ((png_ptr->mode & PNG_AFTER_IDAT) != 0)
  104. png_chunk_benign_error(png_ptr, "Too many IDATs found");
  105. png_ptr->mode |= PNG_HAVE_IDAT;
  106. }
  107. else if ((png_ptr->mode & PNG_HAVE_IDAT) != 0)
  108. {
  109. png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT;
  110. png_ptr->mode |= PNG_AFTER_IDAT;
  111. }
  112. if (chunk_name == png_IHDR)
  113. png_handle_chunk(png_ptr, info_ptr, length);
  114. else if (chunk_name == png_IEND)
  115. png_handle_chunk(png_ptr, info_ptr, length);
  116. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  117. else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
  118. {
  119. png_handle_unknown(png_ptr, info_ptr, length, keep);
  120. if (chunk_name == png_PLTE)
  121. png_ptr->mode |= PNG_HAVE_PLTE;
  122. else if (chunk_name == png_IDAT)
  123. {
  124. png_ptr->idat_size = 0; /* It has been consumed */
  125. break;
  126. }
  127. }
  128. #endif
  129. else if (chunk_name == png_IDAT)
  130. {
  131. png_ptr->idat_size = length;
  132. break;
  133. }
  134. else
  135. png_handle_chunk(png_ptr, info_ptr, length);
  136. }
  137. }
  138. #endif /* SEQUENTIAL_READ */
  139. /* Optional call to update the users info_ptr structure */
  140. void PNGAPI
  141. png_read_update_info(png_structrp png_ptr, png_inforp info_ptr)
  142. {
  143. png_debug(1, "in png_read_update_info");
  144. if (png_ptr != NULL)
  145. {
  146. if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
  147. {
  148. png_read_start_row(png_ptr);
  149. # ifdef PNG_READ_TRANSFORMS_SUPPORTED
  150. png_read_transform_info(png_ptr, info_ptr);
  151. # else
  152. PNG_UNUSED(info_ptr)
  153. # endif
  154. }
  155. /* New in 1.6.0 this avoids the bug of doing the initializations twice */
  156. else
  157. png_app_error(png_ptr,
  158. "png_read_update_info/png_start_read_image: duplicate call");
  159. }
  160. }
  161. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  162. /* Initialize palette, background, etc, after transformations
  163. * are set, but before any reading takes place. This allows
  164. * the user to obtain a gamma-corrected palette, for example.
  165. * If the user doesn't call this, we will do it ourselves.
  166. */
  167. void PNGAPI
  168. png_start_read_image(png_structrp png_ptr)
  169. {
  170. png_debug(1, "in png_start_read_image");
  171. if (png_ptr != NULL)
  172. {
  173. if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
  174. png_read_start_row(png_ptr);
  175. /* New in 1.6.0 this avoids the bug of doing the initializations twice */
  176. else
  177. png_app_error(png_ptr,
  178. "png_start_read_image/png_read_update_info: duplicate call");
  179. }
  180. }
  181. #endif /* SEQUENTIAL_READ */
  182. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  183. #ifdef PNG_MNG_FEATURES_SUPPORTED
  184. /* Undoes intrapixel differencing,
  185. * NOTE: this is apparently only supported in the 'sequential' reader.
  186. */
  187. static void
  188. png_do_read_intrapixel(png_row_infop row_info, png_bytep row)
  189. {
  190. png_debug(1, "in png_do_read_intrapixel");
  191. if (
  192. (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
  193. {
  194. int bytes_per_pixel;
  195. png_uint_32 row_width = row_info->width;
  196. if (row_info->bit_depth == 8)
  197. {
  198. png_bytep rp;
  199. png_uint_32 i;
  200. if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  201. bytes_per_pixel = 3;
  202. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  203. bytes_per_pixel = 4;
  204. else
  205. return;
  206. for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
  207. {
  208. *(rp) = (png_byte)((256 + *rp + *(rp + 1)) & 0xff);
  209. *(rp+2) = (png_byte)((256 + *(rp + 2) + *(rp + 1)) & 0xff);
  210. }
  211. }
  212. else if (row_info->bit_depth == 16)
  213. {
  214. png_bytep rp;
  215. png_uint_32 i;
  216. if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  217. bytes_per_pixel = 6;
  218. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  219. bytes_per_pixel = 8;
  220. else
  221. return;
  222. for (i = 0, rp = row; i < row_width; i++, rp += bytes_per_pixel)
  223. {
  224. png_uint_32 s0 = (png_uint_32)(*(rp ) << 8) | *(rp + 1);
  225. png_uint_32 s1 = (png_uint_32)(*(rp + 2) << 8) | *(rp + 3);
  226. png_uint_32 s2 = (png_uint_32)(*(rp + 4) << 8) | *(rp + 5);
  227. png_uint_32 red = (s0 + s1 + 65536) & 0xffff;
  228. png_uint_32 blue = (s2 + s1 + 65536) & 0xffff;
  229. *(rp ) = (png_byte)((red >> 8) & 0xff);
  230. *(rp + 1) = (png_byte)(red & 0xff);
  231. *(rp + 4) = (png_byte)((blue >> 8) & 0xff);
  232. *(rp + 5) = (png_byte)(blue & 0xff);
  233. }
  234. }
  235. }
  236. }
  237. #endif /* MNG_FEATURES */
  238. void PNGAPI
  239. png_read_row(png_structrp png_ptr, png_bytep row, png_bytep dsp_row)
  240. {
  241. png_row_info row_info;
  242. if (png_ptr == NULL)
  243. return;
  244. png_debug2(1, "in png_read_row (row %lu, pass %d)",
  245. (unsigned long)png_ptr->row_number, png_ptr->pass);
  246. /* png_read_start_row sets the information (in particular iwidth) for this
  247. * interlace pass.
  248. */
  249. if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
  250. png_read_start_row(png_ptr);
  251. /* 1.5.6: row_info moved out of png_struct to a local here. */
  252. row_info.width = png_ptr->iwidth; /* NOTE: width of current interlaced row */
  253. row_info.color_type = png_ptr->color_type;
  254. row_info.bit_depth = png_ptr->bit_depth;
  255. row_info.channels = png_ptr->channels;
  256. row_info.pixel_depth = png_ptr->pixel_depth;
  257. row_info.rowbytes = PNG_ROWBYTES(row_info.pixel_depth, row_info.width);
  258. #ifdef PNG_WARNINGS_SUPPORTED
  259. if (png_ptr->row_number == 0 && png_ptr->pass == 0)
  260. {
  261. /* Check for transforms that have been set but were defined out */
  262. #if defined(PNG_WRITE_INVERT_SUPPORTED) && !defined(PNG_READ_INVERT_SUPPORTED)
  263. if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
  264. png_warning(png_ptr, "PNG_READ_INVERT_SUPPORTED is not defined");
  265. #endif
  266. #if defined(PNG_WRITE_FILLER_SUPPORTED) && !defined(PNG_READ_FILLER_SUPPORTED)
  267. if ((png_ptr->transformations & PNG_FILLER) != 0)
  268. png_warning(png_ptr, "PNG_READ_FILLER_SUPPORTED is not defined");
  269. #endif
  270. #if defined(PNG_WRITE_PACKSWAP_SUPPORTED) && \
  271. !defined(PNG_READ_PACKSWAP_SUPPORTED)
  272. if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
  273. png_warning(png_ptr, "PNG_READ_PACKSWAP_SUPPORTED is not defined");
  274. #endif
  275. #if defined(PNG_WRITE_PACK_SUPPORTED) && !defined(PNG_READ_PACK_SUPPORTED)
  276. if ((png_ptr->transformations & PNG_PACK) != 0)
  277. png_warning(png_ptr, "PNG_READ_PACK_SUPPORTED is not defined");
  278. #endif
  279. #if defined(PNG_WRITE_SHIFT_SUPPORTED) && !defined(PNG_READ_SHIFT_SUPPORTED)
  280. if ((png_ptr->transformations & PNG_SHIFT) != 0)
  281. png_warning(png_ptr, "PNG_READ_SHIFT_SUPPORTED is not defined");
  282. #endif
  283. #if defined(PNG_WRITE_BGR_SUPPORTED) && !defined(PNG_READ_BGR_SUPPORTED)
  284. if ((png_ptr->transformations & PNG_BGR) != 0)
  285. png_warning(png_ptr, "PNG_READ_BGR_SUPPORTED is not defined");
  286. #endif
  287. #if defined(PNG_WRITE_SWAP_SUPPORTED) && !defined(PNG_READ_SWAP_SUPPORTED)
  288. if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
  289. png_warning(png_ptr, "PNG_READ_SWAP_SUPPORTED is not defined");
  290. #endif
  291. }
  292. #endif /* WARNINGS */
  293. #ifdef PNG_READ_INTERLACING_SUPPORTED
  294. /* If interlaced and we do not need a new row, combine row and return.
  295. * Notice that the pixels we have from previous rows have been transformed
  296. * already; we can only combine like with like (transformed or
  297. * untransformed) and, because of the libpng API for interlaced images, this
  298. * means we must transform before de-interlacing.
  299. */
  300. if (png_ptr->interlaced != 0 &&
  301. (png_ptr->transformations & PNG_INTERLACE) != 0)
  302. {
  303. switch (png_ptr->pass)
  304. {
  305. case 0:
  306. if (png_ptr->row_number & 0x07)
  307. {
  308. if (dsp_row != NULL)
  309. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  310. png_read_finish_row(png_ptr);
  311. return;
  312. }
  313. break;
  314. case 1:
  315. if ((png_ptr->row_number & 0x07) || png_ptr->width < 5)
  316. {
  317. if (dsp_row != NULL)
  318. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  319. png_read_finish_row(png_ptr);
  320. return;
  321. }
  322. break;
  323. case 2:
  324. if ((png_ptr->row_number & 0x07) != 4)
  325. {
  326. if (dsp_row != NULL && (png_ptr->row_number & 4))
  327. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  328. png_read_finish_row(png_ptr);
  329. return;
  330. }
  331. break;
  332. case 3:
  333. if ((png_ptr->row_number & 3) || png_ptr->width < 3)
  334. {
  335. if (dsp_row != NULL)
  336. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  337. png_read_finish_row(png_ptr);
  338. return;
  339. }
  340. break;
  341. case 4:
  342. if ((png_ptr->row_number & 3) != 2)
  343. {
  344. if (dsp_row != NULL && (png_ptr->row_number & 2))
  345. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  346. png_read_finish_row(png_ptr);
  347. return;
  348. }
  349. break;
  350. case 5:
  351. if ((png_ptr->row_number & 1) || png_ptr->width < 2)
  352. {
  353. if (dsp_row != NULL)
  354. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  355. png_read_finish_row(png_ptr);
  356. return;
  357. }
  358. break;
  359. default:
  360. case 6:
  361. if ((png_ptr->row_number & 1) == 0)
  362. {
  363. png_read_finish_row(png_ptr);
  364. return;
  365. }
  366. break;
  367. }
  368. }
  369. #endif
  370. if ((png_ptr->mode & PNG_HAVE_IDAT) == 0)
  371. png_error(png_ptr, "Invalid attempt to read row data");
  372. /* Fill the row with IDAT data: */
  373. png_ptr->row_buf[0]=255; /* to force error if no data was found */
  374. png_read_IDAT_data(png_ptr, png_ptr->row_buf, row_info.rowbytes + 1);
  375. if (png_ptr->row_buf[0] > PNG_FILTER_VALUE_NONE)
  376. {
  377. if (png_ptr->row_buf[0] < PNG_FILTER_VALUE_LAST)
  378. png_read_filter_row(png_ptr, &row_info, png_ptr->row_buf + 1,
  379. png_ptr->prev_row + 1, png_ptr->row_buf[0]);
  380. else
  381. png_error(png_ptr, "bad adaptive filter value");
  382. }
  383. /* libpng 1.5.6: the following line was copying png_ptr->rowbytes before
  384. * 1.5.6, while the buffer really is this big in current versions of libpng
  385. * it may not be in the future, so this was changed just to copy the
  386. * interlaced count:
  387. */
  388. memcpy(png_ptr->prev_row, png_ptr->row_buf, row_info.rowbytes + 1);
  389. #ifdef PNG_MNG_FEATURES_SUPPORTED
  390. if ((png_ptr->mng_features_permitted & PNG_FLAG_MNG_FILTER_64) != 0 &&
  391. (png_ptr->filter_type == PNG_INTRAPIXEL_DIFFERENCING))
  392. {
  393. /* Intrapixel differencing */
  394. png_do_read_intrapixel(&row_info, png_ptr->row_buf + 1);
  395. }
  396. #endif
  397. #ifdef PNG_READ_TRANSFORMS_SUPPORTED
  398. if (png_ptr->transformations
  399. # ifdef PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
  400. || png_ptr->num_palette_max >= 0
  401. # endif
  402. )
  403. png_do_read_transformations(png_ptr, &row_info);
  404. #endif
  405. /* The transformed pixel depth should match the depth now in row_info. */
  406. if (png_ptr->transformed_pixel_depth == 0)
  407. {
  408. png_ptr->transformed_pixel_depth = row_info.pixel_depth;
  409. if (row_info.pixel_depth > png_ptr->maximum_pixel_depth)
  410. png_error(png_ptr, "sequential row overflow");
  411. }
  412. else if (png_ptr->transformed_pixel_depth != row_info.pixel_depth)
  413. png_error(png_ptr, "internal sequential row size calculation error");
  414. #ifdef PNG_READ_INTERLACING_SUPPORTED
  415. /* Expand interlaced rows to full size */
  416. if (png_ptr->interlaced != 0 &&
  417. (png_ptr->transformations & PNG_INTERLACE) != 0)
  418. {
  419. if (png_ptr->pass < 6)
  420. png_do_read_interlace(&row_info, png_ptr->row_buf + 1, png_ptr->pass,
  421. png_ptr->transformations);
  422. if (dsp_row != NULL)
  423. png_combine_row(png_ptr, dsp_row, 1/*display*/);
  424. if (row != NULL)
  425. png_combine_row(png_ptr, row, 0/*row*/);
  426. }
  427. else
  428. #endif
  429. {
  430. if (row != NULL)
  431. png_combine_row(png_ptr, row, -1/*ignored*/);
  432. if (dsp_row != NULL)
  433. png_combine_row(png_ptr, dsp_row, -1/*ignored*/);
  434. }
  435. png_read_finish_row(png_ptr);
  436. if (png_ptr->read_row_fn != NULL)
  437. (*(png_ptr->read_row_fn))(png_ptr, png_ptr->row_number, png_ptr->pass);
  438. }
  439. #endif /* SEQUENTIAL_READ */
  440. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  441. /* Read one or more rows of image data. If the image is interlaced,
  442. * and png_set_interlace_handling() has been called, the rows need to
  443. * contain the contents of the rows from the previous pass. If the
  444. * image has alpha or transparency, and png_handle_alpha()[*] has been
  445. * called, the rows contents must be initialized to the contents of the
  446. * screen.
  447. *
  448. * "row" holds the actual image, and pixels are placed in it
  449. * as they arrive. If the image is displayed after each pass, it will
  450. * appear to "sparkle" in. "display_row" can be used to display a
  451. * "chunky" progressive image, with finer detail added as it becomes
  452. * available. If you do not want this "chunky" display, you may pass
  453. * NULL for display_row. If you do not want the sparkle display, and
  454. * you have not called png_handle_alpha(), you may pass NULL for rows.
  455. * If you have called png_handle_alpha(), and the image has either an
  456. * alpha channel or a transparency chunk, you must provide a buffer for
  457. * rows. In this case, you do not have to provide a display_row buffer
  458. * also, but you may. If the image is not interlaced, or if you have
  459. * not called png_set_interlace_handling(), the display_row buffer will
  460. * be ignored, so pass NULL to it.
  461. *
  462. * [*] png_handle_alpha() does not exist yet, as of this version of libpng
  463. */
  464. void PNGAPI
  465. png_read_rows(png_structrp png_ptr, png_bytepp row,
  466. png_bytepp display_row, png_uint_32 num_rows)
  467. {
  468. png_uint_32 i;
  469. png_bytepp rp;
  470. png_bytepp dp;
  471. png_debug(1, "in png_read_rows");
  472. if (png_ptr == NULL)
  473. return;
  474. rp = row;
  475. dp = display_row;
  476. if (rp != NULL && dp != NULL)
  477. for (i = 0; i < num_rows; i++)
  478. {
  479. png_bytep rptr = *rp++;
  480. png_bytep dptr = *dp++;
  481. png_read_row(png_ptr, rptr, dptr);
  482. }
  483. else if (rp != NULL)
  484. for (i = 0; i < num_rows; i++)
  485. {
  486. png_bytep rptr = *rp;
  487. png_read_row(png_ptr, rptr, NULL);
  488. rp++;
  489. }
  490. else if (dp != NULL)
  491. for (i = 0; i < num_rows; i++)
  492. {
  493. png_bytep dptr = *dp;
  494. png_read_row(png_ptr, NULL, dptr);
  495. dp++;
  496. }
  497. }
  498. #endif /* SEQUENTIAL_READ */
  499. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  500. /* Read the entire image. If the image has an alpha channel or a tRNS
  501. * chunk, and you have called png_handle_alpha()[*], you will need to
  502. * initialize the image to the current image that PNG will be overlaying.
  503. * We set the num_rows again here, in case it was incorrectly set in
  504. * png_read_start_row() by a call to png_read_update_info() or
  505. * png_start_read_image() if png_set_interlace_handling() wasn't called
  506. * prior to either of these functions like it should have been. You can
  507. * only call this function once. If you desire to have an image for
  508. * each pass of a interlaced image, use png_read_rows() instead.
  509. *
  510. * [*] png_handle_alpha() does not exist yet, as of this version of libpng
  511. */
  512. void PNGAPI
  513. png_read_image(png_structrp png_ptr, png_bytepp image)
  514. {
  515. png_uint_32 i, image_height;
  516. int pass, j;
  517. png_bytepp rp;
  518. png_debug(1, "in png_read_image");
  519. if (png_ptr == NULL)
  520. return;
  521. #ifdef PNG_READ_INTERLACING_SUPPORTED
  522. if ((png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
  523. {
  524. pass = png_set_interlace_handling(png_ptr);
  525. /* And make sure transforms are initialized. */
  526. png_start_read_image(png_ptr);
  527. }
  528. else
  529. {
  530. if (png_ptr->interlaced != 0 &&
  531. (png_ptr->transformations & PNG_INTERLACE) == 0)
  532. {
  533. /* Caller called png_start_read_image or png_read_update_info without
  534. * first turning on the PNG_INTERLACE transform. We can fix this here,
  535. * but the caller should do it!
  536. */
  537. png_warning(png_ptr, "Interlace handling should be turned on when "
  538. "using png_read_image");
  539. /* Make sure this is set correctly */
  540. png_ptr->num_rows = png_ptr->height;
  541. }
  542. /* Obtain the pass number, which also turns on the PNG_INTERLACE flag in
  543. * the above error case.
  544. */
  545. pass = png_set_interlace_handling(png_ptr);
  546. }
  547. #else
  548. if (png_ptr->interlaced)
  549. png_error(png_ptr,
  550. "Cannot read interlaced image -- interlace handler disabled");
  551. pass = 1;
  552. #endif
  553. image_height=png_ptr->height;
  554. for (j = 0; j < pass; j++)
  555. {
  556. rp = image;
  557. for (i = 0; i < image_height; i++)
  558. {
  559. png_read_row(png_ptr, *rp, NULL);
  560. rp++;
  561. }
  562. }
  563. }
  564. #endif /* SEQUENTIAL_READ */
  565. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  566. /* Read the end of the PNG file. Will not read past the end of the
  567. * file, will verify the end is accurate, and will read any comments
  568. * or time information at the end of the file, if info is not NULL.
  569. */
  570. void PNGAPI
  571. png_read_end(png_structrp png_ptr, png_inforp info_ptr)
  572. {
  573. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  574. int keep;
  575. #endif
  576. png_debug(1, "in png_read_end");
  577. if (png_ptr == NULL)
  578. return;
  579. /* If png_read_end is called in the middle of reading the rows there may
  580. * still be pending IDAT data and an owned zstream. Deal with this here.
  581. */
  582. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  583. if (png_chunk_unknown_handling(png_ptr, png_IDAT) == 0)
  584. #endif
  585. png_read_finish_IDAT(png_ptr);
  586. #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
  587. /* Report invalid palette index; added at libng-1.5.10 */
  588. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE &&
  589. png_ptr->num_palette_max >= png_ptr->num_palette)
  590. png_benign_error(png_ptr, "Read palette index exceeding num_palette");
  591. #endif
  592. do
  593. {
  594. png_uint_32 length = png_read_chunk_header(png_ptr);
  595. png_uint_32 chunk_name = png_ptr->chunk_name;
  596. if (chunk_name != png_IDAT)
  597. {
  598. /* These flags must be set consistently for all non-IDAT chunks,
  599. * including the unknown chunks.
  600. */
  601. png_ptr->mode |= PNG_HAVE_CHUNK_AFTER_IDAT | PNG_AFTER_IDAT;
  602. }
  603. if (chunk_name == png_IEND)
  604. png_handle_chunk(png_ptr, info_ptr, length);
  605. else if (chunk_name == png_IHDR)
  606. png_handle_chunk(png_ptr, info_ptr, length);
  607. else if (info_ptr == NULL)
  608. png_crc_finish(png_ptr, length);
  609. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  610. else if ((keep = png_chunk_unknown_handling(png_ptr, chunk_name)) != 0)
  611. {
  612. if (chunk_name == png_IDAT)
  613. {
  614. if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
  615. || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
  616. png_benign_error(png_ptr, ".Too many IDATs found");
  617. }
  618. png_handle_unknown(png_ptr, info_ptr, length, keep);
  619. if (chunk_name == png_PLTE)
  620. png_ptr->mode |= PNG_HAVE_PLTE;
  621. }
  622. #endif
  623. else if (chunk_name == png_IDAT)
  624. {
  625. /* Zero length IDATs are legal after the last IDAT has been
  626. * read, but not after other chunks have been read. 1.6 does not
  627. * always read all the deflate data; specifically it cannot be relied
  628. * upon to read the Adler32 at the end. If it doesn't ignore IDAT
  629. * chunks which are longer than zero as well:
  630. */
  631. if ((length > 0 && !(png_ptr->flags & PNG_FLAG_ZSTREAM_ENDED))
  632. || (png_ptr->mode & PNG_HAVE_CHUNK_AFTER_IDAT) != 0)
  633. png_benign_error(png_ptr, "..Too many IDATs found");
  634. png_crc_finish(png_ptr, length);
  635. }
  636. else
  637. png_handle_chunk(png_ptr, info_ptr, length);
  638. } while ((png_ptr->mode & PNG_HAVE_IEND) == 0);
  639. }
  640. #endif /* SEQUENTIAL_READ */
  641. /* Free all memory used in the read struct */
  642. static void
  643. png_read_destroy(png_structrp png_ptr)
  644. {
  645. png_debug(1, "in png_read_destroy");
  646. #ifdef PNG_READ_GAMMA_SUPPORTED
  647. png_destroy_gamma_table(png_ptr);
  648. #endif
  649. png_free(png_ptr, png_ptr->big_row_buf);
  650. png_ptr->big_row_buf = NULL;
  651. png_free(png_ptr, png_ptr->big_prev_row);
  652. png_ptr->big_prev_row = NULL;
  653. png_free(png_ptr, png_ptr->read_buffer);
  654. png_ptr->read_buffer = NULL;
  655. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  656. png_free(png_ptr, png_ptr->palette_lookup);
  657. png_ptr->palette_lookup = NULL;
  658. png_free(png_ptr, png_ptr->quantize_index);
  659. png_ptr->quantize_index = NULL;
  660. #endif
  661. if ((png_ptr->free_me & PNG_FREE_PLTE) != 0)
  662. {
  663. png_zfree(png_ptr, png_ptr->palette);
  664. png_ptr->palette = NULL;
  665. }
  666. png_ptr->free_me &= ~PNG_FREE_PLTE;
  667. #if defined(PNG_tRNS_SUPPORTED) || \
  668. defined(PNG_READ_EXPAND_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
  669. if ((png_ptr->free_me & PNG_FREE_TRNS) != 0)
  670. {
  671. png_free(png_ptr, png_ptr->trans_alpha);
  672. png_ptr->trans_alpha = NULL;
  673. }
  674. png_ptr->free_me &= ~PNG_FREE_TRNS;
  675. #endif
  676. inflateEnd(&png_ptr->zstream);
  677. #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
  678. png_free(png_ptr, png_ptr->save_buffer);
  679. png_ptr->save_buffer = NULL;
  680. #endif
  681. #if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) && \
  682. defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
  683. png_free(png_ptr, png_ptr->unknown_chunk.data);
  684. png_ptr->unknown_chunk.data = NULL;
  685. #endif
  686. #ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
  687. png_free(png_ptr, png_ptr->chunk_list);
  688. png_ptr->chunk_list = NULL;
  689. #endif
  690. #if defined(PNG_READ_EXPAND_SUPPORTED) && \
  691. (defined(PNG_ARM_NEON_IMPLEMENTATION) || \
  692. defined(PNG_RISCV_RVV_IMPLEMENTATION))
  693. png_free(png_ptr, png_ptr->riffled_palette);
  694. png_ptr->riffled_palette = NULL;
  695. #endif
  696. /* NOTE: the 'setjmp' buffer may still be allocated and the memory and error
  697. * callbacks are still set at this point. They are required to complete the
  698. * destruction of the png_struct itself.
  699. */
  700. }
  701. /* Free all memory used by the read */
  702. void PNGAPI
  703. png_destroy_read_struct(png_structpp png_ptr_ptr, png_infopp info_ptr_ptr,
  704. png_infopp end_info_ptr_ptr)
  705. {
  706. png_structrp png_ptr = NULL;
  707. png_debug(1, "in png_destroy_read_struct");
  708. if (png_ptr_ptr != NULL)
  709. png_ptr = *png_ptr_ptr;
  710. if (png_ptr == NULL)
  711. return;
  712. /* libpng 1.6.0: use the API to destroy info structs to ensure consistent
  713. * behavior. Prior to 1.6.0 libpng did extra 'info' destruction in this API.
  714. * The extra was, apparently, unnecessary yet this hides memory leak bugs.
  715. */
  716. png_destroy_info_struct(png_ptr, end_info_ptr_ptr);
  717. png_destroy_info_struct(png_ptr, info_ptr_ptr);
  718. *png_ptr_ptr = NULL;
  719. png_read_destroy(png_ptr);
  720. png_destroy_png_struct(png_ptr);
  721. }
  722. void PNGAPI
  723. png_set_read_status_fn(png_structrp png_ptr, png_read_status_ptr read_row_fn)
  724. {
  725. if (png_ptr == NULL)
  726. return;
  727. png_ptr->read_row_fn = read_row_fn;
  728. }
  729. #ifdef PNG_SEQUENTIAL_READ_SUPPORTED
  730. #ifdef PNG_INFO_IMAGE_SUPPORTED
  731. void PNGAPI
  732. png_read_png(png_structrp png_ptr, png_inforp info_ptr,
  733. int transforms, voidp params)
  734. {
  735. png_debug(1, "in png_read_png");
  736. if (png_ptr == NULL || info_ptr == NULL)
  737. return;
  738. /* png_read_info() gives us all of the information from the
  739. * PNG file before the first IDAT (image data chunk).
  740. */
  741. png_read_info(png_ptr, info_ptr);
  742. if (info_ptr->height > PNG_UINT_32_MAX/(sizeof (png_bytep)))
  743. png_error(png_ptr, "Image is too high to process with png_read_png()");
  744. /* -------------- image transformations start here ------------------- */
  745. /* libpng 1.6.10: add code to cause a png_app_error if a selected TRANSFORM
  746. * is not implemented. This will only happen in de-configured (non-default)
  747. * libpng builds. The results can be unexpected - png_read_png may return
  748. * short or mal-formed rows because the transform is skipped.
  749. */
  750. /* Tell libpng to strip 16-bit/color files down to 8 bits per color.
  751. */
  752. if ((transforms & PNG_TRANSFORM_SCALE_16) != 0)
  753. /* Added at libpng-1.5.4. "strip_16" produces the same result that it
  754. * did in earlier versions, while "scale_16" is now more accurate.
  755. */
  756. #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  757. png_set_scale_16(png_ptr);
  758. #else
  759. png_app_error(png_ptr, "PNG_TRANSFORM_SCALE_16 not supported");
  760. #endif
  761. /* If both SCALE and STRIP are required pngrtran will effectively cancel the
  762. * latter by doing SCALE first. This is ok and allows apps not to check for
  763. * which is supported to get the right answer.
  764. */
  765. if ((transforms & PNG_TRANSFORM_STRIP_16) != 0)
  766. #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
  767. png_set_strip_16(png_ptr);
  768. #else
  769. png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_16 not supported");
  770. #endif
  771. /* Strip alpha bytes from the input data without combining with
  772. * the background (not recommended).
  773. */
  774. if ((transforms & PNG_TRANSFORM_STRIP_ALPHA) != 0)
  775. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  776. png_set_strip_alpha(png_ptr);
  777. #else
  778. png_app_error(png_ptr, "PNG_TRANSFORM_STRIP_ALPHA not supported");
  779. #endif
  780. /* Extract multiple pixels with bit depths of 1, 2, or 4 from a single
  781. * byte into separate bytes (useful for paletted and grayscale images).
  782. */
  783. if ((transforms & PNG_TRANSFORM_PACKING) != 0)
  784. #ifdef PNG_READ_PACK_SUPPORTED
  785. png_set_packing(png_ptr);
  786. #else
  787. png_app_error(png_ptr, "PNG_TRANSFORM_PACKING not supported");
  788. #endif
  789. /* Change the order of packed pixels to least significant bit first
  790. * (not useful if you are using png_set_packing).
  791. */
  792. if ((transforms & PNG_TRANSFORM_PACKSWAP) != 0)
  793. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  794. png_set_packswap(png_ptr);
  795. #else
  796. png_app_error(png_ptr, "PNG_TRANSFORM_PACKSWAP not supported");
  797. #endif
  798. /* Expand paletted colors into true RGB triplets
  799. * Expand grayscale images to full 8 bits from 1, 2, or 4 bits/pixel
  800. * Expand paletted or RGB images with transparency to full alpha
  801. * channels so the data will be available as RGBA quartets.
  802. */
  803. if ((transforms & PNG_TRANSFORM_EXPAND) != 0)
  804. #ifdef PNG_READ_EXPAND_SUPPORTED
  805. png_set_expand(png_ptr);
  806. #else
  807. png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND not supported");
  808. #endif
  809. /* We don't handle background color or gamma transformation or quantizing.
  810. */
  811. /* Invert monochrome files to have 0 as white and 1 as black
  812. */
  813. if ((transforms & PNG_TRANSFORM_INVERT_MONO) != 0)
  814. #ifdef PNG_READ_INVERT_SUPPORTED
  815. png_set_invert_mono(png_ptr);
  816. #else
  817. png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_MONO not supported");
  818. #endif
  819. /* If you want to shift the pixel values from the range [0,255] or
  820. * [0,65535] to the original [0,7] or [0,31], or whatever range the
  821. * colors were originally in:
  822. */
  823. if ((transforms & PNG_TRANSFORM_SHIFT) != 0)
  824. #ifdef PNG_READ_SHIFT_SUPPORTED
  825. if ((info_ptr->valid & PNG_INFO_sBIT) != 0)
  826. png_set_shift(png_ptr, &info_ptr->sig_bit);
  827. #else
  828. png_app_error(png_ptr, "PNG_TRANSFORM_SHIFT not supported");
  829. #endif
  830. /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
  831. if ((transforms & PNG_TRANSFORM_BGR) != 0)
  832. #ifdef PNG_READ_BGR_SUPPORTED
  833. png_set_bgr(png_ptr);
  834. #else
  835. png_app_error(png_ptr, "PNG_TRANSFORM_BGR not supported");
  836. #endif
  837. /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
  838. if ((transforms & PNG_TRANSFORM_SWAP_ALPHA) != 0)
  839. #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
  840. png_set_swap_alpha(png_ptr);
  841. #else
  842. png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ALPHA not supported");
  843. #endif
  844. /* Swap bytes of 16-bit files to least significant byte first */
  845. if ((transforms & PNG_TRANSFORM_SWAP_ENDIAN) != 0)
  846. #ifdef PNG_READ_SWAP_SUPPORTED
  847. png_set_swap(png_ptr);
  848. #else
  849. png_app_error(png_ptr, "PNG_TRANSFORM_SWAP_ENDIAN not supported");
  850. #endif
  851. /* Added at libpng-1.2.41 */
  852. /* Invert the alpha channel from opacity to transparency */
  853. if ((transforms & PNG_TRANSFORM_INVERT_ALPHA) != 0)
  854. #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
  855. png_set_invert_alpha(png_ptr);
  856. #else
  857. png_app_error(png_ptr, "PNG_TRANSFORM_INVERT_ALPHA not supported");
  858. #endif
  859. /* Added at libpng-1.2.41 */
  860. /* Expand grayscale image to RGB */
  861. if ((transforms & PNG_TRANSFORM_GRAY_TO_RGB) != 0)
  862. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  863. png_set_gray_to_rgb(png_ptr);
  864. #else
  865. png_app_error(png_ptr, "PNG_TRANSFORM_GRAY_TO_RGB not supported");
  866. #endif
  867. /* Added at libpng-1.5.4 */
  868. if ((transforms & PNG_TRANSFORM_EXPAND_16) != 0)
  869. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  870. png_set_expand_16(png_ptr);
  871. #else
  872. png_app_error(png_ptr, "PNG_TRANSFORM_EXPAND_16 not supported");
  873. #endif
  874. /* We don't handle adding filler bytes */
  875. /* We use png_read_image and rely on that for interlace handling, but we also
  876. * call png_read_update_info therefore must turn on interlace handling now:
  877. */
  878. (void)png_set_interlace_handling(png_ptr);
  879. /* Optional call to gamma correct and add the background to the palette
  880. * and update info structure. REQUIRED if you are expecting libpng to
  881. * update the palette for you (i.e., you selected such a transform above).
  882. */
  883. png_read_update_info(png_ptr, info_ptr);
  884. /* -------------- image transformations end here ------------------- */
  885. png_free_data(png_ptr, info_ptr, PNG_FREE_ROWS, 0);
  886. if (info_ptr->row_pointers == NULL)
  887. {
  888. png_uint_32 iptr;
  889. info_ptr->row_pointers = png_voidcast(png_bytepp, png_malloc(png_ptr,
  890. info_ptr->height * (sizeof (png_bytep))));
  891. for (iptr=0; iptr<info_ptr->height; iptr++)
  892. info_ptr->row_pointers[iptr] = NULL;
  893. info_ptr->free_me |= PNG_FREE_ROWS;
  894. for (iptr = 0; iptr < info_ptr->height; iptr++)
  895. info_ptr->row_pointers[iptr] = png_voidcast(png_bytep,
  896. png_malloc(png_ptr, info_ptr->rowbytes));
  897. }
  898. png_read_image(png_ptr, info_ptr->row_pointers);
  899. info_ptr->valid |= PNG_INFO_IDAT;
  900. /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
  901. png_read_end(png_ptr, info_ptr);
  902. PNG_UNUSED(params)
  903. }
  904. #endif /* INFO_IMAGE */
  905. #endif /* SEQUENTIAL_READ */
  906. #ifdef PNG_SIMPLIFIED_READ_SUPPORTED
  907. /* SIMPLIFIED READ
  908. *
  909. * This code currently relies on the sequential reader, though it could easily
  910. * be made to work with the progressive one.
  911. */
  912. /* Arguments to png_image_finish_read: */
  913. /* Encoding of PNG data (used by the color-map code) */
  914. # define P_NOTSET 0 /* File encoding not yet known */
  915. # define P_sRGB 1 /* 8-bit encoded to sRGB gamma */
  916. # define P_LINEAR 2 /* 16-bit linear: not encoded, NOT pre-multiplied! */
  917. # define P_FILE 3 /* 8-bit encoded to file gamma, not sRGB or linear */
  918. # define P_LINEAR8 4 /* 8-bit linear: only from a file value */
  919. /* Color-map processing: after libpng has run on the PNG image further
  920. * processing may be needed to convert the data to color-map indices.
  921. */
  922. #define PNG_CMAP_NONE 0
  923. #define PNG_CMAP_GA 1 /* Process GA data to a color-map with alpha */
  924. #define PNG_CMAP_TRANS 2 /* Process GA data to a background index */
  925. #define PNG_CMAP_RGB 3 /* Process RGB data */
  926. #define PNG_CMAP_RGB_ALPHA 4 /* Process RGBA data */
  927. /* The following document where the background is for each processing case. */
  928. #define PNG_CMAP_NONE_BACKGROUND 256
  929. #define PNG_CMAP_GA_BACKGROUND 231
  930. #define PNG_CMAP_TRANS_BACKGROUND 254
  931. #define PNG_CMAP_RGB_BACKGROUND 256
  932. #define PNG_CMAP_RGB_ALPHA_BACKGROUND 216
  933. typedef struct
  934. {
  935. /* Arguments: */
  936. png_imagep image;
  937. png_voidp buffer;
  938. png_int_32 row_stride;
  939. png_voidp colormap;
  940. png_const_colorp background;
  941. /* Local variables: */
  942. png_voidp local_row;
  943. png_voidp first_row;
  944. ptrdiff_t row_bytes; /* step between rows */
  945. int file_encoding; /* E_ values above */
  946. png_fixed_point gamma_to_linear; /* For P_FILE, reciprocal of gamma */
  947. int colormap_processing; /* PNG_CMAP_ values above */
  948. } png_image_read_control;
  949. /* Do all the *safe* initialization - 'safe' means that png_error won't be
  950. * called, so setting up the jmp_buf is not required. This means that anything
  951. * called from here must *not* call png_malloc - it has to call png_malloc_warn
  952. * instead so that control is returned safely back to this routine.
  953. */
  954. static int
  955. png_image_read_init(png_imagep image)
  956. {
  957. if (image->opaque == NULL)
  958. {
  959. png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, image,
  960. png_safe_error, png_safe_warning);
  961. /* And set the rest of the structure to NULL to ensure that the various
  962. * fields are consistent.
  963. */
  964. memset(image, 0, (sizeof *image));
  965. image->version = PNG_IMAGE_VERSION;
  966. if (png_ptr != NULL)
  967. {
  968. png_infop info_ptr = png_create_info_struct(png_ptr);
  969. if (info_ptr != NULL)
  970. {
  971. png_controlp control = png_voidcast(png_controlp,
  972. png_malloc_warn(png_ptr, (sizeof *control)));
  973. if (control != NULL)
  974. {
  975. memset(control, 0, (sizeof *control));
  976. control->png_ptr = png_ptr;
  977. control->info_ptr = info_ptr;
  978. control->for_write = 0;
  979. image->opaque = control;
  980. return 1;
  981. }
  982. /* Error clean up */
  983. png_destroy_info_struct(png_ptr, &info_ptr);
  984. }
  985. png_destroy_read_struct(&png_ptr, NULL, NULL);
  986. }
  987. return png_image_error(image, "png_image_read: out of memory");
  988. }
  989. return png_image_error(image, "png_image_read: opaque pointer not NULL");
  990. }
  991. /* Utility to find the base format of a PNG file from a png_struct. */
  992. static png_uint_32
  993. png_image_format(png_structrp png_ptr)
  994. {
  995. png_uint_32 format = 0;
  996. if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
  997. format |= PNG_FORMAT_FLAG_COLOR;
  998. if ((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
  999. format |= PNG_FORMAT_FLAG_ALPHA;
  1000. /* Use png_ptr here, not info_ptr, because by examination png_handle_tRNS
  1001. * sets the png_struct fields; that's all we are interested in here. The
  1002. * precise interaction with an app call to png_set_tRNS and PNG file reading
  1003. * is unclear.
  1004. */
  1005. else if (png_ptr->num_trans > 0)
  1006. format |= PNG_FORMAT_FLAG_ALPHA;
  1007. if (png_ptr->bit_depth == 16)
  1008. format |= PNG_FORMAT_FLAG_LINEAR;
  1009. if ((png_ptr->color_type & PNG_COLOR_MASK_PALETTE) != 0)
  1010. format |= PNG_FORMAT_FLAG_COLORMAP;
  1011. return format;
  1012. }
  1013. static int
  1014. chromaticities_match_sRGB(const png_xy *xy)
  1015. {
  1016. # define sRGB_TOLERANCE 1000
  1017. static const png_xy sRGB_xy = /* From ITU-R BT.709-3 */
  1018. {
  1019. /* color x y */
  1020. /* red */ 64000, 33000,
  1021. /* green */ 30000, 60000,
  1022. /* blue */ 15000, 6000,
  1023. /* white */ 31270, 32900
  1024. };
  1025. if (PNG_OUT_OF_RANGE(xy->whitex, sRGB_xy.whitex,sRGB_TOLERANCE) ||
  1026. PNG_OUT_OF_RANGE(xy->whitey, sRGB_xy.whitey,sRGB_TOLERANCE) ||
  1027. PNG_OUT_OF_RANGE(xy->redx, sRGB_xy.redx, sRGB_TOLERANCE) ||
  1028. PNG_OUT_OF_RANGE(xy->redy, sRGB_xy.redy, sRGB_TOLERANCE) ||
  1029. PNG_OUT_OF_RANGE(xy->greenx, sRGB_xy.greenx,sRGB_TOLERANCE) ||
  1030. PNG_OUT_OF_RANGE(xy->greeny, sRGB_xy.greeny,sRGB_TOLERANCE) ||
  1031. PNG_OUT_OF_RANGE(xy->bluex, sRGB_xy.bluex, sRGB_TOLERANCE) ||
  1032. PNG_OUT_OF_RANGE(xy->bluey, sRGB_xy.bluey, sRGB_TOLERANCE))
  1033. return 0;
  1034. return 1;
  1035. }
  1036. /* Is the given gamma significantly different from sRGB? The test is the same
  1037. * one used in pngrtran.c when deciding whether to do gamma correction. The
  1038. * arithmetic optimizes the division by using the fact that the inverse of the
  1039. * file sRGB gamma is 2.2
  1040. */
  1041. static int
  1042. png_gamma_not_sRGB(png_fixed_point g)
  1043. {
  1044. /* 1.6.47: use the same sanity checks as used in pngrtran.c */
  1045. if (g < PNG_LIB_GAMMA_MIN || g > PNG_LIB_GAMMA_MAX)
  1046. return 0; /* Includes the uninitialized value 0 */
  1047. return png_gamma_significant((g * 11 + 2)/5 /* i.e. *2.2, rounded */);
  1048. }
  1049. /* Do the main body of a 'png_image_begin_read' function; read the PNG file
  1050. * header and fill in all the information. This is executed in a safe context,
  1051. * unlike the init routine above.
  1052. */
  1053. static int
  1054. png_image_is_not_sRGB(png_const_structrp png_ptr)
  1055. {
  1056. /* Does the colorspace **not** match sRGB? The flag is only set if the
  1057. * answer can be determined reliably.
  1058. *
  1059. * png_struct::chromaticities always exists since the simplified API
  1060. * requires rgb-to-gray. The mDCV, cICP and cHRM chunks may all set it to
  1061. * a non-sRGB value, so it needs to be checked but **only** if one of
  1062. * those chunks occured in the file.
  1063. */
  1064. /* Highest priority: check to be safe. */
  1065. if (png_has_chunk(png_ptr, cICP) || png_has_chunk(png_ptr, mDCV))
  1066. return !chromaticities_match_sRGB(&png_ptr->chromaticities);
  1067. /* If the image is marked as sRGB then it is... */
  1068. if (png_has_chunk(png_ptr, sRGB))
  1069. return 0;
  1070. /* Last stop: cHRM, must check: */
  1071. if (png_has_chunk(png_ptr, cHRM))
  1072. return !chromaticities_match_sRGB(&png_ptr->chromaticities);
  1073. /* Else default to sRGB */
  1074. return 0;
  1075. }
  1076. static int
  1077. png_image_read_header(png_voidp argument)
  1078. {
  1079. png_imagep image = png_voidcast(png_imagep, argument);
  1080. png_structrp png_ptr = image->opaque->png_ptr;
  1081. png_inforp info_ptr = image->opaque->info_ptr;
  1082. #ifdef PNG_BENIGN_ERRORS_SUPPORTED
  1083. png_set_benign_errors(png_ptr, 1/*warn*/);
  1084. #endif
  1085. png_read_info(png_ptr, info_ptr);
  1086. /* Do this the fast way; just read directly out of png_struct. */
  1087. image->width = png_ptr->width;
  1088. image->height = png_ptr->height;
  1089. {
  1090. png_uint_32 format = png_image_format(png_ptr);
  1091. image->format = format;
  1092. /* Greyscale images don't (typically) have colour space information and
  1093. * using it is pretty much impossible, so use sRGB for grayscale (it
  1094. * doesn't matter r==g==b so the transform is irrelevant.)
  1095. */
  1096. if ((format & PNG_FORMAT_FLAG_COLOR) != 0 &&
  1097. png_image_is_not_sRGB(png_ptr))
  1098. image->flags |= PNG_IMAGE_FLAG_COLORSPACE_NOT_sRGB;
  1099. }
  1100. /* We need the maximum number of entries regardless of the format the
  1101. * application sets here.
  1102. */
  1103. {
  1104. png_uint_32 cmap_entries;
  1105. switch (png_ptr->color_type)
  1106. {
  1107. case PNG_COLOR_TYPE_GRAY:
  1108. cmap_entries = 1U << png_ptr->bit_depth;
  1109. break;
  1110. case PNG_COLOR_TYPE_PALETTE:
  1111. cmap_entries = (png_uint_32)png_ptr->num_palette;
  1112. break;
  1113. default:
  1114. cmap_entries = 256;
  1115. break;
  1116. }
  1117. if (cmap_entries > 256)
  1118. cmap_entries = 256;
  1119. image->colormap_entries = cmap_entries;
  1120. }
  1121. return 1;
  1122. }
  1123. #ifdef PNG_STDIO_SUPPORTED
  1124. int PNGAPI
  1125. png_image_begin_read_from_stdio(png_imagep image, FILE *file)
  1126. {
  1127. if (image != NULL && image->version == PNG_IMAGE_VERSION)
  1128. {
  1129. if (file != NULL)
  1130. {
  1131. if (png_image_read_init(image) != 0)
  1132. {
  1133. /* This is slightly evil, but png_init_io doesn't do anything other
  1134. * than this and we haven't changed the standard IO functions so
  1135. * this saves a 'safe' function.
  1136. */
  1137. image->opaque->png_ptr->io_ptr = file;
  1138. return png_safe_execute(image, png_image_read_header, image);
  1139. }
  1140. }
  1141. else
  1142. return png_image_error(image,
  1143. "png_image_begin_read_from_stdio: invalid argument");
  1144. }
  1145. else if (image != NULL)
  1146. return png_image_error(image,
  1147. "png_image_begin_read_from_stdio: incorrect PNG_IMAGE_VERSION");
  1148. return 0;
  1149. }
  1150. int PNGAPI
  1151. png_image_begin_read_from_file(png_imagep image, const char *file_name)
  1152. {
  1153. if (image != NULL && image->version == PNG_IMAGE_VERSION)
  1154. {
  1155. if (file_name != NULL)
  1156. {
  1157. FILE *fp = fopen(file_name, "rb");
  1158. if (fp != NULL)
  1159. {
  1160. if (png_image_read_init(image) != 0)
  1161. {
  1162. image->opaque->png_ptr->io_ptr = fp;
  1163. image->opaque->owned_file = 1;
  1164. return png_safe_execute(image, png_image_read_header, image);
  1165. }
  1166. /* Clean up: just the opened file. */
  1167. (void)fclose(fp);
  1168. }
  1169. else
  1170. return png_image_error(image, strerror(errno));
  1171. }
  1172. else
  1173. return png_image_error(image,
  1174. "png_image_begin_read_from_file: invalid argument");
  1175. }
  1176. else if (image != NULL)
  1177. return png_image_error(image,
  1178. "png_image_begin_read_from_file: incorrect PNG_IMAGE_VERSION");
  1179. return 0;
  1180. }
  1181. #endif /* STDIO */
  1182. static void PNGCBAPI
  1183. png_image_memory_read(png_structp png_ptr, png_bytep out, size_t need)
  1184. {
  1185. if (png_ptr != NULL)
  1186. {
  1187. png_imagep image = png_voidcast(png_imagep, png_ptr->io_ptr);
  1188. if (image != NULL)
  1189. {
  1190. png_controlp cp = image->opaque;
  1191. if (cp != NULL)
  1192. {
  1193. png_const_bytep memory = cp->memory;
  1194. size_t size = cp->size;
  1195. if (memory != NULL && size >= need)
  1196. {
  1197. memcpy(out, memory, need);
  1198. cp->memory = memory + need;
  1199. cp->size = size - need;
  1200. return;
  1201. }
  1202. png_error(png_ptr, "read beyond end of data");
  1203. }
  1204. }
  1205. png_error(png_ptr, "invalid memory read");
  1206. }
  1207. }
  1208. int PNGAPI png_image_begin_read_from_memory(png_imagep image,
  1209. png_const_voidp memory, size_t size)
  1210. {
  1211. if (image != NULL && image->version == PNG_IMAGE_VERSION)
  1212. {
  1213. if (memory != NULL && size > 0)
  1214. {
  1215. if (png_image_read_init(image) != 0)
  1216. {
  1217. /* Now set the IO functions to read from the memory buffer and
  1218. * store it into io_ptr. Again do this in-place to avoid calling a
  1219. * libpng function that requires error handling.
  1220. */
  1221. image->opaque->memory = png_voidcast(png_const_bytep, memory);
  1222. image->opaque->size = size;
  1223. image->opaque->png_ptr->io_ptr = image;
  1224. image->opaque->png_ptr->read_data_fn = png_image_memory_read;
  1225. return png_safe_execute(image, png_image_read_header, image);
  1226. }
  1227. }
  1228. else
  1229. return png_image_error(image,
  1230. "png_image_begin_read_from_memory: invalid argument");
  1231. }
  1232. else if (image != NULL)
  1233. return png_image_error(image,
  1234. "png_image_begin_read_from_memory: incorrect PNG_IMAGE_VERSION");
  1235. return 0;
  1236. }
  1237. /* Utility function to skip chunks that are not used by the simplified image
  1238. * read functions and an appropriate macro to call it.
  1239. */
  1240. #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED
  1241. static void
  1242. png_image_skip_unused_chunks(png_structrp png_ptr)
  1243. {
  1244. /* Prepare the reader to ignore all recognized chunks whose data will not
  1245. * be used, i.e., all chunks recognized by libpng except for those
  1246. * involved in basic image reading:
  1247. *
  1248. * IHDR, PLTE, IDAT, IEND
  1249. *
  1250. * Or image data handling:
  1251. *
  1252. * tRNS, bKGD, gAMA, cHRM, sRGB, [iCCP] and sBIT.
  1253. *
  1254. * This provides a small performance improvement and eliminates any
  1255. * potential vulnerability to security problems in the unused chunks.
  1256. *
  1257. * At present the iCCP chunk data isn't used, so iCCP chunk can be ignored
  1258. * too. This allows the simplified API to be compiled without iCCP support.
  1259. */
  1260. {
  1261. static const png_byte chunks_to_process[] = {
  1262. 98, 75, 71, 68, '\0', /* bKGD */
  1263. 99, 72, 82, 77, '\0', /* cHRM */
  1264. 99, 73, 67, 80, '\0', /* cICP */
  1265. 103, 65, 77, 65, '\0', /* gAMA */
  1266. 109, 68, 67, 86, '\0', /* mDCV */
  1267. 115, 66, 73, 84, '\0', /* sBIT */
  1268. 115, 82, 71, 66, '\0', /* sRGB */
  1269. };
  1270. /* Ignore unknown chunks and all other chunks except for the
  1271. * IHDR, PLTE, tRNS, IDAT, and IEND chunks.
  1272. */
  1273. png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_NEVER,
  1274. NULL, -1);
  1275. /* But do not ignore image data handling chunks */
  1276. png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_AS_DEFAULT,
  1277. chunks_to_process, (int)/*SAFE*/(sizeof chunks_to_process)/5);
  1278. }
  1279. }
  1280. # define PNG_SKIP_CHUNKS(p) png_image_skip_unused_chunks(p)
  1281. #else
  1282. # define PNG_SKIP_CHUNKS(p) ((void)0)
  1283. #endif /* HANDLE_AS_UNKNOWN */
  1284. /* The following macro gives the exact rounded answer for all values in the
  1285. * range 0..255 (it actually divides by 51.2, but the rounding still generates
  1286. * the correct numbers 0..5
  1287. */
  1288. #define PNG_DIV51(v8) (((v8) * 5 + 130) >> 8)
  1289. /* Utility functions to make particular color-maps */
  1290. static void
  1291. set_file_encoding(png_image_read_control *display)
  1292. {
  1293. png_structrp png_ptr = display->image->opaque->png_ptr;
  1294. png_fixed_point g = png_resolve_file_gamma(png_ptr);
  1295. /* PNGv3: the result may be 0 however the 'default_gamma' should have been
  1296. * set before this is called so zero is an error:
  1297. */
  1298. if (g == 0)
  1299. png_error(png_ptr, "internal: default gamma not set");
  1300. if (png_gamma_significant(g) != 0)
  1301. {
  1302. if (png_gamma_not_sRGB(g) != 0)
  1303. {
  1304. display->file_encoding = P_FILE;
  1305. display->gamma_to_linear = png_reciprocal(g);
  1306. }
  1307. else
  1308. display->file_encoding = P_sRGB;
  1309. }
  1310. else
  1311. display->file_encoding = P_LINEAR8;
  1312. }
  1313. static unsigned int
  1314. decode_gamma(png_image_read_control *display, png_uint_32 value, int encoding)
  1315. {
  1316. if (encoding == P_FILE) /* double check */
  1317. encoding = display->file_encoding;
  1318. if (encoding == P_NOTSET) /* must be the file encoding */
  1319. {
  1320. set_file_encoding(display);
  1321. encoding = display->file_encoding;
  1322. }
  1323. switch (encoding)
  1324. {
  1325. case P_FILE:
  1326. value = png_gamma_16bit_correct(value*257, display->gamma_to_linear);
  1327. break;
  1328. case P_sRGB:
  1329. value = png_sRGB_table[value];
  1330. break;
  1331. case P_LINEAR:
  1332. break;
  1333. case P_LINEAR8:
  1334. value *= 257;
  1335. break;
  1336. #ifdef __GNUC__
  1337. default:
  1338. png_error(display->image->opaque->png_ptr,
  1339. "unexpected encoding (internal error)");
  1340. #endif
  1341. }
  1342. return value;
  1343. }
  1344. static png_uint_32
  1345. png_colormap_compose(png_image_read_control *display,
  1346. png_uint_32 foreground, int foreground_encoding, png_uint_32 alpha,
  1347. png_uint_32 background, int encoding)
  1348. {
  1349. /* The file value is composed on the background, the background has the given
  1350. * encoding and so does the result, the file is encoded with P_FILE and the
  1351. * file and alpha are 8-bit values. The (output) encoding will always be
  1352. * P_LINEAR or P_sRGB.
  1353. */
  1354. png_uint_32 f = decode_gamma(display, foreground, foreground_encoding);
  1355. png_uint_32 b = decode_gamma(display, background, encoding);
  1356. /* The alpha is always an 8-bit value (it comes from the palette), the value
  1357. * scaled by 255 is what PNG_sRGB_FROM_LINEAR requires.
  1358. */
  1359. f = f * alpha + b * (255-alpha);
  1360. if (encoding == P_LINEAR)
  1361. {
  1362. /* Scale to 65535; divide by 255, approximately (in fact this is extremely
  1363. * accurate, it divides by 255.00000005937181414556, with no overflow.)
  1364. */
  1365. f *= 257; /* Now scaled by 65535 */
  1366. f += f >> 16;
  1367. f = (f+32768) >> 16;
  1368. }
  1369. else /* P_sRGB */
  1370. f = PNG_sRGB_FROM_LINEAR(f);
  1371. return f;
  1372. }
  1373. /* NOTE: P_LINEAR values to this routine must be 16-bit, but P_FILE values must
  1374. * be 8-bit.
  1375. */
  1376. static void
  1377. png_create_colormap_entry(png_image_read_control *display,
  1378. png_uint_32 ip, png_uint_32 red, png_uint_32 green, png_uint_32 blue,
  1379. png_uint_32 alpha, int encoding)
  1380. {
  1381. png_imagep image = display->image;
  1382. int output_encoding = (image->format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
  1383. P_LINEAR : P_sRGB;
  1384. int convert_to_Y = (image->format & PNG_FORMAT_FLAG_COLOR) == 0 &&
  1385. (red != green || green != blue);
  1386. if (ip > 255)
  1387. png_error(image->opaque->png_ptr, "color-map index out of range");
  1388. /* Update the cache with whether the file gamma is significantly different
  1389. * from sRGB.
  1390. */
  1391. if (encoding == P_FILE)
  1392. {
  1393. if (display->file_encoding == P_NOTSET)
  1394. set_file_encoding(display);
  1395. /* Note that the cached value may be P_FILE too, but if it is then the
  1396. * gamma_to_linear member has been set.
  1397. */
  1398. encoding = display->file_encoding;
  1399. }
  1400. if (encoding == P_FILE)
  1401. {
  1402. png_fixed_point g = display->gamma_to_linear;
  1403. red = png_gamma_16bit_correct(red*257, g);
  1404. green = png_gamma_16bit_correct(green*257, g);
  1405. blue = png_gamma_16bit_correct(blue*257, g);
  1406. if (convert_to_Y != 0 || output_encoding == P_LINEAR)
  1407. {
  1408. alpha *= 257;
  1409. encoding = P_LINEAR;
  1410. }
  1411. else
  1412. {
  1413. red = PNG_sRGB_FROM_LINEAR(red * 255);
  1414. green = PNG_sRGB_FROM_LINEAR(green * 255);
  1415. blue = PNG_sRGB_FROM_LINEAR(blue * 255);
  1416. encoding = P_sRGB;
  1417. }
  1418. }
  1419. else if (encoding == P_LINEAR8)
  1420. {
  1421. /* This encoding occurs quite frequently in test cases because PngSuite
  1422. * includes a gAMA 1.0 chunk with most images.
  1423. */
  1424. red *= 257;
  1425. green *= 257;
  1426. blue *= 257;
  1427. alpha *= 257;
  1428. encoding = P_LINEAR;
  1429. }
  1430. else if (encoding == P_sRGB &&
  1431. (convert_to_Y != 0 || output_encoding == P_LINEAR))
  1432. {
  1433. /* The values are 8-bit sRGB values, but must be converted to 16-bit
  1434. * linear.
  1435. */
  1436. red = png_sRGB_table[red];
  1437. green = png_sRGB_table[green];
  1438. blue = png_sRGB_table[blue];
  1439. alpha *= 257;
  1440. encoding = P_LINEAR;
  1441. }
  1442. /* This is set if the color isn't gray but the output is. */
  1443. if (encoding == P_LINEAR)
  1444. {
  1445. if (convert_to_Y != 0)
  1446. {
  1447. /* NOTE: these values are copied from png_do_rgb_to_gray */
  1448. png_uint_32 y = (png_uint_32)6968 * red + (png_uint_32)23434 * green +
  1449. (png_uint_32)2366 * blue;
  1450. if (output_encoding == P_LINEAR)
  1451. y = (y + 16384) >> 15;
  1452. else
  1453. {
  1454. /* y is scaled by 32768, we need it scaled by 255: */
  1455. y = (y + 128) >> 8;
  1456. y *= 255;
  1457. y = PNG_sRGB_FROM_LINEAR((y + 64) >> 7);
  1458. alpha = PNG_DIV257(alpha);
  1459. encoding = P_sRGB;
  1460. }
  1461. blue = red = green = y;
  1462. }
  1463. else if (output_encoding == P_sRGB)
  1464. {
  1465. red = PNG_sRGB_FROM_LINEAR(red * 255);
  1466. green = PNG_sRGB_FROM_LINEAR(green * 255);
  1467. blue = PNG_sRGB_FROM_LINEAR(blue * 255);
  1468. alpha = PNG_DIV257(alpha);
  1469. encoding = P_sRGB;
  1470. }
  1471. }
  1472. if (encoding != output_encoding)
  1473. png_error(image->opaque->png_ptr, "bad encoding (internal error)");
  1474. /* Store the value. */
  1475. {
  1476. # ifdef PNG_FORMAT_AFIRST_SUPPORTED
  1477. int afirst = (image->format & PNG_FORMAT_FLAG_AFIRST) != 0 &&
  1478. (image->format & PNG_FORMAT_FLAG_ALPHA) != 0;
  1479. # else
  1480. # define afirst 0
  1481. # endif
  1482. # ifdef PNG_FORMAT_BGR_SUPPORTED
  1483. int bgr = (image->format & PNG_FORMAT_FLAG_BGR) != 0 ? 2 : 0;
  1484. # else
  1485. # define bgr 0
  1486. # endif
  1487. if (output_encoding == P_LINEAR)
  1488. {
  1489. png_uint_16p entry = png_voidcast(png_uint_16p, display->colormap);
  1490. entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
  1491. /* The linear 16-bit values must be pre-multiplied by the alpha channel
  1492. * value, if less than 65535 (this is, effectively, composite on black
  1493. * if the alpha channel is removed.)
  1494. */
  1495. switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
  1496. {
  1497. case 4:
  1498. entry[afirst ? 0 : 3] = (png_uint_16)alpha;
  1499. /* FALLTHROUGH */
  1500. case 3:
  1501. if (alpha < 65535)
  1502. {
  1503. if (alpha > 0)
  1504. {
  1505. blue = (blue * alpha + 32767U)/65535U;
  1506. green = (green * alpha + 32767U)/65535U;
  1507. red = (red * alpha + 32767U)/65535U;
  1508. }
  1509. else
  1510. red = green = blue = 0;
  1511. }
  1512. entry[afirst + (2 ^ bgr)] = (png_uint_16)blue;
  1513. entry[afirst + 1] = (png_uint_16)green;
  1514. entry[afirst + bgr] = (png_uint_16)red;
  1515. break;
  1516. case 2:
  1517. entry[1 ^ afirst] = (png_uint_16)alpha;
  1518. /* FALLTHROUGH */
  1519. case 1:
  1520. if (alpha < 65535)
  1521. {
  1522. if (alpha > 0)
  1523. green = (green * alpha + 32767U)/65535U;
  1524. else
  1525. green = 0;
  1526. }
  1527. entry[afirst] = (png_uint_16)green;
  1528. break;
  1529. default:
  1530. break;
  1531. }
  1532. }
  1533. else /* output encoding is P_sRGB */
  1534. {
  1535. png_bytep entry = png_voidcast(png_bytep, display->colormap);
  1536. entry += ip * PNG_IMAGE_SAMPLE_CHANNELS(image->format);
  1537. switch (PNG_IMAGE_SAMPLE_CHANNELS(image->format))
  1538. {
  1539. case 4:
  1540. entry[afirst ? 0 : 3] = (png_byte)alpha;
  1541. /* FALLTHROUGH */
  1542. case 3:
  1543. entry[afirst + (2 ^ bgr)] = (png_byte)blue;
  1544. entry[afirst + 1] = (png_byte)green;
  1545. entry[afirst + bgr] = (png_byte)red;
  1546. break;
  1547. case 2:
  1548. entry[1 ^ afirst] = (png_byte)alpha;
  1549. /* FALLTHROUGH */
  1550. case 1:
  1551. entry[afirst] = (png_byte)green;
  1552. break;
  1553. default:
  1554. break;
  1555. }
  1556. }
  1557. # ifdef afirst
  1558. # undef afirst
  1559. # endif
  1560. # ifdef bgr
  1561. # undef bgr
  1562. # endif
  1563. }
  1564. }
  1565. static int
  1566. make_gray_file_colormap(png_image_read_control *display)
  1567. {
  1568. unsigned int i;
  1569. for (i=0; i<256; ++i)
  1570. png_create_colormap_entry(display, i, i, i, i, 255, P_FILE);
  1571. return (int)i;
  1572. }
  1573. static int
  1574. make_gray_colormap(png_image_read_control *display)
  1575. {
  1576. unsigned int i;
  1577. for (i=0; i<256; ++i)
  1578. png_create_colormap_entry(display, i, i, i, i, 255, P_sRGB);
  1579. return (int)i;
  1580. }
  1581. #define PNG_GRAY_COLORMAP_ENTRIES 256
  1582. static int
  1583. make_ga_colormap(png_image_read_control *display)
  1584. {
  1585. unsigned int i, a;
  1586. /* Alpha is retained, the output will be a color-map with entries
  1587. * selected by six levels of alpha. One transparent entry, 6 gray
  1588. * levels for all the intermediate alpha values, leaving 230 entries
  1589. * for the opaque grays. The color-map entries are the six values
  1590. * [0..5]*51, the GA processing uses PNG_DIV51(value) to find the
  1591. * relevant entry.
  1592. *
  1593. * if (alpha > 229) // opaque
  1594. * {
  1595. * // The 231 entries are selected to make the math below work:
  1596. * base = 0;
  1597. * entry = (231 * gray + 128) >> 8;
  1598. * }
  1599. * else if (alpha < 26) // transparent
  1600. * {
  1601. * base = 231;
  1602. * entry = 0;
  1603. * }
  1604. * else // partially opaque
  1605. * {
  1606. * base = 226 + 6 * PNG_DIV51(alpha);
  1607. * entry = PNG_DIV51(gray);
  1608. * }
  1609. */
  1610. i = 0;
  1611. while (i < 231)
  1612. {
  1613. unsigned int gray = (i * 256 + 115) / 231;
  1614. png_create_colormap_entry(display, i++, gray, gray, gray, 255, P_sRGB);
  1615. }
  1616. /* 255 is used here for the component values for consistency with the code
  1617. * that undoes premultiplication in pngwrite.c.
  1618. */
  1619. png_create_colormap_entry(display, i++, 255, 255, 255, 0, P_sRGB);
  1620. for (a=1; a<5; ++a)
  1621. {
  1622. unsigned int g;
  1623. for (g=0; g<6; ++g)
  1624. png_create_colormap_entry(display, i++, g*51, g*51, g*51, a*51,
  1625. P_sRGB);
  1626. }
  1627. return (int)i;
  1628. }
  1629. #define PNG_GA_COLORMAP_ENTRIES 256
  1630. static int
  1631. make_rgb_colormap(png_image_read_control *display)
  1632. {
  1633. unsigned int i, r;
  1634. /* Build a 6x6x6 opaque RGB cube */
  1635. for (i=r=0; r<6; ++r)
  1636. {
  1637. unsigned int g;
  1638. for (g=0; g<6; ++g)
  1639. {
  1640. unsigned int b;
  1641. for (b=0; b<6; ++b)
  1642. png_create_colormap_entry(display, i++, r*51, g*51, b*51, 255,
  1643. P_sRGB);
  1644. }
  1645. }
  1646. return (int)i;
  1647. }
  1648. #define PNG_RGB_COLORMAP_ENTRIES 216
  1649. /* Return a palette index to the above palette given three 8-bit sRGB values. */
  1650. #define PNG_RGB_INDEX(r,g,b) \
  1651. ((png_byte)(6 * (6 * PNG_DIV51(r) + PNG_DIV51(g)) + PNG_DIV51(b)))
  1652. static int
  1653. png_image_read_colormap(png_voidp argument)
  1654. {
  1655. png_image_read_control *display =
  1656. png_voidcast(png_image_read_control*, argument);
  1657. png_imagep image = display->image;
  1658. png_structrp png_ptr = image->opaque->png_ptr;
  1659. png_uint_32 output_format = image->format;
  1660. int output_encoding = (output_format & PNG_FORMAT_FLAG_LINEAR) != 0 ?
  1661. P_LINEAR : P_sRGB;
  1662. unsigned int cmap_entries;
  1663. unsigned int output_processing; /* Output processing option */
  1664. unsigned int data_encoding = P_NOTSET; /* Encoding libpng must produce */
  1665. /* Background information; the background color and the index of this color
  1666. * in the color-map if it exists (else 256).
  1667. */
  1668. unsigned int background_index = 256;
  1669. png_uint_32 back_r, back_g, back_b;
  1670. /* Flags to accumulate things that need to be done to the input. */
  1671. int expand_tRNS = 0;
  1672. /* Exclude the NYI feature of compositing onto a color-mapped buffer; it is
  1673. * very difficult to do, the results look awful, and it is difficult to see
  1674. * what possible use it is because the application can't control the
  1675. * color-map.
  1676. */
  1677. if (((png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0 ||
  1678. png_ptr->num_trans > 0) /* alpha in input */ &&
  1679. ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0) /* no alpha in output */)
  1680. {
  1681. if (output_encoding == P_LINEAR) /* compose on black */
  1682. back_b = back_g = back_r = 0;
  1683. else if (display->background == NULL /* no way to remove it */)
  1684. png_error(png_ptr,
  1685. "background color must be supplied to remove alpha/transparency");
  1686. /* Get a copy of the background color (this avoids repeating the checks
  1687. * below.) The encoding is 8-bit sRGB or 16-bit linear, depending on the
  1688. * output format.
  1689. */
  1690. else
  1691. {
  1692. back_g = display->background->green;
  1693. if ((output_format & PNG_FORMAT_FLAG_COLOR) != 0)
  1694. {
  1695. back_r = display->background->red;
  1696. back_b = display->background->blue;
  1697. }
  1698. else
  1699. back_b = back_r = back_g;
  1700. }
  1701. }
  1702. else if (output_encoding == P_LINEAR)
  1703. back_b = back_r = back_g = 65535;
  1704. else
  1705. back_b = back_r = back_g = 255;
  1706. /* Default the input file gamma if required - this is necessary because
  1707. * libpng assumes that if no gamma information is present the data is in the
  1708. * output format, but the simplified API deduces the gamma from the input
  1709. * format. The 'default' gamma value is also set by png_set_alpha_mode, but
  1710. * this is happening before any such call, so:
  1711. *
  1712. * TODO: should be an internal API and all this code should be copied into a
  1713. * single common gamma+colorspace file.
  1714. */
  1715. if (png_ptr->bit_depth == 16 &&
  1716. (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
  1717. png_ptr->default_gamma = PNG_GAMMA_LINEAR;
  1718. else
  1719. png_ptr->default_gamma = PNG_GAMMA_sRGB_INVERSE;
  1720. /* Decide what to do based on the PNG color type of the input data. The
  1721. * utility function png_create_colormap_entry deals with most aspects of the
  1722. * output transformations; this code works out how to produce bytes of
  1723. * color-map entries from the original format.
  1724. */
  1725. switch (png_ptr->color_type)
  1726. {
  1727. case PNG_COLOR_TYPE_GRAY:
  1728. if (png_ptr->bit_depth <= 8)
  1729. {
  1730. /* There at most 256 colors in the output, regardless of
  1731. * transparency.
  1732. */
  1733. unsigned int step, i, val, trans = 256/*ignore*/, back_alpha = 0;
  1734. cmap_entries = 1U << png_ptr->bit_depth;
  1735. if (cmap_entries > image->colormap_entries)
  1736. png_error(png_ptr, "gray[8] color-map: too few entries");
  1737. step = 255 / (cmap_entries - 1);
  1738. output_processing = PNG_CMAP_NONE;
  1739. /* If there is a tRNS chunk then this either selects a transparent
  1740. * value or, if the output has no alpha, the background color.
  1741. */
  1742. if (png_ptr->num_trans > 0)
  1743. {
  1744. trans = png_ptr->trans_color.gray;
  1745. if ((output_format & PNG_FORMAT_FLAG_ALPHA) == 0)
  1746. back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
  1747. }
  1748. /* png_create_colormap_entry just takes an RGBA and writes the
  1749. * corresponding color-map entry using the format from 'image',
  1750. * including the required conversion to sRGB or linear as
  1751. * appropriate. The input values are always either sRGB (if the
  1752. * gamma correction flag is 0) or 0..255 scaled file encoded values
  1753. * (if the function must gamma correct them).
  1754. */
  1755. for (i=val=0; i<cmap_entries; ++i, val += step)
  1756. {
  1757. /* 'i' is a file value. While this will result in duplicated
  1758. * entries for 8-bit non-sRGB encoded files it is necessary to
  1759. * have non-gamma corrected values to do tRNS handling.
  1760. */
  1761. if (i != trans)
  1762. png_create_colormap_entry(display, i, val, val, val, 255,
  1763. P_FILE/*8-bit with file gamma*/);
  1764. /* Else this entry is transparent. The colors don't matter if
  1765. * there is an alpha channel (back_alpha == 0), but it does no
  1766. * harm to pass them in; the values are not set above so this
  1767. * passes in white.
  1768. *
  1769. * NOTE: this preserves the full precision of the application
  1770. * supplied background color when it is used.
  1771. */
  1772. else
  1773. png_create_colormap_entry(display, i, back_r, back_g, back_b,
  1774. back_alpha, output_encoding);
  1775. }
  1776. /* We need libpng to preserve the original encoding. */
  1777. data_encoding = P_FILE;
  1778. /* The rows from libpng, while technically gray values, are now also
  1779. * color-map indices; however, they may need to be expanded to 1
  1780. * byte per pixel. This is what png_set_packing does (i.e., it
  1781. * unpacks the bit values into bytes.)
  1782. */
  1783. if (png_ptr->bit_depth < 8)
  1784. png_set_packing(png_ptr);
  1785. }
  1786. else /* bit depth is 16 */
  1787. {
  1788. /* The 16-bit input values can be converted directly to 8-bit gamma
  1789. * encoded values; however, if a tRNS chunk is present 257 color-map
  1790. * entries are required. This means that the extra entry requires
  1791. * special processing; add an alpha channel, sacrifice gray level
  1792. * 254 and convert transparent (alpha==0) entries to that.
  1793. *
  1794. * Use libpng to chop the data to 8 bits. Convert it to sRGB at the
  1795. * same time to minimize quality loss. If a tRNS chunk is present
  1796. * this means libpng must handle it too; otherwise it is impossible
  1797. * to do the exact match on the 16-bit value.
  1798. *
  1799. * If the output has no alpha channel *and* the background color is
  1800. * gray then it is possible to let libpng handle the substitution by
  1801. * ensuring that the corresponding gray level matches the background
  1802. * color exactly.
  1803. */
  1804. data_encoding = P_sRGB;
  1805. if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
  1806. png_error(png_ptr, "gray[16] color-map: too few entries");
  1807. cmap_entries = (unsigned int)make_gray_colormap(display);
  1808. if (png_ptr->num_trans > 0)
  1809. {
  1810. unsigned int back_alpha;
  1811. if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
  1812. back_alpha = 0;
  1813. else
  1814. {
  1815. if (back_r == back_g && back_g == back_b)
  1816. {
  1817. /* Background is gray; no special processing will be
  1818. * required.
  1819. */
  1820. png_color_16 c;
  1821. png_uint_32 gray = back_g;
  1822. if (output_encoding == P_LINEAR)
  1823. {
  1824. gray = PNG_sRGB_FROM_LINEAR(gray * 255);
  1825. /* And make sure the corresponding palette entry
  1826. * matches.
  1827. */
  1828. png_create_colormap_entry(display, gray, back_g, back_g,
  1829. back_g, 65535, P_LINEAR);
  1830. }
  1831. /* The background passed to libpng, however, must be the
  1832. * sRGB value.
  1833. */
  1834. c.index = 0; /*unused*/
  1835. c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
  1836. /* NOTE: does this work without expanding tRNS to alpha?
  1837. * It should be the color->gray case below apparently
  1838. * doesn't.
  1839. */
  1840. png_set_background_fixed(png_ptr, &c,
  1841. PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
  1842. 0/*gamma: not used*/);
  1843. output_processing = PNG_CMAP_NONE;
  1844. break;
  1845. }
  1846. #ifdef __COVERITY__
  1847. /* Coverity claims that output_encoding cannot be 2 (P_LINEAR)
  1848. * here.
  1849. */
  1850. back_alpha = 255;
  1851. #else
  1852. back_alpha = output_encoding == P_LINEAR ? 65535 : 255;
  1853. #endif
  1854. }
  1855. /* output_processing means that the libpng-processed row will be
  1856. * 8-bit GA and it has to be processing to single byte color-map
  1857. * values. Entry 254 is replaced by either a completely
  1858. * transparent entry or by the background color at full
  1859. * precision (and the background color is not a simple gray
  1860. * level in this case.)
  1861. */
  1862. expand_tRNS = 1;
  1863. output_processing = PNG_CMAP_TRANS;
  1864. background_index = 254;
  1865. /* And set (overwrite) color-map entry 254 to the actual
  1866. * background color at full precision.
  1867. */
  1868. png_create_colormap_entry(display, 254, back_r, back_g, back_b,
  1869. back_alpha, output_encoding);
  1870. }
  1871. else
  1872. output_processing = PNG_CMAP_NONE;
  1873. }
  1874. break;
  1875. case PNG_COLOR_TYPE_GRAY_ALPHA:
  1876. /* 8-bit or 16-bit PNG with two channels - gray and alpha. A minimum
  1877. * of 65536 combinations. If, however, the alpha channel is to be
  1878. * removed there are only 256 possibilities if the background is gray.
  1879. * (Otherwise there is a subset of the 65536 possibilities defined by
  1880. * the triangle between black, white and the background color.)
  1881. *
  1882. * Reduce 16-bit files to 8-bit and sRGB encode the result. No need to
  1883. * worry about tRNS matching - tRNS is ignored if there is an alpha
  1884. * channel.
  1885. */
  1886. data_encoding = P_sRGB;
  1887. if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
  1888. {
  1889. if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
  1890. png_error(png_ptr, "gray+alpha color-map: too few entries");
  1891. cmap_entries = (unsigned int)make_ga_colormap(display);
  1892. background_index = PNG_CMAP_GA_BACKGROUND;
  1893. output_processing = PNG_CMAP_GA;
  1894. }
  1895. else /* alpha is removed */
  1896. {
  1897. /* Alpha must be removed as the PNG data is processed when the
  1898. * background is a color because the G and A channels are
  1899. * independent and the vector addition (non-parallel vectors) is a
  1900. * 2-D problem.
  1901. *
  1902. * This can be reduced to the same algorithm as above by making a
  1903. * colormap containing gray levels (for the opaque grays), a
  1904. * background entry (for a transparent pixel) and a set of four six
  1905. * level color values, one set for each intermediate alpha value.
  1906. * See the comments in make_ga_colormap for how this works in the
  1907. * per-pixel processing.
  1908. *
  1909. * If the background is gray, however, we only need a 256 entry gray
  1910. * level color map. It is sufficient to make the entry generated
  1911. * for the background color be exactly the color specified.
  1912. */
  1913. if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0 ||
  1914. (back_r == back_g && back_g == back_b))
  1915. {
  1916. /* Background is gray; no special processing will be required. */
  1917. png_color_16 c;
  1918. png_uint_32 gray = back_g;
  1919. if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
  1920. png_error(png_ptr, "gray-alpha color-map: too few entries");
  1921. cmap_entries = (unsigned int)make_gray_colormap(display);
  1922. if (output_encoding == P_LINEAR)
  1923. {
  1924. gray = PNG_sRGB_FROM_LINEAR(gray * 255);
  1925. /* And make sure the corresponding palette entry matches. */
  1926. png_create_colormap_entry(display, gray, back_g, back_g,
  1927. back_g, 65535, P_LINEAR);
  1928. }
  1929. /* The background passed to libpng, however, must be the sRGB
  1930. * value.
  1931. */
  1932. c.index = 0; /*unused*/
  1933. c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
  1934. png_set_background_fixed(png_ptr, &c,
  1935. PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
  1936. 0/*gamma: not used*/);
  1937. output_processing = PNG_CMAP_NONE;
  1938. }
  1939. else
  1940. {
  1941. png_uint_32 i, a;
  1942. /* This is the same as png_make_ga_colormap, above, except that
  1943. * the entries are all opaque.
  1944. */
  1945. if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
  1946. png_error(png_ptr, "ga-alpha color-map: too few entries");
  1947. i = 0;
  1948. while (i < 231)
  1949. {
  1950. png_uint_32 gray = (i * 256 + 115) / 231;
  1951. png_create_colormap_entry(display, i++, gray, gray, gray,
  1952. 255, P_sRGB);
  1953. }
  1954. /* NOTE: this preserves the full precision of the application
  1955. * background color.
  1956. */
  1957. background_index = i;
  1958. png_create_colormap_entry(display, i++, back_r, back_g, back_b,
  1959. #ifdef __COVERITY__
  1960. /* Coverity claims that output_encoding
  1961. * cannot be 2 (P_LINEAR) here.
  1962. */ 255U,
  1963. #else
  1964. output_encoding == P_LINEAR ? 65535U : 255U,
  1965. #endif
  1966. output_encoding);
  1967. /* For non-opaque input composite on the sRGB background - this
  1968. * requires inverting the encoding for each component. The input
  1969. * is still converted to the sRGB encoding because this is a
  1970. * reasonable approximate to the logarithmic curve of human
  1971. * visual sensitivity, at least over the narrow range which PNG
  1972. * represents. Consequently 'G' is always sRGB encoded, while
  1973. * 'A' is linear. We need the linear background colors.
  1974. */
  1975. if (output_encoding == P_sRGB) /* else already linear */
  1976. {
  1977. /* This may produce a value not exactly matching the
  1978. * background, but that's ok because these numbers are only
  1979. * used when alpha != 0
  1980. */
  1981. back_r = png_sRGB_table[back_r];
  1982. back_g = png_sRGB_table[back_g];
  1983. back_b = png_sRGB_table[back_b];
  1984. }
  1985. for (a=1; a<5; ++a)
  1986. {
  1987. unsigned int g;
  1988. /* PNG_sRGB_FROM_LINEAR expects a 16-bit linear value scaled
  1989. * by an 8-bit alpha value (0..255).
  1990. */
  1991. png_uint_32 alpha = 51 * a;
  1992. png_uint_32 back_rx = (255-alpha) * back_r;
  1993. png_uint_32 back_gx = (255-alpha) * back_g;
  1994. png_uint_32 back_bx = (255-alpha) * back_b;
  1995. for (g=0; g<6; ++g)
  1996. {
  1997. png_uint_32 gray = png_sRGB_table[g*51] * alpha;
  1998. png_create_colormap_entry(display, i++,
  1999. PNG_sRGB_FROM_LINEAR(gray + back_rx),
  2000. PNG_sRGB_FROM_LINEAR(gray + back_gx),
  2001. PNG_sRGB_FROM_LINEAR(gray + back_bx), 255, P_sRGB);
  2002. }
  2003. }
  2004. cmap_entries = i;
  2005. output_processing = PNG_CMAP_GA;
  2006. }
  2007. }
  2008. break;
  2009. case PNG_COLOR_TYPE_RGB:
  2010. case PNG_COLOR_TYPE_RGB_ALPHA:
  2011. /* Exclude the case where the output is gray; we can always handle this
  2012. * with the cases above.
  2013. */
  2014. if ((output_format & PNG_FORMAT_FLAG_COLOR) == 0)
  2015. {
  2016. /* The color-map will be grayscale, so we may as well convert the
  2017. * input RGB values to a simple grayscale and use the grayscale
  2018. * code above.
  2019. *
  2020. * NOTE: calling this apparently damages the recognition of the
  2021. * transparent color in background color handling; call
  2022. * png_set_tRNS_to_alpha before png_set_background_fixed.
  2023. */
  2024. png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE, -1,
  2025. -1);
  2026. data_encoding = P_sRGB;
  2027. /* The output will now be one or two 8-bit gray or gray+alpha
  2028. * channels. The more complex case arises when the input has alpha.
  2029. */
  2030. if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
  2031. png_ptr->num_trans > 0) &&
  2032. (output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
  2033. {
  2034. /* Both input and output have an alpha channel, so no background
  2035. * processing is required; just map the GA bytes to the right
  2036. * color-map entry.
  2037. */
  2038. expand_tRNS = 1;
  2039. if (PNG_GA_COLORMAP_ENTRIES > image->colormap_entries)
  2040. png_error(png_ptr, "rgb[ga] color-map: too few entries");
  2041. cmap_entries = (unsigned int)make_ga_colormap(display);
  2042. background_index = PNG_CMAP_GA_BACKGROUND;
  2043. output_processing = PNG_CMAP_GA;
  2044. }
  2045. else
  2046. {
  2047. const png_fixed_point gamma = png_resolve_file_gamma(png_ptr);
  2048. /* Either the input or the output has no alpha channel, so there
  2049. * will be no non-opaque pixels in the color-map; it will just be
  2050. * grayscale.
  2051. */
  2052. if (PNG_GRAY_COLORMAP_ENTRIES > image->colormap_entries)
  2053. png_error(png_ptr, "rgb[gray] color-map: too few entries");
  2054. /* Ideally this code would use libpng to do the gamma correction,
  2055. * but if an input alpha channel is to be removed we will hit the
  2056. * libpng bug in gamma+compose+rgb-to-gray (the double gamma
  2057. * correction bug). Fix this by dropping the gamma correction in
  2058. * this case and doing it in the palette; this will result in
  2059. * duplicate palette entries, but that's better than the
  2060. * alternative of double gamma correction.
  2061. *
  2062. * NOTE: PNGv3: check the resolved result of all the potentially
  2063. * different colour space chunks.
  2064. */
  2065. if ((png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
  2066. png_ptr->num_trans > 0) &&
  2067. png_gamma_not_sRGB(gamma) != 0)
  2068. {
  2069. cmap_entries = (unsigned int)make_gray_file_colormap(display);
  2070. data_encoding = P_FILE;
  2071. }
  2072. else
  2073. cmap_entries = (unsigned int)make_gray_colormap(display);
  2074. /* But if the input has alpha or transparency it must be removed
  2075. */
  2076. if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
  2077. png_ptr->num_trans > 0)
  2078. {
  2079. png_color_16 c;
  2080. png_uint_32 gray = back_g;
  2081. /* We need to ensure that the application background exists in
  2082. * the colormap and that completely transparent pixels map to
  2083. * it. Achieve this simply by ensuring that the entry
  2084. * selected for the background really is the background color.
  2085. */
  2086. if (data_encoding == P_FILE) /* from the fixup above */
  2087. {
  2088. /* The app supplied a gray which is in output_encoding, we
  2089. * need to convert it to a value of the input (P_FILE)
  2090. * encoding then set this palette entry to the required
  2091. * output encoding.
  2092. */
  2093. if (output_encoding == P_sRGB)
  2094. gray = png_sRGB_table[gray]; /* now P_LINEAR */
  2095. gray = PNG_DIV257(png_gamma_16bit_correct(gray, gamma));
  2096. /* now P_FILE */
  2097. /* And make sure the corresponding palette entry contains
  2098. * exactly the required sRGB value.
  2099. */
  2100. png_create_colormap_entry(display, gray, back_g, back_g,
  2101. back_g, 0/*unused*/, output_encoding);
  2102. }
  2103. else if (output_encoding == P_LINEAR)
  2104. {
  2105. gray = PNG_sRGB_FROM_LINEAR(gray * 255);
  2106. /* And make sure the corresponding palette entry matches.
  2107. */
  2108. png_create_colormap_entry(display, gray, back_g, back_g,
  2109. back_g, 0/*unused*/, P_LINEAR);
  2110. }
  2111. /* The background passed to libpng, however, must be the
  2112. * output (normally sRGB) value.
  2113. */
  2114. c.index = 0; /*unused*/
  2115. c.gray = c.red = c.green = c.blue = (png_uint_16)gray;
  2116. /* NOTE: the following is apparently a bug in libpng. Without
  2117. * it the transparent color recognition in
  2118. * png_set_background_fixed seems to go wrong.
  2119. */
  2120. expand_tRNS = 1;
  2121. png_set_background_fixed(png_ptr, &c,
  2122. PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
  2123. 0/*gamma: not used*/);
  2124. }
  2125. output_processing = PNG_CMAP_NONE;
  2126. }
  2127. }
  2128. else /* output is color */
  2129. {
  2130. /* We could use png_quantize here so long as there is no transparent
  2131. * color or alpha; png_quantize ignores alpha. Easier overall just
  2132. * to do it once and using PNG_DIV51 on the 6x6x6 reduced RGB cube.
  2133. * Consequently we always want libpng to produce sRGB data.
  2134. */
  2135. data_encoding = P_sRGB;
  2136. /* Is there any transparency or alpha? */
  2137. if (png_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
  2138. png_ptr->num_trans > 0)
  2139. {
  2140. /* Is there alpha in the output too? If so all four channels are
  2141. * processed into a special RGB cube with alpha support.
  2142. */
  2143. if ((output_format & PNG_FORMAT_FLAG_ALPHA) != 0)
  2144. {
  2145. png_uint_32 r;
  2146. if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
  2147. png_error(png_ptr, "rgb+alpha color-map: too few entries");
  2148. cmap_entries = (unsigned int)make_rgb_colormap(display);
  2149. /* Add a transparent entry. */
  2150. png_create_colormap_entry(display, cmap_entries, 255, 255,
  2151. 255, 0, P_sRGB);
  2152. /* This is stored as the background index for the processing
  2153. * algorithm.
  2154. */
  2155. background_index = cmap_entries++;
  2156. /* Add 27 r,g,b entries each with alpha 0.5. */
  2157. for (r=0; r<256; r = (r << 1) | 0x7f)
  2158. {
  2159. png_uint_32 g;
  2160. for (g=0; g<256; g = (g << 1) | 0x7f)
  2161. {
  2162. png_uint_32 b;
  2163. /* This generates components with the values 0, 127 and
  2164. * 255
  2165. */
  2166. for (b=0; b<256; b = (b << 1) | 0x7f)
  2167. png_create_colormap_entry(display, cmap_entries++,
  2168. r, g, b, 128, P_sRGB);
  2169. }
  2170. }
  2171. expand_tRNS = 1;
  2172. output_processing = PNG_CMAP_RGB_ALPHA;
  2173. }
  2174. else
  2175. {
  2176. /* Alpha/transparency must be removed. The background must
  2177. * exist in the color map (achieved by setting adding it after
  2178. * the 666 color-map). If the standard processing code will
  2179. * pick up this entry automatically that's all that is
  2180. * required; libpng can be called to do the background
  2181. * processing.
  2182. */
  2183. unsigned int sample_size =
  2184. PNG_IMAGE_SAMPLE_SIZE(output_format);
  2185. png_uint_32 r, g, b; /* sRGB background */
  2186. if (PNG_RGB_COLORMAP_ENTRIES+1+27 > image->colormap_entries)
  2187. png_error(png_ptr, "rgb-alpha color-map: too few entries");
  2188. cmap_entries = (unsigned int)make_rgb_colormap(display);
  2189. png_create_colormap_entry(display, cmap_entries, back_r,
  2190. back_g, back_b, 0/*unused*/, output_encoding);
  2191. if (output_encoding == P_LINEAR)
  2192. {
  2193. r = PNG_sRGB_FROM_LINEAR(back_r * 255);
  2194. g = PNG_sRGB_FROM_LINEAR(back_g * 255);
  2195. b = PNG_sRGB_FROM_LINEAR(back_b * 255);
  2196. }
  2197. else
  2198. {
  2199. r = back_r;
  2200. g = back_g;
  2201. b = back_g;
  2202. }
  2203. /* Compare the newly-created color-map entry with the one the
  2204. * PNG_CMAP_RGB algorithm will use. If the two entries don't
  2205. * match, add the new one and set this as the background
  2206. * index.
  2207. */
  2208. if (memcmp((png_const_bytep)display->colormap +
  2209. sample_size * cmap_entries,
  2210. (png_const_bytep)display->colormap +
  2211. sample_size * PNG_RGB_INDEX(r,g,b),
  2212. sample_size) != 0)
  2213. {
  2214. /* The background color must be added. */
  2215. background_index = cmap_entries++;
  2216. /* Add 27 r,g,b entries each with created by composing with
  2217. * the background at alpha 0.5.
  2218. */
  2219. for (r=0; r<256; r = (r << 1) | 0x7f)
  2220. {
  2221. for (g=0; g<256; g = (g << 1) | 0x7f)
  2222. {
  2223. /* This generates components with the values 0, 127
  2224. * and 255
  2225. */
  2226. for (b=0; b<256; b = (b << 1) | 0x7f)
  2227. png_create_colormap_entry(display, cmap_entries++,
  2228. png_colormap_compose(display, r, P_sRGB, 128,
  2229. back_r, output_encoding),
  2230. png_colormap_compose(display, g, P_sRGB, 128,
  2231. back_g, output_encoding),
  2232. png_colormap_compose(display, b, P_sRGB, 128,
  2233. back_b, output_encoding),
  2234. 0/*unused*/, output_encoding);
  2235. }
  2236. }
  2237. expand_tRNS = 1;
  2238. output_processing = PNG_CMAP_RGB_ALPHA;
  2239. }
  2240. else /* background color is in the standard color-map */
  2241. {
  2242. png_color_16 c;
  2243. c.index = 0; /*unused*/
  2244. c.red = (png_uint_16)back_r;
  2245. c.gray = c.green = (png_uint_16)back_g;
  2246. c.blue = (png_uint_16)back_b;
  2247. png_set_background_fixed(png_ptr, &c,
  2248. PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
  2249. 0/*gamma: not used*/);
  2250. output_processing = PNG_CMAP_RGB;
  2251. }
  2252. }
  2253. }
  2254. else /* no alpha or transparency in the input */
  2255. {
  2256. /* Alpha in the output is irrelevant, simply map the opaque input
  2257. * pixels to the 6x6x6 color-map.
  2258. */
  2259. if (PNG_RGB_COLORMAP_ENTRIES > image->colormap_entries)
  2260. png_error(png_ptr, "rgb color-map: too few entries");
  2261. cmap_entries = (unsigned int)make_rgb_colormap(display);
  2262. output_processing = PNG_CMAP_RGB;
  2263. }
  2264. }
  2265. break;
  2266. case PNG_COLOR_TYPE_PALETTE:
  2267. /* It's already got a color-map. It may be necessary to eliminate the
  2268. * tRNS entries though.
  2269. */
  2270. {
  2271. unsigned int num_trans = png_ptr->num_trans;
  2272. png_const_bytep trans = num_trans > 0 ? png_ptr->trans_alpha : NULL;
  2273. png_const_colorp colormap = png_ptr->palette;
  2274. int do_background = trans != NULL &&
  2275. (output_format & PNG_FORMAT_FLAG_ALPHA) == 0;
  2276. unsigned int i;
  2277. /* Just in case: */
  2278. if (trans == NULL)
  2279. num_trans = 0;
  2280. output_processing = PNG_CMAP_NONE;
  2281. data_encoding = P_FILE; /* Don't change from color-map indices */
  2282. cmap_entries = (unsigned int)png_ptr->num_palette;
  2283. if (cmap_entries > 256)
  2284. cmap_entries = 256;
  2285. if (cmap_entries > (unsigned int)image->colormap_entries)
  2286. png_error(png_ptr, "palette color-map: too few entries");
  2287. for (i=0; i < cmap_entries; ++i)
  2288. {
  2289. if (do_background != 0 && i < num_trans && trans[i] < 255)
  2290. {
  2291. if (trans[i] == 0)
  2292. png_create_colormap_entry(display, i, back_r, back_g,
  2293. back_b, 0, output_encoding);
  2294. else
  2295. {
  2296. /* Must compose the PNG file color in the color-map entry
  2297. * on the sRGB color in 'back'.
  2298. */
  2299. png_create_colormap_entry(display, i,
  2300. png_colormap_compose(display, colormap[i].red,
  2301. P_FILE, trans[i], back_r, output_encoding),
  2302. png_colormap_compose(display, colormap[i].green,
  2303. P_FILE, trans[i], back_g, output_encoding),
  2304. png_colormap_compose(display, colormap[i].blue,
  2305. P_FILE, trans[i], back_b, output_encoding),
  2306. output_encoding == P_LINEAR ? trans[i] * 257U :
  2307. trans[i],
  2308. output_encoding);
  2309. }
  2310. }
  2311. else
  2312. png_create_colormap_entry(display, i, colormap[i].red,
  2313. colormap[i].green, colormap[i].blue,
  2314. i < num_trans ? trans[i] : 255U, P_FILE/*8-bit*/);
  2315. }
  2316. /* The PNG data may have indices packed in fewer than 8 bits, it
  2317. * must be expanded if so.
  2318. */
  2319. if (png_ptr->bit_depth < 8)
  2320. png_set_packing(png_ptr);
  2321. }
  2322. break;
  2323. default:
  2324. png_error(png_ptr, "invalid PNG color type");
  2325. /*NOT REACHED*/
  2326. }
  2327. /* Now deal with the output processing */
  2328. if (expand_tRNS != 0 && png_ptr->num_trans > 0 &&
  2329. (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) == 0)
  2330. png_set_tRNS_to_alpha(png_ptr);
  2331. switch (data_encoding)
  2332. {
  2333. case P_sRGB:
  2334. /* Change to 8-bit sRGB */
  2335. png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, PNG_GAMMA_sRGB);
  2336. /* FALLTHROUGH */
  2337. case P_FILE:
  2338. if (png_ptr->bit_depth > 8)
  2339. png_set_scale_16(png_ptr);
  2340. break;
  2341. #ifdef __GNUC__
  2342. default:
  2343. png_error(png_ptr, "bad data option (internal error)");
  2344. #endif
  2345. }
  2346. if (cmap_entries > 256 || cmap_entries > image->colormap_entries)
  2347. png_error(png_ptr, "color map overflow (BAD internal error)");
  2348. image->colormap_entries = cmap_entries;
  2349. /* Double check using the recorded background index */
  2350. switch (output_processing)
  2351. {
  2352. case PNG_CMAP_NONE:
  2353. if (background_index != PNG_CMAP_NONE_BACKGROUND)
  2354. goto bad_background;
  2355. break;
  2356. case PNG_CMAP_GA:
  2357. if (background_index != PNG_CMAP_GA_BACKGROUND)
  2358. goto bad_background;
  2359. break;
  2360. case PNG_CMAP_TRANS:
  2361. if (background_index >= cmap_entries ||
  2362. background_index != PNG_CMAP_TRANS_BACKGROUND)
  2363. goto bad_background;
  2364. break;
  2365. case PNG_CMAP_RGB:
  2366. if (background_index != PNG_CMAP_RGB_BACKGROUND)
  2367. goto bad_background;
  2368. break;
  2369. case PNG_CMAP_RGB_ALPHA:
  2370. if (background_index != PNG_CMAP_RGB_ALPHA_BACKGROUND)
  2371. goto bad_background;
  2372. break;
  2373. default:
  2374. png_error(png_ptr, "bad processing option (internal error)");
  2375. bad_background:
  2376. png_error(png_ptr, "bad background index (internal error)");
  2377. }
  2378. display->colormap_processing = (int)output_processing;
  2379. return 1/*ok*/;
  2380. }
  2381. /* The final part of the color-map read called from png_image_finish_read. */
  2382. static int
  2383. png_image_read_and_map(png_voidp argument)
  2384. {
  2385. png_image_read_control *display = png_voidcast(png_image_read_control*,
  2386. argument);
  2387. png_imagep image = display->image;
  2388. png_structrp png_ptr = image->opaque->png_ptr;
  2389. int passes;
  2390. /* Called when the libpng data must be transformed into the color-mapped
  2391. * form. There is a local row buffer in display->local and this routine must
  2392. * do the interlace handling.
  2393. */
  2394. switch (png_ptr->interlaced)
  2395. {
  2396. case PNG_INTERLACE_NONE:
  2397. passes = 1;
  2398. break;
  2399. case PNG_INTERLACE_ADAM7:
  2400. passes = PNG_INTERLACE_ADAM7_PASSES;
  2401. break;
  2402. default:
  2403. png_error(png_ptr, "unknown interlace type");
  2404. }
  2405. {
  2406. png_uint_32 height = image->height;
  2407. png_uint_32 width = image->width;
  2408. int proc = display->colormap_processing;
  2409. png_bytep first_row = png_voidcast(png_bytep, display->first_row);
  2410. ptrdiff_t step_row = display->row_bytes;
  2411. int pass;
  2412. for (pass = 0; pass < passes; ++pass)
  2413. {
  2414. unsigned int startx, stepx, stepy;
  2415. png_uint_32 y;
  2416. if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
  2417. {
  2418. /* The row may be empty for a short image: */
  2419. if (PNG_PASS_COLS(width, pass) == 0)
  2420. continue;
  2421. startx = PNG_PASS_START_COL(pass);
  2422. stepx = PNG_PASS_COL_OFFSET(pass);
  2423. y = PNG_PASS_START_ROW(pass);
  2424. stepy = PNG_PASS_ROW_OFFSET(pass);
  2425. }
  2426. else
  2427. {
  2428. y = 0;
  2429. startx = 0;
  2430. stepx = stepy = 1;
  2431. }
  2432. for (; y<height; y += stepy)
  2433. {
  2434. png_bytep inrow = png_voidcast(png_bytep, display->local_row);
  2435. png_bytep outrow = first_row + y * step_row;
  2436. png_const_bytep end_row = outrow + width;
  2437. /* Read read the libpng data into the temporary buffer. */
  2438. png_read_row(png_ptr, inrow, NULL);
  2439. /* Now process the row according to the processing option, note
  2440. * that the caller verifies that the format of the libpng output
  2441. * data is as required.
  2442. */
  2443. outrow += startx;
  2444. switch (proc)
  2445. {
  2446. case PNG_CMAP_GA:
  2447. for (; outrow < end_row; outrow += stepx)
  2448. {
  2449. /* The data is always in the PNG order */
  2450. unsigned int gray = *inrow++;
  2451. unsigned int alpha = *inrow++;
  2452. unsigned int entry;
  2453. /* NOTE: this code is copied as a comment in
  2454. * make_ga_colormap above. Please update the
  2455. * comment if you change this code!
  2456. */
  2457. if (alpha > 229) /* opaque */
  2458. {
  2459. entry = (231 * gray + 128) >> 8;
  2460. }
  2461. else if (alpha < 26) /* transparent */
  2462. {
  2463. entry = 231;
  2464. }
  2465. else /* partially opaque */
  2466. {
  2467. entry = 226 + 6 * PNG_DIV51(alpha) + PNG_DIV51(gray);
  2468. }
  2469. *outrow = (png_byte)entry;
  2470. }
  2471. break;
  2472. case PNG_CMAP_TRANS:
  2473. for (; outrow < end_row; outrow += stepx)
  2474. {
  2475. png_byte gray = *inrow++;
  2476. png_byte alpha = *inrow++;
  2477. if (alpha == 0)
  2478. *outrow = PNG_CMAP_TRANS_BACKGROUND;
  2479. else if (gray != PNG_CMAP_TRANS_BACKGROUND)
  2480. *outrow = gray;
  2481. else
  2482. *outrow = (png_byte)(PNG_CMAP_TRANS_BACKGROUND+1);
  2483. }
  2484. break;
  2485. case PNG_CMAP_RGB:
  2486. for (; outrow < end_row; outrow += stepx)
  2487. {
  2488. *outrow = PNG_RGB_INDEX(inrow[0], inrow[1], inrow[2]);
  2489. inrow += 3;
  2490. }
  2491. break;
  2492. case PNG_CMAP_RGB_ALPHA:
  2493. for (; outrow < end_row; outrow += stepx)
  2494. {
  2495. unsigned int alpha = inrow[3];
  2496. /* Because the alpha entries only hold alpha==0.5 values
  2497. * split the processing at alpha==0.25 (64) and 0.75
  2498. * (196).
  2499. */
  2500. if (alpha >= 196)
  2501. *outrow = PNG_RGB_INDEX(inrow[0], inrow[1],
  2502. inrow[2]);
  2503. else if (alpha < 64)
  2504. *outrow = PNG_CMAP_RGB_ALPHA_BACKGROUND;
  2505. else
  2506. {
  2507. /* Likewise there are three entries for each of r, g
  2508. * and b. We could select the entry by popcount on
  2509. * the top two bits on those architectures that
  2510. * support it, this is what the code below does,
  2511. * crudely.
  2512. */
  2513. unsigned int back_i = PNG_CMAP_RGB_ALPHA_BACKGROUND+1;
  2514. /* Here are how the values map:
  2515. *
  2516. * 0x00 .. 0x3f -> 0
  2517. * 0x40 .. 0xbf -> 1
  2518. * 0xc0 .. 0xff -> 2
  2519. *
  2520. * So, as above with the explicit alpha checks, the
  2521. * breakpoints are at 64 and 196.
  2522. */
  2523. if (inrow[0] & 0x80) back_i += 9; /* red */
  2524. if (inrow[0] & 0x40) back_i += 9;
  2525. if (inrow[0] & 0x80) back_i += 3; /* green */
  2526. if (inrow[0] & 0x40) back_i += 3;
  2527. if (inrow[0] & 0x80) back_i += 1; /* blue */
  2528. if (inrow[0] & 0x40) back_i += 1;
  2529. *outrow = (png_byte)back_i;
  2530. }
  2531. inrow += 4;
  2532. }
  2533. break;
  2534. default:
  2535. break;
  2536. }
  2537. }
  2538. }
  2539. }
  2540. return 1;
  2541. }
  2542. static int
  2543. png_image_read_colormapped(png_voidp argument)
  2544. {
  2545. png_image_read_control *display = png_voidcast(png_image_read_control*,
  2546. argument);
  2547. png_imagep image = display->image;
  2548. png_controlp control = image->opaque;
  2549. png_structrp png_ptr = control->png_ptr;
  2550. png_inforp info_ptr = control->info_ptr;
  2551. int passes = 0; /* As a flag */
  2552. PNG_SKIP_CHUNKS(png_ptr);
  2553. /* Update the 'info' structure and make sure the result is as required; first
  2554. * make sure to turn on the interlace handling if it will be required
  2555. * (because it can't be turned on *after* the call to png_read_update_info!)
  2556. */
  2557. if (display->colormap_processing == PNG_CMAP_NONE)
  2558. passes = png_set_interlace_handling(png_ptr);
  2559. png_read_update_info(png_ptr, info_ptr);
  2560. /* The expected output can be deduced from the colormap_processing option. */
  2561. switch (display->colormap_processing)
  2562. {
  2563. case PNG_CMAP_NONE:
  2564. /* Output must be one channel and one byte per pixel, the output
  2565. * encoding can be anything.
  2566. */
  2567. if ((info_ptr->color_type == PNG_COLOR_TYPE_PALETTE ||
  2568. info_ptr->color_type == PNG_COLOR_TYPE_GRAY) &&
  2569. info_ptr->bit_depth == 8)
  2570. break;
  2571. goto bad_output;
  2572. case PNG_CMAP_TRANS:
  2573. case PNG_CMAP_GA:
  2574. /* Output must be two channels and the 'G' one must be sRGB, the latter
  2575. * can be checked with an exact number because it should have been set
  2576. * to this number above!
  2577. */
  2578. if (info_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA &&
  2579. info_ptr->bit_depth == 8 &&
  2580. png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
  2581. image->colormap_entries == 256)
  2582. break;
  2583. goto bad_output;
  2584. case PNG_CMAP_RGB:
  2585. /* Output must be 8-bit sRGB encoded RGB */
  2586. if (info_ptr->color_type == PNG_COLOR_TYPE_RGB &&
  2587. info_ptr->bit_depth == 8 &&
  2588. png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
  2589. image->colormap_entries == 216)
  2590. break;
  2591. goto bad_output;
  2592. case PNG_CMAP_RGB_ALPHA:
  2593. /* Output must be 8-bit sRGB encoded RGBA */
  2594. if (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
  2595. info_ptr->bit_depth == 8 &&
  2596. png_ptr->screen_gamma == PNG_GAMMA_sRGB &&
  2597. image->colormap_entries == 244 /* 216 + 1 + 27 */)
  2598. break;
  2599. goto bad_output;
  2600. default:
  2601. bad_output:
  2602. png_error(png_ptr, "bad color-map processing (internal error)");
  2603. }
  2604. /* Now read the rows. Do this here if it is possible to read directly into
  2605. * the output buffer, otherwise allocate a local row buffer of the maximum
  2606. * size libpng requires and call the relevant processing routine safely.
  2607. */
  2608. {
  2609. png_voidp first_row = display->buffer;
  2610. ptrdiff_t row_bytes = display->row_stride;
  2611. /* The following expression is designed to work correctly whether it gives
  2612. * a signed or an unsigned result.
  2613. */
  2614. if (row_bytes < 0)
  2615. {
  2616. char *ptr = png_voidcast(char*, first_row);
  2617. ptr += (image->height-1) * (-row_bytes);
  2618. first_row = png_voidcast(png_voidp, ptr);
  2619. }
  2620. display->first_row = first_row;
  2621. display->row_bytes = row_bytes;
  2622. }
  2623. if (passes == 0)
  2624. {
  2625. int result;
  2626. png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
  2627. display->local_row = row;
  2628. result = png_safe_execute(image, png_image_read_and_map, display);
  2629. display->local_row = NULL;
  2630. png_free(png_ptr, row);
  2631. return result;
  2632. }
  2633. else
  2634. {
  2635. png_alloc_size_t row_bytes = (png_alloc_size_t)display->row_bytes;
  2636. while (--passes >= 0)
  2637. {
  2638. png_uint_32 y = image->height;
  2639. png_bytep row = png_voidcast(png_bytep, display->first_row);
  2640. for (; y > 0; --y)
  2641. {
  2642. png_read_row(png_ptr, row, NULL);
  2643. row += row_bytes;
  2644. }
  2645. }
  2646. return 1;
  2647. }
  2648. }
  2649. /* Row reading for interlaced 16-to-8 bit depth conversion with local buffer. */
  2650. static int
  2651. png_image_read_direct_scaled(png_voidp argument)
  2652. {
  2653. png_image_read_control *display = png_voidcast(png_image_read_control*,
  2654. argument);
  2655. png_imagep image = display->image;
  2656. png_structrp png_ptr = image->opaque->png_ptr;
  2657. png_bytep local_row = png_voidcast(png_bytep, display->local_row);
  2658. png_bytep first_row = png_voidcast(png_bytep, display->first_row);
  2659. ptrdiff_t row_bytes = display->row_bytes;
  2660. int passes;
  2661. /* Handle interlacing. */
  2662. switch (png_ptr->interlaced)
  2663. {
  2664. case PNG_INTERLACE_NONE:
  2665. passes = 1;
  2666. break;
  2667. case PNG_INTERLACE_ADAM7:
  2668. passes = PNG_INTERLACE_ADAM7_PASSES;
  2669. break;
  2670. default:
  2671. png_error(png_ptr, "unknown interlace type");
  2672. }
  2673. /* Read each pass using local_row as intermediate buffer. */
  2674. while (--passes >= 0)
  2675. {
  2676. png_uint_32 y = image->height;
  2677. png_bytep output_row = first_row;
  2678. for (; y > 0; --y)
  2679. {
  2680. /* Read into local_row (gets transformed 8-bit data). */
  2681. png_read_row(png_ptr, local_row, NULL);
  2682. /* Copy from local_row to user buffer. */
  2683. memcpy(output_row, local_row, (size_t)row_bytes);
  2684. output_row += row_bytes;
  2685. }
  2686. }
  2687. return 1;
  2688. }
  2689. /* Just the row reading part of png_image_read. */
  2690. static int
  2691. png_image_read_composite(png_voidp argument)
  2692. {
  2693. png_image_read_control *display = png_voidcast(png_image_read_control*,
  2694. argument);
  2695. png_imagep image = display->image;
  2696. png_structrp png_ptr = image->opaque->png_ptr;
  2697. int passes;
  2698. switch (png_ptr->interlaced)
  2699. {
  2700. case PNG_INTERLACE_NONE:
  2701. passes = 1;
  2702. break;
  2703. case PNG_INTERLACE_ADAM7:
  2704. passes = PNG_INTERLACE_ADAM7_PASSES;
  2705. break;
  2706. default:
  2707. png_error(png_ptr, "unknown interlace type");
  2708. }
  2709. {
  2710. png_uint_32 height = image->height;
  2711. png_uint_32 width = image->width;
  2712. ptrdiff_t step_row = display->row_bytes;
  2713. unsigned int channels =
  2714. (image->format & PNG_FORMAT_FLAG_COLOR) != 0 ? 3 : 1;
  2715. int optimize_alpha = (png_ptr->flags & PNG_FLAG_OPTIMIZE_ALPHA) != 0;
  2716. int pass;
  2717. for (pass = 0; pass < passes; ++pass)
  2718. {
  2719. unsigned int startx, stepx, stepy;
  2720. png_uint_32 y;
  2721. if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
  2722. {
  2723. /* The row may be empty for a short image: */
  2724. if (PNG_PASS_COLS(width, pass) == 0)
  2725. continue;
  2726. startx = PNG_PASS_START_COL(pass) * channels;
  2727. stepx = PNG_PASS_COL_OFFSET(pass) * channels;
  2728. y = PNG_PASS_START_ROW(pass);
  2729. stepy = PNG_PASS_ROW_OFFSET(pass);
  2730. }
  2731. else
  2732. {
  2733. y = 0;
  2734. startx = 0;
  2735. stepx = channels;
  2736. stepy = 1;
  2737. }
  2738. for (; y<height; y += stepy)
  2739. {
  2740. png_bytep inrow = png_voidcast(png_bytep, display->local_row);
  2741. png_bytep outrow;
  2742. png_const_bytep end_row;
  2743. /* Read the row, which is packed: */
  2744. png_read_row(png_ptr, inrow, NULL);
  2745. outrow = png_voidcast(png_bytep, display->first_row);
  2746. outrow += y * step_row;
  2747. end_row = outrow + width * channels;
  2748. /* Now do the composition on each pixel in this row. */
  2749. outrow += startx;
  2750. for (; outrow < end_row; outrow += stepx)
  2751. {
  2752. png_byte alpha = inrow[channels];
  2753. if (alpha > 0) /* else no change to the output */
  2754. {
  2755. unsigned int c;
  2756. for (c=0; c<channels; ++c)
  2757. {
  2758. png_uint_32 component = inrow[c];
  2759. if (alpha < 255) /* else just use component */
  2760. {
  2761. if (optimize_alpha != 0)
  2762. {
  2763. /* This is PNG_OPTIMIZED_ALPHA, the component value
  2764. * is a linear 8-bit value. Combine this with the
  2765. * current outrow[c] value which is sRGB encoded.
  2766. * Arithmetic here is 16-bits to preserve the output
  2767. * values correctly.
  2768. */
  2769. component *= 257*255; /* =65535 */
  2770. component += (255-alpha)*png_sRGB_table[outrow[c]];
  2771. /* Clamp to the valid range to defend against
  2772. * unforeseen cases where the data might be sRGB
  2773. * instead of linear premultiplied.
  2774. * (Belt-and-suspenders for CVE-2025-66293.)
  2775. */
  2776. if (component > 255*65535)
  2777. component = 255*65535;
  2778. /* So 'component' is scaled by 255*65535 and is
  2779. * therefore appropriate for the sRGB-to-linear
  2780. * conversion table.
  2781. */
  2782. component = PNG_sRGB_FROM_LINEAR(component);
  2783. }
  2784. else
  2785. {
  2786. /* Compositing was already done on the palette
  2787. * entries. The data is sRGB premultiplied on black.
  2788. * Composite with the background in sRGB space.
  2789. * This is not gamma-correct, but matches what was
  2790. * done to the palette.
  2791. */
  2792. png_uint_32 background = outrow[c];
  2793. component += ((255-alpha) * background + 127) / 255;
  2794. if (component > 255)
  2795. component = 255;
  2796. }
  2797. }
  2798. outrow[c] = (png_byte)component;
  2799. }
  2800. }
  2801. inrow += channels+1; /* components and alpha channel */
  2802. }
  2803. }
  2804. }
  2805. }
  2806. return 1;
  2807. }
  2808. /* The do_local_background case; called when all the following transforms are to
  2809. * be done:
  2810. *
  2811. * PNG_RGB_TO_GRAY
  2812. * PNG_COMPOSITE
  2813. * PNG_GAMMA
  2814. *
  2815. * This is a work-around for the fact that both the PNG_RGB_TO_GRAY and
  2816. * PNG_COMPOSITE code performs gamma correction, so we get double gamma
  2817. * correction. The fix-up is to prevent the PNG_COMPOSITE operation from
  2818. * happening inside libpng, so this routine sees an 8 or 16-bit gray+alpha
  2819. * row and handles the removal or pre-multiplication of the alpha channel.
  2820. */
  2821. static int
  2822. png_image_read_background(png_voidp argument)
  2823. {
  2824. png_image_read_control *display = png_voidcast(png_image_read_control*,
  2825. argument);
  2826. png_imagep image = display->image;
  2827. png_structrp png_ptr = image->opaque->png_ptr;
  2828. png_inforp info_ptr = image->opaque->info_ptr;
  2829. png_uint_32 height = image->height;
  2830. png_uint_32 width = image->width;
  2831. int pass, passes;
  2832. /* Double check the convoluted logic below. We expect to get here with
  2833. * libpng doing rgb to gray and gamma correction but background processing
  2834. * left to the png_image_read_background function. The rows libpng produce
  2835. * might be 8 or 16-bit but should always have two channels; gray plus alpha.
  2836. */
  2837. if ((png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
  2838. png_error(png_ptr, "lost rgb to gray");
  2839. if ((png_ptr->transformations & PNG_COMPOSE) != 0)
  2840. png_error(png_ptr, "unexpected compose");
  2841. if (png_get_channels(png_ptr, info_ptr) != 2)
  2842. png_error(png_ptr, "lost/gained channels");
  2843. /* Expect the 8-bit case to always remove the alpha channel */
  2844. if ((image->format & PNG_FORMAT_FLAG_LINEAR) == 0 &&
  2845. (image->format & PNG_FORMAT_FLAG_ALPHA) != 0)
  2846. png_error(png_ptr, "unexpected 8-bit transformation");
  2847. switch (png_ptr->interlaced)
  2848. {
  2849. case PNG_INTERLACE_NONE:
  2850. passes = 1;
  2851. break;
  2852. case PNG_INTERLACE_ADAM7:
  2853. passes = PNG_INTERLACE_ADAM7_PASSES;
  2854. break;
  2855. default:
  2856. png_error(png_ptr, "unknown interlace type");
  2857. }
  2858. /* Use direct access to info_ptr here because otherwise the simplified API
  2859. * would require PNG_EASY_ACCESS_SUPPORTED (just for this.) Note this is
  2860. * checking the value after libpng expansions, not the original value in the
  2861. * PNG.
  2862. */
  2863. switch (info_ptr->bit_depth)
  2864. {
  2865. case 8:
  2866. /* 8-bit sRGB gray values with an alpha channel; the alpha channel is
  2867. * to be removed by composing on a background: either the row if
  2868. * display->background is NULL or display->background->green if not.
  2869. * Unlike the code above ALPHA_OPTIMIZED has *not* been done.
  2870. */
  2871. {
  2872. png_bytep first_row = png_voidcast(png_bytep, display->first_row);
  2873. ptrdiff_t step_row = display->row_bytes;
  2874. for (pass = 0; pass < passes; ++pass)
  2875. {
  2876. unsigned int startx, stepx, stepy;
  2877. png_uint_32 y;
  2878. if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
  2879. {
  2880. /* The row may be empty for a short image: */
  2881. if (PNG_PASS_COLS(width, pass) == 0)
  2882. continue;
  2883. startx = PNG_PASS_START_COL(pass);
  2884. stepx = PNG_PASS_COL_OFFSET(pass);
  2885. y = PNG_PASS_START_ROW(pass);
  2886. stepy = PNG_PASS_ROW_OFFSET(pass);
  2887. }
  2888. else
  2889. {
  2890. y = 0;
  2891. startx = 0;
  2892. stepx = stepy = 1;
  2893. }
  2894. if (display->background == NULL)
  2895. {
  2896. for (; y<height; y += stepy)
  2897. {
  2898. png_bytep inrow = png_voidcast(png_bytep,
  2899. display->local_row);
  2900. png_bytep outrow = first_row + y * step_row;
  2901. png_const_bytep end_row = outrow + width;
  2902. /* Read the row, which is packed: */
  2903. png_read_row(png_ptr, inrow, NULL);
  2904. /* Now do the composition on each pixel in this row. */
  2905. outrow += startx;
  2906. for (; outrow < end_row; outrow += stepx)
  2907. {
  2908. png_byte alpha = inrow[1];
  2909. if (alpha > 0) /* else no change to the output */
  2910. {
  2911. png_uint_32 component = inrow[0];
  2912. if (alpha < 255) /* else just use component */
  2913. {
  2914. /* Since PNG_OPTIMIZED_ALPHA was not set it is
  2915. * necessary to invert the sRGB transfer
  2916. * function and multiply the alpha out.
  2917. */
  2918. component = png_sRGB_table[component] * alpha;
  2919. component += png_sRGB_table[outrow[0]] *
  2920. (255-alpha);
  2921. component = PNG_sRGB_FROM_LINEAR(component);
  2922. }
  2923. outrow[0] = (png_byte)component;
  2924. }
  2925. inrow += 2; /* gray and alpha channel */
  2926. }
  2927. }
  2928. }
  2929. else /* constant background value */
  2930. {
  2931. png_byte background8 = display->background->green;
  2932. png_uint_16 background = png_sRGB_table[background8];
  2933. for (; y<height; y += stepy)
  2934. {
  2935. png_bytep inrow = png_voidcast(png_bytep,
  2936. display->local_row);
  2937. png_bytep outrow = first_row + y * step_row;
  2938. png_const_bytep end_row = outrow + width;
  2939. /* Read the row, which is packed: */
  2940. png_read_row(png_ptr, inrow, NULL);
  2941. /* Now do the composition on each pixel in this row. */
  2942. outrow += startx;
  2943. for (; outrow < end_row; outrow += stepx)
  2944. {
  2945. png_byte alpha = inrow[1];
  2946. if (alpha > 0) /* else use background */
  2947. {
  2948. png_uint_32 component = inrow[0];
  2949. if (alpha < 255) /* else just use component */
  2950. {
  2951. component = png_sRGB_table[component] * alpha;
  2952. component += background * (255-alpha);
  2953. component = PNG_sRGB_FROM_LINEAR(component);
  2954. }
  2955. outrow[0] = (png_byte)component;
  2956. }
  2957. else
  2958. outrow[0] = background8;
  2959. inrow += 2; /* gray and alpha channel */
  2960. }
  2961. }
  2962. }
  2963. }
  2964. }
  2965. break;
  2966. case 16:
  2967. /* 16-bit linear with pre-multiplied alpha; the pre-multiplication must
  2968. * still be done and, maybe, the alpha channel removed. This code also
  2969. * handles the alpha-first option.
  2970. */
  2971. {
  2972. png_uint_16p first_row = png_voidcast(png_uint_16p,
  2973. display->first_row);
  2974. /* The division by two is safe because the caller passed in a
  2975. * stride which was multiplied by 2 (below) to get row_bytes.
  2976. */
  2977. ptrdiff_t step_row = display->row_bytes / 2;
  2978. unsigned int preserve_alpha = (image->format &
  2979. PNG_FORMAT_FLAG_ALPHA) != 0;
  2980. unsigned int outchannels = 1U+preserve_alpha;
  2981. int swap_alpha = 0;
  2982. # ifdef PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
  2983. if (preserve_alpha != 0 &&
  2984. (image->format & PNG_FORMAT_FLAG_AFIRST) != 0)
  2985. swap_alpha = 1;
  2986. # endif
  2987. for (pass = 0; pass < passes; ++pass)
  2988. {
  2989. unsigned int startx, stepx, stepy;
  2990. png_uint_32 y;
  2991. /* The 'x' start and step are adjusted to output components here.
  2992. */
  2993. if (png_ptr->interlaced == PNG_INTERLACE_ADAM7)
  2994. {
  2995. /* The row may be empty for a short image: */
  2996. if (PNG_PASS_COLS(width, pass) == 0)
  2997. continue;
  2998. startx = PNG_PASS_START_COL(pass) * outchannels;
  2999. stepx = PNG_PASS_COL_OFFSET(pass) * outchannels;
  3000. y = PNG_PASS_START_ROW(pass);
  3001. stepy = PNG_PASS_ROW_OFFSET(pass);
  3002. }
  3003. else
  3004. {
  3005. y = 0;
  3006. startx = 0;
  3007. stepx = outchannels;
  3008. stepy = 1;
  3009. }
  3010. for (; y<height; y += stepy)
  3011. {
  3012. png_const_uint_16p inrow;
  3013. png_uint_16p outrow = first_row + y*step_row;
  3014. png_uint_16p end_row = outrow + width * outchannels;
  3015. /* Read the row, which is packed: */
  3016. png_read_row(png_ptr, png_voidcast(png_bytep,
  3017. display->local_row), NULL);
  3018. inrow = png_voidcast(png_const_uint_16p, display->local_row);
  3019. /* Now do the pre-multiplication on each pixel in this row.
  3020. */
  3021. outrow += startx;
  3022. for (; outrow < end_row; outrow += stepx)
  3023. {
  3024. png_uint_32 component = inrow[0];
  3025. png_uint_16 alpha = inrow[1];
  3026. if (alpha > 0) /* else 0 */
  3027. {
  3028. if (alpha < 65535) /* else just use component */
  3029. {
  3030. component *= alpha;
  3031. component += 32767;
  3032. component /= 65535;
  3033. }
  3034. }
  3035. else
  3036. component = 0;
  3037. outrow[swap_alpha] = (png_uint_16)component;
  3038. if (preserve_alpha != 0)
  3039. outrow[1 ^ swap_alpha] = alpha;
  3040. inrow += 2; /* components and alpha channel */
  3041. }
  3042. }
  3043. }
  3044. }
  3045. break;
  3046. #ifdef __GNUC__
  3047. default:
  3048. png_error(png_ptr, "unexpected bit depth");
  3049. #endif
  3050. }
  3051. return 1;
  3052. }
  3053. /* The guts of png_image_finish_read as a png_safe_execute callback. */
  3054. static int
  3055. png_image_read_direct(png_voidp argument)
  3056. {
  3057. png_image_read_control *display = png_voidcast(png_image_read_control*,
  3058. argument);
  3059. png_imagep image = display->image;
  3060. png_structrp png_ptr = image->opaque->png_ptr;
  3061. png_inforp info_ptr = image->opaque->info_ptr;
  3062. png_uint_32 format = image->format;
  3063. int linear = (format & PNG_FORMAT_FLAG_LINEAR) != 0;
  3064. int do_local_compose = 0;
  3065. int do_local_background = 0; /* to avoid double gamma correction bug */
  3066. int do_local_scale = 0; /* for interlaced 16-to-8 bit conversion */
  3067. int passes = 0;
  3068. /* Add transforms to ensure the correct output format is produced then check
  3069. * that the required implementation support is there. Always expand; always
  3070. * need 8 bits minimum, no palette and expanded tRNS.
  3071. */
  3072. png_set_expand(png_ptr);
  3073. /* Now check the format to see if it was modified. */
  3074. {
  3075. png_uint_32 base_format = png_image_format(png_ptr) &
  3076. ~PNG_FORMAT_FLAG_COLORMAP /* removed by png_set_expand */;
  3077. png_uint_32 change = format ^ base_format;
  3078. png_fixed_point output_gamma;
  3079. int mode; /* alpha mode */
  3080. /* Do this first so that we have a record if rgb to gray is happening. */
  3081. if ((change & PNG_FORMAT_FLAG_COLOR) != 0)
  3082. {
  3083. /* gray<->color transformation required. */
  3084. if ((format & PNG_FORMAT_FLAG_COLOR) != 0)
  3085. png_set_gray_to_rgb(png_ptr);
  3086. else
  3087. {
  3088. /* libpng can't do both rgb to gray and
  3089. * background/pre-multiplication if there is also significant gamma
  3090. * correction, because both operations require linear colors and
  3091. * the code only supports one transform doing the gamma correction.
  3092. * Handle this by doing the pre-multiplication or background
  3093. * operation in this code, if necessary.
  3094. *
  3095. * TODO: fix this by rewriting pngrtran.c (!)
  3096. *
  3097. * For the moment (given that fixing this in pngrtran.c is an
  3098. * enormous change) 'do_local_background' is used to indicate that
  3099. * the problem exists.
  3100. */
  3101. if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
  3102. do_local_background = 1/*maybe*/;
  3103. png_set_rgb_to_gray_fixed(png_ptr, PNG_ERROR_ACTION_NONE,
  3104. PNG_RGB_TO_GRAY_DEFAULT, PNG_RGB_TO_GRAY_DEFAULT);
  3105. }
  3106. change &= ~PNG_FORMAT_FLAG_COLOR;
  3107. }
  3108. /* Set the gamma appropriately, linear for 16-bit input, sRGB otherwise.
  3109. */
  3110. {
  3111. /* This is safe but should no longer be necessary as
  3112. * png_ptr->default_gamma should have been set after the
  3113. * info-before-IDAT was read in png_image_read_header.
  3114. *
  3115. * TODO: 1.8: remove this and see what happens.
  3116. */
  3117. png_fixed_point input_gamma_default;
  3118. if ((base_format & PNG_FORMAT_FLAG_LINEAR) != 0 &&
  3119. (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
  3120. input_gamma_default = PNG_GAMMA_LINEAR;
  3121. else
  3122. input_gamma_default = PNG_DEFAULT_sRGB;
  3123. /* Call png_set_alpha_mode to set the default for the input gamma; the
  3124. * output gamma is set by a second call below.
  3125. */
  3126. png_set_alpha_mode_fixed(png_ptr, PNG_ALPHA_PNG, input_gamma_default);
  3127. }
  3128. if (linear != 0)
  3129. {
  3130. /* If there *is* an alpha channel in the input it must be multiplied
  3131. * out; use PNG_ALPHA_STANDARD, otherwise just use PNG_ALPHA_PNG.
  3132. */
  3133. if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
  3134. mode = PNG_ALPHA_STANDARD; /* associated alpha */
  3135. else
  3136. mode = PNG_ALPHA_PNG;
  3137. output_gamma = PNG_GAMMA_LINEAR;
  3138. }
  3139. else
  3140. {
  3141. mode = PNG_ALPHA_PNG;
  3142. output_gamma = PNG_DEFAULT_sRGB;
  3143. }
  3144. if ((change & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0)
  3145. {
  3146. mode = PNG_ALPHA_OPTIMIZED;
  3147. change &= ~PNG_FORMAT_FLAG_ASSOCIATED_ALPHA;
  3148. }
  3149. /* If 'do_local_background' is set check for the presence of gamma
  3150. * correction; this is part of the work-round for the libpng bug
  3151. * described above.
  3152. *
  3153. * TODO: fix libpng and remove this.
  3154. */
  3155. if (do_local_background != 0)
  3156. {
  3157. png_fixed_point gtest;
  3158. /* This is 'png_gamma_threshold' from pngrtran.c; the test used for
  3159. * gamma correction, the screen gamma hasn't been set on png_struct
  3160. * yet; it's set below. png_struct::gamma, however, is set to the
  3161. * final value.
  3162. */
  3163. if (png_muldiv(&gtest, output_gamma,
  3164. png_resolve_file_gamma(png_ptr), PNG_FP_1) != 0 &&
  3165. png_gamma_significant(gtest) == 0)
  3166. do_local_background = 0;
  3167. else if (mode == PNG_ALPHA_STANDARD)
  3168. {
  3169. do_local_background = 2/*required*/;
  3170. mode = PNG_ALPHA_PNG; /* prevent libpng doing it */
  3171. }
  3172. /* else leave as 1 for the checks below */
  3173. }
  3174. /* If the bit-depth changes then handle that here. */
  3175. if ((change & PNG_FORMAT_FLAG_LINEAR) != 0)
  3176. {
  3177. if (linear != 0 /*16-bit output*/)
  3178. png_set_expand_16(png_ptr);
  3179. else /* 8-bit output */
  3180. {
  3181. png_set_scale_16(png_ptr);
  3182. /* For interlaced images, use local_row buffer to avoid overflow
  3183. * in png_combine_row() which writes using IHDR bit-depth.
  3184. */
  3185. if (png_ptr->interlaced != 0)
  3186. do_local_scale = 1;
  3187. }
  3188. change &= ~PNG_FORMAT_FLAG_LINEAR;
  3189. }
  3190. /* Now the background/alpha channel changes. */
  3191. if ((change & PNG_FORMAT_FLAG_ALPHA) != 0)
  3192. {
  3193. /* Removing an alpha channel requires composition for the 8-bit
  3194. * formats; for the 16-bit it is already done, above, by the
  3195. * pre-multiplication and the channel just needs to be stripped.
  3196. */
  3197. if ((base_format & PNG_FORMAT_FLAG_ALPHA) != 0)
  3198. {
  3199. /* If RGB->gray is happening the alpha channel must be left and the
  3200. * operation completed locally.
  3201. *
  3202. * TODO: fix libpng and remove this.
  3203. */
  3204. if (do_local_background != 0)
  3205. do_local_background = 2/*required*/;
  3206. /* 16-bit output: just remove the channel */
  3207. else if (linear != 0) /* compose on black (well, pre-multiply) */
  3208. png_set_strip_alpha(png_ptr);
  3209. /* 8-bit output: do an appropriate compose */
  3210. else if (display->background != NULL)
  3211. {
  3212. png_color_16 c;
  3213. c.index = 0; /*unused*/
  3214. c.red = display->background->red;
  3215. c.green = display->background->green;
  3216. c.blue = display->background->blue;
  3217. c.gray = display->background->green;
  3218. /* This is always an 8-bit sRGB value, using the 'green' channel
  3219. * for gray is much better than calculating the luminance here;
  3220. * we can get off-by-one errors in that calculation relative to
  3221. * the app expectations and that will show up in transparent
  3222. * pixels.
  3223. */
  3224. png_set_background_fixed(png_ptr, &c,
  3225. PNG_BACKGROUND_GAMMA_SCREEN, 0/*need_expand*/,
  3226. 0/*gamma: not used*/);
  3227. }
  3228. else /* compose on row: implemented below. */
  3229. {
  3230. do_local_compose = 1;
  3231. /* This leaves the alpha channel in the output, so it has to be
  3232. * removed by the code below. Set the encoding to the 'OPTIMIZE'
  3233. * one so the code only has to hack on the pixels that require
  3234. * composition.
  3235. */
  3236. mode = PNG_ALPHA_OPTIMIZED;
  3237. }
  3238. }
  3239. else /* output needs an alpha channel */
  3240. {
  3241. /* This is tricky because it happens before the swap operation has
  3242. * been accomplished; however, the swap does *not* swap the added
  3243. * alpha channel (weird API), so it must be added in the correct
  3244. * place.
  3245. */
  3246. png_uint_32 filler; /* opaque filler */
  3247. int where;
  3248. if (linear != 0)
  3249. filler = 65535;
  3250. else
  3251. filler = 255;
  3252. #ifdef PNG_FORMAT_AFIRST_SUPPORTED
  3253. if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
  3254. {
  3255. where = PNG_FILLER_BEFORE;
  3256. change &= ~PNG_FORMAT_FLAG_AFIRST;
  3257. }
  3258. else
  3259. #endif
  3260. where = PNG_FILLER_AFTER;
  3261. png_set_add_alpha(png_ptr, filler, where);
  3262. }
  3263. /* This stops the (irrelevant) call to swap_alpha below. */
  3264. change &= ~PNG_FORMAT_FLAG_ALPHA;
  3265. }
  3266. /* Now set the alpha mode correctly; this is always done, even if there is
  3267. * no alpha channel in either the input or the output because it correctly
  3268. * sets the output gamma.
  3269. */
  3270. png_set_alpha_mode_fixed(png_ptr, mode, output_gamma);
  3271. # ifdef PNG_FORMAT_BGR_SUPPORTED
  3272. if ((change & PNG_FORMAT_FLAG_BGR) != 0)
  3273. {
  3274. /* Check only the output format; PNG is never BGR; don't do this if
  3275. * the output is gray, but fix up the 'format' value in that case.
  3276. */
  3277. if ((format & PNG_FORMAT_FLAG_COLOR) != 0)
  3278. png_set_bgr(png_ptr);
  3279. else
  3280. format &= ~PNG_FORMAT_FLAG_BGR;
  3281. change &= ~PNG_FORMAT_FLAG_BGR;
  3282. }
  3283. # endif
  3284. # ifdef PNG_FORMAT_AFIRST_SUPPORTED
  3285. if ((change & PNG_FORMAT_FLAG_AFIRST) != 0)
  3286. {
  3287. /* Only relevant if there is an alpha channel - it's particularly
  3288. * important to handle this correctly because do_local_compose may
  3289. * be set above and then libpng will keep the alpha channel for this
  3290. * code to remove.
  3291. */
  3292. if ((format & PNG_FORMAT_FLAG_ALPHA) != 0)
  3293. {
  3294. /* Disable this if doing a local background,
  3295. * TODO: remove this when local background is no longer required.
  3296. */
  3297. if (do_local_background != 2)
  3298. png_set_swap_alpha(png_ptr);
  3299. }
  3300. else
  3301. format &= ~PNG_FORMAT_FLAG_AFIRST;
  3302. change &= ~PNG_FORMAT_FLAG_AFIRST;
  3303. }
  3304. # endif
  3305. /* If the *output* is 16-bit then we need to check for a byte-swap on this
  3306. * architecture.
  3307. */
  3308. if (linear != 0)
  3309. {
  3310. png_uint_16 le = 0x0001;
  3311. if ((*(png_const_bytep) & le) != 0)
  3312. png_set_swap(png_ptr);
  3313. }
  3314. /* If change is not now 0 some transformation is missing - error out. */
  3315. if (change != 0)
  3316. png_error(png_ptr, "png_read_image: unsupported transformation");
  3317. }
  3318. PNG_SKIP_CHUNKS(png_ptr);
  3319. /* Update the 'info' structure and make sure the result is as required; first
  3320. * make sure to turn on the interlace handling if it will be required
  3321. * (because it can't be turned on *after* the call to png_read_update_info!)
  3322. *
  3323. * TODO: remove the do_local_background fixup below.
  3324. */
  3325. if (do_local_compose == 0 && do_local_background != 2)
  3326. passes = png_set_interlace_handling(png_ptr);
  3327. png_read_update_info(png_ptr, info_ptr);
  3328. {
  3329. png_uint_32 info_format = 0;
  3330. if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
  3331. info_format |= PNG_FORMAT_FLAG_COLOR;
  3332. if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
  3333. {
  3334. /* do_local_compose removes this channel below. */
  3335. if (do_local_compose == 0)
  3336. {
  3337. /* do_local_background does the same if required. */
  3338. if (do_local_background != 2 ||
  3339. (format & PNG_FORMAT_FLAG_ALPHA) != 0)
  3340. info_format |= PNG_FORMAT_FLAG_ALPHA;
  3341. }
  3342. }
  3343. else if (do_local_compose != 0) /* internal error */
  3344. png_error(png_ptr, "png_image_read: alpha channel lost");
  3345. if ((format & PNG_FORMAT_FLAG_ASSOCIATED_ALPHA) != 0) {
  3346. info_format |= PNG_FORMAT_FLAG_ASSOCIATED_ALPHA;
  3347. }
  3348. if (info_ptr->bit_depth == 16)
  3349. info_format |= PNG_FORMAT_FLAG_LINEAR;
  3350. #ifdef PNG_FORMAT_BGR_SUPPORTED
  3351. if ((png_ptr->transformations & PNG_BGR) != 0)
  3352. info_format |= PNG_FORMAT_FLAG_BGR;
  3353. #endif
  3354. #ifdef PNG_FORMAT_AFIRST_SUPPORTED
  3355. if (do_local_background == 2)
  3356. {
  3357. if ((format & PNG_FORMAT_FLAG_AFIRST) != 0)
  3358. info_format |= PNG_FORMAT_FLAG_AFIRST;
  3359. }
  3360. if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0 ||
  3361. ((png_ptr->transformations & PNG_ADD_ALPHA) != 0 &&
  3362. (png_ptr->flags & PNG_FLAG_FILLER_AFTER) == 0))
  3363. {
  3364. if (do_local_background == 2)
  3365. png_error(png_ptr, "unexpected alpha swap transformation");
  3366. info_format |= PNG_FORMAT_FLAG_AFIRST;
  3367. }
  3368. # endif
  3369. /* This is actually an internal error. */
  3370. if (info_format != format)
  3371. png_error(png_ptr, "png_read_image: invalid transformations");
  3372. }
  3373. /* Now read the rows. If do_local_compose is set then it is necessary to use
  3374. * a local row buffer. The output will be GA, RGBA or BGRA and must be
  3375. * converted to G, RGB or BGR as appropriate. The 'local_row' member of the
  3376. * display acts as a flag.
  3377. */
  3378. {
  3379. png_voidp first_row = display->buffer;
  3380. ptrdiff_t row_bytes = display->row_stride;
  3381. if (linear != 0)
  3382. row_bytes *= 2;
  3383. /* The following expression is designed to work correctly whether it gives
  3384. * a signed or an unsigned result.
  3385. */
  3386. if (row_bytes < 0)
  3387. {
  3388. char *ptr = png_voidcast(char*, first_row);
  3389. ptr += (image->height-1) * (-row_bytes);
  3390. first_row = png_voidcast(png_voidp, ptr);
  3391. }
  3392. display->first_row = first_row;
  3393. display->row_bytes = row_bytes;
  3394. }
  3395. if (do_local_compose != 0)
  3396. {
  3397. int result;
  3398. png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
  3399. display->local_row = row;
  3400. result = png_safe_execute(image, png_image_read_composite, display);
  3401. display->local_row = NULL;
  3402. png_free(png_ptr, row);
  3403. return result;
  3404. }
  3405. else if (do_local_background == 2)
  3406. {
  3407. int result;
  3408. png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
  3409. display->local_row = row;
  3410. result = png_safe_execute(image, png_image_read_background, display);
  3411. display->local_row = NULL;
  3412. png_free(png_ptr, row);
  3413. return result;
  3414. }
  3415. else if (do_local_scale != 0)
  3416. {
  3417. /* For interlaced 16-to-8 conversion, use an intermediate row buffer
  3418. * to avoid buffer overflows in png_combine_row. The local_row is sized
  3419. * for the transformed (8-bit) output, preventing the overflow that would
  3420. * occur if png_combine_row wrote 16-bit data directly to the user buffer.
  3421. */
  3422. int result;
  3423. png_voidp row = png_malloc(png_ptr, png_get_rowbytes(png_ptr, info_ptr));
  3424. display->local_row = row;
  3425. result = png_safe_execute(image, png_image_read_direct_scaled, display);
  3426. display->local_row = NULL;
  3427. png_free(png_ptr, row);
  3428. return result;
  3429. }
  3430. else
  3431. {
  3432. png_alloc_size_t row_bytes = (png_alloc_size_t)display->row_bytes;
  3433. while (--passes >= 0)
  3434. {
  3435. png_uint_32 y = image->height;
  3436. png_bytep row = png_voidcast(png_bytep, display->first_row);
  3437. for (; y > 0; --y)
  3438. {
  3439. png_read_row(png_ptr, row, NULL);
  3440. row += row_bytes;
  3441. }
  3442. }
  3443. return 1;
  3444. }
  3445. }
  3446. int PNGAPI
  3447. png_image_finish_read(png_imagep image, png_const_colorp background,
  3448. void *buffer, png_int_32 row_stride, void *colormap)
  3449. {
  3450. if (image != NULL && image->version == PNG_IMAGE_VERSION)
  3451. {
  3452. /* Check for row_stride overflow. This check is not performed on the
  3453. * original PNG format because it may not occur in the output PNG format
  3454. * and libpng deals with the issues of reading the original.
  3455. */
  3456. unsigned int channels = PNG_IMAGE_PIXEL_CHANNELS(image->format);
  3457. /* The following checks just the 'row_stride' calculation to ensure it
  3458. * fits in a signed 32-bit value. Because channels/components can be
  3459. * either 1 or 2 bytes in size the length of a row can still overflow 32
  3460. * bits; this is just to verify that the 'row_stride' argument can be
  3461. * represented.
  3462. */
  3463. if (image->width <= 0x7fffffffU/channels) /* no overflow */
  3464. {
  3465. png_uint_32 check;
  3466. png_uint_32 png_row_stride = image->width * channels;
  3467. if (row_stride == 0)
  3468. row_stride = (png_int_32)/*SAFE*/png_row_stride;
  3469. if (row_stride < 0)
  3470. check = (png_uint_32)(-row_stride);
  3471. else
  3472. check = (png_uint_32)row_stride;
  3473. /* This verifies 'check', the absolute value of the actual stride
  3474. * passed in and detects overflow in the application calculation (i.e.
  3475. * if the app did actually pass in a non-zero 'row_stride'.
  3476. */
  3477. if (image->opaque != NULL && buffer != NULL && check >= png_row_stride)
  3478. {
  3479. /* Now check for overflow of the image buffer calculation; this
  3480. * limits the whole image size to 32 bits for API compatibility with
  3481. * the current, 32-bit, PNG_IMAGE_BUFFER_SIZE macro.
  3482. *
  3483. * The PNG_IMAGE_BUFFER_SIZE macro is:
  3484. *
  3485. * (PNG_IMAGE_PIXEL_COMPONENT_SIZE(fmt)*height*(row_stride))
  3486. *
  3487. * And the component size is always 1 or 2, so make sure that the
  3488. * number of *bytes* that the application is saying are available
  3489. * does actually fit into a 32-bit number.
  3490. *
  3491. * NOTE: this will be changed in 1.7 because PNG_IMAGE_BUFFER_SIZE
  3492. * will be changed to use png_alloc_size_t; bigger images can be
  3493. * accommodated on 64-bit systems.
  3494. */
  3495. if (image->height <=
  3496. 0xffffffffU/PNG_IMAGE_PIXEL_COMPONENT_SIZE(image->format)/check)
  3497. {
  3498. if ((image->format & PNG_FORMAT_FLAG_COLORMAP) == 0 ||
  3499. (image->colormap_entries > 0 && colormap != NULL))
  3500. {
  3501. int result;
  3502. png_image_read_control display;
  3503. memset(&display, 0, (sizeof display));
  3504. display.image = image;
  3505. display.buffer = buffer;
  3506. display.row_stride = row_stride;
  3507. display.colormap = colormap;
  3508. display.background = background;
  3509. display.local_row = NULL;
  3510. /* Choose the correct 'end' routine; for the color-map case
  3511. * all the setup has already been done.
  3512. */
  3513. if ((image->format & PNG_FORMAT_FLAG_COLORMAP) != 0)
  3514. result =
  3515. png_safe_execute(image,
  3516. png_image_read_colormap, &display) &&
  3517. png_safe_execute(image,
  3518. png_image_read_colormapped, &display);
  3519. else
  3520. result =
  3521. png_safe_execute(image,
  3522. png_image_read_direct, &display);
  3523. png_image_free(image);
  3524. return result;
  3525. }
  3526. else
  3527. return png_image_error(image,
  3528. "png_image_finish_read[color-map]: no color-map");
  3529. }
  3530. else
  3531. return png_image_error(image,
  3532. "png_image_finish_read: image too large");
  3533. }
  3534. else
  3535. return png_image_error(image,
  3536. "png_image_finish_read: invalid argument");
  3537. }
  3538. else
  3539. return png_image_error(image,
  3540. "png_image_finish_read: row_stride too large");
  3541. }
  3542. else if (image != NULL)
  3543. return png_image_error(image,
  3544. "png_image_finish_read: damaged PNG_IMAGE_VERSION");
  3545. return 0;
  3546. }
  3547. #endif /* SIMPLIFIED_READ */
  3548. #endif /* READ */