10 #define JPEG_INTERNALS 15 #ifdef QUANT_2PASS_SUPPORTED 76 #define C0_SCALE R_SCALE 79 #define C0_SCALE B_SCALE 82 #define C1_SCALE G_SCALE 85 #define C2_SCALE R_SCALE 88 #define C2_SCALE B_SCALE 118 #define MAXNUMCOLORS (MAXJSAMPLE+1) 123 #define HIST_C0_BITS 5 124 #define HIST_C1_BITS 6 125 #define HIST_C2_BITS 5 128 #define HIST_C0_ELEMS (1<<HIST_C0_BITS) 129 #define HIST_C1_ELEMS (1<<HIST_C1_BITS) 130 #define HIST_C2_ELEMS (1<<HIST_C2_BITS) 133 #define C0_SHIFT (BITS_IN_JSAMPLE-HIST_C0_BITS) 134 #define C1_SHIFT (BITS_IN_JSAMPLE-HIST_C1_BITS) 135 #define C2_SHIFT (BITS_IN_JSAMPLE-HIST_C2_BITS) 171 #if BITS_IN_JSAMPLE == 8 229 for (col =
width; col > 0; col--) {
274 for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
294 for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
295 if (boxp->
volume > maxv) {
313 int c0min,c0max,c1min,c1max,c2min,c2max;
314 INT32 dist0,dist1,dist2;
317 c0min = boxp->c0min; c0max = boxp->c0max;
318 c1min = boxp->c1min; c1max = boxp->c1max;
319 c2min = boxp->c2min; c2max = boxp->c2max;
322 for (c0 = c0min; c0 <= c0max; c0++)
323 for (c1 = c1min; c1 <= c1max; c1++) {
325 for (c2 = c2min; c2 <= c2max; c2++)
327 boxp->c0min = c0min = c0;
333 for (c0 = c0max; c0 >= c0min; c0--)
334 for (c1 = c1min; c1 <= c1max; c1++) {
336 for (c2 = c2min; c2 <= c2max; c2++)
338 boxp->c0max = c0max = c0;
344 for (c1 = c1min; c1 <= c1max; c1++)
345 for (c0 = c0min; c0 <= c0max; c0++) {
347 for (c2 = c2min; c2 <= c2max; c2++)
349 boxp->c1min = c1min = c1;
355 for (c1 = c1max; c1 >= c1min; c1--)
356 for (c0 = c0min; c0 <= c0max; c0++) {
358 for (c2 = c2min; c2 <= c2max; c2++)
360 boxp->c1max = c1max = c1;
366 for (c2 = c2min; c2 <= c2max; c2++)
367 for (c0 = c0min; c0 <= c0max; c0++) {
371 boxp->c2min = c2min = c2;
377 for (c2 = c2max; c2 >= c2min; c2--)
378 for (c0 = c0min; c0 <= c0max; c0++) {
382 boxp->c2max = c2max = c2;
399 boxp->volume = dist0*dist0 + dist1*dist1 + dist2*dist2;
403 for (c0 = c0min; c0 <= c0max; c0++)
404 for (c1 = c1min; c1 <= c1max; c1++) {
406 for (c2 = c2min; c2 <= c2max; c2++, histp++)
411 boxp->colorcount = ccount;
424 while (numboxes < desired_colors) {
428 if (numboxes*2 <= desired_colors) {
435 b2 = &boxlist[numboxes];
437 b2->c0max =
b1->c0max;
b2->c1max =
b1->c1max;
b2->c2max =
b1->c2max;
438 b2->c0min =
b1->c0min;
b2->c1min =
b1->c1min;
b2->c2min =
b1->c2min;
451 if (c0 > cmax) { cmax = c0;
n = 0; }
452 if (c2 > cmax) {
n = 2; }
455 if (c2 > cmax) { cmax = c2;
n = 2; }
456 if (c0 > cmax) {
n = 0; }
466 lb = (
b1->c0max +
b1->c0min) / 2;
471 lb = (
b1->c1max +
b1->c1min) / 2;
476 lb = (
b1->c2max +
b1->c2min) / 2;
500 int c0min,c0max,c1min,c1max,c2min,c2max;
507 c0min = boxp->c0min; c0max = boxp->c0max;
508 c1min = boxp->c1min; c1max = boxp->c1max;
509 c2min = boxp->c2min; c2max = boxp->c2max;
511 for (c0 = c0min; c0 <= c0max; c0++)
512 for (c1 = c1min; c1 <= c1max; c1++) {
514 for (c2 = c2min; c2 <= c2max; c2++) {
515 if ((
count = *histp++) != 0) {
517 c0total += ((c0 << C0_SHIFT) + ((1<<C0_SHIFT)>>1)) *
count;
518 c1total += ((c1 << C1_SHIFT) + ((1<<C1_SHIFT)>>1)) *
count;
519 c2total += ((c2 << C2_SHIFT) + ((1<<C2_SHIFT)>>1)) *
count;
524 cinfo->colormap[0][icolor] = (
JSAMPLE) ((c0total + (total>>1)) / total);
525 cinfo->colormap[1][icolor] = (
JSAMPLE) ((c1total + (total>>1)) / total);
526 cinfo->colormap[2][icolor] = (
JSAMPLE) ((c2total + (total>>1)) / total);
539 boxlist = (
boxptr) (*cinfo->mem->alloc_small)
543 boxlist[0].
c0min = 0;
545 boxlist[0].
c1min = 0;
547 boxlist[0].
c2min = 0;
552 numboxes =
median_cut(cinfo, boxlist, numboxes, desired_colors);
554 for (i = 0; i < numboxes; i++)
556 cinfo->actual_number_of_colors = numboxes;
557 TRACEMS1(cinfo, 1, JTRC_QUANT_SELECTED, numboxes);
616 #define BOX_C0_LOG (HIST_C0_BITS-3) 617 #define BOX_C1_LOG (HIST_C1_BITS-3) 618 #define BOX_C2_LOG (HIST_C2_BITS-3) 620 #define BOX_C0_ELEMS (1<<BOX_C0_LOG) 621 #define BOX_C1_ELEMS (1<<BOX_C1_LOG) 622 #define BOX_C2_ELEMS (1<<BOX_C2_LOG) 624 #define BOX_C0_SHIFT (C0_SHIFT + BOX_C0_LOG) 625 #define BOX_C1_SHIFT (C1_SHIFT + BOX_C1_LOG) 626 #define BOX_C2_SHIFT (C2_SHIFT + BOX_C2_LOG) 649 int numcolors = cinfo->actual_number_of_colors;
650 int maxc0, maxc1, maxc2;
651 int centerc0, centerc1, centerc2;
653 INT32 minmaxdist, min_dist, max_dist, tdist;
663 centerc0 = (minc0 + maxc0) >> 1;
665 centerc1 = (minc1 + maxc1) >> 1;
667 centerc2 = (minc2 + maxc2) >> 1;
677 minmaxdist = 0x7FFFFFFFL;
679 for (i = 0; i < numcolors; i++) {
684 min_dist = tdist*tdist;
686 max_dist = tdist*tdist;
687 }
else if (
x > maxc0) {
689 min_dist = tdist*tdist;
691 max_dist = tdist*tdist;
697 max_dist = tdist*tdist;
700 max_dist = tdist*tdist;
707 min_dist += tdist*tdist;
709 max_dist += tdist*tdist;
710 }
else if (
x > maxc1) {
712 min_dist += tdist*tdist;
714 max_dist += tdist*tdist;
719 max_dist += tdist*tdist;
722 max_dist += tdist*tdist;
729 min_dist += tdist*tdist;
731 max_dist += tdist*tdist;
732 }
else if (
x > maxc2) {
734 min_dist += tdist*tdist;
736 max_dist += tdist*tdist;
741 max_dist += tdist*tdist;
744 max_dist += tdist*tdist;
748 mindist[i] = min_dist;
749 if (max_dist < minmaxdist)
750 minmaxdist = max_dist;
758 for (i = 0; i < numcolors; i++) {
759 if (mindist[i] <= minmaxdist)
760 colorlist[ncolors++] = (
JSAMPLE) i;
784 INT32 inc0, inc1, inc2;
791 *bptr++ = 0x7FFFFFFFL;
799 #define STEP_C0 ((1 << C0_SHIFT) * C0_SCALE) 800 #define STEP_C1 ((1 << C1_SHIFT) * C1_SCALE) 801 #define STEP_C2 ((1 << C2_SHIFT) * C2_SCALE) 803 for (i = 0; i < numcolors; i++) {
854 int minc0, minc1, minc2;
874 minc1 = (c1 << BOX_C1_SHIFT) + ((1 << C1_SHIFT) >> 1);
875 minc2 = (c2 << BOX_C2_SHIFT) + ((1 << C2_SHIFT) >> 1);
893 cachep = &
histogram[c0+ic0][c1+ic1][c2];
923 for (col =
width; col > 0; col--) {
934 *outptr++ = (
JSAMPLE) (*cachep - 1);
959 JSAMPLE *range_limit = cinfo->sample_range_limit;
961 JSAMPROW colormap0 = cinfo->colormap[0];
962 JSAMPROW colormap1 = cinfo->colormap[1];
963 JSAMPROW colormap2 = cinfo->colormap[2];
971 inptr += (
width-1) * 3;
985 cur0 = cur1 = cur2 = 0;
987 belowerr0 = belowerr1 = belowerr2 = 0;
988 bpreverr0 = bpreverr1 = bpreverr2 = 0;
990 for (col =
width; col > 0; col--) {
999 cur0 =
RIGHT_SHIFT(cur0 + errorptr[dir3+0] + 8, 4);
1000 cur1 =
RIGHT_SHIFT(cur1 + errorptr[dir3+1] + 8, 4);
1001 cur2 =
RIGHT_SHIFT(cur2 + errorptr[dir3+2] + 8, 4);
1005 cur0 = error_limit[cur0];
1006 cur1 = error_limit[cur1];
1007 cur2 = error_limit[cur2];
1025 {
int pixcode = *cachep - 1;
1041 errorptr[0] = (
FSERROR) (bpreverr0 + cur0);
1043 bpreverr0 = belowerr0 + cur0;
1044 belowerr0 = bnexterr;
1049 errorptr[1] = (
FSERROR) (bpreverr1 + cur1);
1051 bpreverr1 = belowerr1 + cur1;
1052 belowerr1 = bnexterr;
1057 errorptr[2] = (
FSERROR) (bpreverr2 + cur2);
1059 bpreverr2 = belowerr2 + cur2;
1060 belowerr2 = bnexterr;
1075 errorptr[0] = (
FSERROR) bpreverr0;
1076 errorptr[1] = (
FSERROR) bpreverr1;
1077 errorptr[2] = (
FSERROR) bpreverr2;
1107 table = (
int *) (*cinfo->mem->alloc_small)
1112 #define STEPSIZE ((MAXJSAMPLE+1)/16) 1184 i = cinfo->actual_number_of_colors;
1186 ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 1);
1191 size_t arraysize = (size_t) ((cinfo->output_width + 2) *
1251 if (cinfo->out_color_components != 3)
1268 if (cinfo->enable_2pass_quant) {
1270 int desired = cinfo->desired_number_of_colors;
1273 ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 8);
1277 cquantize->
sv_colormap = (*cinfo->mem->alloc_sarray)
1296 (
size_t) ((cinfo->output_width + 2) * (3 *
SIZEOF(
FSERROR))));
jzero_far(void FAR *target, size_t bytestozero)
GLuint GLuint GLsizei count
prescan_quantize(j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
my_cquantizer * my_cquantize_ptr
find_biggest_volume(boxptr boxlist, int numboxes)
pass2_no_dither(j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
init_error_limit(j_decompress_ptr cinfo)
select_colors(j_decompress_ptr cinfo, int desired_colors)
std::vector< double > histogram(const CONTAINER &v, double limit_min, double limit_max, size_t number_bins, bool do_normalization=false, std::vector< double > *out_bin_centers=NULL)
Computes the normalized or normal histogram of a sequence of numbers given the number of bins and the...
struct jpeg_common_struct * j_common_ptr
GLenum GLsizei GLenum GLenum const GLvoid * table
#define GETJSAMPLE(value)
histcell hist1d[HIST_C2_ELEMS]
#define ERREXIT(cinfo, code)
find_best_colors(j_decompress_ptr cinfo, int minc0, int minc1, int minc2, int numcolors, JSAMPLE colorlist[], JSAMPLE bestcolor[])
find_nearby_colors(j_decompress_ptr cinfo, int minc0, int minc1, int minc2, JSAMPLE colorlist[])
finish_pass2(j_decompress_ptr)
jinit_2pass_quantizer(j_decompress_ptr cinfo)
#define TRACEMS1(cinfo, lvl, code, p1)
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
new_color_map_2_quant(j_decompress_ptr cinfo)
find_biggest_color_pop(boxptr boxlist, int numboxes)
compute_color(j_decompress_ptr cinfo, boxptr boxp, int icolor)
update_box(j_decompress_ptr cinfo, boxptr boxp)
fill_inverse_cmap(j_decompress_ptr cinfo, int c0, int c1, int c2)
int JSAMPARRAY int int num_rows
median_cut(j_decompress_ptr cinfo, boxptr boxlist, int numboxes, int desired_colors)
#define ERREXIT1(cinfo, code, p1)
struct jpeg_color_quantizer pub
pass2_fs_dither(j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
GLenum GLenum GLvoid * row
#define RIGHT_SHIFT(x, shft)
FSERRPTR fserrors[MAX_Q_COMPS]
start_pass_2_quant(j_decompress_ptr cinfo, boolean is_pre_scan)
finish_pass1(j_decompress_ptr cinfo)