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) 180 #if BITS_IN_JSAMPLE == 8 237 for (col =
width; col > 0; col--) {
279 register long maxc = 0;
282 for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) {
299 register INT32 maxv = 0;
302 for (
i = 0, boxp = boxlist;
i < numboxes;
i++, boxp++) {
303 if (boxp->
volume > maxv) {
321 int c0min,c0max,c1min,c1max,c2min,c2max;
322 INT32 dist0,dist1,dist2;
330 for (c0 = c0min; c0 <= c0max; c0++)
331 for (c1 = c1min; c1 <= c1max; c1++) {
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++) {
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++) {
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++) {
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++) {
379 boxp->
c2min = c2min = c2;
385 for (c2 = c2max; c2 >= c2min; c2--)
386 for (c0 = c0min; c0 <= c0max; c0++) {
390 boxp->
c2max = c2max = c2;
407 boxp->
volume = dist0*dist0 + dist1*dist1 + dist2*dist2;
411 for (c0 = c0min; c0 <= c0max; c0++)
412 for (c1 = c1min; c1 <= c1max; c1++) {
414 for (c2 = c2min; c2 <= c2max; c2++, histp++)
432 while (numboxes < desired_colors) {
436 if (numboxes*2 <= desired_colors) {
443 b2 = &boxlist[numboxes];
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; }
508 int c0min,c0max,c1min,c1max,c2min,c2max;
519 for (c0 = c0min; c0 <= c0max; c0++)
520 for (c1 = c1min; c1 <= c1max; c1++) {
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;
560 numboxes =
median_cut(cinfo, boxlist, numboxes, desired_colors);
562 for (
i = 0;
i < numboxes;
i++)
564 cinfo->actual_number_of_colors = numboxes;
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) 657 int numcolors = cinfo->actual_number_of_colors;
658 int maxc0, maxc1, maxc2;
659 int centerc0, centerc1, centerc2;
661 INT32 minmaxdist, min_dist, max_dist, tdist;
671 centerc0 = (minc0 + maxc0) >> 1;
673 centerc1 = (minc1 + maxc1) >> 1;
675 centerc2 = (minc2 + maxc2) >> 1;
685 minmaxdist = 0x7FFFFFFFL;
687 for (i = 0; i < numcolors; i++) {
692 min_dist = tdist*tdist;
694 max_dist = tdist*tdist;
695 }
else if (x > maxc0) {
697 min_dist = tdist*tdist;
699 max_dist = tdist*tdist;
705 max_dist = tdist*tdist;
708 max_dist = tdist*tdist;
715 min_dist += tdist*tdist;
717 max_dist += tdist*tdist;
718 }
else if (x > maxc1) {
720 min_dist += tdist*tdist;
722 max_dist += tdist*tdist;
727 max_dist += tdist*tdist;
730 max_dist += tdist*tdist;
737 min_dist += tdist*tdist;
739 max_dist += tdist*tdist;
740 }
else if (x > maxc2) {
742 min_dist += tdist*tdist;
744 max_dist += tdist*tdist;
749 max_dist += tdist*tdist;
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;
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++) {
862 int minc0, minc1, minc2;
882 minc1 = (c1 << BOX_C1_SHIFT) + ((1 << C1_SHIFT) >> 1);
883 minc2 = (c2 << BOX_C2_SHIFT) + ((1 << C2_SHIFT) >> 1);
901 cachep = &
histogram[c0+ic0][c1+ic1][c2];
923 register int c0, c1, c2;
929 inptr = input_buf[
row];
931 for (col =
width; col > 0; col--) {
942 *outptr++ = (
JSAMPLE) (*cachep - 1);
967 JSAMPLE *range_limit = cinfo->sample_range_limit;
969 JSAMPROW colormap0 = cinfo->colormap[0];
970 JSAMPROW colormap1 = cinfo->colormap[1];
971 JSAMPROW colormap2 = cinfo->colormap[2];
975 inptr = input_buf[
row];
979 inptr += (
width-1) * 3;
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];
1033 {
register int pixcode = *cachep - 1;
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;
1115 table = (
int *) (*cinfo->mem->alloc_small)
1120 #define STEPSIZE ((MAXJSAMPLE+1)/16) 1123 for (in = 0; in <
STEPSIZE; in++, out++) {
1124 table[in] = out; table[-in] = -out;
1127 for (; in <
STEPSIZE*3; in++, out += (in&1) ? 0 : 1) {
1128 table[in] = out; table[-in] = -out;
1132 table[in] = out; table[-in] = -out;
1192 i = cinfo->actual_number_of_colors;
1194 ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 1);
1199 size_t arraysize = (size_t) ((cinfo->output_width + 2) *
1259 if (cinfo->out_color_components != 3)
1276 if (cinfo->enable_2pass_quant) {
1278 int desired = cinfo->desired_number_of_colors;
1281 ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 8);
1285 cquantize->
sv_colormap = (*cinfo->mem->alloc_sarray)
1304 (
size_t) ((cinfo->output_width + 2) * (3 *
SIZEOF(
FSERROR))));
new_color_map_2_quant(j_decompress_ptr cinfo)
median_cut(j_decompress_ptr cinfo, boxptr boxlist, int numboxes, int desired_colors)
struct jpeg_common_struct * j_common_ptr
png_colorp int int png_uint_16p histogram
#define GETJSAMPLE(value)
#define ERREXIT(cinfo, code)
histcell hist1d[HIST_C2_ELEMS]
png_infop png_uint_32 * width
init_error_limit(j_decompress_ptr cinfo)
pass2_fs_dither(j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
#define TRACEMS1(cinfo, lvl, code, p1)
int JSAMPARRAY int int num_rows
finish_pass2(j_decompress_ptr cinfo)
update_box(j_decompress_ptr cinfo, boxptr boxp)
jzero_far(void FAR *target, size_t bytestozero)
prescan_quantize(j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
compute_color(j_decompress_ptr cinfo, boxptr boxp, int icolor)
#define ERREXIT1(cinfo, code, p1)
struct jpeg_color_quantizer pub
my_cquantizer * my_cquantize_ptr
find_nearby_colors(j_decompress_ptr cinfo, int minc0, int minc1, int minc2, JSAMPLE colorlist[])
fill_inverse_cmap(j_decompress_ptr cinfo, int c0, int c1, int c2)
#define RIGHT_SHIFT(x, shft)
start_pass_2_quant(j_decompress_ptr cinfo, boolean is_pre_scan)
find_biggest_volume(boxptr boxlist, int numboxes)
find_best_colors(j_decompress_ptr cinfo, int minc0, int minc1, int minc2, int numcolors, JSAMPLE colorlist[], JSAMPLE bestcolor[])
FSERRPTR fserrors[MAX_Q_COMPS]
jpeg_component_info JCOEFPTR JSAMPARRAY output_buf
finish_pass1(j_decompress_ptr cinfo)
jinit_2pass_quantizer(j_decompress_ptr cinfo)
pass2_no_dither(j_decompress_ptr cinfo, JSAMPARRAY input_buf, JSAMPARRAY output_buf, int num_rows)
find_biggest_color_pop(boxptr boxlist, int numboxes)
select_colors(j_decompress_ptr cinfo, int desired_colors)