📄 gd_topal.c
字号:
finish_pass2 (j_decompress_ptr cinfo){ /* no work */}/* * Initialize for each processing pass. */METHODDEF (void)start_pass_2_quant (j_decompress_ptr cinfo, boolean is_pre_scan){ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; hist3d histogram = cquantize->histogram; int i; /* 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; }}/* * Switch to a new external colormap between output passes. */METHODDEF (void)new_color_map_2_quant (j_decompress_ptr cinfo){ my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize; /* Reset the inverse color map */ cquantize->needs_zeroed = TRUE;}#elsestatic voidzeroHistogram (hist3d histogram){ int i; /* Zero the histogram or inverse color map */ for (i = 0; i < HIST_C0_ELEMS; i++) { memset (histogram[i], 0, HIST_C1_ELEMS * HIST_C2_ELEMS * sizeof (histcell)); }}#endifstatic void gdImageTrueColorToPaletteBody (gdImagePtr oim, int dither, int colorsWanted, gdImagePtr *cimP);BGD_DECLARE(gdImagePtr) gdImageCreatePaletteFromTrueColor (gdImagePtr im, int dither, int colorsWanted){ gdImagePtr nim; gdImageTrueColorToPaletteBody(im, dither, colorsWanted, &nim); return nim;}BGD_DECLARE(void) gdImageTrueColorToPalette (gdImagePtr im, int dither, int colorsWanted){ gdImageTrueColorToPaletteBody(im, dither, colorsWanted, 0);}/* * Module initialization routine for 2-pass color quantization. */#ifdef ORIGINAL_LIB_JPEGGLOBAL (void)jinit_2pass_quantizer (j_decompress_ptr cinfo)#elsestatic void gdImageTrueColorToPaletteBody (gdImagePtr oim, int dither, int colorsWanted, gdImagePtr *cimP)#endif{ my_cquantize_ptr cquantize = NULL; int i;#ifndef ORIGINAL_LIB_JPEG /* Allocate the JPEG palette-storage */ size_t arraysize; int maxColors = gdMaxColors; gdImagePtr nim; if (cimP) { nim = gdImageCreate(oim->sx, oim->sy); *cimP = nim; if (!nim) { return; } } else { nim = oim; } if (!oim->trueColor) { /* (Almost) nothing to do! */ if (cimP) { gdImageCopy(nim, oim, 0, 0, 0, 0, oim->sx, oim->sy); *cimP = nim; } return; } /* If we have a transparent color (the alphaless mode of transparency), we * must reserve a palette entry for it at the end of the palette. */ if (oim->transparent >= 0) { maxColors--; } if (colorsWanted > maxColors) { colorsWanted = maxColors; } if (!cimP) { nim->pixels = gdCalloc (sizeof (unsigned char *), oim->sy); if (!nim->pixels) { /* No can do */ goto outOfMemory; } for (i = 0; (i < nim->sy); i++) { nim->pixels[i] = gdCalloc (sizeof (unsigned char *), oim->sx); if (!nim->pixels[i]) { goto outOfMemory; } } }#endif#ifdef ORIGINAL_LIB_JPEG 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_2_quant; cquantize->pub.new_color_map = new_color_map_2_quant; /* Make sure jdmaster didn't give me a case I can't handle */ if (cinfo->out_color_components != 3) ERREXIT (cinfo, JERR_NOTIMPL);#else cquantize = (my_cquantize_ptr) gdCalloc (sizeof (my_cquantizer), 1); if (!cquantize) { /* No can do */ goto outOfMemory; }#endif cquantize->fserrors = NULL; /* flag optional arrays not allocated */ cquantize->error_limiter = NULL; /* Allocate the histogram/inverse colormap storage */#ifdef ORIGINAL_LIB_JPEG 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 */#else cquantize->histogram = (hist3d) gdMalloc (HIST_C0_ELEMS * sizeof (hist2d)); for (i = 0; i < HIST_C0_ELEMS; i++) { cquantize->histogram[i] = (hist2d) gdMalloc (HIST_C1_ELEMS * HIST_C2_ELEMS * sizeof (histcell)); if (!cquantize->histogram[i]) { goto outOfMemory; } }#endif#ifdef ORIGINAL_LIB_JPEG /* Allocate storage for the completed colormap, if required. * We do this now since it is FAR storage and may affect * the memory manager's space 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 = (*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. * This isn't really needed until pass 2, but again it is FAR storage. * Although we will cope with a later change in dither_mode, * we do not promise to honor max_memory_to_use if dither_mode changes. */ 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); }#else cquantize->fserrors = (FSERRPTR) gdMalloc (3 * sizeof (FSERROR)); init_error_limit (oim, nim, cquantize); arraysize = (size_t) ((nim->sx + 2) * (3 * sizeof (FSERROR))); /* Allocate Floyd-Steinberg workspace. */ cquantize->fserrors = gdCalloc (arraysize, 1); if (!cquantize->fserrors) { goto outOfMemory; } cquantize->on_odd_row = FALSE; /* Do the work! */ zeroHistogram (cquantize->histogram); prescan_quantize (oim, nim, cquantize); /* TBB 2.0.5: pass colorsWanted, not 256! */ select_colors (oim, nim, cquantize, colorsWanted); zeroHistogram (cquantize->histogram); if (dither) { pass2_fs_dither (oim, nim, cquantize); } else { pass2_no_dither (oim, nim, cquantize); }#if 0 /* 2.0.12; we no longer attempt full alpha in palettes */ if (cquantize->transparentIsPresent) { int mt = -1; int mtIndex = -1; for (i = 0; (i < im->colorsTotal); i++) { if (im->alpha[i] > mt) { mtIndex = i; mt = im->alpha[i]; } } for (i = 0; (i < im->colorsTotal); i++) { if (im->alpha[i] == mt) { im->alpha[i] = gdAlphaTransparent; } } } if (cquantize->opaqueIsPresent) { int mo = 128; int moIndex = -1; for (i = 0; (i < im->colorsTotal); i++) { if (im->alpha[i] < mo) { moIndex = i; mo = im->alpha[i]; } } for (i = 0; (i < im->colorsTotal); i++) { if (im->alpha[i] == mo) { im->alpha[i] = gdAlphaOpaque; } } }#endif /* If we had a 'transparent' color, increment the color count so it's * officially in the palette and convert the transparent variable to point to * an index rather than a color (Its data already exists and transparent * pixels have already been mapped to it by this point, it is done late as to * avoid color matching / dithering with it). */ if (oim->transparent >= 0) { nim->transparent = nim->colorsTotal; nim->colorsTotal++; } /* Success! Get rid of the truecolor image data. */ if (!cimP) { oim->trueColor = 0; /* Junk the truecolor pixels */ for (i = 0; i < oim->sy; i++) { gdFree (oim->tpixels[i]); } gdFree (oim->tpixels); oim->tpixels = 0; } goto success; /* Tediously free stuff. */outOfMemory: if (oim->trueColor) { if (!cimP) { /* On failure only */ for (i = 0; i < nim->sy; i++) { if (nim->pixels[i]) { gdFree (nim->pixels[i]); } } if (nim->pixels) { gdFree (nim->pixels); } nim->pixels = 0; } else { gdImageDestroy(nim); *cimP = 0; } }success: for (i = 0; i < HIST_C0_ELEMS; i++) { if (cquantize->histogram[i]) { gdFree (cquantize->histogram[i]); } } if (cquantize->histogram) { gdFree (cquantize->histogram); } if (cquantize->fserrors) { gdFree (cquantize->fserrors); } if (cquantize->error_limiter_storage) { gdFree (cquantize->error_limiter_storage); } if (cquantize) { gdFree (cquantize); }#endif}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -