pngread.c 141 KB

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