12 |
|
|
13 |
|
#include <stdio.h> |
14 |
|
|
15 |
+ |
#include <math.h> |
16 |
+ |
|
17 |
|
#include "color.h" |
18 |
|
|
19 |
|
#define MINELEN 8 /* minimum scanline length for encoding */ |
20 |
+ |
#define MAXELEN 0x7fff /* maximum scanline length for encoding */ |
21 |
|
#define MINRUN 4 /* minimum run length */ |
22 |
|
|
23 |
|
|
27 |
|
{ |
28 |
|
extern char *malloc(), *realloc(); |
29 |
|
static char *tempbuf = NULL; |
30 |
< |
static int tempbuflen = 0; |
30 |
> |
static unsigned tempbuflen = 0; |
31 |
|
|
32 |
|
if (len > tempbuflen) { |
33 |
|
if (tempbuflen > 0) |
42 |
|
|
43 |
|
fwritecolrs(scanline, len, fp) /* write out a colr scanline */ |
44 |
|
register COLR *scanline; |
45 |
< |
int len; |
45 |
> |
unsigned len; |
46 |
|
register FILE *fp; |
47 |
|
{ |
48 |
|
register int i, j, beg, cnt; |
49 |
|
int c2; |
50 |
|
|
51 |
< |
if (len < MINELEN) /* too small to encode */ |
51 |
> |
if (len < MINELEN | len > MAXELEN) /* OOBs, write out flat */ |
52 |
|
return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len); |
53 |
< |
if (len > 32767) /* too big! */ |
51 |
< |
return(-1); |
52 |
< |
putc(2, fp); /* put magic header */ |
53 |
> |
/* put magic header */ |
54 |
|
putc(2, fp); |
55 |
+ |
putc(2, fp); |
56 |
|
putc(len>>8, fp); |
57 |
|
putc(len&255, fp); |
58 |
|
/* put components seperately */ |
65 |
|
if (cnt >= MINRUN) |
66 |
|
break; /* long enough */ |
67 |
|
} |
68 |
< |
while (j < beg) { /* write out non-run(s) */ |
68 |
> |
if (beg-j > 1 && beg-j < MINRUN) { |
69 |
> |
c2 = j+1; |
70 |
> |
while (scanline[c2++][i] == scanline[j][i]) |
71 |
> |
if (c2 == beg) { /* short run */ |
72 |
> |
putc(128+beg-j, fp); |
73 |
> |
putc(scanline[j][i], fp); |
74 |
> |
j = beg; |
75 |
> |
break; |
76 |
> |
} |
77 |
> |
} |
78 |
> |
while (j < beg) { /* write out non-run */ |
79 |
|
if ((c2 = beg-j) > 128) c2 = 128; |
80 |
|
putc(c2, fp); |
81 |
|
while (c2--) |
98 |
|
register FILE *fp; |
99 |
|
{ |
100 |
|
register int i, j; |
101 |
< |
int code; |
101 |
> |
int code, val; |
102 |
|
/* determine scanline type */ |
103 |
< |
if (len < MINELEN) |
103 |
> |
if (len < MINELEN | len > MAXELEN) |
104 |
|
return(oldreadcolrs(scanline, len, fp)); |
105 |
|
if ((i = getc(fp)) == EOF) |
106 |
|
return(-1); |
125 |
|
if ((code = getc(fp)) == EOF) |
126 |
|
return(-1); |
127 |
|
if (code > 128) { /* run */ |
128 |
< |
scanline[j++][i] = getc(fp); |
129 |
< |
for (code &= 127; --code; j++) |
130 |
< |
scanline[j][i] = scanline[j-1][i]; |
128 |
> |
code &= 127; |
129 |
> |
val = getc(fp); |
130 |
> |
while (code--) |
131 |
> |
scanline[j++][i] = val; |
132 |
|
} else /* non-run */ |
133 |
|
while (code--) |
134 |
|
scanline[j++][i] = getc(fp); |
229 |
|
register COLR clr; |
230 |
|
double r, g, b; |
231 |
|
{ |
219 |
– |
double frexp(); |
232 |
|
double d; |
233 |
|
int e; |
234 |
|
|
241 |
|
return; |
242 |
|
} |
243 |
|
|
244 |
< |
d = frexp(d, &e) * 256.0 / d; |
244 |
> |
d = frexp(d, &e) * 255.9999 / d; |
245 |
|
|
246 |
|
clr[RED] = r * d; |
247 |
|
clr[GRN] = g * d; |