/* DECODE.C - An LZW decoder for GIF
* Copyright (C) 1987, by Steven A. Bennett
*
* Permission is given by the author to freely redistribute and include
* this code in any program as long as this credit is given where due.
*
* In accordance with the above, I want to credit Steve Wilhite who wrote
* the code which this is heavily inspired by...
*
* GIF and 'Graphics Interchange Format' are trademarks (tm) of
* Compuserve, Incorporated, an H&R Block Company.
*
* Release Notes: This file contains a decoder routine for GIF images
* which is similar, structurally, to the original routine by Steve Wilhite.
* It is, however, somewhat noticably faster in most cases.
*
*/
#include "std.h"
#include "errs.h"
IMPORT TEXT *malloc(); /* Standard C library allocation */
/* IMPORT INT get_byte()
*
* - This external (machine specific) function is expected to return
* either the next byte from the GIF file, or a negative number, as
* defined in ERRS.H.
*/
IMPORT INT get_byte();
/* IMPORT INT out_line(pixels, linelen)
* UBYTE pixels[];
* INT linelen;
*
* - This function takes a full line of pixels (one byte per pixel) and
* displays them (or does whatever your program wants with them...). It
* should return zero, or negative if an error or some other event occurs
* which would require aborting the decode process... Note that the length
* passed will almost always be equal to the line length passed to the
* decoder function, with the sole exception occurring when an ending code
* occurs in an odd place in the GIF file... In any case, linelen will be
* equal to the number of pixels passed...
*/
IMPORT INT out_line();
/* IMPORT INT bad_code_count;
*
* This value is the only other global required by the using program, and
* is incremented each time an out of range code is read by the decoder.
* When this value is non-zero after a decode, your GIF file is probably
* corrupt in some way...
*/
IMPORT INT bad_code_count;
#define NULL 0L
#define MAX_CODES 4095
/* Static variables */
LOCAL WORD curr_size; /* The current code size */
LOCAL WORD clear; /* Value for a clear code */
LOCAL WORD ending; /* Value for a ending code */
LOCAL WORD newcodes; /* First available code */
LOCAL WORD top_slot; /* Highest code for current size */
LOCAL WORD slot; /* Last read code */
/* The following static variables are used
* for seperating out codes
*/
LOCAL WORD navail_bytes = 0; /* # bytes left in block */
LOCAL WORD nbits_left = 0; /* # bits left in current byte */
LOCAL UTINY b1; /* Current byte */
LOCAL UTINY byte_buff[257]; /* Current block */
LOCAL UTINY *pbytes; /* Pointer to next byte in block */
LOCAL LONG code_mask[13] = {
0,
0x0001, 0x0003,
0x0007, 0x000F,
0x001F, 0x003F,
0x007F, 0x00FF,
0x01FF, 0x03FF,
0x07FF, 0x0FFF
};
/* This function initializes the decoder for reading a new image.
*/
LOCAL WORD init_exp(size)
WORD size;
{
curr_size = size + 1;
top_slot = 1 << curr_size;
clear = 1 << size;
ending = clear + 1;
slot = newcodes = ending + 1;
navail_bytes = nbits_left = 0;
return(0);
}
/* get_next_code()
* - gets the next code from the GIF file. Returns the code, or else
* a negative number in case of file errors...
*/
LOCAL WORD get_next_code()
{
WORD i, x;
ULONG ret;
if (nbits_left == 0)
{
if (navail_bytes <= 0)
{
/* Out of bytes in current block, so read next block
*/
pbytes = byte_buff;
if ((navail_bytes = get_byte()) < 0)
return(navail_bytes);
else if (navail_bytes)
{
for (i = 0; i < navail_bytes; ++i)
{
if ((x = get_byte()) < 0)
return(x);
byte_buff[i] = x;
}
}
}
b1 = *pbytes++;
nbits_left = 8;
--navail_bytes;
}
ret = b1 >> (8 - nbits_left);
while (curr_size > nbits_left)
{
if (navail_bytes <= 0)
{
/* Out of bytes in current block, so read next block
*/
pbytes = byte_buff;
if ((navail_bytes = get_byte()) < 0)
return(navail_bytes);
else if (navail_bytes)
{
for (i = 0; i < navail_bytes; ++i)
{
if ((x = get_byte()) < 0)
return(x);
byte_buff[i] = x;
}
}
}
b1 = *pbytes++;
ret |= b1 << nbits_left;
nbits_left += 8;
--navail_bytes;
}
nbits_left -= curr_size;
ret &= code_mask[curr_size];
return((WORD)(ret));
}
/* The reason we have these seperated like this instead of using
* a structure like the original Wilhite code did, is because this
* stuff generally produces significantly faster code when compiled...
* This code is full of similar speedups... (For a good book on writing
* C for speed or for space optomisation, see Efficient C by Tom Plum,
* published by Plum-Hall Associates...)
*/
LOCAL UTINY stack[MAX_CODES + 1]; /* Stack for storing pixels */
LOCAL UTINY suffix[MAX_CODES + 1]; /* Suffix table */
LOCAL UWORD prefix[MAX_CODES + 1]; /* Prefix linked list */
/* WORD decoder(linewidth)
* WORD linewidth; * Pixels per line of image *
*
* - This function decodes an LZW image, according to the method used
* in the GIF spec. Every *linewidth* "characters" (ie. pixels) decoded
* will generate a call to out_line(), which is a user specific function
* to display a line of pixels. The function gets it's codes from
* get_next_code() which is responsible for reading blocks of data and
* seperating them into the proper size codes. Finally, get_byte() is
* the global routine to read the next byte from the GIF file.
*
* It is generally a good idea to have linewidth correspond to the actual
* width of a line (as specified in the Image header) to make your own
* code a bit simpler, but it isn't absolutely necessary.
*
* Returns: 0 if successful, else negative. (See ERRS.H)
*
*/
WORD decoder(linewidth)
WORD linewidth;
{
FAST UTINY *sp, *bufptr;
UTINY *buf;
FAST WORD code, fc, oc, bufcnt;
WORD c, size, ret;
/* Initialize for decoding a new image...
*/
if ((size = get_byte()) < 0)
return(size);
if (size < 2 || 9 < size)
return(BAD_CODE_SIZE);
init_exp(size);
/* Initialize in case they forgot to put in a clear code.
* (This shouldn't happen, but we'll try and decode it anyway...)
*/
oc = fc = 0;
/* Allocate space for the decode buffer
*/
if ((buf = (UTINY *)malloc(linewidth + 1)) == NULL)
return(OUT_OF_MEMORY);
/* Set up the stack pointer and decode buffer pointer
*/
sp = stack;
bufptr = buf;
bufcnt = linewidth;
/* This is the main loop. For each code we get we pass through the
* linked list of prefix codes, pushing the corresponding "character" for
* each code onto the stack. When the list reaches a single "character"
* we push that on the stack too, and then start unstacking each
* character for output in the correct order. Special handling is
* included for the clear code, and the whole thing ends when we get
* an ending code.
*/
while ((c = get_next_code()) != ending)
{
/* If we had a file error, return without completing the decode
*/
if (c < 0)
{
free(buf);
return(0);
}
/* If the code is a clear code, reinitialize all necessary items.
*/
if (c == clear)
{
curr_size = size + 1;
slot = newcodes;
top_slot = 1
GIF的C语言解码程序
3星 · 超过75%的资源 需积分: 9 168 浏览量
2008-12-04
15:41:06
上传
评论 1
收藏 5KB ZIP 举报
wgc204
- 粉丝: 1
- 资源: 13
最新资源
- ZEND解密dezender12
- sony 索尼IMX334摄像头模组电路板AD版硬件PCB图(6层板).zip
- 基于flask和echarts融合交易策略的bitfinex可视化微服务.zip
- 包含了wvp-assist.tar wvp-talk.tar zlmediakit.tar .
- 3r4efgh53wgrf43tw
- 2024新版Java基础从入门到精通全套视频+资料下载
- Spring AI大模型视频教程+ChatGPT视频教程+OpenAI大模型视频教程(资料+视频教程)
- ABB工业机器人教程PDF版本
- 123321123323211
- 三相桥式全桥整流电路MATALB Simulink仿真文件
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈