pngrtran.c 165 KB

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