pngrtran.c 149 KB

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