pngread.c 139 KB

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