📄 gif_gray.c
字号:
//----------------------------------------------------------------------
// File Name : gif_gray.c
// Project : gsm_gprs
// Module :
// Memo :
// Author Date Description
// -----------------------------------------------
// dts 2004.3.10
//
//----------------------------------------------------------------------
#include "gif_read.h"
#include "gif_error.h"
#include "gif_dither.h"
#include "gif_gray.h"
#include "gif_interface.h"
#ifdef PCVER
extern FILE *FileError;
#endif
extern decompress_info_ptr cinfo;
static void color_quantize3_gray (decompress_info_ptr cinfo, uchar* input_buf,
uchar* output_buf, int num_rows);
static void gif_zero_far (void FAR * target, size_t bytestozero);
static Bool gif_alloc_fs_workspace_gray (decompress_info_ptr cinfo);
static void quantize_fs_dither_gray (decompress_info_ptr cinfo, uchar* input_buf,
uchar* output_buf, int num_rows);
//====================================================================================
// Function Name: void gif_select_gray(GIFParameter* ucGifParameter)
// Purpose : Zero out a chunk of FAR memory. This might be sample-array data,
// block-array data, or alloc_large data.
// Parameter :
// Return : none
// Remarks :
// Change Log :
// Author Date Description
// -----------------------------------------------
//=====================================================================================
static void gif_zero_far (void FAR * target, size_t bytestozero)
{
register char FAR * ptr = (char FAR *) target;
register size_t count;
for (count = bytestozero; count > 0; count--)
{
*ptr++ = 0;
}
}
//====================================================================================
// Function Name: void gif_prepare_range_limit_table_gray (decompress_info_ptr cinfo)
// Purpose : Allocate and fill in the sample_range_limit table
// Parameter :
// Return : none
// Remarks :
// Change Log :
// Author Date Description
// -----------------------------------------------
//=====================================================================================
Bool gif_prepare_range_limit_table_gray (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: static void gif_alloc_fs_workspace_gray (decompress_info_ptr cinfo)
// Purpose :
// Parameter :
// Return : none
// Remarks :
// Change Log :
// Author Date Description
// -----------------------------------------------
//=====================================================================================
static Bool gif_alloc_fs_workspace_gray (decompress_info_ptr cinfo)
{
my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
size_t arraysize;
int i;
arraysize = (size_t) ((cinfo->output_width + 2) * SIZEOF(FSERROR));
for (i = 0; i < cinfo->out_color_components; i++)
{
cquantize->fserrors[i] = (FSERRPTR) gif_alloc_buf(arraysize);
if(cquantize->fserrors[i] == NULL)
{
return FALSE;
}
}
return TRUE;
}
//====================================================================================
// Function Name: Bool Gif_init_gray (decompress_info_ptr cinfo)
// Purpose :
// Parameter :
// Return : none
// Remarks :
// Change Log :
// Author Date Description
// -----------------------------------------------
//=====================================================================================
Bool Gif_init_gray (decompress_info_ptr cinfo)
{
my_cquantize_ptr cquantize;
cquantize = (my_cquantize_ptr)gif_alloc_buf(SIZEOF(my_cquantizer));
if(cquantize == NULL)
{
InsertError(ERROR_MEMEOR_TOO_FEW);
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_NONE)
{
cinfo->dither_mode =GIF_DITHER_FS;
}
if(cinfo->dither_mode == GIF_DITHER_FS)
{
if(!gif_prepare_range_limit_table_gray(cinfo))
{
return FALSE;
}
}
/* Make sure my internal arrays won't overflow */
if (cinfo->out_color_components > MAX_Q_COMPS)
{ // ERREXIT1(cinfo, JERR_QUANT_COMPONENTS, MAX_Q_COMPS);
#ifdef PCVER
WriteToErrorFile("jinit_1pass_quantizer",ERROR_COLOR_COMPONENT_ERROR, FileError);
#endif
ErrorExit(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(!CreateGrayColormap(cinfo))
return FALSE;
if(!CreateGrayColorIndex(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_gray(cinfo))
{
return FALSE;
}
cquantize->on_odd_row = FALSE;
}
return TRUE;
}
//====================================================================================
// Function Name: Bool gif_select_gray(GIFParameter* ucGifParameter)
// Purpose :
// Parameter :
// Return : none
// Remarks :
// Change Log :
// Author Date Description
// -----------------------------------------------
//=====================================================================================
Bool gif_select_gray(GIFParameter* ucGifParameter)
{
switch(ucGifParameter->ucOutGrayscale)
{
case 1:
cinfo->desired_number_of_colors = 2;
break;
case 2:
cinfo->desired_number_of_colors = 4;
break;
case 4:
cinfo->desired_number_of_colors = 16;
break;
case 8:
cinfo->desired_number_of_colors = 256;
break;
default:
return FALSE;
}
return TRUE;
}
//====================================================================================
// Function Name: Bool CreateGrayColormap(decompress_info_ptr cinfo)
// Purpose : create gray color map
// Parameter :
// Return : sucessful return true otherwise return false
// Remarks :
// Change Log :
// Author Date Description
// -----------------------------------------------
//=====================================================================================
Bool CreateGrayColormap(decompress_info_ptr cinfo)
{
uint scale;
uint i,j,k;
uchar reserve;
JSAMPROW indexptr;
uchar** colormap;
my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
#if GRAY_TANSFER == 1
colormap = gif_alloc_memr(3, MAXJSAMPLE+1 );
if(colormap == NULL)
return FALSE;
#else
colormap = cinfo->colormap;
#endif
if(colormap == NULL)
{
return FALSE;
}
#if GRAY_TANSFER == 1
cinfo->colormap = colormap;
#endif
scale = MAXCOLORMAPSIZE/cinfo->desired_number_of_colors;
for (i = 0; i < (uint)(cinfo->out_color_components); i++)
{
k = 0;
reserve = 0;
indexptr = colormap[i];
switch(cinfo->desired_number_of_colors )
{
case 2:
{
for(j = 0 ;j<=255;j++)
{
if(j>127)
{
indexptr[j] = 255;
}
else
{
indexptr[j] = 0;
}
}
}
break;
case 4:
for(j=0;j<=255;j++)
{
if(j>=0 && j<=64)
{
indexptr[j] = 0;
}
if(j>64 && j<128)
{
indexptr[j] = 85;
}
if(j>=128 && j<=192)
{
indexptr[j] = 170;
}
if(j>192)
{
indexptr[j] = 255;
}
}
break;
case 16:
for(j=0; j <= 255; j++)
{
indexptr[j] =(uchar) k;
reserve++;
if(reserve == 16)
{
k += 17;
reserve = 0;
}
if(j == 255)
{
indexptr[j] = 255;
}
}
break;
case 256:
{
for(j=0;j<=255;j++)
{
indexptr[j] =(uchar)j;
}
break;
}
default:
for(j=0;j<=255;j++)
{
indexptr[j] = (uchar)j;
}
break;
}
}
#if GRAY_TANSFER == 1
cquantize->sv_colormap = colormap;
cquantize->sv_actual = 252;//select 252 //
#endif
return TRUE;
}
//====================================================================================
// Function Name: Bool CreateGrayColorIndex(decompress_info_ptr cinfo)
// Purpose : create gray color index
// Parameter :
// Return : sucessful return true otherwise return false
// Remarks :
// Change Log :
// Author Date Description
// -----------------------------------------------
//=====================================================================================
Bool CreateGrayColorIndex(decompress_info_ptr cinfo)
{
JSAMPROW indexptr;
int i,j,k, pad;
uint scale;
uchar reserve;
my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
if (cinfo->dither_mode == GIF_DITHER_ORDERED)
{
pad = MAXJSAMPLE*2;
cquantize->is_padded = TRUE;
}
else
{
pad = 0;
cquantize->is_padded = FALSE;
}
scale = MAXCOLORMAPSIZE/cinfo->desired_number_of_colors;
if(! (cquantize->colorindex[0] = gif_alloc_buf(MAXJSAMPLE+1 + pad)))
{
return FALSE;
}
if(! (cquantize->colorindex[1] = gif_alloc_buf(MAXJSAMPLE+1 + pad)))
{
return FALSE;
}
if(!( cquantize->colorindex[2] = gif_alloc_buf(MAXJSAMPLE+1 + pad)))
{
return FALSE;
}
for (i = 0; i < cinfo->out_color_components; i++)
{
/* adjust colorindex pointers to provide padding at negative indexes. */
if (pad)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -