gif_dither.c
来自「ucOS 模拟环境」· C语言 代码 · 共 1,012 行 · 第 1/3 页
C
1,012 行
//----------------------------------------------------------------------
// File Name : gif_dither.c
// Project :
// Module :
// Memo :
// Author Date Description
// -----------------------------------------------
// dts 2002.4.19
//
//----------------------------------------------------------------------
/* file name gif_dither.c */
/* created by dengtieshan on 3/4/2004 */
/* in this file we private the basal function to */
/* avoid the dither in gray transfer */
#include "gif_read.h"
#include "gif_dither.h"
#include "gif_error.h"
#include "gif_gray.h"
#ifdef PASS_ONE_SURPORT
static const UINT8 base_dither_matrix[ODITHER_SIZE][ODITHER_SIZE] = {
/* Bayer's order-4 dither array. Generated by the code given in
* Stephen Hawley's article "Ordered Dithering" in Graphics Gems I.
* The values in this array must range from 0 to ODITHER_CELLS-1.
*/
{ 0,192, 48,240, 12,204, 60,252, 3,195, 51,243, 15,207, 63,255 },
{ 128, 64,176,112,140, 76,188,124,131, 67,179,115,143, 79,191,127 },
{ 32,224, 16,208, 44,236, 28,220, 35,227, 19,211, 47,239, 31,223 },
{ 160, 96,144, 80,172,108,156, 92,163, 99,147, 83,175,111,159, 95 },
{ 8,200, 56,248, 4,196, 52,244, 11,203, 59,251, 7,199, 55,247 },
{ 136, 72,184,120,132, 68,180,116,139, 75,187,123,135, 71,183,119 },
{ 40,232, 24,216, 36,228, 20,212, 43,235, 27,219, 39,231, 23,215 },
{ 168,104,152, 88,164,100,148, 84,171,107,155, 91,167,103,151, 87 },
{ 2,194, 50,242, 14,206, 62,254, 1,193, 49,241, 13,205, 61,253 },
{ 130, 66,178,114,142, 78,190,126,129, 65,177,113,141, 77,189,125 },
{ 34,226, 18,210, 46,238, 30,222, 33,225, 17,209, 45,237, 29,221 },
{ 162, 98,146, 82,174,110,158, 94,161, 97,145, 81,173,109,157, 93 },
{ 10,202, 58,250, 6,198, 54,246, 9,201, 57,249, 5,197, 53,245 },
{ 138, 74,186,122,134, 70,182,118,137, 73,185,121,133, 69,181,117 },
{ 42,234, 26,218, 38,230, 22,214, 41,233, 25,217, 37,229, 21,213 },
{ 170,106,154, 90,166,102,150, 86,169,105,153, 89,165,101,149, 85 }
};
/* 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 is indexed [component#][position].
* We provide (#columns + 2) entries per component; the extra entry at each
* end saves us from special-casing the first and last pixels.
*
* Note: on a wide image, we might not have enough room in a PC's near data
* segment to hold the error array; so it is allocated with alloc_large.
*/
#ifdef PCVER
extern FILE *FileError;
#endif
extern decompress_info_ptr cinfo;
static Bool gif_create_colormap (decompress_info_ptr cinfo);
static int gif_select_ncolors (decompress_info_ptr cinfo, int Ncolors[]);
static Bool gif_create_colorindex (decompress_info_ptr cinfo);
static void gif_zero_far (void FAR * target, size_t bytestozero);
//======================================================================
// Function Name: void gif_prepare_range_limit_table (decompress_info_ptr cinfo)
// Purpose :
// Parameter :
// Return : none
// Remarks : Allocate and fill in the sample_range_limit table
// Change Log :
// Author Date Description
// -----------------------------------------------
//======================================================================
Bool gif_prepare_range_limit_table (decompress_info_ptr cinfo)
{
JSAMPLE * table;
int i;
table = (JSAMPLE *)gif_alloc_buf((5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
if(table == NULL)
{
return FALSE;
}
table += (MAXJSAMPLE+1);
/* allow negative subscripts of simple table */
cinfo->sample_range_limit = table;
/* First segment of "simple" table: limit[x] = 0 for x < 0 */
MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
/* Main part of "simple" table: limit[x] = x */
for (i = 0; i <= MAXJSAMPLE; i++)
{
table[i] = (JSAMPLE) i;
}
table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
/* End of simple table, rest of first half of post-IDCT table */
for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
{
table[i] = MAXJSAMPLE;
}
/* Second half of post-IDCT table */
MEMZERO(table + (2 * (MAXJSAMPLE+1)),
(2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
return TRUE;
}
//======================================================================
// Function Name: Bool Gif_init_1pass_quantizer (decompress_info_ptr cinfo)
// Purpose :
// Parameter :
// Return : none
// Remarks :
// Change Log :
// Author Date Description
// -----------------------------------------------
//======================================================================
Bool Gif_init_1pass_quantizer (decompress_info_ptr cinfo)
{
my_cquantize_ptr cquantize;
size_t arraysize;
int i;
cquantize = (my_cquantize_ptr)gif_alloc_buf(SIZEOF(my_cquantizer));
if(cquantize == NULL)
{
return FALSE;
}
cinfo->cquantize = (struct jpeg_color_quantizer *) cquantize;
cquantize->fserrors[0] = NULL; /* Flag FS workspace not allocated */
cquantize->odither[0] = NULL; /* Also flag odither arrays not allocated */
if(cinfo->dither_mode == GIF_DITHER_FS)
{
if(!gif_prepare_range_limit_table(cinfo))
{
InsertError(ERROR_DITHER_FS);
return FALSE;
}
}
/* Make sure my internal arrays won't overflow */
if (cinfo->out_color_components > MAX_Q_COMPS)
{
#ifdef PCVER
WriteToErrorFile("jinit_1pass_quantizer",ERROR_COLOR_COMPONENT_ERROR, FileError);
#endif
InsertError(ERROR_COLOR_COMPONENT_ERROR);
return FALSE;
}
/* Make sure colormap indexes can be represented by JSAMPLEs */
if (cinfo->desired_number_of_colors > (MAXJSAMPLE+1))
{
#ifdef PCVER
WriteToErrorFile("jinit_1pass_quantizer",ERROR_QUANT_MANY_COLORS, FileError);
#endif
InsertError(ERROR_QUANT_MANY_COLORS);
return FALSE;
}
/* Create the colormap and color index table. */
if(!gif_create_colormap(cinfo))
{
return FALSE;
}
if(!gif_create_colorindex(cinfo))
{
return FALSE;
}
/* Allocate Floyd-Steinberg workspace now if requested.
* We do this now since it is FAR storage and may affect the memory
* manager's space calculations. If the user changes to FS dither
* mode in a later pass, we will allocate the space then, and will
* possibly overrun the max_memory_to_use setting.
*/
if (cinfo->dither_mode == GIF_DITHER_FS)
{
if(!gif_alloc_fs_workspace(cinfo))
{
InsertError(ERROR_DITHER_FS);
return FALSE;
}
arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR));
for (i = 0; i < cinfo->out_color_components; i++)
{
gif_zero_far((void FAR *) cquantize->fserrors[i], arraysize);
}
cquantize->on_odd_row = FALSE; /* initialize state for F-S dither */
}
cquantize->row_index = 0;
return TRUE;
}
//====================================================================================
// Function Name: static int gif_select_ncolors (decompress_info_ptr cinfo, int Ncolors[])
// Purpose :
// Parameter :
// Return : none
// Remarks : 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).
// Change Log :
// Author Date Description
// -----------------------------------------------
//=====================================================================================
static int gif_select_ncolors (decompress_info_ptr cinfo, int Ncolors[])
{
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;
static const int RGB_order[3] = { RGB_GREEN, RGB_RED, RGB_BLUE };
/* 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)
{
#ifdef PCVER
WriteToErrorFile("select_ncolors",ERROR_OUT_LIMIT,FileError);
#endif
InsertError(ERROR_OUT_LIMIT);
}
/* 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
* exceeding max_colors, though we know not all can be incremented.
* Sometimes, the first component can be incremented more than once!
* (Example: for 16 colors, we start at 2*2*2, go to 3*2*2, then 4*2*2.)
* In RGB colorspace, try to increment G first, then R, then B.
*/
do
{
changed = FALSE;
for (i = 0; i < nc; i++)
{
j = (cinfo->out_color_space == GIF_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;
}
//====================================================================================
// Function Name: static int output_value (decompress_info_ptr cinfo, int ci, int j, int maxj)
// Purpose :
// Parameter :
// Return : none
// Remarks : Return j'th output value, where j will range from 0 to maxj The output
// values must fall in 0..MAXJSAMPLE in increasing order
// Change Log :
// Author Date Description
// -----------------------------------------------
//=====================================================================================
static int output_value (decompress_info_ptr cinfo, int ci, int j, int maxj)
{
/* We always provide values 0 and MAXJSAMPLE for each component;
* any additional values are equally spaced between these limits.
* (Forcing the upper and lower values to the limits ensures that
* dithering can't produce a color outside the selected gamut.)
*/
return (int) (((INT32) j * MAXJSAMPLE + maxj/2) / maxj);
}
//====================================================================================
// Function Name: void create_colormap (decompress_info_ptr cinfo)
// Purpose : create color map for suitable color limit
// Parameter :
// Return : none
// Remarks :
// Change Log :
// Author Date Description
// -----------------------------------------------
//=====================================================================================
static Bool gif_create_colormap (decompress_info_ptr cinfo)
{
my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
uchar** 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 = gif_select_ncolors(cinfo, cquantize->Ncolors);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?