pngrtran.c 141 KB

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