#define ODITHER_SIZE 16/* dimension of dither matrix */ /* NB: if ODITHER_SIZE is not a power of 2, ODITHER_MASK uses will break */ #define ODITHER_CELLS (ODITHER_SIZE * ODITHER_SIZE) /* # cells in matrix */ #define ODITHER_MASK (ODITHER_SIZE - 1) /* mask for wrapping around
counters */
#if BITS_IN_JSAMPLE == 8 typedef INT16 FSERROR; /* 16 bits should be enough */ typedefint LOCFSERROR; /* use 'int' for calculation temps */ #else typedef JLONG FSERROR; /* may need more than 16 bits */ typedef JLONG LOCFSERROR; /* be sure calculation temps are big enough */ #endif
typedef FSERROR *FSERRPTR; /* pointer to error array */
/* Private subobject */
#define MAX_Q_COMPS 4/* max components I can handle */
typedefstruct { struct jpeg_color_quantizer pub; /* public fields */
/* Initially allocated colormap is saved here */
_JSAMPARRAY sv_colormap; /* The color map as a 2-D pixel array */ int sv_actual; /* number of entries in use */
_JSAMPARRAY colorindex; /* Precomputed mapping for speed */ /* colorindex[i][j] = index of color closest to pixel value j in component i, *premultipliedasdescribedabove.Sincecolormapindexesmustfitinto *_JSAMPLEs,theentriesofthisarraywilltoo.
*/
boolean is_padded; /* is the colorindex padded for odither? */
int Ncolors[MAX_Q_COMPS]; /* # of values allocated to each component */
/* Variables for ordered dithering */ int row_index; /* cur row's vertical index in dither matrix */
ODITHER_MATRIX_PTR odither[MAX_Q_COMPS]; /* one dither array per component */
/* Variables for Floyd-Steinberg dithering */
FSERRPTR fserrors[MAX_Q_COMPS]; /* accumulated errors */
boolean on_odd_row; /* flag to remember which row we are on */
} my_cquantizer;
LOCAL(int)
select_ncolors(j_decompress_ptr cinfo, int Ncolors[]) /* Determine allocation of desired colors to components, */ /* and fill in Ncolors[] array to indicate choice. */ /* Return value is total number of colors (product of Ncolors[] values). */
{ int nc = cinfo->out_color_components; /* number of color components */ int max_colors = cinfo->desired_number_of_colors; int total_colors, iroot, i, j;
boolean changed; long temp; int RGB_order[3] = { RGB_GREEN, RGB_RED, RGB_BLUE };
RGB_order[0] = rgb_green[cinfo->out_color_space];
RGB_order[1] = rgb_red[cinfo->out_color_space];
RGB_order[2] = rgb_blue[cinfo->out_color_space];
/* We can allocate at least the nc'th root of max_colors per component. */ /* Compute floor(nc'th root of max_colors). */
iroot = 1; do {
iroot++;
temp = iroot; /* set temp = iroot ** nc */ for (i = 1; i < nc; i++)
temp *= iroot;
} while (temp <= (long)max_colors); /* repeat till iroot exceeds root */
iroot--; /* now iroot = floor(root) */
/* Must have at least 2 color values per component */ if (iroot < 2)
ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, (int)temp);
/* Initialize to iroot color values for each component */
total_colors = 1; for (i = 0; i < nc; i++) {
Ncolors[i] = iroot;
total_colors *= iroot;
} /* We may be able to increment the count for one or more components without *exceedingmax_colors,thoughweknownotallcanbeincremented. *Sometimes,thefirstcomponentcanbeincrementedmorethanonce! *(Example:for16colors,westartat2*2*2,goto3*2*2,then4*2*2.) *InRGBcolorspace,trytoincrementGfirst,thenR,thenB.
*/ do {
changed = FALSE; for (i = 0; i < nc; i++) {
j = (cinfo->out_color_space == JCS_RGB ? RGB_order[i] : i); /* calculate new total_colors if Ncolors[j] is incremented */
temp = total_colors / Ncolors[j];
temp *= Ncolors[j] + 1; /* done in long arith to avoid oflo */ if (temp > (long)max_colors) break; /* won't fit, done with this pass */
Ncolors[j]++; /* OK, apply the increment */
total_colors = (int)temp;
changed = TRUE;
}
} while (changed);
return total_colors;
}
LOCAL(int)
output_value(j_decompress_ptr cinfo, int ci, int j, int maxj) /* Return j'th output value, where j will range from 0 to maxj */ /* The output values must fall in 0.._MAXJSAMPLE in increasing order */
{ /* We always provide values 0 and _MAXJSAMPLE for each component; *anyadditionalvaluesareequallyspacedbetweentheselimits. *(Forcingtheupperandlowervaluestothelimitsensuresthat *ditheringcan'tproduceacoloroutsidetheselectedgamut.)
*/ return (int)(((JLONG)j * _MAXJSAMPLE + maxj / 2) / maxj);
}
LOCAL(int)
largest_input_value(j_decompress_ptr cinfo, int ci, int j, int maxj) /* Return largest input value that should map to j'th output value */ /* Must have largest(j=0) >= 0, and largest(j=maxj) >= _MAXJSAMPLE */
{ /* Breakpoints are halfway between values returned by output_value */ return (int)(((JLONG)(2 * j + 1) * _MAXJSAMPLE + maxj) / (2 * maxj));
}
/* *Createthecolormap.
*/
LOCAL(void)
create_colormap(j_decompress_ptr cinfo)
{
my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize;
_JSAMPARRAY colormap; /* Created colormap */ int total_colors; /* Number of distinct output colors */ int i, j, k, nci, blksize, blkdist, ptr, val;
/* Select number of colors for each component */
total_colors = select_ncolors(cinfo, cquantize->Ncolors);
/* Allocate and fill in the colormap. */ /* The colors are ordered in the map in standard row-major order, */ /* i.e. rightmost (highest-indexed) color changes most rapidly. */
/* blksize is number of adjacent repeated entries for a component */ /* blkdist is distance between groups of identical entries for a component */
blkdist = total_colors;
for (i = 0; i < cinfo->out_color_components; i++) { /* fill in colormap entries for i'th color component */
nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
blksize = blkdist / nci; for (j = 0; j < nci; j++) { /* Compute j'th output value (out of nci) for component */
val = output_value(cinfo, i, j, nci - 1); /* Fill in all colormap entries that have this value of this component */ for (ptr = j * blksize; ptr < total_colors; ptr += blkdist) { /* fill in blksize entries beginning at ptr */ for (k = 0; k < blksize; k++)
colormap[i][ptr + k] = (_JSAMPLE)val;
}
}
blkdist = blksize; /* blksize of this color is blkdist of next */
}
/* Save the colormap in private storage, *whereitwillsurvivecolorquantizationmodechanges.
*/
cquantize->sv_colormap = colormap;
cquantize->sv_actual = total_colors;
}
/* *Createthecolorindextable.
*/
LOCAL(void)
create_colorindex(j_decompress_ptr cinfo)
{
my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize;
_JSAMPROW indexptr; int i, j, k, nci, blksize, val, pad;
/* For ordered dither, we pad the color index tables by _MAXJSAMPLE in *eachdirection(inputindexvaluescanbe-_MAXJSAMPLE..2*_MAXJSAMPLE). *Thisisnotnecessaryintheotherditheringmodes.However,we *flagwhetheritwasdoneincaseuserchangesditheringmode.
*/ if (cinfo->dither_mode == JDITHER_ORDERED) {
pad = _MAXJSAMPLE * 2;
cquantize->is_padded = TRUE;
} else {
pad = 0;
cquantize->is_padded = FALSE;
}
/* blksize is number of adjacent repeated entries for a component */
blksize = cquantize->sv_actual;
for (i = 0; i < cinfo->out_color_components; i++) { /* fill in colorindex entries for i'th color component */
nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
blksize = blksize / nci;
/* adjust colorindex pointers to provide padding at negative indexes. */ if (pad)
cquantize->colorindex[i] += _MAXJSAMPLE;
/* in loop, val = index of current output value, */ /* and k = largest j that maps to current val */
indexptr = cquantize->colorindex[i];
val = 0;
k = largest_input_value(cinfo, i, 0, nci - 1); for (j = 0; j <= _MAXJSAMPLE; j++) { while (j > k) /* advance val if past boundary */
k = largest_input_value(cinfo, i, ++val, nci - 1); /* premultiply so that no multiplication needed in main processing */
indexptr[j] = (_JSAMPLE)(val * blksize);
} /* Pad at both ends if necessary */ if (pad) for (j = 1; j <= _MAXJSAMPLE; j++) {
indexptr[-j] = indexptr[0];
indexptr[_MAXJSAMPLE + j] = indexptr[_MAXJSAMPLE];
}
}
}
LOCAL(void)
create_odither_tables(j_decompress_ptr cinfo)
{
my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize;
ODITHER_MATRIX_PTR odither; int i, j, nci;
for (i = 0; i < cinfo->out_color_components; i++) {
nci = cquantize->Ncolors[i]; /* # of distinct values for this color */
odither = NULL; /* search for matching prior component */ for (j = 0; j < i; j++) { if (nci == cquantize->Ncolors[j]) {
odither = cquantize->odither[j]; break;
}
} if (odither == NULL) /* need a new table? */
odither = make_odither_array(cinfo, nci);
cquantize->odither[i] = odither;
}
}
METHODDEF(void)
quantize_ord_dither(j_decompress_ptr cinfo, _JSAMPARRAY input_buf,
_JSAMPARRAY output_buf, int num_rows) /* General case, with ordered dithering */
{
my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize; register _JSAMPROW input_ptr; register _JSAMPROW output_ptr;
_JSAMPROW colorindex_ci; int *dither; /* points to active row of dither matrix */ int row_index, col_index; /* current indexes into dither matrix */ int nc = cinfo->out_color_components; int ci; int row;
JDIMENSION col;
JDIMENSION width = cinfo->output_width;
for (row = 0; row < num_rows; row++) { /* Initialize output values to 0 so can process components separately */
jzero_far((void *)output_buf[row], (size_t)(width * sizeof(_JSAMPLE)));
row_index = cquantize->row_index; for (ci = 0; ci < nc; ci++) {
input_ptr = input_buf[row] + ci;
output_ptr = output_buf[row];
colorindex_ci = cquantize->colorindex[ci];
dither = cquantize->odither[ci][row_index];
col_index = 0;
for (col = width; col > 0; col--) { /* Form pixel value + dither, range-limit to 0.._MAXJSAMPLE, *selectoutputvalue,accumulateintooutputcodeforthispixel. *Range-limitingneednotbedoneexplicitly,aswehaveextended *thecolorindextabletoproducetherightanswersforout-of-range *inputs.Themaximumditheris+-_MAXJSAMPLE;thissetsthe *requiredamountofpadding.
*/
*output_ptr +=
colorindex_ci[*input_ptr + dither[col_index]];
input_ptr += nc;
output_ptr++;
col_index = (col_index + 1) & ODITHER_MASK;
}
} /* Advance row index for next row */
row_index = (row_index + 1) & ODITHER_MASK;
cquantize->row_index = row_index;
}
}
METHODDEF(void)
quantize3_ord_dither(j_decompress_ptr cinfo, _JSAMPARRAY input_buf,
_JSAMPARRAY output_buf, int num_rows) /* Fast path for out_color_components==3, with ordered dithering */
{
my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize; registerint pixcode; register _JSAMPROW input_ptr; register _JSAMPROW output_ptr;
_JSAMPROW colorindex0 = cquantize->colorindex[0];
_JSAMPROW colorindex1 = cquantize->colorindex[1];
_JSAMPROW colorindex2 = cquantize->colorindex[2]; int *dither0; /* points to active row of dither matrix */ int *dither1; int *dither2; int row_index, col_index; /* current indexes into dither matrix */ int row;
JDIMENSION col;
JDIMENSION width = cinfo->output_width;
METHODDEF(void)
quantize_fs_dither(j_decompress_ptr cinfo, _JSAMPARRAY input_buf,
_JSAMPARRAY output_buf, int num_rows) /* General case, with Floyd-Steinberg dithering */
{
my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize; register LOCFSERROR cur; /* current error or pixel value */
LOCFSERROR belowerr; /* error for pixel below cur */
LOCFSERROR bpreverr; /* error for below/prev col */
LOCFSERROR bnexterr; /* error for below/next col */
LOCFSERROR delta; register FSERRPTR errorptr; /* => fserrors[] at column before current */ register _JSAMPROW input_ptr; register _JSAMPROW output_ptr;
_JSAMPROW colorindex_ci;
_JSAMPROW colormap_ci; int pixcode; int nc = cinfo->out_color_components; int dir; /* 1 for left-to-right, -1 for right-to-left */ int dirnc; /* dir * nc */ int ci; int row;
JDIMENSION col;
JDIMENSION width = cinfo->output_width;
_JSAMPLE *range_limit = (_JSAMPLE *)cinfo->sample_range_limit;
SHIFT_TEMPS
for (row = 0; row < num_rows; row++) { /* Initialize output values to 0 so can process components separately */
jzero_far((void *)output_buf[row], (size_t)(width * sizeof(_JSAMPLE))); for (ci = 0; ci < nc; ci++) {
input_ptr = input_buf[row] + ci;
output_ptr = output_buf[row]; if (cquantize->on_odd_row) { /* work right to left in this row */
input_ptr += (width - 1) * nc; /* so point to rightmost pixel */
output_ptr += width - 1;
dir = -1;
dirnc = -nc;
errorptr = cquantize->fserrors[ci] + (width + 1); /* => entry after last column */
} else { /* work left to right in this row */
dir = 1;
dirnc = nc;
errorptr = cquantize->fserrors[ci]; /* => entry before first column */
}
colorindex_ci = cquantize->colorindex[ci];
colormap_ci = cquantize->sv_colormap[ci]; /* Preset error values: no error propagated to first pixel from left */
cur = 0; /* and no error propagated to row below yet */
belowerr = bpreverr = 0;
for (col = width; col > 0; col--) { /* cur holds the error propagated from the previous pixel on the *currentline.Addtheerrorpropagatedfromthepreviousline *toformthecompleteerrorcorrectiontermforthispixel,and *roundtheerrorterm(whichisexpressed*16)toaninteger. *RIGHT_SHIFTroundstowardsminusinfinity,soadding8iscorrect *foreithersignoftheerrorvalue. *Note:errorptrpointsto*previous*column'sarrayentry.
*/
cur = RIGHT_SHIFT(cur + errorptr[dir] + 8, 4); /* Form pixel value + error, and range-limit to 0.._MAXJSAMPLE. *Themaximumerroris+-_MAXJSAMPLE;thissetstherequiredsize *oftherange_limitarray.
*/
cur += *input_ptr;
cur = range_limit[cur]; /* Select output value, accumulate into output code for this pixel */
pixcode = colorindex_ci[cur];
*output_ptr += (_JSAMPLE)pixcode; /* Compute actual representation error at this pixel */ /* Note: we can do this even though we don't have the final */ /* pixel code, because the colormap is orthogonal. */
cur -= colormap_ci[pixcode]; /* Compute error fractions to be propagated to adjacent pixels. *Addtheseintotherunningsums,andsimultaneouslyshiftthe *next-lineerrorsumsleftby1column.
*/
bnexterr = cur;
delta = cur * 2;
cur += delta; /* form error * 3 */
errorptr[0] = (FSERROR)(bpreverr + cur);
cur += delta; /* form error * 5 */
bpreverr = belowerr + cur;
belowerr = bnexterr;
cur += delta; /* form error * 7 */ /* At this point cur contains the 7/16 error value to be propagated *tothenextpixelonthecurrentline,andalltheerrorsforthe *nextlinehavebeenshiftedover.Wearethereforereadytomoveon.
*/
input_ptr += dirnc; /* advance input ptr to next column */
output_ptr += dir; /* advance output ptr to next column */
errorptr += dir; /* advance errorptr to current column */
} /* Post-loop cleanup: we must unload the final error value into the *finalfserrors[]entry.Noteweneednotunloadbelowerrbecause *itisforthedummycolumnbeforeoraftertheactualarray.
*/
errorptr[0] = (FSERROR)bpreverr; /* unload prev err into array */
}
cquantize->on_odd_row = (cquantize->on_odd_row ? FALSE : TRUE);
}
}
if (cinfo->data_precision != BITS_IN_JSAMPLE)
ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
/* Color quantization is not supported with lossless JPEG images */ if (cinfo->master->lossless)
ERREXIT(cinfo, JERR_NOTIMPL);
cquantize = (my_cquantize_ptr)
(*cinfo->mem->alloc_small) ((j_common_ptr)cinfo, JPOOL_IMAGE, sizeof(my_cquantizer));
cinfo->cquantize = (struct jpeg_color_quantizer *)cquantize;
cquantize->pub.start_pass = start_pass_1_quant;
cquantize->pub.finish_pass = finish_pass_1_quant;
cquantize->pub.new_color_map = new_color_map_1_quant;
cquantize->fserrors[0] = NULL; /* Flag FS workspace not allocated */
cquantize->odither[0] = NULL; /* Also flag odither arrays not allocated */
/* Make sure my internal arrays won't overflow */ if (cinfo->out_color_components > MAX_Q_COMPS)
ERREXIT1(cinfo, JERR_QUANT_COMPONENTS, MAX_Q_COMPS); /* Make sure colormap indexes can be represented by _JSAMPLEs */ if (cinfo->desired_number_of_colors > (_MAXJSAMPLE + 1))
ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, _MAXJSAMPLE + 1);
/* Create the colormap and color index table. */
create_colormap(cinfo);
create_colorindex(cinfo);
/* Allocate Floyd-Steinberg workspace now if requested. *Wedothisnowsinceitmayaffectthememorymanager'sspace *calculations.IftheuserchangestoFSdithermodeinalaterpass,we *willallocatethespacethen,andwillpossiblyoverrunthe *max_memory_to_usesetting.
*/ if (cinfo->dither_mode == JDITHER_FS)
alloc_fs_workspace(cinfo);
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.