10 #define JPEG_INTERNALS 15 #ifdef QUANT_2PASS_SUPPORTED 75 #define C0_SCALE R_SCALE 78 #define C0_SCALE B_SCALE 81 #define C1_SCALE G_SCALE 84 #define C2_SCALE R_SCALE 87 #define C2_SCALE B_SCALE 116 #define MAXNUMCOLORS (MAXJSAMPLE + 1) 121 #define HIST_C0_BITS 5 122 #define HIST_C1_BITS 6 123 #define HIST_C2_BITS 5 126 #define HIST_C0_ELEMS (1 << HIST_C0_BITS) 127 #define HIST_C1_ELEMS (1 << HIST_C1_BITS) 128 #define HIST_C2_ELEMS (1 << HIST_C2_BITS) 131 #define C0_SHIFT (BITS_IN_JSAMPLE - HIST_C0_BITS) 132 #define C1_SHIFT (BITS_IN_JSAMPLE - HIST_C1_BITS) 133 #define C2_SHIFT (BITS_IN_JSAMPLE - HIST_C2_BITS) 167 #if BITS_IN_JSAMPLE == 8 226 ptr = input_buf[
row];
227 for (col =
width; col > 0; col--)
234 if (++(*histp) <= 0) (*histp)--;
271 for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++)
292 for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++)
312 int c0min, c0max, c1min, c1max, c2min, c2max;
313 INT32 dist0, dist1, dist2;
324 for (c0 = c0min; c0 <= c0max; c0++)
325 for (c1 = c1min; c1 <= c1max; c1++)
328 for (c2 = c2min; c2 <= c2max; c2++)
331 boxp->c0min = c0min = c0;
337 for (c0 = c0max; c0 >= c0min; c0--)
338 for (c1 = c1min; c1 <= c1max; c1++)
341 for (c2 = c2min; c2 <= c2max; c2++)
344 boxp->c0max = c0max = c0;
350 for (c1 = c1min; c1 <= c1max; c1++)
351 for (c0 = c0min; c0 <= c0max; c0++)
354 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++)
367 for (c2 = c2min; c2 <= c2max; c2++)
370 boxp->c1max = c1max = c1;
376 for (c2 = c2min; c2 <= c2max; c2++)
377 for (c0 = c0min; c0 <= c0max; c0++)
383 boxp->c2min = c2min = c2;
389 for (c2 = c2max; c2 >= c2min; c2--)
390 for (c0 = c0min; c0 <= c0max; c0++)
396 boxp->c2max = c2max = c2;
413 boxp->volume = dist0 * dist0 + dist1 * dist1 + dist2 * dist2;
417 for (c0 = c0min; c0 <= c0max; c0++)
418 for (c1 = c1min; c1 <= c1max; c1++)
421 for (c2 = c2min; c2 <= c2max; c2++, histp++)
427 boxp->colorcount = ccount;
436 int c0, c1, c2, cmax;
439 while (numboxes < desired_colors)
444 if (numboxes * 2 <= desired_colors)
454 b2 = &boxlist[numboxes];
456 b2->c0max =
b1->c0max;
457 b2->c1max =
b1->c1max;
458 b2->c2max =
b1->c2max;
459 b2->c0min =
b1->c0min;
460 b2->c1min =
b1->c1min;
461 b2->c2min =
b1->c2min;
506 lb = (
b1->c0max +
b1->c0min) / 2;
511 lb = (
b1->c1max +
b1->c1min) / 2;
516 lb = (
b1->c2max +
b1->c2min) / 2;
539 int c0min, c0max, c1min, c1max, c2min, c2max;
553 for (c0 = c0min; c0 <= c0max; c0++)
554 for (c1 = c1min; c1 <= c1max; c1++)
557 for (c2 = c2min; c2 <= c2max; c2++)
559 if ((
count = *histp++) != 0)
563 ((c0 << C0_SHIFT) + ((1 << C0_SHIFT) >> 1)) *
count;
565 ((c1 << C1_SHIFT) + ((1 << C1_SHIFT) >> 1)) *
count;
567 ((c2 << C2_SHIFT) + ((1 << C2_SHIFT) >> 1)) *
count;
572 cinfo->colormap[0][icolor] = (
JSAMPLE)((c0total + (total >> 1)) / total);
573 cinfo->colormap[1][icolor] = (
JSAMPLE)((c1total + (total >> 1)) / total);
574 cinfo->colormap[2][icolor] = (
JSAMPLE)((c2total + (total >> 1)) / total);
586 boxlist = (
boxptr)(*cinfo->mem->alloc_small)(
590 boxlist[0].
c0min = 0;
592 boxlist[0].
c1min = 0;
594 boxlist[0].
c2min = 0;
599 numboxes =
median_cut(cinfo, boxlist, numboxes, desired_colors);
601 for (i = 0; i < numboxes; i++)
compute_color(cinfo, &boxlist[i], i);
602 cinfo->actual_number_of_colors = numboxes;
603 TRACEMS1(cinfo, 1, JTRC_QUANT_SELECTED, numboxes);
660 #define BOX_C0_LOG (HIST_C0_BITS - 3) 661 #define BOX_C1_LOG (HIST_C1_BITS - 3) 662 #define BOX_C2_LOG (HIST_C2_BITS - 3) 664 #define BOX_C0_ELEMS (1 << BOX_C0_LOG) 665 #define BOX_C1_ELEMS (1 << BOX_C1_LOG) 666 #define BOX_C2_ELEMS (1 << BOX_C2_LOG) 668 #define BOX_C0_SHIFT (C0_SHIFT + BOX_C0_LOG) 669 #define BOX_C1_SHIFT (C1_SHIFT + BOX_C1_LOG) 670 #define BOX_C2_SHIFT (C2_SHIFT + BOX_C2_LOG) 693 int numcolors = cinfo->actual_number_of_colors;
694 int maxc0, maxc1, maxc2;
695 int centerc0, centerc1, centerc2;
697 INT32 minmaxdist, min_dist, max_dist, tdist;
707 centerc0 = (minc0 + maxc0) >> 1;
709 centerc1 = (minc1 + maxc1) >> 1;
711 centerc2 = (minc2 + maxc2) >> 1;
721 minmaxdist = 0x7FFFFFFFL;
723 for (i = 0; i < numcolors; i++)
731 min_dist = tdist * tdist;
733 max_dist = tdist * tdist;
738 min_dist = tdist * tdist;
740 max_dist = tdist * tdist;
749 max_dist = tdist * tdist;
754 max_dist = tdist * tdist;
762 min_dist += tdist * tdist;
764 max_dist += tdist * tdist;
769 min_dist += tdist * tdist;
771 max_dist += tdist * tdist;
779 max_dist += tdist * tdist;
784 max_dist += tdist * tdist;
792 min_dist += tdist * tdist;
794 max_dist += tdist * tdist;
799 min_dist += tdist * tdist;
801 max_dist += tdist * tdist;
809 max_dist += tdist * tdist;
814 max_dist += tdist * tdist;
818 mindist[i] = min_dist;
819 if (max_dist < minmaxdist) minmaxdist = max_dist;
827 for (i = 0; i < numcolors; i++)
829 if (mindist[i] <= minmaxdist) colorlist[ncolors++] = (
JSAMPLE)i;
853 INT32 inc0, inc1, inc2;
861 *bptr++ = 0x7FFFFFFFL;
869 #define STEP_C0 ((1 << C0_SHIFT) * C0_SCALE) 870 #define STEP_C1 ((1 << C1_SHIFT) * C1_SCALE) 871 #define STEP_C2 ((1 << C2_SHIFT) * C2_SCALE) 873 for (i = 0; i < numcolors; i++)
880 dist0 += inc1 * inc1;
882 dist0 += inc2 * inc2;
929 int minc0, minc1, minc2;
949 minc1 = (c1 << BOX_C1_SHIFT) + ((1 << C1_SHIFT) >> 1);
950 minc2 = (c2 << BOX_C2_SHIFT) + ((1 << C2_SHIFT) >> 1);
959 cinfo, minc0, minc1, minc2, numcolors, colorlist, bestcolor);
970 cachep = &
histogram[c0 + ic0][c1 + ic1][c2];
1002 for (col =
width; col > 0; col--)
1041 JSAMPROW colormap0 = cinfo->colormap[0];
1042 JSAMPROW colormap1 = cinfo->colormap[1];
1043 JSAMPROW colormap2 = cinfo->colormap[2];
1071 cur0 = cur1 = cur2 = 0;
1073 belowerr0 = belowerr1 = belowerr2 = 0;
1074 bpreverr0 = bpreverr1 = bpreverr2 = 0;
1076 for (col =
width; col > 0; col--)
1086 cur0 =
RIGHT_SHIFT(cur0 + errorptr[dir3 + 0] + 8, 4);
1087 cur1 =
RIGHT_SHIFT(cur1 + errorptr[dir3 + 1] + 8, 4);
1088 cur2 =
RIGHT_SHIFT(cur2 + errorptr[dir3 + 2] + 8, 4);
1092 cur0 = error_limit[cur0];
1093 cur1 = error_limit[cur1];
1094 cur2 = error_limit[cur2];
1116 int pixcode = *cachep - 1;
1133 errorptr[0] = (
FSERROR)(bpreverr0 + cur0);
1135 bpreverr0 = belowerr0 + cur0;
1136 belowerr0 = bnexterr;
1141 errorptr[1] = (
FSERROR)(bpreverr1 + cur1);
1143 bpreverr1 = belowerr1 + cur1;
1144 belowerr1 = bnexterr;
1149 errorptr[2] = (
FSERROR)(bpreverr2 + cur2);
1151 bpreverr2 = belowerr2 + cur2;
1152 belowerr2 = bnexterr;
1168 errorptr[0] = (
FSERROR)bpreverr0;
1169 errorptr[1] = (
FSERROR)bpreverr1;
1170 errorptr[2] = (
FSERROR)bpreverr2;
1199 table = (
int*)(*cinfo->mem->alloc_small)(
1204 #define STEPSIZE ((MAXJSAMPLE + 1) / 16) 1277 i = cinfo->actual_number_of_colors;
1278 if (i < 1)
ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 1);
1285 (size_t)((cinfo->output_width + 2) * (3 *
SIZEOF(
FSERROR)));
1287 if (cquantize->
fserrors ==
nullptr)
1342 if (cinfo->out_color_components != 3)
ERREXIT(cinfo, JERR_NOTIMPL);
1359 if (cinfo->enable_2pass_quant)
1362 int desired = cinfo->desired_number_of_colors;
1364 if (desired < 8)
ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 8);
1368 cquantize->
sv_colormap = (*cinfo->mem->alloc_sarray)(
1389 (
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)
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)
for(ctr=DCTSIZE;ctr > 0;ctr--)
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.
jpeg_component_info JCOEFPTR JSAMPARRAY output_buf
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)
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
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=nullptr)
Computes the normalized or normal histogram of a sequence of numbers given the number of bins and the...
#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)