pngrtran.c 165 KB

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