30 static char rcsid[] =
"$Id: giftorle.c,v 3.0.1.4 1992/04/30 13:56:24 spencer Exp spencer $";
36 #define MAXCOLORMAPSIZE 256
45 #define MAX_LWZ_BITS 12
47 #define ReadOK(file,buffer,len) (fread(buffer,len,1
,file)!=0
)
48 #define EasyFail(str,status) {fprintf(stderr
,str);return(status);}
49 #define HardFail(str,status) {fprintf(stderr
,str);exit (status);}
51 #define LM_to_uint(a,b) (((b)<<8
)|(a))
54 int ReadColorMap(), IgnoreExtention(), GetCode(), LWZReadByte();
67 unsigned int BitPixel;
68 unsigned int ColorResolution;
69 unsigned int Background;
79 int oflag = 0, nfname = 0;
80 char *outfname = NULL, **infname = NULL;
84 if (
scanargs( argc
, argv
, "% c%- o%-outfile.rle!s infile.gif%*s",
86 &nfname
, &infname
) == 0 )
95 (
void)ReadGIF( NULL );
97 while ( nfname-- > 0 )
98 (
void)ReadGIF( *infname++ );
106 unsigned char buf[16];
110 int use_global_colormap;
118 if (strncmp((
char *)buf,
"GIF87a",6)!=0)
119 EasyFail(
"bad magic number (version mismatch?)\n",
TRUE);
124 Screen.BitPixel = 2<<(buf[4]&0x07);
125 Screen.ColorResolution = (((buf[4]&0x70)>>3)+1);
126 Screen.Background = buf[5];
127 if ((buf[4]&0x80)==0x80) {
128 if (ReadColorMap(fd,Screen.BitPixel,Screen.ColorMap))
140 if (IgnoreExtention(fd))
145 fprintf(stderr,
"Bogus character ignoring '%c'\n",c);
156 if ((buf[8]&0x80)==0x80)
157 use_global_colormap =
FALSE ;
159 use_global_colormap =
TRUE ;
161 bit_pixel = 1<<((buf[8]&0x07)+1);
163 if (! use_global_colormap) {
164 if (ReadColorMap(fd,bit_pixel,LocalColorMap))
168 if (ReadRaster((buf[8]&0x40)==0x40, fd,
LM_to_uint(buf[4],buf[5]),
170 use_global_colormap?Screen.ColorMap:LocalColorMap))
176 ReadColorMap(fd,number,buffer)
182 unsigned char rgb[3];
184 for (i=0;i<number;i++) {
185 if (!
ReadOK(fd,rgb,
sizeof(rgb)))
187 buffer[
CM_RED][i] = rgb[0] ;
198 static char buf[256];
206 if (read(fd,buf,(
int) c)!=(
int) c)
212 GetCode(fd, code_size, flag)
217 static unsigned char buf[280];
218 static int curbit,lastbit,done,last_byte;
229 if ( (curbit+code_size) >= lastbit) {
232 EasyFail(
"Ran off the end of my bits\n",-1);
234 buf[0] = buf[last_byte-2];
235 buf[1] = buf[last_byte-1];
236 if (!
ReadOK(fd,&count,1)) {
237 EasyFail(
"Error in getting buffer size\n",-1);
241 }
else if (!
ReadOK(fd,&buf[2],count))
242 EasyFail(
"Error in getting buffer\n",-1);
243 last_byte = 2 + count;
244 curbit = (curbit - lastbit) + 16;
245 lastbit = (2+count)*8 ;
249 for( i = curbit, j = 0; j < code_size; i++, j++ )
250 ret |= ((buf[ i / 8 ] & (1 << (i % 8))) != 0) << j;
258 LWZReadByte(fd,flag,input_code_size)
263 static int fresh=
FALSE;
265 static int code_size,set_code_size;
266 static int max_code,max_code_size;
267 static int firstcode,oldcode;
268 static int clear_code,end_code;
274 set_code_size = input_code_size;
275 code_size = set_code_size+1;
276 clear_code = 1 << set_code_size ;
277 end_code = clear_code + 1;
278 max_code_size = 2*clear_code;
279 max_code = clear_code+2;
281 GetCode(fd,NULL,
TRUE);
285 for (i=0;i<clear_code;i++) {
290 table[0][i] = table[1][0] = 0;
299 GetCode(fd, code_size,
FALSE);
300 }
while (firstcode == clear_code);
307 while ((code=GetCode(fd,code_size,
FALSE))>=0) {
308 if (code == clear_code) {
309 for (i=0;i<clear_code;i++) {
314 table[0][i] = table[1][i] = 0;
315 code_size = set_code_size+1;
316 max_code_size = 2*clear_code;
317 max_code = clear_code+2;
320 GetCode(fd,code_size,
FALSE);
322 }
else if (code == end_code) {
326 while (
ReadOK(fd,&count,1) && (count!=0))
327 while (count-->0 &&
ReadOK(fd,&junk,1));
329 EasyFail(
"missing EOD in data stream (common occurance)\n",-3);
335 if (code >= max_code) {
340 while (code >= clear_code) {
341 *sp++ = table[1][code];
342 if (code == table[0][code])
343 EasyFail(
"Circular table entry BIG ERROR\n",-1);
344 code = table[0][code];
347 *sp++ = firstcode = table[1][code];
350 table[0][code] = oldcode;
351 table[1][code] = firstcode;
353 if ((max_code >= max_code_size) &&
369 ReadRaster(interlace,fd,len,height,cmap)
382 int ypos=0, pass=interlace ? 0 : 4;
387 malloc(height*
sizeof(
rle_pixel *)))==NULL)
388 EasyFail(
"Unable to malloc space for pixels #1\n",1);
389 for (i=0;i<height;i++) {
394 if (scanline[j][i]!=NULL) free(scanline[i]);
395 EasyFail(
"Unable to malloc space for pixels #2\n",1);
408 for (i=0;i<(1<<8);i++) {
421 hdr
.ymax = height - 1;
427 if (LWZReadByte(fd,
TRUE,c)<0)
430 while ((v=LWZReadByte(fd,
FALSE,c))>=0) {
432 scanline[
RLE_RED][ypos][xpos] = v ;
452 fprintf( stderr,
"%s: Data past end of image.\n", hdr
.cmd );
455 if (ypos >= height) {
471 for ( i = height - 1; i >= 0; i-- ) {
472 ptr[0] = scanline[
RLE_RED][i] ;
482 for (i=0;i<height;i++) {
483 if (scanline[0][i]!=NULL) free(scanline[0][i]);
486 if (scanline[1][i]!=NULL) free(scanline[1][i]);
487 if (scanline[2][i]!=NULL) free(scanline[2][i]);
FILE * rle_open_f(char *prog_name, char *file_name, char *mode)
static int output_colormap
#define RLE_SET_BIT(glob, bit)
void rle_names(rle_hdr *the_hdr, const char *pgmname, const char *fname, int img_num)
#define EasyFail(str, status)
char * cmd_name(char **argv)
void main(int argc, char **argv)
#define ReadOK(file, buffer, len)
int scanargs(int argc, char **argv, const char *format,...)
void rle_puteof(rle_hdr *the_hdr)
void rle_putrow(rows, int rowlen, rle_hdr *the_hdr)
static rle_map out_map[3 *(1<< 8)]
void rle_addhist(argv, rle_hdr *in_hdr, rle_hdr *out_hdr)
void rle_put_setup(rle_hdr *the_hdr)
rle_hdr * rle_hdr_init(rle_hdr *the_hdr)