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 203 boolean needs_zeroed;
237 for (col =
width; col > 0; col--) {
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;
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;
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++) {
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++)
419 boxp->colorcount = ccount;
432 while (numboxes < desired_colors) {
436 if (numboxes*2 <= desired_colors) {
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;
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;
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++) {
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;
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];
975 inptr = input_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];
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)
1118 cquantize->error_limiter =
table;
1120 #define STEPSIZE ((MAXJSAMPLE+1)/16) 1148 cinfo->colormap = cquantize->sv_colormap;
1151 cquantize->needs_zeroed =
TRUE;
1182 cquantize->needs_zeroed =
TRUE;
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) *
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)
1210 cquantize->on_odd_row =
FALSE;
1215 if (cquantize->needs_zeroed) {
1220 cquantize->needs_zeroed =
FALSE;
1235 cquantize->needs_zeroed =
TRUE;
1255 cquantize->fserrors =
NULL;
1256 cquantize->error_limiter =
NULL;
1259 if (cinfo->out_color_components != 3)
1263 cquantize->histogram = (
hist3d) (*cinfo->mem->alloc_small)
1266 cquantize->histogram[
i] = (
hist2d) (*cinfo->mem->alloc_large)
1270 cquantize->needs_zeroed =
TRUE;
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)
1289 cquantize->sv_colormap =
NULL;
1302 cquantize->fserrors = (
FSERRPTR) (*cinfo->mem->alloc_large)
1304 (
size_t) ((cinfo->output_width + 2) * (3 *
SIZEOF(
FSERROR))));
new_color_map_2_quant(j_decompress_ptr cinfo)
for(n=1;n< outline->n_points;n++)
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)
GLint GLint GLsizei width
#define ERREXIT(cinfo, code)
GLint GLint GLint GLint GLint x
histcell hist1d[HIST_C2_ELEMS]
init_error_limit(j_decompress_ptr cinfo)
GLenum GLenum GLvoid * row
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
local int out(void *out_desc, unsigned char *buf, unsigned len)
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)
if(!abbox) return FT_THROW(Invalid_Argument)
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[])
my_cquantizer * my_cquantize_ptr
GLuint GLuint GLsizei count
jpeg_component_info JCOEFPTR JSAMPARRAY output_buf
finish_pass1(j_decompress_ptr cinfo)
GLenum GLsizei GLenum GLenum const GLvoid * table
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)