pngrtran.c 166 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118
  1. /* pngrtran.c - transforms the data in a row for PNG readers
  2. *
  3. * Copyright (c) 2018-2025 Cosmin Truta
  4. * Copyright (c) 1998-2002,2004,2006-2018 Glenn Randers-Pehrson
  5. * Copyright (c) 1996-1997 Andreas Dilger
  6. * Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.
  7. *
  8. * This code is released under the libpng license.
  9. * For conditions of distribution and use, see the disclaimer
  10. * and license in png.h
  11. *
  12. * This file contains functions optionally called by an application
  13. * in order to tell libpng how to handle data when reading a PNG.
  14. * Transformations that are used in both reading and writing are
  15. * in pngtrans.c.
  16. */
  17. #include "pngpriv.h"
  18. #ifdef PNG_ARM_NEON_IMPLEMENTATION
  19. # if PNG_ARM_NEON_IMPLEMENTATION == 1
  20. # define PNG_ARM_NEON_INTRINSICS_AVAILABLE
  21. # if defined(_MSC_VER) && !defined(__clang__) && defined(_M_ARM64)
  22. # include <arm64_neon.h>
  23. # else
  24. # include <arm_neon.h>
  25. # endif
  26. # endif
  27. #endif
  28. #ifdef PNG_READ_SUPPORTED
  29. /* Set the action on getting a CRC error for an ancillary or critical chunk. */
  30. void PNGAPI
  31. png_set_crc_action(png_structrp png_ptr, int crit_action, int ancil_action)
  32. {
  33. png_debug(1, "in png_set_crc_action");
  34. if (png_ptr == NULL)
  35. return;
  36. /* Tell libpng how we react to CRC errors in critical chunks */
  37. switch (crit_action)
  38. {
  39. case PNG_CRC_NO_CHANGE: /* Leave setting as is */
  40. break;
  41. case PNG_CRC_WARN_USE: /* Warn/use data */
  42. png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
  43. png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
  44. break;
  45. case PNG_CRC_QUIET_USE: /* Quiet/use data */
  46. png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
  47. png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
  48. PNG_FLAG_CRC_CRITICAL_IGNORE;
  49. break;
  50. case PNG_CRC_WARN_DISCARD: /* Not a valid action for critical data */
  51. png_warning(png_ptr,
  52. "Can't discard critical data on CRC error");
  53. /* FALLTHROUGH */
  54. case PNG_CRC_ERROR_QUIT: /* Error/quit */
  55. case PNG_CRC_DEFAULT:
  56. default:
  57. png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
  58. break;
  59. }
  60. /* Tell libpng how we react to CRC errors in ancillary chunks */
  61. switch (ancil_action)
  62. {
  63. case PNG_CRC_NO_CHANGE: /* Leave setting as is */
  64. break;
  65. case PNG_CRC_WARN_USE: /* Warn/use data */
  66. png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
  67. png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
  68. break;
  69. case PNG_CRC_QUIET_USE: /* Quiet/use data */
  70. png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
  71. png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
  72. PNG_FLAG_CRC_ANCILLARY_NOWARN;
  73. break;
  74. case PNG_CRC_ERROR_QUIT: /* Error/quit */
  75. png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
  76. png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
  77. break;
  78. case PNG_CRC_WARN_DISCARD: /* Warn/discard data */
  79. case PNG_CRC_DEFAULT:
  80. default:
  81. png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
  82. break;
  83. }
  84. }
  85. #ifdef PNG_READ_TRANSFORMS_SUPPORTED
  86. /* Is it OK to set a transformation now? Only if png_start_read_image or
  87. * png_read_update_info have not been called. It is not necessary for the IHDR
  88. * to have been read in all cases; the need_IHDR parameter allows for this
  89. * check too.
  90. */
  91. static int
  92. png_rtran_ok(png_structrp png_ptr, int need_IHDR)
  93. {
  94. if (png_ptr != NULL)
  95. {
  96. if ((png_ptr->flags & PNG_FLAG_ROW_INIT) != 0)
  97. png_app_error(png_ptr,
  98. "invalid after png_start_read_image or png_read_update_info");
  99. else if (need_IHDR && (png_ptr->mode & PNG_HAVE_IHDR) == 0)
  100. png_app_error(png_ptr, "invalid before the PNG header has been read");
  101. else
  102. {
  103. /* Turn on failure to initialize correctly for all transforms. */
  104. png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED;
  105. return 1; /* Ok */
  106. }
  107. }
  108. return 0; /* no png_error possible! */
  109. }
  110. #endif
  111. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  112. /* Handle alpha and tRNS via a background color */
  113. void PNGFAPI
  114. png_set_background_fixed(png_structrp png_ptr,
  115. png_const_color_16p background_color, int background_gamma_code,
  116. int need_expand, png_fixed_point background_gamma)
  117. {
  118. png_debug(1, "in png_set_background_fixed");
  119. if (png_rtran_ok(png_ptr, 0) == 0 || background_color == NULL)
  120. return;
  121. if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
  122. {
  123. png_warning(png_ptr, "Application must supply a known background gamma");
  124. return;
  125. }
  126. png_ptr->transformations |= PNG_COMPOSE | PNG_STRIP_ALPHA;
  127. png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
  128. png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
  129. png_ptr->background = *background_color;
  130. png_ptr->background_gamma = background_gamma;
  131. png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
  132. if (need_expand != 0)
  133. png_ptr->transformations |= PNG_BACKGROUND_EXPAND;
  134. else
  135. png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND;
  136. }
  137. # ifdef PNG_FLOATING_POINT_SUPPORTED
  138. void PNGAPI
  139. png_set_background(png_structrp png_ptr,
  140. png_const_color_16p background_color, int background_gamma_code,
  141. int need_expand, double background_gamma)
  142. {
  143. png_set_background_fixed(png_ptr, background_color, background_gamma_code,
  144. need_expand, png_fixed(png_ptr, background_gamma, "png_set_background"));
  145. }
  146. # endif /* FLOATING_POINT */
  147. #endif /* READ_BACKGROUND */
  148. /* Scale 16-bit depth files to 8-bit depth. If both of these are set then the
  149. * one that pngrtran does first (scale) happens. This is necessary to allow the
  150. * TRANSFORM and API behavior to be somewhat consistent, and it's simpler.
  151. */
  152. #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  153. void PNGAPI
  154. png_set_scale_16(png_structrp png_ptr)
  155. {
  156. png_debug(1, "in png_set_scale_16");
  157. if (png_rtran_ok(png_ptr, 0) == 0)
  158. return;
  159. png_ptr->transformations |= PNG_SCALE_16_TO_8;
  160. }
  161. #endif
  162. #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
  163. /* Chop 16-bit depth files to 8-bit depth */
  164. void PNGAPI
  165. png_set_strip_16(png_structrp png_ptr)
  166. {
  167. png_debug(1, "in png_set_strip_16");
  168. if (png_rtran_ok(png_ptr, 0) == 0)
  169. return;
  170. png_ptr->transformations |= PNG_16_TO_8;
  171. }
  172. #endif
  173. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  174. void PNGAPI
  175. png_set_strip_alpha(png_structrp png_ptr)
  176. {
  177. png_debug(1, "in png_set_strip_alpha");
  178. if (png_rtran_ok(png_ptr, 0) == 0)
  179. return;
  180. png_ptr->transformations |= PNG_STRIP_ALPHA;
  181. }
  182. #endif
  183. #if defined(PNG_READ_ALPHA_MODE_SUPPORTED) || defined(PNG_READ_GAMMA_SUPPORTED)
  184. /* PNGv3 conformance: this private API exists to resolve the now mandatory error
  185. * resolution when multiple conflicting sources of gamma or colour space
  186. * information are available.
  187. *
  188. * Terminology (assuming power law, "gamma", encodings):
  189. * "screen" gamma: a power law imposed by the output device when digital
  190. * samples are converted to visible light output. The EOTF - volage to
  191. * luminance on output.
  192. *
  193. * "file" gamma: a power law used to encode luminance levels from the input
  194. * data (the scene or the mastering display system) into digital voltages.
  195. * The OETF - luminance to voltage on input.
  196. *
  197. * gamma "correction": a power law matching the **inverse** of the overall
  198. * transfer function from input luminance levels to output levels. The
  199. * **inverse** of the OOTF; the correction "corrects" for the OOTF by aiming
  200. * to make the overall OOTF (including the correction) linear.
  201. *
  202. * It is important to understand this terminology because the defined terms are
  203. * scattered throughout the libpng code and it is very easy to end up with the
  204. * inverse of the power law required.
  205. *
  206. * Variable and struct::member names:
  207. * file_gamma OETF how the PNG data was encoded
  208. *
  209. * screen_gamma EOTF how the screen will decode digital levels
  210. *
  211. * -- not used -- OOTF the net effect OETF x EOTF
  212. * gamma_correction the inverse of OOTF to make the result linear
  213. *
  214. * All versions of libpng require a call to "png_set_gamma" to establish the
  215. * "screen" gamma, the power law representing the EOTF. png_set_gamma may also
  216. * set or default the "file" gamma; the OETF. gamma_correction is calculated
  217. * internally.
  218. *
  219. * The earliest libpng versions required file_gamma to be supplied to set_gamma.
  220. * Later versions started allowing png_set_gamma and, later, png_set_alpha_mode,
  221. * to cause defaulting from the file data.
  222. *
  223. * PNGv3 mandated a particular form for this defaulting, one that is compatible
  224. * with what libpng did except that if libpng detected inconsistencies it marked
  225. * all the chunks as "invalid". PNGv3 effectively invalidates this prior code.
  226. *
  227. * Behaviour implemented below:
  228. * translate_gamma_flags(gamma, is_screen)
  229. * The libpng-1.6 API for the gamma parameters to libpng APIs
  230. * (png_set_gamma and png_set_alpha_mode at present). This allows the
  231. * 'gamma' value to be passed as a png_fixed_point number or as one of a
  232. * set of integral values for specific "well known" examples of transfer
  233. * functions. This is compatible with PNGv3.
  234. */
  235. static png_fixed_point
  236. translate_gamma_flags(png_fixed_point output_gamma, int is_screen)
  237. {
  238. /* Check for flag values. The main reason for having the old Mac value as a
  239. * flag is that it is pretty near impossible to work out what the correct
  240. * value is from Apple documentation - a working Mac system is needed to
  241. * discover the value!
  242. */
  243. if (output_gamma == PNG_DEFAULT_sRGB ||
  244. output_gamma == PNG_FP_1 / PNG_DEFAULT_sRGB)
  245. {
  246. if (is_screen != 0)
  247. output_gamma = PNG_GAMMA_sRGB;
  248. else
  249. output_gamma = PNG_GAMMA_sRGB_INVERSE;
  250. }
  251. else if (output_gamma == PNG_GAMMA_MAC_18 ||
  252. output_gamma == PNG_FP_1 / PNG_GAMMA_MAC_18)
  253. {
  254. if (is_screen != 0)
  255. output_gamma = PNG_GAMMA_MAC_OLD;
  256. else
  257. output_gamma = PNG_GAMMA_MAC_INVERSE;
  258. }
  259. return output_gamma;
  260. }
  261. # ifdef PNG_FLOATING_POINT_SUPPORTED
  262. static png_fixed_point
  263. convert_gamma_value(png_structrp png_ptr, double output_gamma)
  264. {
  265. /* The following silently ignores cases where fixed point (times 100,000)
  266. * gamma values are passed to the floating point API. This is safe and it
  267. * means the fixed point constants work just fine with the floating point
  268. * API. The alternative would just lead to undetected errors and spurious
  269. * bug reports. Negative values fail inside the _fixed API unless they
  270. * correspond to the flag values.
  271. */
  272. if (output_gamma > 0 && output_gamma < 128)
  273. output_gamma *= PNG_FP_1;
  274. /* This preserves -1 and -2 exactly: */
  275. output_gamma = floor(output_gamma + .5);
  276. if (output_gamma > PNG_FP_MAX || output_gamma < PNG_FP_MIN)
  277. png_fixed_error(png_ptr, "gamma value");
  278. return (png_fixed_point)output_gamma;
  279. }
  280. # endif
  281. static int
  282. unsupported_gamma(png_structrp png_ptr, png_fixed_point gamma, int warn)
  283. {
  284. /* Validate a gamma value to ensure it is in a reasonable range. The value
  285. * is expected to be 1 or greater, but this range test allows for some
  286. * viewing correction values. The intent is to weed out the API users
  287. * who might use the inverse of the gamma value accidentally!
  288. *
  289. * 1.6.47: apply the test in png_set_gamma as well but only warn and return
  290. * false if it fires.
  291. *
  292. * TODO: 1.8: make this an app_error in png_set_gamma as well.
  293. */
  294. if (gamma < PNG_LIB_GAMMA_MIN || gamma > PNG_LIB_GAMMA_MAX)
  295. {
  296. # define msg "gamma out of supported range"
  297. if (warn)
  298. png_app_warning(png_ptr, msg);
  299. else
  300. png_app_error(png_ptr, msg);
  301. return 1;
  302. # undef msg
  303. }
  304. return 0;
  305. }
  306. #endif /* READ_ALPHA_MODE || READ_GAMMA */
  307. #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
  308. void PNGFAPI
  309. png_set_alpha_mode_fixed(png_structrp png_ptr, int mode,
  310. png_fixed_point output_gamma)
  311. {
  312. png_fixed_point file_gamma;
  313. int compose = 0;
  314. png_debug(1, "in png_set_alpha_mode_fixed");
  315. if (png_rtran_ok(png_ptr, 0) == 0)
  316. return;
  317. output_gamma = translate_gamma_flags(output_gamma, 1/*screen*/);
  318. if (unsupported_gamma(png_ptr, output_gamma, 0/*error*/))
  319. return;
  320. /* The default file gamma is the inverse of the output gamma; the output
  321. * gamma may be changed below so get the file value first. The default_gamma
  322. * is set here and from the simplified API (which uses a different algorithm)
  323. * so don't overwrite a set value:
  324. */
  325. file_gamma = png_ptr->default_gamma;
  326. if (file_gamma == 0)
  327. {
  328. file_gamma = png_reciprocal(output_gamma);
  329. png_ptr->default_gamma = file_gamma;
  330. }
  331. /* There are really 8 possibilities here, composed of any combination
  332. * of:
  333. *
  334. * premultiply the color channels
  335. * do not encode non-opaque pixels
  336. * encode the alpha as well as the color channels
  337. *
  338. * The differences disappear if the input/output ('screen') gamma is 1.0,
  339. * because then the encoding is a no-op and there is only the choice of
  340. * premultiplying the color channels or not.
  341. *
  342. * png_set_alpha_mode and png_set_background interact because both use
  343. * png_compose to do the work. Calling both is only useful when
  344. * png_set_alpha_mode is used to set the default mode - PNG_ALPHA_PNG - along
  345. * with a default gamma value. Otherwise PNG_COMPOSE must not be set.
  346. */
  347. switch (mode)
  348. {
  349. case PNG_ALPHA_PNG: /* default: png standard */
  350. /* No compose, but it may be set by png_set_background! */
  351. png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
  352. png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
  353. break;
  354. case PNG_ALPHA_ASSOCIATED: /* color channels premultiplied */
  355. compose = 1;
  356. png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
  357. png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
  358. /* The output is linear: */
  359. output_gamma = PNG_FP_1;
  360. break;
  361. case PNG_ALPHA_OPTIMIZED: /* associated, non-opaque pixels linear */
  362. compose = 1;
  363. png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
  364. png_ptr->flags |= PNG_FLAG_OPTIMIZE_ALPHA;
  365. /* output_gamma records the encoding of opaque pixels! */
  366. break;
  367. case PNG_ALPHA_BROKEN: /* associated, non-linear, alpha encoded */
  368. compose = 1;
  369. png_ptr->transformations |= PNG_ENCODE_ALPHA;
  370. png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
  371. break;
  372. default:
  373. png_error(png_ptr, "invalid alpha mode");
  374. }
  375. /* Set the screen gamma values: */
  376. png_ptr->screen_gamma = output_gamma;
  377. /* Finally, if pre-multiplying, set the background fields to achieve the
  378. * desired result.
  379. */
  380. if (compose != 0)
  381. {
  382. /* And obtain alpha pre-multiplication by composing on black: */
  383. memset(&png_ptr->background, 0, (sizeof png_ptr->background));
  384. png_ptr->background_gamma = file_gamma; /* just in case */
  385. png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_FILE;
  386. png_ptr->transformations &= ~PNG_BACKGROUND_EXPAND;
  387. if ((png_ptr->transformations & PNG_COMPOSE) != 0)
  388. png_error(png_ptr,
  389. "conflicting calls to set alpha mode and background");
  390. png_ptr->transformations |= PNG_COMPOSE;
  391. }
  392. }
  393. # ifdef PNG_FLOATING_POINT_SUPPORTED
  394. void PNGAPI
  395. png_set_alpha_mode(png_structrp png_ptr, int mode, double output_gamma)
  396. {
  397. png_set_alpha_mode_fixed(png_ptr, mode, convert_gamma_value(png_ptr,
  398. output_gamma));
  399. }
  400. # endif
  401. #endif
  402. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  403. /* Dither file to 8-bit. Supply a palette, the current number
  404. * of elements in the palette, the maximum number of elements
  405. * allowed, and a histogram if possible. If the current number
  406. * of colors is greater than the maximum number, the palette will be
  407. * modified to fit in the maximum number. "full_quantize" indicates
  408. * whether we need a quantizing cube set up for RGB images, or if we
  409. * simply are reducing the number of colors in a paletted image.
  410. */
  411. typedef struct png_dsort_struct
  412. {
  413. struct png_dsort_struct * next;
  414. png_byte left;
  415. png_byte right;
  416. } png_dsort;
  417. typedef png_dsort * png_dsortp;
  418. typedef png_dsort * * png_dsortpp;
  419. void PNGAPI
  420. png_set_quantize(png_structrp png_ptr, png_colorp palette,
  421. int num_palette, int maximum_colors, png_const_uint_16p histogram,
  422. int full_quantize)
  423. {
  424. png_debug(1, "in png_set_quantize");
  425. if (png_rtran_ok(png_ptr, 0) == 0)
  426. return;
  427. png_ptr->transformations |= PNG_QUANTIZE;
  428. if (full_quantize == 0)
  429. {
  430. int i;
  431. png_ptr->quantize_index = (png_bytep)png_malloc(png_ptr,
  432. (png_alloc_size_t)num_palette);
  433. for (i = 0; i < num_palette; i++)
  434. png_ptr->quantize_index[i] = (png_byte)i;
  435. }
  436. if (num_palette > maximum_colors)
  437. {
  438. if (histogram != NULL)
  439. {
  440. /* This is easy enough, just throw out the least used colors.
  441. * Perhaps not the best solution, but good enough.
  442. */
  443. int i;
  444. /* Initialize an array to sort colors */
  445. png_ptr->quantize_sort = (png_bytep)png_malloc(png_ptr,
  446. (png_alloc_size_t)num_palette);
  447. /* Initialize the quantize_sort array */
  448. for (i = 0; i < num_palette; i++)
  449. png_ptr->quantize_sort[i] = (png_byte)i;
  450. /* Find the least used palette entries by starting a
  451. * bubble sort, and running it until we have sorted
  452. * out enough colors. Note that we don't care about
  453. * sorting all the colors, just finding which are
  454. * least used.
  455. */
  456. for (i = num_palette - 1; i >= maximum_colors; i--)
  457. {
  458. int done; /* To stop early if the list is pre-sorted */
  459. int j;
  460. done = 1;
  461. for (j = 0; j < i; j++)
  462. {
  463. if (histogram[png_ptr->quantize_sort[j]]
  464. < histogram[png_ptr->quantize_sort[j + 1]])
  465. {
  466. png_byte t;
  467. t = png_ptr->quantize_sort[j];
  468. png_ptr->quantize_sort[j] = png_ptr->quantize_sort[j + 1];
  469. png_ptr->quantize_sort[j + 1] = t;
  470. done = 0;
  471. }
  472. }
  473. if (done != 0)
  474. break;
  475. }
  476. /* Swap the palette around, and set up a table, if necessary */
  477. if (full_quantize != 0)
  478. {
  479. int j = num_palette;
  480. /* Put all the useful colors within the max, but don't
  481. * move the others.
  482. */
  483. for (i = 0; i < maximum_colors; i++)
  484. {
  485. if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
  486. {
  487. do
  488. j--;
  489. while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
  490. palette[i] = palette[j];
  491. }
  492. }
  493. }
  494. else
  495. {
  496. int j = num_palette;
  497. /* Move all the used colors inside the max limit, and
  498. * develop a translation table.
  499. */
  500. for (i = 0; i < maximum_colors; i++)
  501. {
  502. /* Only move the colors we need to */
  503. if ((int)png_ptr->quantize_sort[i] >= maximum_colors)
  504. {
  505. png_color tmp_color;
  506. do
  507. j--;
  508. while ((int)png_ptr->quantize_sort[j] >= maximum_colors);
  509. tmp_color = palette[j];
  510. palette[j] = palette[i];
  511. palette[i] = tmp_color;
  512. /* Indicate where the color went */
  513. png_ptr->quantize_index[j] = (png_byte)i;
  514. png_ptr->quantize_index[i] = (png_byte)j;
  515. }
  516. }
  517. /* Find closest color for those colors we are not using */
  518. for (i = 0; i < num_palette; i++)
  519. {
  520. if ((int)png_ptr->quantize_index[i] >= maximum_colors)
  521. {
  522. int min_d, k, min_k, d_index;
  523. /* Find the closest color to one we threw out */
  524. d_index = png_ptr->quantize_index[i];
  525. min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
  526. for (k = 1, min_k = 0; k < maximum_colors; k++)
  527. {
  528. int d;
  529. d = PNG_COLOR_DIST(palette[d_index], palette[k]);
  530. if (d < min_d)
  531. {
  532. min_d = d;
  533. min_k = k;
  534. }
  535. }
  536. /* Point to closest color */
  537. png_ptr->quantize_index[i] = (png_byte)min_k;
  538. }
  539. }
  540. }
  541. png_free(png_ptr, png_ptr->quantize_sort);
  542. png_ptr->quantize_sort = NULL;
  543. }
  544. else
  545. {
  546. /* This is much harder to do simply (and quickly). Perhaps
  547. * we need to go through a median cut routine, but those
  548. * don't always behave themselves with only a few colors
  549. * as input. So we will just find the closest two colors,
  550. * and throw out one of them (chosen somewhat randomly).
  551. * [We don't understand this at all, so if someone wants to
  552. * work on improving it, be our guest - AED, GRP]
  553. */
  554. int i;
  555. int max_d;
  556. int num_new_palette;
  557. png_dsortp t;
  558. png_dsortpp hash;
  559. t = NULL;
  560. /* Initialize palette index arrays */
  561. png_ptr->index_to_palette = (png_bytep)png_malloc(png_ptr,
  562. (png_alloc_size_t)num_palette);
  563. png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
  564. (png_alloc_size_t)num_palette);
  565. /* Initialize the sort array */
  566. for (i = 0; i < num_palette; i++)
  567. {
  568. png_ptr->index_to_palette[i] = (png_byte)i;
  569. png_ptr->palette_to_index[i] = (png_byte)i;
  570. }
  571. hash = (png_dsortpp)png_calloc(png_ptr, (png_alloc_size_t)(769 *
  572. (sizeof (png_dsortp))));
  573. num_new_palette = num_palette;
  574. /* Initial wild guess at how far apart the farthest pixel
  575. * pair we will be eliminating will be. Larger
  576. * numbers mean more areas will be allocated, Smaller
  577. * numbers run the risk of not saving enough data, and
  578. * having to do this all over again.
  579. *
  580. * I have not done extensive checking on this number.
  581. */
  582. max_d = 96;
  583. while (num_new_palette > maximum_colors)
  584. {
  585. for (i = 0; i < num_new_palette - 1; i++)
  586. {
  587. int j;
  588. for (j = i + 1; j < num_new_palette; j++)
  589. {
  590. int d;
  591. d = PNG_COLOR_DIST(palette[i], palette[j]);
  592. if (d <= max_d)
  593. {
  594. t = (png_dsortp)png_malloc_warn(png_ptr,
  595. (png_alloc_size_t)(sizeof (png_dsort)));
  596. if (t == NULL)
  597. break;
  598. t->next = hash[d];
  599. t->left = (png_byte)i;
  600. t->right = (png_byte)j;
  601. hash[d] = t;
  602. }
  603. }
  604. if (t == NULL)
  605. break;
  606. }
  607. if (t != NULL)
  608. for (i = 0; i <= max_d; i++)
  609. {
  610. if (hash[i] != NULL)
  611. {
  612. png_dsortp p;
  613. for (p = hash[i]; p; p = p->next)
  614. {
  615. if ((int)png_ptr->index_to_palette[p->left]
  616. < num_new_palette &&
  617. (int)png_ptr->index_to_palette[p->right]
  618. < num_new_palette)
  619. {
  620. int j, next_j;
  621. if (num_new_palette & 0x01)
  622. {
  623. j = p->left;
  624. next_j = p->right;
  625. }
  626. else
  627. {
  628. j = p->right;
  629. next_j = p->left;
  630. }
  631. num_new_palette--;
  632. palette[png_ptr->index_to_palette[j]]
  633. = palette[num_new_palette];
  634. if (full_quantize == 0)
  635. {
  636. int k;
  637. for (k = 0; k < num_palette; k++)
  638. {
  639. if (png_ptr->quantize_index[k] ==
  640. png_ptr->index_to_palette[j])
  641. png_ptr->quantize_index[k] =
  642. png_ptr->index_to_palette[next_j];
  643. if ((int)png_ptr->quantize_index[k] ==
  644. num_new_palette)
  645. png_ptr->quantize_index[k] =
  646. png_ptr->index_to_palette[j];
  647. }
  648. }
  649. png_ptr->index_to_palette[png_ptr->palette_to_index
  650. [num_new_palette]] = png_ptr->index_to_palette[j];
  651. png_ptr->palette_to_index[png_ptr->index_to_palette[j]]
  652. = png_ptr->palette_to_index[num_new_palette];
  653. png_ptr->index_to_palette[j] =
  654. (png_byte)num_new_palette;
  655. png_ptr->palette_to_index[num_new_palette] =
  656. (png_byte)j;
  657. }
  658. if (num_new_palette <= maximum_colors)
  659. break;
  660. }
  661. if (num_new_palette <= maximum_colors)
  662. break;
  663. }
  664. }
  665. for (i = 0; i < 769; i++)
  666. {
  667. if (hash[i] != NULL)
  668. {
  669. png_dsortp p = hash[i];
  670. while (p)
  671. {
  672. t = p->next;
  673. png_free(png_ptr, p);
  674. p = t;
  675. }
  676. }
  677. hash[i] = 0;
  678. }
  679. max_d += 96;
  680. }
  681. png_free(png_ptr, hash);
  682. png_free(png_ptr, png_ptr->palette_to_index);
  683. png_free(png_ptr, png_ptr->index_to_palette);
  684. png_ptr->palette_to_index = NULL;
  685. png_ptr->index_to_palette = NULL;
  686. }
  687. num_palette = maximum_colors;
  688. }
  689. if (png_ptr->palette == NULL)
  690. {
  691. png_ptr->palette = palette;
  692. }
  693. png_ptr->num_palette = (png_uint_16)num_palette;
  694. if (full_quantize != 0)
  695. {
  696. int i;
  697. png_bytep distance;
  698. int total_bits = PNG_QUANTIZE_RED_BITS + PNG_QUANTIZE_GREEN_BITS +
  699. PNG_QUANTIZE_BLUE_BITS;
  700. int num_red = (1 << PNG_QUANTIZE_RED_BITS);
  701. int num_green = (1 << PNG_QUANTIZE_GREEN_BITS);
  702. int num_blue = (1 << PNG_QUANTIZE_BLUE_BITS);
  703. size_t num_entries = ((size_t)1 << total_bits);
  704. png_ptr->palette_lookup = (png_bytep)png_calloc(png_ptr,
  705. (png_alloc_size_t)(num_entries));
  706. distance = (png_bytep)png_malloc(png_ptr, (png_alloc_size_t)num_entries);
  707. memset(distance, 0xff, num_entries);
  708. for (i = 0; i < num_palette; i++)
  709. {
  710. int ir, ig, ib;
  711. int r = (palette[i].red >> (8 - PNG_QUANTIZE_RED_BITS));
  712. int g = (palette[i].green >> (8 - PNG_QUANTIZE_GREEN_BITS));
  713. int b = (palette[i].blue >> (8 - PNG_QUANTIZE_BLUE_BITS));
  714. for (ir = 0; ir < num_red; ir++)
  715. {
  716. /* int dr = abs(ir - r); */
  717. int dr = ((ir > r) ? ir - r : r - ir);
  718. int index_r = (ir << (PNG_QUANTIZE_BLUE_BITS +
  719. PNG_QUANTIZE_GREEN_BITS));
  720. for (ig = 0; ig < num_green; ig++)
  721. {
  722. /* int dg = abs(ig - g); */
  723. int dg = ((ig > g) ? ig - g : g - ig);
  724. int dt = dr + dg;
  725. int dm = ((dr > dg) ? dr : dg);
  726. int index_g = index_r | (ig << PNG_QUANTIZE_BLUE_BITS);
  727. for (ib = 0; ib < num_blue; ib++)
  728. {
  729. int d_index = index_g | ib;
  730. /* int db = abs(ib - b); */
  731. int db = ((ib > b) ? ib - b : b - ib);
  732. int dmax = ((dm > db) ? dm : db);
  733. int d = dmax + dt + db;
  734. if (d < (int)distance[d_index])
  735. {
  736. distance[d_index] = (png_byte)d;
  737. png_ptr->palette_lookup[d_index] = (png_byte)i;
  738. }
  739. }
  740. }
  741. }
  742. }
  743. png_free(png_ptr, distance);
  744. }
  745. }
  746. #endif /* READ_QUANTIZE */
  747. #ifdef PNG_READ_GAMMA_SUPPORTED
  748. void PNGFAPI
  749. png_set_gamma_fixed(png_structrp png_ptr, png_fixed_point scrn_gamma,
  750. png_fixed_point file_gamma)
  751. {
  752. png_debug(1, "in png_set_gamma_fixed");
  753. if (png_rtran_ok(png_ptr, 0) == 0)
  754. return;
  755. /* New in libpng-1.5.4 - reserve particular negative values as flags. */
  756. scrn_gamma = translate_gamma_flags(scrn_gamma, 1/*screen*/);
  757. file_gamma = translate_gamma_flags(file_gamma, 0/*file*/);
  758. /* Checking the gamma values for being >0 was added in 1.5.4 along with the
  759. * premultiplied alpha support; this actually hides an undocumented feature
  760. * of the previous implementation which allowed gamma processing to be
  761. * disabled in background handling. There is no evidence (so far) that this
  762. * was being used; however, png_set_background itself accepted and must still
  763. * accept '0' for the gamma value it takes, because it isn't always used.
  764. *
  765. * Since this is an API change (albeit a very minor one that removes an
  766. * undocumented API feature) the following checks were only enabled in
  767. * libpng-1.6.0.
  768. */
  769. if (file_gamma <= 0)
  770. png_app_error(png_ptr, "invalid file gamma in png_set_gamma");
  771. if (scrn_gamma <= 0)
  772. png_app_error(png_ptr, "invalid screen gamma in png_set_gamma");
  773. if (unsupported_gamma(png_ptr, file_gamma, 1/*warn*/) ||
  774. unsupported_gamma(png_ptr, scrn_gamma, 1/*warn*/))
  775. return;
  776. /* 1.6.47: png_struct::file_gamma and png_struct::screen_gamma are now only
  777. * written by this API. This removes dependencies on the order of API calls
  778. * and allows the complex gamma checks to be delayed until needed.
  779. */
  780. png_ptr->file_gamma = file_gamma;
  781. png_ptr->screen_gamma = scrn_gamma;
  782. }
  783. # ifdef PNG_FLOATING_POINT_SUPPORTED
  784. void PNGAPI
  785. png_set_gamma(png_structrp png_ptr, double scrn_gamma, double file_gamma)
  786. {
  787. png_set_gamma_fixed(png_ptr, convert_gamma_value(png_ptr, scrn_gamma),
  788. convert_gamma_value(png_ptr, file_gamma));
  789. }
  790. # endif /* FLOATING_POINT */
  791. #endif /* READ_GAMMA */
  792. #ifdef PNG_READ_EXPAND_SUPPORTED
  793. /* Expand paletted images to RGB, expand grayscale images of
  794. * less than 8-bit depth to 8-bit depth, and expand tRNS chunks
  795. * to alpha channels.
  796. */
  797. void PNGAPI
  798. png_set_expand(png_structrp png_ptr)
  799. {
  800. png_debug(1, "in png_set_expand");
  801. if (png_rtran_ok(png_ptr, 0) == 0)
  802. return;
  803. png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
  804. }
  805. /* GRR 19990627: the following three functions currently are identical
  806. * to png_set_expand(). However, it is entirely reasonable that someone
  807. * might wish to expand an indexed image to RGB but *not* expand a single,
  808. * fully transparent palette entry to a full alpha channel--perhaps instead
  809. * convert tRNS to the grayscale/RGB format (16-bit RGB value), or replace
  810. * the transparent color with a particular RGB value, or drop tRNS entirely.
  811. * IOW, a future version of the library may make the transformations flag
  812. * a bit more fine-grained, with separate bits for each of these three
  813. * functions.
  814. *
  815. * More to the point, these functions make it obvious what libpng will be
  816. * doing, whereas "expand" can (and does) mean any number of things.
  817. *
  818. * GRP 20060307: In libpng-1.2.9, png_set_gray_1_2_4_to_8() was modified
  819. * to expand only the sample depth but not to expand the tRNS to alpha
  820. * and its name was changed to png_set_expand_gray_1_2_4_to_8().
  821. */
  822. /* Expand paletted images to RGB. */
  823. void PNGAPI
  824. png_set_palette_to_rgb(png_structrp png_ptr)
  825. {
  826. png_debug(1, "in png_set_palette_to_rgb");
  827. if (png_rtran_ok(png_ptr, 0) == 0)
  828. return;
  829. png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
  830. }
  831. /* Expand grayscale images of less than 8-bit depth to 8 bits. */
  832. void PNGAPI
  833. png_set_expand_gray_1_2_4_to_8(png_structrp png_ptr)
  834. {
  835. png_debug(1, "in png_set_expand_gray_1_2_4_to_8");
  836. if (png_rtran_ok(png_ptr, 0) == 0)
  837. return;
  838. png_ptr->transformations |= PNG_EXPAND;
  839. }
  840. /* Expand tRNS chunks to alpha channels. */
  841. void PNGAPI
  842. png_set_tRNS_to_alpha(png_structrp png_ptr)
  843. {
  844. png_debug(1, "in png_set_tRNS_to_alpha");
  845. if (png_rtran_ok(png_ptr, 0) == 0)
  846. return;
  847. png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
  848. }
  849. #endif /* READ_EXPAND */
  850. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  851. /* Expand to 16-bit channels, expand the tRNS chunk too (because otherwise
  852. * it may not work correctly.)
  853. */
  854. void PNGAPI
  855. png_set_expand_16(png_structrp png_ptr)
  856. {
  857. png_debug(1, "in png_set_expand_16");
  858. if (png_rtran_ok(png_ptr, 0) == 0)
  859. return;
  860. png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS);
  861. }
  862. #endif
  863. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  864. void PNGAPI
  865. png_set_gray_to_rgb(png_structrp png_ptr)
  866. {
  867. png_debug(1, "in png_set_gray_to_rgb");
  868. if (png_rtran_ok(png_ptr, 0) == 0)
  869. return;
  870. /* Because rgb must be 8 bits or more: */
  871. png_set_expand_gray_1_2_4_to_8(png_ptr);
  872. png_ptr->transformations |= PNG_GRAY_TO_RGB;
  873. }
  874. #endif
  875. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  876. void PNGFAPI
  877. png_set_rgb_to_gray_fixed(png_structrp png_ptr, int error_action,
  878. png_fixed_point red, png_fixed_point green)
  879. {
  880. png_debug(1, "in png_set_rgb_to_gray_fixed");
  881. /* Need the IHDR here because of the check on color_type below. */
  882. /* TODO: fix this */
  883. if (png_rtran_ok(png_ptr, 1) == 0)
  884. return;
  885. switch (error_action)
  886. {
  887. case PNG_ERROR_ACTION_NONE:
  888. png_ptr->transformations |= PNG_RGB_TO_GRAY;
  889. break;
  890. case PNG_ERROR_ACTION_WARN:
  891. png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
  892. break;
  893. case PNG_ERROR_ACTION_ERROR:
  894. png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
  895. break;
  896. default:
  897. png_error(png_ptr, "invalid error action to rgb_to_gray");
  898. }
  899. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  900. #ifdef PNG_READ_EXPAND_SUPPORTED
  901. png_ptr->transformations |= PNG_EXPAND;
  902. #else
  903. {
  904. /* Make this an error in 1.6 because otherwise the application may assume
  905. * that it just worked and get a memory overwrite.
  906. */
  907. png_error(png_ptr,
  908. "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED");
  909. /* png_ptr->transformations &= ~PNG_RGB_TO_GRAY; */
  910. }
  911. #endif
  912. {
  913. if (red >= 0 && green >= 0 && red + green <= PNG_FP_1)
  914. {
  915. png_uint_16 red_int, green_int;
  916. /* NOTE: this calculation does not round, but this behavior is retained
  917. * for consistency; the inaccuracy is very small. The code here always
  918. * overwrites the coefficients, regardless of whether they have been
  919. * defaulted or set already.
  920. */
  921. red_int = (png_uint_16)(((png_uint_32)red*32768)/100000);
  922. green_int = (png_uint_16)(((png_uint_32)green*32768)/100000);
  923. png_ptr->rgb_to_gray_red_coeff = red_int;
  924. png_ptr->rgb_to_gray_green_coeff = green_int;
  925. png_ptr->rgb_to_gray_coefficients_set = 1;
  926. }
  927. else if (red >= 0 && green >= 0)
  928. png_app_warning(png_ptr,
  929. "ignoring out of range rgb_to_gray coefficients");
  930. }
  931. }
  932. #ifdef PNG_FLOATING_POINT_SUPPORTED
  933. /* Convert a RGB image to a grayscale of the same width. This allows us,
  934. * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
  935. */
  936. void PNGAPI
  937. png_set_rgb_to_gray(png_structrp png_ptr, int error_action, double red,
  938. double green)
  939. {
  940. png_set_rgb_to_gray_fixed(png_ptr, error_action,
  941. png_fixed(png_ptr, red, "rgb to gray red coefficient"),
  942. png_fixed(png_ptr, green, "rgb to gray green coefficient"));
  943. }
  944. #endif /* FLOATING POINT */
  945. #endif /* RGB_TO_GRAY */
  946. #if defined(PNG_READ_USER_TRANSFORM_SUPPORTED) || \
  947. defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
  948. void PNGAPI
  949. png_set_read_user_transform_fn(png_structrp png_ptr, png_user_transform_ptr
  950. read_user_transform_fn)
  951. {
  952. png_debug(1, "in png_set_read_user_transform_fn");
  953. #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
  954. png_ptr->transformations |= PNG_USER_TRANSFORM;
  955. png_ptr->read_user_transform_fn = read_user_transform_fn;
  956. #endif
  957. }
  958. #endif
  959. #ifdef PNG_READ_TRANSFORMS_SUPPORTED
  960. #ifdef PNG_READ_GAMMA_SUPPORTED
  961. /* In the case of gamma transformations only do transformations on images where
  962. * the [file] gamma and screen_gamma are not close reciprocals, otherwise it
  963. * slows things down slightly, and also needlessly introduces small errors.
  964. */
  965. static int /* PRIVATE */
  966. png_gamma_threshold(png_fixed_point screen_gamma, png_fixed_point file_gamma)
  967. {
  968. /* PNG_GAMMA_THRESHOLD is the threshold for performing gamma
  969. * correction as a difference of the overall transform from 1.0
  970. *
  971. * We want to compare the threshold with s*f - 1, if we get
  972. * overflow here it is because of wacky gamma values so we
  973. * turn on processing anyway.
  974. */
  975. png_fixed_point gtest;
  976. return !png_muldiv(&gtest, screen_gamma, file_gamma, PNG_FP_1) ||
  977. png_gamma_significant(gtest);
  978. }
  979. #endif
  980. /* Initialize everything needed for the read. This includes modifying
  981. * the palette.
  982. */
  983. /* For the moment 'png_init_palette_transformations' and
  984. * 'png_init_rgb_transformations' only do some flag canceling optimizations.
  985. * The intent is that these two routines should have palette or rgb operations
  986. * extracted from 'png_init_read_transformations'.
  987. */
  988. static void /* PRIVATE */
  989. png_init_palette_transformations(png_structrp png_ptr)
  990. {
  991. /* Called to handle the (input) palette case. In png_do_read_transformations
  992. * the first step is to expand the palette if requested, so this code must
  993. * take care to only make changes that are invariant with respect to the
  994. * palette expansion, or only do them if there is no expansion.
  995. *
  996. * STRIP_ALPHA has already been handled in the caller (by setting num_trans
  997. * to 0.)
  998. */
  999. int input_has_alpha = 0;
  1000. int input_has_transparency = 0;
  1001. if (png_ptr->num_trans > 0)
  1002. {
  1003. int i;
  1004. /* Ignore if all the entries are opaque (unlikely!) */
  1005. for (i=0; i<png_ptr->num_trans; ++i)
  1006. {
  1007. if (png_ptr->trans_alpha[i] == 255)
  1008. continue;
  1009. else if (png_ptr->trans_alpha[i] == 0)
  1010. input_has_transparency = 1;
  1011. else
  1012. {
  1013. input_has_transparency = 1;
  1014. input_has_alpha = 1;
  1015. break;
  1016. }
  1017. }
  1018. }
  1019. /* If no alpha we can optimize. */
  1020. if (input_has_alpha == 0)
  1021. {
  1022. /* Any alpha means background and associative alpha processing is
  1023. * required, however if the alpha is 0 or 1 throughout OPTIMIZE_ALPHA
  1024. * and ENCODE_ALPHA are irrelevant.
  1025. */
  1026. png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
  1027. png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
  1028. if (input_has_transparency == 0)
  1029. png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
  1030. }
  1031. #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
  1032. /* png_set_background handling - deals with the complexity of whether the
  1033. * background color is in the file format or the screen format in the case
  1034. * where an 'expand' will happen.
  1035. */
  1036. /* The following code cannot be entered in the alpha pre-multiplication case
  1037. * because PNG_BACKGROUND_EXPAND is cancelled below.
  1038. */
  1039. if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 &&
  1040. (png_ptr->transformations & PNG_EXPAND) != 0)
  1041. {
  1042. {
  1043. png_ptr->background.red =
  1044. png_ptr->palette[png_ptr->background.index].red;
  1045. png_ptr->background.green =
  1046. png_ptr->palette[png_ptr->background.index].green;
  1047. png_ptr->background.blue =
  1048. png_ptr->palette[png_ptr->background.index].blue;
  1049. #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
  1050. if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
  1051. {
  1052. if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0)
  1053. {
  1054. /* Invert the alpha channel (in tRNS) unless the pixels are
  1055. * going to be expanded, in which case leave it for later
  1056. */
  1057. int i, istop = png_ptr->num_trans;
  1058. for (i = 0; i < istop; i++)
  1059. png_ptr->trans_alpha[i] =
  1060. (png_byte)(255 - png_ptr->trans_alpha[i]);
  1061. }
  1062. }
  1063. #endif /* READ_INVERT_ALPHA */
  1064. }
  1065. } /* background expand and (therefore) no alpha association. */
  1066. #endif /* READ_EXPAND && READ_BACKGROUND */
  1067. }
  1068. static void /* PRIVATE */
  1069. png_init_rgb_transformations(png_structrp png_ptr)
  1070. {
  1071. /* Added to libpng-1.5.4: check the color type to determine whether there
  1072. * is any alpha or transparency in the image and simply cancel the
  1073. * background and alpha mode stuff if there isn't.
  1074. */
  1075. int input_has_alpha = (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0;
  1076. int input_has_transparency = png_ptr->num_trans > 0;
  1077. /* If no alpha we can optimize. */
  1078. if (input_has_alpha == 0)
  1079. {
  1080. /* Any alpha means background and associative alpha processing is
  1081. * required, however if the alpha is 0 or 1 throughout OPTIMIZE_ALPHA
  1082. * and ENCODE_ALPHA are irrelevant.
  1083. */
  1084. # ifdef PNG_READ_ALPHA_MODE_SUPPORTED
  1085. png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
  1086. png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
  1087. # endif
  1088. if (input_has_transparency == 0)
  1089. png_ptr->transformations &= ~(PNG_COMPOSE | PNG_BACKGROUND_EXPAND);
  1090. }
  1091. #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
  1092. /* png_set_background handling - deals with the complexity of whether the
  1093. * background color is in the file format or the screen format in the case
  1094. * where an 'expand' will happen.
  1095. */
  1096. /* The following code cannot be entered in the alpha pre-multiplication case
  1097. * because PNG_BACKGROUND_EXPAND is cancelled below.
  1098. */
  1099. if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0 &&
  1100. (png_ptr->transformations & PNG_EXPAND) != 0 &&
  1101. (png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
  1102. /* i.e., GRAY or GRAY_ALPHA */
  1103. {
  1104. {
  1105. /* Expand background and tRNS chunks */
  1106. int gray = png_ptr->background.gray;
  1107. int trans_gray = png_ptr->trans_color.gray;
  1108. switch (png_ptr->bit_depth)
  1109. {
  1110. case 1:
  1111. gray *= 0xff;
  1112. trans_gray *= 0xff;
  1113. break;
  1114. case 2:
  1115. gray *= 0x55;
  1116. trans_gray *= 0x55;
  1117. break;
  1118. case 4:
  1119. gray *= 0x11;
  1120. trans_gray *= 0x11;
  1121. break;
  1122. default:
  1123. case 8:
  1124. /* FALLTHROUGH */ /* (Already 8 bits) */
  1125. case 16:
  1126. /* Already a full 16 bits */
  1127. break;
  1128. }
  1129. png_ptr->background.red = png_ptr->background.green =
  1130. png_ptr->background.blue = (png_uint_16)gray;
  1131. if ((png_ptr->transformations & PNG_EXPAND_tRNS) == 0)
  1132. {
  1133. png_ptr->trans_color.red = png_ptr->trans_color.green =
  1134. png_ptr->trans_color.blue = (png_uint_16)trans_gray;
  1135. }
  1136. }
  1137. } /* background expand and (therefore) no alpha association. */
  1138. #endif /* READ_EXPAND && READ_BACKGROUND */
  1139. }
  1140. #ifdef PNG_READ_GAMMA_SUPPORTED
  1141. png_fixed_point /* PRIVATE */
  1142. png_resolve_file_gamma(png_const_structrp png_ptr)
  1143. {
  1144. png_fixed_point file_gamma;
  1145. /* The file gamma is determined by these precedence rules, in this order
  1146. * (i.e. use the first value found):
  1147. *
  1148. * png_set_gamma; png_struct::file_gammma if not zero, then:
  1149. * png_struct::chunk_gamma if not 0 (determined the PNGv3 rules), then:
  1150. * png_set_gamma; 1/png_struct::screen_gamma if not zero
  1151. *
  1152. * 0 (i.e. do no gamma handling)
  1153. */
  1154. file_gamma = png_ptr->file_gamma;
  1155. if (file_gamma != 0)
  1156. return file_gamma;
  1157. file_gamma = png_ptr->chunk_gamma;
  1158. if (file_gamma != 0)
  1159. return file_gamma;
  1160. file_gamma = png_ptr->default_gamma;
  1161. if (file_gamma != 0)
  1162. return file_gamma;
  1163. /* If png_reciprocal oveflows it returns 0 which indicates to the caller that
  1164. * there is no usable file gamma. (The checks added to png_set_gamma and
  1165. * png_set_alpha_mode should prevent a screen_gamma which would overflow.)
  1166. */
  1167. if (png_ptr->screen_gamma != 0)
  1168. file_gamma = png_reciprocal(png_ptr->screen_gamma);
  1169. return file_gamma;
  1170. }
  1171. static int
  1172. png_init_gamma_values(png_structrp png_ptr)
  1173. {
  1174. /* The following temporary indicates if overall gamma correction is
  1175. * required.
  1176. */
  1177. int gamma_correction = 0;
  1178. png_fixed_point file_gamma, screen_gamma;
  1179. /* Resolve the file_gamma. See above: if png_ptr::screen_gamma is set
  1180. * file_gamma will always be set here:
  1181. */
  1182. file_gamma = png_resolve_file_gamma(png_ptr);
  1183. screen_gamma = png_ptr->screen_gamma;
  1184. if (file_gamma > 0) /* file has been set */
  1185. {
  1186. if (screen_gamma > 0) /* screen set too */
  1187. gamma_correction = png_gamma_threshold(file_gamma, screen_gamma);
  1188. else
  1189. /* Assume the output matches the input; a long time default behavior
  1190. * of libpng, although the standard has nothing to say about this.
  1191. */
  1192. screen_gamma = png_reciprocal(file_gamma);
  1193. }
  1194. else /* both unset, prevent corrections: */
  1195. file_gamma = screen_gamma = PNG_FP_1;
  1196. png_ptr->file_gamma = file_gamma;
  1197. png_ptr->screen_gamma = screen_gamma;
  1198. return gamma_correction;
  1199. }
  1200. #endif /* READ_GAMMA */
  1201. void /* PRIVATE */
  1202. png_init_read_transformations(png_structrp png_ptr)
  1203. {
  1204. png_debug(1, "in png_init_read_transformations");
  1205. /* This internal function is called from png_read_start_row in pngrutil.c
  1206. * and it is called before the 'rowbytes' calculation is done, so the code
  1207. * in here can change or update the transformations flags.
  1208. *
  1209. * First do updates that do not depend on the details of the PNG image data
  1210. * being processed.
  1211. */
  1212. #ifdef PNG_READ_GAMMA_SUPPORTED
  1213. /* Prior to 1.5.4 these tests were performed from png_set_gamma, 1.5.4 adds
  1214. * png_set_alpha_mode and this is another source for a default file gamma so
  1215. * the test needs to be performed later - here. In addition prior to 1.5.4
  1216. * the tests were repeated for the PALETTE color type here - this is no
  1217. * longer necessary (and doesn't seem to have been necessary before.)
  1218. *
  1219. * PNGv3: the new mandatory precedence/priority rules for colour space chunks
  1220. * are handled here (by calling the above function).
  1221. *
  1222. * Turn the gamma transformation on or off as appropriate. Notice that
  1223. * PNG_GAMMA just refers to the file->screen correction. Alpha composition
  1224. * may independently cause gamma correction because it needs linear data
  1225. * (e.g. if the file has a gAMA chunk but the screen gamma hasn't been
  1226. * specified.) In any case this flag may get turned off in the code
  1227. * immediately below if the transform can be handled outside the row loop.
  1228. */
  1229. if (png_init_gamma_values(png_ptr) != 0)
  1230. png_ptr->transformations |= PNG_GAMMA;
  1231. else
  1232. png_ptr->transformations &= ~PNG_GAMMA;
  1233. #endif
  1234. /* Certain transformations have the effect of preventing other
  1235. * transformations that happen afterward in png_do_read_transformations;
  1236. * resolve the interdependencies here. From the code of
  1237. * png_do_read_transformations the order is:
  1238. *
  1239. * 1) PNG_EXPAND (including PNG_EXPAND_tRNS)
  1240. * 2) PNG_STRIP_ALPHA (if no compose)
  1241. * 3) PNG_RGB_TO_GRAY
  1242. * 4) PNG_GRAY_TO_RGB iff !PNG_BACKGROUND_IS_GRAY
  1243. * 5) PNG_COMPOSE
  1244. * 6) PNG_GAMMA
  1245. * 7) PNG_STRIP_ALPHA (if compose)
  1246. * 8) PNG_ENCODE_ALPHA
  1247. * 9) PNG_SCALE_16_TO_8
  1248. * 10) PNG_16_TO_8
  1249. * 11) PNG_QUANTIZE (converts to palette)
  1250. * 12) PNG_EXPAND_16
  1251. * 13) PNG_GRAY_TO_RGB iff PNG_BACKGROUND_IS_GRAY
  1252. * 14) PNG_INVERT_MONO
  1253. * 15) PNG_INVERT_ALPHA
  1254. * 16) PNG_SHIFT
  1255. * 17) PNG_PACK
  1256. * 18) PNG_BGR
  1257. * 19) PNG_PACKSWAP
  1258. * 20) PNG_FILLER (includes PNG_ADD_ALPHA)
  1259. * 21) PNG_SWAP_ALPHA
  1260. * 22) PNG_SWAP_BYTES
  1261. * 23) PNG_USER_TRANSFORM [must be last]
  1262. */
  1263. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  1264. if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
  1265. (png_ptr->transformations & PNG_COMPOSE) == 0)
  1266. {
  1267. /* Stripping the alpha channel happens immediately after the 'expand'
  1268. * transformations, before all other transformation, so it cancels out
  1269. * the alpha handling. It has the side effect negating the effect of
  1270. * PNG_EXPAND_tRNS too:
  1271. */
  1272. png_ptr->transformations &= ~(PNG_BACKGROUND_EXPAND | PNG_ENCODE_ALPHA |
  1273. PNG_EXPAND_tRNS);
  1274. png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
  1275. /* Kill the tRNS chunk itself too. Prior to 1.5.4 this did not happen
  1276. * so transparency information would remain just so long as it wasn't
  1277. * expanded. This produces unexpected API changes if the set of things
  1278. * that do PNG_EXPAND_tRNS changes (perfectly possible given the
  1279. * documentation - which says ask for what you want, accept what you
  1280. * get.) This makes the behavior consistent from 1.5.4:
  1281. */
  1282. png_ptr->num_trans = 0;
  1283. }
  1284. #endif /* STRIP_ALPHA supported, no COMPOSE */
  1285. #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
  1286. /* If the screen gamma is about 1.0 then the OPTIMIZE_ALPHA and ENCODE_ALPHA
  1287. * settings will have no effect.
  1288. */
  1289. if (png_gamma_significant(png_ptr->screen_gamma) == 0)
  1290. {
  1291. png_ptr->transformations &= ~PNG_ENCODE_ALPHA;
  1292. png_ptr->flags &= ~PNG_FLAG_OPTIMIZE_ALPHA;
  1293. }
  1294. #endif
  1295. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  1296. /* Make sure the coefficients for the rgb to gray conversion are set
  1297. * appropriately.
  1298. */
  1299. if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
  1300. png_set_rgb_coefficients(png_ptr);
  1301. #endif
  1302. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  1303. #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
  1304. /* Detect gray background and attempt to enable optimization for
  1305. * gray --> RGB case.
  1306. *
  1307. * Note: if PNG_BACKGROUND_EXPAND is set and color_type is either RGB or
  1308. * RGB_ALPHA (in which case need_expand is superfluous anyway), the
  1309. * background color might actually be gray yet not be flagged as such.
  1310. * This is not a problem for the current code, which uses
  1311. * PNG_BACKGROUND_IS_GRAY only to decide when to do the
  1312. * png_do_gray_to_rgb() transformation.
  1313. *
  1314. * TODO: this code needs to be revised to avoid the complexity and
  1315. * interdependencies. The color type of the background should be recorded in
  1316. * png_set_background, along with the bit depth, then the code has a record
  1317. * of exactly what color space the background is currently in.
  1318. */
  1319. if ((png_ptr->transformations & PNG_BACKGROUND_EXPAND) != 0)
  1320. {
  1321. /* PNG_BACKGROUND_EXPAND: the background is in the file color space, so if
  1322. * the file was grayscale the background value is gray.
  1323. */
  1324. if ((png_ptr->color_type & PNG_COLOR_MASK_COLOR) == 0)
  1325. png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
  1326. }
  1327. else if ((png_ptr->transformations & PNG_COMPOSE) != 0)
  1328. {
  1329. /* PNG_COMPOSE: png_set_background was called with need_expand false,
  1330. * so the color is in the color space of the output or png_set_alpha_mode
  1331. * was called and the color is black. Ignore RGB_TO_GRAY because that
  1332. * happens before GRAY_TO_RGB.
  1333. */
  1334. if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
  1335. {
  1336. if (png_ptr->background.red == png_ptr->background.green &&
  1337. png_ptr->background.red == png_ptr->background.blue)
  1338. {
  1339. png_ptr->mode |= PNG_BACKGROUND_IS_GRAY;
  1340. png_ptr->background.gray = png_ptr->background.red;
  1341. }
  1342. }
  1343. }
  1344. #endif /* READ_EXPAND && READ_BACKGROUND */
  1345. #endif /* READ_GRAY_TO_RGB */
  1346. /* For indexed PNG data (PNG_COLOR_TYPE_PALETTE) many of the transformations
  1347. * can be performed directly on the palette, and some (such as rgb to gray)
  1348. * can be optimized inside the palette. This is particularly true of the
  1349. * composite (background and alpha) stuff, which can be pretty much all done
  1350. * in the palette even if the result is expanded to RGB or gray afterward.
  1351. *
  1352. * NOTE: this is Not Yet Implemented, the code behaves as in 1.5.1 and
  1353. * earlier and the palette stuff is actually handled on the first row. This
  1354. * leads to the reported bug that the palette returned by png_get_PLTE is not
  1355. * updated.
  1356. */
  1357. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1358. png_init_palette_transformations(png_ptr);
  1359. else
  1360. png_init_rgb_transformations(png_ptr);
  1361. #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
  1362. defined(PNG_READ_EXPAND_16_SUPPORTED)
  1363. if ((png_ptr->transformations & PNG_EXPAND_16) != 0 &&
  1364. (png_ptr->transformations & PNG_COMPOSE) != 0 &&
  1365. (png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 &&
  1366. png_ptr->bit_depth != 16)
  1367. {
  1368. /* TODO: fix this. Because the expand_16 operation is after the compose
  1369. * handling the background color must be 8, not 16, bits deep, but the
  1370. * application will supply a 16-bit value so reduce it here.
  1371. *
  1372. * The PNG_BACKGROUND_EXPAND code above does not expand to 16 bits at
  1373. * present, so that case is ok (until do_expand_16 is moved.)
  1374. *
  1375. * NOTE: this discards the low 16 bits of the user supplied background
  1376. * color, but until expand_16 works properly there is no choice!
  1377. */
  1378. # define CHOP(x) (x)=((png_uint_16)PNG_DIV257(x))
  1379. CHOP(png_ptr->background.red);
  1380. CHOP(png_ptr->background.green);
  1381. CHOP(png_ptr->background.blue);
  1382. CHOP(png_ptr->background.gray);
  1383. # undef CHOP
  1384. }
  1385. #endif /* READ_BACKGROUND && READ_EXPAND_16 */
  1386. #if defined(PNG_READ_BACKGROUND_SUPPORTED) && \
  1387. (defined(PNG_READ_SCALE_16_TO_8_SUPPORTED) || \
  1388. defined(PNG_READ_STRIP_16_TO_8_SUPPORTED))
  1389. if ((png_ptr->transformations & (PNG_16_TO_8|PNG_SCALE_16_TO_8)) != 0 &&
  1390. (png_ptr->transformations & PNG_COMPOSE) != 0 &&
  1391. (png_ptr->transformations & PNG_BACKGROUND_EXPAND) == 0 &&
  1392. png_ptr->bit_depth == 16)
  1393. {
  1394. /* On the other hand, if a 16-bit file is to be reduced to 8-bits per
  1395. * component this will also happen after PNG_COMPOSE and so the background
  1396. * color must be pre-expanded here.
  1397. *
  1398. * TODO: fix this too.
  1399. */
  1400. png_ptr->background.red = (png_uint_16)(png_ptr->background.red * 257);
  1401. png_ptr->background.green =
  1402. (png_uint_16)(png_ptr->background.green * 257);
  1403. png_ptr->background.blue = (png_uint_16)(png_ptr->background.blue * 257);
  1404. png_ptr->background.gray = (png_uint_16)(png_ptr->background.gray * 257);
  1405. }
  1406. #endif
  1407. /* NOTE: below 'PNG_READ_ALPHA_MODE_SUPPORTED' is presumed to also enable the
  1408. * background support (see the comments in scripts/pnglibconf.dfa), this
  1409. * allows pre-multiplication of the alpha channel to be implemented as
  1410. * compositing on black. This is probably sub-optimal and has been done in
  1411. * 1.5.4 betas simply to enable external critique and testing (i.e. to
  1412. * implement the new API quickly, without lots of internal changes.)
  1413. */
  1414. #ifdef PNG_READ_GAMMA_SUPPORTED
  1415. # ifdef PNG_READ_BACKGROUND_SUPPORTED
  1416. /* Includes ALPHA_MODE */
  1417. png_ptr->background_1 = png_ptr->background;
  1418. # endif
  1419. /* This needs to change - in the palette image case a whole set of tables are
  1420. * built when it would be quicker to just calculate the correct value for
  1421. * each palette entry directly. Also, the test is too tricky - why check
  1422. * PNG_RGB_TO_GRAY if PNG_GAMMA is not set? The answer seems to be that
  1423. * PNG_GAMMA is cancelled even if the gamma is known? The test excludes the
  1424. * PNG_COMPOSE case, so apparently if there is no *overall* gamma correction
  1425. * the gamma tables will not be built even if composition is required on a
  1426. * gamma encoded value.
  1427. *
  1428. * In 1.5.4 this is addressed below by an additional check on the individual
  1429. * file gamma - if it is not 1.0 both RGB_TO_GRAY and COMPOSE need the
  1430. * tables.
  1431. */
  1432. if ((png_ptr->transformations & PNG_GAMMA) != 0 ||
  1433. ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0 &&
  1434. (png_gamma_significant(png_ptr->file_gamma) != 0 ||
  1435. png_gamma_significant(png_ptr->screen_gamma) != 0)) ||
  1436. ((png_ptr->transformations & PNG_COMPOSE) != 0 &&
  1437. (png_gamma_significant(png_ptr->file_gamma) != 0 ||
  1438. png_gamma_significant(png_ptr->screen_gamma) != 0
  1439. # ifdef PNG_READ_BACKGROUND_SUPPORTED
  1440. || (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_UNIQUE &&
  1441. png_gamma_significant(png_ptr->background_gamma) != 0)
  1442. # endif
  1443. )) || ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 &&
  1444. png_gamma_significant(png_ptr->screen_gamma) != 0))
  1445. {
  1446. png_build_gamma_table(png_ptr, png_ptr->bit_depth);
  1447. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  1448. if ((png_ptr->transformations & PNG_COMPOSE) != 0)
  1449. {
  1450. /* Issue a warning about this combination: because RGB_TO_GRAY is
  1451. * optimized to do the gamma transform if present yet do_background has
  1452. * to do the same thing if both options are set a
  1453. * double-gamma-correction happens. This is true in all versions of
  1454. * libpng to date.
  1455. */
  1456. if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
  1457. png_warning(png_ptr,
  1458. "libpng does not support gamma+background+rgb_to_gray");
  1459. if ((png_ptr->color_type == PNG_COLOR_TYPE_PALETTE) != 0)
  1460. {
  1461. /* We don't get to here unless there is a tRNS chunk with non-opaque
  1462. * entries - see the checking code at the start of this function.
  1463. */
  1464. png_color back, back_1;
  1465. png_colorp palette = png_ptr->palette;
  1466. int num_palette = png_ptr->num_palette;
  1467. int i;
  1468. if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
  1469. {
  1470. back.red = png_ptr->gamma_table[png_ptr->background.red];
  1471. back.green = png_ptr->gamma_table[png_ptr->background.green];
  1472. back.blue = png_ptr->gamma_table[png_ptr->background.blue];
  1473. back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
  1474. back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
  1475. back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
  1476. }
  1477. else
  1478. {
  1479. png_fixed_point g, gs;
  1480. switch (png_ptr->background_gamma_type)
  1481. {
  1482. case PNG_BACKGROUND_GAMMA_SCREEN:
  1483. g = (png_ptr->screen_gamma);
  1484. gs = PNG_FP_1;
  1485. break;
  1486. case PNG_BACKGROUND_GAMMA_FILE:
  1487. g = png_reciprocal(png_ptr->file_gamma);
  1488. gs = png_reciprocal2(png_ptr->file_gamma,
  1489. png_ptr->screen_gamma);
  1490. break;
  1491. case PNG_BACKGROUND_GAMMA_UNIQUE:
  1492. g = png_reciprocal(png_ptr->background_gamma);
  1493. gs = png_reciprocal2(png_ptr->background_gamma,
  1494. png_ptr->screen_gamma);
  1495. break;
  1496. default:
  1497. g = PNG_FP_1; /* back_1 */
  1498. gs = PNG_FP_1; /* back */
  1499. break;
  1500. }
  1501. if (png_gamma_significant(gs) != 0)
  1502. {
  1503. back.red = png_gamma_8bit_correct(png_ptr->background.red,
  1504. gs);
  1505. back.green = png_gamma_8bit_correct(png_ptr->background.green,
  1506. gs);
  1507. back.blue = png_gamma_8bit_correct(png_ptr->background.blue,
  1508. gs);
  1509. }
  1510. else
  1511. {
  1512. back.red = (png_byte)png_ptr->background.red;
  1513. back.green = (png_byte)png_ptr->background.green;
  1514. back.blue = (png_byte)png_ptr->background.blue;
  1515. }
  1516. if (png_gamma_significant(g) != 0)
  1517. {
  1518. back_1.red = png_gamma_8bit_correct(png_ptr->background.red,
  1519. g);
  1520. back_1.green = png_gamma_8bit_correct(
  1521. png_ptr->background.green, g);
  1522. back_1.blue = png_gamma_8bit_correct(png_ptr->background.blue,
  1523. g);
  1524. }
  1525. else
  1526. {
  1527. back_1.red = (png_byte)png_ptr->background.red;
  1528. back_1.green = (png_byte)png_ptr->background.green;
  1529. back_1.blue = (png_byte)png_ptr->background.blue;
  1530. }
  1531. }
  1532. for (i = 0; i < num_palette; i++)
  1533. {
  1534. if (i < (int)png_ptr->num_trans &&
  1535. png_ptr->trans_alpha[i] != 0xff)
  1536. {
  1537. if (png_ptr->trans_alpha[i] == 0)
  1538. {
  1539. palette[i] = back;
  1540. }
  1541. else /* if (png_ptr->trans_alpha[i] != 0xff) */
  1542. {
  1543. png_byte v, w;
  1544. v = png_ptr->gamma_to_1[palette[i].red];
  1545. png_composite(w, v, png_ptr->trans_alpha[i], back_1.red);
  1546. palette[i].red = png_ptr->gamma_from_1[w];
  1547. v = png_ptr->gamma_to_1[palette[i].green];
  1548. png_composite(w, v, png_ptr->trans_alpha[i], back_1.green);
  1549. palette[i].green = png_ptr->gamma_from_1[w];
  1550. v = png_ptr->gamma_to_1[palette[i].blue];
  1551. png_composite(w, v, png_ptr->trans_alpha[i], back_1.blue);
  1552. palette[i].blue = png_ptr->gamma_from_1[w];
  1553. }
  1554. }
  1555. else
  1556. {
  1557. palette[i].red = png_ptr->gamma_table[palette[i].red];
  1558. palette[i].green = png_ptr->gamma_table[palette[i].green];
  1559. palette[i].blue = png_ptr->gamma_table[palette[i].blue];
  1560. }
  1561. }
  1562. /* Prevent the transformations being done again.
  1563. *
  1564. * NOTE: this is highly dubious; it removes the transformations in
  1565. * place. This seems inconsistent with the general treatment of the
  1566. * transformations elsewhere.
  1567. */
  1568. png_ptr->transformations &= ~(PNG_COMPOSE | PNG_GAMMA);
  1569. } /* color_type == PNG_COLOR_TYPE_PALETTE */
  1570. /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN) */
  1571. else /* color_type != PNG_COLOR_TYPE_PALETTE */
  1572. {
  1573. int gs_sig, g_sig;
  1574. png_fixed_point g = PNG_FP_1; /* Correction to linear */
  1575. png_fixed_point gs = PNG_FP_1; /* Correction to screen */
  1576. switch (png_ptr->background_gamma_type)
  1577. {
  1578. case PNG_BACKGROUND_GAMMA_SCREEN:
  1579. g = png_ptr->screen_gamma;
  1580. /* gs = PNG_FP_1; */
  1581. break;
  1582. case PNG_BACKGROUND_GAMMA_FILE:
  1583. g = png_reciprocal(png_ptr->file_gamma);
  1584. gs = png_reciprocal2(png_ptr->file_gamma,
  1585. png_ptr->screen_gamma);
  1586. break;
  1587. case PNG_BACKGROUND_GAMMA_UNIQUE:
  1588. g = png_reciprocal(png_ptr->background_gamma);
  1589. gs = png_reciprocal2(png_ptr->background_gamma,
  1590. png_ptr->screen_gamma);
  1591. break;
  1592. default:
  1593. png_error(png_ptr, "invalid background gamma type");
  1594. }
  1595. g_sig = png_gamma_significant(g);
  1596. gs_sig = png_gamma_significant(gs);
  1597. if (g_sig != 0)
  1598. png_ptr->background_1.gray = png_gamma_correct(png_ptr,
  1599. png_ptr->background.gray, g);
  1600. if (gs_sig != 0)
  1601. png_ptr->background.gray = png_gamma_correct(png_ptr,
  1602. png_ptr->background.gray, gs);
  1603. if ((png_ptr->background.red != png_ptr->background.green) ||
  1604. (png_ptr->background.red != png_ptr->background.blue) ||
  1605. (png_ptr->background.red != png_ptr->background.gray))
  1606. {
  1607. /* RGB or RGBA with color background */
  1608. if (g_sig != 0)
  1609. {
  1610. png_ptr->background_1.red = png_gamma_correct(png_ptr,
  1611. png_ptr->background.red, g);
  1612. png_ptr->background_1.green = png_gamma_correct(png_ptr,
  1613. png_ptr->background.green, g);
  1614. png_ptr->background_1.blue = png_gamma_correct(png_ptr,
  1615. png_ptr->background.blue, g);
  1616. }
  1617. if (gs_sig != 0)
  1618. {
  1619. png_ptr->background.red = png_gamma_correct(png_ptr,
  1620. png_ptr->background.red, gs);
  1621. png_ptr->background.green = png_gamma_correct(png_ptr,
  1622. png_ptr->background.green, gs);
  1623. png_ptr->background.blue = png_gamma_correct(png_ptr,
  1624. png_ptr->background.blue, gs);
  1625. }
  1626. }
  1627. else
  1628. {
  1629. /* GRAY, GRAY ALPHA, RGB, or RGBA with gray background */
  1630. png_ptr->background_1.red = png_ptr->background_1.green
  1631. = png_ptr->background_1.blue = png_ptr->background_1.gray;
  1632. png_ptr->background.red = png_ptr->background.green
  1633. = png_ptr->background.blue = png_ptr->background.gray;
  1634. }
  1635. /* The background is now in screen gamma: */
  1636. png_ptr->background_gamma_type = PNG_BACKGROUND_GAMMA_SCREEN;
  1637. } /* color_type != PNG_COLOR_TYPE_PALETTE */
  1638. }/* png_ptr->transformations & PNG_BACKGROUND */
  1639. else
  1640. /* Transformation does not include PNG_BACKGROUND */
  1641. #endif /* READ_BACKGROUND */
  1642. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE
  1643. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  1644. /* RGB_TO_GRAY needs to have non-gamma-corrected values! */
  1645. && ((png_ptr->transformations & PNG_EXPAND) == 0 ||
  1646. (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0)
  1647. #endif
  1648. )
  1649. {
  1650. png_colorp palette = png_ptr->palette;
  1651. int num_palette = png_ptr->num_palette;
  1652. int i;
  1653. /* NOTE: there are other transformations that should probably be in
  1654. * here too.
  1655. */
  1656. for (i = 0; i < num_palette; i++)
  1657. {
  1658. palette[i].red = png_ptr->gamma_table[palette[i].red];
  1659. palette[i].green = png_ptr->gamma_table[palette[i].green];
  1660. palette[i].blue = png_ptr->gamma_table[palette[i].blue];
  1661. }
  1662. /* Done the gamma correction. */
  1663. png_ptr->transformations &= ~PNG_GAMMA;
  1664. } /* color_type == PALETTE && !PNG_BACKGROUND transformation */
  1665. }
  1666. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  1667. else
  1668. #endif
  1669. #endif /* READ_GAMMA */
  1670. #ifdef PNG_READ_BACKGROUND_SUPPORTED
  1671. /* No GAMMA transformation (see the hanging else 4 lines above) */
  1672. if ((png_ptr->transformations & PNG_COMPOSE) != 0 &&
  1673. (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
  1674. {
  1675. int i;
  1676. int istop = (int)png_ptr->num_trans;
  1677. png_color back;
  1678. png_colorp palette = png_ptr->palette;
  1679. back.red = (png_byte)png_ptr->background.red;
  1680. back.green = (png_byte)png_ptr->background.green;
  1681. back.blue = (png_byte)png_ptr->background.blue;
  1682. for (i = 0; i < istop; i++)
  1683. {
  1684. if (png_ptr->trans_alpha[i] == 0)
  1685. {
  1686. palette[i] = back;
  1687. }
  1688. else if (png_ptr->trans_alpha[i] != 0xff)
  1689. {
  1690. /* The png_composite() macro is defined in png.h */
  1691. png_composite(palette[i].red, palette[i].red,
  1692. png_ptr->trans_alpha[i], back.red);
  1693. png_composite(palette[i].green, palette[i].green,
  1694. png_ptr->trans_alpha[i], back.green);
  1695. png_composite(palette[i].blue, palette[i].blue,
  1696. png_ptr->trans_alpha[i], back.blue);
  1697. }
  1698. }
  1699. png_ptr->transformations &= ~PNG_COMPOSE;
  1700. }
  1701. #endif /* READ_BACKGROUND */
  1702. #ifdef PNG_READ_SHIFT_SUPPORTED
  1703. if ((png_ptr->transformations & PNG_SHIFT) != 0 &&
  1704. (png_ptr->transformations & PNG_EXPAND) == 0 &&
  1705. (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE))
  1706. {
  1707. int i;
  1708. int istop = png_ptr->num_palette;
  1709. int shift = 8 - png_ptr->sig_bit.red;
  1710. png_ptr->transformations &= ~PNG_SHIFT;
  1711. /* significant bits can be in the range 1 to 7 for a meaningful result, if
  1712. * the number of significant bits is 0 then no shift is done (this is an
  1713. * error condition which is silently ignored.)
  1714. */
  1715. if (shift > 0 && shift < 8)
  1716. for (i=0; i<istop; ++i)
  1717. {
  1718. int component = png_ptr->palette[i].red;
  1719. component >>= shift;
  1720. png_ptr->palette[i].red = (png_byte)component;
  1721. }
  1722. shift = 8 - png_ptr->sig_bit.green;
  1723. if (shift > 0 && shift < 8)
  1724. for (i=0; i<istop; ++i)
  1725. {
  1726. int component = png_ptr->palette[i].green;
  1727. component >>= shift;
  1728. png_ptr->palette[i].green = (png_byte)component;
  1729. }
  1730. shift = 8 - png_ptr->sig_bit.blue;
  1731. if (shift > 0 && shift < 8)
  1732. for (i=0; i<istop; ++i)
  1733. {
  1734. int component = png_ptr->palette[i].blue;
  1735. component >>= shift;
  1736. png_ptr->palette[i].blue = (png_byte)component;
  1737. }
  1738. }
  1739. #endif /* READ_SHIFT */
  1740. }
  1741. /* Modify the info structure to reflect the transformations. The
  1742. * info should be updated so a PNG file could be written with it,
  1743. * assuming the transformations result in valid PNG data.
  1744. */
  1745. void /* PRIVATE */
  1746. png_read_transform_info(png_structrp png_ptr, png_inforp info_ptr)
  1747. {
  1748. png_debug(1, "in png_read_transform_info");
  1749. #ifdef PNG_READ_EXPAND_SUPPORTED
  1750. if ((png_ptr->transformations & PNG_EXPAND) != 0)
  1751. {
  1752. if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1753. {
  1754. /* This check must match what actually happens in
  1755. * png_do_expand_palette; if it ever checks the tRNS chunk to see if
  1756. * it is all opaque we must do the same (at present it does not.)
  1757. */
  1758. if (png_ptr->num_trans > 0)
  1759. info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  1760. else
  1761. info_ptr->color_type = PNG_COLOR_TYPE_RGB;
  1762. info_ptr->bit_depth = 8;
  1763. info_ptr->num_trans = 0;
  1764. if (png_ptr->palette == NULL)
  1765. png_error (png_ptr, "Palette is NULL in indexed image");
  1766. }
  1767. else
  1768. {
  1769. if (png_ptr->num_trans != 0)
  1770. {
  1771. if ((png_ptr->transformations & PNG_EXPAND_tRNS) != 0)
  1772. info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
  1773. }
  1774. if (info_ptr->bit_depth < 8)
  1775. info_ptr->bit_depth = 8;
  1776. info_ptr->num_trans = 0;
  1777. }
  1778. }
  1779. #endif
  1780. #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
  1781. defined(PNG_READ_ALPHA_MODE_SUPPORTED)
  1782. /* The following is almost certainly wrong unless the background value is in
  1783. * the screen space!
  1784. */
  1785. if ((png_ptr->transformations & PNG_COMPOSE) != 0)
  1786. info_ptr->background = png_ptr->background;
  1787. #endif
  1788. #ifdef PNG_READ_GAMMA_SUPPORTED
  1789. /* The following used to be conditional on PNG_GAMMA (prior to 1.5.4),
  1790. * however it seems that the code in png_init_read_transformations, which has
  1791. * been called before this from png_read_update_info->png_read_start_row
  1792. * sometimes does the gamma transform and cancels the flag.
  1793. *
  1794. * TODO: this is confusing. It only changes the result of png_get_gAMA and,
  1795. * yes, it does return the value that the transformed data effectively has
  1796. * but does any app really understand this?
  1797. */
  1798. info_ptr->gamma = png_ptr->file_gamma;
  1799. #endif
  1800. if (info_ptr->bit_depth == 16)
  1801. {
  1802. # ifdef PNG_READ_16BIT_SUPPORTED
  1803. # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  1804. if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0)
  1805. info_ptr->bit_depth = 8;
  1806. # endif
  1807. # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
  1808. if ((png_ptr->transformations & PNG_16_TO_8) != 0)
  1809. info_ptr->bit_depth = 8;
  1810. # endif
  1811. # else
  1812. /* No 16-bit support: force chopping 16-bit input down to 8, in this case
  1813. * the app program can chose if both APIs are available by setting the
  1814. * correct scaling to use.
  1815. */
  1816. # ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
  1817. /* For compatibility with previous versions use the strip method by
  1818. * default. This code works because if PNG_SCALE_16_TO_8 is already
  1819. * set the code below will do that in preference to the chop.
  1820. */
  1821. png_ptr->transformations |= PNG_16_TO_8;
  1822. info_ptr->bit_depth = 8;
  1823. # else
  1824. # ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  1825. png_ptr->transformations |= PNG_SCALE_16_TO_8;
  1826. info_ptr->bit_depth = 8;
  1827. # else
  1828. CONFIGURATION ERROR: you must enable at least one 16 to 8 method
  1829. # endif
  1830. # endif
  1831. #endif /* !READ_16BIT */
  1832. }
  1833. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  1834. if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0)
  1835. info_ptr->color_type = (png_byte)(info_ptr->color_type |
  1836. PNG_COLOR_MASK_COLOR);
  1837. #endif
  1838. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  1839. if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
  1840. info_ptr->color_type = (png_byte)(info_ptr->color_type &
  1841. ~PNG_COLOR_MASK_COLOR);
  1842. #endif
  1843. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  1844. if ((png_ptr->transformations & PNG_QUANTIZE) != 0)
  1845. {
  1846. if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
  1847. (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
  1848. png_ptr->palette_lookup != 0 && info_ptr->bit_depth == 8)
  1849. {
  1850. info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
  1851. }
  1852. }
  1853. #endif
  1854. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  1855. if ((png_ptr->transformations & PNG_EXPAND_16) != 0 &&
  1856. info_ptr->bit_depth == 8 &&
  1857. info_ptr->color_type != PNG_COLOR_TYPE_PALETTE)
  1858. {
  1859. info_ptr->bit_depth = 16;
  1860. }
  1861. #endif
  1862. #ifdef PNG_READ_PACK_SUPPORTED
  1863. if ((png_ptr->transformations & PNG_PACK) != 0 &&
  1864. (info_ptr->bit_depth < 8))
  1865. info_ptr->bit_depth = 8;
  1866. #endif
  1867. if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1868. info_ptr->channels = 1;
  1869. else if ((info_ptr->color_type & PNG_COLOR_MASK_COLOR) != 0)
  1870. info_ptr->channels = 3;
  1871. else
  1872. info_ptr->channels = 1;
  1873. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  1874. if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0)
  1875. {
  1876. info_ptr->color_type = (png_byte)(info_ptr->color_type &
  1877. ~PNG_COLOR_MASK_ALPHA);
  1878. info_ptr->num_trans = 0;
  1879. }
  1880. #endif
  1881. if ((info_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)
  1882. info_ptr->channels++;
  1883. #ifdef PNG_READ_FILLER_SUPPORTED
  1884. /* STRIP_ALPHA and FILLER allowed: MASK_ALPHA bit stripped above */
  1885. if ((png_ptr->transformations & PNG_FILLER) != 0 &&
  1886. (info_ptr->color_type == PNG_COLOR_TYPE_RGB ||
  1887. info_ptr->color_type == PNG_COLOR_TYPE_GRAY))
  1888. {
  1889. info_ptr->channels++;
  1890. /* If adding a true alpha channel not just filler */
  1891. if ((png_ptr->transformations & PNG_ADD_ALPHA) != 0)
  1892. info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
  1893. }
  1894. #endif
  1895. #if defined(PNG_USER_TRANSFORM_PTR_SUPPORTED) && \
  1896. defined(PNG_READ_USER_TRANSFORM_SUPPORTED)
  1897. if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
  1898. {
  1899. if (png_ptr->user_transform_depth != 0)
  1900. info_ptr->bit_depth = png_ptr->user_transform_depth;
  1901. if (png_ptr->user_transform_channels != 0)
  1902. info_ptr->channels = png_ptr->user_transform_channels;
  1903. }
  1904. #endif
  1905. info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
  1906. info_ptr->bit_depth);
  1907. info_ptr->rowbytes = PNG_ROWBYTES(info_ptr->pixel_depth, info_ptr->width);
  1908. /* Adding in 1.5.4: cache the above value in png_struct so that we can later
  1909. * check in png_rowbytes that the user buffer won't get overwritten. Note
  1910. * that the field is not always set - if png_read_update_info isn't called
  1911. * the application has to either not do any transforms or get the calculation
  1912. * right itself.
  1913. */
  1914. png_ptr->info_rowbytes = info_ptr->rowbytes;
  1915. #ifndef PNG_READ_EXPAND_SUPPORTED
  1916. if (png_ptr != NULL)
  1917. return;
  1918. #endif
  1919. }
  1920. #ifdef PNG_READ_PACK_SUPPORTED
  1921. /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
  1922. * without changing the actual values. Thus, if you had a row with
  1923. * a bit depth of 1, you would end up with bytes that only contained
  1924. * the numbers 0 or 1. If you would rather they contain 0 and 255, use
  1925. * png_do_shift() after this.
  1926. */
  1927. static void
  1928. png_do_unpack(png_row_infop row_info, png_bytep row)
  1929. {
  1930. png_debug(1, "in png_do_unpack");
  1931. if (row_info->bit_depth < 8)
  1932. {
  1933. png_uint_32 i;
  1934. png_uint_32 row_width=row_info->width;
  1935. switch (row_info->bit_depth)
  1936. {
  1937. case 1:
  1938. {
  1939. png_bytep sp = row + (size_t)((row_width - 1) >> 3);
  1940. png_bytep dp = row + (size_t)row_width - 1;
  1941. png_uint_32 shift = 7U - ((row_width + 7U) & 0x07);
  1942. for (i = 0; i < row_width; i++)
  1943. {
  1944. *dp = (png_byte)((*sp >> shift) & 0x01);
  1945. if (shift == 7)
  1946. {
  1947. shift = 0;
  1948. sp--;
  1949. }
  1950. else
  1951. shift++;
  1952. dp--;
  1953. }
  1954. break;
  1955. }
  1956. case 2:
  1957. {
  1958. png_bytep sp = row + (size_t)((row_width - 1) >> 2);
  1959. png_bytep dp = row + (size_t)row_width - 1;
  1960. png_uint_32 shift = ((3U - ((row_width + 3U) & 0x03)) << 1);
  1961. for (i = 0; i < row_width; i++)
  1962. {
  1963. *dp = (png_byte)((*sp >> shift) & 0x03);
  1964. if (shift == 6)
  1965. {
  1966. shift = 0;
  1967. sp--;
  1968. }
  1969. else
  1970. shift += 2;
  1971. dp--;
  1972. }
  1973. break;
  1974. }
  1975. case 4:
  1976. {
  1977. png_bytep sp = row + (size_t)((row_width - 1) >> 1);
  1978. png_bytep dp = row + (size_t)row_width - 1;
  1979. png_uint_32 shift = ((1U - ((row_width + 1U) & 0x01)) << 2);
  1980. for (i = 0; i < row_width; i++)
  1981. {
  1982. *dp = (png_byte)((*sp >> shift) & 0x0f);
  1983. if (shift == 4)
  1984. {
  1985. shift = 0;
  1986. sp--;
  1987. }
  1988. else
  1989. shift = 4;
  1990. dp--;
  1991. }
  1992. break;
  1993. }
  1994. default:
  1995. break;
  1996. }
  1997. row_info->bit_depth = 8;
  1998. row_info->pixel_depth = (png_byte)(8 * row_info->channels);
  1999. row_info->rowbytes = row_width * row_info->channels;
  2000. }
  2001. }
  2002. #endif
  2003. #ifdef PNG_READ_SHIFT_SUPPORTED
  2004. /* Reverse the effects of png_do_shift. This routine merely shifts the
  2005. * pixels back to their significant bits values. Thus, if you have
  2006. * a row of bit depth 8, but only 5 are significant, this will shift
  2007. * the values back to 0 through 31.
  2008. */
  2009. static void
  2010. png_do_unshift(png_row_infop row_info, png_bytep row,
  2011. png_const_color_8p sig_bits)
  2012. {
  2013. int color_type;
  2014. png_debug(1, "in png_do_unshift");
  2015. /* The palette case has already been handled in the _init routine. */
  2016. color_type = row_info->color_type;
  2017. if (color_type != PNG_COLOR_TYPE_PALETTE)
  2018. {
  2019. int shift[4];
  2020. int channels = 0;
  2021. int bit_depth = row_info->bit_depth;
  2022. if ((color_type & PNG_COLOR_MASK_COLOR) != 0)
  2023. {
  2024. shift[channels++] = bit_depth - sig_bits->red;
  2025. shift[channels++] = bit_depth - sig_bits->green;
  2026. shift[channels++] = bit_depth - sig_bits->blue;
  2027. }
  2028. else
  2029. {
  2030. shift[channels++] = bit_depth - sig_bits->gray;
  2031. }
  2032. if ((color_type & PNG_COLOR_MASK_ALPHA) != 0)
  2033. {
  2034. shift[channels++] = bit_depth - sig_bits->alpha;
  2035. }
  2036. {
  2037. int c, have_shift;
  2038. for (c = have_shift = 0; c < channels; ++c)
  2039. {
  2040. /* A shift of more than the bit depth is an error condition but it
  2041. * gets ignored here.
  2042. */
  2043. if (shift[c] <= 0 || shift[c] >= bit_depth)
  2044. shift[c] = 0;
  2045. else
  2046. have_shift = 1;
  2047. }
  2048. if (have_shift == 0)
  2049. return;
  2050. }
  2051. switch (bit_depth)
  2052. {
  2053. default:
  2054. /* Must be 1bpp gray: should not be here! */
  2055. /* NOTREACHED */
  2056. break;
  2057. case 2:
  2058. /* Must be 2bpp gray */
  2059. /* assert(channels == 1 && shift[0] == 1) */
  2060. {
  2061. png_bytep bp = row;
  2062. png_bytep bp_end = bp + row_info->rowbytes;
  2063. while (bp < bp_end)
  2064. {
  2065. int b = (*bp >> 1) & 0x55;
  2066. *bp++ = (png_byte)b;
  2067. }
  2068. break;
  2069. }
  2070. case 4:
  2071. /* Must be 4bpp gray */
  2072. /* assert(channels == 1) */
  2073. {
  2074. png_bytep bp = row;
  2075. png_bytep bp_end = bp + row_info->rowbytes;
  2076. int gray_shift = shift[0];
  2077. int mask = 0xf >> gray_shift;
  2078. mask |= mask << 4;
  2079. while (bp < bp_end)
  2080. {
  2081. int b = (*bp >> gray_shift) & mask;
  2082. *bp++ = (png_byte)b;
  2083. }
  2084. break;
  2085. }
  2086. case 8:
  2087. /* Single byte components, G, GA, RGB, RGBA */
  2088. {
  2089. png_bytep bp = row;
  2090. png_bytep bp_end = bp + row_info->rowbytes;
  2091. int channel = 0;
  2092. while (bp < bp_end)
  2093. {
  2094. int b = *bp >> shift[channel];
  2095. if (++channel >= channels)
  2096. channel = 0;
  2097. *bp++ = (png_byte)b;
  2098. }
  2099. break;
  2100. }
  2101. #ifdef PNG_READ_16BIT_SUPPORTED
  2102. case 16:
  2103. /* Double byte components, G, GA, RGB, RGBA */
  2104. {
  2105. png_bytep bp = row;
  2106. png_bytep bp_end = bp + row_info->rowbytes;
  2107. int channel = 0;
  2108. while (bp < bp_end)
  2109. {
  2110. int value = (bp[0] << 8) + bp[1];
  2111. value >>= shift[channel];
  2112. if (++channel >= channels)
  2113. channel = 0;
  2114. *bp++ = (png_byte)(value >> 8);
  2115. *bp++ = (png_byte)value;
  2116. }
  2117. break;
  2118. }
  2119. #endif
  2120. }
  2121. }
  2122. }
  2123. #endif
  2124. #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  2125. /* Scale rows of bit depth 16 down to 8 accurately */
  2126. static void
  2127. png_do_scale_16_to_8(png_row_infop row_info, png_bytep row)
  2128. {
  2129. png_debug(1, "in png_do_scale_16_to_8");
  2130. if (row_info->bit_depth == 16)
  2131. {
  2132. png_bytep sp = row; /* source */
  2133. png_bytep dp = row; /* destination */
  2134. png_bytep ep = sp + row_info->rowbytes; /* end+1 */
  2135. while (sp < ep)
  2136. {
  2137. /* The input is an array of 16-bit components, these must be scaled to
  2138. * 8 bits each. For a 16-bit value V the required value (from the PNG
  2139. * specification) is:
  2140. *
  2141. * (V * 255) / 65535
  2142. *
  2143. * This reduces to round(V / 257), or floor((V + 128.5)/257)
  2144. *
  2145. * Represent V as the two byte value vhi.vlo. Make a guess that the
  2146. * result is the top byte of V, vhi, then the correction to this value
  2147. * is:
  2148. *
  2149. * error = floor(((V-vhi.vhi) + 128.5) / 257)
  2150. * = floor(((vlo-vhi) + 128.5) / 257)
  2151. *
  2152. * This can be approximated using integer arithmetic (and a signed
  2153. * shift):
  2154. *
  2155. * error = (vlo-vhi+128) >> 8;
  2156. *
  2157. * The approximate differs from the exact answer only when (vlo-vhi) is
  2158. * 128; it then gives a correction of +1 when the exact correction is
  2159. * 0. This gives 128 errors. The exact answer (correct for all 16-bit
  2160. * input values) is:
  2161. *
  2162. * error = (vlo-vhi+128)*65535 >> 24;
  2163. *
  2164. * An alternative arithmetic calculation which also gives no errors is:
  2165. *
  2166. * (V * 255 + 32895) >> 16
  2167. */
  2168. png_int_32 tmp = *sp++; /* must be signed! */
  2169. tmp += (((int)*sp++ - tmp + 128) * 65535) >> 24;
  2170. *dp++ = (png_byte)tmp;
  2171. }
  2172. row_info->bit_depth = 8;
  2173. row_info->pixel_depth = (png_byte)(8 * row_info->channels);
  2174. row_info->rowbytes = row_info->width * row_info->channels;
  2175. }
  2176. }
  2177. #endif
  2178. #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
  2179. static void
  2180. /* Simply discard the low byte. This was the default behavior prior
  2181. * to libpng-1.5.4.
  2182. */
  2183. png_do_chop(png_row_infop row_info, png_bytep row)
  2184. {
  2185. png_debug(1, "in png_do_chop");
  2186. if (row_info->bit_depth == 16)
  2187. {
  2188. png_bytep sp = row; /* source */
  2189. png_bytep dp = row; /* destination */
  2190. png_bytep ep = sp + row_info->rowbytes; /* end+1 */
  2191. while (sp < ep)
  2192. {
  2193. *dp++ = *sp;
  2194. sp += 2; /* skip low byte */
  2195. }
  2196. row_info->bit_depth = 8;
  2197. row_info->pixel_depth = (png_byte)(8 * row_info->channels);
  2198. row_info->rowbytes = row_info->width * row_info->channels;
  2199. }
  2200. }
  2201. #endif
  2202. #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
  2203. static void
  2204. png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
  2205. {
  2206. png_uint_32 row_width = row_info->width;
  2207. png_debug(1, "in png_do_read_swap_alpha");
  2208. if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  2209. {
  2210. /* This converts from RGBA to ARGB */
  2211. if (row_info->bit_depth == 8)
  2212. {
  2213. png_bytep sp = row + row_info->rowbytes;
  2214. png_bytep dp = sp;
  2215. png_byte save;
  2216. png_uint_32 i;
  2217. for (i = 0; i < row_width; i++)
  2218. {
  2219. save = *(--sp);
  2220. *(--dp) = *(--sp);
  2221. *(--dp) = *(--sp);
  2222. *(--dp) = *(--sp);
  2223. *(--dp) = save;
  2224. }
  2225. }
  2226. #ifdef PNG_READ_16BIT_SUPPORTED
  2227. /* This converts from RRGGBBAA to AARRGGBB */
  2228. else
  2229. {
  2230. png_bytep sp = row + row_info->rowbytes;
  2231. png_bytep dp = sp;
  2232. png_byte save[2];
  2233. png_uint_32 i;
  2234. for (i = 0; i < row_width; i++)
  2235. {
  2236. save[0] = *(--sp);
  2237. save[1] = *(--sp);
  2238. *(--dp) = *(--sp);
  2239. *(--dp) = *(--sp);
  2240. *(--dp) = *(--sp);
  2241. *(--dp) = *(--sp);
  2242. *(--dp) = *(--sp);
  2243. *(--dp) = *(--sp);
  2244. *(--dp) = save[0];
  2245. *(--dp) = save[1];
  2246. }
  2247. }
  2248. #endif
  2249. }
  2250. else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  2251. {
  2252. /* This converts from GA to AG */
  2253. if (row_info->bit_depth == 8)
  2254. {
  2255. png_bytep sp = row + row_info->rowbytes;
  2256. png_bytep dp = sp;
  2257. png_byte save;
  2258. png_uint_32 i;
  2259. for (i = 0; i < row_width; i++)
  2260. {
  2261. save = *(--sp);
  2262. *(--dp) = *(--sp);
  2263. *(--dp) = save;
  2264. }
  2265. }
  2266. #ifdef PNG_READ_16BIT_SUPPORTED
  2267. /* This converts from GGAA to AAGG */
  2268. else
  2269. {
  2270. png_bytep sp = row + row_info->rowbytes;
  2271. png_bytep dp = sp;
  2272. png_byte save[2];
  2273. png_uint_32 i;
  2274. for (i = 0; i < row_width; i++)
  2275. {
  2276. save[0] = *(--sp);
  2277. save[1] = *(--sp);
  2278. *(--dp) = *(--sp);
  2279. *(--dp) = *(--sp);
  2280. *(--dp) = save[0];
  2281. *(--dp) = save[1];
  2282. }
  2283. }
  2284. #endif
  2285. }
  2286. }
  2287. #endif
  2288. #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
  2289. static void
  2290. png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
  2291. {
  2292. png_uint_32 row_width;
  2293. png_debug(1, "in png_do_read_invert_alpha");
  2294. row_width = row_info->width;
  2295. if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  2296. {
  2297. if (row_info->bit_depth == 8)
  2298. {
  2299. /* This inverts the alpha channel in RGBA */
  2300. png_bytep sp = row + row_info->rowbytes;
  2301. png_bytep dp = sp;
  2302. png_uint_32 i;
  2303. for (i = 0; i < row_width; i++)
  2304. {
  2305. *(--dp) = (png_byte)(255 - *(--sp));
  2306. /* This does nothing:
  2307. *(--dp) = *(--sp);
  2308. *(--dp) = *(--sp);
  2309. *(--dp) = *(--sp);
  2310. We can replace it with:
  2311. */
  2312. sp-=3;
  2313. dp=sp;
  2314. }
  2315. }
  2316. #ifdef PNG_READ_16BIT_SUPPORTED
  2317. /* This inverts the alpha channel in RRGGBBAA */
  2318. else
  2319. {
  2320. png_bytep sp = row + row_info->rowbytes;
  2321. png_bytep dp = sp;
  2322. png_uint_32 i;
  2323. for (i = 0; i < row_width; i++)
  2324. {
  2325. *(--dp) = (png_byte)(255 - *(--sp));
  2326. *(--dp) = (png_byte)(255 - *(--sp));
  2327. /* This does nothing:
  2328. *(--dp) = *(--sp);
  2329. *(--dp) = *(--sp);
  2330. *(--dp) = *(--sp);
  2331. *(--dp) = *(--sp);
  2332. *(--dp) = *(--sp);
  2333. *(--dp) = *(--sp);
  2334. We can replace it with:
  2335. */
  2336. sp-=6;
  2337. dp=sp;
  2338. }
  2339. }
  2340. #endif
  2341. }
  2342. else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  2343. {
  2344. if (row_info->bit_depth == 8)
  2345. {
  2346. /* This inverts the alpha channel in GA */
  2347. png_bytep sp = row + row_info->rowbytes;
  2348. png_bytep dp = sp;
  2349. png_uint_32 i;
  2350. for (i = 0; i < row_width; i++)
  2351. {
  2352. *(--dp) = (png_byte)(255 - *(--sp));
  2353. *(--dp) = *(--sp);
  2354. }
  2355. }
  2356. #ifdef PNG_READ_16BIT_SUPPORTED
  2357. else
  2358. {
  2359. /* This inverts the alpha channel in GGAA */
  2360. png_bytep sp = row + row_info->rowbytes;
  2361. png_bytep dp = sp;
  2362. png_uint_32 i;
  2363. for (i = 0; i < row_width; i++)
  2364. {
  2365. *(--dp) = (png_byte)(255 - *(--sp));
  2366. *(--dp) = (png_byte)(255 - *(--sp));
  2367. /*
  2368. *(--dp) = *(--sp);
  2369. *(--dp) = *(--sp);
  2370. */
  2371. sp-=2;
  2372. dp=sp;
  2373. }
  2374. }
  2375. #endif
  2376. }
  2377. }
  2378. #endif
  2379. #ifdef PNG_READ_FILLER_SUPPORTED
  2380. /* Add filler channel if we have RGB color */
  2381. static void
  2382. png_do_read_filler(png_row_infop row_info, png_bytep row,
  2383. png_uint_32 filler, png_uint_32 flags)
  2384. {
  2385. png_uint_32 i;
  2386. png_uint_32 row_width = row_info->width;
  2387. #ifdef PNG_READ_16BIT_SUPPORTED
  2388. png_byte hi_filler = (png_byte)(filler>>8);
  2389. #endif
  2390. png_byte lo_filler = (png_byte)filler;
  2391. png_debug(1, "in png_do_read_filler");
  2392. if (
  2393. row_info->color_type == PNG_COLOR_TYPE_GRAY)
  2394. {
  2395. if (row_info->bit_depth == 8)
  2396. {
  2397. if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
  2398. {
  2399. /* This changes the data from G to GX */
  2400. png_bytep sp = row + (size_t)row_width;
  2401. png_bytep dp = sp + (size_t)row_width;
  2402. for (i = 1; i < row_width; i++)
  2403. {
  2404. *(--dp) = lo_filler;
  2405. *(--dp) = *(--sp);
  2406. }
  2407. *(--dp) = lo_filler;
  2408. row_info->channels = 2;
  2409. row_info->pixel_depth = 16;
  2410. row_info->rowbytes = row_width * 2;
  2411. }
  2412. else
  2413. {
  2414. /* This changes the data from G to XG */
  2415. png_bytep sp = row + (size_t)row_width;
  2416. png_bytep dp = sp + (size_t)row_width;
  2417. for (i = 0; i < row_width; i++)
  2418. {
  2419. *(--dp) = *(--sp);
  2420. *(--dp) = lo_filler;
  2421. }
  2422. row_info->channels = 2;
  2423. row_info->pixel_depth = 16;
  2424. row_info->rowbytes = row_width * 2;
  2425. }
  2426. }
  2427. #ifdef PNG_READ_16BIT_SUPPORTED
  2428. else if (row_info->bit_depth == 16)
  2429. {
  2430. if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
  2431. {
  2432. /* This changes the data from GG to GGXX */
  2433. png_bytep sp = row + (size_t)row_width * 2;
  2434. png_bytep dp = sp + (size_t)row_width * 2;
  2435. for (i = 1; i < row_width; i++)
  2436. {
  2437. *(--dp) = lo_filler;
  2438. *(--dp) = hi_filler;
  2439. *(--dp) = *(--sp);
  2440. *(--dp) = *(--sp);
  2441. }
  2442. *(--dp) = lo_filler;
  2443. *(--dp) = hi_filler;
  2444. row_info->channels = 2;
  2445. row_info->pixel_depth = 32;
  2446. row_info->rowbytes = row_width * 4;
  2447. }
  2448. else
  2449. {
  2450. /* This changes the data from GG to XXGG */
  2451. png_bytep sp = row + (size_t)row_width * 2;
  2452. png_bytep dp = sp + (size_t)row_width * 2;
  2453. for (i = 0; i < row_width; i++)
  2454. {
  2455. *(--dp) = *(--sp);
  2456. *(--dp) = *(--sp);
  2457. *(--dp) = lo_filler;
  2458. *(--dp) = hi_filler;
  2459. }
  2460. row_info->channels = 2;
  2461. row_info->pixel_depth = 32;
  2462. row_info->rowbytes = row_width * 4;
  2463. }
  2464. }
  2465. #endif
  2466. } /* COLOR_TYPE == GRAY */
  2467. else if (row_info->color_type == PNG_COLOR_TYPE_RGB)
  2468. {
  2469. if (row_info->bit_depth == 8)
  2470. {
  2471. if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
  2472. {
  2473. /* This changes the data from RGB to RGBX */
  2474. png_bytep sp = row + (size_t)row_width * 3;
  2475. png_bytep dp = sp + (size_t)row_width;
  2476. for (i = 1; i < row_width; i++)
  2477. {
  2478. *(--dp) = lo_filler;
  2479. *(--dp) = *(--sp);
  2480. *(--dp) = *(--sp);
  2481. *(--dp) = *(--sp);
  2482. }
  2483. *(--dp) = lo_filler;
  2484. row_info->channels = 4;
  2485. row_info->pixel_depth = 32;
  2486. row_info->rowbytes = row_width * 4;
  2487. }
  2488. else
  2489. {
  2490. /* This changes the data from RGB to XRGB */
  2491. png_bytep sp = row + (size_t)row_width * 3;
  2492. png_bytep dp = sp + (size_t)row_width;
  2493. for (i = 0; i < row_width; i++)
  2494. {
  2495. *(--dp) = *(--sp);
  2496. *(--dp) = *(--sp);
  2497. *(--dp) = *(--sp);
  2498. *(--dp) = lo_filler;
  2499. }
  2500. row_info->channels = 4;
  2501. row_info->pixel_depth = 32;
  2502. row_info->rowbytes = row_width * 4;
  2503. }
  2504. }
  2505. #ifdef PNG_READ_16BIT_SUPPORTED
  2506. else if (row_info->bit_depth == 16)
  2507. {
  2508. if ((flags & PNG_FLAG_FILLER_AFTER) != 0)
  2509. {
  2510. /* This changes the data from RRGGBB to RRGGBBXX */
  2511. png_bytep sp = row + (size_t)row_width * 6;
  2512. png_bytep dp = sp + (size_t)row_width * 2;
  2513. for (i = 1; i < row_width; i++)
  2514. {
  2515. *(--dp) = lo_filler;
  2516. *(--dp) = hi_filler;
  2517. *(--dp) = *(--sp);
  2518. *(--dp) = *(--sp);
  2519. *(--dp) = *(--sp);
  2520. *(--dp) = *(--sp);
  2521. *(--dp) = *(--sp);
  2522. *(--dp) = *(--sp);
  2523. }
  2524. *(--dp) = lo_filler;
  2525. *(--dp) = hi_filler;
  2526. row_info->channels = 4;
  2527. row_info->pixel_depth = 64;
  2528. row_info->rowbytes = row_width * 8;
  2529. }
  2530. else
  2531. {
  2532. /* This changes the data from RRGGBB to XXRRGGBB */
  2533. png_bytep sp = row + (size_t)row_width * 6;
  2534. png_bytep dp = sp + (size_t)row_width * 2;
  2535. for (i = 0; i < row_width; i++)
  2536. {
  2537. *(--dp) = *(--sp);
  2538. *(--dp) = *(--sp);
  2539. *(--dp) = *(--sp);
  2540. *(--dp) = *(--sp);
  2541. *(--dp) = *(--sp);
  2542. *(--dp) = *(--sp);
  2543. *(--dp) = lo_filler;
  2544. *(--dp) = hi_filler;
  2545. }
  2546. row_info->channels = 4;
  2547. row_info->pixel_depth = 64;
  2548. row_info->rowbytes = row_width * 8;
  2549. }
  2550. }
  2551. #endif
  2552. } /* COLOR_TYPE == RGB */
  2553. }
  2554. #endif
  2555. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  2556. /* Expand grayscale files to RGB, with or without alpha */
  2557. static void
  2558. png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
  2559. {
  2560. png_uint_32 i;
  2561. png_uint_32 row_width = row_info->width;
  2562. png_debug(1, "in png_do_gray_to_rgb");
  2563. if (row_info->bit_depth >= 8 &&
  2564. (row_info->color_type & PNG_COLOR_MASK_COLOR) == 0)
  2565. {
  2566. if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
  2567. {
  2568. if (row_info->bit_depth == 8)
  2569. {
  2570. /* This changes G to RGB */
  2571. png_bytep sp = row + (size_t)row_width - 1;
  2572. png_bytep dp = sp + (size_t)row_width * 2;
  2573. for (i = 0; i < row_width; i++)
  2574. {
  2575. *(dp--) = *sp;
  2576. *(dp--) = *sp;
  2577. *(dp--) = *(sp--);
  2578. }
  2579. }
  2580. else
  2581. {
  2582. /* This changes GG to RRGGBB */
  2583. png_bytep sp = row + (size_t)row_width * 2 - 1;
  2584. png_bytep dp = sp + (size_t)row_width * 4;
  2585. for (i = 0; i < row_width; i++)
  2586. {
  2587. *(dp--) = *sp;
  2588. *(dp--) = *(sp - 1);
  2589. *(dp--) = *sp;
  2590. *(dp--) = *(sp - 1);
  2591. *(dp--) = *(sp--);
  2592. *(dp--) = *(sp--);
  2593. }
  2594. }
  2595. }
  2596. else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  2597. {
  2598. if (row_info->bit_depth == 8)
  2599. {
  2600. /* This changes GA to RGBA */
  2601. png_bytep sp = row + (size_t)row_width * 2 - 1;
  2602. png_bytep dp = sp + (size_t)row_width * 2;
  2603. for (i = 0; i < row_width; i++)
  2604. {
  2605. *(dp--) = *(sp--);
  2606. *(dp--) = *sp;
  2607. *(dp--) = *sp;
  2608. *(dp--) = *(sp--);
  2609. }
  2610. }
  2611. else
  2612. {
  2613. /* This changes GGAA to RRGGBBAA */
  2614. png_bytep sp = row + (size_t)row_width * 4 - 1;
  2615. png_bytep dp = sp + (size_t)row_width * 4;
  2616. for (i = 0; i < row_width; i++)
  2617. {
  2618. *(dp--) = *(sp--);
  2619. *(dp--) = *(sp--);
  2620. *(dp--) = *sp;
  2621. *(dp--) = *(sp - 1);
  2622. *(dp--) = *sp;
  2623. *(dp--) = *(sp - 1);
  2624. *(dp--) = *(sp--);
  2625. *(dp--) = *(sp--);
  2626. }
  2627. }
  2628. }
  2629. row_info->channels = (png_byte)(row_info->channels + 2);
  2630. row_info->color_type |= PNG_COLOR_MASK_COLOR;
  2631. row_info->pixel_depth = (png_byte)(row_info->channels *
  2632. row_info->bit_depth);
  2633. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  2634. }
  2635. }
  2636. #endif
  2637. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  2638. /* Reduce RGB files to grayscale, with or without alpha
  2639. * using the equation given in Poynton's ColorFAQ of 1998-01-04 at
  2640. * <http://www.inforamp.net/~poynton/> (THIS LINK IS DEAD June 2008 but
  2641. * versions dated 1998 through November 2002 have been archived at
  2642. * https://web.archive.org/web/20000816232553/www.inforamp.net/
  2643. * ~poynton/notes/colour_and_gamma/ColorFAQ.txt )
  2644. * Charles Poynton poynton at poynton.com
  2645. *
  2646. * Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
  2647. *
  2648. * which can be expressed with integers as
  2649. *
  2650. * Y = (6969 * R + 23434 * G + 2365 * B)/32768
  2651. *
  2652. * Poynton's current link (as of January 2003 through July 2011):
  2653. * <http://www.poynton.com/notes/colour_and_gamma/>
  2654. * has changed the numbers slightly:
  2655. *
  2656. * Y = 0.2126*R + 0.7152*G + 0.0722*B
  2657. *
  2658. * which can be expressed with integers as
  2659. *
  2660. * Y = (6966 * R + 23436 * G + 2366 * B)/32768
  2661. *
  2662. * Historically, however, libpng uses numbers derived from the ITU-R Rec 709
  2663. * end point chromaticities and the D65 white point. Depending on the
  2664. * precision used for the D65 white point this produces a variety of different
  2665. * numbers, however if the four decimal place value used in ITU-R Rec 709 is
  2666. * used (0.3127,0.3290) the Y calculation would be:
  2667. *
  2668. * Y = (6968 * R + 23435 * G + 2366 * B)/32768
  2669. *
  2670. * While this is correct the rounding results in an overflow for white, because
  2671. * the sum of the rounded coefficients is 32769, not 32768. Consequently
  2672. * libpng uses, instead, the closest non-overflowing approximation:
  2673. *
  2674. * Y = (6968 * R + 23434 * G + 2366 * B)/32768
  2675. *
  2676. * Starting with libpng-1.5.5, if the image being converted has a cHRM chunk
  2677. * (including an sRGB chunk) then the chromaticities are used to calculate the
  2678. * coefficients. See the chunk handling in pngrutil.c for more information.
  2679. *
  2680. * In all cases the calculation is to be done in a linear colorspace. If no
  2681. * gamma information is available to correct the encoding of the original RGB
  2682. * values this results in an implicit assumption that the original PNG RGB
  2683. * values were linear.
  2684. *
  2685. * Other integer coefficients can be used via png_set_rgb_to_gray(). Because
  2686. * the API takes just red and green coefficients the blue coefficient is
  2687. * calculated to make the sum 32768. This will result in different rounding
  2688. * to that used above.
  2689. */
  2690. static int
  2691. png_do_rgb_to_gray(png_structrp png_ptr, png_row_infop row_info, png_bytep row)
  2692. {
  2693. int rgb_error = 0;
  2694. png_debug(1, "in png_do_rgb_to_gray");
  2695. if ((row_info->color_type & PNG_COLOR_MASK_PALETTE) == 0 &&
  2696. (row_info->color_type & PNG_COLOR_MASK_COLOR) != 0)
  2697. {
  2698. png_uint_32 rc = png_ptr->rgb_to_gray_red_coeff;
  2699. png_uint_32 gc = png_ptr->rgb_to_gray_green_coeff;
  2700. png_uint_32 bc = 32768 - rc - gc;
  2701. png_uint_32 row_width = row_info->width;
  2702. int have_alpha = (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0;
  2703. if (row_info->bit_depth == 8)
  2704. {
  2705. #ifdef PNG_READ_GAMMA_SUPPORTED
  2706. /* Notice that gamma to/from 1 are not necessarily inverses (if
  2707. * there is an overall gamma correction). Prior to 1.5.5 this code
  2708. * checked the linearized values for equality; this doesn't match
  2709. * the documentation, the original values must be checked.
  2710. */
  2711. if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
  2712. {
  2713. png_bytep sp = row;
  2714. png_bytep dp = row;
  2715. png_uint_32 i;
  2716. for (i = 0; i < row_width; i++)
  2717. {
  2718. png_byte red = *(sp++);
  2719. png_byte green = *(sp++);
  2720. png_byte blue = *(sp++);
  2721. if (red != green || red != blue)
  2722. {
  2723. red = png_ptr->gamma_to_1[red];
  2724. green = png_ptr->gamma_to_1[green];
  2725. blue = png_ptr->gamma_to_1[blue];
  2726. rgb_error |= 1;
  2727. *(dp++) = png_ptr->gamma_from_1[
  2728. (rc*red + gc*green + bc*blue + 16384)>>15];
  2729. }
  2730. else
  2731. {
  2732. /* If there is no overall correction the table will not be
  2733. * set.
  2734. */
  2735. if (png_ptr->gamma_table != NULL)
  2736. red = png_ptr->gamma_table[red];
  2737. *(dp++) = red;
  2738. }
  2739. if (have_alpha != 0)
  2740. *(dp++) = *(sp++);
  2741. }
  2742. }
  2743. else
  2744. #endif
  2745. {
  2746. png_bytep sp = row;
  2747. png_bytep dp = row;
  2748. png_uint_32 i;
  2749. for (i = 0; i < row_width; i++)
  2750. {
  2751. png_byte red = *(sp++);
  2752. png_byte green = *(sp++);
  2753. png_byte blue = *(sp++);
  2754. if (red != green || red != blue)
  2755. {
  2756. rgb_error |= 1;
  2757. /* NOTE: this is the historical approach which simply
  2758. * truncates the results.
  2759. */
  2760. *(dp++) = (png_byte)((rc*red + gc*green + bc*blue)>>15);
  2761. }
  2762. else
  2763. *(dp++) = red;
  2764. if (have_alpha != 0)
  2765. *(dp++) = *(sp++);
  2766. }
  2767. }
  2768. }
  2769. else /* RGB bit_depth == 16 */
  2770. {
  2771. #ifdef PNG_READ_GAMMA_SUPPORTED
  2772. if (png_ptr->gamma_16_to_1 != NULL && png_ptr->gamma_16_from_1 != NULL)
  2773. {
  2774. png_bytep sp = row;
  2775. png_bytep dp = row;
  2776. png_uint_32 i;
  2777. for (i = 0; i < row_width; i++)
  2778. {
  2779. png_uint_16 red, green, blue, w;
  2780. png_byte hi,lo;
  2781. hi=*(sp)++; lo=*(sp)++; red = (png_uint_16)((hi << 8) | (lo));
  2782. hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo));
  2783. hi=*(sp)++; lo=*(sp)++; blue = (png_uint_16)((hi << 8) | (lo));
  2784. if (red == green && red == blue)
  2785. {
  2786. if (png_ptr->gamma_16_table != NULL)
  2787. w = png_ptr->gamma_16_table[(red & 0xff)
  2788. >> png_ptr->gamma_shift][red >> 8];
  2789. else
  2790. w = red;
  2791. }
  2792. else
  2793. {
  2794. png_uint_16 red_1 = png_ptr->gamma_16_to_1[(red & 0xff)
  2795. >> png_ptr->gamma_shift][red>>8];
  2796. png_uint_16 green_1 =
  2797. png_ptr->gamma_16_to_1[(green & 0xff) >>
  2798. png_ptr->gamma_shift][green>>8];
  2799. png_uint_16 blue_1 = png_ptr->gamma_16_to_1[(blue & 0xff)
  2800. >> png_ptr->gamma_shift][blue>>8];
  2801. png_uint_16 gray16 = (png_uint_16)((rc*red_1 + gc*green_1
  2802. + bc*blue_1 + 16384)>>15);
  2803. w = png_ptr->gamma_16_from_1[(gray16 & 0xff) >>
  2804. png_ptr->gamma_shift][gray16 >> 8];
  2805. rgb_error |= 1;
  2806. }
  2807. *(dp++) = (png_byte)((w>>8) & 0xff);
  2808. *(dp++) = (png_byte)(w & 0xff);
  2809. if (have_alpha != 0)
  2810. {
  2811. *(dp++) = *(sp++);
  2812. *(dp++) = *(sp++);
  2813. }
  2814. }
  2815. }
  2816. else
  2817. #endif
  2818. {
  2819. png_bytep sp = row;
  2820. png_bytep dp = row;
  2821. png_uint_32 i;
  2822. for (i = 0; i < row_width; i++)
  2823. {
  2824. png_uint_16 red, green, blue, gray16;
  2825. png_byte hi,lo;
  2826. hi=*(sp)++; lo=*(sp)++; red = (png_uint_16)((hi << 8) | (lo));
  2827. hi=*(sp)++; lo=*(sp)++; green = (png_uint_16)((hi << 8) | (lo));
  2828. hi=*(sp)++; lo=*(sp)++; blue = (png_uint_16)((hi << 8) | (lo));
  2829. if (red != green || red != blue)
  2830. rgb_error |= 1;
  2831. /* From 1.5.5 in the 16-bit case do the accurate conversion even
  2832. * in the 'fast' case - this is because this is where the code
  2833. * ends up when handling linear 16-bit data.
  2834. */
  2835. gray16 = (png_uint_16)((rc*red + gc*green + bc*blue + 16384) >>
  2836. 15);
  2837. *(dp++) = (png_byte)((gray16 >> 8) & 0xff);
  2838. *(dp++) = (png_byte)(gray16 & 0xff);
  2839. if (have_alpha != 0)
  2840. {
  2841. *(dp++) = *(sp++);
  2842. *(dp++) = *(sp++);
  2843. }
  2844. }
  2845. }
  2846. }
  2847. row_info->channels = (png_byte)(row_info->channels - 2);
  2848. row_info->color_type = (png_byte)(row_info->color_type &
  2849. ~PNG_COLOR_MASK_COLOR);
  2850. row_info->pixel_depth = (png_byte)(row_info->channels *
  2851. row_info->bit_depth);
  2852. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  2853. }
  2854. return rgb_error;
  2855. }
  2856. #endif
  2857. #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
  2858. defined(PNG_READ_ALPHA_MODE_SUPPORTED)
  2859. /* Replace any alpha or transparency with the supplied background color.
  2860. * "background" is already in the screen gamma, while "background_1" is
  2861. * at a gamma of 1.0. Paletted files have already been taken care of.
  2862. */
  2863. static void
  2864. png_do_compose(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
  2865. {
  2866. #ifdef PNG_READ_GAMMA_SUPPORTED
  2867. png_const_bytep gamma_table = png_ptr->gamma_table;
  2868. png_const_bytep gamma_from_1 = png_ptr->gamma_from_1;
  2869. png_const_bytep gamma_to_1 = png_ptr->gamma_to_1;
  2870. png_const_uint_16pp gamma_16 = png_ptr->gamma_16_table;
  2871. png_const_uint_16pp gamma_16_from_1 = png_ptr->gamma_16_from_1;
  2872. png_const_uint_16pp gamma_16_to_1 = png_ptr->gamma_16_to_1;
  2873. int gamma_shift = png_ptr->gamma_shift;
  2874. int optimize = (png_ptr->flags & PNG_FLAG_OPTIMIZE_ALPHA) != 0;
  2875. #endif
  2876. png_bytep sp;
  2877. png_uint_32 i;
  2878. png_uint_32 row_width = row_info->width;
  2879. int shift;
  2880. png_debug(1, "in png_do_compose");
  2881. switch (row_info->color_type)
  2882. {
  2883. case PNG_COLOR_TYPE_GRAY:
  2884. {
  2885. switch (row_info->bit_depth)
  2886. {
  2887. case 1:
  2888. {
  2889. sp = row;
  2890. shift = 7;
  2891. for (i = 0; i < row_width; i++)
  2892. {
  2893. if ((png_uint_16)((*sp >> shift) & 0x01)
  2894. == png_ptr->trans_color.gray)
  2895. {
  2896. unsigned int tmp = *sp & (0x7f7f >> (7 - shift));
  2897. tmp |=
  2898. (unsigned int)(png_ptr->background.gray << shift);
  2899. *sp = (png_byte)(tmp & 0xff);
  2900. }
  2901. if (shift == 0)
  2902. {
  2903. shift = 7;
  2904. sp++;
  2905. }
  2906. else
  2907. shift--;
  2908. }
  2909. break;
  2910. }
  2911. case 2:
  2912. {
  2913. #ifdef PNG_READ_GAMMA_SUPPORTED
  2914. if (gamma_table != NULL)
  2915. {
  2916. sp = row;
  2917. shift = 6;
  2918. for (i = 0; i < row_width; i++)
  2919. {
  2920. if ((png_uint_16)((*sp >> shift) & 0x03)
  2921. == png_ptr->trans_color.gray)
  2922. {
  2923. unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
  2924. tmp |=
  2925. (unsigned int)png_ptr->background.gray << shift;
  2926. *sp = (png_byte)(tmp & 0xff);
  2927. }
  2928. else
  2929. {
  2930. unsigned int p = (*sp >> shift) & 0x03;
  2931. unsigned int g = (gamma_table [p | (p << 2) |
  2932. (p << 4) | (p << 6)] >> 6) & 0x03;
  2933. unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
  2934. tmp |= (unsigned int)(g << shift);
  2935. *sp = (png_byte)(tmp & 0xff);
  2936. }
  2937. if (shift == 0)
  2938. {
  2939. shift = 6;
  2940. sp++;
  2941. }
  2942. else
  2943. shift -= 2;
  2944. }
  2945. }
  2946. else
  2947. #endif
  2948. {
  2949. sp = row;
  2950. shift = 6;
  2951. for (i = 0; i < row_width; i++)
  2952. {
  2953. if ((png_uint_16)((*sp >> shift) & 0x03)
  2954. == png_ptr->trans_color.gray)
  2955. {
  2956. unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
  2957. tmp |=
  2958. (unsigned int)png_ptr->background.gray << shift;
  2959. *sp = (png_byte)(tmp & 0xff);
  2960. }
  2961. if (shift == 0)
  2962. {
  2963. shift = 6;
  2964. sp++;
  2965. }
  2966. else
  2967. shift -= 2;
  2968. }
  2969. }
  2970. break;
  2971. }
  2972. case 4:
  2973. {
  2974. #ifdef PNG_READ_GAMMA_SUPPORTED
  2975. if (gamma_table != NULL)
  2976. {
  2977. sp = row;
  2978. shift = 4;
  2979. for (i = 0; i < row_width; i++)
  2980. {
  2981. if ((png_uint_16)((*sp >> shift) & 0x0f)
  2982. == png_ptr->trans_color.gray)
  2983. {
  2984. unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
  2985. tmp |=
  2986. (unsigned int)(png_ptr->background.gray << shift);
  2987. *sp = (png_byte)(tmp & 0xff);
  2988. }
  2989. else
  2990. {
  2991. unsigned int p = (*sp >> shift) & 0x0f;
  2992. unsigned int g = (gamma_table[p | (p << 4)] >> 4) &
  2993. 0x0f;
  2994. unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
  2995. tmp |= (unsigned int)(g << shift);
  2996. *sp = (png_byte)(tmp & 0xff);
  2997. }
  2998. if (shift == 0)
  2999. {
  3000. shift = 4;
  3001. sp++;
  3002. }
  3003. else
  3004. shift -= 4;
  3005. }
  3006. }
  3007. else
  3008. #endif
  3009. {
  3010. sp = row;
  3011. shift = 4;
  3012. for (i = 0; i < row_width; i++)
  3013. {
  3014. if ((png_uint_16)((*sp >> shift) & 0x0f)
  3015. == png_ptr->trans_color.gray)
  3016. {
  3017. unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
  3018. tmp |=
  3019. (unsigned int)(png_ptr->background.gray << shift);
  3020. *sp = (png_byte)(tmp & 0xff);
  3021. }
  3022. if (shift == 0)
  3023. {
  3024. shift = 4;
  3025. sp++;
  3026. }
  3027. else
  3028. shift -= 4;
  3029. }
  3030. }
  3031. break;
  3032. }
  3033. case 8:
  3034. {
  3035. #ifdef PNG_READ_GAMMA_SUPPORTED
  3036. if (gamma_table != NULL)
  3037. {
  3038. sp = row;
  3039. for (i = 0; i < row_width; i++, sp++)
  3040. {
  3041. if (*sp == png_ptr->trans_color.gray)
  3042. *sp = (png_byte)png_ptr->background.gray;
  3043. else
  3044. *sp = gamma_table[*sp];
  3045. }
  3046. }
  3047. else
  3048. #endif
  3049. {
  3050. sp = row;
  3051. for (i = 0; i < row_width; i++, sp++)
  3052. {
  3053. if (*sp == png_ptr->trans_color.gray)
  3054. *sp = (png_byte)png_ptr->background.gray;
  3055. }
  3056. }
  3057. break;
  3058. }
  3059. case 16:
  3060. {
  3061. #ifdef PNG_READ_GAMMA_SUPPORTED
  3062. if (gamma_16 != NULL)
  3063. {
  3064. sp = row;
  3065. for (i = 0; i < row_width; i++, sp += 2)
  3066. {
  3067. png_uint_16 v;
  3068. v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  3069. if (v == png_ptr->trans_color.gray)
  3070. {
  3071. /* Background is already in screen gamma */
  3072. *sp = (png_byte)((png_ptr->background.gray >> 8)
  3073. & 0xff);
  3074. *(sp + 1) = (png_byte)(png_ptr->background.gray
  3075. & 0xff);
  3076. }
  3077. else
  3078. {
  3079. v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  3080. *sp = (png_byte)((v >> 8) & 0xff);
  3081. *(sp + 1) = (png_byte)(v & 0xff);
  3082. }
  3083. }
  3084. }
  3085. else
  3086. #endif
  3087. {
  3088. sp = row;
  3089. for (i = 0; i < row_width; i++, sp += 2)
  3090. {
  3091. png_uint_16 v;
  3092. v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  3093. if (v == png_ptr->trans_color.gray)
  3094. {
  3095. *sp = (png_byte)((png_ptr->background.gray >> 8)
  3096. & 0xff);
  3097. *(sp + 1) = (png_byte)(png_ptr->background.gray
  3098. & 0xff);
  3099. }
  3100. }
  3101. }
  3102. break;
  3103. }
  3104. default:
  3105. break;
  3106. }
  3107. break;
  3108. }
  3109. case PNG_COLOR_TYPE_RGB:
  3110. {
  3111. if (row_info->bit_depth == 8)
  3112. {
  3113. #ifdef PNG_READ_GAMMA_SUPPORTED
  3114. if (gamma_table != NULL)
  3115. {
  3116. sp = row;
  3117. for (i = 0; i < row_width; i++, sp += 3)
  3118. {
  3119. if (*sp == png_ptr->trans_color.red &&
  3120. *(sp + 1) == png_ptr->trans_color.green &&
  3121. *(sp + 2) == png_ptr->trans_color.blue)
  3122. {
  3123. *sp = (png_byte)png_ptr->background.red;
  3124. *(sp + 1) = (png_byte)png_ptr->background.green;
  3125. *(sp + 2) = (png_byte)png_ptr->background.blue;
  3126. }
  3127. else
  3128. {
  3129. *sp = gamma_table[*sp];
  3130. *(sp + 1) = gamma_table[*(sp + 1)];
  3131. *(sp + 2) = gamma_table[*(sp + 2)];
  3132. }
  3133. }
  3134. }
  3135. else
  3136. #endif
  3137. {
  3138. sp = row;
  3139. for (i = 0; i < row_width; i++, sp += 3)
  3140. {
  3141. if (*sp == png_ptr->trans_color.red &&
  3142. *(sp + 1) == png_ptr->trans_color.green &&
  3143. *(sp + 2) == png_ptr->trans_color.blue)
  3144. {
  3145. *sp = (png_byte)png_ptr->background.red;
  3146. *(sp + 1) = (png_byte)png_ptr->background.green;
  3147. *(sp + 2) = (png_byte)png_ptr->background.blue;
  3148. }
  3149. }
  3150. }
  3151. }
  3152. else /* if (row_info->bit_depth == 16) */
  3153. {
  3154. #ifdef PNG_READ_GAMMA_SUPPORTED
  3155. if (gamma_16 != NULL)
  3156. {
  3157. sp = row;
  3158. for (i = 0; i < row_width; i++, sp += 6)
  3159. {
  3160. png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  3161. png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
  3162. + *(sp + 3));
  3163. png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
  3164. + *(sp + 5));
  3165. if (r == png_ptr->trans_color.red &&
  3166. g == png_ptr->trans_color.green &&
  3167. b == png_ptr->trans_color.blue)
  3168. {
  3169. /* Background is already in screen gamma */
  3170. *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
  3171. *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
  3172. *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
  3173. & 0xff);
  3174. *(sp + 3) = (png_byte)(png_ptr->background.green
  3175. & 0xff);
  3176. *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
  3177. & 0xff);
  3178. *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
  3179. }
  3180. else
  3181. {
  3182. png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  3183. *sp = (png_byte)((v >> 8) & 0xff);
  3184. *(sp + 1) = (png_byte)(v & 0xff);
  3185. v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
  3186. *(sp + 2) = (png_byte)((v >> 8) & 0xff);
  3187. *(sp + 3) = (png_byte)(v & 0xff);
  3188. v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
  3189. *(sp + 4) = (png_byte)((v >> 8) & 0xff);
  3190. *(sp + 5) = (png_byte)(v & 0xff);
  3191. }
  3192. }
  3193. }
  3194. else
  3195. #endif
  3196. {
  3197. sp = row;
  3198. for (i = 0; i < row_width; i++, sp += 6)
  3199. {
  3200. png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  3201. png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
  3202. + *(sp + 3));
  3203. png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
  3204. + *(sp + 5));
  3205. if (r == png_ptr->trans_color.red &&
  3206. g == png_ptr->trans_color.green &&
  3207. b == png_ptr->trans_color.blue)
  3208. {
  3209. *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
  3210. *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
  3211. *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
  3212. & 0xff);
  3213. *(sp + 3) = (png_byte)(png_ptr->background.green
  3214. & 0xff);
  3215. *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
  3216. & 0xff);
  3217. *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
  3218. }
  3219. }
  3220. }
  3221. }
  3222. break;
  3223. }
  3224. case PNG_COLOR_TYPE_GRAY_ALPHA:
  3225. {
  3226. if (row_info->bit_depth == 8)
  3227. {
  3228. #ifdef PNG_READ_GAMMA_SUPPORTED
  3229. if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
  3230. gamma_table != NULL)
  3231. {
  3232. sp = row;
  3233. for (i = 0; i < row_width; i++, sp += 2)
  3234. {
  3235. png_uint_16 a = *(sp + 1);
  3236. if (a == 0xff)
  3237. *sp = gamma_table[*sp];
  3238. else if (a == 0)
  3239. {
  3240. /* Background is already in screen gamma */
  3241. *sp = (png_byte)png_ptr->background.gray;
  3242. }
  3243. else
  3244. {
  3245. png_byte v, w;
  3246. v = gamma_to_1[*sp];
  3247. png_composite(w, v, a, png_ptr->background_1.gray);
  3248. if (optimize == 0)
  3249. w = gamma_from_1[w];
  3250. *sp = w;
  3251. }
  3252. }
  3253. }
  3254. else
  3255. #endif
  3256. {
  3257. sp = row;
  3258. for (i = 0; i < row_width; i++, sp += 2)
  3259. {
  3260. png_byte a = *(sp + 1);
  3261. if (a == 0)
  3262. *sp = (png_byte)png_ptr->background.gray;
  3263. else if (a < 0xff)
  3264. png_composite(*sp, *sp, a, png_ptr->background.gray);
  3265. }
  3266. }
  3267. }
  3268. else /* if (png_ptr->bit_depth == 16) */
  3269. {
  3270. #ifdef PNG_READ_GAMMA_SUPPORTED
  3271. if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
  3272. gamma_16_to_1 != NULL)
  3273. {
  3274. sp = row;
  3275. for (i = 0; i < row_width; i++, sp += 4)
  3276. {
  3277. png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
  3278. + *(sp + 3));
  3279. if (a == (png_uint_16)0xffff)
  3280. {
  3281. png_uint_16 v;
  3282. v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  3283. *sp = (png_byte)((v >> 8) & 0xff);
  3284. *(sp + 1) = (png_byte)(v & 0xff);
  3285. }
  3286. else if (a == 0)
  3287. {
  3288. /* Background is already in screen gamma */
  3289. *sp = (png_byte)((png_ptr->background.gray >> 8)
  3290. & 0xff);
  3291. *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
  3292. }
  3293. else
  3294. {
  3295. png_uint_16 g, v, w;
  3296. g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
  3297. png_composite_16(v, g, a, png_ptr->background_1.gray);
  3298. if (optimize != 0)
  3299. w = v;
  3300. else
  3301. w = gamma_16_from_1[(v & 0xff) >>
  3302. gamma_shift][v >> 8];
  3303. *sp = (png_byte)((w >> 8) & 0xff);
  3304. *(sp + 1) = (png_byte)(w & 0xff);
  3305. }
  3306. }
  3307. }
  3308. else
  3309. #endif
  3310. {
  3311. sp = row;
  3312. for (i = 0; i < row_width; i++, sp += 4)
  3313. {
  3314. png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
  3315. + *(sp + 3));
  3316. if (a == 0)
  3317. {
  3318. *sp = (png_byte)((png_ptr->background.gray >> 8)
  3319. & 0xff);
  3320. *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
  3321. }
  3322. else if (a < 0xffff)
  3323. {
  3324. png_uint_16 g, v;
  3325. g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  3326. png_composite_16(v, g, a, png_ptr->background.gray);
  3327. *sp = (png_byte)((v >> 8) & 0xff);
  3328. *(sp + 1) = (png_byte)(v & 0xff);
  3329. }
  3330. }
  3331. }
  3332. }
  3333. break;
  3334. }
  3335. case PNG_COLOR_TYPE_RGB_ALPHA:
  3336. {
  3337. if (row_info->bit_depth == 8)
  3338. {
  3339. #ifdef PNG_READ_GAMMA_SUPPORTED
  3340. if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
  3341. gamma_table != NULL)
  3342. {
  3343. sp = row;
  3344. for (i = 0; i < row_width; i++, sp += 4)
  3345. {
  3346. png_byte a = *(sp + 3);
  3347. if (a == 0xff)
  3348. {
  3349. *sp = gamma_table[*sp];
  3350. *(sp + 1) = gamma_table[*(sp + 1)];
  3351. *(sp + 2) = gamma_table[*(sp + 2)];
  3352. }
  3353. else if (a == 0)
  3354. {
  3355. /* Background is already in screen gamma */
  3356. *sp = (png_byte)png_ptr->background.red;
  3357. *(sp + 1) = (png_byte)png_ptr->background.green;
  3358. *(sp + 2) = (png_byte)png_ptr->background.blue;
  3359. }
  3360. else
  3361. {
  3362. png_byte v, w;
  3363. v = gamma_to_1[*sp];
  3364. png_composite(w, v, a, png_ptr->background_1.red);
  3365. if (optimize == 0) w = gamma_from_1[w];
  3366. *sp = w;
  3367. v = gamma_to_1[*(sp + 1)];
  3368. png_composite(w, v, a, png_ptr->background_1.green);
  3369. if (optimize == 0) w = gamma_from_1[w];
  3370. *(sp + 1) = w;
  3371. v = gamma_to_1[*(sp + 2)];
  3372. png_composite(w, v, a, png_ptr->background_1.blue);
  3373. if (optimize == 0) w = gamma_from_1[w];
  3374. *(sp + 2) = w;
  3375. }
  3376. }
  3377. }
  3378. else
  3379. #endif
  3380. {
  3381. sp = row;
  3382. for (i = 0; i < row_width; i++, sp += 4)
  3383. {
  3384. png_byte a = *(sp + 3);
  3385. if (a == 0)
  3386. {
  3387. *sp = (png_byte)png_ptr->background.red;
  3388. *(sp + 1) = (png_byte)png_ptr->background.green;
  3389. *(sp + 2) = (png_byte)png_ptr->background.blue;
  3390. }
  3391. else if (a < 0xff)
  3392. {
  3393. png_composite(*sp, *sp, a, png_ptr->background.red);
  3394. png_composite(*(sp + 1), *(sp + 1), a,
  3395. png_ptr->background.green);
  3396. png_composite(*(sp + 2), *(sp + 2), a,
  3397. png_ptr->background.blue);
  3398. }
  3399. }
  3400. }
  3401. }
  3402. else /* if (row_info->bit_depth == 16) */
  3403. {
  3404. #ifdef PNG_READ_GAMMA_SUPPORTED
  3405. if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
  3406. gamma_16_to_1 != NULL)
  3407. {
  3408. sp = row;
  3409. for (i = 0; i < row_width; i++, sp += 8)
  3410. {
  3411. png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
  3412. << 8) + (png_uint_16)(*(sp + 7)));
  3413. if (a == (png_uint_16)0xffff)
  3414. {
  3415. png_uint_16 v;
  3416. v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  3417. *sp = (png_byte)((v >> 8) & 0xff);
  3418. *(sp + 1) = (png_byte)(v & 0xff);
  3419. v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
  3420. *(sp + 2) = (png_byte)((v >> 8) & 0xff);
  3421. *(sp + 3) = (png_byte)(v & 0xff);
  3422. v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
  3423. *(sp + 4) = (png_byte)((v >> 8) & 0xff);
  3424. *(sp + 5) = (png_byte)(v & 0xff);
  3425. }
  3426. else if (a == 0)
  3427. {
  3428. /* Background is already in screen gamma */
  3429. *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
  3430. *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
  3431. *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
  3432. & 0xff);
  3433. *(sp + 3) = (png_byte)(png_ptr->background.green
  3434. & 0xff);
  3435. *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
  3436. & 0xff);
  3437. *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
  3438. }
  3439. else
  3440. {
  3441. png_uint_16 v, w;
  3442. v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
  3443. png_composite_16(w, v, a, png_ptr->background_1.red);
  3444. if (optimize == 0)
  3445. w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >>
  3446. 8];
  3447. *sp = (png_byte)((w >> 8) & 0xff);
  3448. *(sp + 1) = (png_byte)(w & 0xff);
  3449. v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
  3450. png_composite_16(w, v, a, png_ptr->background_1.green);
  3451. if (optimize == 0)
  3452. w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >>
  3453. 8];
  3454. *(sp + 2) = (png_byte)((w >> 8) & 0xff);
  3455. *(sp + 3) = (png_byte)(w & 0xff);
  3456. v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
  3457. png_composite_16(w, v, a, png_ptr->background_1.blue);
  3458. if (optimize == 0)
  3459. w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >>
  3460. 8];
  3461. *(sp + 4) = (png_byte)((w >> 8) & 0xff);
  3462. *(sp + 5) = (png_byte)(w & 0xff);
  3463. }
  3464. }
  3465. }
  3466. else
  3467. #endif
  3468. {
  3469. sp = row;
  3470. for (i = 0; i < row_width; i++, sp += 8)
  3471. {
  3472. png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
  3473. << 8) + (png_uint_16)(*(sp + 7)));
  3474. if (a == 0)
  3475. {
  3476. *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
  3477. *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
  3478. *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
  3479. & 0xff);
  3480. *(sp + 3) = (png_byte)(png_ptr->background.green
  3481. & 0xff);
  3482. *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
  3483. & 0xff);
  3484. *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
  3485. }
  3486. else if (a < 0xffff)
  3487. {
  3488. png_uint_16 v;
  3489. png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  3490. png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
  3491. + *(sp + 3));
  3492. png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
  3493. + *(sp + 5));
  3494. png_composite_16(v, r, a, png_ptr->background.red);
  3495. *sp = (png_byte)((v >> 8) & 0xff);
  3496. *(sp + 1) = (png_byte)(v & 0xff);
  3497. png_composite_16(v, g, a, png_ptr->background.green);
  3498. *(sp + 2) = (png_byte)((v >> 8) & 0xff);
  3499. *(sp + 3) = (png_byte)(v & 0xff);
  3500. png_composite_16(v, b, a, png_ptr->background.blue);
  3501. *(sp + 4) = (png_byte)((v >> 8) & 0xff);
  3502. *(sp + 5) = (png_byte)(v & 0xff);
  3503. }
  3504. }
  3505. }
  3506. }
  3507. break;
  3508. }
  3509. default:
  3510. break;
  3511. }
  3512. }
  3513. #endif /* READ_BACKGROUND || READ_ALPHA_MODE */
  3514. #ifdef PNG_READ_GAMMA_SUPPORTED
  3515. /* Gamma correct the image, avoiding the alpha channel. Make sure
  3516. * you do this after you deal with the transparency issue on grayscale
  3517. * or RGB images. If your bit depth is 8, use gamma_table, if it
  3518. * is 16, use gamma_16_table and gamma_shift. Build these with
  3519. * build_gamma_table().
  3520. */
  3521. static void
  3522. png_do_gamma(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
  3523. {
  3524. png_const_bytep gamma_table = png_ptr->gamma_table;
  3525. png_const_uint_16pp gamma_16_table = png_ptr->gamma_16_table;
  3526. int gamma_shift = png_ptr->gamma_shift;
  3527. png_bytep sp;
  3528. png_uint_32 i;
  3529. png_uint_32 row_width=row_info->width;
  3530. png_debug(1, "in png_do_gamma");
  3531. if (((row_info->bit_depth <= 8 && gamma_table != NULL) ||
  3532. (row_info->bit_depth == 16 && gamma_16_table != NULL)))
  3533. {
  3534. switch (row_info->color_type)
  3535. {
  3536. case PNG_COLOR_TYPE_RGB:
  3537. {
  3538. if (row_info->bit_depth == 8)
  3539. {
  3540. sp = row;
  3541. for (i = 0; i < row_width; i++)
  3542. {
  3543. *sp = gamma_table[*sp];
  3544. sp++;
  3545. *sp = gamma_table[*sp];
  3546. sp++;
  3547. *sp = gamma_table[*sp];
  3548. sp++;
  3549. }
  3550. }
  3551. else /* if (row_info->bit_depth == 16) */
  3552. {
  3553. sp = row;
  3554. for (i = 0; i < row_width; i++)
  3555. {
  3556. png_uint_16 v;
  3557. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3558. *sp = (png_byte)((v >> 8) & 0xff);
  3559. *(sp + 1) = (png_byte)(v & 0xff);
  3560. sp += 2;
  3561. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3562. *sp = (png_byte)((v >> 8) & 0xff);
  3563. *(sp + 1) = (png_byte)(v & 0xff);
  3564. sp += 2;
  3565. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3566. *sp = (png_byte)((v >> 8) & 0xff);
  3567. *(sp + 1) = (png_byte)(v & 0xff);
  3568. sp += 2;
  3569. }
  3570. }
  3571. break;
  3572. }
  3573. case PNG_COLOR_TYPE_RGB_ALPHA:
  3574. {
  3575. if (row_info->bit_depth == 8)
  3576. {
  3577. sp = row;
  3578. for (i = 0; i < row_width; i++)
  3579. {
  3580. *sp = gamma_table[*sp];
  3581. sp++;
  3582. *sp = gamma_table[*sp];
  3583. sp++;
  3584. *sp = gamma_table[*sp];
  3585. sp++;
  3586. sp++;
  3587. }
  3588. }
  3589. else /* if (row_info->bit_depth == 16) */
  3590. {
  3591. sp = row;
  3592. for (i = 0; i < row_width; i++)
  3593. {
  3594. png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3595. *sp = (png_byte)((v >> 8) & 0xff);
  3596. *(sp + 1) = (png_byte)(v & 0xff);
  3597. sp += 2;
  3598. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3599. *sp = (png_byte)((v >> 8) & 0xff);
  3600. *(sp + 1) = (png_byte)(v & 0xff);
  3601. sp += 2;
  3602. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3603. *sp = (png_byte)((v >> 8) & 0xff);
  3604. *(sp + 1) = (png_byte)(v & 0xff);
  3605. sp += 4;
  3606. }
  3607. }
  3608. break;
  3609. }
  3610. case PNG_COLOR_TYPE_GRAY_ALPHA:
  3611. {
  3612. if (row_info->bit_depth == 8)
  3613. {
  3614. sp = row;
  3615. for (i = 0; i < row_width; i++)
  3616. {
  3617. *sp = gamma_table[*sp];
  3618. sp += 2;
  3619. }
  3620. }
  3621. else /* if (row_info->bit_depth == 16) */
  3622. {
  3623. sp = row;
  3624. for (i = 0; i < row_width; i++)
  3625. {
  3626. png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3627. *sp = (png_byte)((v >> 8) & 0xff);
  3628. *(sp + 1) = (png_byte)(v & 0xff);
  3629. sp += 4;
  3630. }
  3631. }
  3632. break;
  3633. }
  3634. case PNG_COLOR_TYPE_GRAY:
  3635. {
  3636. if (row_info->bit_depth == 2)
  3637. {
  3638. sp = row;
  3639. for (i = 0; i < row_width; i += 4)
  3640. {
  3641. int a = *sp & 0xc0;
  3642. int b = *sp & 0x30;
  3643. int c = *sp & 0x0c;
  3644. int d = *sp & 0x03;
  3645. *sp = (png_byte)(
  3646. ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)|
  3647. ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
  3648. ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
  3649. ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
  3650. sp++;
  3651. }
  3652. }
  3653. if (row_info->bit_depth == 4)
  3654. {
  3655. sp = row;
  3656. for (i = 0; i < row_width; i += 2)
  3657. {
  3658. int msb = *sp & 0xf0;
  3659. int lsb = *sp & 0x0f;
  3660. *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
  3661. | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
  3662. sp++;
  3663. }
  3664. }
  3665. else if (row_info->bit_depth == 8)
  3666. {
  3667. sp = row;
  3668. for (i = 0; i < row_width; i++)
  3669. {
  3670. *sp = gamma_table[*sp];
  3671. sp++;
  3672. }
  3673. }
  3674. else if (row_info->bit_depth == 16)
  3675. {
  3676. sp = row;
  3677. for (i = 0; i < row_width; i++)
  3678. {
  3679. png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3680. *sp = (png_byte)((v >> 8) & 0xff);
  3681. *(sp + 1) = (png_byte)(v & 0xff);
  3682. sp += 2;
  3683. }
  3684. }
  3685. break;
  3686. }
  3687. default:
  3688. break;
  3689. }
  3690. }
  3691. }
  3692. #endif
  3693. #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
  3694. /* Encode the alpha channel to the output gamma (the input channel is always
  3695. * linear.) Called only with color types that have an alpha channel. Needs the
  3696. * from_1 tables.
  3697. */
  3698. static void
  3699. png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
  3700. {
  3701. png_uint_32 row_width = row_info->width;
  3702. png_debug(1, "in png_do_encode_alpha");
  3703. if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
  3704. {
  3705. if (row_info->bit_depth == 8)
  3706. {
  3707. png_bytep table = png_ptr->gamma_from_1;
  3708. if (table != NULL)
  3709. {
  3710. int step = (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 4 : 2;
  3711. /* The alpha channel is the last component: */
  3712. row += step - 1;
  3713. for (; row_width > 0; --row_width, row += step)
  3714. *row = table[*row];
  3715. return;
  3716. }
  3717. }
  3718. else if (row_info->bit_depth == 16)
  3719. {
  3720. png_uint_16pp table = png_ptr->gamma_16_from_1;
  3721. int gamma_shift = png_ptr->gamma_shift;
  3722. if (table != NULL)
  3723. {
  3724. int step = (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 8 : 4;
  3725. /* The alpha channel is the last component: */
  3726. row += step - 2;
  3727. for (; row_width > 0; --row_width, row += step)
  3728. {
  3729. png_uint_16 v;
  3730. v = table[*(row + 1) >> gamma_shift][*row];
  3731. *row = (png_byte)((v >> 8) & 0xff);
  3732. *(row + 1) = (png_byte)(v & 0xff);
  3733. }
  3734. return;
  3735. }
  3736. }
  3737. }
  3738. /* Only get to here if called with a weird row_info; no harm has been done,
  3739. * so just issue a warning.
  3740. */
  3741. png_warning(png_ptr, "png_do_encode_alpha: unexpected call");
  3742. }
  3743. #endif
  3744. #ifdef PNG_READ_EXPAND_SUPPORTED
  3745. /* Expands a palette row to an RGB or RGBA row depending
  3746. * upon whether you supply trans and num_trans.
  3747. */
  3748. static void
  3749. png_do_expand_palette(png_structrp png_ptr, png_row_infop row_info,
  3750. png_bytep row, png_const_colorp palette, png_const_bytep trans_alpha,
  3751. int num_trans)
  3752. {
  3753. int shift, value;
  3754. png_bytep sp, dp;
  3755. png_uint_32 i;
  3756. png_uint_32 row_width=row_info->width;
  3757. png_debug(1, "in png_do_expand_palette");
  3758. if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
  3759. {
  3760. if (row_info->bit_depth < 8)
  3761. {
  3762. switch (row_info->bit_depth)
  3763. {
  3764. case 1:
  3765. {
  3766. sp = row + (size_t)((row_width - 1) >> 3);
  3767. dp = row + (size_t)row_width - 1;
  3768. shift = 7 - (int)((row_width + 7) & 0x07);
  3769. for (i = 0; i < row_width; i++)
  3770. {
  3771. if ((*sp >> shift) & 0x01)
  3772. *dp = 1;
  3773. else
  3774. *dp = 0;
  3775. if (shift == 7)
  3776. {
  3777. shift = 0;
  3778. sp--;
  3779. }
  3780. else
  3781. shift++;
  3782. dp--;
  3783. }
  3784. break;
  3785. }
  3786. case 2:
  3787. {
  3788. sp = row + (size_t)((row_width - 1) >> 2);
  3789. dp = row + (size_t)row_width - 1;
  3790. shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
  3791. for (i = 0; i < row_width; i++)
  3792. {
  3793. value = (*sp >> shift) & 0x03;
  3794. *dp = (png_byte)value;
  3795. if (shift == 6)
  3796. {
  3797. shift = 0;
  3798. sp--;
  3799. }
  3800. else
  3801. shift += 2;
  3802. dp--;
  3803. }
  3804. break;
  3805. }
  3806. case 4:
  3807. {
  3808. sp = row + (size_t)((row_width - 1) >> 1);
  3809. dp = row + (size_t)row_width - 1;
  3810. shift = (int)((row_width & 0x01) << 2);
  3811. for (i = 0; i < row_width; i++)
  3812. {
  3813. value = (*sp >> shift) & 0x0f;
  3814. *dp = (png_byte)value;
  3815. if (shift == 4)
  3816. {
  3817. shift = 0;
  3818. sp--;
  3819. }
  3820. else
  3821. shift += 4;
  3822. dp--;
  3823. }
  3824. break;
  3825. }
  3826. default:
  3827. break;
  3828. }
  3829. row_info->bit_depth = 8;
  3830. row_info->pixel_depth = 8;
  3831. row_info->rowbytes = row_width;
  3832. }
  3833. if (row_info->bit_depth == 8)
  3834. {
  3835. {
  3836. if (num_trans > 0)
  3837. {
  3838. sp = row + (size_t)row_width - 1;
  3839. dp = row + ((size_t)row_width << 2) - 1;
  3840. i = 0;
  3841. #ifdef PNG_ARM_NEON_INTRINSICS_AVAILABLE
  3842. if (png_ptr->riffled_palette != NULL)
  3843. {
  3844. /* The RGBA optimization works with png_ptr->bit_depth == 8
  3845. * but sometimes row_info->bit_depth has been changed to 8.
  3846. * In these cases, the palette hasn't been riffled.
  3847. */
  3848. i = png_do_expand_palette_rgba8_neon(png_ptr, row_info, row,
  3849. &sp, &dp);
  3850. }
  3851. #else
  3852. PNG_UNUSED(png_ptr)
  3853. #endif
  3854. for (; i < row_width; i++)
  3855. {
  3856. if ((int)(*sp) >= num_trans)
  3857. *dp-- = 0xff;
  3858. else
  3859. *dp-- = trans_alpha[*sp];
  3860. *dp-- = palette[*sp].blue;
  3861. *dp-- = palette[*sp].green;
  3862. *dp-- = palette[*sp].red;
  3863. sp--;
  3864. }
  3865. row_info->bit_depth = 8;
  3866. row_info->pixel_depth = 32;
  3867. row_info->rowbytes = row_width * 4;
  3868. row_info->color_type = 6;
  3869. row_info->channels = 4;
  3870. }
  3871. else
  3872. {
  3873. sp = row + (size_t)row_width - 1;
  3874. dp = row + (size_t)(row_width * 3) - 1;
  3875. i = 0;
  3876. #ifdef PNG_ARM_NEON_INTRINSICS_AVAILABLE
  3877. i = png_do_expand_palette_rgb8_neon(png_ptr, row_info, row,
  3878. &sp, &dp);
  3879. #else
  3880. PNG_UNUSED(png_ptr)
  3881. #endif
  3882. for (; i < row_width; i++)
  3883. {
  3884. *dp-- = palette[*sp].blue;
  3885. *dp-- = palette[*sp].green;
  3886. *dp-- = palette[*sp].red;
  3887. sp--;
  3888. }
  3889. row_info->bit_depth = 8;
  3890. row_info->pixel_depth = 24;
  3891. row_info->rowbytes = row_width * 3;
  3892. row_info->color_type = 2;
  3893. row_info->channels = 3;
  3894. }
  3895. }
  3896. }
  3897. }
  3898. }
  3899. /* If the bit depth < 8, it is expanded to 8. Also, if the already
  3900. * expanded transparency value is supplied, an alpha channel is built.
  3901. */
  3902. static void
  3903. png_do_expand(png_row_infop row_info, png_bytep row,
  3904. png_const_color_16p trans_color)
  3905. {
  3906. int shift, value;
  3907. png_bytep sp, dp;
  3908. png_uint_32 i;
  3909. png_uint_32 row_width=row_info->width;
  3910. png_debug(1, "in png_do_expand");
  3911. if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
  3912. {
  3913. unsigned int gray = trans_color != NULL ? trans_color->gray : 0;
  3914. if (row_info->bit_depth < 8)
  3915. {
  3916. switch (row_info->bit_depth)
  3917. {
  3918. case 1:
  3919. {
  3920. gray = (gray & 0x01) * 0xff;
  3921. sp = row + (size_t)((row_width - 1) >> 3);
  3922. dp = row + (size_t)row_width - 1;
  3923. shift = 7 - (int)((row_width + 7) & 0x07);
  3924. for (i = 0; i < row_width; i++)
  3925. {
  3926. if ((*sp >> shift) & 0x01)
  3927. *dp = 0xff;
  3928. else
  3929. *dp = 0;
  3930. if (shift == 7)
  3931. {
  3932. shift = 0;
  3933. sp--;
  3934. }
  3935. else
  3936. shift++;
  3937. dp--;
  3938. }
  3939. break;
  3940. }
  3941. case 2:
  3942. {
  3943. gray = (gray & 0x03) * 0x55;
  3944. sp = row + (size_t)((row_width - 1) >> 2);
  3945. dp = row + (size_t)row_width - 1;
  3946. shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
  3947. for (i = 0; i < row_width; i++)
  3948. {
  3949. value = (*sp >> shift) & 0x03;
  3950. *dp = (png_byte)(value | (value << 2) | (value << 4) |
  3951. (value << 6));
  3952. if (shift == 6)
  3953. {
  3954. shift = 0;
  3955. sp--;
  3956. }
  3957. else
  3958. shift += 2;
  3959. dp--;
  3960. }
  3961. break;
  3962. }
  3963. case 4:
  3964. {
  3965. gray = (gray & 0x0f) * 0x11;
  3966. sp = row + (size_t)((row_width - 1) >> 1);
  3967. dp = row + (size_t)row_width - 1;
  3968. shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
  3969. for (i = 0; i < row_width; i++)
  3970. {
  3971. value = (*sp >> shift) & 0x0f;
  3972. *dp = (png_byte)(value | (value << 4));
  3973. if (shift == 4)
  3974. {
  3975. shift = 0;
  3976. sp--;
  3977. }
  3978. else
  3979. shift = 4;
  3980. dp--;
  3981. }
  3982. break;
  3983. }
  3984. default:
  3985. break;
  3986. }
  3987. row_info->bit_depth = 8;
  3988. row_info->pixel_depth = 8;
  3989. row_info->rowbytes = row_width;
  3990. }
  3991. if (trans_color != NULL)
  3992. {
  3993. if (row_info->bit_depth == 8)
  3994. {
  3995. gray = gray & 0xff;
  3996. sp = row + (size_t)row_width - 1;
  3997. dp = row + ((size_t)row_width << 1) - 1;
  3998. for (i = 0; i < row_width; i++)
  3999. {
  4000. if ((*sp & 0xffU) == gray)
  4001. *dp-- = 0;
  4002. else
  4003. *dp-- = 0xff;
  4004. *dp-- = *sp--;
  4005. }
  4006. }
  4007. else if (row_info->bit_depth == 16)
  4008. {
  4009. unsigned int gray_high = (gray >> 8) & 0xff;
  4010. unsigned int gray_low = gray & 0xff;
  4011. sp = row + row_info->rowbytes - 1;
  4012. dp = row + (row_info->rowbytes << 1) - 1;
  4013. for (i = 0; i < row_width; i++)
  4014. {
  4015. if ((*(sp - 1) & 0xffU) == gray_high &&
  4016. (*(sp) & 0xffU) == gray_low)
  4017. {
  4018. *dp-- = 0;
  4019. *dp-- = 0;
  4020. }
  4021. else
  4022. {
  4023. *dp-- = 0xff;
  4024. *dp-- = 0xff;
  4025. }
  4026. *dp-- = *sp--;
  4027. *dp-- = *sp--;
  4028. }
  4029. }
  4030. row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
  4031. row_info->channels = 2;
  4032. row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
  4033. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
  4034. row_width);
  4035. }
  4036. }
  4037. else if (row_info->color_type == PNG_COLOR_TYPE_RGB &&
  4038. trans_color != NULL)
  4039. {
  4040. if (row_info->bit_depth == 8)
  4041. {
  4042. png_byte red = (png_byte)(trans_color->red & 0xff);
  4043. png_byte green = (png_byte)(trans_color->green & 0xff);
  4044. png_byte blue = (png_byte)(trans_color->blue & 0xff);
  4045. sp = row + (size_t)row_info->rowbytes - 1;
  4046. dp = row + ((size_t)row_width << 2) - 1;
  4047. for (i = 0; i < row_width; i++)
  4048. {
  4049. if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
  4050. *dp-- = 0;
  4051. else
  4052. *dp-- = 0xff;
  4053. *dp-- = *sp--;
  4054. *dp-- = *sp--;
  4055. *dp-- = *sp--;
  4056. }
  4057. }
  4058. else if (row_info->bit_depth == 16)
  4059. {
  4060. png_byte red_high = (png_byte)((trans_color->red >> 8) & 0xff);
  4061. png_byte green_high = (png_byte)((trans_color->green >> 8) & 0xff);
  4062. png_byte blue_high = (png_byte)((trans_color->blue >> 8) & 0xff);
  4063. png_byte red_low = (png_byte)(trans_color->red & 0xff);
  4064. png_byte green_low = (png_byte)(trans_color->green & 0xff);
  4065. png_byte blue_low = (png_byte)(trans_color->blue & 0xff);
  4066. sp = row + row_info->rowbytes - 1;
  4067. dp = row + ((size_t)row_width << 3) - 1;
  4068. for (i = 0; i < row_width; i++)
  4069. {
  4070. if (*(sp - 5) == red_high &&
  4071. *(sp - 4) == red_low &&
  4072. *(sp - 3) == green_high &&
  4073. *(sp - 2) == green_low &&
  4074. *(sp - 1) == blue_high &&
  4075. *(sp ) == blue_low)
  4076. {
  4077. *dp-- = 0;
  4078. *dp-- = 0;
  4079. }
  4080. else
  4081. {
  4082. *dp-- = 0xff;
  4083. *dp-- = 0xff;
  4084. }
  4085. *dp-- = *sp--;
  4086. *dp-- = *sp--;
  4087. *dp-- = *sp--;
  4088. *dp-- = *sp--;
  4089. *dp-- = *sp--;
  4090. *dp-- = *sp--;
  4091. }
  4092. }
  4093. row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  4094. row_info->channels = 4;
  4095. row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
  4096. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  4097. }
  4098. }
  4099. #endif
  4100. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  4101. /* If the bit depth is 8 and the color type is not a palette type expand the
  4102. * whole row to 16 bits. Has no effect otherwise.
  4103. */
  4104. static void
  4105. png_do_expand_16(png_row_infop row_info, png_bytep row)
  4106. {
  4107. if (row_info->bit_depth == 8 &&
  4108. row_info->color_type != PNG_COLOR_TYPE_PALETTE)
  4109. {
  4110. /* The row have a sequence of bytes containing [0..255] and we need
  4111. * to turn it into another row containing [0..65535], to do this we
  4112. * calculate:
  4113. *
  4114. * (input / 255) * 65535
  4115. *
  4116. * Which happens to be exactly input * 257 and this can be achieved
  4117. * simply by byte replication in place (copying backwards).
  4118. */
  4119. png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */
  4120. png_byte *dp = sp + row_info->rowbytes; /* destination, end + 1 */
  4121. while (dp > sp)
  4122. {
  4123. dp[-2] = dp[-1] = *--sp; dp -= 2;
  4124. }
  4125. row_info->rowbytes *= 2;
  4126. row_info->bit_depth = 16;
  4127. row_info->pixel_depth = (png_byte)(row_info->channels * 16);
  4128. }
  4129. }
  4130. #endif
  4131. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  4132. static void
  4133. png_do_quantize(png_row_infop row_info, png_bytep row,
  4134. png_const_bytep palette_lookup, png_const_bytep quantize_lookup)
  4135. {
  4136. png_bytep sp, dp;
  4137. png_uint_32 i;
  4138. png_uint_32 row_width=row_info->width;
  4139. png_debug(1, "in png_do_quantize");
  4140. if (row_info->bit_depth == 8)
  4141. {
  4142. if (row_info->color_type == PNG_COLOR_TYPE_RGB && palette_lookup)
  4143. {
  4144. int r, g, b, p;
  4145. sp = row;
  4146. dp = row;
  4147. for (i = 0; i < row_width; i++)
  4148. {
  4149. r = *sp++;
  4150. g = *sp++;
  4151. b = *sp++;
  4152. /* This looks real messy, but the compiler will reduce
  4153. * it down to a reasonable formula. For example, with
  4154. * 5 bits per color, we get:
  4155. * p = (((r >> 3) & 0x1f) << 10) |
  4156. * (((g >> 3) & 0x1f) << 5) |
  4157. * ((b >> 3) & 0x1f);
  4158. */
  4159. p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
  4160. ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
  4161. (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
  4162. (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
  4163. ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
  4164. (PNG_QUANTIZE_BLUE_BITS)) |
  4165. ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
  4166. ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
  4167. *dp++ = palette_lookup[p];
  4168. }
  4169. row_info->color_type = PNG_COLOR_TYPE_PALETTE;
  4170. row_info->channels = 1;
  4171. row_info->pixel_depth = row_info->bit_depth;
  4172. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  4173. }
  4174. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
  4175. palette_lookup != NULL)
  4176. {
  4177. int r, g, b, p;
  4178. sp = row;
  4179. dp = row;
  4180. for (i = 0; i < row_width; i++)
  4181. {
  4182. r = *sp++;
  4183. g = *sp++;
  4184. b = *sp++;
  4185. sp++;
  4186. p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
  4187. ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
  4188. (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
  4189. (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
  4190. ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
  4191. (PNG_QUANTIZE_BLUE_BITS)) |
  4192. ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
  4193. ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
  4194. *dp++ = palette_lookup[p];
  4195. }
  4196. row_info->color_type = PNG_COLOR_TYPE_PALETTE;
  4197. row_info->channels = 1;
  4198. row_info->pixel_depth = row_info->bit_depth;
  4199. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  4200. }
  4201. else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
  4202. quantize_lookup)
  4203. {
  4204. sp = row;
  4205. for (i = 0; i < row_width; i++, sp++)
  4206. {
  4207. *sp = quantize_lookup[*sp];
  4208. }
  4209. }
  4210. }
  4211. }
  4212. #endif /* READ_QUANTIZE */
  4213. /* Transform the row. The order of transformations is significant,
  4214. * and is very touchy. If you add a transformation, take care to
  4215. * decide how it fits in with the other transformations here.
  4216. */
  4217. void /* PRIVATE */
  4218. png_do_read_transformations(png_structrp png_ptr, png_row_infop row_info)
  4219. {
  4220. png_debug(1, "in png_do_read_transformations");
  4221. if (png_ptr->row_buf == NULL)
  4222. {
  4223. /* Prior to 1.5.4 this output row/pass where the NULL pointer is, but this
  4224. * error is incredibly rare and incredibly easy to debug without this
  4225. * information.
  4226. */
  4227. png_error(png_ptr, "NULL row buffer");
  4228. }
  4229. /* The following is debugging; prior to 1.5.4 the code was never compiled in;
  4230. * in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro
  4231. * PNG_WARN_UNINITIALIZED_ROW removed. In 1.6 the new flag is set only for
  4232. * all transformations, however in practice the ROW_INIT always gets done on
  4233. * demand, if necessary.
  4234. */
  4235. if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 &&
  4236. (png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
  4237. {
  4238. /* Application has failed to call either png_read_start_image() or
  4239. * png_read_update_info() after setting transforms that expand pixels.
  4240. * This check added to libpng-1.2.19 (but not enabled until 1.5.4).
  4241. */
  4242. png_error(png_ptr, "Uninitialized row");
  4243. }
  4244. #ifdef PNG_READ_EXPAND_SUPPORTED
  4245. if ((png_ptr->transformations & PNG_EXPAND) != 0)
  4246. {
  4247. if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
  4248. {
  4249. #ifdef PNG_ARM_NEON_INTRINSICS_AVAILABLE
  4250. if ((png_ptr->num_trans > 0) && (png_ptr->bit_depth == 8))
  4251. {
  4252. if (png_ptr->riffled_palette == NULL)
  4253. {
  4254. /* Initialize the accelerated palette expansion. */
  4255. png_ptr->riffled_palette =
  4256. (png_bytep)png_malloc(png_ptr, 256 * 4);
  4257. png_riffle_palette_neon(png_ptr);
  4258. }
  4259. }
  4260. #endif
  4261. png_do_expand_palette(png_ptr, row_info, png_ptr->row_buf + 1,
  4262. png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);
  4263. }
  4264. else
  4265. {
  4266. if (png_ptr->num_trans != 0 &&
  4267. (png_ptr->transformations & PNG_EXPAND_tRNS) != 0)
  4268. png_do_expand(row_info, png_ptr->row_buf + 1,
  4269. &(png_ptr->trans_color));
  4270. else
  4271. png_do_expand(row_info, png_ptr->row_buf + 1, NULL);
  4272. }
  4273. }
  4274. #endif
  4275. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  4276. if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
  4277. (png_ptr->transformations & PNG_COMPOSE) == 0 &&
  4278. (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
  4279. row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
  4280. png_do_strip_channel(row_info, png_ptr->row_buf + 1,
  4281. 0 /* at_start == false, because SWAP_ALPHA happens later */);
  4282. #endif
  4283. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  4284. if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
  4285. {
  4286. int rgb_error =
  4287. png_do_rgb_to_gray(png_ptr, row_info,
  4288. png_ptr->row_buf + 1);
  4289. if (rgb_error != 0)
  4290. {
  4291. png_ptr->rgb_to_gray_status=1;
  4292. if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
  4293. PNG_RGB_TO_GRAY_WARN)
  4294. png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
  4295. if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
  4296. PNG_RGB_TO_GRAY_ERR)
  4297. png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
  4298. }
  4299. }
  4300. #endif
  4301. /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
  4302. *
  4303. * In most cases, the "simple transparency" should be done prior to doing
  4304. * gray-to-RGB, or you will have to test 3x as many bytes to check if a
  4305. * pixel is transparent. You would also need to make sure that the
  4306. * transparency information is upgraded to RGB.
  4307. *
  4308. * To summarize, the current flow is:
  4309. * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
  4310. * with background "in place" if transparent,
  4311. * convert to RGB if necessary
  4312. * - Gray + alpha -> composite with gray background and remove alpha bytes,
  4313. * convert to RGB if necessary
  4314. *
  4315. * To support RGB backgrounds for gray images we need:
  4316. * - Gray + simple transparency -> convert to RGB + simple transparency,
  4317. * compare 3 or 6 bytes and composite with
  4318. * background "in place" if transparent
  4319. * (3x compare/pixel compared to doing
  4320. * composite with gray bkgrnd)
  4321. * - Gray + alpha -> convert to RGB + alpha, composite with background and
  4322. * remove alpha bytes (3x float
  4323. * operations/pixel compared with composite
  4324. * on gray background)
  4325. *
  4326. * Greg's change will do this. The reason it wasn't done before is for
  4327. * performance, as this increases the per-pixel operations. If we would check
  4328. * in advance if the background was gray or RGB, and position the gray-to-RGB
  4329. * transform appropriately, then it would save a lot of work/time.
  4330. */
  4331. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  4332. /* If gray -> RGB, do so now only if background is non-gray; else do later
  4333. * for performance reasons
  4334. */
  4335. if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 &&
  4336. (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) == 0)
  4337. png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
  4338. #endif
  4339. #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
  4340. defined(PNG_READ_ALPHA_MODE_SUPPORTED)
  4341. if ((png_ptr->transformations & PNG_COMPOSE) != 0)
  4342. png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr);
  4343. #endif
  4344. #ifdef PNG_READ_GAMMA_SUPPORTED
  4345. if ((png_ptr->transformations & PNG_GAMMA) != 0 &&
  4346. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  4347. /* Because RGB_TO_GRAY does the gamma transform. */
  4348. (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0 &&
  4349. #endif
  4350. #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
  4351. defined(PNG_READ_ALPHA_MODE_SUPPORTED)
  4352. /* Because PNG_COMPOSE does the gamma transform if there is something to
  4353. * do (if there is an alpha channel or transparency.)
  4354. */
  4355. !((png_ptr->transformations & PNG_COMPOSE) != 0 &&
  4356. ((png_ptr->num_trans != 0) ||
  4357. (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)) &&
  4358. #endif
  4359. /* Because png_init_read_transformations transforms the palette, unless
  4360. * RGB_TO_GRAY will do the transform.
  4361. */
  4362. (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
  4363. png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr);
  4364. #endif
  4365. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  4366. if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
  4367. (png_ptr->transformations & PNG_COMPOSE) != 0 &&
  4368. (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
  4369. row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
  4370. png_do_strip_channel(row_info, png_ptr->row_buf + 1,
  4371. 0 /* at_start == false, because SWAP_ALPHA happens later */);
  4372. #endif
  4373. #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
  4374. if ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 &&
  4375. (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
  4376. png_do_encode_alpha(row_info, png_ptr->row_buf + 1, png_ptr);
  4377. #endif
  4378. #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  4379. if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0)
  4380. png_do_scale_16_to_8(row_info, png_ptr->row_buf + 1);
  4381. #endif
  4382. #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
  4383. /* There is no harm in doing both of these because only one has any effect,
  4384. * by putting the 'scale' option first if the app asks for scale (either by
  4385. * calling the API or in a TRANSFORM flag) this is what happens.
  4386. */
  4387. if ((png_ptr->transformations & PNG_16_TO_8) != 0)
  4388. png_do_chop(row_info, png_ptr->row_buf + 1);
  4389. #endif
  4390. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  4391. if ((png_ptr->transformations & PNG_QUANTIZE) != 0)
  4392. {
  4393. png_do_quantize(row_info, png_ptr->row_buf + 1,
  4394. png_ptr->palette_lookup, png_ptr->quantize_index);
  4395. if (row_info->rowbytes == 0)
  4396. png_error(png_ptr, "png_do_quantize returned rowbytes=0");
  4397. }
  4398. #endif /* READ_QUANTIZE */
  4399. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  4400. /* Do the expansion now, after all the arithmetic has been done. Notice
  4401. * that previous transformations can handle the PNG_EXPAND_16 flag if this
  4402. * is efficient (particularly true in the case of gamma correction, where
  4403. * better accuracy results faster!)
  4404. */
  4405. if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
  4406. png_do_expand_16(row_info, png_ptr->row_buf + 1);
  4407. #endif
  4408. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  4409. /* NOTE: moved here in 1.5.4 (from much later in this list.) */
  4410. if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 &&
  4411. (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) != 0)
  4412. png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
  4413. #endif
  4414. #ifdef PNG_READ_INVERT_SUPPORTED
  4415. if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
  4416. png_do_invert(row_info, png_ptr->row_buf + 1);
  4417. #endif
  4418. #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
  4419. if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
  4420. png_do_read_invert_alpha(row_info, png_ptr->row_buf + 1);
  4421. #endif
  4422. #ifdef PNG_READ_SHIFT_SUPPORTED
  4423. if ((png_ptr->transformations & PNG_SHIFT) != 0)
  4424. png_do_unshift(row_info, png_ptr->row_buf + 1,
  4425. &(png_ptr->shift));
  4426. #endif
  4427. #ifdef PNG_READ_PACK_SUPPORTED
  4428. if ((png_ptr->transformations & PNG_PACK) != 0)
  4429. png_do_unpack(row_info, png_ptr->row_buf + 1);
  4430. #endif
  4431. #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
  4432. /* Added at libpng-1.5.10 */
  4433. if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
  4434. png_ptr->num_palette_max >= 0)
  4435. png_do_check_palette_indexes(png_ptr, row_info);
  4436. #endif
  4437. #ifdef PNG_READ_BGR_SUPPORTED
  4438. if ((png_ptr->transformations & PNG_BGR) != 0)
  4439. png_do_bgr(row_info, png_ptr->row_buf + 1);
  4440. #endif
  4441. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  4442. if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
  4443. png_do_packswap(row_info, png_ptr->row_buf + 1);
  4444. #endif
  4445. #ifdef PNG_READ_FILLER_SUPPORTED
  4446. if ((png_ptr->transformations & PNG_FILLER) != 0)
  4447. png_do_read_filler(row_info, png_ptr->row_buf + 1,
  4448. (png_uint_32)png_ptr->filler, png_ptr->flags);
  4449. #endif
  4450. #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
  4451. if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0)
  4452. png_do_read_swap_alpha(row_info, png_ptr->row_buf + 1);
  4453. #endif
  4454. #ifdef PNG_READ_16BIT_SUPPORTED
  4455. #ifdef PNG_READ_SWAP_SUPPORTED
  4456. if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
  4457. png_do_swap(row_info, png_ptr->row_buf + 1);
  4458. #endif
  4459. #endif
  4460. #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
  4461. if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
  4462. {
  4463. if (png_ptr->read_user_transform_fn != NULL)
  4464. (*(png_ptr->read_user_transform_fn)) /* User read transform function */
  4465. (png_ptr, /* png_ptr */
  4466. row_info, /* row_info: */
  4467. /* png_uint_32 width; width of row */
  4468. /* size_t rowbytes; number of bytes in row */
  4469. /* png_byte color_type; color type of pixels */
  4470. /* png_byte bit_depth; bit depth of samples */
  4471. /* png_byte channels; number of channels (1-4) */
  4472. /* png_byte pixel_depth; bits per pixel (depth*channels) */
  4473. png_ptr->row_buf + 1); /* start of pixel data for row */
  4474. #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
  4475. if (png_ptr->user_transform_depth != 0)
  4476. row_info->bit_depth = png_ptr->user_transform_depth;
  4477. if (png_ptr->user_transform_channels != 0)
  4478. row_info->channels = png_ptr->user_transform_channels;
  4479. #endif
  4480. row_info->pixel_depth = (png_byte)(row_info->bit_depth *
  4481. row_info->channels);
  4482. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width);
  4483. }
  4484. #endif
  4485. }
  4486. #endif /* READ_TRANSFORMS */
  4487. #endif /* READ */