pngread.c 138 KB

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