pngread.c 141 KB

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