📄 rdgif.c
字号:
sinfo->code_size++;
sinfo->limit_code <<= 1; /* keep equal to 2^code_size */
}
}
sinfo->oldcode = incode; /* save last input symbol for future use */
return sinfo->firstcode; /* return first byte of symbol's expansion */
}
LOCAL(void)
ReadColorMap (gif_source_ptr sinfo, int cmaplen, JSAMPARRAY cmap)
/* Read a GIF colormap */
{
int i;
for (i = 0; i < cmaplen; i++) {
#if BITS_IN_JSAMPLE == 8
#define UPSCALE(x) (x)
#else
#define UPSCALE(x) ((x) << (BITS_IN_JSAMPLE-8))
#endif
cmap[CM_RED][i] = (JSAMPLE) UPSCALE(ReadByte(sinfo));
cmap[CM_GREEN][i] = (JSAMPLE) UPSCALE(ReadByte(sinfo));
cmap[CM_BLUE][i] = (JSAMPLE) UPSCALE(ReadByte(sinfo));
}
}
LOCAL(void)
DoExtension (gif_source_ptr sinfo)
/* Process an extension block */
/* Currently we ignore 'em all */
{
int extlabel;
/* Read extension label byte */
extlabel = ReadByte(sinfo);
TRACEMS1(sinfo->cinfo, 1, JTRC_GIF_EXTENSION, extlabel);
/* Skip the data block(s) associated with the extension */
SkipDataBlocks(sinfo);
}
/*
* Read the file header; return image size and component count.
*/
METHODDEF(void)
start_input_gif (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
{
gif_source_ptr source = (gif_source_ptr) sinfo;
char hdrbuf[10]; /* workspace for reading control blocks */
unsigned int width, height; /* image dimensions */
int colormaplen, aspectRatio;
int c;
/* Allocate space to store the colormap */
source->colormap = (*cinfo->mem->alloc_sarray)
((j_common_ptr) cinfo, JPOOL_IMAGE,
(JDIMENSION) MAXCOLORMAPSIZE, (JDIMENSION) NUMCOLORS);
/* Read and verify GIF Header */
if (! ReadOK(source->pub.input_file, hdrbuf, 6))
ERREXIT(cinfo, JERR_GIF_NOT);
if (hdrbuf[0] != 'G' || hdrbuf[1] != 'I' || hdrbuf[2] != 'F')
ERREXIT(cinfo, JERR_GIF_NOT);
/* Check for expected version numbers.
* If unknown version, give warning and try to process anyway;
* this is per recommendation in GIF89a standard.
*/
if ((hdrbuf[3] != '8' || hdrbuf[4] != '7' || hdrbuf[5] != 'a') &&
(hdrbuf[3] != '8' || hdrbuf[4] != '9' || hdrbuf[5] != 'a'))
TRACEMS3(cinfo, 1, JTRC_GIF_BADVERSION, hdrbuf[3], hdrbuf[4], hdrbuf[5]);
/* Read and decipher Logical Screen Descriptor */
if (! ReadOK(source->pub.input_file, hdrbuf, 7))
ERREXIT(cinfo, JERR_INPUT_EOF);
width = LM_to_uint(hdrbuf[0],hdrbuf[1]);
height = LM_to_uint(hdrbuf[2],hdrbuf[3]);
colormaplen = 2 << (hdrbuf[4] & 0x07);
/* we ignore the color resolution, sort flag, and background color index */
aspectRatio = hdrbuf[6] & 0xFF;
if (aspectRatio != 0 && aspectRatio != 49)
TRACEMS(cinfo, 1, JTRC_GIF_NONSQUARE);
/* Read global colormap if header indicates it is present */
if (BitSet(hdrbuf[4], COLORMAPFLAG))
ReadColorMap(source, colormaplen, source->colormap);
/* Scan until we reach start of desired image.
* We don't currently support skipping images, but could add it easily.
*/
for (;;) {
c = ReadByte(source);
if (c == ';') /* GIF terminator?? */
ERREXIT(cinfo, JERR_GIF_IMAGENOTFOUND);
if (c == '!') { /* Extension */
DoExtension(source);
continue;
}
if (c != ',') { /* Not an image separator? */
WARNMS1(cinfo, JWRN_GIF_CHAR, c);
continue;
}
/* Read and decipher Local Image Descriptor */
if (! ReadOK(source->pub.input_file, hdrbuf, 9))
ERREXIT(cinfo, JERR_INPUT_EOF);
/* we ignore top/left position info, also sort flag */
width = LM_to_uint(hdrbuf[4],hdrbuf[5]);
height = LM_to_uint(hdrbuf[6],hdrbuf[7]);
source->is_interlaced = BitSet(hdrbuf[8], INTERLACE);
/* Read local colormap if header indicates it is present */
/* Note: if we wanted to support skipping images, */
/* we'd need to skip rather than read colormap for ignored images */
if (BitSet(hdrbuf[8], COLORMAPFLAG)) {
colormaplen = 2 << (hdrbuf[8] & 0x07);
ReadColorMap(source, colormaplen, source->colormap);
}
source->input_code_size = ReadByte(source); /* get min-code-size byte */
if (source->input_code_size < 2 || source->input_code_size >= MAX_LZW_BITS)
ERREXIT1(cinfo, JERR_GIF_CODESIZE, source->input_code_size);
/* Reached desired image, so break out of loop */
/* If we wanted to skip this image, */
/* we'd call SkipDataBlocks and then continue the loop */
break;
}
/* Prepare to read selected image: first initialize LZW decompressor */
source->symbol_head = (UINT16 FAR *)
(*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
LZW_TABLE_SIZE * SIZEOF(UINT16));
source->symbol_tail = (UINT8 FAR *)
(*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
LZW_TABLE_SIZE * SIZEOF(UINT8));
source->symbol_stack = (UINT8 FAR *)
(*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
LZW_TABLE_SIZE * SIZEOF(UINT8));
InitLZWCode(source);
/*
* If image is interlaced, we read it into a full-size sample array,
* decompressing as we go; then get_interlaced_row selects rows from the
* sample array in the proper order.
*/
if (source->is_interlaced) {
/* We request the virtual array now, but can't access it until virtual
* arrays have been allocated. Hence, the actual work of reading the
* image is postponed until the first call to get_pixel_rows.
*/
source->interlaced_image = (*cinfo->mem->request_virt_sarray)
((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
(JDIMENSION) width, (JDIMENSION) height, (JDIMENSION) 1);
if (cinfo->progress != NULL) {
cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
progress->total_extra_passes++; /* count file input as separate pass */
}
source->pub.get_pixel_rows = load_interlaced_image;
} else {
source->pub.get_pixel_rows = get_pixel_rows;
}
/* Create compressor input buffer. */
source->pub.buffer = (*cinfo->mem->alloc_sarray)
((j_common_ptr) cinfo, JPOOL_IMAGE,
(JDIMENSION) width * NUMCOLORS, (JDIMENSION) 1);
source->pub.buffer_height = 1;
/* Return info about the image. */
cinfo->in_color_space = JCS_RGB;
cinfo->input_components = NUMCOLORS;
cinfo->data_precision = BITS_IN_JSAMPLE; /* we always rescale data to this */
cinfo->image_width = width;
cinfo->image_height = height;
TRACEMS3(cinfo, 1, JTRC_GIF, width, height, colormaplen);
}
/*
* Read one row of pixels.
* This version is used for noninterlaced GIF images:
* we read directly from the GIF file.
*/
METHODDEF(JDIMENSION)
get_pixel_rows (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
{
gif_source_ptr source = (gif_source_ptr) sinfo;
register int c;
register JSAMPROW ptr;
register JDIMENSION col;
register JSAMPARRAY colormap = source->colormap;
ptr = source->pub.buffer[0];
for (col = cinfo->image_width; col > 0; col--) {
c = LZWReadByte(source);
*ptr++ = colormap[CM_RED][c];
*ptr++ = colormap[CM_GREEN][c];
*ptr++ = colormap[CM_BLUE][c];
}
return 1;
}
/*
* Read one row of pixels.
* This version is used for the first call on get_pixel_rows when
* reading an interlaced GIF file: we read the whole image into memory.
*/
METHODDEF(JDIMENSION)
load_interlaced_image (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
{
gif_source_ptr source = (gif_source_ptr) sinfo;
JSAMPARRAY image_ptr;
register JSAMPROW sptr;
register JDIMENSION col;
JDIMENSION row;
cd_progress_ptr progress = (cd_progress_ptr) cinfo->progress;
/* Read the interlaced image into the virtual array we've created. */
for (row = 0; row < cinfo->image_height; row++) {
if (progress != NULL) {
progress->pub.pass_counter = (long) row;
progress->pub.pass_limit = (long) cinfo->image_height;
(*progress->pub.progress_monitor) ((j_common_ptr) cinfo);
}
image_ptr = (*cinfo->mem->access_virt_sarray)
((j_common_ptr) cinfo, source->interlaced_image,
row, (JDIMENSION) 1, TRUE);
sptr = image_ptr[0];
for (col = cinfo->image_width; col > 0; col--) {
*sptr++ = (JSAMPLE) LZWReadByte(source);
}
}
if (progress != NULL)
progress->completed_extra_passes++;
/* Replace method pointer so subsequent calls don't come here. */
source->pub.get_pixel_rows = get_interlaced_row;
/* Initialize for get_interlaced_row, and perform first call on it. */
source->cur_row_number = 0;
source->pass2_offset = (cinfo->image_height + 7) / 8;
source->pass3_offset = source->pass2_offset + (cinfo->image_height + 3) / 8;
source->pass4_offset = source->pass3_offset + (cinfo->image_height + 1) / 4;
return get_interlaced_row(cinfo, sinfo);
}
/*
* Read one row of pixels.
* This version is used for interlaced GIF images:
* we read from the virtual array.
*/
METHODDEF(JDIMENSION)
get_interlaced_row (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
{
gif_source_ptr source = (gif_source_ptr) sinfo;
JSAMPARRAY image_ptr;
register int c;
register JSAMPROW sptr, ptr;
register JDIMENSION col;
register JSAMPARRAY colormap = source->colormap;
JDIMENSION irow;
/* Figure out which row of interlaced image is needed, and access it. */
switch ((int) (source->cur_row_number & 7)) {
case 0: /* first-pass row */
irow = source->cur_row_number >> 3;
break;
case 4: /* second-pass row */
irow = (source->cur_row_number >> 3) + source->pass2_offset;
break;
case 2: /* third-pass row */
case 6:
irow = (source->cur_row_number >> 2) + source->pass3_offset;
break;
default: /* fourth-pass row */
irow = (source->cur_row_number >> 1) + source->pass4_offset;
break;
}
image_ptr = (*cinfo->mem->access_virt_sarray)
((j_common_ptr) cinfo, source->interlaced_image,
irow, (JDIMENSION) 1, FALSE);
/* Scan the row, expand colormap, and output */
sptr = image_ptr[0];
ptr = source->pub.buffer[0];
for (col = cinfo->image_width; col > 0; col--) {
c = GETJSAMPLE(*sptr++);
*ptr++ = colormap[CM_RED][c];
*ptr++ = colormap[CM_GREEN][c];
*ptr++ = colormap[CM_BLUE][c];
}
source->cur_row_number++; /* for next time */
return 1;
}
/*
* Finish up at the end of the file.
*/
METHODDEF(void)
finish_input_gif (j_compress_ptr cinfo, cjpeg_source_ptr sinfo)
{
/* no work */
}
/*
* The module selection routine for GIF format input.
*/
GLOBAL(cjpeg_source_ptr)
jinit_read_gif (j_compress_ptr cinfo)
{
gif_source_ptr source;
/* Create module interface object */
source = (gif_source_ptr)
(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
SIZEOF(gif_source_struct));
source->cinfo = cinfo; /* make back link for subroutines */
/* Fill in method ptrs, except get_pixel_rows which start_input sets */
source->pub.start_input = start_input_gif;
source->pub.finish_input = finish_input_gif;
return (cjpeg_source_ptr) source;
}
#endif /* GIF_SUPPORTED */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -