pngrtran.c 163 KB

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