pngrtran.c 142 KB

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