Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pngrtran.c 172KB

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