COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
jquant2.cpp File Reference
#include "jinclude.h"
#include "jpeglib.h"
Include dependency graph for jquant2.cpp:

Classes

struct  my_cquantizer
 
struct  box
 

Macros

#define JPEG_INTERNALS
 
#define R_SCALE   2 /* scale R distances by this much */
 
#define G_SCALE   3 /* scale G distances by this much */
 
#define B_SCALE   1 /* and B by this much */
 
#define C0_SCALE   R_SCALE
 
#define C1_SCALE   G_SCALE
 
#define C2_SCALE   B_SCALE
 
#define MAXNUMCOLORS   (MAXJSAMPLE+1) /* maximum size of colormap */
 
#define HIST_C0_BITS   5 /* bits of precision in R/B histogram */
 
#define HIST_C1_BITS   6 /* bits of precision in G histogram */
 
#define HIST_C2_BITS   5 /* bits of precision in B/R histogram */
 
#define HIST_C0_ELEMS   (1<<HIST_C0_BITS)
 
#define HIST_C1_ELEMS   (1<<HIST_C1_BITS)
 
#define HIST_C2_ELEMS   (1<<HIST_C2_BITS)
 
#define C0_SHIFT   (BITS_IN_JSAMPLE-HIST_C0_BITS)
 
#define C1_SHIFT   (BITS_IN_JSAMPLE-HIST_C1_BITS)
 
#define C2_SHIFT   (BITS_IN_JSAMPLE-HIST_C2_BITS)
 
#define BOX_C0_LOG   (HIST_C0_BITS-3)
 
#define BOX_C1_LOG   (HIST_C1_BITS-3)
 
#define BOX_C2_LOG   (HIST_C2_BITS-3)
 
#define BOX_C0_ELEMS   (1<<BOX_C0_LOG) /* # of hist cells in update box */
 
#define BOX_C1_ELEMS   (1<<BOX_C1_LOG)
 
#define BOX_C2_ELEMS   (1<<BOX_C2_LOG)
 
#define BOX_C0_SHIFT   (C0_SHIFT + BOX_C0_LOG)
 
#define BOX_C1_SHIFT   (C1_SHIFT + BOX_C1_LOG)
 
#define BOX_C2_SHIFT   (C2_SHIFT + BOX_C2_LOG)
 
#define STEP_C0   ((1 << C0_SHIFT) * C0_SCALE)
 
#define STEP_C1   ((1 << C1_SHIFT) * C1_SCALE)
 
#define STEP_C2   ((1 << C2_SHIFT) * C2_SCALE)
 
#define STEPSIZE   ((MAXJSAMPLE+1)/16)
 

Typedefs

typedef uint16_t histcell
 
typedef histcellhistptr
 
typedef histcell hist1d[(1<< 5)]
 
typedef hist1dhist2d
 
typedef hist2dhist3d
 
typedef int16_t FSERROR
 
typedef int LOCFSERROR
 
typedef FSERRORFSERRPTR
 
typedef my_cquantizermy_cquantize_ptr
 
typedef boxboxptr
 

Functions

static void prescan_quantize (j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
 
static boxptr find_biggest_color_pop (boxptr boxlist, int numboxes)
 
static boxptr find_biggest_volume (boxptr boxlist, int numboxes)
 
static void update_box (j_decompress_ptr cinfo, boxptr boxp)
 
static int median_cut (j_decompress_ptr cinfo, boxptr boxlist, int numboxes, int desired_colors)
 
static void compute_color (j_decompress_ptr cinfo, boxptr boxp, int icolor)
 
static void select_colors (j_decompress_ptr cinfo, int desired_colors)
 
static int find_nearby_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2, JSAMPLE colorlist[])
 
static void find_best_colors (j_decompress_ptr cinfo, int minc0, int minc1, int minc2, int numcolors, JSAMPLE colorlist[], JSAMPLE bestcolor[])
 
static void fill_inverse_cmap (j_decompress_ptr cinfo, int c0, int c1, int c2)
 
static void pass2_no_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
 
static void pass2_fs_dither (j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
 
static void init_error_limit (j_decompress_ptr cinfo)
 
static void finish_pass1 (j_decompress_ptr cinfo)
 
static void finish_pass2 (j_decompress_ptr cinfo)
 
static void start_pass_2_quant (j_decompress_ptr cinfo, boolean is_pre_scan)
 
static void new_color_map_2_quant (j_decompress_ptr cinfo)
 
void jinit_2pass_quantizer (j_decompress_ptr cinfo)
 

Macro Definition Documentation

#define JPEG_INTERNALS
#define R_SCALE   2 /* scale R distances by this much */
#define G_SCALE   3 /* scale G distances by this much */
#define B_SCALE   1 /* and B by this much */
#define C0_SCALE   R_SCALE
#define C1_SCALE   G_SCALE
#define C2_SCALE   B_SCALE
#define MAXNUMCOLORS   (MAXJSAMPLE+1) /* maximum size of colormap */
#define HIST_C0_BITS   5 /* bits of precision in R/B histogram */
#define HIST_C1_BITS   6 /* bits of precision in G histogram */
#define HIST_C2_BITS   5 /* bits of precision in B/R histogram */
#define HIST_C0_ELEMS   (1<<HIST_C0_BITS)
#define HIST_C1_ELEMS   (1<<HIST_C1_BITS)
#define HIST_C2_ELEMS   (1<<HIST_C2_BITS)
#define C0_SHIFT   (BITS_IN_JSAMPLE-HIST_C0_BITS)
#define C1_SHIFT   (BITS_IN_JSAMPLE-HIST_C1_BITS)
#define C2_SHIFT   (BITS_IN_JSAMPLE-HIST_C2_BITS)
#define BOX_C0_LOG   (HIST_C0_BITS-3)
#define BOX_C1_LOG   (HIST_C1_BITS-3)
#define BOX_C2_LOG   (HIST_C2_BITS-3)
#define BOX_C0_ELEMS   (1<<BOX_C0_LOG) /* # of hist cells in update box */
#define BOX_C1_ELEMS   (1<<BOX_C1_LOG)
#define BOX_C2_ELEMS   (1<<BOX_C2_LOG)
#define BOX_C0_SHIFT   (C0_SHIFT + BOX_C0_LOG)
#define BOX_C1_SHIFT   (C1_SHIFT + BOX_C1_LOG)
#define BOX_C2_SHIFT   (C2_SHIFT + BOX_C2_LOG)
#define STEP_C0   ((1 << C0_SHIFT) * C0_SCALE)
#define STEP_C1   ((1 << C1_SHIFT) * C1_SCALE)
#define STEP_C2   ((1 << C2_SHIFT) * C2_SCALE)
#define STEPSIZE   ((MAXJSAMPLE+1)/16)

Typedef Documentation

typedef uint16_t histcell
typedef histcell* histptr
typedef histcell hist1d[(1<< 5)]
typedef hist1d* hist2d
typedef hist2d* hist3d
typedef int16_t FSERROR
typedef int LOCFSERROR
typedef FSERROR* FSERRPTR
typedef box* boxptr

Function Documentation

static void prescan_quantize ( j_decompress_ptr  cinfo,
JSAMPARRAY  input_buf,
JSAMPARRAY  output_buf,
int  num_rows 
)
static
226 {
227  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
228  register JSAMPROW ptr;
229  register histptr histp;
230  register hist3d histogram = cquantize->histogram;
231  int row;
232  JDIMENSION col;
233  JDIMENSION width = cinfo->output_width;
234 
235  for (row = 0; row < num_rows; row++) {
236  ptr = input_buf[row];
237  for (col = width; col > 0; col--) {
238  /* get pixel value and index into the histogram */
239  histp = & histogram[GETJSAMPLE(ptr[0]) >> C0_SHIFT]
240  [GETJSAMPLE(ptr[1]) >> C1_SHIFT]
241  [GETJSAMPLE(ptr[2]) >> C2_SHIFT];
242  /* increment, check for overflow and undo increment if so. */
243  if (++(*histp) <= 0)
244  (*histp)--;
245  ptr += 3;
246  }
247  }
248 }
my_cquantizer * my_cquantize_ptr
Definition: jquant2.cpp:211
#define GETJSAMPLE(value)
Definition: jmorecfg.h:68
JDIMENSION output_width
Definition: jpeglib.h:464
histcell * histptr
Definition: jquant2.cpp:149
JSAMPLE * JSAMPROW
Definition: jpeglib.h:71
hist2d * hist3d
Definition: jquant2.cpp:153
#define C0_SHIFT
Definition: jquant2.cpp:142
struct jpeg_color_quantizer * cquantize
Definition: jpeglib.h:634
int JSAMPARRAY int int num_rows
Definition: jpegint.h:373
Definition: jquant1.cpp:142
unsigned int JDIMENSION
Definition: jmorecfg.h:171
#define C2_SHIFT
Definition: jquant2.cpp:144
#define C1_SHIFT
Definition: jquant2.cpp:143
hist3d histogram
Definition: jquant2.cpp:201
static boxptr find_biggest_color_pop ( boxptr  boxlist,
int  numboxes 
)
static
276 {
277  register boxptr boxp;
278  register int i;
279  register long maxc = 0;
280  boxptr which = NULL;
281 
282  for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
283  if (boxp->colorcount > maxc && boxp->volume > 0) {
284  which = boxp;
285  maxc = boxp->colorcount;
286  }
287  }
288  return which;
289 }
int i
Definition: rw_test.cpp:37
long colorcount
Definition: jquant2.cpp:266
int32_t volume
Definition: jquant2.cpp:264
Definition: jquant2.cpp:258
static boxptr find_biggest_volume ( boxptr  boxlist,
int  numboxes 
)
static
296 {
297  register boxptr boxp;
298  register int i;
299  register int32_t maxv = 0;
300  boxptr which = NULL;
301 
302  for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
303  if (boxp->volume > maxv) {
304  which = boxp;
305  maxv = boxp->volume;
306  }
307  }
308  return which;
309 }
int i
Definition: rw_test.cpp:37
int32_t volume
Definition: jquant2.cpp:264
Definition: jquant2.cpp:258
static void update_box ( j_decompress_ptr  cinfo,
boxptr  boxp 
)
static
316 {
317  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
318  hist3d histogram = cquantize->histogram;
319  histptr histp;
320  int c0,c1,c2;
321  int c0min,c0max,c1min,c1max,c2min,c2max;
322  int32_t dist0,dist1,dist2;
323  long ccount;
324 
325  c0min = boxp->c0min; c0max = boxp->c0max;
326  c1min = boxp->c1min; c1max = boxp->c1max;
327  c2min = boxp->c2min; c2max = boxp->c2max;
328 
329  if (c0max > c0min)
330  for (c0 = c0min; c0 <= c0max; c0++)
331  for (c1 = c1min; c1 <= c1max; c1++) {
332  histp = & histogram[c0][c1][c2min];
333  for (c2 = c2min; c2 <= c2max; c2++)
334  if (*histp++ != 0) {
335  boxp->c0min = c0min = c0;
336  goto have_c0min;
337  }
338  }
339  have_c0min:
340  if (c0max > c0min)
341  for (c0 = c0max; c0 >= c0min; c0--)
342  for (c1 = c1min; c1 <= c1max; c1++) {
343  histp = & histogram[c0][c1][c2min];
344  for (c2 = c2min; c2 <= c2max; c2++)
345  if (*histp++ != 0) {
346  boxp->c0max = c0max = c0;
347  goto have_c0max;
348  }
349  }
350  have_c0max:
351  if (c1max > c1min)
352  for (c1 = c1min; c1 <= c1max; c1++)
353  for (c0 = c0min; c0 <= c0max; c0++) {
354  histp = & histogram[c0][c1][c2min];
355  for (c2 = c2min; c2 <= c2max; c2++)
356  if (*histp++ != 0) {
357  boxp->c1min = c1min = c1;
358  goto have_c1min;
359  }
360  }
361  have_c1min:
362  if (c1max > c1min)
363  for (c1 = c1max; c1 >= c1min; c1--)
364  for (c0 = c0min; c0 <= c0max; c0++) {
365  histp = & histogram[c0][c1][c2min];
366  for (c2 = c2min; c2 <= c2max; c2++)
367  if (*histp++ != 0) {
368  boxp->c1max = c1max = c1;
369  goto have_c1max;
370  }
371  }
372  have_c1max:
373  if (c2max > c2min)
374  for (c2 = c2min; c2 <= c2max; c2++)
375  for (c0 = c0min; c0 <= c0max; c0++) {
376  histp = & histogram[c0][c1min][c2];
377  for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
378  if (*histp != 0) {
379  boxp->c2min = c2min = c2;
380  goto have_c2min;
381  }
382  }
383  have_c2min:
384  if (c2max > c2min)
385  for (c2 = c2max; c2 >= c2min; c2--)
386  for (c0 = c0min; c0 <= c0max; c0++) {
387  histp = & histogram[c0][c1min][c2];
388  for (c1 = c1min; c1 <= c1max; c1++, histp += HIST_C2_ELEMS)
389  if (*histp != 0) {
390  boxp->c2max = c2max = c2;
391  goto have_c2max;
392  }
393  }
394  have_c2max:
395 
396  /* Update box volume.
397  * We use 2-norm rather than real volume here; this biases the method
398  * against making long narrow boxes, and it has the side benefit that
399  * a box is splittable iff norm > 0.
400  * Since the differences are expressed in histogram-cell units,
401  * we have to shift back to JSAMPLE units to get consistent distances;
402  * after which, we scale according to the selected distance scale factors.
403  */
404  dist0 = ((c0max - c0min) << C0_SHIFT) * C0_SCALE;
405  dist1 = ((c1max - c1min) << C1_SHIFT) * C1_SCALE;
406  dist2 = ((c2max - c2min) << C2_SHIFT) * C2_SCALE;
407  boxp->volume = dist0*dist0 + dist1*dist1 + dist2*dist2;
408 
409  /* Now scan remaining volume of box and compute population */
410  ccount = 0;
411  for (c0 = c0min; c0 <= c0max; c0++)
412  for (c1 = c1min; c1 <= c1max; c1++) {
413  histp = & histogram[c0][c1][c2min];
414  for (c2 = c2min; c2 <= c2max; c2++, histp++)
415  if (*histp != 0) {
416  ccount++;
417  }
418  }
419  boxp->colorcount = ccount;
420 }
int c1max
Definition: jquant2.cpp:261
my_cquantizer * my_cquantize_ptr
Definition: jquant2.cpp:211
int c2min
Definition: jquant2.cpp:262
#define C1_SCALE
Definition: jquant2.cpp:91
histcell * histptr
Definition: jquant2.cpp:149
hist2d * hist3d
Definition: jquant2.cpp:153
long colorcount
Definition: jquant2.cpp:266
int c0max
Definition: jquant2.cpp:260
#define C0_SCALE
Definition: jquant2.cpp:85
int c2max
Definition: jquant2.cpp:262
#define C0_SHIFT
Definition: jquant2.cpp:142
struct jpeg_color_quantizer * cquantize
Definition: jpeglib.h:634
#define HIST_C2_ELEMS
Definition: jquant2.cpp:139
int c0min
Definition: jquant2.cpp:260
Definition: jquant1.cpp:142
#define C2_SCALE
Definition: jquant2.cpp:97
int c1min
Definition: jquant2.cpp:261
#define C2_SHIFT
Definition: jquant2.cpp:144
#define C1_SHIFT
Definition: jquant2.cpp:143
hist3d histogram
Definition: jquant2.cpp:201
int32_t volume
Definition: jquant2.cpp:264
static int median_cut ( j_decompress_ptr  cinfo,
boxptr  boxlist,
int  numboxes,
int  desired_colors 
)
static
427 {
428  int n,lb;
429  int c0,c1,c2,cmax;
430  register boxptr b1,b2;
431 
432  while (numboxes < desired_colors) {
433  /* Select box to split.
434  * Current algorithm: by population for first half, then by volume.
435  */
436  if (numboxes*2 <= desired_colors) {
437  b1 = find_biggest_color_pop(boxlist, numboxes);
438  } else {
439  b1 = find_biggest_volume(boxlist, numboxes);
440  }
441  if (b1 == NULL) /* no splittable boxes left! */
442  break;
443  b2 = &boxlist[numboxes]; /* where new box will go */
444  /* Copy the color bounds to the new box. */
445  b2->c0max = b1->c0max; b2->c1max = b1->c1max; b2->c2max = b1->c2max;
446  b2->c0min = b1->c0min; b2->c1min = b1->c1min; b2->c2min = b1->c2min;
447  /* Choose which axis to split the box on.
448  * Current algorithm: longest scaled axis.
449  * See notes in update_box about scaling distances.
450  */
451  c0 = ((b1->c0max - b1->c0min) << C0_SHIFT) * C0_SCALE;
452  c1 = ((b1->c1max - b1->c1min) << C1_SHIFT) * C1_SCALE;
453  c2 = ((b1->c2max - b1->c2min) << C2_SHIFT) * C2_SCALE;
454  /* We want to break any ties in favor of green, then red, blue last.
455  * This code does the right thing for R,G,B or B,G,R color orders only.
456  */
457 #if RGB_RED == 0
458  cmax = c1; n = 1;
459  if (c0 > cmax) { cmax = c0; n = 0; }
460  if (c2 > cmax) { n = 2; }
461 #else
462  cmax = c1; n = 1;
463  if (c2 > cmax) { cmax = c2; n = 2; }
464  if (c0 > cmax) { n = 0; }
465 #endif
466  /* Choose split point along selected axis, and update box bounds.
467  * Current algorithm: split at halfway point.
468  * (Since the box has been shrunk to minimum volume,
469  * any split will produce two nonempty subboxes.)
470  * Note that lb value is max for lower box, so must be < old max.
471  */
472  switch (n) {
473  case 0:
474  lb = (b1->c0max + b1->c0min) / 2;
475  b1->c0max = lb;
476  b2->c0min = lb+1;
477  break;
478  case 1:
479  lb = (b1->c1max + b1->c1min) / 2;
480  b1->c1max = lb;
481  b2->c1min = lb+1;
482  break;
483  case 2:
484  lb = (b1->c2max + b1->c2min) / 2;
485  b1->c2max = lb;
486  b2->c2min = lb+1;
487  break;
488  }
489  /* Update stats for boxes */
490  update_box(cinfo, b1);
491  update_box(cinfo, b2);
492  numboxes++;
493  }
494  return numboxes;
495 }
static void update_box(j_decompress_ptr cinfo, boxptr boxp)
Definition: jquant2.cpp:313
int c1max
Definition: jquant2.cpp:261
int c2min
Definition: jquant2.cpp:262
#define C1_SCALE
Definition: jquant2.cpp:91
static boxptr find_biggest_volume(boxptr boxlist, int numboxes)
Definition: jquant2.cpp:293
int c0max
Definition: jquant2.cpp:260
#define C0_SCALE
Definition: jquant2.cpp:85
int c2max
Definition: jquant2.cpp:262
#define C0_SHIFT
Definition: jquant2.cpp:142
int c0min
Definition: jquant2.cpp:260
static boxptr find_biggest_color_pop(boxptr boxlist, int numboxes)
Definition: jquant2.cpp:273
#define C2_SCALE
Definition: jquant2.cpp:97
int c1min
Definition: jquant2.cpp:261
#define C2_SHIFT
Definition: jquant2.cpp:144
#define C1_SHIFT
Definition: jquant2.cpp:143
Definition: jquant2.cpp:258
static void compute_color ( j_decompress_ptr  cinfo,
boxptr  boxp,
int  icolor 
)
static
501 {
502  /* Current algorithm: mean weighted by pixels (not colors) */
503  /* Note it is important to get the rounding correct! */
504  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
505  hist3d histogram = cquantize->histogram;
506  histptr histp;
507  int c0,c1,c2;
508  int c0min,c0max,c1min,c1max,c2min,c2max;
509  long count;
510  long total = 0;
511  long c0total = 0;
512  long c1total = 0;
513  long c2total = 0;
514 
515  c0min = boxp->c0min; c0max = boxp->c0max;
516  c1min = boxp->c1min; c1max = boxp->c1max;
517  c2min = boxp->c2min; c2max = boxp->c2max;
518 
519  for (c0 = c0min; c0 <= c0max; c0++)
520  for (c1 = c1min; c1 <= c1max; c1++) {
521  histp = & histogram[c0][c1][c2min];
522  for (c2 = c2min; c2 <= c2max; c2++) {
523  if ((count = *histp++) != 0) {
524  total += count;
525  c0total += ((c0 << C0_SHIFT) + ((1<<C0_SHIFT)>>1)) * count;
526  c1total += ((c1 << C1_SHIFT) + ((1<<C1_SHIFT)>>1)) * count;
527  c2total += ((c2 << C2_SHIFT) + ((1<<C2_SHIFT)>>1)) * count;
528  }
529  }
530  }
531 
532  cinfo->colormap[0][icolor] = (JSAMPLE) ((c0total + (total>>1)) / total);
533  cinfo->colormap[1][icolor] = (JSAMPLE) ((c1total + (total>>1)) / total);
534  cinfo->colormap[2][icolor] = (JSAMPLE) ((c2total + (total>>1)) / total);
535 }
char JSAMPLE
Definition: jmorecfg.h:64
int c1max
Definition: jquant2.cpp:261
my_cquantizer * my_cquantize_ptr
Definition: jquant2.cpp:211
int c2min
Definition: jquant2.cpp:262
int count
Definition: rw_test.cpp:36
histcell * histptr
Definition: jquant2.cpp:149
hist2d * hist3d
Definition: jquant2.cpp:153
int c0max
Definition: jquant2.cpp:260
int c2max
Definition: jquant2.cpp:262
struct jpeg_color_quantizer * cquantize
Definition: jpeglib.h:634
int c0min
Definition: jquant2.cpp:260
JSAMPARRAY colormap
Definition: jpeglib.h:484
Definition: jquant1.cpp:142
int c1min
Definition: jquant2.cpp:261
hist3d histogram
Definition: jquant2.cpp:201
static void select_colors ( j_decompress_ptr  cinfo,
int  desired_colors 
)
static
541 {
542  boxptr boxlist;
543  int numboxes;
544  int i;
545 
546  /* Allocate workspace for box list */
547  boxlist = (boxptr) (*cinfo->mem->alloc_small)
548  ((j_common_ptr) cinfo, JPOOL_IMAGE, desired_colors * SIZEOF(box));
549  /* Initialize one box containing whole space */
550  numboxes = 1;
551  boxlist[0].c0min = 0;
552  boxlist[0].c0max = MAXJSAMPLE >> C0_SHIFT;
553  boxlist[0].c1min = 0;
554  boxlist[0].c1max = MAXJSAMPLE >> C1_SHIFT;
555  boxlist[0].c2min = 0;
556  boxlist[0].c2max = MAXJSAMPLE >> C2_SHIFT;
557  /* Shrink it to actually-used volume and set its statistics */
558  update_box(cinfo, & boxlist[0]);
559  /* Perform median-cut to produce final box list */
560  numboxes = median_cut(cinfo, boxlist, numboxes, desired_colors);
561  /* Compute the representative color for each box, fill colormap */
562  for (i = 0; i < numboxes; i++)
563  compute_color(cinfo, & boxlist[i], i);
564  cinfo->actual_number_of_colors = numboxes;
565  TRACEMS1(cinfo, 1, JTRC_QUANT_SELECTED, numboxes);
566 }
box * boxptr
Definition: jquant2.cpp:269
static void update_box(j_decompress_ptr cinfo, boxptr boxp)
Definition: jquant2.cpp:313
int c1max
Definition: jquant2.cpp:261
int i
Definition: rw_test.cpp:37
int c2min
Definition: jquant2.cpp:262
struct jpeg_common_struct * j_common_ptr
Definition: jpeglib.h:266
void *(* alloc_small)()
Definition: jpeglib.h:764
#define SIZEOF(object)
Definition: jinclude.h:80
#define MAXJSAMPLE
Definition: jmorecfg.h:73
static void compute_color(j_decompress_ptr cinfo, boxptr boxp, int icolor)
Definition: jquant2.cpp:499
#define JPOOL_IMAGE
Definition: jpeglib.h:754
struct jpeg_memory_mgr * mem
Definition: jpeglib.h:417
#define TRACEMS1(cinfo, lvl, code, p1)
Definition: jerror.h:255
Definition: jerror.h:149
int c0max
Definition: jquant2.cpp:260
int c2max
Definition: jquant2.cpp:262
#define C0_SHIFT
Definition: jquant2.cpp:142
static int median_cut(j_decompress_ptr cinfo, boxptr boxlist, int numboxes, int desired_colors)
Definition: jquant2.cpp:424
int c0min
Definition: jquant2.cpp:260
int c1min
Definition: jquant2.cpp:261
#define C2_SHIFT
Definition: jquant2.cpp:144
#define C1_SHIFT
Definition: jquant2.cpp:143
int actual_number_of_colors
Definition: jpeglib.h:483
Definition: jquant2.cpp:258
static int find_nearby_colors ( j_decompress_ptr  cinfo,
int  minc0,
int  minc1,
int  minc2,
JSAMPLE  colorlist[] 
)
static
656 {
657  int numcolors = cinfo->actual_number_of_colors;
658  int maxc0, maxc1, maxc2;
659  int centerc0, centerc1, centerc2;
660  int i, x, ncolors;
661  int32_t minmaxdist, min_dist, max_dist, tdist;
662  int32_t mindist[MAXNUMCOLORS]; /* min distance to colormap entry i */
663 
664  /* Compute true coordinates of update box's upper corner and center.
665  * Actually we compute the coordinates of the center of the upper-corner
666  * histogram cell, which are the upper bounds of the volume we care about.
667  * Note that since ">>" rounds down, the "center" values may be closer to
668  * min than to max; hence comparisons to them must be "<=", not "<".
669  */
670  maxc0 = minc0 + ((1 << BOX_C0_SHIFT) - (1 << C0_SHIFT));
671  centerc0 = (minc0 + maxc0) >> 1;
672  maxc1 = minc1 + ((1 << BOX_C1_SHIFT) - (1 << C1_SHIFT));
673  centerc1 = (minc1 + maxc1) >> 1;
674  maxc2 = minc2 + ((1 << BOX_C2_SHIFT) - (1 << C2_SHIFT));
675  centerc2 = (minc2 + maxc2) >> 1;
676 
677  /* For each color in colormap, find:
678  * 1. its minimum squared-distance to any point in the update box
679  * (zero if color is within update box);
680  * 2. its maximum squared-distance to any point in the update box.
681  * Both of these can be found by considering only the corners of the box.
682  * We save the minimum distance for each color in mindist[];
683  * only the smallest maximum distance is of interest.
684  */
685  minmaxdist = 0x7FFFFFFFL;
686 
687  for (i = 0; i < numcolors; i++) {
688  /* We compute the squared-c0-distance term, then add in the other two. */
689  x = GETJSAMPLE(cinfo->colormap[0][i]);
690  if (x < minc0) {
691  tdist = (x - minc0) * C0_SCALE;
692  min_dist = tdist*tdist;
693  tdist = (x - maxc0) * C0_SCALE;
694  max_dist = tdist*tdist;
695  } else if (x > maxc0) {
696  tdist = (x - maxc0) * C0_SCALE;
697  min_dist = tdist*tdist;
698  tdist = (x - minc0) * C0_SCALE;
699  max_dist = tdist*tdist;
700  } else {
701  /* within cell range so no contribution to min_dist */
702  min_dist = 0;
703  if (x <= centerc0) {
704  tdist = (x - maxc0) * C0_SCALE;
705  max_dist = tdist*tdist;
706  } else {
707  tdist = (x - minc0) * C0_SCALE;
708  max_dist = tdist*tdist;
709  }
710  }
711 
712  x = GETJSAMPLE(cinfo->colormap[1][i]);
713  if (x < minc1) {
714  tdist = (x - minc1) * C1_SCALE;
715  min_dist += tdist*tdist;
716  tdist = (x - maxc1) * C1_SCALE;
717  max_dist += tdist*tdist;
718  } else if (x > maxc1) {
719  tdist = (x - maxc1) * C1_SCALE;
720  min_dist += tdist*tdist;
721  tdist = (x - minc1) * C1_SCALE;
722  max_dist += tdist*tdist;
723  } else {
724  /* within cell range so no contribution to min_dist */
725  if (x <= centerc1) {
726  tdist = (x - maxc1) * C1_SCALE;
727  max_dist += tdist*tdist;
728  } else {
729  tdist = (x - minc1) * C1_SCALE;
730  max_dist += tdist*tdist;
731  }
732  }
733 
734  x = GETJSAMPLE(cinfo->colormap[2][i]);
735  if (x < minc2) {
736  tdist = (x - minc2) * C2_SCALE;
737  min_dist += tdist*tdist;
738  tdist = (x - maxc2) * C2_SCALE;
739  max_dist += tdist*tdist;
740  } else if (x > maxc2) {
741  tdist = (x - maxc2) * C2_SCALE;
742  min_dist += tdist*tdist;
743  tdist = (x - minc2) * C2_SCALE;
744  max_dist += tdist*tdist;
745  } else {
746  /* within cell range so no contribution to min_dist */
747  if (x <= centerc2) {
748  tdist = (x - maxc2) * C2_SCALE;
749  max_dist += tdist*tdist;
750  } else {
751  tdist = (x - minc2) * C2_SCALE;
752  max_dist += tdist*tdist;
753  }
754  }
755 
756  mindist[i] = min_dist; /* save away the results */
757  if (max_dist < minmaxdist)
758  minmaxdist = max_dist;
759  }
760 
761  /* Now we know that no cell in the update box is more than minmaxdist
762  * away from some colormap entry. Therefore, only colors that are
763  * within minmaxdist of some part of the box need be considered.
764  */
765  ncolors = 0;
766  for (i = 0; i < numcolors; i++) {
767  if (mindist[i] <= minmaxdist)
768  colorlist[ncolors++] = (JSAMPLE) i;
769  }
770  return ncolors;
771 }
char JSAMPLE
Definition: jmorecfg.h:64
int i
Definition: rw_test.cpp:37
#define GETJSAMPLE(value)
Definition: jmorecfg.h:68
#define C1_SCALE
Definition: jquant2.cpp:91
#define MAXNUMCOLORS
Definition: jquant2.cpp:127
#define BOX_C2_SHIFT
Definition: jquant2.cpp:634
#define C0_SCALE
Definition: jquant2.cpp:85
#define C0_SHIFT
Definition: jquant2.cpp:142
#define BOX_C1_SHIFT
Definition: jquant2.cpp:633
x
Definition: inputfile.py:6
JSAMPARRAY colormap
Definition: jpeglib.h:484
#define C2_SCALE
Definition: jquant2.cpp:97
#define C2_SHIFT
Definition: jquant2.cpp:144
#define C1_SHIFT
Definition: jquant2.cpp:143
#define BOX_C0_SHIFT
Definition: jquant2.cpp:632
int actual_number_of_colors
Definition: jpeglib.h:483
static void find_best_colors ( j_decompress_ptr  cinfo,
int  minc0,
int  minc1,
int  minc2,
int  numcolors,
JSAMPLE  colorlist[],
JSAMPLE  bestcolor[] 
)
static
783 {
784  int ic0, ic1, ic2;
785  int i, icolor;
786  register int32_t * bptr; /* pointer into bestdist[] array */
787  JSAMPLE * cptr; /* pointer into bestcolor[] array */
788  int32_t dist0, dist1; /* initial distance values */
789  register int32_t dist2; /* current distance in inner loop */
790  int32_t xx0, xx1; /* distance increments */
791  register int32_t xx2;
792  int32_t inc0, inc1, inc2; /* initial values for increments */
793  /* This array holds the distance to the nearest-so-far color for each cell */
794  int32_t bestdist[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
795 
796  /* Initialize best-distance for each cell of the update box */
797  bptr = bestdist;
798  for (i = BOX_C0_ELEMS*BOX_C1_ELEMS*BOX_C2_ELEMS-1; i >= 0; i--)
799  *bptr++ = 0x7FFFFFFFL;
800 
801  /* For each color selected by find_nearby_colors,
802  * compute its distance to the center of each cell in the box.
803  * If that's less than best-so-far, update best distance and color number.
804  */
805 
806  /* Nominal steps between cell centers ("x" in Thomas article) */
807 #define STEP_C0 ((1 << C0_SHIFT) * C0_SCALE)
808 #define STEP_C1 ((1 << C1_SHIFT) * C1_SCALE)
809 #define STEP_C2 ((1 << C2_SHIFT) * C2_SCALE)
810 
811  for (i = 0; i < numcolors; i++) {
812  icolor = GETJSAMPLE(colorlist[i]);
813  /* Compute (square of) distance from minc0/c1/c2 to this color */
814  inc0 = (minc0 - GETJSAMPLE(cinfo->colormap[0][icolor])) * C0_SCALE;
815  dist0 = inc0*inc0;
816  inc1 = (minc1 - GETJSAMPLE(cinfo->colormap[1][icolor])) * C1_SCALE;
817  dist0 += inc1*inc1;
818  inc2 = (minc2 - GETJSAMPLE(cinfo->colormap[2][icolor])) * C2_SCALE;
819  dist0 += inc2*inc2;
820  /* Form the initial difference increments */
821  inc0 = inc0 * (2 * STEP_C0) + STEP_C0 * STEP_C0;
822  inc1 = inc1 * (2 * STEP_C1) + STEP_C1 * STEP_C1;
823  inc2 = inc2 * (2 * STEP_C2) + STEP_C2 * STEP_C2;
824  /* Now loop over all cells in box, updating distance per Thomas method */
825  bptr = bestdist;
826  cptr = bestcolor;
827  xx0 = inc0;
828  for (ic0 = BOX_C0_ELEMS-1; ic0 >= 0; ic0--) {
829  dist1 = dist0;
830  xx1 = inc1;
831  for (ic1 = BOX_C1_ELEMS-1; ic1 >= 0; ic1--) {
832  dist2 = dist1;
833  xx2 = inc2;
834  for (ic2 = BOX_C2_ELEMS-1; ic2 >= 0; ic2--) {
835  if (dist2 < *bptr) {
836  *bptr = dist2;
837  *cptr = (JSAMPLE) icolor;
838  }
839  dist2 += xx2;
840  xx2 += 2 * STEP_C2 * STEP_C2;
841  bptr++;
842  cptr++;
843  }
844  dist1 += xx1;
845  xx1 += 2 * STEP_C1 * STEP_C1;
846  }
847  dist0 += xx0;
848  xx0 += 2 * STEP_C0 * STEP_C0;
849  }
850  }
851 }
char JSAMPLE
Definition: jmorecfg.h:64
int i
Definition: rw_test.cpp:37
#define GETJSAMPLE(value)
Definition: jmorecfg.h:68
#define C1_SCALE
Definition: jquant2.cpp:91
#define BOX_C1_ELEMS
Definition: jquant2.cpp:629
#define STEP_C0
#define C0_SCALE
Definition: jquant2.cpp:85
#define STEP_C2
#define BOX_C2_ELEMS
Definition: jquant2.cpp:630
JSAMPARRAY colormap
Definition: jpeglib.h:484
#define C2_SCALE
Definition: jquant2.cpp:97
#define STEP_C1
#define BOX_C0_ELEMS
Definition: jquant2.cpp:628
static void fill_inverse_cmap ( j_decompress_ptr  cinfo,
int  c0,
int  c1,
int  c2 
)
static
859 {
860  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
861  hist3d histogram = cquantize->histogram;
862  int minc0, minc1, minc2; /* lower left corner of update box */
863  int ic0, ic1, ic2;
864  register JSAMPLE * cptr; /* pointer into bestcolor[] array */
865  register histptr cachep; /* pointer into main cache array */
866  /* This array lists the candidate colormap indexes. */
867  JSAMPLE colorlist[MAXNUMCOLORS];
868  int numcolors; /* number of candidate colors */
869  /* This array holds the actually closest colormap index for each cell. */
871 
872  /* Convert cell coordinates to update box ID */
873  c0 >>= BOX_C0_LOG;
874  c1 >>= BOX_C1_LOG;
875  c2 >>= BOX_C2_LOG;
876 
877  /* Compute true coordinates of update box's origin corner.
878  * Actually we compute the coordinates of the center of the corner
879  * histogram cell, which are the lower bounds of the volume we care about.
880  */
881  minc0 = (c0 << BOX_C0_SHIFT) + ((1 << C0_SHIFT) >> 1);
882  minc1 = (c1 << BOX_C1_SHIFT) + ((1 << C1_SHIFT) >> 1);
883  minc2 = (c2 << BOX_C2_SHIFT) + ((1 << C2_SHIFT) >> 1);
884 
885  /* Determine which colormap entries are close enough to be candidates
886  * for the nearest entry to some cell in the update box.
887  */
888  numcolors = find_nearby_colors(cinfo, minc0, minc1, minc2, colorlist);
889 
890  /* Determine the actually nearest colors. */
891  find_best_colors(cinfo, minc0, minc1, minc2, numcolors, colorlist,
892  bestcolor);
893 
894  /* Save the best color numbers (plus 1) in the main cache array */
895  c0 <<= BOX_C0_LOG; /* convert ID back to base cell indexes */
896  c1 <<= BOX_C1_LOG;
897  c2 <<= BOX_C2_LOG;
898  cptr = bestcolor;
899  for (ic0 = 0; ic0 < BOX_C0_ELEMS; ic0++) {
900  for (ic1 = 0; ic1 < BOX_C1_ELEMS; ic1++) {
901  cachep = & histogram[c0+ic0][c1+ic1][c2];
902  for (ic2 = 0; ic2 < BOX_C2_ELEMS; ic2++) {
903  *cachep++ = (histcell) (GETJSAMPLE(*cptr++) + 1);
904  }
905  }
906  }
907 }
char JSAMPLE
Definition: jmorecfg.h:64
my_cquantizer * my_cquantize_ptr
Definition: jquant2.cpp:211
uint16_t histcell
Definition: jquant2.cpp:147
#define GETJSAMPLE(value)
Definition: jmorecfg.h:68
#define BOX_C1_ELEMS
Definition: jquant2.cpp:629
histcell * histptr
Definition: jquant2.cpp:149
#define BOX_C2_LOG
Definition: jquant2.cpp:626
#define MAXNUMCOLORS
Definition: jquant2.cpp:127
hist2d * hist3d
Definition: jquant2.cpp:153
#define C0_SHIFT
Definition: jquant2.cpp:142
struct jpeg_color_quantizer * cquantize
Definition: jpeglib.h:634
#define BOX_C0_LOG
Definition: jquant2.cpp:624
#define BOX_C1_LOG
Definition: jquant2.cpp:625
static int find_nearby_colors(j_decompress_ptr cinfo, int minc0, int minc1, int minc2, JSAMPLE colorlist[])
Definition: jquant2.cpp:646
#define BOX_C2_ELEMS
Definition: jquant2.cpp:630
Definition: jquant1.cpp:142
hist3d histogram
Definition: jquant2.cpp:201
#define BOX_C0_ELEMS
Definition: jquant2.cpp:628
#define BOX_C0_SHIFT
Definition: jquant2.cpp:632
static void find_best_colors(j_decompress_ptr cinfo, int minc0, int minc1, int minc2, int numcolors, JSAMPLE colorlist[], JSAMPLE bestcolor[])
Definition: jquant2.cpp:775
static void pass2_no_dither ( j_decompress_ptr  cinfo,
JSAMPARRAY  input_buf,
JSAMPARRAY  output_buf,
int  num_rows 
)
static
918 {
919  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
920  hist3d histogram = cquantize->histogram;
921  register JSAMPROW inptr, outptr;
922  register histptr cachep;
923  register int c0, c1, c2;
924  int row;
925  JDIMENSION col;
926  JDIMENSION width = cinfo->output_width;
927 
928  for (row = 0; row < num_rows; row++) {
929  inptr = input_buf[row];
930  outptr = output_buf[row];
931  for (col = width; col > 0; col--) {
932  /* get pixel value and index into the cache */
933  c0 = GETJSAMPLE(*inptr++) >> C0_SHIFT;
934  c1 = GETJSAMPLE(*inptr++) >> C1_SHIFT;
935  c2 = GETJSAMPLE(*inptr++) >> C2_SHIFT;
936  cachep = & histogram[c0][c1][c2];
937  /* If we have not seen this color before, find nearest colormap entry */
938  /* and update the cache */
939  if (*cachep == 0)
940  fill_inverse_cmap(cinfo, c0,c1,c2);
941  /* Now emit the colormap index for this cell */
942  *outptr++ = (JSAMPLE) (*cachep - 1);
943  }
944  }
945 }
char JSAMPLE
Definition: jmorecfg.h:64
my_cquantizer * my_cquantize_ptr
Definition: jquant2.cpp:211
#define GETJSAMPLE(value)
Definition: jmorecfg.h:68
JDIMENSION output_width
Definition: jpeglib.h:464
histcell * histptr
Definition: jquant2.cpp:149
JSAMPLE * JSAMPROW
Definition: jpeglib.h:71
hist2d * hist3d
Definition: jquant2.cpp:153
#define C0_SHIFT
Definition: jquant2.cpp:142
struct jpeg_color_quantizer * cquantize
Definition: jpeglib.h:634
int JSAMPARRAY int int num_rows
Definition: jpegint.h:373
Definition: jquant1.cpp:142
static void fill_inverse_cmap(j_decompress_ptr cinfo, int c0, int c1, int c2)
Definition: jquant2.cpp:855
jpeg_component_info JCOEFPTR JSAMPARRAY output_buf
Definition: jdct.h:102
unsigned int JDIMENSION
Definition: jmorecfg.h:171
#define C2_SHIFT
Definition: jquant2.cpp:144
#define C1_SHIFT
Definition: jquant2.cpp:143
hist3d histogram
Definition: jquant2.cpp:201
static void pass2_fs_dither ( j_decompress_ptr  cinfo,
JSAMPARRAY  input_buf,
JSAMPARRAY  output_buf,
int  num_rows 
)
static
952 {
953  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
954  hist3d histogram = cquantize->histogram;
955  register LOCFSERROR cur0, cur1, cur2; /* current error or pixel value */
956  LOCFSERROR belowerr0, belowerr1, belowerr2; /* error for pixel below cur */
957  LOCFSERROR bpreverr0, bpreverr1, bpreverr2; /* error for below/prev col */
958  register FSERRPTR errorptr; /* => fserrors[] at column before current */
959  JSAMPROW inptr; /* => current input pixel */
960  JSAMPROW outptr; /* => current output pixel */
961  histptr cachep;
962  int dir; /* +1 or -1 depending on direction */
963  int dir3; /* 3*dir, for advancing inptr & errorptr */
964  int row;
965  JDIMENSION col;
966  JDIMENSION width = cinfo->output_width;
967  JSAMPLE *range_limit = cinfo->sample_range_limit;
968  int *error_limit = cquantize->error_limiter;
969  JSAMPROW colormap0 = cinfo->colormap[0];
970  JSAMPROW colormap1 = cinfo->colormap[1];
971  JSAMPROW colormap2 = cinfo->colormap[2];
973 
974  for (row = 0; row < num_rows; row++) {
975  inptr = input_buf[row];
976  outptr = output_buf[row];
977  if (cquantize->on_odd_row) {
978  /* work right to left in this row */
979  inptr += (width-1) * 3; /* so point to rightmost pixel */
980  outptr += width-1;
981  dir = -1;
982  dir3 = -3;
983  errorptr = cquantize->fserrors + (width+1)*3; /* => entry after last column */
984  cquantize->on_odd_row = FALSE; /* flip for next time */
985  } else {
986  /* work left to right in this row */
987  dir = 1;
988  dir3 = 3;
989  errorptr = cquantize->fserrors; /* => entry before first real column */
990  cquantize->on_odd_row = TRUE; /* flip for next time */
991  }
992  /* Preset error values: no error propagated to first pixel from left */
993  cur0 = cur1 = cur2 = 0;
994  /* and no error propagated to row below yet */
995  belowerr0 = belowerr1 = belowerr2 = 0;
996  bpreverr0 = bpreverr1 = bpreverr2 = 0;
997 
998  for (col = width; col > 0; col--) {
999  /* curN holds the error propagated from the previous pixel on the
1000  * current line. Add the error propagated from the previous line
1001  * to form the complete error correction term for this pixel, and
1002  * round the error term (which is expressed * 16) to an integer.
1003  * RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
1004  * for either sign of the error value.
1005  * Note: errorptr points to *previous* column's array entry.
1006  */
1007  cur0 = RIGHT_SHIFT(cur0 + errorptr[dir3+0] + 8, 4);
1008  cur1 = RIGHT_SHIFT(cur1 + errorptr[dir3+1] + 8, 4);
1009  cur2 = RIGHT_SHIFT(cur2 + errorptr[dir3+2] + 8, 4);
1010  /* Limit the error using transfer function set by init_error_limit.
1011  * See comments with init_error_limit for rationale.
1012  */
1013  cur0 = error_limit[cur0];
1014  cur1 = error_limit[cur1];
1015  cur2 = error_limit[cur2];
1016  /* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
1017  * The maximum error is +- MAXJSAMPLE (or less with error limiting);
1018  * this sets the required size of the range_limit array.
1019  */
1020  cur0 += GETJSAMPLE(inptr[0]);
1021  cur1 += GETJSAMPLE(inptr[1]);
1022  cur2 += GETJSAMPLE(inptr[2]);
1023  cur0 = GETJSAMPLE(range_limit[cur0]);
1024  cur1 = GETJSAMPLE(range_limit[cur1]);
1025  cur2 = GETJSAMPLE(range_limit[cur2]);
1026  /* Index into the cache with adjusted pixel value */
1027  cachep = & histogram[cur0>>C0_SHIFT][cur1>>C1_SHIFT][cur2>>C2_SHIFT];
1028  /* If we have not seen this color before, find nearest colormap */
1029  /* entry and update the cache */
1030  if (*cachep == 0)
1031  fill_inverse_cmap(cinfo, cur0>>C0_SHIFT,cur1>>C1_SHIFT,cur2>>C2_SHIFT);
1032  /* Now emit the colormap index for this cell */
1033  { register int pixcode = *cachep - 1;
1034  *outptr = (JSAMPLE) pixcode;
1035  /* Compute representation error for this pixel */
1036  cur0 -= GETJSAMPLE(colormap0[pixcode]);
1037  cur1 -= GETJSAMPLE(colormap1[pixcode]);
1038  cur2 -= GETJSAMPLE(colormap2[pixcode]);
1039  }
1040  /* Compute error fractions to be propagated to adjacent pixels.
1041  * Add these into the running sums, and simultaneously shift the
1042  * next-line error sums left by 1 column.
1043  */
1044  { register LOCFSERROR bnexterr, delta;
1045 
1046  bnexterr = cur0; /* Process component 0 */
1047  delta = cur0 * 2;
1048  cur0 += delta; /* form error * 3 */
1049  errorptr[0] = (FSERROR) (bpreverr0 + cur0);
1050  cur0 += delta; /* form error * 5 */
1051  bpreverr0 = belowerr0 + cur0;
1052  belowerr0 = bnexterr;
1053  cur0 += delta; /* form error * 7 */
1054  bnexterr = cur1; /* Process component 1 */
1055  delta = cur1 * 2;
1056  cur1 += delta; /* form error * 3 */
1057  errorptr[1] = (FSERROR) (bpreverr1 + cur1);
1058  cur1 += delta; /* form error * 5 */
1059  bpreverr1 = belowerr1 + cur1;
1060  belowerr1 = bnexterr;
1061  cur1 += delta; /* form error * 7 */
1062  bnexterr = cur2; /* Process component 2 */
1063  delta = cur2 * 2;
1064  cur2 += delta; /* form error * 3 */
1065  errorptr[2] = (FSERROR) (bpreverr2 + cur2);
1066  cur2 += delta; /* form error * 5 */
1067  bpreverr2 = belowerr2 + cur2;
1068  belowerr2 = bnexterr;
1069  cur2 += delta; /* form error * 7 */
1070  }
1071  /* At this point curN contains the 7/16 error value to be propagated
1072  * to the next pixel on the current line, and all the errors for the
1073  * next line have been shifted over. We are therefore ready to move on.
1074  */
1075  inptr += dir3; /* Advance pixel pointers to next column */
1076  outptr += dir;
1077  errorptr += dir3; /* advance errorptr to current column */
1078  }
1079  /* Post-loop cleanup: we must unload the final error values into the
1080  * final fserrors[] entry. Note we need not unload belowerrN because
1081  * it is for the dummy column before or after the actual array.
1082  */
1083  errorptr[0] = (FSERROR) bpreverr0; /* unload prev errs into array */
1084  errorptr[1] = (FSERROR) bpreverr1;
1085  errorptr[2] = (FSERROR) bpreverr2;
1086  }
1087 }
FSERROR * FSERRPTR
Definition: jquant1.cpp:135
char JSAMPLE
Definition: jmorecfg.h:64
my_cquantizer * my_cquantize_ptr
Definition: jquant2.cpp:211
static double delta
Definition: agent_exec-2-0.cpp:141
int * error_limiter
Definition: jquant2.cpp:208
int16_t FSERROR
Definition: jquant2.cpp:181
#define GETJSAMPLE(value)
Definition: jmorecfg.h:68
JDIMENSION output_width
Definition: jpeglib.h:464
histcell * histptr
Definition: jquant2.cpp:149
#define SHIFT_TEMPS
Definition: jpegint.h:289
JSAMPLE * JSAMPROW
Definition: jpeglib.h:71
#define FALSE
Definition: jpleph.cpp:69
hist2d * hist3d
Definition: jquant2.cpp:153
JSAMPLE * sample_range_limit
Definition: jpeglib.h:594
#define C0_SHIFT
Definition: jquant2.cpp:142
struct jpeg_color_quantizer * cquantize
Definition: jpeglib.h:634
int JSAMPARRAY int int num_rows
Definition: jpegint.h:373
#define TRUE
Definition: jpleph.cpp:68
int LOCFSERROR
Definition: jquant1.cpp:129
JSAMPARRAY colormap
Definition: jpeglib.h:484
boolean on_odd_row
Definition: jquant1.cpp:164
Definition: jquant1.cpp:142
#define RIGHT_SHIFT(x, shft)
Definition: jpegint.h:290
static void fill_inverse_cmap(j_decompress_ptr cinfo, int c0, int c1, int c2)
Definition: jquant2.cpp:855
FSERRPTR fserrors[4]
Definition: jquant1.cpp:163
jpeg_component_info JCOEFPTR JSAMPARRAY output_buf
Definition: jdct.h:102
unsigned int JDIMENSION
Definition: jmorecfg.h:171
#define C2_SHIFT
Definition: jquant2.cpp:144
#define C1_SHIFT
Definition: jquant2.cpp:143
hist3d histogram
Definition: jquant2.cpp:201
static void init_error_limit ( j_decompress_ptr  cinfo)
static
1110 {
1111  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
1112  int * table;
1113  int in, out;
1114 
1115  table = (int *) (*cinfo->mem->alloc_small)
1116  ((j_common_ptr) cinfo, JPOOL_IMAGE, (MAXJSAMPLE*2+1) * SIZEOF(int));
1117  table += MAXJSAMPLE; /* so can index -MAXJSAMPLE .. +MAXJSAMPLE */
1118  cquantize->error_limiter = table;
1119 
1120 #define STEPSIZE ((MAXJSAMPLE+1)/16)
1121  /* Map errors 1:1 up to +- MAXJSAMPLE/16 */
1122  out = 0;
1123  for (in = 0; in < STEPSIZE; in++, out++) {
1124  table[in] = out; table[-in] = -out;
1125  }
1126  /* Map errors 1:2 up to +- 3*MAXJSAMPLE/16 */
1127  for (; in < STEPSIZE*3; in++, out += (in&1) ? 0 : 1) {
1128  table[in] = out; table[-in] = -out;
1129  }
1130  /* Clamp the rest to final out value (which is (MAXJSAMPLE+1)/8) */
1131  for (; in <= MAXJSAMPLE; in++) {
1132  table[in] = out; table[-in] = -out;
1133  }
1134 #undef STEPSIZE
1135 }
my_cquantizer * my_cquantize_ptr
Definition: jquant2.cpp:211
int * error_limiter
Definition: jquant2.cpp:208
void *(* alloc_small)()
Definition: jpeglib.h:764
Definition: jpeglib.h:258
#define SIZEOF(object)
Definition: jinclude.h:80
#define MAXJSAMPLE
Definition: jmorecfg.h:73
#define JPOOL_IMAGE
Definition: jpeglib.h:754
struct jpeg_memory_mgr * mem
Definition: jpeglib.h:417
struct jpeg_color_quantizer * cquantize
Definition: jpeglib.h:634
Definition: jquant1.cpp:142
#define STEPSIZE
static void finish_pass1 ( j_decompress_ptr  cinfo)
static
1144 {
1145  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
1146 
1147  /* Select the representative colors and fill in cinfo->colormap */
1148  cinfo->colormap = cquantize->sv_colormap;
1149  select_colors(cinfo, cquantize->desired);
1150  /* Force next pass to zero the color index table */
1151  cquantize->needs_zeroed = TRUE;
1152 }
my_cquantizer * my_cquantize_ptr
Definition: jquant2.cpp:211
struct jpeg_color_quantizer * cquantize
Definition: jpeglib.h:634
#define TRUE
Definition: jpleph.cpp:68
JSAMPARRAY colormap
Definition: jpeglib.h:484
Definition: jquant1.cpp:142
static void select_colors(j_decompress_ptr cinfo, int desired_colors)
Definition: jquant2.cpp:539
boolean needs_zeroed
Definition: jquant2.cpp:203
JSAMPARRAY sv_colormap
Definition: jquant1.cpp:146
int desired
Definition: jquant2.cpp:198
static void finish_pass2 ( j_decompress_ptr  cinfo)
static
1157 {
1158  /* no work */
1159 }
static void start_pass_2_quant ( j_decompress_ptr  cinfo,
boolean  is_pre_scan 
)
static
1168 {
1169  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
1170  hist3d histogram = cquantize->histogram;
1171  int i;
1172 
1173  /* Only F-S dithering or no dithering is supported. */
1174  /* If user asks for ordered dither, give him F-S. */
1175  if (cinfo->dither_mode != JDITHER_NONE)
1176  cinfo->dither_mode = JDITHER_FS;
1177 
1178  if (is_pre_scan) {
1179  /* Set up method pointers */
1180  cquantize->pub.color_quantize = prescan_quantize;
1181  cquantize->pub.finish_pass = finish_pass1;
1182  cquantize->needs_zeroed = TRUE; /* Always zero histogram */
1183  } else {
1184  /* Set up method pointers */
1185  if (cinfo->dither_mode == JDITHER_FS)
1186  cquantize->pub.color_quantize = pass2_fs_dither;
1187  else
1188  cquantize->pub.color_quantize = pass2_no_dither;
1189  cquantize->pub.finish_pass = finish_pass2;
1190 
1191  /* Make sure color count is acceptable */
1192  i = cinfo->actual_number_of_colors;
1193  if (i < 1)
1194  ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 1);
1195  if (i > MAXNUMCOLORS)
1197 
1198  if (cinfo->dither_mode == JDITHER_FS) {
1199  size_t arraysize = (size_t) ((cinfo->output_width + 2) *
1200  (3 * SIZEOF(FSERROR)));
1201  /* Allocate Floyd-Steinberg workspace if we didn't already. */
1202  if (cquantize->fserrors == NULL)
1203  cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
1204  ((j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
1205  /* Initialize the propagated errors to zero. */
1206  jzero_far((void FAR *) cquantize->fserrors, arraysize);
1207  /* Make the error-limit table if we didn't already. */
1208  if (cquantize->error_limiter == NULL)
1209  init_error_limit(cinfo);
1210  cquantize->on_odd_row = FALSE;
1211  }
1212 
1213  }
1214  /* Zero the histogram or inverse color map, if necessary */
1215  if (cquantize->needs_zeroed) {
1216  for (i = 0; i < HIST_C0_ELEMS; i++) {
1217  jzero_far((void FAR *) histogram[i],
1219  }
1220  cquantize->needs_zeroed = FALSE;
1221  }
1222 }
#define HIST_C0_ELEMS
Definition: jquant2.cpp:137
my_cquantizer * my_cquantize_ptr
Definition: jquant2.cpp:211
static void finish_pass2(j_decompress_ptr cinfo)
Definition: jquant2.cpp:1156
Definition: jerror.h:105
int i
Definition: rw_test.cpp:37
FSERROR * FSERRPTR
Definition: jquant2.cpp:188
int * error_limiter
Definition: jquant2.cpp:208
struct jpeg_common_struct * j_common_ptr
Definition: jpeglib.h:266
uint16_t histcell
Definition: jquant2.cpp:147
JDIMENSION output_width
Definition: jpeglib.h:464
#define SIZEOF(object)
Definition: jinclude.h:80
#define MAXNUMCOLORS
Definition: jquant2.cpp:127
#define HIST_C1_ELEMS
Definition: jquant2.cpp:138
#define JPOOL_IMAGE
Definition: jpeglib.h:754
static void prescan_quantize(j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
Definition: jquant2.cpp:224
int16_t FSERROR
Definition: jquant1.cpp:128
struct jpeg_memory_mgr * mem
Definition: jpeglib.h:417
#define FALSE
Definition: jpleph.cpp:69
hist2d * hist3d
Definition: jquant2.cpp:153
struct jpeg_color_quantizer * cquantize
Definition: jpeglib.h:634
#define FAR
Definition: jmorecfg.h:215
#define HIST_C2_ELEMS
Definition: jquant2.cpp:139
#define TRUE
Definition: jpleph.cpp:68
Definition: jpeglib.h:238
J_DITHER_MODE dither_mode
Definition: jpeglib.h:450
static void pass2_no_dither(j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
Definition: jquant2.cpp:915
static void init_error_limit(j_decompress_ptr cinfo)
Definition: jquant2.cpp:1108
#define ERREXIT1(cinfo, code, p1)
Definition: jerror.h:208
struct jpeg_color_quantizer pub
Definition: jquant1.cpp:143
boolean on_odd_row
Definition: jquant1.cpp:164
Definition: jpeglib.h:240
Definition: jquant1.cpp:142
Definition: jerror.h:104
static void pass2_fs_dither(j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
Definition: jquant2.cpp:949
void *(* alloc_large)()
Definition: jpeglib.h:766
FSERRPTR fserrors[4]
Definition: jquant1.cpp:163
boolean needs_zeroed
Definition: jquant2.cpp:203
hist3d histogram
Definition: jquant2.cpp:201
void jzero_far(void *target, size_t bytestozero)
Definition: jutils.cpp:165
int actual_number_of_colors
Definition: jpeglib.h:483
static void finish_pass1(j_decompress_ptr cinfo)
Definition: jquant2.cpp:1143
static void new_color_map_2_quant ( j_decompress_ptr  cinfo)
static
1231 {
1232  my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
1233 
1234  /* Reset the inverse color map */
1235  cquantize->needs_zeroed = TRUE;
1236 }
my_cquantizer * my_cquantize_ptr
Definition: jquant2.cpp:211
struct jpeg_color_quantizer * cquantize
Definition: jpeglib.h:634
#define TRUE
Definition: jpleph.cpp:68
Definition: jquant1.cpp:142
boolean needs_zeroed
Definition: jquant2.cpp:203
void jinit_2pass_quantizer ( j_decompress_ptr  cinfo)
1245 {
1246  my_cquantize_ptr cquantize;
1247  int i;
1248 
1249  cquantize = (my_cquantize_ptr)
1250  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
1252  cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
1253  cquantize->pub.start_pass = start_pass_2_quant;
1254  cquantize->pub.new_color_map = new_color_map_2_quant;
1255  cquantize->fserrors = NULL; /* flag optional arrays not allocated */
1256  cquantize->error_limiter = NULL;
1257 
1258  /* Make sure jdmaster didn't give me a case I can't handle */
1259  if (cinfo->out_color_components != 3)
1260  ERREXIT(cinfo, JERR_NOTIMPL);
1261 
1262  /* Allocate the histogram/inverse colormap storage */
1263  cquantize->histogram = (hist3d) (*cinfo->mem->alloc_small)
1265  for (i = 0; i < HIST_C0_ELEMS; i++) {
1266  cquantize->histogram[i] = (hist2d) (*cinfo->mem->alloc_large)
1267  ((j_common_ptr) cinfo, JPOOL_IMAGE,
1269  }
1270  cquantize->needs_zeroed = TRUE; /* histogram is garbage now */
1271 
1272  /* Allocate storage for the completed colormap, if required.
1273  * We do this now since it is FAR storage and may affect
1274  * the memory manager's space calculations.
1275  */
1276  if (cinfo->enable_2pass_quant) {
1277  /* Make sure color count is acceptable */
1278  int desired = cinfo->desired_number_of_colors;
1279  /* Lower bound on # of colors ... somewhat arbitrary as long as > 0 */
1280  if (desired < 8)
1281  ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 8);
1282  /* Make sure colormap indexes can be represented by JSAMPLEs */
1283  if (desired > MAXNUMCOLORS)
1285  cquantize->sv_colormap = (*cinfo->mem->alloc_sarray)
1286  ((j_common_ptr) cinfo,JPOOL_IMAGE, (JDIMENSION) desired, (JDIMENSION) 3);
1287  cquantize->desired = desired;
1288  } else
1289  cquantize->sv_colormap = NULL;
1290 
1291  /* Only F-S dithering or no dithering is supported. */
1292  /* If user asks for ordered dither, give him F-S. */
1293  if (cinfo->dither_mode != JDITHER_NONE)
1294  cinfo->dither_mode = JDITHER_FS;
1295 
1296  /* Allocate Floyd-Steinberg workspace if necessary.
1297  * This isn't really needed until pass 2, but again it is FAR storage.
1298  * Although we will cope with a later change in dither_mode,
1299  * we do not promise to honor max_memory_to_use if dither_mode changes.
1300  */
1301  if (cinfo->dither_mode == JDITHER_FS) {
1302  cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
1303  ((j_common_ptr) cinfo, JPOOL_IMAGE,
1304  (size_t) ((cinfo->output_width + 2) * (3 * SIZEOF(FSERROR))));
1305  /* Might as well create the error-limiting table too. */
1306  init_error_limit(cinfo);
1307  }
1308 }
#define HIST_C0_ELEMS
Definition: jquant2.cpp:137
my_cquantizer * my_cquantize_ptr
Definition: jquant2.cpp:211
Definition: jerror.h:105
int i
Definition: rw_test.cpp:37
FSERROR * FSERRPTR
Definition: jquant2.cpp:188
int * error_limiter
Definition: jquant2.cpp:208
boolean enable_2pass_quant
Definition: jpeglib.h:456
struct jpeg_common_struct * j_common_ptr
Definition: jpeglib.h:266
uint16_t histcell
Definition: jquant2.cpp:147
void *(* alloc_small)()
Definition: jpeglib.h:764
#define ERREXIT(cinfo, code)
Definition: jerror.h:205
Definition: jpeglib.h:258
JDIMENSION output_width
Definition: jpeglib.h:464
#define SIZEOF(object)
Definition: jinclude.h:80
int desired_number_of_colors
Definition: jpeglib.h:452
#define MAXNUMCOLORS
Definition: jquant2.cpp:127
#define HIST_C1_ELEMS
Definition: jquant2.cpp:138
#define JPOOL_IMAGE
Definition: jpeglib.h:754
int16_t FSERROR
Definition: jquant1.cpp:128
struct jpeg_memory_mgr * mem
Definition: jpeglib.h:417
hist2d * hist3d
Definition: jquant2.cpp:153
struct jpeg_color_quantizer * cquantize
Definition: jpeglib.h:634
int out_color_components
Definition: jpeglib.h:466
#define HIST_C2_ELEMS
Definition: jquant2.cpp:139
#define TRUE
Definition: jpleph.cpp:68
Definition: jpeglib.h:238
Definition: jerror.h:94
J_DITHER_MODE dither_mode
Definition: jpeglib.h:450
static void init_error_limit(j_decompress_ptr cinfo)
Definition: jquant2.cpp:1108
#define ERREXIT1(cinfo, code, p1)
Definition: jerror.h:208
struct jpeg_color_quantizer pub
Definition: jquant1.cpp:143
Definition: jpeglib.h:240
hist1d * hist2d
Definition: jquant2.cpp:152
Definition: jpegint.h:254
Definition: jquant1.cpp:142
Definition: jerror.h:104
void *(* alloc_large)()
Definition: jpeglib.h:766
static void new_color_map_2_quant(j_decompress_ptr cinfo)
Definition: jquant2.cpp:1230
FSERRPTR fserrors[4]
Definition: jquant1.cpp:163
JSAMPARRAY(* alloc_sarray)()
Definition: jpeglib.h:769
unsigned int JDIMENSION
Definition: jmorecfg.h:171
boolean needs_zeroed
Definition: jquant2.cpp:203
hist3d histogram
Definition: jquant2.cpp:201
JSAMPARRAY sv_colormap
Definition: jquant1.cpp:146
int desired
Definition: jquant2.cpp:198
static void start_pass_2_quant(j_decompress_ptr cinfo, boolean is_pre_scan)
Definition: jquant2.cpp:1167