pngrtran.c 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386
  1. /* pngrtran.c - transforms the data in a row for PNG readers
  2. *
  3. * libpng 0.99b
  4. * For conditions of distribution and use, see copyright notice in png.h
  5. * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  6. * Copyright (c) 1996, 1997 Andreas Dilger
  7. * Copyright (c) 1998, Glenn Randers-Pehrson
  8. * February 3, 1998
  9. *
  10. * This file contains functions optionally called by an application
  11. * in order to tell libpng how to handle data when reading a PNG.
  12. * Transformations which are used in both reading and writing are
  13. * in pngtrans.c.
  14. */
  15. #define PNG_INTERNAL
  16. #include "png.h"
  17. #ifdef PNG_READ_COMPOSITE_NODIV_SUPPORTED
  18. /* With these routines, we avoid an integer divide, which will be slower on
  19. * many machines. However, it does take more operations than the corresponding
  20. * divide method, so it may be slower on some RISC systems. There are two
  21. * shifts (by 8 or 16 bits) and an addition, versus a single integer divide.
  22. *
  23. * Note that the rounding factors are NOT supposed to be the same! 128 and
  24. * 32768 are correct for the NODIV code; 127 and 32767 are correct for the
  25. * standard method.
  26. *
  27. * [Optimized code by Greg Roelofs and Mark Adler...blame us for bugs. :-) ]
  28. */
  29. /* fg and bg should be in `gamma 1.0' space; alpha is the opacity */
  30. # define png_composite(composite, fg, alpha, bg) \
  31. { png_uint_16 temp = ((png_uint_16)(fg) * (png_uint_16)(alpha) + \
  32. (png_uint_16)(bg)*(png_uint_16)(255 - \
  33. (png_uint_16)(alpha)) + (png_uint_16)128); \
  34. (composite) = (png_byte)((temp + (temp >> 8)) >> 8); }
  35. # define png_composite_16(composite, fg, alpha, bg) \
  36. { png_uint_32 temp = ((png_uint_32)(fg) * (png_uint_32)(alpha) + \
  37. (png_uint_32)(bg)*(png_uint_32)(65535L - \
  38. (png_uint_32)(alpha)) + (png_uint_32)32768L); \
  39. (composite) = (png_uint_16)((temp + (temp >> 16)) >> 16); }
  40. #else /* standard method using integer division */
  41. /* fg and bg should be in `gamma 1.0' space; alpha is the opacity */
  42. # define png_composite(composite, fg, alpha, bg) \
  43. (composite) = (png_byte)(((png_uint_16)(fg) * (png_uint_16)(alpha) + \
  44. (png_uint_16)(bg) * (png_uint_16)(255 - (png_uint_16)(alpha)) + \
  45. (png_uint_16)127) / 255)
  46. # define png_composite_16(composite, fg, alpha, bg) \
  47. (composite) = (png_uint_16)(((png_uint_32)(fg) * (png_uint_32)(alpha) + \
  48. (png_uint_32)(bg)*(png_uint_32)(65535L - (png_uint_32)(alpha)) + \
  49. (png_uint_32)32767) / (png_uint_32)65535L)
  50. #endif /* ?PNG_READ_COMPOSITE_NODIV_SUPPORTED */
  51. /* Set the action on getting a CRC error for an ancillary or critical chunk. */
  52. void
  53. png_set_crc_action(png_structp png_ptr, int crit_action, int ancil_action)
  54. {
  55. png_debug(1, "in png_set_crc_action\n");
  56. /* Tell libpng how we react to CRC errors in critical chunks */
  57. switch (crit_action)
  58. {
  59. case PNG_CRC_NO_CHANGE: /* leave setting as is */
  60. break;
  61. case PNG_CRC_WARN_USE: /* warn/use data */
  62. png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
  63. png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE;
  64. break;
  65. case PNG_CRC_QUIET_USE: /* quiet/use data */
  66. png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
  67. png_ptr->flags |= PNG_FLAG_CRC_CRITICAL_USE |
  68. PNG_FLAG_CRC_CRITICAL_IGNORE;
  69. break;
  70. case PNG_CRC_WARN_DISCARD: /* not a valid action for critical data */
  71. png_warning(png_ptr, "Can't discard critical data on CRC error.");
  72. case PNG_CRC_ERROR_QUIT: /* error/quit */
  73. case PNG_CRC_DEFAULT:
  74. default:
  75. png_ptr->flags &= ~PNG_FLAG_CRC_CRITICAL_MASK;
  76. break;
  77. }
  78. switch (ancil_action)
  79. {
  80. case PNG_CRC_NO_CHANGE: /* leave setting as is */
  81. break;
  82. case PNG_CRC_WARN_USE: /* warn/use data */
  83. png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
  84. png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE;
  85. break;
  86. case PNG_CRC_QUIET_USE: /* quiet/use data */
  87. png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
  88. png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_USE |
  89. PNG_FLAG_CRC_ANCILLARY_NOWARN;
  90. break;
  91. case PNG_CRC_ERROR_QUIT: /* error/quit */
  92. png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
  93. png_ptr->flags |= PNG_FLAG_CRC_ANCILLARY_NOWARN;
  94. break;
  95. case PNG_CRC_WARN_DISCARD: /* warn/discard data */
  96. case PNG_CRC_DEFAULT:
  97. default:
  98. png_ptr->flags &= ~PNG_FLAG_CRC_ANCILLARY_MASK;
  99. break;
  100. }
  101. }
  102. #if defined(PNG_READ_BACKGROUND_SUPPORTED)
  103. /* handle alpha and tRNS via a background color */
  104. void
  105. png_set_background(png_structp png_ptr,
  106. png_color_16p background_color, int background_gamma_code,
  107. int need_expand, double background_gamma)
  108. {
  109. png_debug(1, "in png_set_background\n");
  110. if (background_gamma_code == PNG_BACKGROUND_GAMMA_UNKNOWN)
  111. {
  112. png_warning(png_ptr, "Application must supply a known background gamma");
  113. return;
  114. }
  115. png_ptr->transformations |= PNG_BACKGROUND;
  116. png_memcpy(&(png_ptr->background), background_color,
  117. sizeof(png_color_16));
  118. png_ptr->background_gamma = (float)background_gamma;
  119. png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
  120. png_ptr->transformations |= (need_expand ? PNG_BACKGROUND_EXPAND : 0);
  121. }
  122. #endif
  123. #if defined(PNG_READ_16_TO_8_SUPPORTED)
  124. /* strip 16 bit depth files to 8 bit depth */
  125. void
  126. png_set_strip_16(png_structp png_ptr)
  127. {
  128. png_debug(1, "in png_set_strip_16\n");
  129. png_ptr->transformations |= PNG_16_TO_8;
  130. }
  131. #endif
  132. #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
  133. void
  134. png_set_strip_alpha(png_structp png_ptr)
  135. {
  136. png_debug(1, "in png_set_strip_alpha\n");
  137. png_ptr->transformations |= PNG_STRIP_ALPHA;
  138. }
  139. #endif
  140. #if defined(PNG_READ_DITHER_SUPPORTED)
  141. /* Dither file to 8 bit. Supply a palette, the current number
  142. * of elements in the palette, the maximum number of elements
  143. * allowed, and a histogram if possible. If the current number
  144. * of colors is greater then the maximum number, the palette will be
  145. * modified to fit in the maximum number. "full_dither" indicates
  146. * whether we need a dithering cube set up for RGB images, or if we
  147. * simply are reducing the number of colors in a paletted image.
  148. */
  149. typedef struct png_dsort_struct
  150. {
  151. struct png_dsort_struct FAR * next;
  152. png_byte left;
  153. png_byte right;
  154. } png_dsort;
  155. typedef png_dsort FAR * png_dsortp;
  156. typedef png_dsort FAR * FAR * png_dsortpp;
  157. void
  158. png_set_dither(png_structp png_ptr, png_colorp palette,
  159. int num_palette, int maximum_colors, png_uint_16p histogram,
  160. int full_dither)
  161. {
  162. png_debug(1, "in png_set_dither\n");
  163. png_ptr->transformations |= PNG_DITHER;
  164. if (!full_dither)
  165. {
  166. int i;
  167. png_ptr->dither_index = (png_bytep)png_malloc(png_ptr,
  168. (png_uint_32)(num_palette * sizeof (png_byte)));
  169. for (i = 0; i < num_palette; i++)
  170. png_ptr->dither_index[i] = (png_byte)i;
  171. }
  172. if (num_palette > maximum_colors)
  173. {
  174. if (histogram != NULL)
  175. {
  176. /* This is easy enough, just throw out the least used colors.
  177. Perhaps not the best solution, but good enough. */
  178. int i;
  179. png_bytep sort;
  180. /* initialize an array to sort colors */
  181. sort = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_palette
  182. * sizeof (png_byte)));
  183. /* initialize the sort array */
  184. for (i = 0; i < num_palette; i++)
  185. sort[i] = (png_byte)i;
  186. /* Find the least used palette entries by starting a
  187. bubble sort, and running it until we have sorted
  188. out enough colors. Note that we don't care about
  189. sorting all the colors, just finding which are
  190. least used. */
  191. for (i = num_palette - 1; i >= maximum_colors; i--)
  192. {
  193. int done; /* to stop early if the list is pre-sorted */
  194. int j;
  195. done = 1;
  196. for (j = 0; j < i; j++)
  197. {
  198. if (histogram[sort[j]] < histogram[sort[j + 1]])
  199. {
  200. png_byte t;
  201. t = sort[j];
  202. sort[j] = sort[j + 1];
  203. sort[j + 1] = t;
  204. done = 0;
  205. }
  206. }
  207. if (done)
  208. break;
  209. }
  210. /* swap the palette around, and set up a table, if necessary */
  211. if (full_dither)
  212. {
  213. int j;
  214. /* put all the useful colors within the max, but don't
  215. move the others */
  216. for (i = 0, j = num_palette; i < maximum_colors; i++)
  217. {
  218. if ((int)sort[i] >= maximum_colors)
  219. {
  220. do
  221. j--;
  222. while ((int)sort[j] >= maximum_colors);
  223. palette[i] = palette[j];
  224. }
  225. }
  226. }
  227. else
  228. {
  229. int j;
  230. /* move all the used colors inside the max limit, and
  231. develop a translation table */
  232. for (i = 0, j = num_palette; i < maximum_colors; i++)
  233. {
  234. /* only move the colors we need to */
  235. if ((int)sort[i] >= maximum_colors)
  236. {
  237. png_color tmp_color;
  238. do
  239. j--;
  240. while ((int)sort[j] >= maximum_colors);
  241. tmp_color = palette[j];
  242. palette[j] = palette[i];
  243. palette[i] = tmp_color;
  244. /* indicate where the color went */
  245. png_ptr->dither_index[j] = (png_byte)i;
  246. png_ptr->dither_index[i] = (png_byte)j;
  247. }
  248. }
  249. /* find closest color for those colors we are not using */
  250. for (i = 0; i < num_palette; i++)
  251. {
  252. if (png_ptr->dither_index[i] >= maximum_colors)
  253. {
  254. int min_d, k, min_k, d_index;
  255. /* find the closest color to one we threw out */
  256. d_index = png_ptr->dither_index[i];
  257. min_d = PNG_COLOR_DIST(palette[d_index], palette[0]);
  258. for (k = 1, min_k = 0; k < maximum_colors; k++)
  259. {
  260. int d;
  261. d = PNG_COLOR_DIST(palette[d_index], palette[k]);
  262. if (d < min_d)
  263. {
  264. min_d = d;
  265. min_k = k;
  266. }
  267. }
  268. /* point to closest color */
  269. png_ptr->dither_index[i] = (png_byte)min_k;
  270. }
  271. }
  272. }
  273. png_free(png_ptr, sort);
  274. }
  275. else
  276. {
  277. /* This is much harder to do simply (and quickly). Perhaps
  278. we need to go through a median cut routine, but those
  279. don't always behave themselves with only a few colors
  280. as input. So we will just find the closest two colors,
  281. and throw out one of them (chosen somewhat randomly).
  282. [I don't understand this at all, so if someone wants to
  283. work on improving it, be my guest - AED]
  284. */
  285. int i;
  286. int max_d;
  287. int num_new_palette;
  288. png_dsortpp hash;
  289. png_bytep index_to_palette;
  290. /* where the original index currently is in the palette */
  291. png_bytep palette_to_index;
  292. /* which original index points to this palette color */
  293. /* initialize palette index arrays */
  294. index_to_palette = (png_bytep)png_malloc(png_ptr,
  295. (png_uint_32)(num_palette * sizeof (png_byte)));
  296. palette_to_index = (png_bytep)png_malloc(png_ptr,
  297. (png_uint_32)(num_palette * sizeof (png_byte)));
  298. /* initialize the sort array */
  299. for (i = 0; i < num_palette; i++)
  300. {
  301. index_to_palette[i] = (png_byte)i;
  302. palette_to_index[i] = (png_byte)i;
  303. }
  304. hash = (png_dsortpp)png_malloc(png_ptr, (png_uint_32)(769 *
  305. sizeof (png_dsortp)));
  306. for (i = 0; i < 769; i++)
  307. hash[i] = NULL;
  308. /* png_memset(hash, 0, 769 * sizeof (png_dsortp)); */
  309. num_new_palette = num_palette;
  310. /* initial wild guess at how far apart the farthest pixel
  311. pair we will be eliminating will be. Larger
  312. numbers mean more areas will be allocated, Smaller
  313. numbers run the risk of not saving enough data, and
  314. having to do this all over again.
  315. I have not done extensive checking on this number.
  316. */
  317. max_d = 96;
  318. while (num_new_palette > maximum_colors)
  319. {
  320. for (i = 0; i < num_new_palette - 1; i++)
  321. {
  322. int j;
  323. for (j = i + 1; j < num_new_palette; j++)
  324. {
  325. int d;
  326. d = PNG_COLOR_DIST(palette[i], palette[j]);
  327. if (d <= max_d)
  328. {
  329. png_dsortp t;
  330. t = (png_dsortp)png_malloc(png_ptr, (png_uint_32)(sizeof
  331. (png_dsort)));
  332. t->next = hash[d];
  333. t->left = (png_byte)i;
  334. t->right = (png_byte)j;
  335. hash[d] = t;
  336. }
  337. }
  338. }
  339. for (i = 0; i <= max_d; i++)
  340. {
  341. if (hash[i] != NULL)
  342. {
  343. png_dsortp p;
  344. for (p = hash[i]; p; p = p->next)
  345. {
  346. if ((int)index_to_palette[p->left] < num_new_palette &&
  347. (int)index_to_palette[p->right] < num_new_palette)
  348. {
  349. int j, next_j;
  350. if (num_new_palette & 1)
  351. {
  352. j = p->left;
  353. next_j = p->right;
  354. }
  355. else
  356. {
  357. j = p->right;
  358. next_j = p->left;
  359. }
  360. num_new_palette--;
  361. palette[index_to_palette[j]] = palette[num_new_palette];
  362. if (!full_dither)
  363. {
  364. int k;
  365. for (k = 0; k < num_palette; k++)
  366. {
  367. if (png_ptr->dither_index[k] ==
  368. index_to_palette[j])
  369. png_ptr->dither_index[k] =
  370. index_to_palette[next_j];
  371. if ((int)png_ptr->dither_index[k] ==
  372. num_new_palette)
  373. png_ptr->dither_index[k] =
  374. index_to_palette[j];
  375. }
  376. }
  377. index_to_palette[palette_to_index[num_new_palette]] =
  378. index_to_palette[j];
  379. palette_to_index[index_to_palette[j]] =
  380. palette_to_index[num_new_palette];
  381. index_to_palette[j] = (png_byte)num_new_palette;
  382. palette_to_index[num_new_palette] = (png_byte)j;
  383. }
  384. if (num_new_palette <= maximum_colors)
  385. break;
  386. }
  387. if (num_new_palette <= maximum_colors)
  388. break;
  389. }
  390. }
  391. for (i = 0; i < 769; i++)
  392. {
  393. if (hash[i] != NULL)
  394. {
  395. png_dsortp p;
  396. p = hash[i];
  397. while (p)
  398. {
  399. png_dsortp t;
  400. t = p->next;
  401. png_free(png_ptr, p);
  402. p = t;
  403. }
  404. }
  405. hash[i] = 0;
  406. }
  407. max_d += 96;
  408. }
  409. png_free(png_ptr, hash);
  410. png_free(png_ptr, palette_to_index);
  411. png_free(png_ptr, index_to_palette);
  412. }
  413. num_palette = maximum_colors;
  414. }
  415. if (png_ptr->palette == NULL)
  416. {
  417. png_ptr->palette = palette;
  418. }
  419. png_ptr->num_palette = (png_uint_16)num_palette;
  420. if (full_dither)
  421. {
  422. int i;
  423. int total_bits, num_red, num_green, num_blue;
  424. png_size_t num_entries;
  425. png_bytep distance;
  426. total_bits = PNG_DITHER_RED_BITS + PNG_DITHER_GREEN_BITS +
  427. PNG_DITHER_BLUE_BITS;
  428. num_red = (1 << PNG_DITHER_RED_BITS);
  429. num_green = (1 << PNG_DITHER_GREEN_BITS);
  430. num_blue = (1 << PNG_DITHER_BLUE_BITS);
  431. num_entries = ((png_size_t)1 << total_bits);
  432. png_ptr->palette_lookup = (png_bytep )png_malloc(png_ptr,
  433. (png_uint_32)(num_entries * sizeof (png_byte)));
  434. png_memset(png_ptr->palette_lookup, 0, num_entries * sizeof (png_byte));
  435. distance = (png_bytep)png_malloc(png_ptr, (png_uint_32)(num_entries *
  436. sizeof(png_byte)));
  437. png_memset(distance, 0xff, num_entries * sizeof(png_byte));
  438. for (i = 0; i < num_palette; i++)
  439. {
  440. int r, g, b, ir, ig, ib;
  441. r = (palette[i].red >> (8 - PNG_DITHER_RED_BITS));
  442. g = (palette[i].green >> (8 - PNG_DITHER_GREEN_BITS));
  443. b = (palette[i].blue >> (8 - PNG_DITHER_BLUE_BITS));
  444. for (ir = 0; ir < num_red; ir++)
  445. {
  446. int dr, index_r;
  447. dr = abs(ir - r);
  448. index_r = (ir << (PNG_DITHER_BLUE_BITS + PNG_DITHER_GREEN_BITS));
  449. for (ig = 0; ig < num_green; ig++)
  450. {
  451. int dg, dt, dm, index_g;
  452. dg = abs(ig - g);
  453. dt = dr + dg;
  454. dm = ((dr > dg) ? dr : dg);
  455. index_g = index_r | (ig << PNG_DITHER_BLUE_BITS);
  456. for (ib = 0; ib < num_blue; ib++)
  457. {
  458. int d_index, db, dmax, d;
  459. d_index = index_g | ib;
  460. db = abs(ib - b);
  461. dmax = ((dm > db) ? dm : db);
  462. d = dmax + dt + db;
  463. if (d < (int)distance[d_index])
  464. {
  465. distance[d_index] = (png_byte)d;
  466. png_ptr->palette_lookup[d_index] = (png_byte)i;
  467. }
  468. }
  469. }
  470. }
  471. }
  472. png_free(png_ptr, distance);
  473. }
  474. }
  475. #endif
  476. #if defined(PNG_READ_GAMMA_SUPPORTED)
  477. /* Transform the image from the file_gamma to the screen_gamma. We
  478. * only do transformations on images where the file_gamma and screen_gamma
  479. * are not close reciprocals, otherwise it slows things down slightly, and
  480. * also needlessly introduces small errors.
  481. */
  482. void
  483. png_set_gamma(png_structp png_ptr, double scrn_gamma, double file_gamma)
  484. {
  485. png_debug(1, "in png_set_gamma\n");
  486. if (fabs(scrn_gamma * file_gamma - 1.0) > PNG_GAMMA_THRESHOLD)
  487. png_ptr->transformations |= PNG_GAMMA;
  488. png_ptr->gamma = (float)file_gamma;
  489. png_ptr->screen_gamma = (float)scrn_gamma;
  490. }
  491. #endif
  492. #if defined(PNG_READ_EXPAND_SUPPORTED)
  493. /* Expand paletted images to rgb, expand grayscale images of
  494. * less then 8 bit depth to 8 bit depth, and expand tRNS chunks
  495. * to alpha channels.
  496. */
  497. void
  498. png_set_expand(png_structp png_ptr)
  499. {
  500. png_debug(1, "in png_set_expand\n");
  501. png_ptr->transformations |= PNG_EXPAND;
  502. }
  503. #endif
  504. #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
  505. void
  506. png_set_gray_to_rgb(png_structp png_ptr)
  507. {
  508. png_debug(1, "in png_set_gray_to_rgb\n");
  509. png_ptr->transformations |= PNG_GRAY_TO_RGB;
  510. }
  511. #endif
  512. #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
  513. /* Convert a RGB image to a grayscale of the given width. This would
  514. * allow us, for example, to convert a 24 bpp RGB image into an 8 or
  515. * 16 bpp grayscale image. (Not yet implemented.)
  516. */
  517. void
  518. png_set_rgb_to_gray(png_structp png_ptr, int gray_bits)
  519. {
  520. png_debug(1, "in png_set_rgb_to_gray\n");
  521. png_ptr->transformations |= PNG_RGB_TO_GRAY;
  522. /* Need to do something with gray_bits here. */
  523. png_warning(png_ptr, "RGB to GRAY transformation is not yet implemented.");
  524. }
  525. #endif
  526. /* Initialize everything needed for the read. This includes modifying
  527. * the palette.
  528. */
  529. void
  530. png_init_read_transformations(png_structp png_ptr)
  531. {
  532. int color_type;
  533. png_debug(1, "in png_init_read_transformations\n");
  534. color_type = png_ptr->color_type;
  535. #if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
  536. if (png_ptr->transformations & PNG_BACKGROUND_EXPAND)
  537. {
  538. if (color_type == PNG_COLOR_TYPE_GRAY)
  539. {
  540. /* expand background chunk. */
  541. switch (png_ptr->bit_depth)
  542. {
  543. case 1:
  544. png_ptr->background.gray *= (png_uint_16)0xff;
  545. png_ptr->background.red = png_ptr->background.green =
  546. png_ptr->background.blue = png_ptr->background.gray;
  547. break;
  548. case 2:
  549. png_ptr->background.gray *= (png_uint_16)0x55;
  550. png_ptr->background.red = png_ptr->background.green =
  551. png_ptr->background.blue = png_ptr->background.gray;
  552. break;
  553. case 4:
  554. png_ptr->background.gray *= (png_uint_16)0x11;
  555. png_ptr->background.red = png_ptr->background.green =
  556. png_ptr->background.blue = png_ptr->background.gray;
  557. break;
  558. case 8:
  559. case 16:
  560. png_ptr->background.red = png_ptr->background.green =
  561. png_ptr->background.blue = png_ptr->background.gray;
  562. break;
  563. }
  564. }
  565. else if (color_type == PNG_COLOR_TYPE_PALETTE)
  566. {
  567. png_ptr->background.red =
  568. png_ptr->palette[png_ptr->background.index].red;
  569. png_ptr->background.green =
  570. png_ptr->palette[png_ptr->background.index].green;
  571. png_ptr->background.blue =
  572. png_ptr->palette[png_ptr->background.index].blue;
  573. #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
  574. if (png_ptr->transformations & PNG_INVERT_ALPHA)
  575. {
  576. #if defined(PNG_READ_EXPAND_SUPPORTED)
  577. if (png_ptr->transformations & !PNG_EXPAND)
  578. #endif
  579. {
  580. /* invert the alpha channel (in tRNS) unless the pixels are
  581. going to be expanded, in which case leave it for later */
  582. int i;
  583. for (i=0; i<(int)png_ptr->num_trans; i++)
  584. png_ptr->trans[i] = 255 - png_ptr->trans[i];
  585. }
  586. }
  587. #endif
  588. }
  589. }
  590. #endif
  591. #if defined(PNG_READ_BACKGROUND_SUPPORTED)
  592. png_ptr->background_1 = png_ptr->background;
  593. #endif
  594. #if defined(PNG_READ_GAMMA_SUPPORTED)
  595. if (png_ptr->transformations & PNG_GAMMA)
  596. {
  597. png_build_gamma_table(png_ptr);
  598. #if defined(PNG_READ_BACKGROUND_SUPPORTED)
  599. if (png_ptr->transformations & PNG_BACKGROUND)
  600. {
  601. if (color_type == PNG_COLOR_TYPE_PALETTE)
  602. {
  603. int num_palette, i;
  604. png_color back, back_1;
  605. png_colorp palette;
  606. palette = png_ptr->palette;
  607. num_palette = png_ptr->num_palette;
  608. if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
  609. {
  610. back.red = png_ptr->gamma_table[png_ptr->background.red];
  611. back.green = png_ptr->gamma_table[png_ptr->background.green];
  612. back.blue = png_ptr->gamma_table[png_ptr->background.blue];
  613. back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
  614. back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
  615. back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
  616. }
  617. else
  618. {
  619. double g;
  620. g = 1.0 / (png_ptr->background_gamma * png_ptr->screen_gamma);
  621. if (png_ptr->background_gamma_type==PNG_BACKGROUND_GAMMA_SCREEN||
  622. fabs(g - 1.0) < PNG_GAMMA_THRESHOLD)
  623. {
  624. back.red = (png_byte)png_ptr->background.red;
  625. back.green = (png_byte)png_ptr->background.green;
  626. back.blue = (png_byte)png_ptr->background.blue;
  627. }
  628. else
  629. {
  630. back.red =
  631. (png_byte)(pow((double)png_ptr->background.red/255, g) *
  632. 255.0 + 0.5);
  633. back.green =
  634. (png_byte)(pow((double)png_ptr->background.green/255, g) *
  635. 255.0 + 0.5);
  636. back.blue =
  637. (png_byte)(pow((double)png_ptr->background.blue/255, g) *
  638. 255.0 + 0.5);
  639. }
  640. g = 1.0 / png_ptr->background_gamma;
  641. back_1.red =
  642. (png_byte)(pow((double)png_ptr->background.red/255, g) *
  643. 255.0 + 0.5);
  644. back_1.green =
  645. (png_byte)(pow((double)png_ptr->background.green/255, g) *
  646. 255.0 + 0.5);
  647. back_1.blue =
  648. (png_byte)(pow((double)png_ptr->background.blue/255, g) *
  649. 255.0 + 0.5);
  650. }
  651. for (i = 0; i < num_palette; i++)
  652. {
  653. if (i < (int)png_ptr->num_trans && png_ptr->trans[i] != 0xff)
  654. {
  655. if (png_ptr->trans[i] == 0)
  656. {
  657. palette[i] = back;
  658. }
  659. else /* if (png_ptr->trans[i] != 0xff) */
  660. {
  661. png_byte v, w;
  662. v = png_ptr->gamma_to_1[palette[i].red];
  663. png_composite(w, v, png_ptr->trans[i], back_1.red);
  664. palette[i].red = png_ptr->gamma_from_1[w];
  665. v = png_ptr->gamma_to_1[palette[i].green];
  666. png_composite(w, v, png_ptr->trans[i], back_1.green);
  667. palette[i].green = png_ptr->gamma_from_1[w];
  668. v = png_ptr->gamma_to_1[palette[i].blue];
  669. png_composite(w, v, png_ptr->trans[i], back_1.blue);
  670. palette[i].blue = png_ptr->gamma_from_1[w];
  671. }
  672. }
  673. else
  674. {
  675. palette[i].red = png_ptr->gamma_table[palette[i].red];
  676. palette[i].green = png_ptr->gamma_table[palette[i].green];
  677. palette[i].blue = png_ptr->gamma_table[palette[i].blue];
  678. }
  679. }
  680. }
  681. else
  682. /* if (png_ptr->background_gamma_type!=PNG_BACKGROUND_GAMMA_UNKNOWN)*/
  683. {
  684. double g, gs, m;
  685. m = (double)(((png_uint_32)1 << png_ptr->bit_depth) - 1);
  686. g = 1.0;
  687. gs = 1.0;
  688. switch (png_ptr->background_gamma_type)
  689. {
  690. case PNG_BACKGROUND_GAMMA_SCREEN:
  691. g = (png_ptr->screen_gamma);
  692. gs = 1.0;
  693. break;
  694. case PNG_BACKGROUND_GAMMA_FILE:
  695. g = 1.0 / (png_ptr->gamma);
  696. gs = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
  697. break;
  698. case PNG_BACKGROUND_GAMMA_UNIQUE:
  699. g = 1.0 / (png_ptr->background_gamma);
  700. gs = 1.0 / (png_ptr->background_gamma *
  701. png_ptr->screen_gamma);
  702. break;
  703. }
  704. if (color_type & PNG_COLOR_MASK_COLOR)
  705. {
  706. png_ptr->background_1.red = (png_uint_16)(pow(
  707. (double)png_ptr->background.red / m, g) * m + .5);
  708. png_ptr->background_1.green = (png_uint_16)(pow(
  709. (double)png_ptr->background.green / m, g) * m + .5);
  710. png_ptr->background_1.blue = (png_uint_16)(pow(
  711. (double)png_ptr->background.blue / m, g) * m + .5);
  712. png_ptr->background.red = (png_uint_16)(pow(
  713. (double)png_ptr->background.red / m, gs) * m + .5);
  714. png_ptr->background.green = (png_uint_16)(pow(
  715. (double)png_ptr->background.green / m, gs) * m + .5);
  716. png_ptr->background.blue = (png_uint_16)(pow(
  717. (double)png_ptr->background.blue / m, gs) * m + .5);
  718. }
  719. else
  720. {
  721. png_ptr->background_1.gray = (png_uint_16)(pow(
  722. (double)png_ptr->background.gray / m, g) * m + .5);
  723. png_ptr->background.gray = (png_uint_16)(pow(
  724. (double)png_ptr->background.gray / m, gs) * m + .5);
  725. }
  726. }
  727. }
  728. else
  729. #endif
  730. if (color_type == PNG_COLOR_TYPE_PALETTE)
  731. {
  732. int num_palette, i;
  733. png_colorp palette;
  734. palette = png_ptr->palette;
  735. num_palette = png_ptr->num_palette;
  736. for (i = 0; i < num_palette; i++)
  737. {
  738. palette[i].red = png_ptr->gamma_table[palette[i].red];
  739. palette[i].green = png_ptr->gamma_table[palette[i].green];
  740. palette[i].blue = png_ptr->gamma_table[palette[i].blue];
  741. }
  742. }
  743. }
  744. #if defined(PNG_READ_BACKGROUND_SUPPORTED)
  745. else
  746. #endif
  747. #endif
  748. #if defined(PNG_READ_BACKGROUND_SUPPORTED)
  749. if (png_ptr->transformations & PNG_BACKGROUND &&
  750. color_type == PNG_COLOR_TYPE_PALETTE)
  751. {
  752. int i;
  753. png_color back;
  754. png_colorp palette;
  755. palette = png_ptr->palette;
  756. back.red = (png_byte)png_ptr->background.red;
  757. back.green = (png_byte)png_ptr->background.green;
  758. back.blue = (png_byte)png_ptr->background.blue;
  759. for (i = 0; i < (int)png_ptr->num_trans; i++)
  760. {
  761. if (png_ptr->trans[i] == 0)
  762. {
  763. palette[i] = back;
  764. }
  765. else if (png_ptr->trans[i] != 0xff)
  766. {
  767. png_composite(palette[i].red, palette[i].red,
  768. png_ptr->trans[i], back.red);
  769. png_composite(palette[i].green, palette[i].green,
  770. png_ptr->trans[i], back.green);
  771. png_composite(palette[i].blue, palette[i].blue,
  772. png_ptr->trans[i], back.blue);
  773. }
  774. }
  775. }
  776. #endif
  777. #if defined(PNG_READ_SHIFT_SUPPORTED)
  778. if ((png_ptr->transformations & PNG_SHIFT) &&
  779. color_type == PNG_COLOR_TYPE_PALETTE)
  780. {
  781. png_uint_16 i;
  782. int sr, sg, sb;
  783. sr = 8 - png_ptr->sig_bit.red;
  784. if (sr < 0 || sr > 8)
  785. sr = 0;
  786. sg = 8 - png_ptr->sig_bit.green;
  787. if (sg < 0 || sg > 8)
  788. sg = 0;
  789. sb = 8 - png_ptr->sig_bit.blue;
  790. if (sb < 0 || sb > 8)
  791. sb = 0;
  792. for (i = 0; i < png_ptr->num_palette; i++)
  793. {
  794. png_ptr->palette[i].red >>= sr;
  795. png_ptr->palette[i].green >>= sg;
  796. png_ptr->palette[i].blue >>= sb;
  797. }
  798. }
  799. #endif
  800. }
  801. /* Modify the info structure to reflect the transformations. The
  802. * info should be updated so a PNG file could be written with it,
  803. * assuming the transformations result in valid PNG data.
  804. */
  805. void
  806. png_read_transform_info(png_structp png_ptr, png_infop info_ptr)
  807. {
  808. png_debug(1, "in png_read_transform_info\n");
  809. #if defined(PNG_READ_EXPAND_SUPPORTED)
  810. if (png_ptr->transformations & PNG_EXPAND)
  811. {
  812. if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  813. {
  814. if (png_ptr->num_trans)
  815. info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  816. else
  817. info_ptr->color_type = PNG_COLOR_TYPE_RGB;
  818. info_ptr->bit_depth = 8;
  819. info_ptr->num_trans = 0;
  820. }
  821. else
  822. {
  823. if (png_ptr->num_trans)
  824. info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
  825. if (info_ptr->bit_depth < 8)
  826. info_ptr->bit_depth = 8;
  827. info_ptr->num_trans = 0;
  828. }
  829. }
  830. #endif
  831. #if defined(PNG_READ_BACKGROUND_SUPPORTED)
  832. if (png_ptr->transformations & PNG_BACKGROUND)
  833. {
  834. info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
  835. info_ptr->num_trans = 0;
  836. info_ptr->background = png_ptr->background;
  837. }
  838. #endif
  839. #if defined(PNG_READ_16_TO_8_SUPPORTED)
  840. if ((png_ptr->transformations & PNG_16_TO_8) && info_ptr->bit_depth == 16)
  841. info_ptr->bit_depth = 8;
  842. #endif
  843. #if defined(PNG_READ_DITHER_SUPPORTED)
  844. if (png_ptr->transformations & PNG_DITHER)
  845. {
  846. if (((info_ptr->color_type == PNG_COLOR_TYPE_RGB) ||
  847. (info_ptr->color_type == PNG_COLOR_TYPE_RGB_ALPHA)) &&
  848. png_ptr->palette_lookup && info_ptr->bit_depth == 8)
  849. {
  850. info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
  851. }
  852. }
  853. #endif
  854. #if defined(PNG_READ_PACK_SUPPORTED)
  855. if ((png_ptr->transformations & PNG_PACK) && info_ptr->bit_depth < 8)
  856. info_ptr->bit_depth = 8;
  857. #endif
  858. #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
  859. if ((png_ptr->transformations & PNG_GRAY_TO_RGB) &&
  860. !(info_ptr->color_type & PNG_COLOR_MASK_COLOR))
  861. info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
  862. #endif
  863. if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  864. info_ptr->channels = 1;
  865. else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
  866. info_ptr->channels = 3;
  867. else
  868. info_ptr->channels = 1;
  869. #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
  870. if ((png_ptr->transformations & PNG_STRIP_ALPHA) &&
  871. info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  872. {
  873. info_ptr->channels--;
  874. info_ptr->color_type &= ~PNG_COLOR_MASK_ALPHA;
  875. }
  876. #endif
  877. #if defined(PNG_READ_FILLER_SUPPORTED)
  878. if ((png_ptr->transformations & PNG_FILLER) &&
  879. info_ptr->color_type & PNG_COLOR_TYPE_RGB &&
  880. info_ptr->channels == 3)
  881. {
  882. info_ptr->channels = 4;
  883. }
  884. #endif
  885. if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
  886. info_ptr->channels++;
  887. info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
  888. info_ptr->bit_depth);
  889. info_ptr->rowbytes =
  890. (png_size_t)((info_ptr->width * info_ptr->pixel_depth + 7) >> 3);
  891. }
  892. /* Transform the row. The order of transformations is significant,
  893. * and is very touchy. If you add a transformation, take care to
  894. * decide how it fits in with the other transformations here.
  895. */
  896. void
  897. png_do_read_transformations(png_structp png_ptr)
  898. {
  899. png_debug(1, "in png_do_read_transformations\n");
  900. #if !defined(PNG_USELESS_TESTS_SUPPORTED)
  901. if (png_ptr->row_buf == NULL)
  902. {
  903. #if !defined(PNG_NO_STDIO)
  904. char msg[50];
  905. sprintf(msg, "NULL row buffer for row %ld, pass %d", png_ptr->row_number,
  906. png_ptr->pass);
  907. png_error(png_ptr, msg);
  908. #else
  909. png_error(png_ptr, "NULL row buffer");
  910. #endif
  911. }
  912. #endif
  913. #if defined(PNG_READ_EXPAND_SUPPORTED)
  914. if (png_ptr->transformations & PNG_EXPAND)
  915. {
  916. if (png_ptr->row_info.color_type == PNG_COLOR_TYPE_PALETTE)
  917. {
  918. png_do_expand_palette(&(png_ptr->row_info), png_ptr->row_buf + 1,
  919. png_ptr->palette, png_ptr->trans, png_ptr->num_trans);
  920. }
  921. else if (png_ptr->transformations & PNG_EXPAND)
  922. {
  923. if (png_ptr->num_trans)
  924. png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
  925. &(png_ptr->trans_values));
  926. else
  927. png_do_expand(&(png_ptr->row_info), png_ptr->row_buf + 1,
  928. NULL);
  929. }
  930. }
  931. #endif
  932. #if defined(PNG_READ_STRIP_ALPHA_SUPPORTED)
  933. if (png_ptr->transformations & PNG_STRIP_ALPHA)
  934. png_do_strip_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
  935. PNG_FLAG_FILLER_AFTER);
  936. #endif
  937. #if defined(PNG_READ_BACKGROUND_SUPPORTED)
  938. if ((png_ptr->transformations & PNG_BACKGROUND) &&
  939. ((png_ptr->num_trans != 0 ) ||
  940. (png_ptr->color_type & PNG_COLOR_MASK_ALPHA)))
  941. png_do_background(&(png_ptr->row_info), png_ptr->row_buf + 1,
  942. &(png_ptr->trans_values), &(png_ptr->background),
  943. &(png_ptr->background_1),
  944. png_ptr->gamma_table, png_ptr->gamma_from_1,
  945. png_ptr->gamma_to_1, png_ptr->gamma_16_table,
  946. png_ptr->gamma_16_from_1, png_ptr->gamma_16_to_1,
  947. png_ptr->gamma_shift);
  948. #endif
  949. #if defined(PNG_READ_GAMMA_SUPPORTED)
  950. if ((png_ptr->transformations & PNG_GAMMA) &&
  951. !(png_ptr->transformations & PNG_BACKGROUND) &&
  952. (png_ptr->color_type != PNG_COLOR_TYPE_PALETTE))
  953. png_do_gamma(&(png_ptr->row_info), png_ptr->row_buf + 1,
  954. png_ptr->gamma_table, png_ptr->gamma_16_table,
  955. png_ptr->gamma_shift);
  956. #endif
  957. #if defined(PNG_RGB_TO_GRAY_SUPPORTED)
  958. if (png_ptr->transformations & PNG_RGB_TO_GRAY)
  959. png_do_rgb_to_gray(&(png_ptr->row_info), png_ptr->row_buf + 1);
  960. #endif
  961. #if defined(PNG_READ_16_TO_8_SUPPORTED)
  962. if (png_ptr->transformations & PNG_16_TO_8)
  963. png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
  964. #endif
  965. #if defined(PNG_READ_DITHER_SUPPORTED)
  966. if (png_ptr->transformations & PNG_DITHER)
  967. {
  968. png_do_dither((png_row_infop)&(png_ptr->row_info), png_ptr->row_buf + 1,
  969. png_ptr->palette_lookup, png_ptr->dither_index);
  970. if(png_ptr->row_info.rowbytes == (png_size_t)0)
  971. png_error(png_ptr, "png_do_dither returned rowbytes=0");
  972. }
  973. #endif
  974. #if defined(PNG_READ_INVERT_SUPPORTED)
  975. if (png_ptr->transformations & PNG_INVERT_MONO)
  976. png_do_invert(&(png_ptr->row_info), png_ptr->row_buf + 1);
  977. #endif
  978. #if defined(PNG_READ_SHIFT_SUPPORTED)
  979. if (png_ptr->transformations & PNG_SHIFT)
  980. png_do_unshift(&(png_ptr->row_info), png_ptr->row_buf + 1,
  981. &(png_ptr->shift));
  982. #endif
  983. #if defined(PNG_READ_PACK_SUPPORTED)
  984. if (png_ptr->transformations & PNG_PACK)
  985. png_do_unpack(&(png_ptr->row_info), png_ptr->row_buf + 1);
  986. #endif
  987. #if defined(PNG_READ_BGR_SUPPORTED)
  988. if (png_ptr->transformations & PNG_BGR)
  989. png_do_bgr(&(png_ptr->row_info), png_ptr->row_buf + 1);
  990. #endif
  991. #if defined(PNG_READ_PACKSWAP_SUPPORTED)
  992. if (png_ptr->transformations & PNG_PACKSWAP)
  993. png_do_packswap(&(png_ptr->row_info), png_ptr->row_buf + 1);
  994. #endif
  995. #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
  996. if (png_ptr->transformations & PNG_GRAY_TO_RGB)
  997. png_do_gray_to_rgb(&(png_ptr->row_info), png_ptr->row_buf + 1);
  998. #endif
  999. #if defined(PNG_READ_FILLER_SUPPORTED)
  1000. if (png_ptr->transformations & PNG_FILLER)
  1001. png_do_read_filler(&(png_ptr->row_info), png_ptr->row_buf + 1,
  1002. (png_uint_32)png_ptr->filler, png_ptr->flags);
  1003. #endif
  1004. #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
  1005. if (png_ptr->transformations & PNG_SWAP_ALPHA)
  1006. png_do_read_swap_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
  1007. #endif
  1008. #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
  1009. if (png_ptr->transformations & PNG_INVERT_ALPHA)
  1010. png_do_read_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
  1011. #endif
  1012. #if defined(PNG_READ_SWAP_SUPPORTED)
  1013. if (png_ptr->transformations & PNG_SWAP_BYTES)
  1014. png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
  1015. #endif
  1016. }
  1017. #if defined(PNG_READ_PACK_SUPPORTED)
  1018. /* Unpack pixels of 1, 2, or 4 bits per pixel into 1 byte per pixel,
  1019. * without changing the actual values. Thus, if you had a row with
  1020. * a bit depth of 1, you would end up with bytes that only contained
  1021. * the numbers 0 or 1. If you would rather they contain 0 and 255, use
  1022. * png_do_shift() after this.
  1023. */
  1024. void
  1025. png_do_unpack(png_row_infop row_info, png_bytep row)
  1026. {
  1027. png_debug(1, "in png_do_unpack\n");
  1028. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  1029. if (row != NULL && row_info != NULL && row_info->bit_depth < 8)
  1030. #else
  1031. if (row_info->bit_depth < 8)
  1032. #endif
  1033. {
  1034. png_uint_32 shift, i;
  1035. png_bytep sp, dp;
  1036. switch (row_info->bit_depth)
  1037. {
  1038. case 1:
  1039. {
  1040. sp = row + (png_size_t)((row_info->width - 1) >> 3);
  1041. dp = row + (png_size_t)row_info->width - 1;
  1042. shift = 7 - (int)((row_info->width + 7) & 7);
  1043. for (i = 0; i < row_info->width; i++)
  1044. {
  1045. *dp = (png_byte)((*sp >> shift) & 0x1);
  1046. if (shift == 7)
  1047. {
  1048. shift = 0;
  1049. sp--;
  1050. }
  1051. else
  1052. shift++;
  1053. dp--;
  1054. }
  1055. break;
  1056. }
  1057. case 2:
  1058. {
  1059. sp = row + (png_size_t)((row_info->width - 1) >> 2);
  1060. dp = row + (png_size_t)row_info->width - 1;
  1061. shift = (int)((3 - ((row_info->width + 3) & 3)) << 1);
  1062. for (i = 0; i < row_info->width; i++)
  1063. {
  1064. *dp = (png_byte)((*sp >> shift) & 0x3);
  1065. if (shift == 6)
  1066. {
  1067. shift = 0;
  1068. sp--;
  1069. }
  1070. else
  1071. shift += 2;
  1072. dp--;
  1073. }
  1074. break;
  1075. }
  1076. case 4:
  1077. {
  1078. sp = row + (png_size_t)((row_info->width - 1) >> 1);
  1079. dp = row + (png_size_t)row_info->width - 1;
  1080. shift = (int)((1 - ((row_info->width + 1) & 1)) << 2);
  1081. for (i = 0; i < row_info->width; i++)
  1082. {
  1083. *dp = (png_byte)((*sp >> shift) & 0xf);
  1084. if (shift == 4)
  1085. {
  1086. shift = 0;
  1087. sp--;
  1088. }
  1089. else
  1090. shift = 4;
  1091. dp--;
  1092. }
  1093. break;
  1094. }
  1095. }
  1096. row_info->bit_depth = 8;
  1097. row_info->pixel_depth = (png_byte)(8 * row_info->channels);
  1098. row_info->rowbytes = (png_size_t)row_info->width * row_info->channels;
  1099. }
  1100. }
  1101. #endif
  1102. #if defined(PNG_READ_SHIFT_SUPPORTED)
  1103. /* Reverse the effects of png_do_shift. This routine merely shifts the
  1104. * pixels back to their significant bits values. Thus, if you have
  1105. * a row of bit depth 8, but only 5 are significant, this will shift
  1106. * the values back to 0 through 31.
  1107. */
  1108. void
  1109. png_do_unshift(png_row_infop row_info, png_bytep row, png_color_8p sig_bits)
  1110. {
  1111. png_debug(1, "in png_do_unshift\n");
  1112. if (
  1113. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  1114. row != NULL && row_info != NULL && sig_bits != NULL &&
  1115. #endif
  1116. row_info->color_type != PNG_COLOR_TYPE_PALETTE)
  1117. {
  1118. int shift[4];
  1119. int channels, c;
  1120. png_uint_16 value;
  1121. channels = 0;
  1122. if (row_info->color_type & PNG_COLOR_MASK_COLOR)
  1123. {
  1124. shift[channels++] = row_info->bit_depth - sig_bits->red;
  1125. shift[channels++] = row_info->bit_depth - sig_bits->green;
  1126. shift[channels++] = row_info->bit_depth - sig_bits->blue;
  1127. }
  1128. else
  1129. {
  1130. shift[channels++] = row_info->bit_depth - sig_bits->gray;
  1131. }
  1132. if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
  1133. {
  1134. shift[channels++] = row_info->bit_depth - sig_bits->alpha;
  1135. }
  1136. value = 0;
  1137. for (c = 0; c < channels; c++)
  1138. {
  1139. if (shift[c] <= 0)
  1140. shift[c] = 0;
  1141. else
  1142. value = 1;
  1143. }
  1144. if (!value)
  1145. return;
  1146. switch (row_info->bit_depth)
  1147. {
  1148. case 2:
  1149. {
  1150. png_bytep bp;
  1151. png_size_t i;
  1152. for (bp = row, i = 0; i < row_info->rowbytes; i++, bp++)
  1153. {
  1154. *bp >>= 1;
  1155. *bp &= 0x55;
  1156. }
  1157. break;
  1158. }
  1159. case 4:
  1160. {
  1161. png_bytep bp;
  1162. png_byte mask;
  1163. png_size_t i;
  1164. mask = (png_byte)(((int)0xf0 >> shift[0]) & (int)0xf0) |
  1165. (png_byte)((int)0xf >> shift[0]);
  1166. for (bp = row, i = 0; i < row_info->rowbytes; i++, bp++)
  1167. {
  1168. *bp >>= shift[0];
  1169. *bp &= mask;
  1170. }
  1171. break;
  1172. }
  1173. case 8:
  1174. {
  1175. png_bytep bp;
  1176. png_size_t i;
  1177. for (bp = row, i = 0; i < row_info->width; i++)
  1178. {
  1179. for (c = 0; c < (int)row_info->channels; c++, bp++)
  1180. {
  1181. *bp >>= shift[c];
  1182. }
  1183. }
  1184. break;
  1185. }
  1186. case 16:
  1187. {
  1188. png_bytep bp;
  1189. png_size_t i;
  1190. for (bp = row, i = 0; i < row_info->width; i++)
  1191. {
  1192. for (c = 0; c < (int)row_info->channels; c++, bp += 2)
  1193. {
  1194. value = (png_uint_16)((*bp << 8) + *(bp + 1));
  1195. value >>= shift[c];
  1196. *bp = (png_byte)(value >> 8);
  1197. *(bp + 1) = (png_byte)(value & 0xff);
  1198. }
  1199. }
  1200. break;
  1201. }
  1202. }
  1203. }
  1204. }
  1205. #endif
  1206. #if defined(PNG_READ_16_TO_8_SUPPORTED)
  1207. /* chop rows of bit depth 16 down to 8 */
  1208. void
  1209. png_do_chop(png_row_infop row_info, png_bytep row)
  1210. {
  1211. png_debug(1, "in png_do_chop\n");
  1212. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  1213. if (row != NULL && row_info != NULL && row_info->bit_depth == 16)
  1214. #else
  1215. if (row_info->bit_depth == 16)
  1216. #endif
  1217. {
  1218. png_bytep sp, dp;
  1219. png_uint_32 i;
  1220. sp = row;
  1221. dp = row;
  1222. for (i = 0; i < row_info->width * row_info->channels; i++, sp += 2, dp++)
  1223. {
  1224. #if defined(PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED)
  1225. /* This does a more accurate scaling of the 16-bit color
  1226. * value, rather than a simple low-byte truncation.
  1227. *
  1228. * What the ideal calculation should be:
  1229. *dp = (((((png_uint_32)(*sp) << 8) |
  1230. (png_uint_32)(*(sp + 1))) * 255 + 127) / (png_uint_32)65535L;
  1231. * GRR: no, I think this is what it really should be:
  1232. *dp = (((((png_uint_32)(*sp) << 8) |
  1233. (png_uint_32)(*(sp + 1))) + 128L) / (png_uint_32)257L;
  1234. * GRR: here's the exact calculation with shifts:
  1235. temp = (((png_uint_32)(*sp) << 8) | (png_uint_32)(*(sp + 1))) + 128L;
  1236. *dp = (temp - (temp >> 8)) >> 8;
  1237. * Approximate calculation with shift/add instead of multiply/divide:
  1238. *dp = ((((png_uint_32)(*sp) << 8) |
  1239. (png_uint_32)((int)(*(sp + 1)) - *sp)) + 128) >> 8;
  1240. * What we actually do to avoid extra shifting and conversion: */
  1241. *dp = *sp + ((((int)(*(sp + 1)) - *sp) > 128) ? 1 : 0);
  1242. #else
  1243. *dp = *sp;
  1244. #endif
  1245. }
  1246. row_info->bit_depth = 8;
  1247. row_info->pixel_depth = (png_byte)(8 * row_info->channels);
  1248. row_info->rowbytes = (png_size_t)row_info->width * row_info->channels;
  1249. }
  1250. }
  1251. #endif
  1252. #if defined(PNG_READ_SWAP_ALPHA_SUPPORTED)
  1253. void
  1254. png_do_read_swap_alpha(png_row_infop row_info, png_bytep row)
  1255. {
  1256. png_debug(1, "in png_do_read_swap_alpha\n");
  1257. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  1258. if (row != NULL && row_info != NULL)
  1259. #endif
  1260. {
  1261. if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  1262. {
  1263. /* This converts from RGBA to ARGB */
  1264. if (row_info->bit_depth == 8)
  1265. {
  1266. png_bytep sp, dp;
  1267. png_byte save;
  1268. png_uint_32 i;
  1269. for (i = 0, sp = dp = row + row_info->rowbytes;
  1270. i < row_info->width; i++)
  1271. {
  1272. save = *(--sp);
  1273. *(--dp) = *(--sp);
  1274. *(--dp) = *(--sp);
  1275. *(--dp) = *(--sp);
  1276. *(--dp) = save;
  1277. }
  1278. }
  1279. /* This converts from RRGGBBAA to AARRGGBB */
  1280. else
  1281. {
  1282. png_bytep sp, dp;
  1283. png_byte save[2];
  1284. png_uint_32 i;
  1285. for (i = 0, sp = dp = row + row_info->rowbytes;
  1286. i < row_info->width; i++)
  1287. {
  1288. save[0] = *(--sp);
  1289. save[1] = *(--sp);
  1290. *(--dp) = *(--sp);
  1291. *(--dp) = *(--sp);
  1292. *(--dp) = *(--sp);
  1293. *(--dp) = *(--sp);
  1294. *(--dp) = *(--sp);
  1295. *(--dp) = *(--sp);
  1296. *(--dp) = save[0];
  1297. *(--dp) = save[1];
  1298. }
  1299. }
  1300. }
  1301. else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  1302. {
  1303. /* This converts from GA to AG */
  1304. if (row_info->bit_depth == 8)
  1305. {
  1306. png_bytep sp, dp;
  1307. png_byte save;
  1308. png_uint_32 i;
  1309. for (i = 0, sp = dp = row + row_info->rowbytes;
  1310. i < row_info->width; i++)
  1311. {
  1312. save = *(--sp);
  1313. *(--dp) = *(--sp);
  1314. *(--dp) = save;
  1315. }
  1316. }
  1317. /* This converts from GGAA to AAGG */
  1318. else
  1319. {
  1320. png_bytep sp, dp;
  1321. png_byte save[2];
  1322. png_uint_32 i;
  1323. for (i = 0, sp = dp = row + row_info->rowbytes;
  1324. i < row_info->width; i++)
  1325. {
  1326. save[0] = *(--sp);
  1327. save[1] = *(--sp);
  1328. *(--dp) = *(--sp);
  1329. *(--dp) = *(--sp);
  1330. *(--dp) = save[0];
  1331. *(--dp) = save[1];
  1332. }
  1333. }
  1334. }
  1335. }
  1336. }
  1337. #endif
  1338. #if defined(PNG_READ_INVERT_ALPHA_SUPPORTED)
  1339. void
  1340. png_do_read_invert_alpha(png_row_infop row_info, png_bytep row)
  1341. {
  1342. png_debug(1, "in png_do_read_invert_alpha\n");
  1343. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  1344. if (row != NULL && row_info != NULL)
  1345. #endif
  1346. {
  1347. if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
  1348. {
  1349. /* This inverts the alpha channel in RGBA */
  1350. if (row_info->bit_depth == 8)
  1351. {
  1352. png_bytep sp, dp;
  1353. png_uint_32 i;
  1354. for (i = 0, sp = dp = row + row_info->rowbytes;
  1355. i < row_info->width; i++)
  1356. {
  1357. *(--dp) = *(--sp);
  1358. *(--dp) = *(--sp);
  1359. *(--dp) = *(--sp);
  1360. *(--dp) = 255 - *(--sp);
  1361. }
  1362. }
  1363. /* This inverts the alpha channel in RRGGBBAA */
  1364. else
  1365. {
  1366. png_bytep sp, dp;
  1367. png_uint_32 i;
  1368. for (i = 0, sp = dp = row + row_info->rowbytes;
  1369. i < row_info->width; i++)
  1370. {
  1371. *(--dp) = *(--sp);
  1372. *(--dp) = *(--sp);
  1373. *(--dp) = *(--sp);
  1374. *(--dp) = *(--sp);
  1375. *(--dp) = *(--sp);
  1376. *(--dp) = *(--sp);
  1377. *(--dp) = 255 - *(--sp);
  1378. *(--dp) = 255 - *(--sp);
  1379. }
  1380. }
  1381. }
  1382. else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  1383. {
  1384. /* This inverts the alpha channel in AG */
  1385. if (row_info->bit_depth == 8)
  1386. {
  1387. png_bytep sp, dp;
  1388. png_uint_32 i;
  1389. for (i = 0, sp = dp = row + row_info->rowbytes;
  1390. i < row_info->width; i++)
  1391. {
  1392. *(--dp) = *(--sp);
  1393. *(--dp) = 255 - *(--sp);
  1394. }
  1395. }
  1396. /* This inverts the alpha channel in AAGG */
  1397. else
  1398. {
  1399. png_bytep sp, dp;
  1400. png_uint_32 i;
  1401. for (i = 0, sp = dp = row + row_info->rowbytes;
  1402. i < row_info->width; i++)
  1403. {
  1404. *(--dp) = *(--sp);
  1405. *(--dp) = *(--sp);
  1406. *(--dp) = 255 - *(--sp);
  1407. *(--dp) = 255 - *(--sp);
  1408. }
  1409. }
  1410. }
  1411. }
  1412. }
  1413. #endif
  1414. #if defined(PNG_READ_FILLER_SUPPORTED)
  1415. /* Add filler channel if we have RGB color */
  1416. void
  1417. png_do_read_filler(png_row_infop row_info, png_bytep row,
  1418. png_uint_32 filler, png_uint_32 flags)
  1419. {
  1420. png_bytep sp, dp;
  1421. png_uint_32 i;
  1422. png_debug(1, "in png_do_read_filler\n");
  1423. if (
  1424. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  1425. row != NULL && row_info != NULL &&
  1426. #endif
  1427. row_info->color_type == PNG_COLOR_TYPE_RGB && row_info->bit_depth == 8)
  1428. {
  1429. /* This changes the data from RGB to RGBX */
  1430. if (flags & PNG_FLAG_FILLER_AFTER)
  1431. {
  1432. for (i = 1, sp = row + (png_size_t)row_info->width * 3,
  1433. dp = row + (png_size_t)row_info->width * 4;
  1434. i < row_info->width;
  1435. i++)
  1436. {
  1437. *(--dp) = (png_byte)filler;
  1438. *(--dp) = *(--sp);
  1439. *(--dp) = *(--sp);
  1440. *(--dp) = *(--sp);
  1441. }
  1442. *(--dp) = (png_byte)filler;
  1443. row_info->channels = 4;
  1444. row_info->pixel_depth = 32;
  1445. row_info->rowbytes = (png_size_t)row_info->width * 4;
  1446. }
  1447. /* This changes the data from RGB to XRGB */
  1448. else
  1449. {
  1450. for (i = 0, sp = row + (png_size_t)row_info->width * 3,
  1451. dp = row + (png_size_t)row_info->width * 4;
  1452. i < row_info->width;
  1453. i++)
  1454. {
  1455. *(--dp) = *(--sp);
  1456. *(--dp) = *(--sp);
  1457. *(--dp) = *(--sp);
  1458. *(--dp) = (png_byte)filler;
  1459. }
  1460. row_info->channels = 4;
  1461. row_info->pixel_depth = 32;
  1462. row_info->rowbytes = (png_size_t)row_info->width * 4;
  1463. }
  1464. }
  1465. }
  1466. #endif
  1467. #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
  1468. /* expand grayscale files to RGB, with or without alpha */
  1469. void
  1470. png_do_gray_to_rgb(png_row_infop row_info, png_bytep row)
  1471. {
  1472. png_bytep sp, dp;
  1473. png_uint_32 i;
  1474. png_debug(1, "in png_do_gray_to_rgb\n");
  1475. if (row_info->bit_depth >= 8 &&
  1476. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  1477. row != NULL && row_info != NULL &&
  1478. #endif
  1479. !(row_info->color_type & PNG_COLOR_MASK_COLOR))
  1480. {
  1481. if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
  1482. {
  1483. if (row_info->bit_depth == 8)
  1484. {
  1485. for (i = 0, sp = row + (png_size_t)row_info->width - 1,
  1486. dp = row + (png_size_t)row_info->width * 3 - 1;
  1487. i < row_info->width;
  1488. i++)
  1489. {
  1490. *(dp--) = *sp;
  1491. *(dp--) = *sp;
  1492. *(dp--) = *sp;
  1493. sp--;
  1494. }
  1495. }
  1496. else
  1497. {
  1498. for (i = 0, sp = row + (png_size_t)row_info->width * 2 - 1,
  1499. dp = row + (png_size_t)row_info->width * 6 - 1;
  1500. i < row_info->width;
  1501. i++)
  1502. {
  1503. *(dp--) = *sp;
  1504. *(dp--) = *(sp - 1);
  1505. *(dp--) = *sp;
  1506. *(dp--) = *(sp - 1);
  1507. *(dp--) = *sp;
  1508. *(dp--) = *(sp - 1);
  1509. sp--;
  1510. sp--;
  1511. }
  1512. }
  1513. }
  1514. else if (row_info->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
  1515. {
  1516. if (row_info->bit_depth == 8)
  1517. {
  1518. for (i = 0, sp = row + (png_size_t)row_info->width * 2 - 1,
  1519. dp = row + (png_size_t)row_info->width * 4 - 1;
  1520. i < row_info->width;
  1521. i++)
  1522. {
  1523. *(dp--) = *(sp--);
  1524. *(dp--) = *sp;
  1525. *(dp--) = *sp;
  1526. *(dp--) = *sp;
  1527. sp--;
  1528. }
  1529. }
  1530. else
  1531. {
  1532. for (i = 0, sp = row + (png_size_t)row_info->width * 4 - 1,
  1533. dp = row + (png_size_t)row_info->width * 8 - 1;
  1534. i < row_info->width;
  1535. i++)
  1536. {
  1537. *(dp--) = *(sp--);
  1538. *(dp--) = *(sp--);
  1539. *(dp--) = *sp;
  1540. *(dp--) = *(sp - 1);
  1541. *(dp--) = *sp;
  1542. *(dp--) = *(sp - 1);
  1543. *(dp--) = *sp;
  1544. *(dp--) = *(sp - 1);
  1545. sp--;
  1546. sp--;
  1547. }
  1548. }
  1549. }
  1550. row_info->channels += (png_byte)2;
  1551. row_info->color_type |= PNG_COLOR_MASK_COLOR;
  1552. row_info->pixel_depth = (png_byte)(row_info->channels *
  1553. row_info->bit_depth);
  1554. row_info->rowbytes = (png_size_t)((row_info->width *
  1555. row_info->pixel_depth + 7) >> 3);
  1556. }
  1557. }
  1558. #endif
  1559. /* Build a grayscale palette. Palette is assumed to be 1 << bit_depth
  1560. * large of png_color. This lets grayscale images be treated as
  1561. * paletted. Most useful for gamma correction and simplification
  1562. * of code.
  1563. */
  1564. void
  1565. png_build_grayscale_palette(int bit_depth, png_colorp palette)
  1566. {
  1567. int num_palette;
  1568. int color_inc;
  1569. int i;
  1570. int v;
  1571. png_debug(1, "in png_do_build_grayscale_palette\n");
  1572. if (palette == NULL)
  1573. return;
  1574. switch (bit_depth)
  1575. {
  1576. case 1:
  1577. num_palette = 2;
  1578. color_inc = 0xff;
  1579. break;
  1580. case 2:
  1581. num_palette = 4;
  1582. color_inc = 0x55;
  1583. break;
  1584. case 4:
  1585. num_palette = 16;
  1586. color_inc = 0x11;
  1587. break;
  1588. case 8:
  1589. num_palette = 256;
  1590. color_inc = 1;
  1591. break;
  1592. default:
  1593. num_palette = 0;
  1594. color_inc = 0;
  1595. break;
  1596. }
  1597. for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
  1598. {
  1599. palette[i].red = (png_byte)v;
  1600. palette[i].green = (png_byte)v;
  1601. palette[i].blue = (png_byte)v;
  1602. }
  1603. }
  1604. /* This function is currently unused. Do we really need it? */
  1605. #if defined(PNG_READ_DITHER_SUPPORTED) && defined(PNG_CORRECT_PALETTE_SUPPORTED)
  1606. void
  1607. png_correct_palette(png_structp png_ptr, png_colorp palette,
  1608. int num_palette)
  1609. {
  1610. png_debug(1, "in png_correct_palette\n");
  1611. #if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
  1612. if ((png_ptr->transformations & (PNG_GAMMA)) &&
  1613. (png_ptr->transformations & (PNG_BACKGROUND)))
  1614. {
  1615. png_color back, back_1;
  1616. if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_FILE)
  1617. {
  1618. back.red = png_ptr->gamma_table[png_ptr->background.red];
  1619. back.green = png_ptr->gamma_table[png_ptr->background.green];
  1620. back.blue = png_ptr->gamma_table[png_ptr->background.blue];
  1621. back_1.red = png_ptr->gamma_to_1[png_ptr->background.red];
  1622. back_1.green = png_ptr->gamma_to_1[png_ptr->background.green];
  1623. back_1.blue = png_ptr->gamma_to_1[png_ptr->background.blue];
  1624. }
  1625. else
  1626. {
  1627. double g;
  1628. g = 1.0 / (png_ptr->background_gamma * png_ptr->screen_gamma);
  1629. if (png_ptr->background_gamma_type == PNG_BACKGROUND_GAMMA_SCREEN ||
  1630. fabs(g - 1.0) < PNG_GAMMA_THRESHOLD)
  1631. {
  1632. back.red = png_ptr->background.red;
  1633. back.green = png_ptr->background.green;
  1634. back.blue = png_ptr->background.blue;
  1635. }
  1636. else
  1637. {
  1638. back.red =
  1639. (png_byte)(pow((double)png_ptr->background.red/255, g) *
  1640. 255.0 + 0.5);
  1641. back.green =
  1642. (png_byte)(pow((double)png_ptr->background.green/255, g) *
  1643. 255.0 + 0.5);
  1644. back.blue =
  1645. (png_byte)(pow((double)png_ptr->background.blue/255, g) *
  1646. 255.0 + 0.5);
  1647. }
  1648. g = 1.0 / png_ptr->background_gamma;
  1649. back_1.red =
  1650. (png_byte)(pow((double)png_ptr->background.red/255, g) *
  1651. 255.0 + 0.5);
  1652. back_1.green =
  1653. (png_byte)(pow((double)png_ptr->background.green/255, g) *
  1654. 255.0 + 0.5);
  1655. back_1.blue =
  1656. (png_byte)(pow((double)png_ptr->background.blue/255, g) *
  1657. 255.0 + 0.5);
  1658. }
  1659. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1660. {
  1661. png_uint_32 i;
  1662. for (i = 0; i < (png_uint_32)num_palette; i++)
  1663. {
  1664. if (i < png_ptr->num_trans && png_ptr->trans[i] == 0)
  1665. {
  1666. palette[i] = back;
  1667. }
  1668. else if (i < png_ptr->num_trans && png_ptr->trans[i] != 0xff)
  1669. {
  1670. png_byte v, w;
  1671. v = png_ptr->gamma_to_1[png_ptr->palette[i].red];
  1672. png_composite(w, v, png_ptr->trans[i], back_1.red);
  1673. palette[i].red = png_ptr->gamma_from_1[w];
  1674. v = png_ptr->gamma_to_1[png_ptr->palette[i].green];
  1675. png_composite(w, v, png_ptr->trans[i], back_1.green);
  1676. palette[i].green = png_ptr->gamma_from_1[w];
  1677. v = png_ptr->gamma_to_1[png_ptr->palette[i].blue];
  1678. png_composite(w, v, png_ptr->trans[i], back_1.blue);
  1679. palette[i].blue = png_ptr->gamma_from_1[w];
  1680. }
  1681. else
  1682. {
  1683. palette[i].red = png_ptr->gamma_table[palette[i].red];
  1684. palette[i].green = png_ptr->gamma_table[palette[i].green];
  1685. palette[i].blue = png_ptr->gamma_table[palette[i].blue];
  1686. }
  1687. }
  1688. }
  1689. else
  1690. {
  1691. int i;
  1692. for (i = 0; i < num_palette; i++)
  1693. {
  1694. if (palette[i].red == (png_byte)png_ptr->trans_values.gray)
  1695. {
  1696. palette[i] = back;
  1697. }
  1698. else
  1699. {
  1700. palette[i].red = png_ptr->gamma_table[palette[i].red];
  1701. palette[i].green = png_ptr->gamma_table[palette[i].green];
  1702. palette[i].blue = png_ptr->gamma_table[palette[i].blue];
  1703. }
  1704. }
  1705. }
  1706. }
  1707. else
  1708. #endif
  1709. #if defined(PNG_READ_GAMMA_SUPPORTED)
  1710. if (png_ptr->transformations & PNG_GAMMA)
  1711. {
  1712. int i;
  1713. for (i = 0; i < num_palette; i++)
  1714. {
  1715. palette[i].red = png_ptr->gamma_table[palette[i].red];
  1716. palette[i].green = png_ptr->gamma_table[palette[i].green];
  1717. palette[i].blue = png_ptr->gamma_table[palette[i].blue];
  1718. }
  1719. }
  1720. #if defined(PNG_READ_BACKGROUND_SUPPORTED)
  1721. else
  1722. #endif
  1723. #endif
  1724. #if defined(PNG_READ_BACKGROUND_SUPPORTED)
  1725. if (png_ptr->transformations & PNG_BACKGROUND)
  1726. {
  1727. if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
  1728. {
  1729. int i;
  1730. png_color back;
  1731. back.red = (png_byte)png_ptr->background.red;
  1732. back.green = (png_byte)png_ptr->background.green;
  1733. back.blue = (png_byte)png_ptr->background.blue;
  1734. for (i = 0; i < num_palette; i++)
  1735. {
  1736. if (i >= (int)png_ptr->num_trans ||
  1737. png_ptr->trans[i] == 0)
  1738. {
  1739. palette[i].red = back.red;
  1740. palette[i].green = back.green;
  1741. palette[i].blue = back.blue;
  1742. }
  1743. else if (i < (int)png_ptr->num_trans ||
  1744. png_ptr->trans[i] != 0xff)
  1745. {
  1746. png_composite(palette[i].red, png_ptr->palette[i].red,
  1747. png_ptr->trans[i], back.red);
  1748. png_composite(palette[i].green, png_ptr->palette[i].green,
  1749. png_ptr->trans[i], back.green);
  1750. png_composite(palette[i].blue, png_ptr->palette[i].blue,
  1751. png_ptr->trans[i], back.blue);
  1752. }
  1753. }
  1754. }
  1755. else /* assume grayscale palette (what else could it be?) */
  1756. {
  1757. int i;
  1758. for (i = 0; i < num_palette; i++)
  1759. {
  1760. if (i == (png_byte)png_ptr->trans_values.gray)
  1761. {
  1762. palette[i].red = (png_byte)png_ptr->background.red;
  1763. palette[i].green = (png_byte)png_ptr->background.green;
  1764. palette[i].blue = (png_byte)png_ptr->background.blue;
  1765. }
  1766. }
  1767. }
  1768. }
  1769. #endif
  1770. }
  1771. #endif
  1772. #if defined(PNG_READ_BACKGROUND_SUPPORTED)
  1773. /* Replace any alpha or transparency with the supplied background color.
  1774. * "background" is already in the screen gamma, while "background_1" is
  1775. * at a gamma of 1.0. Paletted files have already been taken care of.
  1776. */
  1777. void
  1778. png_do_background(png_row_infop row_info, png_bytep row,
  1779. png_color_16p trans_values, png_color_16p background,
  1780. png_color_16p background_1,
  1781. png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
  1782. png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
  1783. png_uint_16pp gamma_16_to_1, int gamma_shift)
  1784. {
  1785. png_bytep sp, dp;
  1786. png_uint_32 i;
  1787. int shift;
  1788. png_debug(1, "in png_do_background\n");
  1789. if (background != NULL &&
  1790. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  1791. row != NULL && row_info != NULL &&
  1792. #endif
  1793. (!(row_info->color_type & PNG_COLOR_MASK_ALPHA) ||
  1794. (row_info->color_type != PNG_COLOR_TYPE_PALETTE && trans_values)))
  1795. {
  1796. switch (row_info->color_type)
  1797. {
  1798. case PNG_COLOR_TYPE_GRAY:
  1799. {
  1800. /* We currently don't do gamma correction for 2 and 4 bit */
  1801. switch (row_info->bit_depth)
  1802. {
  1803. case 1:
  1804. {
  1805. sp = row;
  1806. shift = 7;
  1807. for (i = 0; i < row_info->width; i++)
  1808. {
  1809. if ((png_uint_16)((*sp >> shift) & 0x1)
  1810. == trans_values->gray)
  1811. {
  1812. *sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
  1813. *sp |= (png_byte)(background->gray << shift);
  1814. }
  1815. if (!shift)
  1816. {
  1817. shift = 7;
  1818. sp++;
  1819. }
  1820. else
  1821. shift--;
  1822. }
  1823. break;
  1824. }
  1825. case 2:
  1826. {
  1827. sp = row;
  1828. shift = 6;
  1829. for (i = 0; i < row_info->width; i++)
  1830. {
  1831. if ((png_uint_16)((*sp >> shift) & 0x3)
  1832. == trans_values->gray)
  1833. {
  1834. *sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
  1835. *sp |= (png_byte)(background->gray << shift);
  1836. }
  1837. if (!shift)
  1838. {
  1839. shift = 6;
  1840. sp++;
  1841. }
  1842. else
  1843. shift -= 2;
  1844. }
  1845. break;
  1846. }
  1847. case 4:
  1848. {
  1849. sp = row;
  1850. shift = 4;
  1851. for (i = 0; i < row_info->width; i++)
  1852. {
  1853. if ((png_uint_16)((*sp >> shift) & 0xf)
  1854. == trans_values->gray)
  1855. {
  1856. *sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
  1857. *sp |= (png_byte)(background->gray << shift);
  1858. }
  1859. if (!shift)
  1860. {
  1861. shift = 4;
  1862. sp++;
  1863. }
  1864. else
  1865. shift -= 4;
  1866. }
  1867. break;
  1868. }
  1869. case 8:
  1870. {
  1871. #if defined(PNG_READ_GAMMA_SUPPORTED)
  1872. if (gamma_table != NULL)
  1873. {
  1874. for (i = 0, sp = row; i < row_info->width; i++, sp++)
  1875. {
  1876. if (*sp == trans_values->gray)
  1877. {
  1878. *sp = (png_byte)background->gray;
  1879. }
  1880. else
  1881. {
  1882. *sp = gamma_table[*sp];
  1883. }
  1884. }
  1885. }
  1886. else
  1887. #endif
  1888. {
  1889. for (i = 0, sp = row; i < row_info->width; i++, sp++)
  1890. {
  1891. if (*sp == trans_values->gray)
  1892. {
  1893. *sp = (png_byte)background->gray;
  1894. }
  1895. }
  1896. }
  1897. break;
  1898. }
  1899. case 16:
  1900. {
  1901. #if defined(PNG_READ_GAMMA_SUPPORTED)
  1902. if (gamma_16 != NULL)
  1903. {
  1904. for (i = 0, sp = row; i < row_info->width; i++, sp += 2)
  1905. {
  1906. png_uint_16 v;
  1907. v = ((png_uint_16)(*sp) << 8) + *(sp + 1);
  1908. if (v == trans_values->gray)
  1909. {
  1910. /* background is already in screen gamma */
  1911. *sp = (png_byte)((background->gray >> 8) & 0xff);
  1912. *(sp + 1) = (png_byte)(background->gray & 0xff);
  1913. }
  1914. else
  1915. {
  1916. v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  1917. *sp = (png_byte)((v >> 8) & 0xff);
  1918. *(sp + 1) = (png_byte)(v & 0xff);
  1919. }
  1920. }
  1921. }
  1922. else
  1923. #endif
  1924. {
  1925. for (i = 0, sp = row; i < row_info->width; i++, sp += 2)
  1926. {
  1927. png_uint_16 v;
  1928. v = ((png_uint_16)(*sp) << 8) + *(sp + 1);
  1929. if (v == trans_values->gray)
  1930. {
  1931. *sp = (png_byte)((background->gray >> 8) & 0xff);
  1932. *(sp + 1) = (png_byte)(background->gray & 0xff);
  1933. }
  1934. }
  1935. }
  1936. break;
  1937. }
  1938. }
  1939. break;
  1940. }
  1941. case PNG_COLOR_TYPE_RGB:
  1942. {
  1943. if (row_info->bit_depth == 8)
  1944. {
  1945. #if defined(PNG_READ_GAMMA_SUPPORTED)
  1946. if (gamma_table != NULL)
  1947. {
  1948. for (i = 0, sp = row; i < row_info->width; i++, sp += 3)
  1949. {
  1950. if (*sp == trans_values->red &&
  1951. *(sp + 1) == trans_values->green &&
  1952. *(sp + 2) == trans_values->blue)
  1953. {
  1954. *sp = (png_byte)background->red;
  1955. *(sp + 1) = (png_byte)background->green;
  1956. *(sp + 2) = (png_byte)background->blue;
  1957. }
  1958. else
  1959. {
  1960. *sp = gamma_table[*sp];
  1961. *(sp + 1) = gamma_table[*(sp + 1)];
  1962. *(sp + 2) = gamma_table[*(sp + 2)];
  1963. }
  1964. }
  1965. }
  1966. else
  1967. #endif
  1968. {
  1969. for (i = 0, sp = row; i < row_info->width; i++, sp += 3)
  1970. {
  1971. if (*sp == trans_values->red &&
  1972. *(sp + 1) == trans_values->green &&
  1973. *(sp + 2) == trans_values->blue)
  1974. {
  1975. *sp = (png_byte)background->red;
  1976. *(sp + 1) = (png_byte)background->green;
  1977. *(sp + 2) = (png_byte)background->blue;
  1978. }
  1979. }
  1980. }
  1981. }
  1982. else /* if (row_info->bit_depth == 16) */
  1983. {
  1984. #if defined(PNG_READ_GAMMA_SUPPORTED)
  1985. if (gamma_16 != NULL)
  1986. {
  1987. for (i = 0, sp = row; i < row_info->width; i++, sp += 6)
  1988. {
  1989. png_uint_16 r, g, b;
  1990. r = ((png_uint_16)(*sp) << 8) + *(sp + 1);
  1991. g = ((png_uint_16)(*(sp + 2)) << 8) + *(sp + 3);
  1992. b = ((png_uint_16)(*(sp + 4)) << 8) + *(sp + 5);
  1993. if (r == trans_values->red && g == trans_values->green &&
  1994. b == trans_values->blue)
  1995. {
  1996. /* background is already in screen gamma */
  1997. *sp = (png_byte)((background->red >> 8) & 0xff);
  1998. *(sp + 1) = (png_byte)(background->red & 0xff);
  1999. *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
  2000. *(sp + 3) = (png_byte)(background->green & 0xff);
  2001. *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
  2002. *(sp + 5) = (png_byte)(background->blue & 0xff);
  2003. }
  2004. else
  2005. {
  2006. png_uint_16 v;
  2007. v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  2008. *sp = (png_byte)((v >> 8) & 0xff);
  2009. *(sp + 1) = (png_byte)(v & 0xff);
  2010. v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
  2011. *(sp + 2) = (png_byte)((v >> 8) & 0xff);
  2012. *(sp + 3) = (png_byte)(v & 0xff);
  2013. v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
  2014. *(sp + 4) = (png_byte)((v >> 8) & 0xff);
  2015. *(sp + 5) = (png_byte)(v & 0xff);
  2016. }
  2017. }
  2018. }
  2019. else
  2020. #endif
  2021. {
  2022. for (i = 0, sp = row; i < row_info->width; i++, sp += 6)
  2023. {
  2024. png_uint_16 r, g, b;
  2025. r = ((png_uint_16)(*sp) << 8) + *(sp + 1);
  2026. g = ((png_uint_16)(*(sp + 2)) << 8) + *(sp + 3);
  2027. b = ((png_uint_16)(*(sp + 4)) << 8) + *(sp + 5);
  2028. if (r == trans_values->red && g == trans_values->green &&
  2029. b == trans_values->blue)
  2030. {
  2031. *sp = (png_byte)((background->red >> 8) & 0xff);
  2032. *(sp + 1) = (png_byte)(background->red & 0xff);
  2033. *(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
  2034. *(sp + 3) = (png_byte)(background->green & 0xff);
  2035. *(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
  2036. *(sp + 5) = (png_byte)(background->blue & 0xff);
  2037. }
  2038. }
  2039. }
  2040. }
  2041. break;
  2042. }
  2043. case PNG_COLOR_TYPE_GRAY_ALPHA:
  2044. {
  2045. if (row_info->bit_depth == 8)
  2046. {
  2047. #if defined(PNG_READ_GAMMA_SUPPORTED)
  2048. if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
  2049. gamma_table != NULL)
  2050. {
  2051. for (i = 0, sp = row, dp = row;
  2052. i < row_info->width; i++, sp += 2, dp++)
  2053. {
  2054. png_uint_16 a;
  2055. a = *(sp + 1);
  2056. if (a == 0xff)
  2057. {
  2058. *dp = gamma_table[*sp];
  2059. }
  2060. else if (a == 0)
  2061. {
  2062. /* background is already in screen gamma */
  2063. *dp = (png_byte)background->gray;
  2064. }
  2065. else
  2066. {
  2067. png_byte v, w;
  2068. v = gamma_to_1[*sp];
  2069. png_composite(w, v, a, background_1->gray);
  2070. *dp = gamma_from_1[w];
  2071. }
  2072. }
  2073. }
  2074. else
  2075. #endif
  2076. {
  2077. for (i = 0, sp = row, dp = row;
  2078. i < row_info->width; i++, sp += 2, dp++)
  2079. {
  2080. png_byte a;
  2081. a = *(sp + 1);
  2082. if (a == 0xff)
  2083. {
  2084. *dp = *sp;
  2085. }
  2086. else if (a == 0)
  2087. {
  2088. *dp = (png_byte)background->gray;
  2089. }
  2090. else
  2091. {
  2092. png_composite(*dp, *sp, a, background_1->gray);
  2093. }
  2094. }
  2095. }
  2096. }
  2097. else /* if (png_ptr->bit_depth == 16) */
  2098. {
  2099. #if defined(PNG_READ_GAMMA_SUPPORTED)
  2100. if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
  2101. gamma_16_to_1 != NULL)
  2102. {
  2103. for (i = 0, sp = row, dp = row;
  2104. i < row_info->width; i++, sp += 4, dp += 2)
  2105. {
  2106. png_uint_16 a;
  2107. a = ((png_uint_16)(*(sp + 2)) << 8) + *(sp + 3);
  2108. if (a == (png_uint_16)0xffff)
  2109. {
  2110. png_uint_16 v;
  2111. v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  2112. *dp = (png_byte)((v >> 8) & 0xff);
  2113. *(dp + 1) = (png_byte)(v & 0xff);
  2114. }
  2115. else if (a == 0)
  2116. {
  2117. /* background is already in screen gamma */
  2118. *dp = (png_byte)((background->gray >> 8) & 0xff);
  2119. *(dp + 1) = (png_byte)(background->gray & 0xff);
  2120. }
  2121. else
  2122. {
  2123. png_uint_16 g, v, w;
  2124. g = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
  2125. png_composite_16(v, g, a, background_1->gray);
  2126. w = gamma_16_from_1[(v&0xff) >> gamma_shift][v >> 8];
  2127. *dp = (png_byte)((w >> 8) & 0xff);
  2128. *(dp + 1) = (png_byte)(w & 0xff);
  2129. }
  2130. }
  2131. }
  2132. else
  2133. #endif
  2134. {
  2135. for (i = 0, sp = row, dp = row;
  2136. i < row_info->width; i++, sp += 4, dp += 2)
  2137. {
  2138. png_uint_16 a;
  2139. a = ((png_uint_16)(*(sp + 2)) << 8) + *(sp + 3);
  2140. if (a == (png_uint_16)0xffff)
  2141. {
  2142. png_memcpy(dp, sp, 2);
  2143. }
  2144. else if (a == 0)
  2145. {
  2146. *dp = (png_byte)((background->gray >> 8) & 0xff);
  2147. *(dp + 1) = (png_byte)(background->gray & 0xff);
  2148. }
  2149. else
  2150. {
  2151. png_uint_16 g, v;
  2152. g = ((png_uint_16)(*sp) << 8) + *(sp + 1);
  2153. png_composite_16(v, g, a, background_1->gray);
  2154. *dp = (png_byte)((v >> 8) & 0xff);
  2155. *(dp + 1) = (png_byte)(v & 0xff);
  2156. }
  2157. }
  2158. }
  2159. }
  2160. break;
  2161. }
  2162. case PNG_COLOR_TYPE_RGB_ALPHA:
  2163. {
  2164. if (row_info->bit_depth == 8)
  2165. {
  2166. #if defined(PNG_READ_GAMMA_SUPPORTED)
  2167. if (gamma_to_1 != NULL && gamma_from_1 != NULL &&
  2168. gamma_table != NULL)
  2169. {
  2170. for (i = 0, sp = row, dp = row;
  2171. i < row_info->width; i++, sp += 4, dp += 3)
  2172. {
  2173. png_byte a;
  2174. a = *(sp + 3);
  2175. if (a == 0xff)
  2176. {
  2177. *dp = gamma_table[*sp];
  2178. *(dp + 1) = gamma_table[*(sp + 1)];
  2179. *(dp + 2) = gamma_table[*(sp + 2)];
  2180. }
  2181. else if (a == 0)
  2182. {
  2183. /* background is already in screen gamma */
  2184. *dp = (png_byte)background->red;
  2185. *(dp + 1) = (png_byte)background->green;
  2186. *(dp + 2) = (png_byte)background->blue;
  2187. }
  2188. else
  2189. {
  2190. png_byte v, w;
  2191. v = gamma_to_1[*sp];
  2192. png_composite(w, v, a, background_1->red);
  2193. *dp = gamma_from_1[w];
  2194. v = gamma_to_1[*(sp + 1)];
  2195. png_composite(w, v, a, background_1->green);
  2196. *(dp + 1) = gamma_from_1[w];
  2197. v = gamma_to_1[*(sp + 2)];
  2198. png_composite(w, v, a, background_1->blue);
  2199. *(dp + 2) = gamma_from_1[w];
  2200. }
  2201. }
  2202. }
  2203. else
  2204. #endif
  2205. {
  2206. for (i = 0, sp = row, dp = row;
  2207. i < row_info->width; i++, sp += 4, dp += 3)
  2208. {
  2209. png_byte a;
  2210. a = *(sp + 3);
  2211. if (a == 0xff)
  2212. {
  2213. *dp = *sp;
  2214. *(dp + 1) = *(sp + 1);
  2215. *(dp + 2) = *(sp + 2);
  2216. }
  2217. else if (a == 0)
  2218. {
  2219. *dp = (png_byte)background->red;
  2220. *(dp + 1) = (png_byte)background->green;
  2221. *(dp + 2) = (png_byte)background->blue;
  2222. }
  2223. else
  2224. {
  2225. png_composite(*dp, *sp, a, background->red);
  2226. png_composite(*(dp + 1), *(sp + 1), a,
  2227. background->green);
  2228. png_composite(*(dp + 2), *(sp + 2), a,
  2229. background->blue);
  2230. }
  2231. }
  2232. }
  2233. }
  2234. else /* if (row_info->bit_depth == 16) */
  2235. {
  2236. #if defined(PNG_READ_GAMMA_SUPPORTED)
  2237. if (gamma_16 != NULL && gamma_16_from_1 != NULL &&
  2238. gamma_16_to_1 != NULL)
  2239. {
  2240. for (i = 0, sp = row, dp = row;
  2241. i < row_info->width; i++, sp += 8, dp += 6)
  2242. {
  2243. png_uint_16 a;
  2244. a = (png_uint_16)(((png_uint_16)(*(sp + 6)) << 8) +
  2245. (png_uint_16)(*(sp + 7)));
  2246. if (a == (png_uint_16)0xffff)
  2247. {
  2248. png_uint_16 v;
  2249. v = gamma_16[*(sp + 1) >> gamma_shift][*sp];
  2250. *dp = (png_byte)((v >> 8) & 0xff);
  2251. *(dp + 1) = (png_byte)(v & 0xff);
  2252. v = gamma_16[*(sp + 3) >> gamma_shift][*(sp + 2)];
  2253. *(dp + 2) = (png_byte)((v >> 8) & 0xff);
  2254. *(dp + 3) = (png_byte)(v & 0xff);
  2255. v = gamma_16[*(sp + 5) >> gamma_shift][*(sp + 4)];
  2256. *(dp + 4) = (png_byte)((v >> 8) & 0xff);
  2257. *(dp + 5) = (png_byte)(v & 0xff);
  2258. }
  2259. else if (a == 0)
  2260. {
  2261. /* background is already in screen gamma */
  2262. *dp = (png_byte)((background->red >> 8) & 0xff);
  2263. *(dp + 1) = (png_byte)(background->red & 0xff);
  2264. *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
  2265. *(dp + 3) = (png_byte)(background->green & 0xff);
  2266. *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
  2267. *(dp + 5) = (png_byte)(background->blue & 0xff);
  2268. }
  2269. else
  2270. {
  2271. png_uint_16 v, w, x;
  2272. v = gamma_16_to_1[*(sp + 1) >> gamma_shift][*sp];
  2273. png_composite_16(w, v, a, background->red);
  2274. x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
  2275. *dp = (png_byte)((x >> 8) & 0xff);
  2276. *(dp + 1) = (png_byte)(x & 0xff);
  2277. v = gamma_16_to_1[*(sp + 3) >> gamma_shift][*(sp + 2)];
  2278. png_composite_16(w, v, a, background->green);
  2279. x = gamma_16_from_1[((w&0xff) >> gamma_shift)][w >> 8];
  2280. *(dp + 2) = (png_byte)((x >> 8) & 0xff);
  2281. *(dp + 3) = (png_byte)(x & 0xff);
  2282. v = gamma_16_to_1[*(sp + 5) >> gamma_shift][*(sp + 4)];
  2283. png_composite_16(w, v, a, background->blue);
  2284. x = gamma_16_from_1[(w & 0xff) >> gamma_shift][w >> 8];
  2285. *(dp + 4) = (png_byte)((x >> 8) & 0xff);
  2286. *(dp + 5) = (png_byte)(x & 0xff);
  2287. }
  2288. }
  2289. }
  2290. else
  2291. #endif
  2292. {
  2293. for (i = 0, sp = row, dp = row;
  2294. i < row_info->width; i++, sp += 8, dp += 6)
  2295. {
  2296. png_uint_16 a;
  2297. a = (png_uint_16)(((png_uint_16)(*(sp + 6)) << 8) +
  2298. (png_uint_16)(*(sp + 7)));
  2299. if (a == (png_uint_16)0xffff)
  2300. {
  2301. png_memcpy(dp, sp, 6);
  2302. }
  2303. else if (a == 0)
  2304. {
  2305. *dp = (png_byte)((background->red >> 8) & 0xff);
  2306. *(dp + 1) = (png_byte)(background->red & 0xff);
  2307. *(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
  2308. *(dp + 3) = (png_byte)(background->green & 0xff);
  2309. *(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
  2310. *(dp + 5) = (png_byte)(background->blue & 0xff);
  2311. }
  2312. else
  2313. {
  2314. png_uint_16 r, g, b, v;
  2315. r = ((png_uint_16)(*sp) << 8) + *(sp + 1);
  2316. g = ((png_uint_16)(*(sp + 2)) << 8) + *(sp + 3);
  2317. b = ((png_uint_16)(*(sp + 4)) << 8) + *(sp + 5);
  2318. png_composite_16(v, r, a, background->red);
  2319. *dp = (png_byte)((v >> 8) & 0xff);
  2320. *(dp + 1) = (png_byte)(v & 0xff);
  2321. png_composite_16(v, g, a, background->green);
  2322. *(dp + 2) = (png_byte)((v >> 8) & 0xff);
  2323. *(dp + 3) = (png_byte)(v & 0xff);
  2324. png_composite_16(v, b, a, background->blue);
  2325. *(dp + 4) = (png_byte)((v >> 8) & 0xff);
  2326. *(dp + 5) = (png_byte)(v & 0xff);
  2327. }
  2328. }
  2329. }
  2330. }
  2331. break;
  2332. }
  2333. }
  2334. if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
  2335. {
  2336. row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
  2337. row_info->channels--;
  2338. row_info->pixel_depth = (png_byte)(row_info->channels *
  2339. row_info->bit_depth);
  2340. row_info->rowbytes = (png_size_t)((row_info->width *
  2341. row_info->pixel_depth + 7) >> 3);
  2342. }
  2343. }
  2344. }
  2345. #endif
  2346. #if defined(PNG_READ_GAMMA_SUPPORTED)
  2347. /* Gamma correct the image, avoiding the alpha channel. Make sure
  2348. * you do this after you deal with the trasparency issue on grayscale
  2349. * or rgb images. If your bit depth is 8, use gamma_table, if it
  2350. * is 16, use gamma_16_table and gamma_shift. Build these with
  2351. * build_gamma_table().
  2352. */
  2353. void
  2354. png_do_gamma(png_row_infop row_info, png_bytep row,
  2355. png_bytep gamma_table, png_uint_16pp gamma_16_table,
  2356. int gamma_shift)
  2357. {
  2358. png_bytep sp;
  2359. png_uint_32 i;
  2360. png_debug(1, "in png_do_gamma\n");
  2361. if (
  2362. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  2363. row != NULL && row_info != NULL &&
  2364. #endif
  2365. ((row_info->bit_depth <= 8 && gamma_table != NULL) ||
  2366. (row_info->bit_depth == 16 && gamma_16_table != NULL)))
  2367. {
  2368. switch (row_info->color_type)
  2369. {
  2370. case PNG_COLOR_TYPE_RGB:
  2371. {
  2372. if (row_info->bit_depth == 8)
  2373. {
  2374. for (i = 0, sp = row; i < row_info->width; i++)
  2375. {
  2376. *sp = gamma_table[*sp];
  2377. sp++;
  2378. *sp = gamma_table[*sp];
  2379. sp++;
  2380. *sp = gamma_table[*sp];
  2381. sp++;
  2382. }
  2383. }
  2384. else /* if (row_info->bit_depth == 16) */
  2385. {
  2386. for (i = 0, sp = row; i < row_info->width; i++)
  2387. {
  2388. png_uint_16 v;
  2389. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  2390. *sp = (png_byte)((v >> 8) & 0xff);
  2391. *(sp + 1) = (png_byte)(v & 0xff);
  2392. sp += 2;
  2393. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  2394. *sp = (png_byte)((v >> 8) & 0xff);
  2395. *(sp + 1) = (png_byte)(v & 0xff);
  2396. sp += 2;
  2397. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  2398. *sp = (png_byte)((v >> 8) & 0xff);
  2399. *(sp + 1) = (png_byte)(v & 0xff);
  2400. sp += 2;
  2401. }
  2402. }
  2403. break;
  2404. }
  2405. case PNG_COLOR_TYPE_RGB_ALPHA:
  2406. {
  2407. if (row_info->bit_depth == 8)
  2408. {
  2409. for (i = 0, sp = row;
  2410. i < row_info->width; i++)
  2411. {
  2412. *sp = gamma_table[*sp];
  2413. sp++;
  2414. *sp = gamma_table[*sp];
  2415. sp++;
  2416. *sp = gamma_table[*sp];
  2417. sp++;
  2418. sp++;
  2419. }
  2420. }
  2421. else /* if (row_info->bit_depth == 16) */
  2422. {
  2423. for (i = 0, sp = row;
  2424. i < row_info->width; i++)
  2425. {
  2426. png_uint_16 v;
  2427. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  2428. *sp = (png_byte)((v >> 8) & 0xff);
  2429. *(sp + 1) = (png_byte)(v & 0xff);
  2430. sp += 2;
  2431. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  2432. *sp = (png_byte)((v >> 8) & 0xff);
  2433. *(sp + 1) = (png_byte)(v & 0xff);
  2434. sp += 2;
  2435. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  2436. *sp = (png_byte)((v >> 8) & 0xff);
  2437. *(sp + 1) = (png_byte)(v & 0xff);
  2438. sp += 4;
  2439. }
  2440. }
  2441. break;
  2442. }
  2443. case PNG_COLOR_TYPE_GRAY_ALPHA:
  2444. {
  2445. if (row_info->bit_depth == 8)
  2446. {
  2447. for (i = 0, sp = row;
  2448. i < row_info->width; i++)
  2449. {
  2450. *sp = gamma_table[*sp];
  2451. sp += 2;
  2452. }
  2453. }
  2454. else /* if (row_info->bit_depth == 16) */
  2455. {
  2456. for (i = 0, sp = row;
  2457. i < row_info->width; i++)
  2458. {
  2459. png_uint_16 v;
  2460. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  2461. *sp = (png_byte)((v >> 8) & 0xff);
  2462. *(sp + 1) = (png_byte)(v & 0xff);
  2463. sp += 4;
  2464. }
  2465. }
  2466. break;
  2467. }
  2468. case PNG_COLOR_TYPE_GRAY:
  2469. {
  2470. if (row_info->bit_depth == 2)
  2471. {
  2472. for (i = 0, sp = row; i < row_info->width; i += 4)
  2473. {
  2474. int a = *sp & 0xc0;
  2475. int b = *sp & 0x30;
  2476. int c = *sp & 0x0c;
  2477. int d = *sp & 0x03;
  2478. *sp = ((((int)gamma_table[a|(a>>2)|(a>>4)|(a>>6)]) ) & 0xc0)|
  2479. ((((int)gamma_table[(b<<2)|b|(b>>2)|(b>>4)])>>2) & 0x30)|
  2480. ((((int)gamma_table[(c<<4)|(c<<2)|c|(c>>2)])>>4) & 0x0c)|
  2481. ((((int)gamma_table[(d<<6)|(d<<4)|(d<<2)|d])>>6) );
  2482. sp++;
  2483. }
  2484. }
  2485. if (row_info->bit_depth == 4)
  2486. {
  2487. for (i = 0, sp = row; i < row_info->width; i += 2)
  2488. {
  2489. int msb = *sp & 0xf0;
  2490. int lsb = *sp & 0x0f;
  2491. *sp = (((int)gamma_table[msb | (msb >> 4)]) & 0xf0) |
  2492. (((int)gamma_table[(lsb << 4) | lsb]) >> 4);
  2493. sp++;
  2494. }
  2495. }
  2496. else if (row_info->bit_depth == 8)
  2497. {
  2498. for (i = 0, sp = row; i < row_info->width; i++)
  2499. {
  2500. *sp = gamma_table[*sp];
  2501. sp++;
  2502. }
  2503. }
  2504. else if (row_info->bit_depth == 16)
  2505. {
  2506. for (i = 0, sp = row; i < row_info->width; i++)
  2507. {
  2508. png_uint_16 v;
  2509. v = gamma_16_table[*(sp + 1) >> gamma_shift][*sp];
  2510. *sp = (png_byte)((v >> 8) & 0xff);
  2511. *(sp + 1) = (png_byte)(v & 0xff);
  2512. sp += 2;
  2513. }
  2514. }
  2515. break;
  2516. }
  2517. }
  2518. }
  2519. }
  2520. #endif
  2521. #if defined(PNG_READ_EXPAND_SUPPORTED)
  2522. /* Expands a palette row to an rgb or rgba row depending
  2523. * upon whether you supply trans and num_trans.
  2524. */
  2525. void
  2526. png_do_expand_palette(png_row_infop row_info, png_bytep row,
  2527. png_colorp palette, png_bytep trans, int num_trans)
  2528. {
  2529. int shift, value;
  2530. png_bytep sp, dp;
  2531. png_uint_32 i;
  2532. png_debug(1, "in png_do_expand_palette\n");
  2533. if (
  2534. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  2535. row != NULL && row_info != NULL &&
  2536. #endif
  2537. row_info->color_type == PNG_COLOR_TYPE_PALETTE)
  2538. {
  2539. if (row_info->bit_depth < 8)
  2540. {
  2541. switch (row_info->bit_depth)
  2542. {
  2543. case 1:
  2544. {
  2545. sp = row + (png_size_t)((row_info->width - 1) >> 3);
  2546. dp = row + (png_size_t)row_info->width - 1;
  2547. shift = 7 - (int)((row_info->width + 7) & 7);
  2548. for (i = 0; i < row_info->width; i++)
  2549. {
  2550. if ((*sp >> shift) & 0x1)
  2551. *dp = 1;
  2552. else
  2553. *dp = 0;
  2554. if (shift == 7)
  2555. {
  2556. shift = 0;
  2557. sp--;
  2558. }
  2559. else
  2560. shift++;
  2561. dp--;
  2562. }
  2563. break;
  2564. }
  2565. case 2:
  2566. {
  2567. sp = row + (png_size_t)((row_info->width - 1) >> 2);
  2568. dp = row + (png_size_t)row_info->width - 1;
  2569. shift = (int)((3 - ((row_info->width + 3) & 3)) << 1);
  2570. for (i = 0; i < row_info->width; i++)
  2571. {
  2572. value = (*sp >> shift) & 0x3;
  2573. *dp = (png_byte)value;
  2574. if (shift == 6)
  2575. {
  2576. shift = 0;
  2577. sp--;
  2578. }
  2579. else
  2580. shift += 2;
  2581. dp--;
  2582. }
  2583. break;
  2584. }
  2585. case 4:
  2586. {
  2587. sp = row + (png_size_t)((row_info->width - 1) >> 1);
  2588. dp = row + (png_size_t)row_info->width - 1;
  2589. shift = (int)((row_info->width & 1) << 2);
  2590. for (i = 0; i < row_info->width; i++)
  2591. {
  2592. value = (*sp >> shift) & 0xf;
  2593. *dp = (png_byte)value;
  2594. if (shift == 4)
  2595. {
  2596. shift = 0;
  2597. sp--;
  2598. }
  2599. else
  2600. shift += 4;
  2601. dp--;
  2602. }
  2603. break;
  2604. }
  2605. }
  2606. row_info->bit_depth = 8;
  2607. row_info->pixel_depth = 8;
  2608. row_info->rowbytes = (png_size_t)row_info->width;
  2609. }
  2610. switch (row_info->bit_depth)
  2611. {
  2612. case 8:
  2613. {
  2614. if (trans != NULL)
  2615. {
  2616. sp = row + (png_size_t)row_info->width - 1;
  2617. dp = row + (png_size_t)(row_info->width << 2) - 1;
  2618. for (i = 0; i < row_info->width; i++)
  2619. {
  2620. if ((int)(*sp) >= num_trans)
  2621. *dp-- = 0xff;
  2622. else
  2623. *dp-- = trans[*sp];
  2624. *dp-- = palette[*sp].blue;
  2625. *dp-- = palette[*sp].green;
  2626. *dp-- = palette[*sp].red;
  2627. sp--;
  2628. }
  2629. row_info->bit_depth = 8;
  2630. row_info->pixel_depth = 32;
  2631. row_info->rowbytes = (png_size_t)row_info->width * 4;
  2632. row_info->color_type = 6;
  2633. row_info->channels = 4;
  2634. }
  2635. else
  2636. {
  2637. sp = row + (png_size_t)row_info->width - 1;
  2638. dp = row + (png_size_t)(row_info->width * 3) - 1;
  2639. for (i = 0; i < row_info->width; i++)
  2640. {
  2641. *dp-- = palette[*sp].blue;
  2642. *dp-- = palette[*sp].green;
  2643. *dp-- = palette[*sp].red;
  2644. sp--;
  2645. }
  2646. row_info->bit_depth = 8;
  2647. row_info->pixel_depth = 24;
  2648. row_info->rowbytes = (png_size_t)row_info->width * 3;
  2649. row_info->color_type = 2;
  2650. row_info->channels = 3;
  2651. }
  2652. break;
  2653. }
  2654. }
  2655. }
  2656. }
  2657. /* If the bit depth < 8, it is expanded to 8. Also, if the
  2658. * transparency value is supplied, an alpha channel is built.
  2659. */
  2660. void
  2661. png_do_expand(png_row_infop row_info, png_bytep row,
  2662. png_color_16p trans_value)
  2663. {
  2664. int shift, value;
  2665. png_bytep sp, dp;
  2666. png_uint_32 i;
  2667. png_debug(1, "in png_do_expand\n");
  2668. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  2669. if (row != NULL && row_info != NULL)
  2670. #endif
  2671. {
  2672. if (row_info->color_type == PNG_COLOR_TYPE_GRAY)
  2673. {
  2674. png_uint_16 gray = trans_value ? trans_value->gray : 0;
  2675. if (row_info->bit_depth < 8)
  2676. {
  2677. switch (row_info->bit_depth)
  2678. {
  2679. case 1:
  2680. {
  2681. gray *= 0xff;
  2682. sp = row + (png_size_t)((row_info->width - 1) >> 3);
  2683. dp = row + (png_size_t)row_info->width - 1;
  2684. shift = 7 - (int)((row_info->width + 7) & 7);
  2685. for (i = 0; i < row_info->width; i++)
  2686. {
  2687. if ((*sp >> shift) & 0x1)
  2688. *dp = 0xff;
  2689. else
  2690. *dp = 0;
  2691. if (shift == 7)
  2692. {
  2693. shift = 0;
  2694. sp--;
  2695. }
  2696. else
  2697. shift++;
  2698. dp--;
  2699. }
  2700. break;
  2701. }
  2702. case 2:
  2703. {
  2704. gray *= 0x55;
  2705. sp = row + (png_size_t)((row_info->width - 1) >> 2);
  2706. dp = row + (png_size_t)row_info->width - 1;
  2707. shift = (int)((3 - ((row_info->width + 3) & 3)) << 1);
  2708. for (i = 0; i < row_info->width; i++)
  2709. {
  2710. value = (*sp >> shift) & 0x3;
  2711. *dp = (png_byte)(value | (value << 2) | (value << 4) |
  2712. (value << 6));
  2713. if (shift == 6)
  2714. {
  2715. shift = 0;
  2716. sp--;
  2717. }
  2718. else
  2719. shift += 2;
  2720. dp--;
  2721. }
  2722. break;
  2723. }
  2724. case 4:
  2725. {
  2726. gray *= 0x11;
  2727. sp = row + (png_size_t)((row_info->width - 1) >> 1);
  2728. dp = row + (png_size_t)row_info->width - 1;
  2729. shift = (int)((1 - ((row_info->width + 1) & 1)) << 2);
  2730. for (i = 0; i < row_info->width; i++)
  2731. {
  2732. value = (*sp >> shift) & 0xf;
  2733. *dp = (png_byte)(value | (value << 4));
  2734. if (shift == 4)
  2735. {
  2736. shift = 0;
  2737. sp--;
  2738. }
  2739. else
  2740. shift = 4;
  2741. dp--;
  2742. }
  2743. break;
  2744. }
  2745. }
  2746. row_info->bit_depth = 8;
  2747. row_info->pixel_depth = 8;
  2748. row_info->rowbytes = (png_size_t)row_info->width;
  2749. }
  2750. if (trans_value != NULL)
  2751. {
  2752. if (row_info->bit_depth == 8)
  2753. {
  2754. sp = row + (png_size_t)row_info->width - 1;
  2755. dp = row + (png_size_t)(row_info->width << 1) - 1;
  2756. for (i = 0; i < row_info->width; i++)
  2757. {
  2758. if (*sp == gray)
  2759. *dp-- = 0;
  2760. else
  2761. *dp-- = 0xff;
  2762. *dp-- = *sp--;
  2763. }
  2764. }
  2765. else if (row_info->bit_depth == 16)
  2766. {
  2767. sp = row + row_info->rowbytes - 1;
  2768. dp = row + (row_info->rowbytes << 1) - 1;
  2769. for (i = 0; i < row_info->width; i++)
  2770. {
  2771. if (((png_uint_16)*(sp) |
  2772. ((png_uint_16)*(sp - 1) << 8)) == gray)
  2773. {
  2774. *dp-- = 0;
  2775. *dp-- = 0;
  2776. }
  2777. else
  2778. {
  2779. *dp-- = 0xff;
  2780. *dp-- = 0xff;
  2781. }
  2782. *dp-- = *sp--;
  2783. *dp-- = *sp--;
  2784. }
  2785. }
  2786. row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
  2787. row_info->channels = 2;
  2788. row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
  2789. row_info->rowbytes =
  2790. (png_size_t)((row_info->width * row_info->pixel_depth) >> 3);
  2791. }
  2792. }
  2793. else if (row_info->color_type == PNG_COLOR_TYPE_RGB && trans_value)
  2794. {
  2795. if (row_info->bit_depth == 8)
  2796. {
  2797. sp = row + (png_size_t)row_info->rowbytes - 1;
  2798. dp = row + (png_size_t)(row_info->width << 2) - 1;
  2799. for (i = 0; i < row_info->width; i++)
  2800. {
  2801. if (*(sp - 2) == trans_value->red &&
  2802. *(sp - 1) == trans_value->green &&
  2803. *(sp - 0) == trans_value->blue)
  2804. *dp-- = 0;
  2805. else
  2806. *dp-- = 0xff;
  2807. *dp-- = *sp--;
  2808. *dp-- = *sp--;
  2809. *dp-- = *sp--;
  2810. }
  2811. }
  2812. else if (row_info->bit_depth == 16)
  2813. {
  2814. sp = row + row_info->rowbytes - 1;
  2815. dp = row + (png_size_t)(row_info->width << 3) - 1;
  2816. for (i = 0; i < row_info->width; i++)
  2817. {
  2818. if ((((png_uint_16)*(sp - 4) |
  2819. ((png_uint_16)*(sp - 5) << 8)) == trans_value->red) &&
  2820. (((png_uint_16)*(sp - 2) |
  2821. ((png_uint_16)*(sp - 3) << 8)) == trans_value->green) &&
  2822. (((png_uint_16)*(sp - 0) |
  2823. ((png_uint_16)*(sp - 1) << 8)) == trans_value->blue))
  2824. {
  2825. *dp-- = 0;
  2826. *dp-- = 0;
  2827. }
  2828. else
  2829. {
  2830. *dp-- = 0xff;
  2831. *dp-- = 0xff;
  2832. }
  2833. *dp-- = *sp--;
  2834. *dp-- = *sp--;
  2835. *dp-- = *sp--;
  2836. *dp-- = *sp--;
  2837. *dp-- = *sp--;
  2838. *dp-- = *sp--;
  2839. }
  2840. }
  2841. row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
  2842. row_info->channels = 4;
  2843. row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
  2844. row_info->rowbytes =
  2845. (png_size_t)((row_info->width * row_info->pixel_depth) >> 3);
  2846. }
  2847. }
  2848. }
  2849. #endif
  2850. #if defined(PNG_READ_DITHER_SUPPORTED)
  2851. void
  2852. png_do_dither(png_row_infop row_info, png_bytep row,
  2853. png_bytep palette_lookup, png_bytep dither_lookup)
  2854. {
  2855. png_bytep sp, dp;
  2856. png_uint_32 i;
  2857. png_debug(1, "in png_do_dither\n");
  2858. #if defined(PNG_USELESS_TESTS_SUPPORTED)
  2859. if (row != NULL && row_info != NULL)
  2860. #endif
  2861. {
  2862. if (row_info->color_type == PNG_COLOR_TYPE_RGB &&
  2863. palette_lookup && row_info->bit_depth == 8)
  2864. {
  2865. png_uint_32 rowbytes;
  2866. int r, g, b, p;
  2867. sp = row;
  2868. dp = row;
  2869. for (i = 0; i < row_info->width; i++)
  2870. {
  2871. r = *sp++;
  2872. g = *sp++;
  2873. b = *sp++;
  2874. /* this looks real messy, but the compiler will reduce
  2875. it down to a reasonable formula. For example, with
  2876. 5 bits per color, we get:
  2877. p = (((r >> 3) & 0x1f) << 10) |
  2878. (((g >> 3) & 0x1f) << 5) |
  2879. ((b >> 3) & 0x1f);
  2880. */
  2881. p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
  2882. ((1 << PNG_DITHER_RED_BITS) - 1)) <<
  2883. (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
  2884. (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
  2885. ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
  2886. (PNG_DITHER_BLUE_BITS)) |
  2887. ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
  2888. ((1 << PNG_DITHER_BLUE_BITS) - 1));
  2889. *dp++ = palette_lookup[p];
  2890. }
  2891. row_info->color_type = PNG_COLOR_TYPE_PALETTE;
  2892. row_info->channels = 1;
  2893. row_info->pixel_depth = row_info->bit_depth;
  2894. rowbytes = ((row_info->width * row_info->pixel_depth + 7) >> 3);
  2895. row_info->rowbytes = (png_size_t)rowbytes;
  2896. if((png_uint_32)row_info->rowbytes != rowbytes)
  2897. /* we should call png_error here, but we don't have png_ptr */
  2898. row_info->rowbytes = (png_size_t)0;
  2899. }
  2900. else if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA &&
  2901. palette_lookup != NULL && row_info->bit_depth == 8)
  2902. {
  2903. png_uint_32 rowbytes;
  2904. int r, g, b, p;
  2905. sp = row;
  2906. dp = row;
  2907. for (i = 0; i < row_info->width; i++)
  2908. {
  2909. r = *sp++;
  2910. g = *sp++;
  2911. b = *sp++;
  2912. sp++;
  2913. p = (((r >> (8 - PNG_DITHER_RED_BITS)) &
  2914. ((1 << PNG_DITHER_RED_BITS) - 1)) <<
  2915. (PNG_DITHER_GREEN_BITS + PNG_DITHER_BLUE_BITS)) |
  2916. (((g >> (8 - PNG_DITHER_GREEN_BITS)) &
  2917. ((1 << PNG_DITHER_GREEN_BITS) - 1)) <<
  2918. (PNG_DITHER_BLUE_BITS)) |
  2919. ((b >> (8 - PNG_DITHER_BLUE_BITS)) &
  2920. ((1 << PNG_DITHER_BLUE_BITS) - 1));
  2921. *dp++ = palette_lookup[p];
  2922. }
  2923. row_info->color_type = PNG_COLOR_TYPE_PALETTE;
  2924. row_info->channels = 1;
  2925. row_info->pixel_depth = row_info->bit_depth;
  2926. rowbytes = ((row_info->width * row_info->pixel_depth + 7) >> 3);
  2927. row_info->rowbytes = (png_size_t)rowbytes;
  2928. if((png_uint_32)row_info->rowbytes != rowbytes)
  2929. /* we should call png_error here, but we don't have png_ptr */
  2930. row_info->rowbytes = (png_size_t)0;
  2931. }
  2932. else if (row_info->color_type == PNG_COLOR_TYPE_PALETTE &&
  2933. dither_lookup && row_info->bit_depth == 8)
  2934. {
  2935. sp = row;
  2936. for (i = 0; i < row_info->width; i++, sp++)
  2937. {
  2938. *sp = dither_lookup[*sp];
  2939. }
  2940. }
  2941. }
  2942. }
  2943. #endif
  2944. #if defined(PNG_READ_GAMMA_SUPPORTED)
  2945. static int png_gamma_shift[] =
  2946. {0x10, 0x21, 0x42, 0x84, 0x110, 0x248, 0x550, 0xff0};
  2947. /* We build the 8- or 16-bit gamma tables here. Note that for 16-bit
  2948. * tables, we don't make a full table if we are reducing to 8-bit in
  2949. * the future. Note also how the gamma_16 tables are segmented so that
  2950. * we don't need to allocate > 64K chunks for a full 16-bit table.
  2951. */
  2952. void
  2953. png_build_gamma_table(png_structp png_ptr)
  2954. {
  2955. png_debug(1, "in png_build_gamma_table\n");
  2956. if (png_ptr->bit_depth <= 8)
  2957. {
  2958. int i;
  2959. double g;
  2960. g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
  2961. png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr,
  2962. (png_uint_32)256);
  2963. for (i = 0; i < 256; i++)
  2964. {
  2965. png_ptr->gamma_table[i] = (png_byte)(pow((double)i / 255.0,
  2966. g) * 255.0 + .5);
  2967. }
  2968. #if defined(PNG_READ_BACKGROUND_SUPPORTED)
  2969. if (png_ptr->transformations & PNG_BACKGROUND)
  2970. {
  2971. g = 1.0 / (png_ptr->gamma);
  2972. png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr,
  2973. (png_uint_32)256);
  2974. for (i = 0; i < 256; i++)
  2975. {
  2976. png_ptr->gamma_to_1[i] = (png_byte)(pow((double)i / 255.0,
  2977. g) * 255.0 + .5);
  2978. }
  2979. g = 1.0 / (png_ptr->screen_gamma);
  2980. png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr,
  2981. (png_uint_32)256);
  2982. for (i = 0; i < 256; i++)
  2983. {
  2984. png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0,
  2985. g) * 255.0 + .5);
  2986. }
  2987. }
  2988. #endif /* PNG_BACKGROUND_SUPPORTED */
  2989. }
  2990. else
  2991. {
  2992. double g;
  2993. int i, j, shift, num;
  2994. int sig_bit;
  2995. png_uint_32 ig;
  2996. if (png_ptr->color_type & PNG_COLOR_MASK_COLOR)
  2997. {
  2998. sig_bit = (int)png_ptr->sig_bit.red;
  2999. if ((int)png_ptr->sig_bit.green > sig_bit)
  3000. sig_bit = png_ptr->sig_bit.green;
  3001. if ((int)png_ptr->sig_bit.blue > sig_bit)
  3002. sig_bit = png_ptr->sig_bit.blue;
  3003. }
  3004. else
  3005. {
  3006. sig_bit = (int)png_ptr->sig_bit.gray;
  3007. }
  3008. if (sig_bit > 0)
  3009. shift = 16 - sig_bit;
  3010. else
  3011. shift = 0;
  3012. if (png_ptr->transformations & PNG_16_TO_8)
  3013. {
  3014. if (shift < (16 - PNG_MAX_GAMMA_8))
  3015. shift = (16 - PNG_MAX_GAMMA_8);
  3016. }
  3017. if (shift > 8)
  3018. shift = 8;
  3019. if (shift < 0)
  3020. shift = 0;
  3021. png_ptr->gamma_shift = (png_byte)shift;
  3022. num = (1 << (8 - shift));
  3023. g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
  3024. png_ptr->gamma_16_table = (png_uint_16pp)png_malloc(png_ptr,
  3025. (png_uint_32)(num * sizeof (png_uint_16p)));
  3026. if ((png_ptr->transformations & PNG_16_TO_8) &&
  3027. !(png_ptr->transformations & PNG_BACKGROUND))
  3028. {
  3029. double fin, fout;
  3030. png_uint_32 last, max;
  3031. for (i = 0; i < num; i++)
  3032. {
  3033. png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
  3034. (png_uint_32)(256 * sizeof (png_uint_16)));
  3035. }
  3036. g = 1.0 / g;
  3037. last = 0;
  3038. for (i = 0; i < 256; i++)
  3039. {
  3040. fout = ((double)i + 0.5) / 256.0;
  3041. fin = pow(fout, g);
  3042. max = (png_uint_32)(fin * (double)((png_uint_32)num << 8));
  3043. while (last <= max)
  3044. {
  3045. png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
  3046. [(int)(last >> (8 - shift))] = (png_uint_16)(
  3047. (png_uint_16)i | ((png_uint_16)i << 8));
  3048. last++;
  3049. }
  3050. }
  3051. while (last < ((png_uint_32)num << 8))
  3052. {
  3053. png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
  3054. [(int)(last >> (8 - shift))] = (png_uint_16)65535L;
  3055. last++;
  3056. }
  3057. }
  3058. else
  3059. {
  3060. for (i = 0; i < num; i++)
  3061. {
  3062. png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
  3063. (png_uint_32)(256 * sizeof (png_uint_16)));
  3064. ig = (((png_uint_32)i * (png_uint_32)png_gamma_shift[shift]) >> 4);
  3065. for (j = 0; j < 256; j++)
  3066. {
  3067. png_ptr->gamma_16_table[i][j] =
  3068. (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
  3069. 65535.0, g) * 65535.0 + .5);
  3070. }
  3071. }
  3072. }
  3073. #if defined(PNG_READ_BACKGROUND_SUPPORTED)
  3074. if (png_ptr->transformations & PNG_BACKGROUND)
  3075. {
  3076. g = 1.0 / (png_ptr->gamma);
  3077. png_ptr->gamma_16_to_1 = (png_uint_16pp)png_malloc(png_ptr,
  3078. (png_uint_32)(num * sizeof (png_uint_16p )));
  3079. for (i = 0; i < num; i++)
  3080. {
  3081. png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr,
  3082. (png_uint_32)(256 * sizeof (png_uint_16)));
  3083. ig = (((png_uint_32)i *
  3084. (png_uint_32)png_gamma_shift[shift]) >> 4);
  3085. for (j = 0; j < 256; j++)
  3086. {
  3087. png_ptr->gamma_16_to_1[i][j] =
  3088. (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
  3089. 65535.0, g) * 65535.0 + .5);
  3090. }
  3091. }
  3092. g = 1.0 / (png_ptr->screen_gamma);
  3093. png_ptr->gamma_16_from_1 = (png_uint_16pp)png_malloc(png_ptr,
  3094. (png_uint_32)(num * sizeof (png_uint_16p)));
  3095. for (i = 0; i < num; i++)
  3096. {
  3097. png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr,
  3098. (png_uint_32)(256 * sizeof (png_uint_16)));
  3099. ig = (((png_uint_32)i *
  3100. (png_uint_32)png_gamma_shift[shift]) >> 4);
  3101. for (j = 0; j < 256; j++)
  3102. {
  3103. png_ptr->gamma_16_from_1[i][j] =
  3104. (png_uint_16)(pow((double)(ig + ((png_uint_32)j << 8)) /
  3105. 65535.0, g) * 65535.0 + .5);
  3106. }
  3107. }
  3108. }
  3109. #endif /* PNG_BACKGROUND_SUPPORTED */
  3110. }
  3111. }
  3112. #endif