pngrtran.c 164 KB

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