| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: color.c,v 2.15 2004/09/14 02:53:50 greg Exp $";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
* color.c - routines for color calculations.
|
| 6 |
*
|
| 7 |
* Externals declared in color.h
|
| 8 |
*/
|
| 9 |
|
| 10 |
#include "copyright.h"
|
| 11 |
|
| 12 |
#include <stdio.h>
|
| 13 |
|
| 14 |
#include <stdlib.h>
|
| 15 |
|
| 16 |
#include <math.h>
|
| 17 |
|
| 18 |
#include "color.h"
|
| 19 |
|
| 20 |
#ifdef getc_unlocked /* avoid horrendous overhead of flockfile */
|
| 21 |
#undef getc
|
| 22 |
#undef putc
|
| 23 |
#define getc getc_unlocked
|
| 24 |
#define putc putc_unlocked
|
| 25 |
#endif
|
| 26 |
|
| 27 |
#define MINELEN 8 /* minimum scanline length for encoding */
|
| 28 |
#define MAXELEN 0x7fff /* maximum scanline length for encoding */
|
| 29 |
#define MINRUN 4 /* minimum run length */
|
| 30 |
|
| 31 |
|
| 32 |
char *
|
| 33 |
tempbuffer(len) /* get a temporary buffer */
|
| 34 |
unsigned int len;
|
| 35 |
{
|
| 36 |
static char *tempbuf = NULL;
|
| 37 |
static unsigned tempbuflen = 0;
|
| 38 |
|
| 39 |
if (len > tempbuflen) {
|
| 40 |
if (tempbuflen > 0)
|
| 41 |
tempbuf = (char *)realloc((void *)tempbuf, len);
|
| 42 |
else
|
| 43 |
tempbuf = (char *)malloc(len);
|
| 44 |
tempbuflen = tempbuf==NULL ? 0 : len;
|
| 45 |
}
|
| 46 |
return(tempbuf);
|
| 47 |
}
|
| 48 |
|
| 49 |
|
| 50 |
int
|
| 51 |
fwritecolrs(scanline, len, fp) /* write out a colr scanline */
|
| 52 |
register COLR *scanline;
|
| 53 |
int len;
|
| 54 |
register FILE *fp;
|
| 55 |
{
|
| 56 |
register int i, j, beg, cnt = 1;
|
| 57 |
int c2;
|
| 58 |
|
| 59 |
if ((len < MINELEN) | (len > MAXELEN)) /* OOBs, write out flat */
|
| 60 |
return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len);
|
| 61 |
/* put magic header */
|
| 62 |
putc(2, fp);
|
| 63 |
putc(2, fp);
|
| 64 |
putc(len>>8, fp);
|
| 65 |
putc(len&255, fp);
|
| 66 |
/* put components seperately */
|
| 67 |
for (i = 0; i < 4; i++) {
|
| 68 |
for (j = 0; j < len; j += cnt) { /* find next run */
|
| 69 |
for (beg = j; beg < len; beg += cnt) {
|
| 70 |
for (cnt = 1; cnt < 127 && beg+cnt < len &&
|
| 71 |
scanline[beg+cnt][i] == scanline[beg][i]; cnt++)
|
| 72 |
;
|
| 73 |
if (cnt >= MINRUN)
|
| 74 |
break; /* long enough */
|
| 75 |
}
|
| 76 |
if (beg-j > 1 && beg-j < MINRUN) {
|
| 77 |
c2 = j+1;
|
| 78 |
while (scanline[c2++][i] == scanline[j][i])
|
| 79 |
if (c2 == beg) { /* short run */
|
| 80 |
putc(128+beg-j, fp);
|
| 81 |
putc(scanline[j][i], fp);
|
| 82 |
j = beg;
|
| 83 |
break;
|
| 84 |
}
|
| 85 |
}
|
| 86 |
while (j < beg) { /* write out non-run */
|
| 87 |
if ((c2 = beg-j) > 128) c2 = 128;
|
| 88 |
putc(c2, fp);
|
| 89 |
while (c2--)
|
| 90 |
putc(scanline[j++][i], fp);
|
| 91 |
}
|
| 92 |
if (cnt >= MINRUN) { /* write out run */
|
| 93 |
putc(128+cnt, fp);
|
| 94 |
putc(scanline[beg][i], fp);
|
| 95 |
} else
|
| 96 |
cnt = 0;
|
| 97 |
}
|
| 98 |
}
|
| 99 |
return(ferror(fp) ? -1 : 0);
|
| 100 |
}
|
| 101 |
|
| 102 |
|
| 103 |
static int
|
| 104 |
oldreadcolrs(scanline, len, fp) /* read in an old colr scanline */
|
| 105 |
register COLR *scanline;
|
| 106 |
int len;
|
| 107 |
register FILE *fp;
|
| 108 |
{
|
| 109 |
int rshift;
|
| 110 |
register int i;
|
| 111 |
|
| 112 |
rshift = 0;
|
| 113 |
|
| 114 |
while (len > 0) {
|
| 115 |
scanline[0][RED] = getc(fp);
|
| 116 |
scanline[0][GRN] = getc(fp);
|
| 117 |
scanline[0][BLU] = getc(fp);
|
| 118 |
scanline[0][EXP] = getc(fp);
|
| 119 |
if (feof(fp) || ferror(fp))
|
| 120 |
return(-1);
|
| 121 |
if (scanline[0][RED] == 1 &&
|
| 122 |
scanline[0][GRN] == 1 &&
|
| 123 |
scanline[0][BLU] == 1) {
|
| 124 |
for (i = scanline[0][EXP] << rshift; i > 0; i--) {
|
| 125 |
copycolr(scanline[0], scanline[-1]);
|
| 126 |
scanline++;
|
| 127 |
len--;
|
| 128 |
}
|
| 129 |
rshift += 8;
|
| 130 |
} else {
|
| 131 |
scanline++;
|
| 132 |
len--;
|
| 133 |
rshift = 0;
|
| 134 |
}
|
| 135 |
}
|
| 136 |
return(0);
|
| 137 |
}
|
| 138 |
|
| 139 |
|
| 140 |
int
|
| 141 |
freadcolrs(scanline, len, fp) /* read in an encoded colr scanline */
|
| 142 |
register COLR *scanline;
|
| 143 |
int len;
|
| 144 |
register FILE *fp;
|
| 145 |
{
|
| 146 |
register int i, j;
|
| 147 |
int code, val;
|
| 148 |
/* determine scanline type */
|
| 149 |
if ((len < MINELEN) | (len > MAXELEN))
|
| 150 |
return(oldreadcolrs(scanline, len, fp));
|
| 151 |
if ((i = getc(fp)) == EOF)
|
| 152 |
return(-1);
|
| 153 |
if (i != 2) {
|
| 154 |
ungetc(i, fp);
|
| 155 |
return(oldreadcolrs(scanline, len, fp));
|
| 156 |
}
|
| 157 |
scanline[0][GRN] = getc(fp);
|
| 158 |
scanline[0][BLU] = getc(fp);
|
| 159 |
if ((i = getc(fp)) == EOF)
|
| 160 |
return(-1);
|
| 161 |
if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) {
|
| 162 |
scanline[0][RED] = 2;
|
| 163 |
scanline[0][EXP] = i;
|
| 164 |
return(oldreadcolrs(scanline+1, len-1, fp));
|
| 165 |
}
|
| 166 |
if ((scanline[0][BLU]<<8 | i) != len)
|
| 167 |
return(-1); /* length mismatch! */
|
| 168 |
/* read each component */
|
| 169 |
for (i = 0; i < 4; i++)
|
| 170 |
for (j = 0; j < len; ) {
|
| 171 |
if ((code = getc(fp)) == EOF)
|
| 172 |
return(-1);
|
| 173 |
if (code > 128) { /* run */
|
| 174 |
code &= 127;
|
| 175 |
if ((val = getc(fp)) == EOF)
|
| 176 |
return -1;
|
| 177 |
if (j + code > len)
|
| 178 |
return -1; /* overrun */
|
| 179 |
while (code--)
|
| 180 |
scanline[j++][i] = val;
|
| 181 |
} else { /* non-run */
|
| 182 |
if (j + code > len)
|
| 183 |
return -1; /* overrun */
|
| 184 |
while (code--) {
|
| 185 |
if ((val = getc(fp)) == EOF)
|
| 186 |
return -1;
|
| 187 |
scanline[j++][i] = val;
|
| 188 |
}
|
| 189 |
}
|
| 190 |
}
|
| 191 |
return(0);
|
| 192 |
}
|
| 193 |
|
| 194 |
|
| 195 |
int
|
| 196 |
fwritescan(scanline, len, fp) /* write out a scanline */
|
| 197 |
register COLOR *scanline;
|
| 198 |
int len;
|
| 199 |
FILE *fp;
|
| 200 |
{
|
| 201 |
COLR *clrscan;
|
| 202 |
int n;
|
| 203 |
register COLR *sp;
|
| 204 |
/* get scanline buffer */
|
| 205 |
if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
|
| 206 |
return(-1);
|
| 207 |
clrscan = sp;
|
| 208 |
/* convert scanline */
|
| 209 |
n = len;
|
| 210 |
while (n-- > 0) {
|
| 211 |
setcolr(sp[0], scanline[0][RED],
|
| 212 |
scanline[0][GRN],
|
| 213 |
scanline[0][BLU]);
|
| 214 |
scanline++;
|
| 215 |
sp++;
|
| 216 |
}
|
| 217 |
return(fwritecolrs(clrscan, len, fp));
|
| 218 |
}
|
| 219 |
|
| 220 |
|
| 221 |
int
|
| 222 |
freadscan(scanline, len, fp) /* read in a scanline */
|
| 223 |
register COLOR *scanline;
|
| 224 |
int len;
|
| 225 |
FILE *fp;
|
| 226 |
{
|
| 227 |
register COLR *clrscan;
|
| 228 |
|
| 229 |
if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
|
| 230 |
return(-1);
|
| 231 |
if (freadcolrs(clrscan, len, fp) < 0)
|
| 232 |
return(-1);
|
| 233 |
/* convert scanline */
|
| 234 |
colr_color(scanline[0], clrscan[0]);
|
| 235 |
while (--len > 0) {
|
| 236 |
scanline++; clrscan++;
|
| 237 |
if (clrscan[0][RED] == clrscan[-1][RED] &&
|
| 238 |
clrscan[0][GRN] == clrscan[-1][GRN] &&
|
| 239 |
clrscan[0][BLU] == clrscan[-1][BLU] &&
|
| 240 |
clrscan[0][EXP] == clrscan[-1][EXP])
|
| 241 |
copycolor(scanline[0], scanline[-1]);
|
| 242 |
else
|
| 243 |
colr_color(scanline[0], clrscan[0]);
|
| 244 |
}
|
| 245 |
return(0);
|
| 246 |
}
|
| 247 |
|
| 248 |
|
| 249 |
void
|
| 250 |
setcolr(clr, r, g, b) /* assign a short color value */
|
| 251 |
register COLR clr;
|
| 252 |
double r, g, b;
|
| 253 |
{
|
| 254 |
double d;
|
| 255 |
int e;
|
| 256 |
|
| 257 |
d = r > g ? r : g;
|
| 258 |
if (b > d) d = b;
|
| 259 |
|
| 260 |
if (d <= 1e-32) {
|
| 261 |
clr[RED] = clr[GRN] = clr[BLU] = 0;
|
| 262 |
clr[EXP] = 0;
|
| 263 |
return;
|
| 264 |
}
|
| 265 |
|
| 266 |
d = frexp(d, &e) * 255.9999 / d;
|
| 267 |
|
| 268 |
if (r > 0.0)
|
| 269 |
clr[RED] = r * d;
|
| 270 |
else
|
| 271 |
clr[RED] = 0;
|
| 272 |
if (g > 0.0)
|
| 273 |
clr[GRN] = g * d;
|
| 274 |
else
|
| 275 |
clr[GRN] = 0;
|
| 276 |
if (b > 0.0)
|
| 277 |
clr[BLU] = b * d;
|
| 278 |
else
|
| 279 |
clr[BLU] = 0;
|
| 280 |
|
| 281 |
clr[EXP] = e + COLXS;
|
| 282 |
}
|
| 283 |
|
| 284 |
|
| 285 |
void
|
| 286 |
colr_color(col, clr) /* convert short to float color */
|
| 287 |
register COLOR col;
|
| 288 |
register COLR clr;
|
| 289 |
{
|
| 290 |
double f;
|
| 291 |
|
| 292 |
if (clr[EXP] == 0)
|
| 293 |
col[RED] = col[GRN] = col[BLU] = 0.0;
|
| 294 |
else {
|
| 295 |
f = ldexp(1.0, (int)clr[EXP]-(COLXS+8));
|
| 296 |
col[RED] = (clr[RED] + 0.5)*f;
|
| 297 |
col[GRN] = (clr[GRN] + 0.5)*f;
|
| 298 |
col[BLU] = (clr[BLU] + 0.5)*f;
|
| 299 |
}
|
| 300 |
}
|
| 301 |
|
| 302 |
|
| 303 |
int
|
| 304 |
bigdiff(c1, c2, md) /* c1 delta c2 > md? */
|
| 305 |
register COLOR c1, c2;
|
| 306 |
double md;
|
| 307 |
{
|
| 308 |
register int i;
|
| 309 |
|
| 310 |
for (i = 0; i < 3; i++)
|
| 311 |
if (colval(c1,i)-colval(c2,i) > md*colval(c2,i) ||
|
| 312 |
colval(c2,i)-colval(c1,i) > md*colval(c1,i))
|
| 313 |
return(1);
|
| 314 |
return(0);
|
| 315 |
}
|