pngrtran.c 166 KB

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