#define MAXNUMCOLORS (_MAXJSAMPLE + 1) /* maximum size of colormap */
/* These will do the right thing for either R,G,B or B,G,R color order, *butyoumaynotliketheresultsforothercolororders.
*/ #define HIST_C0_BITS 5/* bits of precision in R/B histogram */ #define HIST_C1_BITS 6/* bits of precision in G histogram */ #define HIST_C2_BITS 5/* bits of precision in B/R histogram */
/* Number of elements along histogram axes. */ #define HIST_C0_ELEMS (1 << HIST_C0_BITS) #define HIST_C1_ELEMS (1 << HIST_C1_BITS) #define HIST_C2_ELEMS (1 << HIST_C2_BITS)
/* These are the amounts to shift an input value to get a histogram index. */ #define C0_SHIFT (BITS_IN_JSAMPLE - HIST_C0_BITS) #define C1_SHIFT (BITS_IN_JSAMPLE - HIST_C1_BITS) #define C2_SHIFT (BITS_IN_JSAMPLE - HIST_C2_BITS)
typedef UINT16 histcell; /* histogram cell; prefer an unsigned type */
typedef histcell *histptr; /* for pointers to histogram cells */
typedef histcell hist1d[HIST_C2_ELEMS]; /* typedefs for the array */ typedef hist1d *hist2d; /* type for the 2nd-level pointers */ typedef hist2d *hist3d; /* type for top-level pointer */
#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 */
typedefstruct { struct jpeg_color_quantizer pub; /* public fields */
/* Space for the eventually created colormap is stashed here */
_JSAMPARRAY sv_colormap; /* colormap allocated at init time */ int desired; /* desired # of colors = size of colormap */
/* Variables for accumulating image statistics */
hist3d histogram; /* pointer to the histogram */
boolean needs_zeroed; /* TRUE if next pass must zero histogram */
/* Variables for Floyd-Steinberg dithering */
FSERRPTR fserrors; /* accumulated errors */
boolean on_odd_row; /* flag to remember which row we are on */ int *error_limiter; /* table for clamping the applied error */
} my_cquantizer;
for (row = 0; row < num_rows; row++) {
ptr = input_buf[row]; for (col = width; col > 0; col--) { /* get pixel value and index into the histogram */
histp = &histogram[ptr[0] >> C0_SHIFT]
[ptr[1] >> C1_SHIFT]
[ptr[2] >> C2_SHIFT]; /* increment, check for overflow and undo increment if so. */ if (++(*histp) <= 0)
(*histp)--;
ptr += 3;
}
}
}
typedefstruct { /* The bounds of the box (inclusive); expressed as histogram indexes */ int c0min, c0max; int c1min, c1max; int c2min, c2max; /* The volume (actually 2-norm) of the box */
JLONG volume; /* The number of nonzero histogram cells within this box */ long colorcount;
} box;
typedef box *boxptr;
LOCAL(boxptr)
find_biggest_color_pop(boxptr boxlist, int numboxes) /* Find the splittable box with the largest color population */ /* Returns NULL if no splittable boxes remain */
{ register boxptr boxp; registerint i; registerlong maxc = 0;
boxptr which = NULL;
for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) { if (boxp->colorcount > maxc && boxp->volume > 0) {
which = boxp;
maxc = boxp->colorcount;
}
} return which;
}
LOCAL(boxptr)
find_biggest_volume(boxptr boxlist, int numboxes) /* Find the splittable box with the largest (scaled) volume */ /* Returns NULL if no splittable boxes remain */
{ register boxptr boxp; registerint i; register JLONG maxv = 0;
boxptr which = NULL;
for (i = 0, boxp = boxlist; i < numboxes; i++, boxp++) { if (boxp->volume > maxv) {
which = boxp;
maxv = boxp->volume;
}
} return which;
}
LOCAL(void)
update_box(j_decompress_ptr cinfo, boxptr boxp) /* Shrink the min/max bounds of a box to enclose only nonzero elements, */ /* and recompute its volume and population */
{
my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize;
hist3d histogram = cquantize->histogram;
histptr histp; int c0, c1, c2; int c0min, c0max, c1min, c1max, c2min, c2max;
JLONG dist0, dist1, dist2; long ccount;
/* Now scan remaining volume of box and compute population */
ccount = 0; for (c0 = c0min; c0 <= c0max; c0++) for (c1 = c1min; c1 <= c1max; c1++) {
histp = &histogram[c0][c1][c2min]; for (c2 = c2min; c2 <= c2max; c2++, histp++) if (*histp != 0) {
ccount++;
}
}
boxp->colorcount = ccount;
}
LOCAL(int)
median_cut(j_decompress_ptr cinfo, boxptr boxlist, int numboxes, int desired_colors) /* Repeatedly select and split the largest box until we have enough boxes */
{ int n, lb; int c0, c1, c2, cmax; register boxptr b1, b2;
while (numboxes < desired_colors) { /* Select box to split. *Currentalgorithm:bypopulationforfirsthalf,thenbyvolume.
*/ if (numboxes * 2 <= desired_colors) {
b1 = find_biggest_color_pop(boxlist, numboxes);
} else {
b1 = find_biggest_volume(boxlist, numboxes);
} if (b1 == NULL) /* no splittable boxes left! */ break;
b2 = &boxlist[numboxes]; /* where new box will go */ /* Copy the color bounds to the new box. */
b2->c0max = b1->c0max; b2->c1max = b1->c1max; b2->c2max = b1->c2max;
b2->c0min = b1->c0min; b2->c1min = b1->c1min; b2->c2min = b1->c2min; /* Choose which axis to split the box on. *Currentalgorithm:longestscaledaxis. *Seenotesinupdate_boxaboutscalingdistances.
*/
c0 = ((b1->c0max - b1->c0min) << C0_SHIFT) * C0_SCALE;
c1 = ((b1->c1max - b1->c1min) << C1_SHIFT) * C1_SCALE;
c2 = ((b1->c2max - b1->c2min) << C2_SHIFT) * C2_SCALE; /* We want to break any ties in favor of green, then red, blue last. *ThiscodedoestherightthingforR,G,BorB,G,Rcolorordersonly.
*/ if (rgb_red[cinfo->out_color_space] == 0) {
cmax = c1; n = 1; if (c0 > cmax) { cmax = c0; n = 0; } if (c2 > cmax) { n = 2; }
} else {
cmax = c1; n = 1; if (c2 > cmax) { cmax = c2; n = 2; } if (c0 > cmax) { n = 0; }
} /* Choose split point along selected axis, and update box bounds. *Currentalgorithm:splitathalfwaypoint. *(Sincetheboxhasbeenshrunktominimumvolume, *anysplitwillproducetwononemptysubboxes.) *Notethatlbvalueismaxforlowerbox,somustbe<oldmax.
*/ switch (n) { case0:
lb = (b1->c0max + b1->c0min) / 2;
b1->c0max = lb;
b2->c0min = lb + 1; break; case1:
lb = (b1->c1max + b1->c1min) / 2;
b1->c1max = lb;
b2->c1min = lb + 1; break; case2:
lb = (b1->c2max + b1->c2min) / 2;
b1->c2max = lb;
b2->c2min = lb + 1; break;
} /* Update stats for boxes */
update_box(cinfo, b1);
update_box(cinfo, b2);
numboxes++;
} return numboxes;
}
LOCAL(void)
compute_color(j_decompress_ptr cinfo, boxptr boxp, int icolor) /* Compute representative color for a box, put it in colormap[icolor] */
{ /* Current algorithm: mean weighted by pixels (not colors) */ /* Note it is important to get the rounding correct! */
my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize;
hist3d histogram = cquantize->histogram;
histptr histp; int c0, c1, c2; int c0min, c0max, c1min, c1max, c2min, c2max; long count; long total = 0; long c0total = 0; long c1total = 0; long c2total = 0;
/* log2(histogram cells in update box) for each axis; this can be adjusted */ #define BOX_C0_LOG (HIST_C0_BITS - 3) #define BOX_C1_LOG (HIST_C1_BITS - 3) #define BOX_C2_LOG (HIST_C2_BITS - 3)
LOCAL(int)
find_nearby_colors(j_decompress_ptr cinfo, int minc0, int minc1, int minc2,
_JSAMPLE colorlist[]) /* Locate the colormap entries close enough to an update box to be candidates *forthenearestentrytosomecell(s)intheupdatebox.Theupdatebox *isspecifiedbythecentercoordinatesofitsfirstcell.Thenumberof *candidatecolormapentriesisreturned,andtheircolormapindexesare *placedincolorlist[]. *ThisroutineusesHeckbert's"locallysortedsearch"criteriontoselect *thecolorsthatneedfurtherconsideration.
*/
{ int numcolors = cinfo->actual_number_of_colors; int maxc0, maxc1, maxc2; int centerc0, centerc1, centerc2; int i, x, ncolors;
JLONG minmaxdist, min_dist, max_dist, tdist;
JLONG mindist[MAXNUMCOLORS]; /* min distance to colormap entry i */
/* For each color in colormap, find: *1.itsminimumsquared-distancetoanypointintheupdatebox *(zeroifcoloriswithinupdatebox); *2.itsmaximumsquared-distancetoanypointintheupdatebox. *Bothofthesecanbefoundbyconsideringonlythecornersofthebox. *Wesavetheminimumdistanceforeachcolorinmindist[]; *onlythesmallestmaximumdistanceisofinterest.
*/
minmaxdist = 0x7FFFFFFFL;
for (i = 0; i < numcolors; i++) { /* We compute the squared-c0-distance term, then add in the other two. */
x = ((_JSAMPARRAY)cinfo->colormap)[0][i]; if (x < minc0) {
tdist = (x - minc0) * C0_SCALE;
min_dist = tdist * tdist;
tdist = (x - maxc0) * C0_SCALE;
max_dist = tdist * tdist;
} elseif (x > maxc0) {
tdist = (x - maxc0) * C0_SCALE;
min_dist = tdist * tdist;
tdist = (x - minc0) * C0_SCALE;
max_dist = tdist * tdist;
} else { /* within cell range so no contribution to min_dist */
min_dist = 0; if (x <= centerc0) {
tdist = (x - maxc0) * C0_SCALE;
max_dist = tdist * tdist;
} else {
tdist = (x - minc0) * C0_SCALE;
max_dist = tdist * tdist;
}
}
mindist[i] = min_dist; /* save away the results */ if (max_dist < minmaxdist)
minmaxdist = max_dist;
}
/* Now we know that no cell in the update box is more than minmaxdist *awayfromsomecolormapentry.Therefore,onlycolorsthatare *withinminmaxdistofsomepartoftheboxneedbeconsidered.
*/
ncolors = 0; for (i = 0; i < numcolors; i++) { if (mindist[i] <= minmaxdist)
colorlist[ncolors++] = (_JSAMPLE)i;
} return ncolors;
}
LOCAL(void)
find_best_colors(j_decompress_ptr cinfo, int minc0, int minc1, int minc2, int numcolors, _JSAMPLE colorlist[], _JSAMPLE bestcolor[]) /* Find the closest colormap entry for each cell in the update box, *giventhelistofcandidatecolorspreparedbyfind_nearby_colors. *Returntheindexesoftheclosestentriesinthebestcolor[]array. *ThisroutineusesThomas'incrementaldistancecalculationmethodto *findthedistancefromacolormapentrytosuccessivecellsinthebox.
*/
{ int ic0, ic1, ic2; int i, icolor; register JLONG *bptr; /* pointer into bestdist[] array */
_JSAMPLE *cptr; /* pointer into bestcolor[] array */
JLONG dist0, dist1; /* initial distance values */ register JLONG dist2; /* current distance in inner loop */
JLONG xx0, xx1; /* distance increments */ register JLONG xx2;
JLONG inc0, inc1, inc2; /* initial values for increments */ /* This array holds the distance to the nearest-so-far color for each cell */
JLONG bestdist[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
/* Initialize best-distance for each cell of the update box */
bptr = bestdist; for (i = BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS - 1; i >= 0; i--)
*bptr++ = 0x7FFFFFFFL;
/* For each color selected by find_nearby_colors, *computeitsdistancetothecenterofeachcellinthebox. *Ifthat'slessthanbest-so-far,updatebestdistanceandcolornumber.
*/
LOCAL(void)
fill_inverse_cmap(j_decompress_ptr cinfo, int c0, int c1, int c2) /* Fill the inverse-colormap entries in the update box that contains */ /* histogram cell c0/c1/c2. (Only that one cell MUST be filled, but */ /* we can fill as many others as we wish.) */
{
my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize;
hist3d histogram = cquantize->histogram; int minc0, minc1, minc2; /* lower left corner of update box */ int ic0, ic1, ic2; register _JSAMPLE *cptr; /* pointer into bestcolor[] array */ register histptr cachep; /* pointer into main cache array */ /* This array lists the candidate colormap indexes. */
_JSAMPLE colorlist[MAXNUMCOLORS]; int numcolors; /* number of candidate colors */ /* This array holds the actually closest colormap index for each cell. */
_JSAMPLE bestcolor[BOX_C0_ELEMS * BOX_C1_ELEMS * BOX_C2_ELEMS];
/* Convert cell coordinates to update box ID */
c0 >>= BOX_C0_LOG;
c1 >>= BOX_C1_LOG;
c2 >>= BOX_C2_LOG;
/* Determine which colormap entries are close enough to be candidates *forthenearestentrytosomecellintheupdatebox.
*/
numcolors = find_nearby_colors(cinfo, minc0, minc1, minc2, colorlist);
METHODDEF(void)
pass2_no_dither(j_decompress_ptr cinfo, _JSAMPARRAY input_buf,
_JSAMPARRAY output_buf, int num_rows) /* This version performs no dithering */
{
my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize;
hist3d histogram = cquantize->histogram; register _JSAMPROW inptr, outptr; register histptr cachep; registerint c0, c1, c2; int row;
JDIMENSION col;
JDIMENSION width = cinfo->output_width;
for (row = 0; row < num_rows; row++) {
inptr = input_buf[row];
outptr = output_buf[row]; for (col = width; col > 0; col--) { /* get pixel value and index into the cache */
c0 = (*inptr++) >> C0_SHIFT;
c1 = (*inptr++) >> C1_SHIFT;
c2 = (*inptr++) >> C2_SHIFT;
cachep = &histogram[c0][c1][c2]; /* If we have not seen this color before, find nearest colormap entry */ /* and update the cache */ if (*cachep == 0)
fill_inverse_cmap(cinfo, c0, c1, c2); /* Now emit the colormap index for this cell */
*outptr++ = (_JSAMPLE)(*cachep - 1);
}
}
}
METHODDEF(void)
pass2_fs_dither(j_decompress_ptr cinfo, _JSAMPARRAY input_buf,
_JSAMPARRAY output_buf, int num_rows) /* This version performs Floyd-Steinberg dithering */
{
my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize;
hist3d histogram = cquantize->histogram; register LOCFSERROR cur0, cur1, cur2; /* current error or pixel value */
LOCFSERROR belowerr0, belowerr1, belowerr2; /* error for pixel below cur */
LOCFSERROR bpreverr0, bpreverr1, bpreverr2; /* error for below/prev col */ register FSERRPTR errorptr; /* => fserrors[] at column before current */
_JSAMPROW inptr; /* => current input pixel */
_JSAMPROW outptr; /* => current output pixel */
histptr cachep; int dir; /* +1 or -1 depending on direction */ int dir3; /* 3*dir, for advancing inptr & errorptr */ int row;
JDIMENSION col;
JDIMENSION width = cinfo->output_width;
_JSAMPLE *range_limit = (_JSAMPLE *)cinfo->sample_range_limit; int *error_limit = cquantize->error_limiter;
_JSAMPROW colormap0 = ((_JSAMPARRAY)cinfo->colormap)[0];
_JSAMPROW colormap1 = ((_JSAMPARRAY)cinfo->colormap)[1];
_JSAMPROW colormap2 = ((_JSAMPARRAY)cinfo->colormap)[2];
SHIFT_TEMPS
for (row = 0; row < num_rows; row++) {
inptr = input_buf[row];
outptr = output_buf[row]; if (cquantize->on_odd_row) { /* work right to left in this row */
inptr += (width - 1) * 3; /* so point to rightmost pixel */
outptr += width - 1;
dir = -1;
dir3 = -3;
errorptr = cquantize->fserrors + (width + 1) * 3; /* => entry after last column */
cquantize->on_odd_row = FALSE; /* flip for next time */
} else { /* work left to right in this row */
dir = 1;
dir3 = 3;
errorptr = cquantize->fserrors; /* => entry before first real column */
cquantize->on_odd_row = TRUE; /* flip for next time */
} /* Preset error values: no error propagated to first pixel from left */
cur0 = cur1 = cur2 = 0; /* and no error propagated to row below yet */
belowerr0 = belowerr1 = belowerr2 = 0;
bpreverr0 = bpreverr1 = bpreverr2 = 0;
for (col = width; col > 0; col--) { /* curN 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.
*/
cur0 = RIGHT_SHIFT(cur0 + errorptr[dir3 + 0] + 8, 4);
cur1 = RIGHT_SHIFT(cur1 + errorptr[dir3 + 1] + 8, 4);
cur2 = RIGHT_SHIFT(cur2 + errorptr[dir3 + 2] + 8, 4); /* Limit the error using transfer function set by init_error_limit. *Seecommentswithinit_error_limitforrationale.
*/
cur0 = error_limit[cur0];
cur1 = error_limit[cur1];
cur2 = error_limit[cur2]; /* Form pixel value + error, and range-limit to 0.._MAXJSAMPLE. *Themaximumerroris+-_MAXJSAMPLE(orlesswitherrorlimiting); *thissetstherequiredsizeoftherange_limitarray.
*/
cur0 += inptr[0];
cur1 += inptr[1];
cur2 += inptr[2];
cur0 = range_limit[cur0];
cur1 = range_limit[cur1];
cur2 = range_limit[cur2]; /* Index into the cache with adjusted pixel value */
cachep =
&histogram[cur0 >> C0_SHIFT][cur1 >> C1_SHIFT][cur2 >> C2_SHIFT]; /* If we have not seen this color before, find nearest colormap */ /* entry and update the cache */ if (*cachep == 0)
fill_inverse_cmap(cinfo, cur0 >> C0_SHIFT, cur1 >> C1_SHIFT,
cur2 >> C2_SHIFT); /* Now emit the colormap index for this cell */
{ registerint pixcode = *cachep - 1;
*outptr = (_JSAMPLE)pixcode; /* Compute representation error for this pixel */
cur0 -= colormap0[pixcode];
cur1 -= colormap1[pixcode];
cur2 -= colormap2[pixcode];
} /* Compute error fractions to be propagated to adjacent pixels. *Addtheseintotherunningsums,andsimultaneouslyshiftthe *next-lineerrorsumsleftby1column.
*/
{ register LOCFSERROR bnexterr;
bnexterr = cur0; /* Process component 0 */
errorptr[0] = (FSERROR)(bpreverr0 + cur0 * 3);
bpreverr0 = belowerr0 + cur0 * 5;
belowerr0 = bnexterr;
cur0 *= 7;
bnexterr = cur1; /* Process component 1 */
errorptr[1] = (FSERROR)(bpreverr1 + cur1 * 3);
bpreverr1 = belowerr1 + cur1 * 5;
belowerr1 = bnexterr;
cur1 *= 7;
bnexterr = cur2; /* Process component 2 */
errorptr[2] = (FSERROR)(bpreverr2 + cur2 * 3);
bpreverr2 = belowerr2 + cur2 * 5;
belowerr2 = bnexterr;
cur2 *= 7;
} /* At this point curN contains the 7/16 error value to be propagated *tothenextpixelonthecurrentline,andalltheerrorsforthe *nextlinehavebeenshiftedover.Wearethereforereadytomoveon.
*/
inptr += dir3; /* Advance pixel pointers to next column */
outptr += dir;
errorptr += dir3; /* advance errorptr to current column */
} /* Post-loop cleanup: we must unload the final error values into the *finalfserrors[]entry.NoteweneednotunloadbelowerrNbecause *itisforthedummycolumnbeforeoraftertheactualarray.
*/
errorptr[0] = (FSERROR)bpreverr0; /* unload prev errs into array */
errorptr[1] = (FSERROR)bpreverr1;
errorptr[2] = (FSERROR)bpreverr2;
}
}
LOCAL(void)
init_error_limit(j_decompress_ptr cinfo) /* Allocate and fill in the error_limiter table */
{
my_cquantize_ptr cquantize = (my_cquantize_ptr)cinfo->cquantize; int *table; int in, out;
table = (int *)(*cinfo->mem->alloc_small)
((j_common_ptr)cinfo, JPOOL_IMAGE, (_MAXJSAMPLE * 2 + 1) * sizeof(int));
table += _MAXJSAMPLE; /* so can index -_MAXJSAMPLE .. +_MAXJSAMPLE */
cquantize->error_limiter = table;
#define STEPSIZE ((_MAXJSAMPLE + 1) / 16) /* Map errors 1:1 up to +- _MAXJSAMPLE/16 */
out = 0; for (in = 0; in < STEPSIZE; in++, out++) {
table[in] = out; table[-in] = -out;
} /* Map errors 1:2 up to +- 3*_MAXJSAMPLE/16 */ for (; in < STEPSIZE * 3; in++, out += (in & 1) ? 0 : 1) {
table[in] = out; table[-in] = -out;
} /* Clamp the rest to final out value (which is (_MAXJSAMPLE+1)/8) */ for (; in <= _MAXJSAMPLE; in++) {
table[in] = out; table[-in] = -out;
} #undef STEPSIZE
}
/* Select the representative colors and fill in cinfo->colormap */
cinfo->colormap = (JSAMPARRAY)cquantize->sv_colormap;
select_colors(cinfo, cquantize->desired); /* Force next pass to zero the color index table */
cquantize->needs_zeroed = TRUE;
}
METHODDEF(void)
finish_pass2(j_decompress_ptr cinfo)
{ /* no work */
}
/* Only F-S dithering or no dithering is supported. */ /* If user asks for ordered dither, give them F-S. */ if (cinfo->dither_mode != JDITHER_NONE)
cinfo->dither_mode = JDITHER_FS;
if (is_pre_scan) { /* Set up method pointers */
cquantize->pub._color_quantize = prescan_quantize;
cquantize->pub.finish_pass = finish_pass1;
cquantize->needs_zeroed = TRUE; /* Always zero histogram */
} else { /* Set up method pointers */ if (cinfo->dither_mode == JDITHER_FS)
cquantize->pub._color_quantize = pass2_fs_dither; else
cquantize->pub._color_quantize = pass2_no_dither;
cquantize->pub.finish_pass = finish_pass2;
/* Make sure color count is acceptable */
i = cinfo->actual_number_of_colors; if (i < 1)
ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 1); if (i > MAXNUMCOLORS)
ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);
if (cinfo->dither_mode == JDITHER_FS) {
size_t arraysize =
(size_t)((cinfo->output_width + 2) * (3 * sizeof(FSERROR))); /* Allocate Floyd-Steinberg workspace if we didn't already. */ if (cquantize->fserrors == NULL)
cquantize->fserrors = (FSERRPTR)(*cinfo->mem->alloc_large)
((j_common_ptr)cinfo, JPOOL_IMAGE, arraysize); /* Initialize the propagated errors to zero. */
jzero_far((void *)cquantize->fserrors, arraysize); /* Make the error-limit table if we didn't already. */ if (cquantize->error_limiter == NULL)
init_error_limit(cinfo);
cquantize->on_odd_row = FALSE;
}
} /* Zero the histogram or inverse color map, if necessary */ if (cquantize->needs_zeroed) { for (i = 0; i < HIST_C0_ELEMS; i++) {
jzero_far((void *)histogram[i],
HIST_C1_ELEMS * HIST_C2_ELEMS * sizeof(histcell));
}
cquantize->needs_zeroed = FALSE;
}
}
/* Make sure jdmaster didn't give me a case I can't handle */ if (cinfo->out_color_components != 3 ||
cinfo->out_color_space == JCS_RGB565 || cinfo->master->lossless)
ERREXIT(cinfo, JERR_NOTIMPL);
/* Allocate the histogram/inverse colormap storage */
cquantize->histogram = (hist3d)(*cinfo->mem->alloc_small)
((j_common_ptr)cinfo, JPOOL_IMAGE, HIST_C0_ELEMS * sizeof(hist2d)); for (i = 0; i < HIST_C0_ELEMS; i++) {
cquantize->histogram[i] = (hist2d)(*cinfo->mem->alloc_large)
((j_common_ptr)cinfo, JPOOL_IMAGE,
HIST_C1_ELEMS * HIST_C2_ELEMS * sizeof(histcell));
}
cquantize->needs_zeroed = TRUE; /* histogram is garbage now */
/* Allocate storage for the completed colormap, if required. *Wedothisnowsinceitmayaffectthememorymanager'sspace *calculations.
*/ if (cinfo->enable_2pass_quant) { /* Make sure color count is acceptable */ int desired = cinfo->desired_number_of_colors; /* Lower bound on # of colors ... somewhat arbitrary as long as > 0 */ if (desired < 8)
ERREXIT1(cinfo, JERR_QUANT_FEW_COLORS, 8); /* Make sure colormap indexes can be represented by _JSAMPLEs */ if (desired > MAXNUMCOLORS)
ERREXIT1(cinfo, JERR_QUANT_MANY_COLORS, MAXNUMCOLORS);
cquantize->sv_colormap = (_JSAMPARRAY)(*cinfo->mem->alloc_sarray)
((j_common_ptr)cinfo, JPOOL_IMAGE, (JDIMENSION)desired, (JDIMENSION)3);
cquantize->desired = desired;
} else
cquantize->sv_colormap = NULL;
/* Only F-S dithering or no dithering is supported. */ /* If user asks for ordered dither, give them F-S. */ if (cinfo->dither_mode != JDITHER_NONE)
cinfo->dither_mode = JDITHER_FS;
/* Allocate Floyd-Steinberg workspace if necessary. *Thisisn'treallyneededuntilpass2,butagainitmayaffectthememory *manager'sspacecalculations.Althoughwewillcopewithalaterchange *indither_mode,wedonotpromisetohonormax_memory_to_useif *dither_modechanges.
*/ if (cinfo->dither_mode == JDITHER_FS) {
cquantize->fserrors = (FSERRPTR)(*cinfo->mem->alloc_large)
((j_common_ptr)cinfo, JPOOL_IMAGE,
(size_t)((cinfo->output_width + 2) * (3 * sizeof(FSERROR)))); /* Might as well create the error-limiting table too. */
init_error_limit(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.