pngread.c 141 KB

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