📄 gif_gray.c
字号:
cquantize->colorindex[i] += MAXJSAMPLE;
}
/* in loop, val = index of current output value, */
/* and k = largest j that maps to current val */
indexptr = cquantize->colorindex[i];
k = 0;
reserve = 0;
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] = 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] = j;
}
break;
}
default:
for(j=0;j<=255;j++)
{
indexptr[j] = j;
}
break;
}
/* Pad at both ends if necessary */
if (pad)
{
for (j = 1; j <= MAXJSAMPLE; j++)
{
indexptr[-j] = indexptr[0];
indexptr[MAXJSAMPLE+j] = indexptr[MAXJSAMPLE];
}
}
}
return TRUE;
}
//====================================================================================
// Function Name: Bool gif_gray(int Width, int Height, uchar* pSrcBuffer,
// uchar* pDestbuffer, int GrayScale)
// Purpose :
// Parameter :
// Return : COLOR to gray
// Remarks :
// Change Log :
// Author Date Description
// -----------------------------------------------
//=====================================================================================
Bool gif_gray(int Width, int Height, uchar* pSrcBuffer, uchar* pDestbuffer, int GrayScale)
{
int offset,cur_x,cur_y;
uchar color;
uchar byte;
cur_x = 0;
if( Width < 0 || Height <0)
{
#ifdef PCVER
WriteToErrorFile("gif_gray",ERROR_PARAMETER_ERROR,FileError);
#endif
InsertError(ERROR_PARAMETER_ERROR);
return FALSE;
}
for(cur_y = 0; cur_y < Height; cur_y++)
{
for(cur_x = 0; cur_x < Width; cur_x++)
{
offset = 3*cur_y*Width + cur_x*3;
color = (pSrcBuffer[offset]*114/1000 + pSrcBuffer[offset +1]*587/1000 + pSrcBuffer[offset +2]*299/1000);
byte = (uchar)color;
pDestbuffer[offset] = byte;
pDestbuffer[offset + 1] = byte;
pDestbuffer[offset + 2] = byte;
}
}
return TRUE;
}
Bool gif_gray1(int Width, int Height, uchar* pSrcBuffer, uchar* pDestbuffer, int GrayScale)
{
int offset,cur_x,cur_y;
uchar color;
uchar byte;
int offset1;
cur_x = 0;
if( Width < 0 || Height <0)
{
#ifdef PCVER
WriteToErrorFile("gif_gray",ERROR_PARAMETER_ERROR,FileError);
#endif
InsertError(ERROR_PARAMETER_ERROR);
return FALSE;
}
for(cur_y = 0; cur_y < Height; cur_y++)
{
for(cur_x = 0; cur_x < Width; cur_x++)
{
offset = 3*cur_y*Width + cur_x*3;
offset1 = cur_y*Width + cur_x;
color = (pSrcBuffer[offset]*114/1000 + pSrcBuffer[offset +1]*587/1000 + pSrcBuffer[offset +2]*299/1000);
byte = (uchar)color;
pDestbuffer[offset1] = byte;
}
}
return TRUE;
}
//====================================================================================
// Function Name: Bool gif_start_gray (decompress_info_ptr cinfo, uint rows,
// uchar* input_buf,uchar* out_buf)
// Purpose : start to improve color quality
// Parameter :
// Return : true or false
// Remarks :
// Change Log :
// Author Date Description
// -----------------------------------------------
//=====================================================================================
Bool gif_start_gray (decompress_info_ptr cinfo, uint rows, uchar* input_buf,uchar* out_buf)
{
my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
size_t arraysize;
int i;
/* Install my colormap. */
cinfo->colormap = cquantize->sv_colormap;
cinfo->actual_number_of_colors = cquantize->sv_actual;
/* Initialize for desired dithering mode. */
switch (cinfo->dither_mode)
{
case GIF_DITHER_NONE:
if (cinfo->out_color_components == 3)
{
color_quantize3_gray(cinfo,input_buf,out_buf,rows);
}
else
{
// color_quantize(cinfo,input_buf,out_buf,rows);
return FALSE;
}
break;
case GIF_DITHER_FS:
cquantize->on_odd_row = FALSE; /* initialize state for F-S dither */
/* Allocate Floyd-Steinberg workspace if didn't already. */
if (cquantize->fserrors[0] == NULL)
{
if(!gif_alloc_fs_workspace_gray(cinfo))
return FALSE;
}
/* Initialize the propagated errors to zero. */
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);
}
//start Floyd-Steinberg
quantize_fs_dither_gray(cinfo,input_buf,out_buf,rows);
//
break;
default:
return FALSE;
}
return TRUE;
}
//====================================================================================
// Function Name: static void color_quantize3_gray (decompress_info_ptr cinfo, uchar* input_buf,
// uchar* output_buf, int num_rows)
// Purpose : Fast path for out_color_components==3, no dithering
// Parameter :
// Return : true or false
// Remarks :
// Change Log :
// Author Date Description
// -----------------------------------------------
//=====================================================================================
static void color_quantize3_gray (decompress_info_ptr cinfo, uchar* input_buf,
uchar* output_buf, int num_rows)
{
my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
register int pixcode;
register JSAMPROW ptrin, ptrout;
JSAMPROW colorindex0 = cquantize->colorindex[0];
JSAMPROW colorindex1 = cquantize->colorindex[1];
JSAMPROW colorindex2 = cquantize->colorindex[2];
int row;
JDIMENSION col;
JDIMENSION width = cinfo->output_width;
for (row = 0; row < num_rows; row++)
{
ptrin = (uchar*)(input_buf + row * width *3) ;
ptrout = (uchar*)(output_buf + row* width );
for (col = width; col > 0; col--)
{
pixcode = GETJSAMPLE(colorindex0[GETJSAMPLE(*ptrin++)]);
pixcode += GETJSAMPLE(colorindex1[GETJSAMPLE(*ptrin++)]);
pixcode += GETJSAMPLE(colorindex2[GETJSAMPLE(*ptrin++)]);
pixcode = pixcode/3;
*ptrout++ = (JSAMPLE) pixcode;
}
}
}
//====================================================================================
// Function Name: static void quantize_fs_dither_gray (decompress_info_ptr cinfo, uchar* input_buf,
// uchar* output_buf, int num_rows)
// Purpose : Floyd-Steinberg dithering for gray color space
// Parameter :
// Return : none
// Remarks :
// Change Log :
// Author Date Description
// -----------------------------------------------
//=====================================================================================
static void quantize_fs_dither_gray (decompress_info_ptr cinfo, uchar* input_buf,
uchar* output_buf, int num_rows)
{
my_cquantize_ptr cquantize = (my_cquantize_ptr) cinfo->cquantize;
register LOCFSERROR cur; /* current error or pixel value */
LOCFSERROR belowerr; /* error for pixel below cur */
LOCFSERROR bpreverr; /* error for below/prev col */
LOCFSERROR bnexterr; /* error for below/next col */
LOCFSERROR delta;
register FSERRPTR errorptr; /* => fserrors[] at column before current */
register JSAMPROW input_ptr;
register JSAMPROW output_ptr;
JSAMPROW colorindex_ci;
JSAMPROW colormap_ci;
int pixcode;
int nc = cinfo->out_color_components;
int dir; /* 1 for left-to-right, -1 for right-to-left */
int dirnc; /* dir * nc */
int row;
JDIMENSION col;
JDIMENSION width = cinfo->output_width;
JSAMPLE *range_limit = cinfo->sample_range_limit;
//SHIFT_TEMPS
for (row = 0; row < num_rows; row++)
{
/* Initialize output values to 0 so can process components separately */
//for (ci = 0; ci < nc; ci++) {
input_ptr = input_buf + row * nc * width ;
output_ptr = output_buf + row * width;
if (cquantize->on_odd_row)
{
/* work right to left in this row */
input_ptr += (width-1) * nc; /* so point to rightmost pixel */
output_ptr += width-1;
dir = -1;
dirnc = -nc;
errorptr = cquantize->fserrors[0] + (width+1); /* => entry after last column */
}
else
{
/* work left to right in this row */
dir = 1;
dirnc = nc;
errorptr = cquantize->fserrors[0]; /* => entry before first column */
}
colorindex_ci = cquantize->colorindex[0];
colormap_ci = cquantize->sv_colormap[0];
/* Preset error values: no error propagated to first pixel from left */
cur = 0;
/* and no error propagated to row below yet */
belowerr = bpreverr = 0;
for (col = width; col > 0; col--)
{
/* cur holds the error propagated from the previous pixel on the
* current line. Add the error propagated from the previous line
* to form the complete error correction term for this pixel, and
* round the error term (which is expressed * 16) to an integer.
* RIGHT_SHIFT rounds towards minus infinity, so adding 8 is correct
* for either sign of the error value.
* Note: errorptr points to *previous* column's array entry.
*/
cur = RIGHT_SHIFT(cur + errorptr[dir] + 8, 4);
/* Form pixel value + error, and range-limit to 0..MAXJSAMPLE.
* The maximum error is +- MAXJSAMPLE; this sets the required size
* of the range_limit array.
*/
cur += GETJSAMPLE(*input_ptr);
cur = GETJSAMPLE(range_limit[cur]);
/* Select output value, accumulate into output code for this pixel */
pixcode = GETJSAMPLE(colorindex_ci[cur]);
*output_ptr = (JSAMPLE) pixcode;
/* Compute actual representation error at this pixel */
/* Note: we can do this even though we don't have the final */
/* pixel code, because the colormap is orthogonal. */
cur -= GETJSAMPLE(colormap_ci[pixcode]);
/* Compute error fractions to be propagated to adjacent pixels.
* Add these into the running sums, and simultaneously shift the
* next-line error sums left by 1 column.
*/
bnexterr = cur;
delta = cur * 2;
cur += delta; /* form error * 3 */
errorptr[0] = (FSERROR) (bpreverr + cur);
cur += delta; /* form error * 5 */
bpreverr = belowerr + cur;
belowerr = bnexterr;
cur += delta; /* form error * 7 */
/* At this point cur contains the 7/16 error value to be propagated
* to the next pixel on the current line, and all the errors for the
* next line have been shifted over. We are therefore ready to move on.
*/
input_ptr += dirnc; /* advance input ptr to next column */
output_ptr += dir; /* advance output ptr to next column */
errorptr += dir; /* advance errorptr to current column */
}
/* Post-loop cleanup: we must unload the final error value into the
* final fserrors[] entry. Note we need not unload belowerr because
* it is for the dummy column before or after the actual array.
*/
errorptr[0] = (FSERROR) bpreverr; /* unload prev err into array */
cquantize->on_odd_row = (cquantize->on_odd_row ? FALSE : TRUE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -