⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gd_topal.c

📁 下载来的一个看图软件的源代码
💻 C
📖 第 1 页 / 共 4 页
字号:
/* * gd_topal.c  *  * This code is adapted pretty much entirely from jquant2.c, * Copyright (C) 1991-1996, Thomas G. Lane. That file is * part of the Independent JPEG Group's software. Conditions of * use are compatible with the gd license. See the gd license  * statement and README-JPEG.TXT for additional information. * * This file contains 2-pass color quantization (color mapping) routines. * These routines provide selection of a custom color map for an image, * followed by mapping of the image to that color map, with optional * Floyd-Steinberg dithering. * * It is also possible to use just the second pass to map to an arbitrary * externally-given color map. * * Note: ordered dithering is not supported, since there isn't any fast * way to compute intercolor distances; it's unclear that ordered dither's * fundamental assumptions even hold with an irregularly spaced color map. * * SUPPORT FOR ALPHA CHANNELS WAS HACKED IN BY THOMAS BOUTELL, who also * adapted the code to work within gd rather than within libjpeg, and * may not have done a great job of either. It's not Thomas G. Lane's fault. */#include "gd.h"#include "gdhelpers.h"/* * This module implements the well-known Heckbert paradigm for color * quantization.  Most of the ideas used here can be traced back to * Heckbert's seminal paper *   Heckbert, Paul.  "Color Image Quantization for Frame Buffer Display", *   Proc. SIGGRAPH '82, Computer Graphics v.16 #3 (July 1982), pp 297-304. * * In the first pass over the image, we accumulate a histogram showing the * usage count of each possible color.  To keep the histogram to a reasonable * size, we reduce the precision of the input; typical practice is to retain * 5 or 6 bits per color, so that 8 or 4 different input values are counted * in the same histogram cell. * * Next, the color-selection step begins with a box representing the whole * color space, and repeatedly splits the "largest" remaining box until we * have as many boxes as desired colors.  Then the mean color in each * remaining box becomes one of the possible output colors. *  * The second pass over the image maps each input pixel to the closest output * color (optionally after applying a Floyd-Steinberg dithering correction). * This mapping is logically trivial, but making it go fast enough requires * considerable care. * * Heckbert-style quantizers vary a good deal in their policies for choosing * the "largest" box and deciding where to cut it.  The particular policies * used here have proved out well in experimental comparisons, but better ones * may yet be found. * * In earlier versions of the IJG code, this module quantized in YCbCr color * space, processing the raw upsampled data without a color conversion step. * This allowed the color conversion math to be done only once per colormap * entry, not once per pixel.  However, that optimization precluded other * useful optimizations (such as merging color conversion with upsampling) * and it also interfered with desired capabilities such as quantizing to an * externally-supplied colormap.  We have therefore abandoned that approach. * The present code works in the post-conversion color space, typically RGB. * * To improve the visual quality of the results, we actually work in scaled * RGBA space, giving G distances more weight than R, and R in turn more than * B.  Alpha is weighted least. To do everything in integer math, we must  * use integer scale factors. The 2/3/1 scale factors used here correspond  * loosely to the relative weights of the colors in the NTSC grayscale  * equation.  */#ifndef TRUE#define TRUE 1#endif /* TRUE */#ifndef FALSE#define FALSE 0#endif /* FALSE */#define R_SCALE 2		/* scale R distances by this much */#define G_SCALE 3		/* scale G distances by this much */#define B_SCALE 1		/* and B by this much */#define A_SCALE 4		/* and alpha by this much. This really				   only scales by 1 because alpha				   values are 7-bit to begin with. *//* Channel ordering (fixed in gd) */#define C0_SCALE R_SCALE#define C1_SCALE G_SCALE#define C2_SCALE B_SCALE#define C3_SCALE A_SCALE/* * First we have the histogram data structure and routines for creating it. * * The number of bits of precision can be adjusted by changing these symbols. * We recommend keeping 6 bits for G and 5 each for R and B. * If you have plenty of memory and cycles, 6 bits all around gives marginally * better results; if you are short of memory, 5 bits all around will save * some space but degrade the results. * To maintain a fully accurate histogram, we'd need to allocate a "long" * (preferably unsigned long) for each cell.  In practice this is overkill; * we can get by with 16 bits per cell.  Few of the cell counts will overflow, * and clamping those that do overflow to the maximum value will give close- * enough results.  This reduces the recommended histogram size from 256Kb * to 128Kb, which is a useful savings on PC-class machines. * (In the second pass the histogram space is re-used for pixel mapping data; * in that capacity, each cell must be able to store zero to the number of * desired colors.  16 bits/cell is plenty for that too.) * Since the JPEG code is intended to run in small memory model on 80x86 * machines, we can't just allocate the histogram in one chunk.  Instead * of a true 3-D array, we use a row of pointers to 2-D arrays.  Each * pointer corresponds to a C0 value (typically 2^5 = 32 pointers) and * each 2-D array has 2^6*2^5 = 2048 or 2^6*2^6 = 4096 entries.  Note that * on 80x86 machines, the pointer row is in near memory but the actual * arrays are in far memory (same arrangement as we use for image arrays). */#define MAXNUMCOLORS  (gdMaxColors)	/* maximum size of colormap */#define HIST_C0_BITS  5		/* bits of precision in R histogram */#define HIST_C1_BITS  6		/* bits of precision in G histogram */#define HIST_C2_BITS  5		/* bits of precision in B histogram */#define HIST_C3_BITS  3		/* bits of precision in A 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)#define HIST_C3_ELEMS  (1<<HIST_C3_BITS)/* These are the amounts to shift an input value to get a histogram index. */#define C0_SHIFT  (8-HIST_C0_BITS)#define C1_SHIFT  (8-HIST_C1_BITS)#define C2_SHIFT  (8-HIST_C2_BITS)/* Beware! Alpha is 7 bit to begin with */#define C3_SHIFT  (7-HIST_C3_BITS)typedef unsigned short histcell;	/* histogram cell; prefer an unsigned type */typedef histcell *histptr;	/* for pointers to histogram cells */typedef histcell hist1d[HIST_C3_ELEMS];		/* typedefs for the array */typedef hist1d *hist2d;		/* type for the 2nd-level pointers */typedef hist2d *hist3d;		/* type for third-level pointer */typedef hist3d *hist4d;		/* type for top-level pointer *//* Declarations for Floyd-Steinberg dithering. * Errors are accumulated into the array fserrors[], at a resolution of * 1/16th of a pixel count.  The error at a given pixel is propagated * to its not-yet-processed neighbors using the standard F-S fractions, *              ...     (here)  7/16 *              3/16    5/16    1/16 * We work left-to-right on even rows, right-to-left on odd rows. * * We can get away with a single array (holding one row's worth of errors) * by using it to store the current row's errors at pixel columns not yet * processed, but the next row's errors at columns already processed.  We * need only a few extra variables to hold the errors immediately around the * current column.  (If we are lucky, those variables are in registers, but * even if not, they're probably cheaper to access than array elements are.) * * The fserrors[] array has (#columns + 2) entries; the extra entry at * each end saves us from special-casing the first and last pixels. * Each entry is three values long, one value for each color component. * */typedef signed short FSERROR;	/* 16 bits should be enough */typedef int LOCFSERROR;		/* use 'int' for calculation temps */typedef FSERROR *FSERRPTR;	/* pointer to error array *//* Private object */typedef struct  {    hist4d histogram;		/* pointer to the histogram */    int needs_zeroed;		/* TRUE if next pass must zero histogram */    /* Variables for Floyd-Steinberg dithering */    FSERRPTR fserrors;		/* accumulated errors */    int on_odd_row;		/* flag to remember which row we are on */    int *error_limiter;		/* table for clamping the applied error */    int *error_limiter_storage;	/* gdMalloc'd storage for the above */    int transparentIsPresent;	/* TBB: for rescaling to ensure that */    int opaqueIsPresent;	/* 100% opacity & transparency are preserved */  }my_cquantizer;typedef my_cquantizer *my_cquantize_ptr;/* * Prescan the pixel array.  *  * The prescan simply updates the histogram, which has been * initialized to zeroes by start_pass. * */static voidprescan_quantize (gdImagePtr im, my_cquantize_ptr cquantize){  register histptr histp;  register hist4d histogram = cquantize->histogram;  int row;  int col;  int *ptr;  int width = im->sx;  for (row = 0; row < im->sy; row++)    {      ptr = im->tpixels[row];      for (col = width; col > 0; col--)	{	  /* get pixel value and index into the histogram */	  int r, g, b, a;	  r = gdTrueColorGetRed (*ptr) >> C0_SHIFT;	  g = gdTrueColorGetGreen (*ptr) >> C1_SHIFT;	  b = gdTrueColorGetBlue (*ptr) >> C2_SHIFT;	  a = gdTrueColorGetAlpha (*ptr);	  /* We must have 100% opacity and transparency available	     in the color map to do an acceptable job with alpha 	     channel, if opacity and transparency are present in the	     original, because of the visual properties of large	     flat-color border areas (requiring 100% transparency) 	     and the behavior of poorly implemented browsers 	     (requiring 100% opacity). Test for the presence of	     these here, and rescale the most opaque and transparent	     palette entries at the end if so. This avoids the need	     to develop a fuller understanding I have not been able	     to reach so far in my study of this subject. TBB */	  if (a == gdAlphaTransparent)	    {	      cquantize->transparentIsPresent = 1;	    }	  if (a == gdAlphaOpaque)	    {	      cquantize->opaqueIsPresent = 1;	    }	  a >>= C3_SHIFT;	  histp = &histogram[r][g][b][a];	  /* increment, check for overflow and undo increment if so. */	  if (++(*histp) <= 0)	    (*histp)--;	  ptr++;	}    }}/* * Next we have the really interesting routines: selection of a colormap * given the completed histogram. * These routines work with a list of "boxes", each representing a rectangular * subset of the input color space (to histogram precision). */typedef struct{  /* The bounds of the box (inclusive); expressed as histogram indexes */  int c0min, c0max;  int c1min, c1max;  int c2min, c2max;  int c3min, c3max;  /* The volume (actually 2-norm) of the box */  int volume;  /* The number of nonzero histogram cells within this box */  long colorcount;}box;typedef box *boxptr;static boxptrfind_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;  register int i;  register long 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;}static boxptrfind_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;  register int i;  register int 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;}static voidupdate_box (gdImagePtr im, my_cquantize_ptr cquantize, boxptr boxp)/* Shrink the min/max bounds of a box to enclose only nonzero elements, *//* and recompute its volume and population */{  hist4d histogram = cquantize->histogram;  histptr histp;  int c0, c1, c2, c3;  int c0min, c0max, c1min, c1max, c2min, c2max, c3min, c3max;  int dist0, dist1, dist2, dist3;  long ccount;  c0min = boxp->c0min;  c0max = boxp->c0max;  c1min = boxp->c1min;  c1max = boxp->c1max;  c2min = boxp->c2min;  c2max = boxp->c2max;  c3min = boxp->c3min;  c3max = boxp->c3max;  if (c0max > c0min)    {      for (c0 = c0min; c0 <= c0max; c0++)	{	  for (c1 = c1min; c1 <= c1max; c1++)	    {	      for (c2 = c2min; c2 <= c2max; c2++)		{		  histp = &histogram[c0][c1][c2][c3min];		  for (c3 = c3min; c3 <= c3max; c3++)		    {		      if (*histp++ != 0)			{			  boxp->c0min = c0min = c0;			  goto have_c0min;			}		    }		}	    }	}    }have_c0min:  if (c0max > c0min)    {      for (c0 = c0max; c0 >= c0min; c0--)	{	  for (c1 = c1min; c1 <= c1max; c1++)	    {	      for (c2 = c2min; c2 <= c2max; c2++)		{		  histp = &histogram[c0][c1][c2][c3min];		  for (c3 = c3min; c3 <= c3max; c3++)		    {		      if (*histp++ != 0)			{			  boxp->c0max = c0max = c0;			  goto have_c0max;			}		    }		}	    }	}    }have_c0max:  if (c1max > c1min)    for (c1 = c1min; c1 <= c1max; c1++)      for (c0 = c0min; c0 <= c0max; c0++)	{	  for (c2 = c2min; c2 <= c2max; c2++)	    {	      histp = &histogram[c0][c1][c2][c3min];	      for (c3 = c3min; c3 <= c3max; c3++)		if (*histp++ != 0)		  {		    boxp->c1min = c1min = c1;		    goto have_c1min;		  }	    }	}have_c1min:  if (c1max > c1min)    for (c1 = c1max; c1 >= c1min; c1--)      for (c0 = c0min; c0 <= c0max; c0++)	{	  for (c2 = c2min; c2 <= c2max; c2++)	    {	      histp = &histogram[c0][c1][c2][c3min];	      for (c3 = c3min; c3 <= c3max; c3++)		if (*histp++ != 0)		  {		    boxp->c1max = c1max = c1;		    goto have_c1max;		  }	    }	}have_c1max:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -