20 #define JPEG_INTERNALS
24 #ifdef QUANT_2PASS_SUPPORTED
85 #define C0_SCALE R_SCALE
88 #define C0_SCALE B_SCALE
91 #define C1_SCALE G_SCALE
94 #define C2_SCALE R_SCALE
97 #define C2_SCALE B_SCALE
127 #define MAXNUMCOLORS (MAXJSAMPLE+1)
132 #define HIST_C0_BITS 5
133 #define HIST_C1_BITS 6
134 #define HIST_C2_BITS 5
137 #define HIST_C0_ELEMS (1<<HIST_C0_BITS)
138 #define HIST_C1_ELEMS (1<<HIST_C1_BITS)
139 #define HIST_C2_ELEMS (1<<HIST_C2_BITS)
142 #define C0_SHIFT (BITS_IN_JSAMPLE-HIST_C0_BITS)
143 #define C1_SHIFT (BITS_IN_JSAMPLE-HIST_C1_BITS)
144 #define C2_SHIFT (BITS_IN_JSAMPLE-HIST_C2_BITS)
149 typedef histcell
FAR * histptr;
151 typedef histcell hist1d[HIST_C2_ELEMS];
152 typedef hist1d
FAR * hist2d;
153 typedef hist2d * hist3d;
180 #if BITS_IN_JSAMPLE == 8
181 typedef INT16 FSERROR;
182 typedef int LOCFSERROR;
184 typedef INT32 FSERROR;
185 typedef INT32 LOCFSERROR;
188 typedef FSERROR
FAR *FSERRPTR;
203 boolean needs_zeroed;
211 typedef my_cquantizer * my_cquantize_ptr;
227 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
229 register histptr histp;
230 register hist3d histogram = cquantize->histogram;
236 ptr = input_buf[
row];
237 for (col =
width; col > 0; col--) {
239 histp = & histogram[
GETJSAMPLE(ptr[0]) >> C0_SHIFT]
269 typedef box * boxptr;
273 find_biggest_color_pop (boxptr boxlist,
int numboxes)
277 register boxptr boxp;
279 register long maxc = 0;
282 for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
283 if (boxp->colorcount > maxc && boxp->volume > 0) {
285 maxc = boxp->colorcount;
293 find_biggest_volume (boxptr boxlist,
int numboxes)
297 register boxptr boxp;
299 register INT32 maxv = 0;
302 for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
303 if (boxp->volume > maxv) {
317 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
318 hist3d histogram = cquantize->histogram;
321 int c0min,c0max,c1min,c1max,c2min,c2max;
322 INT32 dist0,dist1,dist2;
325 c0min = boxp->c0min; c0max = boxp->c0max;
326 c1min = boxp->c1min; c1max = boxp->c1max;
327 c2min = boxp->c2min; c2max = boxp->c2max;
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++)
335 boxp->c0min = c0min = c0;
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++)
346 boxp->c0max = c0max = c0;
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++)
357 boxp->c1min = c1min = c1;
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++)
368 boxp->c1max = c1max = c1;
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)
379 boxp->c2min = c2min = c2;
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)
390 boxp->c2max = c2max = c2;
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;
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++)
419 boxp->colorcount = ccount;
430 register boxptr b1,b2;
432 while (numboxes < desired_colors) {
436 if (numboxes*2 <= desired_colors) {
437 b1 = find_biggest_color_pop(boxlist, numboxes);
439 b1 = find_biggest_volume(boxlist, numboxes);
443 b2 = &boxlist[numboxes];
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;
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;
459 if (c0 > cmax) { cmax = c0; n = 0; }
460 if (c2 > cmax) { n = 2; }
463 if (c2 > cmax) { cmax = c2; n = 2; }
464 if (c0 > cmax) { n = 0; }
474 lb = (b1->c0max + b1->c0min) / 2;
479 lb = (b1->c1max + b1->c1min) / 2;
484 lb = (b1->c2max + b1->c2min) / 2;
490 update_box(cinfo, b1);
491 update_box(cinfo, b2);
504 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
505 hist3d histogram = cquantize->histogram;
508 int c0min,c0max,c1min,c1max,c2min,c2max;
515 c0min = boxp->c0min; c0max = boxp->c0max;
516 c1min = boxp->c1min; c1max = boxp->c1max;
517 c2min = boxp->c2min; c2max = boxp->c2max;
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) {
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;
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);
547 boxlist = (boxptr) (*cinfo->mem->alloc_small)
551 boxlist[0].c0min = 0;
553 boxlist[0].c1min = 0;
555 boxlist[0].c2min = 0;
558 update_box(cinfo, & boxlist[0]);
560 numboxes = median_cut(cinfo, boxlist, numboxes, desired_colors);
562 for (i = 0; i < numboxes; i++)
563 compute_color(cinfo, & boxlist[i], i);
565 TRACEMS1(cinfo, 1, JTRC_QUANT_SELECTED, numboxes);
624 #define BOX_C0_LOG (HIST_C0_BITS-3)
625 #define BOX_C1_LOG (HIST_C1_BITS-3)
626 #define BOX_C2_LOG (HIST_C2_BITS-3)
628 #define BOX_C0_ELEMS (1<<BOX_C0_LOG)
629 #define BOX_C1_ELEMS (1<<BOX_C1_LOG)
630 #define BOX_C2_ELEMS (1<<BOX_C2_LOG)
632 #define BOX_C0_SHIFT (C0_SHIFT + BOX_C0_LOG)
633 #define BOX_C1_SHIFT (C1_SHIFT + BOX_C1_LOG)
634 #define BOX_C2_SHIFT (C2_SHIFT + BOX_C2_LOG)
646 find_nearby_colors (
j_decompress_ptr cinfo,
int minc0,
int minc1,
int minc2,
658 int maxc0, maxc1, maxc2;
659 int centerc0, centerc1, centerc2;
661 INT32 minmaxdist, min_dist, max_dist, tdist;
662 INT32 mindist[MAXNUMCOLORS];
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;
685 minmaxdist = 0x7FFFFFFF
L;
687 for (i = 0; i < numcolors; i++) {
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;
704 tdist = (x - maxc0) * C0_SCALE;
705 max_dist = tdist*tdist;
707 tdist = (x - minc0) * C0_SCALE;
708 max_dist = tdist*tdist;
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;
726 tdist = (x - maxc1) * C1_SCALE;
727 max_dist += tdist*tdist;
729 tdist = (x - minc1) * C1_SCALE;
730 max_dist += tdist*tdist;
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;
748 tdist = (x - maxc2) * C2_SCALE;
749 max_dist += tdist*tdist;
751 tdist = (x - minc2) * C2_SCALE;
752 max_dist += tdist*tdist;
756 mindist[
i] = min_dist;
757 if (max_dist < minmaxdist)
758 minmaxdist = max_dist;
766 for (i = 0; i < numcolors; i++) {
767 if (mindist[i] <= minmaxdist)
768 colorlist[ncolors++] = (
JSAMPLE) i;
786 register INT32 * bptr;
789 register INT32 dist2;
792 INT32 inc0, inc1, inc2;
794 INT32 bestdist[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
798 for (i = BOX_C0_ELEMS*BOX_C1_ELEMS*BOX_C2_ELEMS-1; i >= 0; i--)
799 *bptr++ = 0x7FFFFFFFL;
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)
811 for (i = 0; i < numcolors; i++) {
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;
828 for (ic0 = BOX_C0_ELEMS-1; ic0 >= 0; ic0--) {
831 for (ic1 = BOX_C1_ELEMS-1; ic1 >= 0; ic1--) {
834 for (ic2 = BOX_C2_ELEMS-1; ic2 >= 0; ic2--) {
840 xx2 += 2 * STEP_C2 * STEP_C2;
845 xx1 += 2 * STEP_C1 * STEP_C1;
848 xx0 += 2 * STEP_C0 * STEP_C0;
860 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
861 hist3d histogram = cquantize->histogram;
862 int minc0, minc1, minc2;
865 register histptr cachep;
867 JSAMPLE colorlist[MAXNUMCOLORS];
870 JSAMPLE bestcolor[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
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);
888 numcolors = find_nearby_colors(cinfo, minc0, minc1, minc2, colorlist);
891 find_best_colors(cinfo, minc0, minc1, minc2, numcolors, colorlist,
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);
919 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
920 hist3d histogram = cquantize->histogram;
922 register histptr cachep;
923 register int c0, c1, c2;
929 inptr = input_buf[
row];
930 outptr = output_buf[
row];
931 for (col =
width; col > 0; col--) {
936 cachep = & histogram[c0][c1][c2];
940 fill_inverse_cmap(cinfo, c0,c1,c2);
942 *outptr++ = (
JSAMPLE) (*cachep - 1);
953 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
954 hist3d histogram = cquantize->histogram;
955 register LOCFSERROR cur0, cur1, cur2;
956 LOCFSERROR belowerr0, belowerr1, belowerr2;
957 LOCFSERROR bpreverr0, bpreverr1, bpreverr2;
958 register FSERRPTR errorptr;
968 int *error_limit = cquantize->error_limiter;
975 inptr = input_buf[
row];
976 outptr = output_buf[
row];
977 if (cquantize->on_odd_row) {
979 inptr += (
width-1) * 3;
983 errorptr = cquantize->fserrors + (
width+1)*3;
984 cquantize->on_odd_row =
FALSE;
989 errorptr = cquantize->fserrors;
990 cquantize->on_odd_row =
TRUE;
993 cur0 = cur1 = cur2 = 0;
995 belowerr0 = belowerr1 = belowerr2 = 0;
996 bpreverr0 = bpreverr1 = bpreverr2 = 0;
998 for (col =
width; col > 0; col--) {
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);
1013 cur0 = error_limit[cur0];
1014 cur1 = error_limit[cur1];
1015 cur2 = error_limit[cur2];
1027 cachep = & histogram[cur0>>C0_SHIFT][cur1>>C1_SHIFT][cur2>>C2_SHIFT];
1031 fill_inverse_cmap(cinfo, cur0>>C0_SHIFT,cur1>>C1_SHIFT,cur2>>C2_SHIFT);
1033 {
register int pixcode = *cachep - 1;
1044 {
register LOCFSERROR bnexterr, delta;
1049 errorptr[0] = (FSERROR) (bpreverr0 + cur0);
1051 bpreverr0 = belowerr0 + cur0;
1052 belowerr0 = bnexterr;
1057 errorptr[1] = (FSERROR) (bpreverr1 + cur1);
1059 bpreverr1 = belowerr1 + cur1;
1060 belowerr1 = bnexterr;
1065 errorptr[2] = (FSERROR) (bpreverr2 + cur2);
1067 bpreverr2 = belowerr2 + cur2;
1068 belowerr2 = bnexterr;
1083 errorptr[0] = (FSERROR) bpreverr0;
1084 errorptr[1] = (FSERROR) bpreverr1;
1085 errorptr[2] = (FSERROR) bpreverr2;
1111 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
1115 table = (
int *) (*cinfo->mem->alloc_small)
1118 cquantize->error_limiter =
table;
1120 #define STEPSIZE ((MAXJSAMPLE+1)/16)
1123 for (in = 0; in < STEPSIZE; in++, out++) {
1127 for (; in < STEPSIZE*3; in++, out += (in&1) ? 0 : 1) {
1145 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
1148 cinfo->
colormap = cquantize->sv_colormap;
1149 select_colors(cinfo, cquantize->desired);
1151 cquantize->needs_zeroed =
TRUE;
1169 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
1170 hist3d histogram = cquantize->histogram;
1180 cquantize->pub.color_quantize = prescan_quantize;
1181 cquantize->pub.finish_pass = finish_pass1;
1182 cquantize->needs_zeroed =
TRUE;
1186 cquantize->pub.color_quantize = pass2_fs_dither;
1188 cquantize->pub.color_quantize = pass2_no_dither;
1189 cquantize->pub.finish_pass = finish_pass2;
1194 ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 1);
1195 if (i > MAXNUMCOLORS)
1196 ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);
1199 size_t arraysize = (size_t) ((cinfo->
output_width + 2) *
1202 if (cquantize->fserrors ==
NULL)
1203 cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
1206 jzero_far((
void FAR *) cquantize->fserrors, arraysize);
1208 if (cquantize->error_limiter ==
NULL)
1209 init_error_limit(cinfo);
1210 cquantize->on_odd_row =
FALSE;
1215 if (cquantize->needs_zeroed) {
1216 for (i = 0; i < HIST_C0_ELEMS; i++) {
1218 HIST_C1_ELEMS*HIST_C2_ELEMS *
SIZEOF(histcell));
1220 cquantize->needs_zeroed =
FALSE;
1232 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
1235 cquantize->needs_zeroed =
TRUE;
1246 my_cquantize_ptr cquantize;
1249 cquantize = (my_cquantize_ptr)
1250 (*cinfo->mem->alloc_small) ((
j_common_ptr) cinfo, JPOOL_IMAGE,
1253 cquantize->pub.start_pass = start_pass_2_quant;
1254 cquantize->pub.new_color_map = new_color_map_2_quant;
1255 cquantize->fserrors =
NULL;
1256 cquantize->error_limiter =
NULL;
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)
1268 HIST_C1_ELEMS*HIST_C2_ELEMS *
SIZEOF(histcell));
1270 cquantize->needs_zeroed =
TRUE;
1281 ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 8);
1283 if (desired > MAXNUMCOLORS)
1284 ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);
1285 cquantize->sv_colormap = (*cinfo->mem->alloc_sarray)
1287 cquantize->desired = desired;
1289 cquantize->sv_colormap =
NULL;
1302 cquantize->fserrors = (FSERRPTR) (*cinfo->mem->alloc_large)
1306 init_error_limit(cinfo);
boolean enable_2pass_quant
struct jpeg_common_struct * j_common_ptr
GLenum GLsizei GLenum GLenum const GLvoid * table
#define GETJSAMPLE(value)
#define ERREXIT(cinfo, code)
int desired_number_of_colors
#define TRACEMS1(cinfo, lvl, code, p1)
GLuint GLuint GLsizei count
JSAMPLE * sample_range_limit
struct jpeg_color_quantizer * cquantize
J_DITHER_MODE dither_mode
#define ERREXIT1(cinfo, code, p1)
GLOBAL void jzero_far(void FAR *target, size_t bytestozero)
GLenum GLenum GLvoid * row
#define RIGHT_SHIFT(x, shft)
if(!ValidDisplayID(prefInfo.prefDisplayID)) prefInfo.prefDisplayID
int actual_number_of_colors