pngrtran.c 165 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006
  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)((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)((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)((png_uint_32)num_palette * (sizeof (png_byte))));
  496. png_ptr->palette_to_index = (png_bytep)png_malloc(png_ptr,
  497. (png_uint_32)((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 = 7U - ((row_width + 7U) & 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 = ((3U - ((row_width + 3U) & 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 = ((1U - ((row_width + 1U) & 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 |=
  2820. (unsigned int)(png_ptr->background.gray << shift);
  2821. *sp = (png_byte)(tmp & 0xff);
  2822. }
  2823. if (shift == 0)
  2824. {
  2825. shift = 7;
  2826. sp++;
  2827. }
  2828. else
  2829. shift--;
  2830. }
  2831. break;
  2832. }
  2833. case 2:
  2834. {
  2835. #ifdef PNG_READ_GAMMA_SUPPORTED
  2836. if (gamma_table != NULL)
  2837. {
  2838. sp = row;
  2839. shift = 6;
  2840. for (i = 0; i < row_width; i++)
  2841. {
  2842. if ((png_uint_16)((*sp >> shift) & 0x03)
  2843. == png_ptr->trans_color.gray)
  2844. {
  2845. unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
  2846. tmp |=
  2847. (unsigned int)png_ptr->background.gray << shift;
  2848. *sp = (png_byte)(tmp & 0xff);
  2849. }
  2850. else
  2851. {
  2852. unsigned int p = (*sp >> shift) & 0x03;
  2853. unsigned int g = (gamma_table [p | (p << 2) |
  2854. (p << 4) | (p << 6)] >> 6) & 0x03;
  2855. unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
  2856. tmp |= (unsigned int)(g << shift);
  2857. *sp = (png_byte)(tmp & 0xff);
  2858. }
  2859. if (shift == 0)
  2860. {
  2861. shift = 6;
  2862. sp++;
  2863. }
  2864. else
  2865. shift -= 2;
  2866. }
  2867. }
  2868. else
  2869. #endif
  2870. {
  2871. sp = row;
  2872. shift = 6;
  2873. for (i = 0; i < row_width; i++)
  2874. {
  2875. if ((png_uint_16)((*sp >> shift) & 0x03)
  2876. == png_ptr->trans_color.gray)
  2877. {
  2878. unsigned int tmp = *sp & (0x3f3f >> (6 - shift));
  2879. tmp |=
  2880. (unsigned int)png_ptr->background.gray << shift;
  2881. *sp = (png_byte)(tmp & 0xff);
  2882. }
  2883. if (shift == 0)
  2884. {
  2885. shift = 6;
  2886. sp++;
  2887. }
  2888. else
  2889. shift -= 2;
  2890. }
  2891. }
  2892. break;
  2893. }
  2894. case 4:
  2895. {
  2896. #ifdef PNG_READ_GAMMA_SUPPORTED
  2897. if (gamma_table != NULL)
  2898. {
  2899. sp = row;
  2900. shift = 4;
  2901. for (i = 0; i < row_width; i++)
  2902. {
  2903. if ((png_uint_16)((*sp >> shift) & 0x0f)
  2904. == png_ptr->trans_color.gray)
  2905. {
  2906. unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
  2907. tmp |=
  2908. (unsigned int)(png_ptr->background.gray << shift);
  2909. *sp = (png_byte)(tmp & 0xff);
  2910. }
  2911. else
  2912. {
  2913. unsigned int p = (*sp >> shift) & 0x0f;
  2914. unsigned int g = (gamma_table[p | (p << 4)] >> 4) &
  2915. 0x0f;
  2916. unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
  2917. tmp |= (unsigned int)(g << shift);
  2918. *sp = (png_byte)(tmp & 0xff);
  2919. }
  2920. if (shift == 0)
  2921. {
  2922. shift = 4;
  2923. sp++;
  2924. }
  2925. else
  2926. shift -= 4;
  2927. }
  2928. }
  2929. else
  2930. #endif
  2931. {
  2932. sp = row;
  2933. shift = 4;
  2934. for (i = 0; i < row_width; i++)
  2935. {
  2936. if ((png_uint_16)((*sp >> shift) & 0x0f)
  2937. == png_ptr->trans_color.gray)
  2938. {
  2939. unsigned int tmp = *sp & (0x0f0f >> (4 - shift));
  2940. tmp |=
  2941. (unsigned int)(png_ptr->background.gray << shift);
  2942. *sp = (png_byte)(tmp & 0xff);
  2943. }
  2944. if (shift == 0)
  2945. {
  2946. shift = 4;
  2947. sp++;
  2948. }
  2949. else
  2950. shift -= 4;
  2951. }
  2952. }
  2953. break;
  2954. }
  2955. case 8:
  2956. {
  2957. #ifdef PNG_READ_GAMMA_SUPPORTED
  2958. if (gamma_table != NULL)
  2959. {
  2960. sp = row;
  2961. for (i = 0; i < row_width; i++, sp++)
  2962. {
  2963. if (*sp == png_ptr->trans_color.gray)
  2964. *sp = (png_byte)png_ptr->background.gray;
  2965. else
  2966. *sp = gamma_table[*sp];
  2967. }
  2968. }
  2969. else
  2970. #endif
  2971. {
  2972. sp = row;
  2973. for (i = 0; i < row_width; i++, sp++)
  2974. {
  2975. if (*sp == png_ptr->trans_color.gray)
  2976. *sp = (png_byte)png_ptr->background.gray;
  2977. }
  2978. }
  2979. break;
  2980. }
  2981. case 16:
  2982. {
  2983. #ifdef PNG_READ_GAMMA_SUPPORTED
  2984. if (gamma_16 != NULL)
  2985. {
  2986. sp = row;
  2987. for (i = 0; i < row_width; i++, sp += 2)
  2988. {
  2989. png_uint_16 v;
  2990. v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  2991. if (v == png_ptr->trans_color.gray)
  2992. {
  2993. /* Background is already in screen gamma */
  2994. *sp = (png_byte)((png_ptr->background.gray >> 8)
  2995. & 0xff);
  2996. *(sp + 1) = (png_byte)(png_ptr->background.gray
  2997. & 0xff);
  2998. }
  2999. else
  3000. {
  3001. v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  3002. *sp = (png_byte)((v >> 8) & 0xff);
  3003. *(sp + 1) = (png_byte)(v & 0xff);
  3004. }
  3005. }
  3006. }
  3007. else
  3008. #endif
  3009. {
  3010. sp = row;
  3011. for (i = 0; i < row_width; i++, sp += 2)
  3012. {
  3013. png_uint_16 v;
  3014. v = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  3015. if (v == png_ptr->trans_color.gray)
  3016. {
  3017. *sp = (png_byte)((png_ptr->background.gray >> 8)
  3018. & 0xff);
  3019. *(sp + 1) = (png_byte)(png_ptr->background.gray
  3020. & 0xff);
  3021. }
  3022. }
  3023. }
  3024. break;
  3025. }
  3026. default:
  3027. break;
  3028. }
  3029. break;
  3030. }
  3031. case PNG_COLOR_TYPE_RGB:
  3032. {
  3033. if (row_info->bit_depth == 8)
  3034. {
  3035. #ifdef PNG_READ_GAMMA_SUPPORTED
  3036. if (gamma_table != NULL)
  3037. {
  3038. sp = row;
  3039. for (i = 0; i < row_width; i++, sp += 3)
  3040. {
  3041. if (*sp == png_ptr->trans_color.red &&
  3042. *(sp + 1) == png_ptr->trans_color.green &&
  3043. *(sp + 2) == png_ptr->trans_color.blue)
  3044. {
  3045. *sp = (png_byte)png_ptr->background.red;
  3046. *(sp + 1) = (png_byte)png_ptr->background.green;
  3047. *(sp + 2) = (png_byte)png_ptr->background.blue;
  3048. }
  3049. else
  3050. {
  3051. *sp = gamma_table[*sp];
  3052. *(sp + 1) = gamma_table[*(sp + 1)];
  3053. *(sp + 2) = gamma_table[*(sp + 2)];
  3054. }
  3055. }
  3056. }
  3057. else
  3058. #endif
  3059. {
  3060. sp = row;
  3061. for (i = 0; i < row_width; i++, sp += 3)
  3062. {
  3063. if (*sp == png_ptr->trans_color.red &&
  3064. *(sp + 1) == png_ptr->trans_color.green &&
  3065. *(sp + 2) == png_ptr->trans_color.blue)
  3066. {
  3067. *sp = (png_byte)png_ptr->background.red;
  3068. *(sp + 1) = (png_byte)png_ptr->background.green;
  3069. *(sp + 2) = (png_byte)png_ptr->background.blue;
  3070. }
  3071. }
  3072. }
  3073. }
  3074. else /* if (row_info->bit_depth == 16) */
  3075. {
  3076. #ifdef PNG_READ_GAMMA_SUPPORTED
  3077. if (gamma_16 != NULL)
  3078. {
  3079. sp = row;
  3080. for (i = 0; i < row_width; i++, sp += 6)
  3081. {
  3082. png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  3083. png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
  3084. + *(sp + 3));
  3085. png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
  3086. + *(sp + 5));
  3087. if (r == png_ptr->trans_color.red &&
  3088. g == png_ptr->trans_color.green &&
  3089. b == png_ptr->trans_color.blue)
  3090. {
  3091. /* Background is already in screen gamma */
  3092. *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
  3093. *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
  3094. *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
  3095. & 0xff);
  3096. *(sp + 3) = (png_byte)(png_ptr->background.green
  3097. & 0xff);
  3098. *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
  3099. & 0xff);
  3100. *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
  3101. }
  3102. else
  3103. {
  3104. png_uint_16 v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  3105. *sp = (png_byte)((v >> 8) & 0xff);
  3106. *(sp + 1) = (png_byte)(v & 0xff);
  3107. v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
  3108. *(sp + 2) = (png_byte)((v >> 8) & 0xff);
  3109. *(sp + 3) = (png_byte)(v & 0xff);
  3110. v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
  3111. *(sp + 4) = (png_byte)((v >> 8) & 0xff);
  3112. *(sp + 5) = (png_byte)(v & 0xff);
  3113. }
  3114. }
  3115. }
  3116. else
  3117. #endif
  3118. {
  3119. sp = row;
  3120. for (i = 0; i < row_width; i++, sp += 6)
  3121. {
  3122. png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  3123. png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
  3124. + *(sp + 3));
  3125. png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
  3126. + *(sp + 5));
  3127. if (r == png_ptr->trans_color.red &&
  3128. g == png_ptr->trans_color.green &&
  3129. b == png_ptr->trans_color.blue)
  3130. {
  3131. *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
  3132. *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
  3133. *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
  3134. & 0xff);
  3135. *(sp + 3) = (png_byte)(png_ptr->background.green
  3136. & 0xff);
  3137. *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
  3138. & 0xff);
  3139. *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
  3140. }
  3141. }
  3142. }
  3143. }
  3144. break;
  3145. }
  3146. case PNG_COLOR_TYPE_GRAY_ALPHA:
  3147. {
  3148. if (row_info->bit_depth == 8)
  3149. {
  3150. #ifdef PNG_READ_GAMMA_SUPPORTED
  3151. if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
  3152. gamma_table != NULL)
  3153. {
  3154. sp = row;
  3155. for (i = 0; i < row_width; i++, sp += 2)
  3156. {
  3157. png_uint_16 a = *(sp + 1);
  3158. if (a == 0xff)
  3159. *sp = gamma_table[*sp];
  3160. else if (a == 0)
  3161. {
  3162. /* Background is already in screen gamma */
  3163. *sp = (png_byte)png_ptr->background.gray;
  3164. }
  3165. else
  3166. {
  3167. png_byte v, w;
  3168. v = gamma_to_1[*sp];
  3169. png_composite(w, v, a, png_ptr->background_1.gray);
  3170. if (optimize == 0)
  3171. w = gamma_from_1[w];
  3172. *sp = w;
  3173. }
  3174. }
  3175. }
  3176. else
  3177. #endif
  3178. {
  3179. sp = row;
  3180. for (i = 0; i < row_width; i++, sp += 2)
  3181. {
  3182. png_byte a = *(sp + 1);
  3183. if (a == 0)
  3184. *sp = (png_byte)png_ptr->background.gray;
  3185. else if (a < 0xff)
  3186. png_composite(*sp, *sp, a, png_ptr->background.gray);
  3187. }
  3188. }
  3189. }
  3190. else /* if (png_ptr->bit_depth == 16) */
  3191. {
  3192. #ifdef PNG_READ_GAMMA_SUPPORTED
  3193. if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
  3194. gamma_16_to_1 != NULL)
  3195. {
  3196. sp = row;
  3197. for (i = 0; i < row_width; i++, sp += 4)
  3198. {
  3199. png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
  3200. + *(sp + 3));
  3201. if (a == (png_uint_16)0xffff)
  3202. {
  3203. png_uint_16 v;
  3204. v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  3205. *sp = (png_byte)((v >> 8) & 0xff);
  3206. *(sp + 1) = (png_byte)(v & 0xff);
  3207. }
  3208. else if (a == 0)
  3209. {
  3210. /* Background is already in screen gamma */
  3211. *sp = (png_byte)((png_ptr->background.gray >> 8)
  3212. & 0xff);
  3213. *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
  3214. }
  3215. else
  3216. {
  3217. png_uint_16 g, v, w;
  3218. g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
  3219. png_composite_16(v, g, a, png_ptr->background_1.gray);
  3220. if (optimize != 0)
  3221. w = v;
  3222. else
  3223. w = gamma_16_from_1[(v & 0xff) >>
  3224. gamma_shift][v >> 8];
  3225. *sp = (png_byte)((w >> 8) & 0xff);
  3226. *(sp + 1) = (png_byte)(w & 0xff);
  3227. }
  3228. }
  3229. }
  3230. else
  3231. #endif
  3232. {
  3233. sp = row;
  3234. for (i = 0; i < row_width; i++, sp += 4)
  3235. {
  3236. png_uint_16 a = (png_uint_16)(((*(sp + 2)) << 8)
  3237. + *(sp + 3));
  3238. if (a == 0)
  3239. {
  3240. *sp = (png_byte)((png_ptr->background.gray >> 8)
  3241. & 0xff);
  3242. *(sp + 1) = (png_byte)(png_ptr->background.gray & 0xff);
  3243. }
  3244. else if (a < 0xffff)
  3245. {
  3246. png_uint_16 g, v;
  3247. g = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  3248. png_composite_16(v, g, a, png_ptr->background.gray);
  3249. *sp = (png_byte)((v >> 8) & 0xff);
  3250. *(sp + 1) = (png_byte)(v & 0xff);
  3251. }
  3252. }
  3253. }
  3254. }
  3255. break;
  3256. }
  3257. case PNG_COLOR_TYPE_RGB_ALPHA:
  3258. {
  3259. if (row_info->bit_depth == 8)
  3260. {
  3261. #ifdef PNG_READ_GAMMA_SUPPORTED
  3262. if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
  3263. gamma_table != NULL)
  3264. {
  3265. sp = row;
  3266. for (i = 0; i < row_width; i++, sp += 4)
  3267. {
  3268. png_byte a = *(sp + 3);
  3269. if (a == 0xff)
  3270. {
  3271. *sp = gamma_table[*sp];
  3272. *(sp + 1) = gamma_table[*(sp + 1)];
  3273. *(sp + 2) = gamma_table[*(sp + 2)];
  3274. }
  3275. else if (a == 0)
  3276. {
  3277. /* Background is already in screen gamma */
  3278. *sp = (png_byte)png_ptr->background.red;
  3279. *(sp + 1) = (png_byte)png_ptr->background.green;
  3280. *(sp + 2) = (png_byte)png_ptr->background.blue;
  3281. }
  3282. else
  3283. {
  3284. png_byte v, w;
  3285. v = gamma_to_1[*sp];
  3286. png_composite(w, v, a, png_ptr->background_1.red);
  3287. if (optimize == 0) w = gamma_from_1[w];
  3288. *sp = w;
  3289. v = gamma_to_1[*(sp + 1)];
  3290. png_composite(w, v, a, png_ptr->background_1.green);
  3291. if (optimize == 0) w = gamma_from_1[w];
  3292. *(sp + 1) = w;
  3293. v = gamma_to_1[*(sp + 2)];
  3294. png_composite(w, v, a, png_ptr->background_1.blue);
  3295. if (optimize == 0) w = gamma_from_1[w];
  3296. *(sp + 2) = w;
  3297. }
  3298. }
  3299. }
  3300. else
  3301. #endif
  3302. {
  3303. sp = row;
  3304. for (i = 0; i < row_width; i++, sp += 4)
  3305. {
  3306. png_byte a = *(sp + 3);
  3307. if (a == 0)
  3308. {
  3309. *sp = (png_byte)png_ptr->background.red;
  3310. *(sp + 1) = (png_byte)png_ptr->background.green;
  3311. *(sp + 2) = (png_byte)png_ptr->background.blue;
  3312. }
  3313. else if (a < 0xff)
  3314. {
  3315. png_composite(*sp, *sp, a, png_ptr->background.red);
  3316. png_composite(*(sp + 1), *(sp + 1), a,
  3317. png_ptr->background.green);
  3318. png_composite(*(sp + 2), *(sp + 2), a,
  3319. png_ptr->background.blue);
  3320. }
  3321. }
  3322. }
  3323. }
  3324. else /* if (row_info->bit_depth == 16) */
  3325. {
  3326. #ifdef PNG_READ_GAMMA_SUPPORTED
  3327. if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
  3328. gamma_16_to_1 != NULL)
  3329. {
  3330. sp = row;
  3331. for (i = 0; i < row_width; i++, sp += 8)
  3332. {
  3333. png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
  3334. << 8) + (png_uint_16)(*(sp + 7)));
  3335. if (a == (png_uint_16)0xffff)
  3336. {
  3337. png_uint_16 v;
  3338. v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  3339. *sp = (png_byte)((v >> 8) & 0xff);
  3340. *(sp + 1) = (png_byte)(v & 0xff);
  3341. v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
  3342. *(sp + 2) = (png_byte)((v >> 8) & 0xff);
  3343. *(sp + 3) = (png_byte)(v & 0xff);
  3344. v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
  3345. *(sp + 4) = (png_byte)((v >> 8) & 0xff);
  3346. *(sp + 5) = (png_byte)(v & 0xff);
  3347. }
  3348. else if (a == 0)
  3349. {
  3350. /* Background is already in screen gamma */
  3351. *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
  3352. *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
  3353. *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
  3354. & 0xff);
  3355. *(sp + 3) = (png_byte)(png_ptr->background.green
  3356. & 0xff);
  3357. *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
  3358. & 0xff);
  3359. *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
  3360. }
  3361. else
  3362. {
  3363. png_uint_16 v, w;
  3364. v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
  3365. png_composite_16(w, v, a, png_ptr->background_1.red);
  3366. if (optimize == 0)
  3367. w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >>
  3368. 8];
  3369. *sp = (png_byte)((w >> 8) & 0xff);
  3370. *(sp + 1) = (png_byte)(w & 0xff);
  3371. v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
  3372. png_composite_16(w, v, a, png_ptr->background_1.green);
  3373. if (optimize == 0)
  3374. w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >>
  3375. 8];
  3376. *(sp + 2) = (png_byte)((w >> 8) & 0xff);
  3377. *(sp + 3) = (png_byte)(w & 0xff);
  3378. v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
  3379. png_composite_16(w, v, a, png_ptr->background_1.blue);
  3380. if (optimize == 0)
  3381. w = gamma_16_from_1[((w & 0xff) >> gamma_shift)][w >>
  3382. 8];
  3383. *(sp + 4) = (png_byte)((w >> 8) & 0xff);
  3384. *(sp + 5) = (png_byte)(w & 0xff);
  3385. }
  3386. }
  3387. }
  3388. else
  3389. #endif
  3390. {
  3391. sp = row;
  3392. for (i = 0; i < row_width; i++, sp += 8)
  3393. {
  3394. png_uint_16 a = (png_uint_16)(((png_uint_16)(*(sp + 6))
  3395. << 8) + (png_uint_16)(*(sp + 7)));
  3396. if (a == 0)
  3397. {
  3398. *sp = (png_byte)((png_ptr->background.red >> 8) & 0xff);
  3399. *(sp + 1) = (png_byte)(png_ptr->background.red & 0xff);
  3400. *(sp + 2) = (png_byte)((png_ptr->background.green >> 8)
  3401. & 0xff);
  3402. *(sp + 3) = (png_byte)(png_ptr->background.green
  3403. & 0xff);
  3404. *(sp + 4) = (png_byte)((png_ptr->background.blue >> 8)
  3405. & 0xff);
  3406. *(sp + 5) = (png_byte)(png_ptr->background.blue & 0xff);
  3407. }
  3408. else if (a < 0xffff)
  3409. {
  3410. png_uint_16 v;
  3411. png_uint_16 r = (png_uint_16)(((*sp) << 8) + *(sp + 1));
  3412. png_uint_16 g = (png_uint_16)(((*(sp + 2)) << 8)
  3413. + *(sp + 3));
  3414. png_uint_16 b = (png_uint_16)(((*(sp + 4)) << 8)
  3415. + *(sp + 5));
  3416. png_composite_16(v, r, a, png_ptr->background.red);
  3417. *sp = (png_byte)((v >> 8) & 0xff);
  3418. *(sp + 1) = (png_byte)(v & 0xff);
  3419. png_composite_16(v, g, a, png_ptr->background.green);
  3420. *(sp + 2) = (png_byte)((v >> 8) & 0xff);
  3421. *(sp + 3) = (png_byte)(v & 0xff);
  3422. png_composite_16(v, b, a, png_ptr->background.blue);
  3423. *(sp + 4) = (png_byte)((v >> 8) & 0xff);
  3424. *(sp + 5) = (png_byte)(v & 0xff);
  3425. }
  3426. }
  3427. }
  3428. }
  3429. break;
  3430. }
  3431. default:
  3432. break;
  3433. }
  3434. }
  3435. }
  3436. #endif /* READ_BACKGROUND || READ_ALPHA_MODE */
  3437. #ifdef PNG_READ_GAMMA_SUPPORTED
  3438. /* Gamma correct the image, avoiding the alpha channel. Make sure
  3439. * you do this after you deal with the transparency issue on grayscale
  3440. * or RGB images. If your bit depth is 8, use gamma_table, if it
  3441. * is 16, use gamma_16_table and gamma_shift. Build these with
  3442. * build_gamma_table().
  3443. */
  3444. static void
  3445. png_do_gamma(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
  3446. {
  3447. png_const_bytep gamma_table = png_ptr->gamma_table;
  3448. png_const_uint_16pp gamma_16_table = png_ptr->gamma_16_table;
  3449. int gamma_shift = png_ptr->gamma_shift;
  3450. png_bytep sp;
  3451. png_uint_32 i;
  3452. png_uint_32 row_width=row_info->width;
  3453. png_debug(1, "in png_do_gamma");
  3454. if (((row_info->bit_depth <= 8 && gamma_table != NULL) ||
  3455. (row_info->bit_depth == 16 && gamma_16_table != NULL)))
  3456. {
  3457. switch (row_info->color_type)
  3458. {
  3459. case PNG_COLOR_TYPE_RGB:
  3460. {
  3461. if (row_info->bit_depth == 8)
  3462. {
  3463. sp = row;
  3464. for (i = 0; i < row_width; i++)
  3465. {
  3466. *sp = gamma_table[*sp];
  3467. sp++;
  3468. *sp = gamma_table[*sp];
  3469. sp++;
  3470. *sp = gamma_table[*sp];
  3471. sp++;
  3472. }
  3473. }
  3474. else /* if (row_info->bit_depth == 16) */
  3475. {
  3476. sp = row;
  3477. for (i = 0; i < row_width; i++)
  3478. {
  3479. png_uint_16 v;
  3480. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3481. *sp = (png_byte)((v >> 8) & 0xff);
  3482. *(sp + 1) = (png_byte)(v & 0xff);
  3483. sp += 2;
  3484. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3485. *sp = (png_byte)((v >> 8) & 0xff);
  3486. *(sp + 1) = (png_byte)(v & 0xff);
  3487. sp += 2;
  3488. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3489. *sp = (png_byte)((v >> 8) & 0xff);
  3490. *(sp + 1) = (png_byte)(v & 0xff);
  3491. sp += 2;
  3492. }
  3493. }
  3494. break;
  3495. }
  3496. case PNG_COLOR_TYPE_RGB_ALPHA:
  3497. {
  3498. if (row_info->bit_depth == 8)
  3499. {
  3500. sp = row;
  3501. for (i = 0; i < row_width; i++)
  3502. {
  3503. *sp = gamma_table[*sp];
  3504. sp++;
  3505. *sp = gamma_table[*sp];
  3506. sp++;
  3507. *sp = gamma_table[*sp];
  3508. sp++;
  3509. sp++;
  3510. }
  3511. }
  3512. else /* if (row_info->bit_depth == 16) */
  3513. {
  3514. sp = row;
  3515. for (i = 0; i < row_width; i++)
  3516. {
  3517. png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3518. *sp = (png_byte)((v >> 8) & 0xff);
  3519. *(sp + 1) = (png_byte)(v & 0xff);
  3520. sp += 2;
  3521. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3522. *sp = (png_byte)((v >> 8) & 0xff);
  3523. *(sp + 1) = (png_byte)(v & 0xff);
  3524. sp += 2;
  3525. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3526. *sp = (png_byte)((v >> 8) & 0xff);
  3527. *(sp + 1) = (png_byte)(v & 0xff);
  3528. sp += 4;
  3529. }
  3530. }
  3531. break;
  3532. }
  3533. case PNG_COLOR_TYPE_GRAY_ALPHA:
  3534. {
  3535. if (row_info->bit_depth == 8)
  3536. {
  3537. sp = row;
  3538. for (i = 0; i < row_width; i++)
  3539. {
  3540. *sp = gamma_table[*sp];
  3541. sp += 2;
  3542. }
  3543. }
  3544. else /* if (row_info->bit_depth == 16) */
  3545. {
  3546. sp = row;
  3547. for (i = 0; i < row_width; i++)
  3548. {
  3549. png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3550. *sp = (png_byte)((v >> 8) & 0xff);
  3551. *(sp + 1) = (png_byte)(v & 0xff);
  3552. sp += 4;
  3553. }
  3554. }
  3555. break;
  3556. }
  3557. case PNG_COLOR_TYPE_GRAY:
  3558. {
  3559. if (row_info->bit_depth == 2)
  3560. {
  3561. sp = row;
  3562. for (i = 0; i < row_width; i += 4)
  3563. {
  3564. int a = *sp & 0xc0;
  3565. int b = *sp & 0x30;
  3566. int c = *sp & 0x0c;
  3567. int d = *sp & 0x03;
  3568. *sp = (png_byte)(
  3569. ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)|
  3570. ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
  3571. ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
  3572. ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) ));
  3573. sp++;
  3574. }
  3575. }
  3576. if (row_info->bit_depth == 4)
  3577. {
  3578. sp = row;
  3579. for (i = 0; i < row_width; i += 2)
  3580. {
  3581. int msb = *sp & 0xf0;
  3582. int lsb = *sp & 0x0f;
  3583. *sp = (png_byte)((((int)gamma_table[msb | (msb >> 4)]) & 0xf0)
  3584. | (((int)gamma_table[(lsb << 4) | lsb]) >> 4));
  3585. sp++;
  3586. }
  3587. }
  3588. else if (row_info->bit_depth == 8)
  3589. {
  3590. sp = row;
  3591. for (i = 0; i < row_width; i++)
  3592. {
  3593. *sp = gamma_table[*sp];
  3594. sp++;
  3595. }
  3596. }
  3597. else if (row_info->bit_depth == 16)
  3598. {
  3599. sp = row;
  3600. for (i = 0; i < row_width; i++)
  3601. {
  3602. png_uint_16 v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  3603. *sp = (png_byte)((v >> 8) & 0xff);
  3604. *(sp + 1) = (png_byte)(v & 0xff);
  3605. sp += 2;
  3606. }
  3607. }
  3608. break;
  3609. }
  3610. default:
  3611. break;
  3612. }
  3613. }
  3614. }
  3615. #endif
  3616. #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
  3617. /* Encode the alpha channel to the output gamma (the input channel is always
  3618. * linear.) Called only with color types that have an alpha channel. Needs the
  3619. * from_1 tables.
  3620. */
  3621. static void
  3622. png_do_encode_alpha(png_row_infop row_info, png_bytep row, png_structrp png_ptr)
  3623. {
  3624. png_uint_32 row_width = row_info->width;
  3625. png_debug(1, "in png_do_encode_alpha");
  3626. if ((row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
  3627. {
  3628. if (row_info->bit_depth == 8)
  3629. {
  3630. PNG_CONST png_bytep table = png_ptr->gamma_from_1;
  3631. if (table != NULL)
  3632. {
  3633. PNG_CONST int step =
  3634. (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 4 : 2;
  3635. /* The alpha channel is the last component: */
  3636. row += step - 1;
  3637. for (; row_width > 0; --row_width, row += step)
  3638. *row = table[*row];
  3639. return;
  3640. }
  3641. }
  3642. else if (row_info->bit_depth == 16)
  3643. {
  3644. PNG_CONST png_uint_16pp table = png_ptr->gamma_16_from_1;
  3645. PNG_CONST int gamma_shift = png_ptr->gamma_shift;
  3646. if (table != NULL)
  3647. {
  3648. PNG_CONST int step =
  3649. (row_info->color_type & PNG_COLOR_MASK_COLOR) ? 8 : 4;
  3650. /* The alpha channel is the last component: */
  3651. row += step - 2;
  3652. for (; row_width > 0; --row_width, row += step)
  3653. {
  3654. png_uint_16 v;
  3655. v = table[*(row + 1) >> gamma_shift][*row];
  3656. *row = (png_byte)((v >> 8) & 0xff);
  3657. *(row + 1) = (png_byte)(v & 0xff);
  3658. }
  3659. return;
  3660. }
  3661. }
  3662. }
  3663. /* Only get to here if called with a weird row_info; no harm has been done,
  3664. * so just issue a warning.
  3665. */
  3666. png_warning(png_ptr, "png_do_encode_alpha: unexpected call");
  3667. }
  3668. #endif
  3669. #ifdef PNG_READ_EXPAND_SUPPORTED
  3670. /* Expands a palette row to an RGB or RGBA row depending
  3671. * upon whether you supply trans and num_trans.
  3672. */
  3673. static void
  3674. png_do_expand_palette(png_row_infop row_info, png_bytep row,
  3675. png_const_colorp palette, png_const_bytep trans_alpha, int num_trans)
  3676. {
  3677. int shift, value;
  3678. png_bytep sp, dp;
  3679. png_uint_32 i;
  3680. png_uint_32 row_width=row_info->width;
  3681. png_debug(1, "in png_do_expand_palette");
  3682. if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
  3683. {
  3684. if (row_info->bit_depth < 8)
  3685. {
  3686. switch (row_info->bit_depth)
  3687. {
  3688. case 1:
  3689. {
  3690. sp = row + (png_size_t)((row_width - 1) >> 3);
  3691. dp = row + (png_size_t)row_width - 1;
  3692. shift = 7 - (int)((row_width + 7) & 0x07);
  3693. for (i = 0; i < row_width; i++)
  3694. {
  3695. if ((*sp >> shift) & 0x01)
  3696. *dp = 1;
  3697. else
  3698. *dp = 0;
  3699. if (shift == 7)
  3700. {
  3701. shift = 0;
  3702. sp--;
  3703. }
  3704. else
  3705. shift++;
  3706. dp--;
  3707. }
  3708. break;
  3709. }
  3710. case 2:
  3711. {
  3712. sp = row + (png_size_t)((row_width - 1) >> 2);
  3713. dp = row + (png_size_t)row_width - 1;
  3714. shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
  3715. for (i = 0; i < row_width; i++)
  3716. {
  3717. value = (*sp >> shift) & 0x03;
  3718. *dp = (png_byte)value;
  3719. if (shift == 6)
  3720. {
  3721. shift = 0;
  3722. sp--;
  3723. }
  3724. else
  3725. shift += 2;
  3726. dp--;
  3727. }
  3728. break;
  3729. }
  3730. case 4:
  3731. {
  3732. sp = row + (png_size_t)((row_width - 1) >> 1);
  3733. dp = row + (png_size_t)row_width - 1;
  3734. shift = (int)((row_width & 0x01) << 2);
  3735. for (i = 0; i < row_width; i++)
  3736. {
  3737. value = (*sp >> shift) & 0x0f;
  3738. *dp = (png_byte)value;
  3739. if (shift == 4)
  3740. {
  3741. shift = 0;
  3742. sp--;
  3743. }
  3744. else
  3745. shift += 4;
  3746. dp--;
  3747. }
  3748. break;
  3749. }
  3750. default:
  3751. break;
  3752. }
  3753. row_info->bit_depth = 8;
  3754. row_info->pixel_depth = 8;
  3755. row_info->rowbytes = row_width;
  3756. }
  3757. if (row_info->bit_depth == 8)
  3758. {
  3759. {
  3760. if (num_trans > 0)
  3761. {
  3762. sp = row + (png_size_t)row_width - 1;
  3763. dp = row + (png_size_t)(row_width << 2) - 1;
  3764. for (i = 0; i < row_width; i++)
  3765. {
  3766. if ((int)(*sp) >= num_trans)
  3767. *dp-- = 0xff;
  3768. else
  3769. *dp-- = trans_alpha[*sp];
  3770. *dp-- = palette[*sp].blue;
  3771. *dp-- = palette[*sp].green;
  3772. *dp-- = palette[*sp].red;
  3773. sp--;
  3774. }
  3775. row_info->bit_depth = 8;
  3776. row_info->pixel_depth = 32;
  3777. row_info->rowbytes = row_width * 4;
  3778. row_info->color_type = 6;
  3779. row_info->channels = 4;
  3780. }
  3781. else
  3782. {
  3783. sp = row + (png_size_t)row_width - 1;
  3784. dp = row + (png_size_t)(row_width * 3) - 1;
  3785. for (i = 0; i < row_width; i++)
  3786. {
  3787. *dp-- = palette[*sp].blue;
  3788. *dp-- = palette[*sp].green;
  3789. *dp-- = palette[*sp].red;
  3790. sp--;
  3791. }
  3792. row_info->bit_depth = 8;
  3793. row_info->pixel_depth = 24;
  3794. row_info->rowbytes = row_width * 3;
  3795. row_info->color_type = 2;
  3796. row_info->channels = 3;
  3797. }
  3798. }
  3799. }
  3800. }
  3801. }
  3802. /* If the bit depth < 8, it is expanded to 8. Also, if the already
  3803. * expanded transparency value is supplied, an alpha channel is built.
  3804. */
  3805. static void
  3806. png_do_expand(png_row_infop row_info, png_bytep row,
  3807. png_const_color_16p trans_color)
  3808. {
  3809. int shift, value;
  3810. png_bytep sp, dp;
  3811. png_uint_32 i;
  3812. png_uint_32 row_width=row_info->width;
  3813. png_debug(1, "in png_do_expand");
  3814. {
  3815. if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
  3816. {
  3817. unsigned int gray = trans_color != NULL ? trans_color->gray : 0;
  3818. if (row_info->bit_depth < 8)
  3819. {
  3820. switch (row_info->bit_depth)
  3821. {
  3822. case 1:
  3823. {
  3824. gray = (gray & 0x01) * 0xff;
  3825. sp = row + (png_size_t)((row_width - 1) >> 3);
  3826. dp = row + (png_size_t)row_width - 1;
  3827. shift = 7 - (int)((row_width + 7) & 0x07);
  3828. for (i = 0; i < row_width; i++)
  3829. {
  3830. if ((*sp >> shift) & 0x01)
  3831. *dp = 0xff;
  3832. else
  3833. *dp = 0;
  3834. if (shift == 7)
  3835. {
  3836. shift = 0;
  3837. sp--;
  3838. }
  3839. else
  3840. shift++;
  3841. dp--;
  3842. }
  3843. break;
  3844. }
  3845. case 2:
  3846. {
  3847. gray = (gray & 0x03) * 0x55;
  3848. sp = row + (png_size_t)((row_width - 1) >> 2);
  3849. dp = row + (png_size_t)row_width - 1;
  3850. shift = (int)((3 - ((row_width + 3) & 0x03)) << 1);
  3851. for (i = 0; i < row_width; i++)
  3852. {
  3853. value = (*sp >> shift) & 0x03;
  3854. *dp = (png_byte)(value | (value << 2) | (value << 4) |
  3855. (value << 6));
  3856. if (shift == 6)
  3857. {
  3858. shift = 0;
  3859. sp--;
  3860. }
  3861. else
  3862. shift += 2;
  3863. dp--;
  3864. }
  3865. break;
  3866. }
  3867. case 4:
  3868. {
  3869. gray = (gray & 0x0f) * 0x11;
  3870. sp = row + (png_size_t)((row_width - 1) >> 1);
  3871. dp = row + (png_size_t)row_width - 1;
  3872. shift = (int)((1 - ((row_width + 1) & 0x01)) << 2);
  3873. for (i = 0; i < row_width; i++)
  3874. {
  3875. value = (*sp >> shift) & 0x0f;
  3876. *dp = (png_byte)(value | (value << 4));
  3877. if (shift == 4)
  3878. {
  3879. shift = 0;
  3880. sp--;
  3881. }
  3882. else
  3883. shift = 4;
  3884. dp--;
  3885. }
  3886. break;
  3887. }
  3888. default:
  3889. break;
  3890. }
  3891. row_info->bit_depth = 8;
  3892. row_info->pixel_depth = 8;
  3893. row_info->rowbytes = row_width;
  3894. }
  3895. if (trans_color != NULL)
  3896. {
  3897. if (row_info->bit_depth == 8)
  3898. {
  3899. gray = gray & 0xff;
  3900. sp = row + (png_size_t)row_width - 1;
  3901. dp = row + (png_size_t)(row_width << 1) - 1;
  3902. for (i = 0; i < row_width; i++)
  3903. {
  3904. if ((*sp & 0xffU) == gray)
  3905. *dp-- = 0;
  3906. else
  3907. *dp-- = 0xff;
  3908. *dp-- = *sp--;
  3909. }
  3910. }
  3911. else if (row_info->bit_depth == 16)
  3912. {
  3913. unsigned int gray_high = (gray >> 8) & 0xff;
  3914. unsigned int gray_low = gray & 0xff;
  3915. sp = row + row_info->rowbytes - 1;
  3916. dp = row + (row_info->rowbytes << 1) - 1;
  3917. for (i = 0; i < row_width; i++)
  3918. {
  3919. if ((*(sp - 1) & 0xffU) == gray_high &&
  3920. (*(sp) & 0xffU) == gray_low)
  3921. {
  3922. *dp-- = 0;
  3923. *dp-- = 0;
  3924. }
  3925. else
  3926. {
  3927. *dp-- = 0xff;
  3928. *dp-- = 0xff;
  3929. }
  3930. *dp-- = *sp--;
  3931. *dp-- = *sp--;
  3932. }
  3933. }
  3934. row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
  3935. row_info->channels = 2;
  3936. row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
  3937. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth,
  3938. row_width);
  3939. }
  3940. }
  3941. else if (row_info->color_type == PNG_COLOR_TYPE_RGB &&
  3942. trans_color != NULL)
  3943. {
  3944. if (row_info->bit_depth == 8)
  3945. {
  3946. png_byte red = (png_byte)(trans_color->red & 0xff);
  3947. png_byte green = (png_byte)(trans_color->green & 0xff);
  3948. png_byte blue = (png_byte)(trans_color->blue & 0xff);
  3949. sp = row + (png_size_t)row_info->rowbytes - 1;
  3950. dp = row + (png_size_t)(row_width << 2) - 1;
  3951. for (i = 0; i < row_width; i++)
  3952. {
  3953. if (*(sp - 2) == red && *(sp - 1) == green && *(sp) == blue)
  3954. *dp-- = 0;
  3955. else
  3956. *dp-- = 0xff;
  3957. *dp-- = *sp--;
  3958. *dp-- = *sp--;
  3959. *dp-- = *sp--;
  3960. }
  3961. }
  3962. else if (row_info->bit_depth == 16)
  3963. {
  3964. png_byte red_high = (png_byte)((trans_color->red >> 8) & 0xff);
  3965. png_byte green_high = (png_byte)((trans_color->green >> 8) & 0xff);
  3966. png_byte blue_high = (png_byte)((trans_color->blue >> 8) & 0xff);
  3967. png_byte red_low = (png_byte)(trans_color->red & 0xff);
  3968. png_byte green_low = (png_byte)(trans_color->green & 0xff);
  3969. png_byte blue_low = (png_byte)(trans_color->blue & 0xff);
  3970. sp = row + row_info->rowbytes - 1;
  3971. dp = row + (png_size_t)(row_width << 3) - 1;
  3972. for (i = 0; i < row_width; i++)
  3973. {
  3974. if (*(sp - 5) == red_high &&
  3975. *(sp - 4) == red_low &&
  3976. *(sp - 3) == green_high &&
  3977. *(sp - 2) == green_low &&
  3978. *(sp - 1) == blue_high &&
  3979. *(sp ) == blue_low)
  3980. {
  3981. *dp-- = 0;
  3982. *dp-- = 0;
  3983. }
  3984. else
  3985. {
  3986. *dp-- = 0xff;
  3987. *dp-- = 0xff;
  3988. }
  3989. *dp-- = *sp--;
  3990. *dp-- = *sp--;
  3991. *dp-- = *sp--;
  3992. *dp-- = *sp--;
  3993. *dp-- = *sp--;
  3994. *dp-- = *sp--;
  3995. }
  3996. }
  3997. row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  3998. row_info->channels = 4;
  3999. row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
  4000. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  4001. }
  4002. }
  4003. }
  4004. #endif
  4005. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  4006. /* If the bit depth is 8 and the color type is not a palette type expand the
  4007. * whole row to 16 bits. Has no effect otherwise.
  4008. */
  4009. static void
  4010. png_do_expand_16(png_row_infop row_info, png_bytep row)
  4011. {
  4012. if (row_info->bit_depth == 8 &&
  4013. row_info->color_type != PNG_COLOR_TYPE_PALETTE)
  4014. {
  4015. /* The row have a sequence of bytes containing [0..255] and we need
  4016. * to turn it into another row containing [0..65535], to do this we
  4017. * calculate:
  4018. *
  4019. * (input / 255) * 65535
  4020. *
  4021. * Which happens to be exactly input * 257 and this can be achieved
  4022. * simply by byte replication in place (copying backwards).
  4023. */
  4024. png_byte *sp = row + row_info->rowbytes; /* source, last byte + 1 */
  4025. png_byte *dp = sp + row_info->rowbytes; /* destination, end + 1 */
  4026. while (dp > sp)
  4027. dp[-2] = dp[-1] = *--sp, dp -= 2;
  4028. row_info->rowbytes *= 2;
  4029. row_info->bit_depth = 16;
  4030. row_info->pixel_depth = (png_byte)(row_info->channels * 16);
  4031. }
  4032. }
  4033. #endif
  4034. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  4035. static void
  4036. png_do_quantize(png_row_infop row_info, png_bytep row,
  4037. png_const_bytep palette_lookup, png_const_bytep quantize_lookup)
  4038. {
  4039. png_bytep sp, dp;
  4040. png_uint_32 i;
  4041. png_uint_32 row_width=row_info->width;
  4042. png_debug(1, "in png_do_quantize");
  4043. if (row_info->bit_depth == 8)
  4044. {
  4045. if (row_info->color_type == PNG_COLOR_TYPE_RGB && palette_lookup)
  4046. {
  4047. int r, g, b, p;
  4048. sp = row;
  4049. dp = row;
  4050. for (i = 0; i < row_width; i++)
  4051. {
  4052. r = *sp++;
  4053. g = *sp++;
  4054. b = *sp++;
  4055. /* This looks real messy, but the compiler will reduce
  4056. * it down to a reasonable formula. For example, with
  4057. * 5 bits per color, we get:
  4058. * p = (((r >> 3) & 0x1f) << 10) |
  4059. * (((g >> 3) & 0x1f) << 5) |
  4060. * ((b >> 3) & 0x1f);
  4061. */
  4062. p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
  4063. ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
  4064. (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
  4065. (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
  4066. ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
  4067. (PNG_QUANTIZE_BLUE_BITS)) |
  4068. ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
  4069. ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
  4070. *dp++ = palette_lookup[p];
  4071. }
  4072. row_info->color_type = PNG_COLOR_TYPE_PALETTE;
  4073. row_info->channels = 1;
  4074. row_info->pixel_depth = row_info->bit_depth;
  4075. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  4076. }
  4077. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
  4078. palette_lookup != NULL)
  4079. {
  4080. int r, g, b, p;
  4081. sp = row;
  4082. dp = row;
  4083. for (i = 0; i < row_width; i++)
  4084. {
  4085. r = *sp++;
  4086. g = *sp++;
  4087. b = *sp++;
  4088. sp++;
  4089. p = (((r >> (8 - PNG_QUANTIZE_RED_BITS)) &
  4090. ((1 << PNG_QUANTIZE_RED_BITS) - 1)) <<
  4091. (PNG_QUANTIZE_GREEN_BITS + PNG_QUANTIZE_BLUE_BITS)) |
  4092. (((g >> (8 - PNG_QUANTIZE_GREEN_BITS)) &
  4093. ((1 << PNG_QUANTIZE_GREEN_BITS) - 1)) <<
  4094. (PNG_QUANTIZE_BLUE_BITS)) |
  4095. ((b >> (8 - PNG_QUANTIZE_BLUE_BITS)) &
  4096. ((1 << PNG_QUANTIZE_BLUE_BITS) - 1));
  4097. *dp++ = palette_lookup[p];
  4098. }
  4099. row_info->color_type = PNG_COLOR_TYPE_PALETTE;
  4100. row_info->channels = 1;
  4101. row_info->pixel_depth = row_info->bit_depth;
  4102. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_width);
  4103. }
  4104. else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
  4105. quantize_lookup)
  4106. {
  4107. sp = row;
  4108. for (i = 0; i < row_width; i++, sp++)
  4109. {
  4110. *sp = quantize_lookup[*sp];
  4111. }
  4112. }
  4113. }
  4114. }
  4115. #endif /* READ_QUANTIZE */
  4116. /* Transform the row. The order of transformations is significant,
  4117. * and is very touchy. If you add a transformation, take care to
  4118. * decide how it fits in with the other transformations here.
  4119. */
  4120. void /* PRIVATE */
  4121. png_do_read_transformations(png_structrp png_ptr, png_row_infop row_info)
  4122. {
  4123. png_debug(1, "in png_do_read_transformations");
  4124. if (png_ptr->row_buf == NULL)
  4125. {
  4126. /* Prior to 1.5.4 this output row/pass where the NULL pointer is, but this
  4127. * error is incredibly rare and incredibly easy to debug without this
  4128. * information.
  4129. */
  4130. png_error(png_ptr, "NULL row buffer");
  4131. }
  4132. /* The following is debugging; prior to 1.5.4 the code was never compiled in;
  4133. * in 1.5.4 PNG_FLAG_DETECT_UNINITIALIZED was added and the macro
  4134. * PNG_WARN_UNINITIALIZED_ROW removed. In 1.6 the new flag is set only for
  4135. * all transformations, however in practice the ROW_INIT always gets done on
  4136. * demand, if necessary.
  4137. */
  4138. if ((png_ptr->flags & PNG_FLAG_DETECT_UNINITIALIZED) != 0 &&
  4139. (png_ptr->flags & PNG_FLAG_ROW_INIT) == 0)
  4140. {
  4141. /* Application has failed to call either png_read_start_image() or
  4142. * png_read_update_info() after setting transforms that expand pixels.
  4143. * This check added to libpng-1.2.19 (but not enabled until 1.5.4).
  4144. */
  4145. png_error(png_ptr, "Uninitialized row");
  4146. }
  4147. #ifdef PNG_READ_EXPAND_SUPPORTED
  4148. if ((png_ptr->transformations & PNG_EXPAND) != 0)
  4149. {
  4150. if (row_info->color_type == PNG_COLOR_TYPE_PALETTE)
  4151. {
  4152. png_do_expand_palette(row_info, png_ptr->row_buf + 1,
  4153. png_ptr->palette, png_ptr->trans_alpha, png_ptr->num_trans);
  4154. }
  4155. else
  4156. {
  4157. if (png_ptr->num_trans != 0 &&
  4158. (png_ptr->transformations & PNG_EXPAND_tRNS) != 0)
  4159. png_do_expand(row_info, png_ptr->row_buf + 1,
  4160. &(png_ptr->trans_color));
  4161. else
  4162. png_do_expand(row_info, png_ptr->row_buf + 1,
  4163. NULL);
  4164. }
  4165. }
  4166. #endif
  4167. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  4168. if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
  4169. (png_ptr->transformations & PNG_COMPOSE) == 0 &&
  4170. (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
  4171. row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
  4172. png_do_strip_channel(row_info, png_ptr->row_buf + 1,
  4173. 0 /* at_start == false, because SWAP_ALPHA happens later */);
  4174. #endif
  4175. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  4176. if ((png_ptr->transformations & PNG_RGB_TO_GRAY) != 0)
  4177. {
  4178. int rgb_error =
  4179. png_do_rgb_to_gray(png_ptr, row_info,
  4180. png_ptr->row_buf + 1);
  4181. if (rgb_error != 0)
  4182. {
  4183. png_ptr->rgb_to_gray_status=1;
  4184. if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
  4185. PNG_RGB_TO_GRAY_WARN)
  4186. png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
  4187. if ((png_ptr->transformations & PNG_RGB_TO_GRAY) ==
  4188. PNG_RGB_TO_GRAY_ERR)
  4189. png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
  4190. }
  4191. }
  4192. #endif
  4193. /* From Andreas Dilger e-mail to png-implement, 26 March 1998:
  4194. *
  4195. * In most cases, the "simple transparency" should be done prior to doing
  4196. * gray-to-RGB, or you will have to test 3x as many bytes to check if a
  4197. * pixel is transparent. You would also need to make sure that the
  4198. * transparency information is upgraded to RGB.
  4199. *
  4200. * To summarize, the current flow is:
  4201. * - Gray + simple transparency -> compare 1 or 2 gray bytes and composite
  4202. * with background "in place" if transparent,
  4203. * convert to RGB if necessary
  4204. * - Gray + alpha -> composite with gray background and remove alpha bytes,
  4205. * convert to RGB if necessary
  4206. *
  4207. * To support RGB backgrounds for gray images we need:
  4208. * - Gray + simple transparency -> convert to RGB + simple transparency,
  4209. * compare 3 or 6 bytes and composite with
  4210. * background "in place" if transparent
  4211. * (3x compare/pixel compared to doing
  4212. * composite with gray bkgrnd)
  4213. * - Gray + alpha -> convert to RGB + alpha, composite with background and
  4214. * remove alpha bytes (3x float
  4215. * operations/pixel compared with composite
  4216. * on gray background)
  4217. *
  4218. * Greg's change will do this. The reason it wasn't done before is for
  4219. * performance, as this increases the per-pixel operations. If we would check
  4220. * in advance if the background was gray or RGB, and position the gray-to-RGB
  4221. * transform appropriately, then it would save a lot of work/time.
  4222. */
  4223. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  4224. /* If gray -> RGB, do so now only if background is non-gray; else do later
  4225. * for performance reasons
  4226. */
  4227. if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 &&
  4228. (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) == 0)
  4229. png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
  4230. #endif
  4231. #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
  4232. defined(PNG_READ_ALPHA_MODE_SUPPORTED)
  4233. if ((png_ptr->transformations & PNG_COMPOSE) != 0)
  4234. png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr);
  4235. #endif
  4236. #ifdef PNG_READ_GAMMA_SUPPORTED
  4237. if ((png_ptr->transformations & PNG_GAMMA) != 0 &&
  4238. #ifdef PNG_READ_RGB_TO_GRAY_SUPPORTED
  4239. /* Because RGB_TO_GRAY does the gamma transform. */
  4240. (png_ptr->transformations & PNG_RGB_TO_GRAY) == 0 &&
  4241. #endif
  4242. #if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
  4243. defined(PNG_READ_ALPHA_MODE_SUPPORTED)
  4244. /* Because PNG_COMPOSE does the gamma transform if there is something to
  4245. * do (if there is an alpha channel or transparency.)
  4246. */
  4247. !((png_ptr->transformations & PNG_COMPOSE) != 0 &&
  4248. ((png_ptr->num_trans != 0) ||
  4249. (png_ptr->color_type & PNG_COLOR_MASK_ALPHA) != 0)) &&
  4250. #endif
  4251. /* Because png_init_read_transformations transforms the palette, unless
  4252. * RGB_TO_GRAY will do the transform.
  4253. */
  4254. (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
  4255. png_do_gamma(row_info, png_ptr->row_buf + 1, png_ptr);
  4256. #endif
  4257. #ifdef PNG_READ_STRIP_ALPHA_SUPPORTED
  4258. if ((png_ptr->transformations & PNG_STRIP_ALPHA) != 0 &&
  4259. (png_ptr->transformations & PNG_COMPOSE) != 0 &&
  4260. (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA ||
  4261. row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA))
  4262. png_do_strip_channel(row_info, png_ptr->row_buf + 1,
  4263. 0 /* at_start == false, because SWAP_ALPHA happens later */);
  4264. #endif
  4265. #ifdef PNG_READ_ALPHA_MODE_SUPPORTED
  4266. if ((png_ptr->transformations & PNG_ENCODE_ALPHA) != 0 &&
  4267. (row_info->color_type & PNG_COLOR_MASK_ALPHA) != 0)
  4268. png_do_encode_alpha(row_info, png_ptr->row_buf + 1, png_ptr);
  4269. #endif
  4270. #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED
  4271. if ((png_ptr->transformations & PNG_SCALE_16_TO_8) != 0)
  4272. png_do_scale_16_to_8(row_info, png_ptr->row_buf + 1);
  4273. #endif
  4274. #ifdef PNG_READ_STRIP_16_TO_8_SUPPORTED
  4275. /* There is no harm in doing both of these because only one has any effect,
  4276. * by putting the 'scale' option first if the app asks for scale (either by
  4277. * calling the API or in a TRANSFORM flag) this is what happens.
  4278. */
  4279. if ((png_ptr->transformations & PNG_16_TO_8) != 0)
  4280. png_do_chop(row_info, png_ptr->row_buf + 1);
  4281. #endif
  4282. #ifdef PNG_READ_QUANTIZE_SUPPORTED
  4283. if ((png_ptr->transformations & PNG_QUANTIZE) != 0)
  4284. {
  4285. png_do_quantize(row_info, png_ptr->row_buf + 1,
  4286. png_ptr->palette_lookup, png_ptr->quantize_index);
  4287. if (row_info->rowbytes == 0)
  4288. png_error(png_ptr, "png_do_quantize returned rowbytes=0");
  4289. }
  4290. #endif /* READ_QUANTIZE */
  4291. #ifdef PNG_READ_EXPAND_16_SUPPORTED
  4292. /* Do the expansion now, after all the arithmetic has been done. Notice
  4293. * that previous transformations can handle the PNG_EXPAND_16 flag if this
  4294. * is efficient (particularly true in the case of gamma correction, where
  4295. * better accuracy results faster!)
  4296. */
  4297. if ((png_ptr->transformations & PNG_EXPAND_16) != 0)
  4298. png_do_expand_16(row_info, png_ptr->row_buf + 1);
  4299. #endif
  4300. #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
  4301. /* NOTE: moved here in 1.5.4 (from much later in this list.) */
  4302. if ((png_ptr->transformations & PNG_GRAY_TO_RGB) != 0 &&
  4303. (png_ptr->mode & PNG_BACKGROUND_IS_GRAY) != 0)
  4304. png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
  4305. #endif
  4306. #ifdef PNG_READ_INVERT_SUPPORTED
  4307. if ((png_ptr->transformations & PNG_INVERT_MONO) != 0)
  4308. png_do_invert(row_info, png_ptr->row_buf + 1);
  4309. #endif
  4310. #ifdef PNG_READ_INVERT_ALPHA_SUPPORTED
  4311. if ((png_ptr->transformations & PNG_INVERT_ALPHA) != 0)
  4312. png_do_read_invert_alpha(row_info, png_ptr->row_buf + 1);
  4313. #endif
  4314. #ifdef PNG_READ_SHIFT_SUPPORTED
  4315. if ((png_ptr->transformations & PNG_SHIFT) != 0)
  4316. png_do_unshift(row_info, png_ptr->row_buf + 1,
  4317. &(png_ptr->shift));
  4318. #endif
  4319. #ifdef PNG_READ_PACK_SUPPORTED
  4320. if ((png_ptr->transformations & PNG_PACK) != 0)
  4321. png_do_unpack(row_info, png_ptr->row_buf + 1);
  4322. #endif
  4323. #ifdef PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
  4324. /* Added at libpng-1.5.10 */
  4325. if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
  4326. png_ptr->num_palette_max >= 0)
  4327. png_do_check_palette_indexes(png_ptr, row_info);
  4328. #endif
  4329. #ifdef PNG_READ_BGR_SUPPORTED
  4330. if ((png_ptr->transformations & PNG_BGR) != 0)
  4331. png_do_bgr(row_info, png_ptr->row_buf + 1);
  4332. #endif
  4333. #ifdef PNG_READ_PACKSWAP_SUPPORTED
  4334. if ((png_ptr->transformations & PNG_PACKSWAP) != 0)
  4335. png_do_packswap(row_info, png_ptr->row_buf + 1);
  4336. #endif
  4337. #ifdef PNG_READ_FILLER_SUPPORTED
  4338. if ((png_ptr->transformations & PNG_FILLER) != 0)
  4339. png_do_read_filler(row_info, png_ptr->row_buf + 1,
  4340. (png_uint_32)png_ptr->filler, png_ptr->flags);
  4341. #endif
  4342. #ifdef PNG_READ_SWAP_ALPHA_SUPPORTED
  4343. if ((png_ptr->transformations & PNG_SWAP_ALPHA) != 0)
  4344. png_do_read_swap_alpha(row_info, png_ptr->row_buf + 1);
  4345. #endif
  4346. #ifdef PNG_READ_16BIT_SUPPORTED
  4347. #ifdef PNG_READ_SWAP_SUPPORTED
  4348. if ((png_ptr->transformations & PNG_SWAP_BYTES) != 0)
  4349. png_do_swap(row_info, png_ptr->row_buf + 1);
  4350. #endif
  4351. #endif
  4352. #ifdef PNG_READ_USER_TRANSFORM_SUPPORTED
  4353. if ((png_ptr->transformations & PNG_USER_TRANSFORM) != 0)
  4354. {
  4355. if (png_ptr->read_user_transform_fn != NULL)
  4356. (*(png_ptr->read_user_transform_fn)) /* User read transform function */
  4357. (png_ptr, /* png_ptr */
  4358. row_info, /* row_info: */
  4359. /* png_uint_32 width; width of row */
  4360. /* png_size_t rowbytes; number of bytes in row */
  4361. /* png_byte color_type; color type of pixels */
  4362. /* png_byte bit_depth; bit depth of samples */
  4363. /* png_byte channels; number of channels (1-4) */
  4364. /* png_byte pixel_depth; bits per pixel (depth*channels) */
  4365. png_ptr->row_buf + 1); /* start of pixel data for row */
  4366. #ifdef PNG_USER_TRANSFORM_PTR_SUPPORTED
  4367. if (png_ptr->user_transform_depth != 0)
  4368. row_info->bit_depth = png_ptr->user_transform_depth;
  4369. if (png_ptr->user_transform_channels != 0)
  4370. row_info->channels = png_ptr->user_transform_channels;
  4371. #endif
  4372. row_info->pixel_depth = (png_byte)(row_info->bit_depth *
  4373. row_info->channels);
  4374. row_info->rowbytes = PNG_ROWBYTES(row_info->pixel_depth, row_info->width);
  4375. }
  4376. #endif
  4377. }
  4378. #endif /* READ_TRANSFORMS */
  4379. #endif /* READ */