13 #define JPEG_INTERNALS
17 #ifdef QUANT_1PASS_SUPPORTED
71 #define ODITHER_SIZE 16
73 #define ODITHER_CELLS (ODITHER_SIZE*ODITHER_SIZE)
74 #define ODITHER_MASK (ODITHER_SIZE-1)
76 typedef int ODITHER_MATRIX[ODITHER_SIZE][ODITHER_SIZE];
77 typedef int (*ODITHER_MATRIX_PTR)[ODITHER_SIZE];
79 static const UINT8 base_dither_matrix[ODITHER_SIZE][ODITHER_SIZE] = {
84 { 0,192, 48,240, 12,204, 60,252, 3,195, 51,243, 15,207, 63,255 },
85 { 128, 64,176,112,140, 76,188,124,131, 67,179,115,143, 79,191,127 },
86 { 32,224, 16,208, 44,236, 28,220, 35,227, 19,211, 47,239, 31,223 },
87 { 160, 96,144, 80,172,108,156, 92,163, 99,147, 83,175,111,159, 95 },
88 { 8,200, 56,248, 4,196, 52,244, 11,203, 59,251, 7,199, 55,247 },
89 { 136, 72,184,120,132, 68,180,116,139, 75,187,123,135, 71,183,119 },
90 { 40,232, 24,216, 36,228, 20,212, 43,235, 27,219, 39,231, 23,215 },
91 { 168,104,152, 88,164,100,148, 84,171,107,155, 91,167,103,151, 87 },
92 { 2,194, 50,242, 14,206, 62,254, 1,193, 49,241, 13,205, 61,253 },
93 { 130, 66,178,114,142, 78,190,126,129, 65,177,113,141, 77,189,125 },
94 { 34,226, 18,210, 46,238, 30,222, 33,225, 17,209, 45,237, 29,221 },
95 { 162, 98,146, 82,174,110,158, 94,161, 97,145, 81,173,109,157, 93 },
96 { 10,202, 58,250, 6,198, 54,246, 9,201, 57,249, 5,197, 53,245 },
97 { 138, 74,186,122,134, 70,182,118,137, 73,185,121,133, 69,181,117 },
98 { 42,234, 26,218, 38,230, 22,214, 41,233, 25,217, 37,229, 21,213 },
99 { 170,106,154, 90,166,102,150, 86,169,105,153, 89,165,101,149, 85 }
127 #if BITS_IN_JSAMPLE == 8
128 typedef INT16 FSERROR;
129 typedef int LOCFSERROR;
131 typedef INT32 FSERROR;
132 typedef INT32 LOCFSERROR;
135 typedef FSERROR
FAR *FSERRPTR;
140 #define MAX_Q_COMPS 4
156 int Ncolors[MAX_Q_COMPS];
160 ODITHER_MATRIX_PTR odither[MAX_Q_COMPS];
163 FSERRPTR fserrors[MAX_Q_COMPS];
167 typedef my_cquantizer * my_cquantize_ptr;
193 int total_colors, iroot,
i,
j;
196 static const int RGB_order[3] = { RGB_GREEN, RGB_RED, RGB_BLUE };
204 for (i = 1; i < nc; i++)
206 }
while (temp <= (
long) max_colors);
211 ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, (
int) temp);
215 for (i = 0; i < nc; i++) {
217 total_colors *= iroot;
227 for (i = 0; i < nc; i++) {
230 temp = total_colors / Ncolors[
j];
231 temp *= Ncolors[
j]+1;
232 if (temp > (
long) max_colors)
235 total_colors = (
int) temp;
275 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
278 int i,j,k, nci, blksize, blkdist, ptr, val;
281 total_colors = select_ncolors(cinfo, cquantize->Ncolors);
285 TRACEMS4(cinfo, 1, JTRC_QUANT_3_NCOLORS,
286 total_colors, cquantize->Ncolors[0],
287 cquantize->Ncolors[1], cquantize->Ncolors[2]);
289 TRACEMS1(cinfo, 1, JTRC_QUANT_NCOLORS, total_colors);
295 colormap = (*cinfo->mem->alloc_sarray)
301 blkdist = total_colors;
305 nci = cquantize->Ncolors[
i];
306 blksize = blkdist / nci;
307 for (j = 0; j < nci; j++) {
309 val = output_value(cinfo, i, j, nci-1);
311 for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) {
313 for (k = 0; k < blksize; k++)
314 colormap[i][ptr+k] = (
JSAMPLE) val;
323 cquantize->sv_colormap = colormap;
324 cquantize->sv_actual = total_colors;
335 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
337 int i,j,k, nci, blksize, val, pad;
346 cquantize->is_padded =
TRUE;
349 cquantize->is_padded =
FALSE;
352 cquantize->colorindex = (*cinfo->mem->alloc_sarray)
358 blksize = cquantize->sv_actual;
362 nci = cquantize->Ncolors[
i];
363 blksize = blksize / nci;
371 indexptr = cquantize->colorindex[
i];
373 k = largest_input_value(cinfo, i, 0, nci-1);
376 k = largest_input_value(cinfo, i, ++val, nci-1);
378 indexptr[
j] = (
JSAMPLE) (val * blksize);
383 indexptr[-
j] = indexptr[0];
395 LOCAL ODITHER_MATRIX_PTR
398 ODITHER_MATRIX_PTR odither;
402 odither = (ODITHER_MATRIX_PTR)
403 (*cinfo->mem->alloc_small) ((
j_common_ptr) cinfo, JPOOL_IMAGE,
410 den = 2 * ODITHER_CELLS * ((
INT32) (ncolors - 1));
411 for (j = 0; j < ODITHER_SIZE; j++) {
412 for (k = 0; k < ODITHER_SIZE; k++) {
413 num = ((
INT32) (ODITHER_CELLS-1 - 2*((
int)base_dither_matrix[
j][k])))
418 odither[
j][k] = (
int) (num<0 ? -((-num)/den) : num/den);
434 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
435 ODITHER_MATRIX_PTR odither;
439 nci = cquantize->Ncolors[
i];
441 for (j = 0; j <
i; j++) {
442 if (nci == cquantize->Ncolors[j]) {
443 odither = cquantize->odither[
j];
448 odither = make_odither_array(cinfo, nci);
449 cquantize->odither[
i] = odither;
463 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
464 JSAMPARRAY colorindex = cquantize->colorindex;
465 register int pixcode, ci;
473 ptrin = input_buf[
row];
474 ptrout = output_buf[
row];
475 for (col =
width; col > 0; col--) {
477 for (ci = 0; ci < nc; ci++) {
491 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
492 register int pixcode;
494 JSAMPROW colorindex0 = cquantize->colorindex[0];
495 JSAMPROW colorindex1 = cquantize->colorindex[1];
496 JSAMPROW colorindex2 = cquantize->colorindex[2];
502 ptrin = input_buf[
row];
503 ptrout = output_buf[
row];
504 for (col =
width; col > 0; col--) {
519 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
524 int row_index, col_index;
535 row_index = cquantize->row_index;
536 for (ci = 0; ci < nc; ci++) {
537 input_ptr = input_buf[
row] + ci;
538 output_ptr = output_buf[
row];
539 colorindex_ci = cquantize->colorindex[ci];
540 dither = cquantize->odither[ci][row_index];
543 for (col =
width; col > 0; col--) {
551 *output_ptr += colorindex_ci[
GETJSAMPLE(*input_ptr)+dither[col_index]];
554 col_index = (col_index + 1) & ODITHER_MASK;
558 row_index = (row_index + 1) & ODITHER_MASK;
559 cquantize->row_index = row_index;
569 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
570 register int pixcode;
573 JSAMPROW colorindex0 = cquantize->colorindex[0];
574 JSAMPROW colorindex1 = cquantize->colorindex[1];
575 JSAMPROW colorindex2 = cquantize->colorindex[2];
579 int row_index, col_index;
584 for (row = 0; row < num_rows; row++) {
585 row_index = cquantize->row_index;
586 input_ptr = input_buf[
row];
587 output_ptr = output_buf[
row];
588 dither0 = cquantize->odither[0][row_index];
589 dither1 = cquantize->odither[1][row_index];
590 dither2 = cquantize->odither[2][row_index];
593 for (col =
width; col > 0; col--) {
595 dither0[col_index]]);
597 dither1[col_index]]);
599 dither2[col_index]]);
600 *output_ptr++ = (
JSAMPLE) pixcode;
601 col_index = (col_index + 1) & ODITHER_MASK;
603 row_index = (row_index + 1) & ODITHER_MASK;
604 cquantize->row_index = row_index;
614 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
615 register LOCFSERROR cur;
620 register FSERRPTR errorptr;
636 for (row = 0; row < num_rows; row++) {
640 for (ci = 0; ci < nc; ci++) {
641 input_ptr = input_buf[
row] + ci;
642 output_ptr = output_buf[
row];
643 if (cquantize->on_odd_row) {
645 input_ptr += (
width-1) * nc;
646 output_ptr +=
width-1;
649 errorptr = cquantize->fserrors[ci] + (
width+1);
654 errorptr = cquantize->fserrors[ci];
656 colorindex_ci = cquantize->colorindex[ci];
657 colormap_ci = cquantize->sv_colormap[ci];
661 belowerr = bpreverr = 0;
663 for (col =
width; col > 0; col--) {
681 *output_ptr += (
JSAMPLE) pixcode;
693 errorptr[0] = (FSERROR) (bpreverr + cur);
695 bpreverr = belowerr + cur;
710 errorptr[0] = (FSERROR) bpreverr;
712 cquantize->on_odd_row = (cquantize->on_odd_row ?
FALSE :
TRUE);
724 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
730 cquantize->fserrors[
i] = (FSERRPTR)
731 (*cinfo->mem->alloc_large)((
j_common_ptr) cinfo, JPOOL_IMAGE, arraysize);
743 my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->
cquantize;
748 cinfo->
colormap = cquantize->sv_colormap;
755 cquantize->pub.color_quantize = color_quantize3;
757 cquantize->pub.color_quantize = color_quantize;
761 cquantize->pub.color_quantize = quantize3_ord_dither;
763 cquantize->pub.color_quantize = quantize_ord_dither;
764 cquantize->row_index = 0;
769 if (! cquantize->is_padded)
770 create_colorindex(cinfo);
772 if (cquantize->odither[0] ==
NULL)
773 create_odither_tables(cinfo);
776 cquantize->pub.color_quantize = quantize_fs_dither;
777 cquantize->on_odd_row =
FALSE;
779 if (cquantize->fserrors[0] ==
NULL)
780 alloc_fs_workspace(cinfo);
787 ERREXIT(cinfo, JERR_NOT_COMPILED);
812 ERREXIT(cinfo, JERR_MODE_CHANGE);
823 my_cquantize_ptr cquantize;
825 cquantize = (my_cquantize_ptr)
826 (*cinfo->mem->alloc_small) ((
j_common_ptr) cinfo, JPOOL_IMAGE,
829 cquantize->pub.start_pass = start_pass_1_quant;
830 cquantize->pub.finish_pass = finish_pass_1_quant;
831 cquantize->pub.new_color_map = new_color_map_1_quant;
832 cquantize->fserrors[0] =
NULL;
833 cquantize->odither[0] =
NULL;
837 ERREXIT1(cinfo, JERR_QUANT_COMPONENTS, MAX_Q_COMPS);
843 create_colormap(cinfo);
844 create_colorindex(cinfo);
853 alloc_fs_workspace(cinfo);
struct jpeg_common_struct * j_common_ptr
#define GETJSAMPLE(value)
#define ERREXIT(cinfo, code)
int desired_number_of_colors
J_COLOR_SPACE out_color_space
#define TRACEMS1(cinfo, lvl, code, p1)
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)
#define TRACEMS4(cinfo, lvl, code, p1, p2, p3, p4)
if(!ValidDisplayID(prefInfo.prefDisplayID)) prefInfo.prefDisplayID
int actual_number_of_colors