pngrtran.c 164 KB

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