#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 FAR * histptr; /* for pointers to histogram cells */
typedef histcell hist1d[HIST_C2_ELEMS]; /* typedefs for the array */ typedef hist1d FAR * 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 INT32 FSERROR; /* may need more than 16 bits */ typedef INT32 LOCFSERROR; /* be sure calculation temps are big enough */ #endif
typedef FSERROR FAR *FSERRPTR; /* pointer to error array (in FAR storage!) */
/* 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[GETJSAMPLE(ptr[0]) >> C0_SHIFT]
[GETJSAMPLE(ptr[1]) >> C1_SHIFT]
[GETJSAMPLE(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 */
INT32 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 INT32 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;
INT32 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 == 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; } #endif /* 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)
#define BOX_C0_ELEMS (1<<BOX_C0_LOG) /* # of hist cells in update box */ #define BOX_C1_ELEMS (1<<BOX_C1_LOG) #define BOX_C2_ELEMS (1<<BOX_C2_LOG)
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;
INT32 minmaxdist, min_dist, max_dist, tdist;
INT32 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 = GETJSAMPLE(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 INT32 * bptr; /* pointer into bestdist[] array */
JSAMPLE * cptr; /* pointer into bestcolor[] array */
INT32 dist0, dist1; /* initial distance values */ register INT32 dist2; /* current distance in inner loop */
INT32 xx0, xx1; /* distance increments */ register INT32 xx2;
INT32 inc0, inc1, inc2; /* initial values for increments */ /* This array holds the distance to the nearest-so-far color for each cell */
INT32 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 = GETJSAMPLE(*inptr++) >> C0_SHIFT;
c1 = GETJSAMPLE(*inptr++) >> C1_SHIFT;
c2 = GETJSAMPLE(*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 = cinfo->sample_range_limit; int *error_limit = cquantize->error_limiter;
JSAMPROW colormap0 = cinfo->colormap[0];
JSAMPROW colormap1 = cinfo->colormap[1];
JSAMPROW colormap2 = 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 += GETJSAMPLE(inptr[0]);
cur1 += GETJSAMPLE(inptr[1]);
cur2 += GETJSAMPLE(inptr[2]);
cur0 = GETJSAMPLE(range_limit[cur0]);
cur1 = GETJSAMPLE(range_limit[cur1]);
cur2 = GETJSAMPLE(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 -= GETJSAMPLE(colormap0[pixcode]);
cur1 -= GETJSAMPLE(colormap1[pixcode]);
cur2 -= GETJSAMPLE(colormap2[pixcode]);
} /* Compute error fractions to be propagated to adjacent pixels. *Addtheseintotherunningsums,andsimultaneouslyshiftthe *next-lineerrorsumsleftby1column.
*/
{ register LOCFSERROR bnexterr, delta;
bnexterr = cur0; /* Process component 0 */
delta = cur0 * 2;
cur0 += delta; /* form error * 3 */
errorptr[0] = (FSERROR) (bpreverr0 + cur0);
cur0 += delta; /* form error * 5 */
bpreverr0 = belowerr0 + cur0;
belowerr0 = bnexterr;
cur0 += delta; /* form error * 7 */
bnexterr = cur1; /* Process component 1 */
delta = cur1 * 2;
cur1 += delta; /* form error * 3 */
errorptr[1] = (FSERROR) (bpreverr1 + cur1);
cur1 += delta; /* form error * 5 */
bpreverr1 = belowerr1 + cur1;
belowerr1 = bnexterr;
cur1 += delta; /* form error * 7 */
bnexterr = cur2; /* Process component 2 */
delta = cur2 * 2;
cur2 += delta; /* form error * 3 */
errorptr[2] = (FSERROR) (bpreverr2 + cur2);
cur2 += delta; /* form error * 5 */
bpreverr2 = belowerr2 + cur2;
belowerr2 = bnexterr;
cur2 += delta; /* form error * 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 = 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 him 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 FAR *) 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 FAR *) 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)
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. *WedothisnowsinceitisFARstorageandmayaffect *thememorymanager'sspacecalculations.
*/ 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 = (*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 him F-S. */ if (cinfo->dither_mode != JDITHER_NONE)
cinfo->dither_mode = JDITHER_FS;
/* Allocate Floyd-Steinberg workspace if necessary. *Thisisn'treallyneededuntilpass2,butagainitisFARstorage. *Althoughwewillcopewithalaterchangeindither_mode, *wedonotpromisetohonormax_memory_to_useifdither_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);
}
}
#endif/* QUANT_2PASS_SUPPORTED */
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.34 Sekunden
(vorverarbeitet am 2026-06-11)
¤
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.