pngrtran.c 165 KB

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