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

📄 jrdgif.c

📁 EVM板JPEG实现,Texas Instruments TMS320C54x EVM JPEG
💻 C
📖 第 1 页 / 共 2 页
字号:
	symbol_tail[code] = (UINT8) firstcode;
	max_code++;
	/* Is it time to increase code_size? */
	if ((max_code >= limit_code) && (code_size < MAX_LZW_BITS)) {
	  code_size++;
	  limit_code <<= 1;         /* keep equal to 2^code_size */
	}
  }
  
  oldcode = incode;             /* save last input symbol for future use */
  return firstcode;             /* return first byte of symbol's expansion */
}


LOCAL void
ReadColorMap (compress_info_ptr cinfo, int cmaplen, JSAMPARRAY cmap)
/* Read a GIF colormap */
{
  int i;

  for (i = 0; i < cmaplen; i++) {
	cmap[CM_RED][i]   = (JSAMPLE) ReadByte(cinfo);
	cmap[CM_GREEN][i] = (JSAMPLE) ReadByte(cinfo);
	cmap[CM_BLUE][i]  = (JSAMPLE) ReadByte(cinfo);
  }
}


LOCAL void
DoExtension (compress_info_ptr cinfo)
/* Process an extension block */
/* Currently we ignore 'em all */
{
  int extlabel;

  /* Read extension label byte */
  extlabel = ReadByte(cinfo);

  /*
  TRACEMS1(cinfo->emethods, 1, "Ignoring GIF extension block of type 0x%02x",
	   extlabel);
  */

  /* Skip the data block(s) associated with the extension */
  SkipDataBlocks(cinfo);
}


/*
 * Read the file header; return image size and component count.
 */

METHODDEF void
input_init (compress_info_ptr cinfo)
{
  char hdrbuf[10];              /* workspace for reading control blocks */
  UINT16 width, height;         /* image dimensions */
  int colormaplen, aspectRatio;
  int c;

/* Allocate space to store the colormap */
  colormap = (*cinfo->emethods->alloc_small_sarray)
		((long) MAXCOLORMAPSIZE, (long) NUMCOLORS);
/* Read and verify GIF Header */
  if (!(ReadOK(cinfo->input_file, hdrbuf, 5)))
  {
/*  ERREXIT(cinfo->emethods, "Not a GIF file");     */
    send_command(ERR0);  /* send error message      */
    exit();
  }
  if (hdrbuf[0] != 'I' || hdrbuf[1] != 'F')
  {
/*  ERREXIT(cinfo->emethods, "Not a GIF file");     */
    send_command(ERR0);   /* send error message     */
    exit();
  }

  /* Check for expected version numbers.
   * If unknown version, give warning and try to process anyway;
   * this is per recommendation in GIF89a standard.
   */
/*if ((hdrbuf[2] != '8' || hdrbuf[3] != '7' || hdrbuf[4] != 'a') &&
	  (hdrbuf[2] != '8' || hdrbuf[3] != '9' || hdrbuf[4] != 'a'))
	TRACEMS3(cinfo->emethods, 1,
		 "Warning: unexpected GIF version number '%c%c%c'",
		 hdrbuf[3], hdrbuf[4], hdrbuf[5]);
*/
  /* Read and decipher Logical Screen Descriptor */

  if (! ReadOK(cinfo->input_file, hdrbuf, 7))
  {
/*  ERREXIT(cinfo->emethods, "Premature EOF in GIF file"); */
    send_command(ERR1);              /* send error message */
    exit();
  }

  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->emethods, 1, "Warning: nonsquare pixels in input");
*/
  /* Read global colormap if header indicates it is present */
  if (BitSet(hdrbuf[4], COLORMAPFLAG))
	ReadColorMap(cinfo, colormaplen, colormap);

  /* Scan until we reach start of desired image.
   * We don't currently support skipping images, but could add it easily.
   */
  for (;;) {
	c = ReadByte(cinfo);

   if (c == ';')               /* GIF terminator??                */
   {
/*   ERREXIT(cinfo->emethods, "Too few images in GIF file");   */
     send_command(ERR2);   /* send error message               */
     exit();
   }

	if (c == '!') {             /* Extension */
	  DoExtension(cinfo);
	  continue;
	}
	
   if (c != ',') {             /* Not an image separator?                */
/* TRACEMS1(cinfo->emethods, 1, "Bogus input char 0x%02x, ignoring", c); */
     send_command(ERR3);       /* send error message                     */
	  continue;
	}

	/* Read and decipher Local Image Descriptor */

	if (! ReadOK(cinfo->input_file, hdrbuf, 9))
   {
/*   ERREXIT(cinfo->emethods, "Premature EOF in GIF file");    */
     send_command(ERR1);                 /* send error message */
     exit();
   }

	/* 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]);
	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(cinfo, colormaplen, colormap);
	}

   input_code_size = ReadByte(cinfo); /* get minimum-code-size byte   */
/* if (input_code_size < 2 || input_code_size >= MAX_LZW_BITS)
	  ERREXIT1(cinfo->emethods, "Bogus codesize %d", 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 */
  symbol_head = (UINT16 FAR *) (*cinfo->emethods->alloc_medium)
				(LZW_TABLE_SIZE * SIZEOF(UINT16));
  symbol_tail = (UINT8 FAR *) (*cinfo->emethods->alloc_medium)
				(LZW_TABLE_SIZE * SIZEOF(UINT8));
  symbol_stack = (UINT8 FAR *) (*cinfo->emethods->alloc_medium)
				(LZW_TABLE_SIZE * SIZEOF(UINT8));
  InitLZWCode();

  /*
   * If image is interlaced, we read it into a full-size sample array,
   * decompressing as we go; then get_input_row selects rows from the
   * sample array in the proper order.
   */
  if (is_interlaced) {
	/* We request the big array now, but can't access it until the pipeline
	 * controller causes all the big arrays to be allocated.  Hence, the
	 * actual work of reading the image is postponed until the first call
	 * of get_input_row.
	 */
	interlaced_image = (*cinfo->emethods->request_big_sarray)
		((long) width, (long) height, 1L);
	cinfo->methods->get_input_row = load_interlaced_image;
	cinfo->total_passes++;      /* count file reading as separate pass */
  }

  /* Return info about the image. */
  cinfo->input_components = NUMCOLORS;
  cinfo->in_color_space = CS_RGB;
  cinfo->image_width = width;
  cinfo->image_height = height;
  cinfo->data_precision = 8;    /* always, even if 12-bit JSAMPLEs */

/*TRACEMS3(cinfo->emethods, 1, "%ux%ux%d GIF image",
	   (unsigned int) width, (unsigned int) height, colormaplen);
*/

}


/*
 * Read one row of pixels.
 * This version is used for noninterlaced GIF images:
 * we read directly from the GIF file.
 */

METHODDEF void
get_input_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
{
  register JSAMPROW ptr0, ptr1, ptr2;
  register long col;
  register int c;
  
  ptr0 = pixel_row[0];
  ptr1 = pixel_row[1];
  ptr2 = pixel_row[2];
  for (col = cinfo->image_width; col > 0; col--) {
	c = LZWReadByte(cinfo);
	*ptr0++ = colormap[CM_RED][c];
	*ptr1++ = colormap[CM_GREEN][c];
	*ptr2++ = colormap[CM_BLUE][c];
  }
}


/*
 * Read one row of pixels.
 * This version is used for the first call on get_input_row when
 * reading an interlaced GIF file: we read the whole image into memory.
 */

METHODDEF void
load_interlaced_image (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
{
  JSAMPARRAY image_ptr;
  register JSAMPROW sptr;
  register long col;
  long row;

  /* Read the interlaced image into the big array we've created. */
  for (row = 0; row < cinfo->image_height; row++) {
	(*cinfo->methods->progress_monitor) (cinfo, row, cinfo->image_height);
	image_ptr = (*cinfo->emethods->access_big_sarray)
			(interlaced_image, row, TRUE);
	sptr = image_ptr[0];
	for (col = cinfo->image_width; col > 0; col--) {
	  *sptr++ = (JSAMPLE) LZWReadByte(cinfo);
	}
  }
  cinfo->completed_passes++;

  /* Replace method pointer so subsequent calls don't come here. */
  cinfo->methods->get_input_row = get_interlaced_row;
  /* Initialize for get_interlaced_row, and perform first call on it. */
  cur_row_number = 0;
  pass2_offset = (cinfo->image_height + 7L) / 8L;
  pass3_offset = pass2_offset + (cinfo->image_height + 3L) / 8L;
  pass4_offset = pass3_offset + (cinfo->image_height + 1L) / 4L;

  get_interlaced_row(cinfo, pixel_row);
}


/*
 * Read one row of pixels.
 * This version is used for interlaced GIF images:
 * we read from the big in-memory image.
 */

METHODDEF void
get_interlaced_row (compress_info_ptr cinfo, JSAMPARRAY pixel_row)
{
  JSAMPARRAY image_ptr;
  register JSAMPROW sptr, ptr0, ptr1, ptr2;
  register long col;
  register int c;
  long irow;

  /* Figure out which row of interlaced image is needed, and access it. */
  switch ((int) (cur_row_number & 7L)) {
  case 0:                       /* first-pass row */
	irow = cur_row_number >> 3;
	break;
  case 4:                       /* second-pass row */
	irow = (cur_row_number >> 3) + pass2_offset;
	break;
  case 2:                       /* third-pass row */
  case 6:
	irow = (cur_row_number >> 2) + pass3_offset;
	break;
  default:                      /* fourth-pass row */
	irow = (cur_row_number >> 1) + pass4_offset;
	break;
  }
  image_ptr = (*cinfo->emethods->access_big_sarray)
			(interlaced_image, irow, FALSE);
  /* Scan the row, expand colormap, and output */
  sptr = image_ptr[0];
  ptr0 = pixel_row[0];
  ptr1 = pixel_row[1];
  ptr2 = pixel_row[2];
  for (col = cinfo->image_width; col > 0; col--) {
	c = GETJSAMPLE(*sptr++);
	*ptr0++ = colormap[CM_RED][c];
	*ptr1++ = colormap[CM_GREEN][c];
	*ptr2++ = colormap[CM_BLUE][c];
  }
  cur_row_number++;             /* for next time */
}

/*
 * Finish up at the end of the file.
 */

METHODDEF void
input_term (compress_info_ptr cinfo)
{
  /* no work (we let free_all release the workspace) */
}


/*
 * The method selection routine for GIF format input.
 * Note that this must be called by the user interface before calling
 * jpeg_compress.  If multiple input formats are supported, the
 * user interface is responsible for discovering the file format and
 * calling the appropriate method selection routine.
 */

GLOBAL void
jselrgif (compress_info_ptr cinfo)
{
  cinfo->methods->input_init = input_init;
  cinfo->methods->get_input_row = get_input_row; /* assume uninterlaced */
  cinfo->methods->input_term = input_term;
}

#endif /* GIF_SUPPORTED */

⌨️ 快捷键说明

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