Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

jquant2.c 49KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. /*
  2. * jquant2.c
  3. *
  4. * Copyright (C) 1991-1996, Thomas G. Lane.
  5. * This file is part of the Independent JPEG Group's software.
  6. * For conditions of distribution and use, see the accompanying README file.
  7. *
  8. * This file contains 2-pass color quantization (color mapping) routines.
  9. * These routines provide selection of a custom color map for an image,
  10. * followed by mapping of the image to that color map, with optional
  11. * Floyd-Steinberg dithering.
  12. * It is also possible to use just the second pass to map to an arbitrary
  13. * externally-given color map.
  14. *
  15. * Note: ordered dithering is not supported, since there isn't any fast
  16. * way to compute intercolor distances; it's unclear that ordered dither's
  17. * fundamental assumptions even hold with an irregularly spaced color map.
  18. */
  19. #define JPEG_INTERNALS
  20. #include "jinclude.h"
  21. #include "jpeglib.h"
  22. #ifdef QUANT_2PASS_SUPPORTED
  23. /*
  24. * This module implements the well-known Heckbert paradigm for color
  25. * quantization. Most of the ideas used here can be traced back to
  26. * Heckbert's seminal paper
  27. * Heckbert, Paul. "Color Image Quantization for Frame Buffer Display",
  28. * Proc. SIGGRAPH '82, Computer Graphics v.16 #3 (July 1982), pp 297-304.
  29. *
  30. * In the first pass over the image, we accumulate a histogram showing the
  31. * usage count of each possible color. To keep the histogram to a reasonable
  32. * size, we reduce the precision of the input; typical practice is to retain
  33. * 5 or 6 bits per color, so that 8 or 4 different input values are counted
  34. * in the same histogram cell.
  35. *
  36. * Next, the color-selection step begins with a box representing the whole
  37. * color space, and repeatedly splits the "largest" remaining box until we
  38. * have as many boxes as desired colors. Then the mean color in each
  39. * remaining box becomes one of the possible output colors.
  40. *
  41. * The second pass over the image maps each input pixel to the closest output
  42. * color (optionally after applying a Floyd-Steinberg dithering correction).
  43. * This mapping is logically trivial, but making it go fast enough requires
  44. * considerable care.
  45. *
  46. * Heckbert-style quantizers vary a good deal in their policies for choosing
  47. * the "largest" box and deciding where to cut it. The particular policies
  48. * used here have proved out well in experimental comparisons, but better ones
  49. * may yet be found.
  50. *
  51. * In earlier versions of the IJG code, this module quantized in YCbCr color
  52. * space, processing the raw upsampled data without a color conversion step.
  53. * This allowed the color conversion math to be done only once per colormap
  54. * entry, not once per pixel. However, that optimization precluded other
  55. * useful optimizations (such as merging color conversion with upsampling)
  56. * and it also interfered with desired capabilities such as quantizing to an
  57. * externally-supplied colormap. We have therefore abandoned that approach.
  58. * The present code works in the post-conversion color space, typically RGB.
  59. *
  60. * To improve the visual quality of the results, we actually work in scaled
  61. * RGB space, giving G distances more weight than R, and R in turn more than
  62. * B. To do everything in integer math, we must use integer scale factors.
  63. * The 2/3/1 scale factors used here correspond loosely to the relative
  64. * weights of the colors in the NTSC grayscale equation.
  65. * If you want to use this code to quantize a non-RGB color space, you'll
  66. * probably need to change these scale factors.
  67. */
  68. #define R_SCALE 2 /* scale R distances by this much */
  69. #define G_SCALE 3 /* scale G distances by this much */
  70. #define B_SCALE 1 /* and B by this much */
  71. /* Relabel R/G/B as components 0/1/2, respecting the RGB ordering defined
  72. * in jmorecfg.h. As the code stands, it will do the right thing for R,G,B
  73. * and B,G,R orders. If you define some other weird order in jmorecfg.h,
  74. * you'll get compile errors until you extend this logic. In that case
  75. * you'll probably want to tweak the histogram sizes too.
  76. */
  77. #if RGB_RED == 0
  78. #define C0_SCALE R_SCALE
  79. #endif
  80. #if RGB_BLUE == 0
  81. #define C0_SCALE B_SCALE
  82. #endif
  83. #if RGB_GREEN == 1
  84. #define C1_SCALE G_SCALE
  85. #endif
  86. #if RGB_RED == 2
  87. #define C2_SCALE R_SCALE
  88. #endif
  89. #if RGB_BLUE == 2
  90. #define C2_SCALE B_SCALE
  91. #endif
  92. /*
  93. * First we have the histogram data structure and routines for creating it.
  94. *
  95. * The number of bits of precision can be adjusted by changing these symbols.
  96. * We recommend keeping 6 bits for G and 5 each for R and B.
  97. * If you have plenty of memory and cycles, 6 bits all around gives marginally
  98. * better results; if you are short of memory, 5 bits all around will save
  99. * some space but degrade the results.
  100. * To maintain a fully accurate histogram, we'd need to allocate a "long"
  101. * (preferably unsigned long) for each cell. In practice this is overkill;
  102. * we can get by with 16 bits per cell. Few of the cell counts will overflow,
  103. * and clamping those that do overflow to the maximum value will give close-
  104. * enough results. This reduces the recommended histogram size from 256Kb
  105. * to 128Kb, which is a useful savings on PC-class machines.
  106. * (In the second pass the histogram space is re-used for pixel mapping data;
  107. * in that capacity, each cell must be able to store zero to the number of
  108. * desired colors. 16 bits/cell is plenty for that too.)
  109. * Since the JPEG code is intended to run in small memory model on 80x86
  110. * machines, we can't just allocate the histogram in one chunk. Instead
  111. * of a true 3-D array, we use a row of pointers to 2-D arrays. Each
  112. * pointer corresponds to a C0 value (typically 2^5 = 32 pointers) and
  113. * each 2-D array has 2^6*2^5 = 2048 or 2^6*2^6 = 4096 entries. Note that
  114. * on 80x86 machines, the pointer row is in near memory but the actual
  115. * arrays are in far memory (same arrangement as we use for image arrays).
  116. */
  117. #define MAXNUMCOLORS (MAXJSAMPLE+1) /* maximum size of colormap */
  118. /* These will do the right thing for either R,G,B or B,G,R color order,
  119. * but you may not like the results for other color orders.
  120. */
  121. #define HIST_C0_BITS 5 /* bits of precision in R/B histogram */
  122. #define HIST_C1_BITS 6 /* bits of precision in G histogram */
  123. #define HIST_C2_BITS 5 /* bits of precision in B/R histogram */
  124. /* Number of elements along histogram axes. */
  125. #define HIST_C0_ELEMS (1<<HIST_C0_BITS)
  126. #define HIST_C1_ELEMS (1<<HIST_C1_BITS)
  127. #define HIST_C2_ELEMS (1<<HIST_C2_BITS)
  128. /* These are the amounts to shift an input value to get a histogram index. */
  129. #define C0_SHIFT (BITS_IN_JSAMPLE-HIST_C0_BITS)
  130. #define C1_SHIFT (BITS_IN_JSAMPLE-HIST_C1_BITS)
  131. #define C2_SHIFT (BITS_IN_JSAMPLE-HIST_C2_BITS)
  132. typedef UINT16 histcell; /* histogram cell; prefer an unsigned type */
  133. typedef histcell FAR * histptr; /* for pointers to histogram cells */
  134. typedef histcell hist1d[HIST_C2_ELEMS]; /* typedefs for the array */
  135. typedef hist1d FAR * hist2d; /* type for the 2nd-level pointers */
  136. typedef hist2d * hist3d; /* type for top-level pointer */
  137. /* Declarations for Floyd-Steinberg dithering.
  138. *
  139. * Errors are accumulated into the array fserrors[], at a resolution of
  140. * 1/16th of a pixel count. The error at a given pixel is propagated
  141. * to its not-yet-processed neighbors using the standard F-S fractions,
  142. * ... (here) 7/16
  143. * 3/16 5/16 1/16
  144. * We work left-to-right on even rows, right-to-left on odd rows.
  145. *
  146. * We can get away with a single array (holding one row's worth of errors)
  147. * by using it to store the current row's errors at pixel columns not yet
  148. * processed, but the next row's errors at columns already processed. We
  149. * need only a few extra variables to hold the errors immediately around the
  150. * current column. (If we are lucky, those variables are in registers, but
  151. * even if not, they're probably cheaper to access than array elements are.)
  152. *
  153. * The fserrors[] array has (#columns + 2) entries; the extra entry at
  154. * each end saves us from special-casing the first and last pixels.
  155. * Each entry is three values long, one value for each color component.
  156. *
  157. * Note: on a wide image, we might not have enough room in a PC's near data
  158. * segment to hold the error array; so it is allocated with alloc_large.
  159. */
  160. #if BITS_IN_JSAMPLE == 8
  161. typedef INT16 FSERROR; /* 16 bits should be enough */
  162. typedef int LOCFSERROR; /* use 'int' for calculation temps */
  163. #else
  164. typedef INT32 FSERROR; /* may need more than 16 bits */
  165. typedef INT32 LOCFSERROR; /* be sure calculation temps are big enough */
  166. #endif
  167. typedef FSERROR FAR *FSERRPTR; /* pointer to error array (in FAR storage!) */
  168. /* Private subobject */
  169. typedef struct {
  170. struct jpeg_color_quantizer pub; /* public fields */
  171. /* Space for the eventually created colormap is stashed here */
  172. JSAMPARRAY sv_colormap; /* colormap allocated at init time */
  173. int desired; /* desired # of colors = size of colormap */
  174. /* Variables for accumulating image statistics */
  175. hist3d histogram; /* pointer to the histogram */
  176. boolean needs_zeroed; /* TRUE if next pass must zero histogram */
  177. /* Variables for Floyd-Steinberg dithering */
  178. FSERRPTR fserrors; /* accumulated errors */
  179. boolean on_odd_row; /* flag to remember which row we are on */
  180. int * error_limiter; /* table for clamping the applied error */
  181. } my_cquantizer2;
  182. typedef my_cquantizer2 * my_cquantize_ptr2;
  183. /*
  184. * Prescan some rows of pixels.
  185. * In this module the prescan simply updates the histogram, which has been
  186. * initialized to zeroes by start_pass.
  187. * An output_buf parameter is required by the method signature, but no data
  188. * is actually output (in fact the buffer controller is probably passing a
  189. * NULL pointer).
  190. */
  191. METHODDEF(void)
  192. prescan_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf,
  193. JSAMPARRAY, int num_rows)
  194. {
  195. my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;
  196. register JSAMPROW ptr;
  197. register histptr histp;
  198. register hist3d histogram = cquantize->histogram;
  199. int row;
  200. JDIMENSION col;
  201. JDIMENSION width = cinfo->output_width;
  202. for (row = 0; row < num_rows; row++) {
  203. ptr = input_buf[row];
  204. for (col = width; col > 0; col--) {
  205. /* get pixel value and index into the histogram */
  206. histp = & histogram[GETJSAMPLE(ptr[0]) >> C0_SHIFT]
  207. [GETJSAMPLE(ptr[1]) >> C1_SHIFT]
  208. [GETJSAMPLE(ptr[2]) >> C2_SHIFT];
  209. /* increment, check for overflow and undo increment if so. */
  210. if (++(*histp) <= 0)
  211. (*histp)--;
  212. ptr += 3;
  213. }
  214. }
  215. }
  216. /*
  217. * Next we have the really interesting routines: selection of a colormap
  218. * given the completed histogram.
  219. * These routines work with a list of "boxes", each representing a rectangular
  220. * subset of the input color space (to histogram precision).
  221. */
  222. typedef struct {
  223. /* The bounds of the box (inclusive); expressed as histogram indexes */
  224. int c0min, c0max;
  225. int c1min, c1max;
  226. int c2min, c2max;
  227. /* The volume (actually 2-norm) of the box */
  228. INT32 volume;
  229. /* The number of nonzero histogram cells within this box */
  230. long colorcount;
  231. } box;
  232. typedef box * boxptr;
  233. LOCAL(boxptr)
  234. find_biggest_color_pop (boxptr boxlist, int numboxes)
  235. /* Find the splittable box with the largest color population */
  236. /* Returns NULL if no splittable boxes remain */
  237. {
  238. register boxptr boxp;
  239. register int i;
  240. register long maxc = 0;
  241. boxptr which = NULL;
  242. for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
  243. if (boxp->colorcount > maxc && boxp->volume > 0) {
  244. which = boxp;
  245. maxc = boxp->colorcount;
  246. }
  247. }
  248. return which;
  249. }
  250. LOCAL(boxptr)
  251. find_biggest_volume (boxptr boxlist, int numboxes)
  252. /* Find the splittable box with the largest (scaled) volume */
  253. /* Returns NULL if no splittable boxes remain */
  254. {
  255. register boxptr boxp;
  256. register int i;
  257. register INT32 maxv = 0;
  258. boxptr which = NULL;
  259. for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
  260. if (boxp->volume > maxv) {
  261. which = boxp;
  262. maxv = boxp->volume;
  263. }
  264. }
  265. return which;
  266. }
  267. LOCAL(void)
  268. update_box (j_decompress_ptr cinfo, boxptr boxp)
  269. /* Shrink the min/max bounds of a box to enclose only nonzero elements, */
  270. /* and recompute its volume and population */
  271. {
  272. my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;
  273. hist3d histogram = cquantize->histogram;
  274. histptr histp;
  275. int c0,c1,c2;
  276. int c0min,c0max,c1min,c1max,c2min,c2max;
  277. INT32 dist0,dist1,dist2;
  278. long ccount;
  279. c0min = boxp->c0min; c0max = boxp->c0max;
  280. c1min = boxp->c1min; c1max = boxp->c1max;
  281. c2min = boxp->c2min; c2max = boxp->c2max;
  282. if (c0max > c0min)
  283. for (c0 = c0min; c0 <= c0max; c0++)
  284. for (c1 = c1min; c1 <= c1max; c1++) {
  285. histp = & histogram[c0][c1][c2min];
  286. for (c2 = c2min; c2 <= c2max; c2++)
  287. if (*histp++ != 0) {
  288. boxp->c0min = c0min = c0;
  289. goto have_c0min;
  290. }
  291. }
  292. have_c0min:
  293. if (c0max > c0min)
  294. for (c0 = c0max; c0 >= c0min; c0--)
  295. for (c1 = c1min; c1 <= c1max; c1++) {
  296. histp = & histogram[c0][c1][c2min];
  297. for (c2 = c2min; c2 <= c2max; c2++)
  298. if (*histp++ != 0) {
  299. boxp->c0max = c0max = c0;
  300. goto have_c0max;
  301. }
  302. }
  303. have_c0max:
  304. if (c1max > c1min)
  305. for (c1 = c1min; c1 <= c1max; c1++)
  306. for (c0 = c0min; c0 <= c0max; c0++) {
  307. histp = & histogram[c0][c1][c2min];
  308. for (c2 = c2min; c2 <= c2max; c2++)
  309. if (*histp++ != 0) {
  310. boxp->c1min = c1min = c1;
  311. goto have_c1min;
  312. }
  313. }
  314. have_c1min:
  315. if (c1max > c1min)
  316. for (c1 = c1max; c1 >= c1min; c1--)
  317. for (c0 = c0min; c0 <= c0max; c0++) {
  318. histp = & histogram[c0][c1][c2min];
  319. for (c2 = c2min; c2 <= c2max; c2++)
  320. if (*histp++ != 0) {
  321. boxp->c1max = c1max = c1;
  322. goto have_c1max;
  323. }
  324. }
  325. have_c1max:
  326. if (c2max > c2min)
  327. for (c2 = c2min; c2 <= c2max; c2++)
  328. for (c0 = c0min; c0 <= c0max; c0++) {
  329. histp = & histogram[c0][c1min][c2];
  330. for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
  331. if (*histp != 0) {
  332. boxp->c2min = c2min = c2;
  333. goto have_c2min;
  334. }
  335. }
  336. have_c2min:
  337. if (c2max > c2min)
  338. for (c2 = c2max; c2 >= c2min; c2--)
  339. for (c0 = c0min; c0 <= c0max; c0++) {
  340. histp = & histogram[c0][c1min][c2];
  341. for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
  342. if (*histp != 0) {
  343. boxp->c2max = c2max = c2;
  344. goto have_c2max;
  345. }
  346. }
  347. have_c2max:
  348. /* Update box volume.
  349. * We use 2-norm rather than real volume here; this biases the method
  350. * against making long narrow boxes, and it has the side benefit that
  351. * a box is splittable iff norm > 0.
  352. * Since the differences are expressed in histogram-cell units,
  353. * we have to shift back to JSAMPLE units to get consistent distances;
  354. * after which, we scale according to the selected distance scale factors.
  355. */
  356. dist0 = ((c0max - c0min) << C0_SHIFT) * C0_SCALE;
  357. dist1 = ((c1max - c1min) << C1_SHIFT) * C1_SCALE;
  358. dist2 = ((c2max - c2min) << C2_SHIFT) * C2_SCALE;
  359. boxp->volume = dist0*dist0 + dist1*dist1 + dist2*dist2;
  360. /* Now scan remaining volume of box and compute population */
  361. ccount = 0;
  362. for (c0 = c0min; c0 <= c0max; c0++)
  363. for (c1 = c1min; c1 <= c1max; c1++) {
  364. histp = & histogram[c0][c1][c2min];
  365. for (c2 = c2min; c2 <= c2max; c2++, histp++)
  366. if (*histp != 0) {
  367. ccount++;
  368. }
  369. }
  370. boxp->colorcount = ccount;
  371. }
  372. LOCAL(int)
  373. median_cut (j_decompress_ptr cinfo, boxptr boxlist, int numboxes,
  374. int desired_colors)
  375. /* Repeatedly select and split the largest box until we have enough boxes */
  376. {
  377. int n,lb;
  378. int c0,c1,c2,cmax;
  379. register boxptr b1,b2;
  380. while (numboxes < desired_colors) {
  381. /* Select box to split.
  382. * Current algorithm: by population for first half, then by volume.
  383. */
  384. if (numboxes*2 <= desired_colors) {
  385. b1 = find_biggest_color_pop(boxlist, numboxes);
  386. } else {
  387. b1 = find_biggest_volume(boxlist, numboxes);
  388. }
  389. if (b1 == NULL) /* no splittable boxes left! */
  390. break;
  391. b2 = &boxlist[numboxes]; /* where new box will go */
  392. /* Copy the color bounds to the new box. */
  393. b2->c0max = b1->c0max; b2->c1max = b1->c1max; b2->c2max = b1->c2max;
  394. b2->c0min = b1->c0min; b2->c1min = b1->c1min; b2->c2min = b1->c2min;
  395. /* Choose which axis to split the box on.
  396. * Current algorithm: longest scaled axis.
  397. * See notes in update_box about scaling distances.
  398. */
  399. c0 = ((b1->c0max - b1->c0min) << C0_SHIFT) * C0_SCALE;
  400. c1 = ((b1->c1max - b1->c1min) << C1_SHIFT) * C1_SCALE;
  401. c2 = ((b1->c2max - b1->c2min) << C2_SHIFT) * C2_SCALE;
  402. /* We want to break any ties in favor of green, then red, blue last.
  403. * This code does the right thing for R,G,B or B,G,R color orders only.
  404. */
  405. #if RGB_RED == 0
  406. cmax = c1; n = 1;
  407. if (c0 > cmax) { cmax = c0; n = 0; }
  408. if (c2 > cmax) { n = 2; }
  409. #else
  410. cmax = c1; n = 1;
  411. if (c2 > cmax) { cmax = c2; n = 2; }
  412. if (c0 > cmax) { n = 0; }
  413. #endif
  414. /* Choose split point along selected axis, and update box bounds.
  415. * Current algorithm: split at halfway point.
  416. * (Since the box has been shrunk to minimum volume,
  417. * any split will produce two nonempty subboxes.)
  418. * Note that lb value is max for lower box, so must be < old max.
  419. */
  420. switch (n) {
  421. case 0:
  422. lb = (b1->c0max + b1->c0min) / 2;
  423. b1->c0max = lb;
  424. b2->c0min = lb+1;
  425. break;
  426. case 1:
  427. lb = (b1->c1max + b1->c1min) / 2;
  428. b1->c1max = lb;
  429. b2->c1min = lb+1;
  430. break;
  431. case 2:
  432. lb = (b1->c2max + b1->c2min) / 2;
  433. b1->c2max = lb;
  434. b2->c2min = lb+1;
  435. break;
  436. }
  437. /* Update stats for boxes */
  438. update_box(cinfo, b1);
  439. update_box(cinfo, b2);
  440. numboxes++;
  441. }
  442. return numboxes;
  443. }
  444. LOCAL(void)
  445. compute_color (j_decompress_ptr cinfo, boxptr boxp, int icolor)
  446. /* Compute representative color for a box, put it in colormap[icolor] */
  447. {
  448. /* Current algorithm: mean weighted by pixels (not colors) */
  449. /* Note it is important to get the rounding correct! */
  450. my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;
  451. hist3d histogram = cquantize->histogram;
  452. histptr histp;
  453. int c0,c1,c2;
  454. int c0min,c0max,c1min,c1max,c2min,c2max;
  455. long count;
  456. long total = 0;
  457. long c0total = 0;
  458. long c1total = 0;
  459. long c2total = 0;
  460. c0min = boxp->c0min; c0max = boxp->c0max;
  461. c1min = boxp->c1min; c1max = boxp->c1max;
  462. c2min = boxp->c2min; c2max = boxp->c2max;
  463. for (c0 = c0min; c0 <= c0max; c0++)
  464. for (c1 = c1min; c1 <= c1max; c1++) {
  465. histp = & histogram[c0][c1][c2min];
  466. for (c2 = c2min; c2 <= c2max; c2++) {
  467. if ((count = *histp++) != 0) {
  468. total += count;
  469. c0total += ((c0 << C0_SHIFT) + ((1<<C0_SHIFT)>>1)) * count;
  470. c1total += ((c1 << C1_SHIFT) + ((1<<C1_SHIFT)>>1)) * count;
  471. c2total += ((c2 << C2_SHIFT) + ((1<<C2_SHIFT)>>1)) * count;
  472. }
  473. }
  474. }
  475. cinfo->colormap[0][icolor] = (JSAMPLE) ((c0total + (total>>1)) / total);
  476. cinfo->colormap[1][icolor] = (JSAMPLE) ((c1total + (total>>1)) / total);
  477. cinfo->colormap[2][icolor] = (JSAMPLE) ((c2total + (total>>1)) / total);
  478. }
  479. LOCAL(void)
  480. select_colors (j_decompress_ptr cinfo, int desired_colors)
  481. /* Master routine for color selection */
  482. {
  483. boxptr boxlist;
  484. int numboxes;
  485. int i;
  486. /* Allocate workspace for box list */
  487. boxlist = (boxptr) (*cinfo->mem->alloc_small)
  488. ((j_common_ptr) cinfo, JPOOL_IMAGE, desired_colors * SIZEOF(box));
  489. /* Initialize one box containing whole space */
  490. numboxes = 1;
  491. boxlist[0].c0min = 0;
  492. boxlist[0].c0max = MAXJSAMPLE >> C0_SHIFT;
  493. boxlist[0].c1min = 0;
  494. boxlist[0].c1max = MAXJSAMPLE >> C1_SHIFT;
  495. boxlist[0].c2min = 0;
  496. boxlist[0].c2max = MAXJSAMPLE >> C2_SHIFT;
  497. /* Shrink it to actually-used volume and set its statistics */
  498. update_box(cinfo, & boxlist[0]);
  499. /* Perform median-cut to produce final box list */
  500. numboxes = median_cut(cinfo, boxlist, numboxes, desired_colors);
  501. /* Compute the representative color for each box, fill colormap */
  502. for (i = 0; i < numboxes; i++)
  503. compute_color(cinfo, & boxlist[i], i);
  504. cinfo->actual_number_of_colors = numboxes;
  505. TRACEMS1(cinfo, 1, JTRC_QUANT_SELECTED, numboxes);
  506. }
  507. /*
  508. * These routines are concerned with the time-critical task of mapping input
  509. * colors to the nearest color in the selected colormap.
  510. *
  511. * We re-use the histogram space as an "inverse color map", essentially a
  512. * cache for the results of nearest-color searches. All colors within a
  513. * histogram cell will be mapped to the same colormap entry, namely the one
  514. * closest to the cell's center. This may not be quite the closest entry to
  515. * the actual input color, but it's almost as good. A zero in the cache
  516. * indicates we haven't found the nearest color for that cell yet; the array
  517. * is cleared to zeroes before starting the mapping pass. When we find the
  518. * nearest color for a cell, its colormap index plus one is recorded in the
  519. * cache for future use. The pass2 scanning routines call fill_inverse_cmap
  520. * when they need to use an unfilled entry in the cache.
  521. *
  522. * Our method of efficiently finding nearest colors is based on the "locally
  523. * sorted search" idea described by Heckbert and on the incremental distance
  524. * calculation described by Spencer W. Thomas in chapter III.1 of Graphics
  525. * Gems II (James Arvo, ed. Academic Press, 1991). Thomas points out that
  526. * the distances from a given colormap entry to each cell of the histogram can
  527. * be computed quickly using an incremental method: the differences between
  528. * distances to adjacent cells themselves differ by a constant. This allows a
  529. * fairly fast implementation of the "brute force" approach of computing the
  530. * distance from every colormap entry to every histogram cell. Unfortunately,
  531. * it needs a work array to hold the best-distance-so-far for each histogram
  532. * cell (because the inner loop has to be over cells, not colormap entries).
  533. * The work array elements have to be INT32s, so the work array would need
  534. * 256Kb at our recommended precision. This is not feasible in DOS machines.
  535. *
  536. * To get around these problems, we apply Thomas' method to compute the
  537. * nearest colors for only the cells within a small subbox of the histogram.
  538. * The work array need be only as big as the subbox, so the memory usage
  539. * problem is solved. Furthermore, we need not fill subboxes that are never
  540. * referenced in pass2; many images use only part of the color gamut, so a
  541. * fair amount of work is saved. An additional advantage of this
  542. * approach is that we can apply Heckbert's locality criterion to quickly
  543. * eliminate colormap entries that are far away from the subbox; typically
  544. * three-fourths of the colormap entries are rejected by Heckbert's criterion,
  545. * and we need not compute their distances to individual cells in the subbox.
  546. * The speed of this approach is heavily influenced by the subbox size: too
  547. * small means too much overhead, too big loses because Heckbert's criterion
  548. * can't eliminate as many colormap entries. Empirically the best subbox
  549. * size seems to be about 1/512th of the histogram (1/8th in each direction).
  550. *
  551. * Thomas' article also describes a refined method which is asymptotically
  552. * faster than the brute-force method, but it is also far more complex and
  553. * cannot efficiently be applied to small subboxes. It is therefore not
  554. * useful for programs intended to be portable to DOS machines. On machines
  555. * with plenty of memory, filling the whole histogram in one shot with Thomas'
  556. * refined method might be faster than the present code --- but then again,
  557. * it might not be any faster, and it's certainly more complicated.
  558. */
  559. /* log2(histogram cells in update box) for each axis; this can be adjusted */
  560. #define BOX_C0_LOG (HIST_C0_BITS-3)
  561. #define BOX_C1_LOG (HIST_C1_BITS-3)
  562. #define BOX_C2_LOG (HIST_C2_BITS-3)
  563. #define BOX_C0_ELEMS (1<<BOX_C0_LOG) /* # of hist cells in update box */
  564. #define BOX_C1_ELEMS (1<<BOX_C1_LOG)
  565. #define BOX_C2_ELEMS (1<<BOX_C2_LOG)
  566. #define BOX_C0_SHIFT (C0_SHIFT + BOX_C0_LOG)
  567. #define BOX_C1_SHIFT (C1_SHIFT + BOX_C1_LOG)
  568. #define BOX_C2_SHIFT (C2_SHIFT + BOX_C2_LOG)
  569. /*
  570. * The next three routines implement inverse colormap filling. They could
  571. * all be folded into one big routine, but splitting them up this way saves
  572. * some stack space (the mindist[] and bestdist[] arrays need not coexist)
  573. * and may allow some compilers to produce better code by registerizing more
  574. * inner-loop variables.
  575. */
  576. LOCAL(int)
  577. find_nearby_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
  578. JSAMPLE colorlist[])
  579. /* Locate the colormap entries close enough to an update box to be candidates
  580. * for the nearest entry to some cell(s) in the update box. The update box
  581. * is specified by the center coordinates of its first cell. The number of
  582. * candidate colormap entries is returned, and their colormap indexes are
  583. * placed in colorlist[].
  584. * This routine uses Heckbert's "locally sorted search" criterion to select
  585. * the colors that need further consideration.
  586. */
  587. {
  588. int numcolors = cinfo->actual_number_of_colors;
  589. int maxc0, maxc1, maxc2;
  590. int centerc0, centerc1, centerc2;
  591. int i, x, ncolors;
  592. INT32 minmaxdist, min_dist, max_dist, tdist;
  593. INT32 mindist[MAXNUMCOLORS]; /* min distance to colormap entry i */
  594. /* Compute true coordinates of update box's upper corner and center.
  595. * Actually we compute the coordinates of the center of the upper-corner
  596. * histogram cell, which are the upper bounds of the volume we care about.
  597. * Note that since ">>" rounds down, the "center" values may be closer to
  598. * min than to max; hence comparisons to them must be "<=", not "<".
  599. */
  600. maxc0 = minc0 + ((1 << BOX_C0_SHIFT) - (1 << C0_SHIFT));
  601. centerc0 = (minc0 + maxc0) >> 1;
  602. maxc1 = minc1 + ((1 << BOX_C1_SHIFT) - (1 << C1_SHIFT));
  603. centerc1 = (minc1 + maxc1) >> 1;
  604. maxc2 = minc2 + ((1 << BOX_C2_SHIFT) - (1 << C2_SHIFT));
  605. centerc2 = (minc2 + maxc2) >> 1;
  606. /* For each color in colormap, find:
  607. * 1. its minimum squared-distance to any point in the update box
  608. * (zero if color is within update box);
  609. * 2. its maximum squared-distance to any point in the update box.
  610. * Both of these can be found by considering only the corners of the box.
  611. * We save the minimum distance for each color in mindist[];
  612. * only the smallest maximum distance is of interest.
  613. */
  614. minmaxdist = 0x7FFFFFFFL;
  615. for (i = 0; i < numcolors; i++) {
  616. /* We compute the squared-c0-distance term, then add in the other two. */
  617. x = GETJSAMPLE(cinfo->colormap[0][i]);
  618. if (x < minc0) {
  619. tdist = (x - minc0) * C0_SCALE;
  620. min_dist = tdist*tdist;
  621. tdist = (x - maxc0) * C0_SCALE;
  622. max_dist = tdist*tdist;
  623. } else if (x > maxc0) {
  624. tdist = (x - maxc0) * C0_SCALE;
  625. min_dist = tdist*tdist;
  626. tdist = (x - minc0) * C0_SCALE;
  627. max_dist = tdist*tdist;
  628. } else {
  629. /* within cell range so no contribution to min_dist */
  630. min_dist = 0;
  631. if (x <= centerc0) {
  632. tdist = (x - maxc0) * C0_SCALE;
  633. max_dist = tdist*tdist;
  634. } else {
  635. tdist = (x - minc0) * C0_SCALE;
  636. max_dist = tdist*tdist;
  637. }
  638. }
  639. x = GETJSAMPLE(cinfo->colormap[1][i]);
  640. if (x < minc1) {
  641. tdist = (x - minc1) * C1_SCALE;
  642. min_dist += tdist*tdist;
  643. tdist = (x - maxc1) * C1_SCALE;
  644. max_dist += tdist*tdist;
  645. } else if (x > maxc1) {
  646. tdist = (x - maxc1) * C1_SCALE;
  647. min_dist += tdist*tdist;
  648. tdist = (x - minc1) * C1_SCALE;
  649. max_dist += tdist*tdist;
  650. } else {
  651. /* within cell range so no contribution to min_dist */
  652. if (x <= centerc1) {
  653. tdist = (x - maxc1) * C1_SCALE;
  654. max_dist += tdist*tdist;
  655. } else {
  656. tdist = (x - minc1) * C1_SCALE;
  657. max_dist += tdist*tdist;
  658. }
  659. }
  660. x = GETJSAMPLE(cinfo->colormap[2][i]);
  661. if (x < minc2) {
  662. tdist = (x - minc2) * C2_SCALE;
  663. min_dist += tdist*tdist;
  664. tdist = (x - maxc2) * C2_SCALE;
  665. max_dist += tdist*tdist;
  666. } else if (x > maxc2) {
  667. tdist = (x - maxc2) * C2_SCALE;
  668. min_dist += tdist*tdist;
  669. tdist = (x - minc2) * C2_SCALE;
  670. max_dist += tdist*tdist;
  671. } else {
  672. /* within cell range so no contribution to min_dist */
  673. if (x <= centerc2) {
  674. tdist = (x - maxc2) * C2_SCALE;
  675. max_dist += tdist*tdist;
  676. } else {
  677. tdist = (x - minc2) * C2_SCALE;
  678. max_dist += tdist*tdist;
  679. }
  680. }
  681. mindist[i] = min_dist; /* save away the results */
  682. if (max_dist < minmaxdist)
  683. minmaxdist = max_dist;
  684. }
  685. /* Now we know that no cell in the update box is more than minmaxdist
  686. * away from some colormap entry. Therefore, only colors that are
  687. * within minmaxdist of some part of the box need be considered.
  688. */
  689. ncolors = 0;
  690. for (i = 0; i < numcolors; i++) {
  691. if (mindist[i] <= minmaxdist)
  692. colorlist[ncolors++] = (JSAMPLE) i;
  693. }
  694. return ncolors;
  695. }
  696. LOCAL(void)
  697. find_best_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
  698. int numcolors, JSAMPLE colorlist[], JSAMPLE bestcolor[])
  699. /* Find the closest colormap entry for each cell in the update box,
  700. * given the list of candidate colors prepared by find_nearby_colors.
  701. * Return the indexes of the closest entries in the bestcolor[] array.
  702. * This routine uses Thomas' incremental distance calculation method to
  703. * find the distance from a colormap entry to successive cells in the box.
  704. */
  705. {
  706. int ic0, ic1, ic2;
  707. int i, icolor;
  708. register INT32 * bptr; /* pointer into bestdist[] array */
  709. JSAMPLE * cptr; /* pointer into bestcolor[] array */
  710. INT32 dist0, dist1; /* initial distance values */
  711. register INT32 dist2; /* current distance in inner loop */
  712. INT32 xx0, xx1; /* distance increments */
  713. register INT32 xx2;
  714. INT32 inc0, inc1, inc2; /* initial values for increments */
  715. /* This array holds the distance to the nearest-so-far color for each cell */
  716. INT32 bestdist[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
  717. /* Initialize best-distance for each cell of the update box */
  718. bptr = bestdist;
  719. for (i = BOX_C0_ELEMS*BOX_C1_ELEMS*BOX_C2_ELEMS-1; i >= 0; i--)
  720. *bptr++ = 0x7FFFFFFFL;
  721. /* For each color selected by find_nearby_colors,
  722. * compute its distance to the center of each cell in the box.
  723. * If that's less than best-so-far, update best distance and color number.
  724. */
  725. /* Nominal steps between cell centers ("x" in Thomas article) */
  726. #define STEP_C0 ((1 << C0_SHIFT) * C0_SCALE)
  727. #define STEP_C1 ((1 << C1_SHIFT) * C1_SCALE)
  728. #define STEP_C2 ((1 << C2_SHIFT) * C2_SCALE)
  729. for (i = 0; i < numcolors; i++) {
  730. icolor = GETJSAMPLE(colorlist[i]);
  731. /* Compute (square of) distance from minc0/c1/c2 to this color */
  732. inc0 = (minc0 - GETJSAMPLE(cinfo->colormap[0][icolor])) * C0_SCALE;
  733. dist0 = inc0*inc0;
  734. inc1 = (minc1 - GETJSAMPLE(cinfo->colormap[1][icolor])) * C1_SCALE;
  735. dist0 += inc1*inc1;
  736. inc2 = (minc2 - GETJSAMPLE(cinfo->colormap[2][icolor])) * C2_SCALE;
  737. dist0 += inc2*inc2;
  738. /* Form the initial difference increments */
  739. inc0 = inc0 * (2 * STEP_C0) + STEP_C0 * STEP_C0;
  740. inc1 = inc1 * (2 * STEP_C1) + STEP_C1 * STEP_C1;
  741. inc2 = inc2 * (2 * STEP_C2) + STEP_C2 * STEP_C2;
  742. /* Now loop over all cells in box, updating distance per Thomas method */
  743. bptr = bestdist;
  744. cptr = bestcolor;
  745. xx0 = inc0;
  746. for (ic0 = BOX_C0_ELEMS-1; ic0 >= 0; ic0--) {
  747. dist1 = dist0;
  748. xx1 = inc1;
  749. for (ic1 = BOX_C1_ELEMS-1; ic1 >= 0; ic1--) {
  750. dist2 = dist1;
  751. xx2 = inc2;
  752. for (ic2 = BOX_C2_ELEMS-1; ic2 >= 0; ic2--) {
  753. if (dist2 < *bptr) {
  754. *bptr = dist2;
  755. *cptr = (JSAMPLE) icolor;
  756. }
  757. dist2 += xx2;
  758. xx2 += 2 * STEP_C2 * STEP_C2;
  759. bptr++;
  760. cptr++;
  761. }
  762. dist1 += xx1;
  763. xx1 += 2 * STEP_C1 * STEP_C1;
  764. }
  765. dist0 += xx0;
  766. xx0 += 2 * STEP_C0 * STEP_C0;
  767. }
  768. }
  769. }
  770. LOCAL(void)
  771. fill_inverse_cmap (j_decompress_ptr cinfo, int c0, int c1, int c2)
  772. /* Fill the inverse-colormap entries in the update box that contains */
  773. /* histogram cell c0/c1/c2. (Only that one cell MUST be filled, but */
  774. /* we can fill as many others as we wish.) */
  775. {
  776. my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;
  777. hist3d histogram = cquantize->histogram;
  778. int minc0, minc1, minc2; /* lower left corner of update box */
  779. int ic0, ic1, ic2;
  780. register JSAMPLE * cptr; /* pointer into bestcolor[] array */
  781. register histptr cachep; /* pointer into main cache array */
  782. /* This array lists the candidate colormap indexes. */
  783. JSAMPLE colorlist[MAXNUMCOLORS];
  784. int numcolors; /* number of candidate colors */
  785. /* This array holds the actually closest colormap index for each cell. */
  786. JSAMPLE bestcolor[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
  787. /* Convert cell coordinates to update box ID */
  788. c0 >>= BOX_C0_LOG;
  789. c1 >>= BOX_C1_LOG;
  790. c2 >>= BOX_C2_LOG;
  791. /* Compute true coordinates of update box's origin corner.
  792. * Actually we compute the coordinates of the center of the corner
  793. * histogram cell, which are the lower bounds of the volume we care about.
  794. */
  795. minc0 = (c0 << BOX_C0_SHIFT) + ((1 << C0_SHIFT) >> 1);
  796. minc1 = (c1 << BOX_C1_SHIFT) + ((1 << C1_SHIFT) >> 1);
  797. minc2 = (c2 << BOX_C2_SHIFT) + ((1 << C2_SHIFT) >> 1);
  798. /* Determine which colormap entries are close enough to be candidates
  799. * for the nearest entry to some cell in the update box.
  800. */
  801. numcolors = find_nearby_colors(cinfo, minc0, minc1, minc2, colorlist);
  802. /* Determine the actually nearest colors. */
  803. find_best_colors(cinfo, minc0, minc1, minc2, numcolors, colorlist,
  804. bestcolor);
  805. /* Save the best color numbers (plus 1) in the main cache array */
  806. c0 <<= BOX_C0_LOG; /* convert ID back to base cell indexes */
  807. c1 <<= BOX_C1_LOG;
  808. c2 <<= BOX_C2_LOG;
  809. cptr = bestcolor;
  810. for (ic0 = 0; ic0 < BOX_C0_ELEMS; ic0++) {
  811. for (ic1 = 0; ic1 < BOX_C1_ELEMS; ic1++) {
  812. cachep = & histogram[c0+ic0][c1+ic1][c2];
  813. for (ic2 = 0; ic2 < BOX_C2_ELEMS; ic2++) {
  814. *cachep++ = (histcell) (GETJSAMPLE(*cptr++) + 1);
  815. }
  816. }
  817. }
  818. }
  819. /*
  820. * Map some rows of pixels to the output colormapped representation.
  821. */
  822. METHODDEF(void)
  823. pass2_no_dither (j_decompress_ptr cinfo,
  824. JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
  825. /* This version performs no dithering */
  826. {
  827. my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;
  828. hist3d histogram = cquantize->histogram;
  829. register JSAMPROW inptr, outptr;
  830. register histptr cachep;
  831. register int c0, c1, c2;
  832. int row;
  833. JDIMENSION col;
  834. JDIMENSION width = cinfo->output_width;
  835. for (row = 0; row < num_rows; row++) {
  836. inptr = input_buf[row];
  837. outptr = output_buf[row];
  838. for (col = width; col > 0; col--) {
  839. /* get pixel value and index into the cache */
  840. c0 = GETJSAMPLE(*inptr++) >> C0_SHIFT;
  841. c1 = GETJSAMPLE(*inptr++) >> C1_SHIFT;
  842. c2 = GETJSAMPLE(*inptr++) >> C2_SHIFT;
  843. cachep = & histogram[c0][c1][c2];
  844. /* If we have not seen this color before, find nearest colormap entry */
  845. /* and update the cache */
  846. if (*cachep == 0)
  847. fill_inverse_cmap(cinfo, c0,c1,c2);
  848. /* Now emit the colormap index for this cell */
  849. *outptr++ = (JSAMPLE) (*cachep - 1);
  850. }
  851. }
  852. }
  853. METHODDEF(void)
  854. pass2_fs_dither (j_decompress_ptr cinfo,
  855. JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
  856. /* This version performs Floyd-Steinberg dithering */
  857. {
  858. my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;
  859. hist3d histogram = cquantize->histogram;
  860. register LOCFSERROR cur0, cur1, cur2; /* current error or pixel value */
  861. LOCFSERROR belowerr0, belowerr1, belowerr2; /* error for pixel below cur */
  862. LOCFSERROR bpreverr0, bpreverr1, bpreverr2; /* error for below/prev col */
  863. register FSERRPTR errorptr; /* => fserrors[] at column before current */
  864. JSAMPROW inptr; /* => current input pixel */
  865. JSAMPROW outptr; /* => current output pixel */
  866. histptr cachep;
  867. int dir; /* +1 or -1 depending on direction */
  868. int dir3; /* 3*dir, for advancing inptr & errorptr */
  869. int row;
  870. JDIMENSION col;
  871. JDIMENSION width = cinfo->output_width;
  872. JSAMPLE *range_limit = cinfo->sample_range_limit;
  873. int *error_limit = cquantize->error_limiter;
  874. JSAMPROW colormap0 = cinfo->colormap[0];
  875. JSAMPROW colormap1 = cinfo->colormap[1];
  876. JSAMPROW colormap2 = cinfo->colormap[2];
  877. SHIFT_TEMPS
  878. for (row = 0; row < num_rows; row++) {
  879. inptr = input_buf[row];
  880. outptr = output_buf[row];
  881. if (cquantize->on_odd_row) {
  882. /* work right to left in this row */
  883. inptr += (width-1) * 3; /* so point to rightmost pixel */
  884. outptr += width-1;
  885. dir = -1;
  886. dir3 = -3;
  887. errorptr = cquantize->fserrors + (width+1)*3; /* => entry after last column */
  888. cquantize->on_odd_row = FALSE; /* flip for next time */
  889. } else {
  890. /* work left to right in this row */
  891. dir = 1;
  892. dir3 = 3;
  893. errorptr = cquantize->fserrors; /* => entry before first real column */
  894. cquantize->on_odd_row = TRUE; /* flip for next time */
  895. }
  896. /* Preset error values: no error propagated to first pixel from left */
  897. cur0 = cur1 = cur2 = 0;
  898. /* and no error propagated to row below yet */
  899. belowerr0 = belowerr1 = belowerr2 = 0;
  900. bpreverr0 = bpreverr1 = bpreverr2 = 0;
  901. for (col = width; col > 0; col--) {
  902. /* curN holds the error propagated from the previous pixel on the
  903. * current line. Add the error propagated from the previous line
  904. * to form the complete error correction term for this pixel, and
  905. * round the error term (which is expressed * 16) to an integer.
  906. * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
  907. * for either sign of the error value.
  908. * Note: errorptr points to *previous* column's array entry.
  909. */
  910. cur0 = RIGHT_SHIFT(cur0 + errorptr[dir3+0] + 8, 4);
  911. cur1 = RIGHT_SHIFT(cur1 + errorptr[dir3+1] + 8, 4);
  912. cur2 = RIGHT_SHIFT(cur2 + errorptr[dir3+2] + 8, 4);
  913. /* Limit the error using transfer function set by init_error_limit.
  914. * See comments with init_error_limit for rationale.
  915. */
  916. cur0 = error_limit[cur0];
  917. cur1 = error_limit[cur1];
  918. cur2 = error_limit[cur2];
  919. /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
  920. * The maximum error is +- MAXJSAMPLE (or less with error limiting);
  921. * this sets the required size of the range_limit array.
  922. */
  923. cur0 += GETJSAMPLE(inptr[0]);
  924. cur1 += GETJSAMPLE(inptr[1]);
  925. cur2 += GETJSAMPLE(inptr[2]);
  926. cur0 = GETJSAMPLE(range_limit[cur0]);
  927. cur1 = GETJSAMPLE(range_limit[cur1]);
  928. cur2 = GETJSAMPLE(range_limit[cur2]);
  929. /* Index into the cache with adjusted pixel value */
  930. cachep = & histogram[cur0>>C0_SHIFT][cur1>>C1_SHIFT][cur2>>C2_SHIFT];
  931. /* If we have not seen this color before, find nearest colormap */
  932. /* entry and update the cache */
  933. if (*cachep == 0)
  934. fill_inverse_cmap(cinfo, cur0>>C0_SHIFT,cur1>>C1_SHIFT,cur2>>C2_SHIFT);
  935. /* Now emit the colormap index for this cell */
  936. { register int pixcode = *cachep - 1;
  937. *outptr = (JSAMPLE) pixcode;
  938. /* Compute representation error for this pixel */
  939. cur0 -= GETJSAMPLE(colormap0[pixcode]);
  940. cur1 -= GETJSAMPLE(colormap1[pixcode]);
  941. cur2 -= GETJSAMPLE(colormap2[pixcode]);
  942. }
  943. /* Compute error fractions to be propagated to adjacent pixels.
  944. * Add these into the running sums, and simultaneously shift the
  945. * next-line error sums left by 1 column.
  946. */
  947. { register LOCFSERROR bnexterr, delta;
  948. bnexterr = cur0; /* Process component 0 */
  949. delta = cur0 * 2;
  950. cur0 += delta; /* form error * 3 */
  951. errorptr[0] = (FSERROR) (bpreverr0 + cur0);
  952. cur0 += delta; /* form error * 5 */
  953. bpreverr0 = belowerr0 + cur0;
  954. belowerr0 = bnexterr;
  955. cur0 += delta; /* form error * 7 */
  956. bnexterr = cur1; /* Process component 1 */
  957. delta = cur1 * 2;
  958. cur1 += delta; /* form error * 3 */
  959. errorptr[1] = (FSERROR) (bpreverr1 + cur1);
  960. cur1 += delta; /* form error * 5 */
  961. bpreverr1 = belowerr1 + cur1;
  962. belowerr1 = bnexterr;
  963. cur1 += delta; /* form error * 7 */
  964. bnexterr = cur2; /* Process component 2 */
  965. delta = cur2 * 2;
  966. cur2 += delta; /* form error * 3 */
  967. errorptr[2] = (FSERROR) (bpreverr2 + cur2);
  968. cur2 += delta; /* form error * 5 */
  969. bpreverr2 = belowerr2 + cur2;
  970. belowerr2 = bnexterr;
  971. cur2 += delta; /* form error * 7 */
  972. }
  973. /* At this point curN contains the 7/16 error value to be propagated
  974. * to the next pixel on the current line, and all the errors for the
  975. * next line have been shifted over. We are therefore ready to move on.
  976. */
  977. inptr += dir3; /* Advance pixel pointers to next column */
  978. outptr += dir;
  979. errorptr += dir3; /* advance errorptr to current column */
  980. }
  981. /* Post-loop cleanup: we must unload the final error values into the
  982. * final fserrors[] entry. Note we need not unload belowerrN because
  983. * it is for the dummy column before or after the actual array.
  984. */
  985. errorptr[0] = (FSERROR) bpreverr0; /* unload prev errs into array */
  986. errorptr[1] = (FSERROR) bpreverr1;
  987. errorptr[2] = (FSERROR) bpreverr2;
  988. }
  989. }
  990. /*
  991. * Initialize the error-limiting transfer function (lookup table).
  992. * The raw F-S error computation can potentially compute error values of up to
  993. * +- MAXJSAMPLE. But we want the maximum correction applied to a pixel to be
  994. * much less, otherwise obviously wrong pixels will be created. (Typical
  995. * effects include weird fringes at color-area boundaries, isolated bright
  996. * pixels in a dark area, etc.) The standard advice for avoiding this problem
  997. * is to ensure that the "corners" of the color cube are allocated as output
  998. * colors; then repeated errors in the same direction cannot cause cascading
  999. * error buildup. However, that only prevents the error from getting
  1000. * completely out of hand; Aaron Giles reports that error limiting improves
  1001. * the results even with corner colors allocated.
  1002. * A simple clamping of the error values to about +- MAXJSAMPLE/8 works pretty
  1003. * well, but the smoother transfer function used below is even better. Thanks
  1004. * to Aaron Giles for this idea.
  1005. */
  1006. LOCAL(void)
  1007. init_error_limit (j_decompress_ptr cinfo)
  1008. /* Allocate and fill in the error_limiter table */
  1009. {
  1010. my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;
  1011. int * table;
  1012. int in, out;
  1013. table = (int *) (*cinfo->mem->alloc_small)
  1014. ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE*2+1) * SIZEOF(int));
  1015. table += MAXJSAMPLE; /* so can index -MAXJSAMPLE .. +MAXJSAMPLE */
  1016. cquantize->error_limiter = table;
  1017. #define STEPSIZE ((MAXJSAMPLE+1)/16)
  1018. /* Map errors 1:1 up to +- MAXJSAMPLE/16 */
  1019. out = 0;
  1020. for (in = 0; in < STEPSIZE; in++, out++) {
  1021. table[in] = out; table[-in] = -out;
  1022. }
  1023. /* Map errors 1:2 up to +- 3*MAXJSAMPLE/16 */
  1024. for (; in < STEPSIZE*3; in++, out += (in&1) ? 0 : 1) {
  1025. table[in] = out; table[-in] = -out;
  1026. }
  1027. /* Clamp the rest to final out value (which is (MAXJSAMPLE+1)/8) */
  1028. for (; in <= MAXJSAMPLE; in++) {
  1029. table[in] = out; table[-in] = -out;
  1030. }
  1031. #undef STEPSIZE
  1032. }
  1033. /*
  1034. * Finish up at the end of each pass.
  1035. */
  1036. METHODDEF(void)
  1037. finish_pass1 (j_decompress_ptr cinfo)
  1038. {
  1039. my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;
  1040. /* Select the representative colors and fill in cinfo->colormap */
  1041. cinfo->colormap = cquantize->sv_colormap;
  1042. select_colors(cinfo, cquantize->desired);
  1043. /* Force next pass to zero the color index table */
  1044. cquantize->needs_zeroed = TRUE;
  1045. }
  1046. METHODDEF(void)
  1047. finish_pass2 (j_decompress_ptr)
  1048. {
  1049. /* no work */
  1050. }
  1051. /*
  1052. * Initialize for each processing pass.
  1053. */
  1054. METHODDEF(void)
  1055. start_pass_2_quant (j_decompress_ptr cinfo, boolean is_pre_scan)
  1056. {
  1057. my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;
  1058. hist3d histogram = cquantize->histogram;
  1059. int i;
  1060. /* Only F-S dithering or no dithering is supported. */
  1061. /* If user asks for ordered dither, give him F-S. */
  1062. if (cinfo->dither_mode != JDITHER_NONE)
  1063. cinfo->dither_mode = JDITHER_FS;
  1064. if (is_pre_scan) {
  1065. /* Set up method pointers */
  1066. cquantize->pub.color_quantize = prescan_quantize;
  1067. cquantize->pub.finish_pass = finish_pass1;
  1068. cquantize->needs_zeroed = TRUE; /* Always zero histogram */
  1069. } else {
  1070. /* Set up method pointers */
  1071. if (cinfo->dither_mode == JDITHER_FS)
  1072. cquantize->pub.color_quantize = pass2_fs_dither;
  1073. else
  1074. cquantize->pub.color_quantize = pass2_no_dither;
  1075. cquantize->pub.finish_pass = finish_pass2;
  1076. /* Make sure color count is acceptable */
  1077. i = cinfo->actual_number_of_colors;
  1078. if (i < 1)
  1079. ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 1);
  1080. if (i > MAXNUMCOLORS)
  1081. ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);
  1082. if (cinfo->dither_mode == JDITHER_FS) {
  1083. size_t arraysize = (size_t) ((cinfo->output_width + 2) *
  1084. (3 * SIZEOF(FSERROR)));
  1085. /* Allocate Floyd-Steinberg workspace if we didn't already. */
  1086. if (cquantize->fserrors == NULL)
  1087. cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
  1088. ((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
  1089. /* Initialize the propagated errors to zero. */
  1090. jzero_far((void FAR *) cquantize->fserrors, arraysize);
  1091. /* Make the error-limit table if we didn't already. */
  1092. if (cquantize->error_limiter == NULL)
  1093. init_error_limit(cinfo);
  1094. cquantize->on_odd_row = FALSE;
  1095. }
  1096. }
  1097. /* Zero the histogram or inverse color map, if necessary */
  1098. if (cquantize->needs_zeroed) {
  1099. for (i = 0; i < HIST_C0_ELEMS; i++) {
  1100. jzero_far((void FAR *) histogram[i],
  1101. HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
  1102. }
  1103. cquantize->needs_zeroed = FALSE;
  1104. }
  1105. }
  1106. /*
  1107. * Switch to a new external colormap between output passes.
  1108. */
  1109. METHODDEF(void)
  1110. new_color_map_2_quant (j_decompress_ptr cinfo)
  1111. {
  1112. my_cquantize_ptr2 cquantize = (my_cquantize_ptr2) cinfo->cquantize;
  1113. /* Reset the inverse color map */
  1114. cquantize->needs_zeroed = TRUE;
  1115. }
  1116. /*
  1117. * Module initialization routine for 2-pass color quantization.
  1118. */
  1119. GLOBAL(void)
  1120. jinit_2pass_quantizer (j_decompress_ptr cinfo)
  1121. {
  1122. my_cquantize_ptr2 cquantize;
  1123. int i;
  1124. cquantize = (my_cquantize_ptr2)
  1125. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  1126. SIZEOF(my_cquantizer2));
  1127. cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
  1128. cquantize->pub.start_pass = start_pass_2_quant;
  1129. cquantize->pub.new_color_map = new_color_map_2_quant;
  1130. cquantize->fserrors = NULL; /* flag optional arrays not allocated */
  1131. cquantize->error_limiter = NULL;
  1132. /* Make sure jdmaster didn't give me a case I can't handle */
  1133. if (cinfo->out_color_components != 3)
  1134. ERREXIT(cinfo, JERR_NOTIMPL);
  1135. /* Allocate the histogram/inverse colormap storage */
  1136. cquantize->histogram = (hist3d) (*cinfo->mem->alloc_small)
  1137. ((j_common_ptr) cinfo, JPOOL_IMAGE, HIST_C0_ELEMS * SIZEOF(hist2d));
  1138. for (i = 0; i < HIST_C0_ELEMS; i++) {
  1139. cquantize->histogram[i] = (hist2d) (*cinfo->mem->alloc_large)
  1140. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  1141. HIST_C1_ELEMS*HIST_C2_ELEMS * SIZEOF(histcell));
  1142. }
  1143. cquantize->needs_zeroed = TRUE; /* histogram is garbage now */
  1144. /* Allocate storage for the completed colormap, if required.
  1145. * We do this now since it is FAR storage and may affect
  1146. * the memory manager's space calculations.
  1147. */
  1148. if (cinfo->enable_2pass_quant) {
  1149. /* Make sure color count is acceptable */
  1150. int desired = cinfo->desired_number_of_colors;
  1151. /* Lower bound on # of colors ... somewhat arbitrary as long as > 0 */
  1152. if (desired < 8)
  1153. ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 8);
  1154. /* Make sure colormap indexes can be represented by JSAMPLEs */
  1155. if (desired > MAXNUMCOLORS)
  1156. ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);
  1157. cquantize->sv_colormap = (*cinfo->mem->alloc_sarray)
  1158. ((j_common_ptr) cinfo,JPOOL_IMAGE, (JDIMENSION) desired, (JDIMENSION) 3);
  1159. cquantize->desired = desired;
  1160. } else
  1161. cquantize->sv_colormap = NULL;
  1162. /* Only F-S dithering or no dithering is supported. */
  1163. /* If user asks for ordered dither, give him F-S. */
  1164. if (cinfo->dither_mode != JDITHER_NONE)
  1165. cinfo->dither_mode = JDITHER_FS;
  1166. /* Allocate Floyd-Steinberg workspace if necessary.
  1167. * This isn't really needed until pass 2, but again it is FAR storage.
  1168. * Although we will cope with a later change in dither_mode,
  1169. * we do not promise to honor max_memory_to_use if dither_mode changes.
  1170. */
  1171. if (cinfo->dither_mode == JDITHER_FS) {
  1172. cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
  1173. ((j_common_ptr) cinfo, JPOOL_IMAGE,
  1174. (size_t) ((cinfo->output_width + 2) * (3 * SIZEOF(FSERROR))));
  1175. /* Might as well create the error-limiting table too. */
  1176. init_error_limit(cinfo);
  1177. }
  1178. }
  1179. #endif /* QUANT_2PASS_SUPPORTED */