ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/color.c
(Generate patch)

Comparing ray/src/common/color.c (file contents):
Revision 1.8 by greg, Fri Oct 20 20:35:22 1989 UTC vs.
Revision 2.4 by greg, Fri Oct 2 15:59:58 1992 UTC

# Line 1 | Line 1
1 < /* Copyright (c) 1986 Regents of the University of California */
1 > /* Copyright (c) 1991 Regents of the University of California */
2  
3   #ifndef lint
4   static char SCCSid[] = "$SunId$ LBL";
# Line 14 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
14  
15   #include  "color.h"
16  
17 < #ifdef  SPEC_RGB
18 < /*
19 < *      The following table contains the CIE tristimulus integrals
20 < *  for X, Y, and Z.  The table is cumulative, so that
21 < *  each color coordinate integrates to 1.
22 < */
17 > #define  MINELEN        8       /* minimum scanline length for encoding */
18 > #define  MINRUN         4       /* minimum run length */
19  
20 < #define  STARTWL        380             /* starting wavelength (nanometers) */
21 < #define  INCWL          10              /* wavelength increment */
22 < #define  NINC           40              /* # of values */
20 > #ifndef frexp
21 > extern double  frexp();
22 > #endif
23  
28 static BYTE  chroma[3][NINC] = {
29        {                                                       /* X */
30                0,   0,   0,   2,   6,   13,  22,  30,  36,  41,
31                42,  43,  43,  44,  46,  52,  60,  71,  87,  106,
32                128, 153, 178, 200, 219, 233, 243, 249, 252, 254,
33                255, 255, 255, 255, 255, 255, 255, 255, 255, 255
34        }, {                                                    /* Y */
35                0,   0,   0,   0,   0,   1,   2,   4,   7,   11,
36                17,  24,  34,  48,  64,  84,  105, 127, 148, 169,
37                188, 205, 220, 232, 240, 246, 250, 253, 254, 255,
38                255, 255, 255, 255, 255, 255, 255, 255, 255, 255
39        }, {                                                    /* Z */
40                0,   0,   2,   10,  32,  66,  118, 153, 191, 220,
41                237, 246, 251, 253, 254, 255, 255, 255, 255, 255,
42                255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
43                255, 255, 255, 255, 255, 255, 255, 255, 255, 255
44        }
45 };
24  
25 <
26 < spec_rgb(col, s, e)             /* comput RGB color from spectral range */
27 < COLOR  col;
50 < int  s, e;
25 > char *
26 > tempbuffer(len)                 /* get a temporary buffer */
27 > unsigned  len;
28   {
29 <        COLOR  ciecolor;
29 >        extern char  *malloc(), *realloc();
30 >        static char  *tempbuf = NULL;
31 >        static int  tempbuflen = 0;
32  
33 <        spec_cie(ciecolor, s, e);
34 <        cie_rgb(col, ciecolor);
33 >        if (len > tempbuflen) {
34 >                if (tempbuflen > 0)
35 >                        tempbuf = realloc(tempbuf, len);
36 >                else
37 >                        tempbuf = malloc(len);
38 >                tempbuflen = tempbuf==NULL ? 0 : len;
39 >        }
40 >        return(tempbuf);
41   }
42  
43  
44 < spec_cie(col, s, e)             /* compute a color from a spectral range */
45 < COLOR  col;             /* returned color */
46 < int  s, e;              /* starting and ending wavelengths */
44 > fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
45 > register COLR  *scanline;
46 > unsigned  len;
47 > register FILE  *fp;
48   {
49 <        register int  i, d, r;
49 >        register int  i, j, beg, cnt;
50 >        int  c2;
51          
52 <        s -= STARTWL;
53 <        if (s < 0)
54 <                s = 0;
55 <
56 <        e -= STARTWL;
57 <        if (e >= INCWL*(NINC - 1))
58 <                e = INCWL*(NINC - 1) - 1;
59 <
60 <        d = e / INCWL;                  /* interpolate values */
61 <        r = e % INCWL;
62 <        for (i = 0; i < 3; i++)
63 <                col[i] = chroma[i][d]*(INCWL - r) + chroma[i][d + 1]*r;
64 <
65 <        d = s / INCWL;
66 <        r = s % INCWL;
67 <        for (i = 0; i < 3; i++)
68 <                col[i] -= chroma[i][d]*(INCWL - r) - chroma[i][d + 1]*r;
69 <
70 <        col[RED] = (col[RED] + 0.5) / (256*INCWL);
71 <        col[GRN] = (col[GRN] + 0.5) / (256*INCWL);
72 <        col[BLU] = (col[BLU] + 0.5) / (256*INCWL);
73 < }
74 <
75 <
76 < cie_rgb(rgbcolor, ciecolor)             /* convert CIE to RGB (NTSC) */
77 < register COLOR  rgbcolor, ciecolor;
78 < {
79 <        static float  cmat[3][3] = {
80 <                1.73, -.48, -.26,
81 <                -.81, 1.65, -.02,
82 <                 .08, -.17, 1.28,
83 <        };
84 <        register int  i;
85 <
86 <        for (i = 0; i < 3; i++) {
87 <                rgbcolor[i] =   cmat[i][0]*ciecolor[0] +
88 <                                cmat[i][1]*ciecolor[1] +
89 <                                cmat[i][2]*ciecolor[2] ;
90 <                if (rgbcolor[i] < 0.0)
104 <                        rgbcolor[i] = 0.0;
52 >        if (len < MINELEN | len > 0x7fff)       /* OOBs, write out flat */
53 >                return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len);
54 >                                        /* put magic header */
55 >        putc(2, fp);
56 >        putc(2, fp);
57 >        putc(len>>8, fp);
58 >        putc(len&255, fp);
59 >                                        /* put components seperately */
60 >        for (i = 0; i < 4; i++) {
61 >            for (j = 0; j < len; j += cnt) {    /* find next run */
62 >                for (beg = j; beg < len; beg += cnt) {
63 >                    for (cnt = 1; cnt < 127 && beg+cnt < len &&
64 >                            scanline[beg+cnt][i] == scanline[beg][i]; cnt++)
65 >                        ;
66 >                    if (cnt >= MINRUN)
67 >                        break;                  /* long enough */
68 >                }
69 >                if (beg-j > 1 && beg-j < MINRUN) {
70 >                    c2 = j+1;
71 >                    while (scanline[c2++][i] == scanline[j][i])
72 >                        if (c2 == beg) {        /* short run */
73 >                            putc(128+beg-j, fp);
74 >                            putc(scanline[j][i], fp);
75 >                            j = beg;
76 >                            break;
77 >                        }
78 >                }
79 >                while (j < beg) {               /* write out non-run */
80 >                    if ((c2 = beg-j) > 128) c2 = 128;
81 >                    putc(c2, fp);
82 >                    while (c2--)
83 >                        putc(scanline[j++][i], fp);
84 >                }
85 >                if (cnt >= MINRUN) {            /* write out run */
86 >                    putc(128+cnt, fp);
87 >                    putc(scanline[beg][i], fp);
88 >                } else
89 >                    cnt = 0;
90 >            }
91          }
92 +        return(ferror(fp) ? -1 : 0);
93   }
107 #endif
94  
95  
96 < fputresolu(ord, xres, yres, fp)         /* put x and y resolution */
111 < register int  ord;
112 < int  xres, yres;
113 < FILE  *fp;
114 < {
115 <        if (ord&YMAJOR)
116 <                fprintf(fp, "%cY %d %cX %d\n",
117 <                                ord&YDECR ? '-' : '+', yres,
118 <                                ord&XDECR ? '-' : '+', xres);
119 <        else
120 <                fprintf(fp, "%cX %d %cY %d\n",
121 <                                ord&XDECR ? '-' : '+', xres,
122 <                                ord&YDECR ? '-' : '+', yres);
123 < }
124 <
125 <
126 < fgetresolu(xrp, yrp, fp)                /* get x and y resolution */
127 < int  *xrp, *yrp;
128 < FILE  *fp;
129 < {
130 <        char  buf[64], *xndx, *yndx;
131 <        register char  *cp;
132 <        register int  ord;
133 <
134 <        if (fgets(buf, sizeof(buf), fp) == NULL)
135 <                return(-1);
136 <        xndx = yndx = NULL;
137 <        for (cp = buf+1; *cp; cp++)
138 <                if (*cp == 'X')
139 <                        xndx = cp;
140 <                else if (*cp == 'Y')
141 <                        yndx = cp;
142 <        if (xndx == NULL || yndx == NULL)
143 <                return(-1);
144 <        ord = 0;
145 <        if (xndx > yndx) ord |= YMAJOR;
146 <        if (xndx[-1] == '-') ord |= XDECR;
147 <        if (yndx[-1] == '-') ord |= YDECR;
148 <        if ((*xrp = atoi(xndx+1)) <= 0)
149 <                return(-1);
150 <        if ((*yrp = atoi(yndx+1)) <= 0)
151 <                return(-1);
152 <        return(ord);
153 < }
154 <
155 <
156 < fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
96 > freadcolrs(scanline, len, fp)           /* read in an encoded colr scanline */
97   register COLR  *scanline;
98   int  len;
99   register FILE  *fp;
100   {
101 <        COLR  lastcolr;
102 <        int  rept;
103 <        
104 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
105 <        lastcolr[EXP] = 0;
106 <        rept = 0;
107 <        
108 <        while (len > 0) {
109 <                if (scanline[0][EXP] == lastcolr[EXP] &&
110 <                                scanline[0][RED] == lastcolr[RED] &&
171 <                                scanline[0][GRN] == lastcolr[GRN] &&
172 <                                scanline[0][BLU] == lastcolr[BLU])
173 <                        rept++;
174 <                else {
175 <                        while (rept) {          /* write out count */
176 <                                putc(1, fp);
177 <                                putc(1, fp);
178 <                                putc(1, fp);
179 <                                putc(rept & 255, fp);
180 <                                rept >>= 8;
181 <                        }
182 <                        putc(scanline[0][RED], fp);     /* new color */
183 <                        putc(scanline[0][GRN], fp);
184 <                        putc(scanline[0][BLU], fp);
185 <                        putc(scanline[0][EXP], fp);
186 <                        copycolr(lastcolr, scanline[0]);
187 <                        rept = 0;
188 <                }
189 <                scanline++;
190 <                len--;
101 >        register int  i, j;
102 >        int  code;
103 >                                        /* determine scanline type */
104 >        if (len < MINELEN | len > 0x7fff)
105 >                return(oldreadcolrs(scanline, len, fp));
106 >        if ((i = getc(fp)) == EOF)
107 >                return(-1);
108 >        if (i != 2) {
109 >                ungetc(i, fp);
110 >                return(oldreadcolrs(scanline, len, fp));
111          }
112 <        while (rept) {          /* write out count */
113 <                putc(1, fp);
114 <                putc(1, fp);
115 <                putc(1, fp);
116 <                putc(rept & 255, fp);
117 <                rept >>= 8;
112 >        scanline[0][GRN] = getc(fp);
113 >        scanline[0][BLU] = getc(fp);
114 >        if ((i = getc(fp)) == EOF)
115 >                return(-1);
116 >        if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) {
117 >                scanline[0][RED] = 2;
118 >                scanline[0][EXP] = i;
119 >                return(oldreadcolrs(scanline+1, len-1, fp));
120          }
121 <        return(ferror(fp) ? -1 : 0);
121 >        if ((scanline[0][BLU]<<8 | i) != len)
122 >                return(-1);             /* length mismatch! */
123 >                                        /* read each component */
124 >        for (i = 0; i < 4; i++)
125 >            for (j = 0; j < len; ) {
126 >                if ((code = getc(fp)) == EOF)
127 >                    return(-1);
128 >                if (code > 128) {       /* run */
129 >                    scanline[j++][i] = getc(fp);
130 >                    for (code &= 127; --code; j++)
131 >                        scanline[j][i] = scanline[j-1][i];
132 >                } else                  /* non-run */
133 >                    while (code--)
134 >                        scanline[j++][i] = getc(fp);
135 >            }
136 >        return(feof(fp) ? -1 : 0);
137   }
138  
139  
140 < freadcolrs(scanline, len, fp)           /* read in a colr scanline */
140 > oldreadcolrs(scanline, len, fp)         /* read in an old colr scanline */
141   register COLR  *scanline;
142   int  len;
143   register FILE  *fp;
# Line 239 | Line 176 | register FILE  *fp;
176   fwritescan(scanline, len, fp)           /* write out a scanline */
177   register COLOR  *scanline;
178   int  len;
179 < register FILE  *fp;
179 > FILE  *fp;
180   {
181 <        COLR  lastcolr, thiscolr;
182 <        int  rept;
183 <        
184 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
185 <        lastcolr[EXP] = 0;
186 <        rept = 0;
187 <        
188 <        while (len > 0) {
189 <                setcolr(thiscolr, scanline[0][RED],
181 >        COLR  *clrscan;
182 >        int  n;
183 >        register COLR  *sp;
184 >                                        /* get scanline buffer */
185 >        if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
186 >                return(-1);
187 >        clrscan = sp;
188 >                                        /* convert scanline */
189 >        n = len;
190 >        while (n-- > 0) {
191 >                setcolr(sp[0], scanline[0][RED],
192                                    scanline[0][GRN],
193                                    scanline[0][BLU]);
255                if (thiscolr[EXP] == lastcolr[EXP] &&
256                                thiscolr[RED] == lastcolr[RED] &&
257                                thiscolr[GRN] == lastcolr[GRN] &&
258                                thiscolr[BLU] == lastcolr[BLU])
259                        rept++;
260                else {
261                        while (rept) {          /* write out count */
262                                putc(1, fp);
263                                putc(1, fp);
264                                putc(1, fp);
265                                putc(rept & 255, fp);
266                                rept >>= 8;
267                        }
268                        putc(thiscolr[RED], fp);        /* new color */
269                        putc(thiscolr[GRN], fp);
270                        putc(thiscolr[BLU], fp);
271                        putc(thiscolr[EXP], fp);
272                        copycolr(lastcolr, thiscolr);
273                        rept = 0;
274                }
194                  scanline++;
195 <                len--;
195 >                sp++;
196          }
197 <        while (rept) {          /* write out count */
279 <                putc(1, fp);
280 <                putc(1, fp);
281 <                putc(1, fp);
282 <                putc(rept & 255, fp);
283 <                rept >>= 8;
284 <        }
285 <        return(ferror(fp) ? -1 : 0);
197 >        return(fwritecolrs(clrscan, len, fp));
198   }
199  
200  
201   freadscan(scanline, len, fp)            /* read in a scanline */
202   register COLOR  *scanline;
203   int  len;
204 < register FILE  *fp;
204 > FILE  *fp;
205   {
206 <        COLR  thiscolr;
207 <        int  rshift;
208 <        register int  i;
209 <        
210 <        rshift = 0;
211 <        
212 <        while (len > 0) {
213 <                thiscolr[RED] = getc(fp);
214 <                thiscolr[GRN] = getc(fp);
215 <                thiscolr[BLU] = getc(fp);
216 <                thiscolr[EXP] = getc(fp);
217 <                if (feof(fp) || ferror(fp))
218 <                        return(-1);
219 <                if (thiscolr[RED] == 1 &&
220 <                                thiscolr[GRN] == 1 &&
221 <                                thiscolr[BLU] == 1) {
222 <                        for (i = thiscolr[EXP] << rshift; i > 0; i--) {
311 <                                copycolor(scanline[0], scanline[-1]);
312 <                                scanline++;
313 <                                len--;
314 <                        }
315 <                        rshift += 8;
316 <                } else {
317 <                        colr_color(scanline[0], thiscolr);
318 <                        scanline++;
319 <                        len--;
320 <                        rshift = 0;
321 <                }
206 >        register COLR  *clrscan;
207 >
208 >        if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
209 >                return(-1);
210 >        if (freadcolrs(clrscan, len, fp) < 0)
211 >                return(-1);
212 >                                        /* convert scanline */
213 >        colr_color(scanline[0], clrscan[0]);
214 >        while (--len > 0) {
215 >                scanline++; clrscan++;
216 >                if (clrscan[0][RED] == clrscan[-1][RED] &&
217 >                            clrscan[0][GRN] == clrscan[-1][GRN] &&
218 >                            clrscan[0][BLU] == clrscan[-1][BLU] &&
219 >                            clrscan[0][EXP] == clrscan[-1][EXP])
220 >                        copycolor(scanline[0], scanline[-1]);
221 >                else
222 >                        colr_color(scanline[0], clrscan[0]);
223          }
224          return(0);
225   }
# Line 328 | Line 229 | setcolr(clr, r, g, b)          /* assign a short color value *
229   register COLR  clr;
230   double  r, g, b;
231   {
331        double  frexp();
232          double  d;
233          int  e;
234          
# Line 363 | Line 263 | register COLR  clr;
263                  col[RED] = (clr[RED] + 0.5)*f;
264                  col[GRN] = (clr[GRN] + 0.5)*f;
265                  col[BLU] = (clr[BLU] + 0.5)*f;
366        }
367 }
368
369
370 normcolrs(scan, len)            /* normalize a scanline of colrs */
371 register COLR  *scan;
372 int  len;
373 {
374        register int  c;
375        register int  shift;
376
377        while (len-- > 0) {
378                shift = scan[0][EXP] - COLXS;
379                if (shift > 0) {
380                        if (shift >= 8) {
381                                scan[0][RED] =
382                                scan[0][GRN] =
383                                scan[0][BLU] = 255;
384                        } else {
385                                c = scan[0][RED] << shift;
386                                scan[0][RED] = c > 255 ? 255 : c;
387                                c = scan[0][GRN] << shift;
388                                scan[0][GRN] = c > 255 ? 255 : c;
389                                c = scan[0][BLU] << shift;
390                                scan[0][BLU] = c > 255 ? 255 : c;
391                        }
392                } else if (shift < 0) {
393                        if (shift <= -8) {
394                                scan[0][RED] =
395                                scan[0][GRN] =
396                                scan[0][BLU] = 0;
397                        } else {
398                                scan[0][RED] = scan[0][RED] >> -shift;
399                                scan[0][GRN] = scan[0][GRN] >> -shift;
400                                scan[0][BLU] = scan[0][BLU] >> -shift;
401                        }
402                }
403                scan[0][EXP] = COLXS;
404                scan++;
266          }
267   }
268  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines