6 |
|
* Currently, we don't know how to get back. |
7 |
|
*/ |
8 |
|
|
9 |
– |
#include <stdio.h> |
10 |
– |
#include <time.h> |
9 |
|
#include <math.h> |
10 |
|
|
11 |
+ |
#include "rtio.h" |
12 |
|
#include "platform.h" |
13 |
|
#include "color.h" |
14 |
|
#include "resolu.h" |
15 |
+ |
#include "clrtab.h" |
16 |
|
|
17 |
|
#define MAXCOLORS 256 |
18 |
|
|
21 |
|
int bmap[MAXCOLORS]; |
22 |
|
|
23 |
|
int currow; |
24 |
– |
|
25 |
– |
extern long ftell(); |
26 |
– |
|
24 |
|
long picstart; |
25 |
+ |
uby8 clrtab[256][3]; |
26 |
|
|
29 |
– |
BYTE clrtab[256][3]; |
30 |
– |
|
27 |
|
extern int samplefac; |
28 |
|
|
33 |
– |
extern int getgifpix(); |
29 |
|
|
30 |
|
COLR *scanln; |
31 |
< |
BYTE *pixscan; |
31 |
> |
uby8 *pixscan; |
32 |
|
|
33 |
|
int xmax, ymax; /* picture size */ |
39 |
– |
|
34 |
|
double gamv = 2.2; /* gamma correction */ |
41 |
– |
|
35 |
|
int greyscale = 0; /* convert to B&W? */ |
43 |
– |
|
36 |
|
int dither = 1; /* dither colors? */ |
45 |
– |
|
37 |
|
int bradj = 0; /* brightness adjustment */ |
47 |
– |
|
38 |
|
int ncolors = 0; /* number of colors requested */ |
39 |
|
|
40 |
|
char *progname; |
41 |
|
|
42 |
+ |
typedef int ifun_t(int x, int y); /* required by the GIF code below */ |
43 |
|
|
44 |
< |
main(argc, argv) |
45 |
< |
int argc; |
46 |
< |
char *argv[]; |
44 |
> |
static void getrow(int y); |
45 |
> |
static void mkclrmap(int nc); |
46 |
> |
static void mkgrymap(int nc); |
47 |
> |
static ifun_t getgifpix; |
48 |
> |
|
49 |
> |
static void GIFEncode(FILE *fp, int GWidth, int GHeight, int GInterlace, |
50 |
> |
int Background, int BitsPerPixel, int Red[], int Green[], int Blue[], |
51 |
> |
ifun_t* GetPixel); |
52 |
> |
|
53 |
> |
|
54 |
> |
int |
55 |
> |
main(int argc, char *argv[]) |
56 |
|
{ |
57 |
|
int bitsperpix; |
58 |
|
int i; |
118 |
|
/* set up gamma correction */ |
119 |
|
setcolrgam(gamv); |
120 |
|
/* figure out the bits per pixel */ |
121 |
< |
if (ncolors < 2 | ncolors > MAXCOLORS) |
121 |
> |
if ((ncolors < 2) | (ncolors > MAXCOLORS)) |
122 |
|
ncolors = MAXCOLORS; |
123 |
|
for (bitsperpix = 1; ncolors > 1<<bitsperpix; bitsperpix++) |
124 |
|
; |
139 |
|
} |
140 |
|
|
141 |
|
|
142 |
< |
getrow(y) /* get the specified row from our image */ |
143 |
< |
int y; |
142 |
> |
static void |
143 |
> |
getrow( /* get the specified row from our image */ |
144 |
> |
int y |
145 |
> |
) |
146 |
|
{ |
147 |
|
if (y == currow) |
148 |
|
return; |
169 |
|
} |
170 |
|
|
171 |
|
|
172 |
< |
mkclrmap(nc) /* make our color map */ |
173 |
< |
int nc; |
172 |
> |
static void |
173 |
> |
mkclrmap( /* make our color map */ |
174 |
> |
int nc |
175 |
> |
) |
176 |
|
{ |
177 |
|
register int i; |
178 |
|
|
194 |
|
gmap[i] = clrtab[i][GRN]; |
195 |
|
bmap[i] = clrtab[i][BLU]; |
196 |
|
} |
197 |
< |
if (dither && (pixscan = (BYTE *)malloc(xmax)) == NULL) |
197 |
> |
if (dither && (pixscan = (uby8 *)malloc(xmax)) == NULL) |
198 |
|
goto memerr; |
199 |
|
return; |
200 |
|
memerr: |
203 |
|
} |
204 |
|
|
205 |
|
|
206 |
< |
mkgrymap(nc) |
207 |
< |
int nc; |
206 |
> |
static void |
207 |
> |
mkgrymap( |
208 |
> |
int nc |
209 |
> |
) |
210 |
|
{ |
211 |
|
register int i; |
212 |
|
|
218 |
|
} |
219 |
|
|
220 |
|
|
221 |
< |
int |
222 |
< |
getgifpix(x, y) /* get a single pixel from our picture */ |
223 |
< |
int x, y; |
221 |
> |
static int |
222 |
> |
getgifpix( /* get a single pixel from our picture */ |
223 |
> |
int x, |
224 |
> |
int y |
225 |
> |
) |
226 |
|
{ |
219 |
– |
int pix; |
220 |
– |
|
227 |
|
getrow(y); |
228 |
|
if (greyscale) |
229 |
|
return((normbright(scanln[x])*ncolors)>>8); |
237 |
|
* SCARY GIF code follows . . . . sorry. |
238 |
|
* |
239 |
|
* Based on GIFENCOD by David Rowley <[email protected]>.A |
240 |
< |
* Lempel-Zim compression based on "compress". |
240 |
> |
* Lempel-Ziv compression based on "compress". |
241 |
|
* |
242 |
|
*/ |
243 |
|
|
249 |
|
* BitsPerPixel, Red, Green, Blue, GetPixel ) |
250 |
|
* |
251 |
|
*****************************************************************************/ |
246 |
– |
typedef int (* ifunptr)(); |
252 |
|
|
253 |
|
#define TRUE 1 |
254 |
|
#define FALSE 0 |
261 |
|
unsigned long cur_accum = 0; |
262 |
|
int cur_bits = 0; |
263 |
|
|
264 |
+ |
static void BumpPixel(void); |
265 |
+ |
static int GIFNextPixel(ifun_t* getpixel); |
266 |
+ |
static void Putword(int w, FILE *fp); |
267 |
+ |
static void compress(int init_bits, FILE *outfile, ifun_t* ReadValue); |
268 |
+ |
|
269 |
+ |
|
270 |
+ |
/* a code_int must be able to hold 2**CBITS values of type int, and also -1 */ |
271 |
+ |
typedef int code_int; |
272 |
+ |
typedef long int count_int; |
273 |
+ |
typedef unsigned char char_type; |
274 |
+ |
|
275 |
+ |
static void output(code_int code); |
276 |
+ |
static void cl_block(void); |
277 |
+ |
static void cl_hash(count_int hsize); |
278 |
+ |
static void writeerr(void); |
279 |
+ |
|
280 |
+ |
static void char_init(void); |
281 |
+ |
static void char_out(int c); |
282 |
+ |
static void flush_char(void); |
283 |
+ |
static void gammawarp(short *sbuf, float gam, int n); |
284 |
+ |
|
285 |
+ |
|
286 |
|
/* |
287 |
|
* Bump the 'curx' and 'cury' to point to the next pixel |
288 |
|
*/ |
289 |
< |
BumpPixel() |
289 |
> |
static void |
290 |
> |
BumpPixel(void) |
291 |
|
{ |
292 |
|
curx++; |
293 |
|
if( curx == Width ) { |
328 |
|
/* |
329 |
|
* Return the next pixel from the image |
330 |
|
*/ |
331 |
< |
GIFNextPixel( getpixel ) |
332 |
< |
ifunptr getpixel; |
331 |
> |
static int |
332 |
> |
GIFNextPixel( |
333 |
> |
ifun_t* getpixel |
334 |
> |
) |
335 |
|
{ |
336 |
|
int r; |
337 |
|
|
346 |
|
/* |
347 |
|
* public GIFEncode |
348 |
|
*/ |
349 |
< |
GIFEncode( fp, GWidth, GHeight, GInterlace, Background, |
350 |
< |
BitsPerPixel, Red, Green, Blue, GetPixel ) |
351 |
< |
FILE *fp; |
352 |
< |
int GWidth, GHeight; |
353 |
< |
int GInterlace; |
354 |
< |
int Background; |
355 |
< |
int BitsPerPixel; |
356 |
< |
int Red[], Green[], Blue[]; |
357 |
< |
ifunptr GetPixel; |
349 |
> |
static void |
350 |
> |
GIFEncode( |
351 |
> |
FILE *fp, |
352 |
> |
int GWidth, |
353 |
> |
int GHeight, |
354 |
> |
int GInterlace, |
355 |
> |
int Background, |
356 |
> |
int BitsPerPixel, |
357 |
> |
int Red[], |
358 |
> |
int Green[], |
359 |
> |
int Blue[], |
360 |
> |
ifun_t* GetPixel |
361 |
> |
) |
362 |
|
{ |
363 |
|
int B; |
364 |
|
int RWidth, RHeight; |
368 |
|
int InitCodeSize; |
369 |
|
int i; |
370 |
|
|
371 |
< |
long cur_accum = 0; |
371 |
> |
cur_accum = 0; /* globals */ |
372 |
|
cur_bits = 0; |
373 |
|
|
374 |
|
Interlace = GInterlace; |
418 |
|
/* |
419 |
|
* Write out a word to the GIF file |
420 |
|
*/ |
421 |
< |
Putword( w, fp ) |
422 |
< |
int w; |
423 |
< |
FILE *fp; |
421 |
> |
static void |
422 |
> |
Putword( |
423 |
> |
int w, |
424 |
> |
FILE *fp |
425 |
> |
) |
426 |
|
{ |
427 |
|
fputc( w & 0xff, fp ); |
428 |
|
fputc( (w/256) & 0xff, fp ); |
442 |
|
#define HSIZE 5003 /* 80% occupancy */ |
443 |
|
|
444 |
|
/* |
409 |
– |
* a code_int must be able to hold 2**CBITS values of type int, and also -1 |
410 |
– |
*/ |
411 |
– |
typedef int code_int; |
412 |
– |
typedef long int count_int; |
413 |
– |
typedef unsigned char char_type; |
414 |
– |
|
415 |
– |
/* |
445 |
|
* |
446 |
|
* GIF Image compression - modified 'compress' |
447 |
|
* |
516 |
|
int ClearCode; |
517 |
|
int EOFCode; |
518 |
|
|
519 |
< |
compress( init_bits, outfile, ReadValue ) |
520 |
< |
int init_bits; |
521 |
< |
FILE *outfile; |
522 |
< |
ifunptr ReadValue; |
519 |
> |
static void |
520 |
> |
compress( |
521 |
> |
int init_bits, |
522 |
> |
FILE *outfile, |
523 |
> |
ifun_t* ReadValue |
524 |
> |
) |
525 |
|
{ |
526 |
|
register long fcode; |
527 |
|
register code_int i = 0; |
560 |
|
while ( (c = GIFNextPixel( ReadValue )) != EOF ) { |
561 |
|
in_count++; |
562 |
|
fcode = (long) (((long) c << maxbits) + ent); |
563 |
< |
/* i = (((code_int)c << hshift) ~ ent); /* xor hashing */ |
563 |
> |
/* i = (((code_int)c << hshift) ~ ent); */ /* xor hashing */ |
564 |
|
i = (((code_int)c << hshift) ^ ent); /* xor hashing */ |
565 |
|
if ( HashTabOf (i) == fcode ) { |
566 |
|
ent = CodeTabOf (i); |
620 |
|
0x01FF, 0x03FF, 0x07FF, 0x0FFF, |
621 |
|
0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF }; |
622 |
|
|
623 |
< |
output( code ) |
624 |
< |
code_int code; |
623 |
> |
static void |
624 |
> |
output( |
625 |
> |
code_int code |
626 |
> |
) |
627 |
|
{ |
628 |
|
cur_accum &= masks[ cur_bits ]; |
629 |
|
if( cur_bits > 0 ) |
672 |
|
/* |
673 |
|
* Clear out the hash table |
674 |
|
*/ |
675 |
< |
cl_block () /* table clear for block compress */ |
675 |
> |
static void |
676 |
> |
cl_block (void) /* table clear for block compress */ |
677 |
|
{ |
678 |
|
cl_hash ( (count_int) hsize ); |
679 |
|
free_ent = ClearCode + 2; |
681 |
|
output( (code_int)ClearCode ); |
682 |
|
} |
683 |
|
|
684 |
< |
cl_hash(hsize) /* reset code table */ |
685 |
< |
register count_int hsize; |
684 |
> |
static void |
685 |
> |
cl_hash( /* reset code table */ |
686 |
> |
register count_int hsize |
687 |
> |
) |
688 |
|
{ |
689 |
|
register count_int *htab_p = htab+hsize; |
690 |
|
register long i; |
714 |
|
*--htab_p = m1; |
715 |
|
} |
716 |
|
|
717 |
< |
writeerr() |
717 |
> |
static void |
718 |
> |
writeerr(void) |
719 |
|
{ |
720 |
|
printf( "error writing output file\n" ); |
721 |
|
exit(1); |
735 |
|
/* |
736 |
|
* Set up the 'byte output' routine |
737 |
|
*/ |
738 |
< |
char_init() |
738 |
> |
static void |
739 |
> |
char_init(void) |
740 |
|
{ |
741 |
|
a_count = 0; |
742 |
|
} |
750 |
|
* Add a character to the end of the current packet, and if it is 254 |
751 |
|
* characters, flush the packet to disk. |
752 |
|
*/ |
753 |
< |
char_out( c ) |
754 |
< |
int c; |
753 |
> |
static void |
754 |
> |
char_out( |
755 |
> |
int c |
756 |
> |
) |
757 |
|
{ |
758 |
|
accum[ a_count++ ] = c; |
759 |
|
if( a_count >= 254 ) |
763 |
|
/* |
764 |
|
* Flush the packet to disk, and reset the accumulator |
765 |
|
*/ |
766 |
< |
flush_char() |
766 |
> |
static void |
767 |
> |
flush_char(void) |
768 |
|
{ |
769 |
|
if( a_count > 0 ) { |
770 |
|
fputc( a_count, g_outfile ); |
776 |
|
static float curgamma; |
777 |
|
static short gamtab[256]; |
778 |
|
|
779 |
< |
gammawarp(sbuf,gam,n) |
780 |
< |
short *sbuf; |
781 |
< |
float gam; |
782 |
< |
int n; |
779 |
> |
static void |
780 |
> |
gammawarp( |
781 |
> |
short *sbuf, |
782 |
> |
float gam, |
783 |
> |
int n |
784 |
> |
) |
785 |
|
{ |
786 |
|
int i; |
744 |
– |
float f; |
787 |
|
|
788 |
|
if(gam!=curgamma) { |
789 |
|
for(i=0; i<256; i++) |