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.1 by greg, Tue Nov 12 16:55:34 1991 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  
24 #define  STARTWL        380             /* starting wavelength (nanometers) */
25 #define  INCWL          10              /* wavelength increment */
26 #define  NINC           40              /* # of values */
20  
21 < static BYTE  chroma[3][NINC] = {
22 <        {                                                       /* X */
23 <                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 < };
46 <
47 <
48 < spec_rgb(col, s, e)             /* comput RGB color from spectral range */
49 < COLOR  col;
50 < int  s, e;
21 > char *
22 > tempbuffer(len)                 /* get a temporary buffer */
23 > unsigned  len;
24   {
25 <        COLOR  ciecolor;
25 >        extern char  *malloc(), *realloc();
26 >        static char  *tempbuf = NULL;
27 >        static int  tempbuflen = 0;
28  
29 <        spec_cie(ciecolor, s, e);
30 <        cie_rgb(col, ciecolor);
29 >        if (len > tempbuflen) {
30 >                if (tempbuflen > 0)
31 >                        tempbuf = realloc(tempbuf, len);
32 >                else
33 >                        tempbuf = malloc(len);
34 >                tempbuflen = tempbuf==NULL ? 0 : len;
35 >        }
36 >        return(tempbuf);
37   }
38  
39  
40 < spec_cie(col, s, e)             /* compute a color from a spectral range */
41 < COLOR  col;             /* returned color */
42 < int  s, e;              /* starting and ending wavelengths */
40 > fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
41 > register COLR  *scanline;
42 > int  len;
43 > register FILE  *fp;
44   {
45 <        register int  i, d, r;
45 >        register int  i, j, beg, cnt;
46 >        int  c2;
47          
48 <        s -= STARTWL;
49 <        if (s < 0)
50 <                s = 0;
51 <
52 <        e -= STARTWL;
53 <        if (e >= INCWL*(NINC - 1))
54 <                e = INCWL*(NINC - 1) - 1;
55 <
56 <        d = e / INCWL;                  /* interpolate values */
57 <        r = e % INCWL;
58 <        for (i = 0; i < 3; i++)
59 <                col[i] = chroma[i][d]*(INCWL - r) + chroma[i][d + 1]*r;
60 <
61 <        d = s / INCWL;
62 <        r = s % INCWL;
63 <        for (i = 0; i < 3; i++)
64 <                col[i] -= chroma[i][d]*(INCWL - r) - chroma[i][d + 1]*r;
65 <
66 <        col[RED] = (col[RED] + 0.5) / (256*INCWL);
67 <        col[GRN] = (col[GRN] + 0.5) / (256*INCWL);
68 <        col[BLU] = (col[BLU] + 0.5) / (256*INCWL);
69 < }
70 <
71 <
72 < cie_rgb(rgbcolor, ciecolor)             /* convert CIE to RGB (NTSC) */
73 < register COLOR  rgbcolor, ciecolor;
74 < {
75 <        static float  cmat[3][3] = {
76 <                1.73, -.48, -.26,
77 <                -.81, 1.65, -.02,
78 <                 .08, -.17, 1.28,
79 <        };
80 <        register int  i;
81 <
82 <        for (i = 0; i < 3; i++) {
83 <                rgbcolor[i] =   cmat[i][0]*ciecolor[0] +
84 <                                cmat[i][1]*ciecolor[1] +
85 <                                cmat[i][2]*ciecolor[2] ;
86 <                if (rgbcolor[i] < 0.0)
87 <                        rgbcolor[i] = 0.0;
48 >        if (len < MINELEN)              /* too small to encode */
49 >                return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len);
50 >        if (len > 32767)                /* too big! */
51 >                return(-1);
52 >        putc(2, fp);                    /* put magic header */
53 >        putc(2, fp);
54 >        putc(len>>8, fp);
55 >        putc(len&255, fp);
56 >                                        /* put components seperately */
57 >        for (i = 0; i < 4; i++) {
58 >            for (j = 0; j < len; j += cnt) {    /* find next run */
59 >                for (beg = j; beg < len; beg += cnt) {
60 >                    for (cnt = 1; cnt < 127 && beg+cnt < len &&
61 >                            scanline[beg+cnt][i] == scanline[beg][i]; cnt++)
62 >                        ;
63 >                    if (cnt >= MINRUN)
64 >                        break;                  /* long enough */
65 >                }
66 >                if (beg-j > 1 && beg-j < MINRUN) {
67 >                    c2 = j+1;
68 >                    while (scanline[c2++][i] == scanline[j][i])
69 >                        if (c2 == beg) {        /* short run */
70 >                            putc(128+beg-j, fp);
71 >                            putc(scanline[j][i], fp);
72 >                            j = beg;
73 >                            break;
74 >                        }
75 >                }
76 >                while (j < beg) {               /* write out non-run */
77 >                    if ((c2 = beg-j) > 128) c2 = 128;
78 >                    putc(c2, fp);
79 >                    while (c2--)
80 >                        putc(scanline[j++][i], fp);
81 >                }
82 >                if (cnt >= MINRUN) {            /* write out run */
83 >                    putc(128+cnt, fp);
84 >                    putc(scanline[beg][i], fp);
85 >                } else
86 >                    cnt = 0;
87 >            }
88          }
89 +        return(ferror(fp) ? -1 : 0);
90   }
107 #endif
91  
92  
93 < 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 */
93 > freadcolrs(scanline, len, fp)           /* read in an encoded colr scanline */
94   register COLR  *scanline;
95   int  len;
96   register FILE  *fp;
97   {
98 <        COLR  lastcolr;
99 <        int  rept;
100 <        
101 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
102 <        lastcolr[EXP] = 0;
103 <        rept = 0;
104 <        
105 <        while (len > 0) {
106 <                if (scanline[0][EXP] == lastcolr[EXP] &&
107 <                                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--;
98 >        register int  i, j;
99 >        int  code;
100 >                                        /* determine scanline type */
101 >        if (len < MINELEN)
102 >                return(oldreadcolrs(scanline, len, fp));
103 >        if ((i = getc(fp)) == EOF)
104 >                return(-1);
105 >        if (i != 2) {
106 >                ungetc(i, fp);
107 >                return(oldreadcolrs(scanline, len, fp));
108          }
109 <        while (rept) {          /* write out count */
110 <                putc(1, fp);
111 <                putc(1, fp);
112 <                putc(1, fp);
113 <                putc(rept & 255, fp);
114 <                rept >>= 8;
109 >        scanline[0][GRN] = getc(fp);
110 >        scanline[0][BLU] = getc(fp);
111 >        if ((i = getc(fp)) == EOF)
112 >                return(-1);
113 >        if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) {
114 >                scanline[0][RED] = 2;
115 >                scanline[0][EXP] = i;
116 >                return(oldreadcolrs(scanline+1, len-1, fp));
117          }
118 <        return(ferror(fp) ? -1 : 0);
118 >        if ((scanline[0][BLU]<<8 | i) != len)
119 >                return(-1);             /* length mismatch! */
120 >                                        /* read each component */
121 >        for (i = 0; i < 4; i++)
122 >            for (j = 0; j < len; ) {
123 >                if ((code = getc(fp)) == EOF)
124 >                    return(-1);
125 >                if (code > 128) {       /* run */
126 >                    scanline[j++][i] = getc(fp);
127 >                    for (code &= 127; --code; j++)
128 >                        scanline[j][i] = scanline[j-1][i];
129 >                } else                  /* non-run */
130 >                    while (code--)
131 >                        scanline[j++][i] = getc(fp);
132 >            }
133 >        return(feof(fp) ? -1 : 0);
134   }
135  
136  
137 < freadcolrs(scanline, len, fp)           /* read in a colr scanline */
137 > oldreadcolrs(scanline, len, fp)         /* read in an old colr scanline */
138   register COLR  *scanline;
139   int  len;
140   register FILE  *fp;
# Line 239 | Line 173 | register FILE  *fp;
173   fwritescan(scanline, len, fp)           /* write out a scanline */
174   register COLOR  *scanline;
175   int  len;
176 < register FILE  *fp;
176 > FILE  *fp;
177   {
178 <        COLR  lastcolr, thiscolr;
179 <        int  rept;
180 <        
181 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
182 <        lastcolr[EXP] = 0;
183 <        rept = 0;
184 <        
185 <        while (len > 0) {
186 <                setcolr(thiscolr, scanline[0][RED],
178 >        COLR  *clrscan;
179 >        int  n;
180 >        register COLR  *sp;
181 >                                        /* get scanline buffer */
182 >        if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
183 >                return(-1);
184 >        clrscan = sp;
185 >                                        /* convert scanline */
186 >        n = len;
187 >        while (n-- > 0) {
188 >                setcolr(sp[0], scanline[0][RED],
189                                    scanline[0][GRN],
190                                    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                }
191                  scanline++;
192 <                len--;
192 >                sp++;
193          }
194 <        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);
194 >        return(fwritecolrs(clrscan, len, fp));
195   }
196  
197  
198   freadscan(scanline, len, fp)            /* read in a scanline */
199   register COLOR  *scanline;
200   int  len;
201 < register FILE  *fp;
201 > FILE  *fp;
202   {
203 <        COLR  thiscolr;
204 <        int  rshift;
205 <        register int  i;
206 <        
207 <        rshift = 0;
208 <        
209 <        while (len > 0) {
210 <                thiscolr[RED] = getc(fp);
211 <                thiscolr[GRN] = getc(fp);
212 <                thiscolr[BLU] = getc(fp);
213 <                thiscolr[EXP] = getc(fp);
214 <                if (feof(fp) || ferror(fp))
215 <                        return(-1);
216 <                if (thiscolr[RED] == 1 &&
217 <                                thiscolr[GRN] == 1 &&
218 <                                thiscolr[BLU] == 1) {
219 <                        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 <                }
203 >        register COLR  *clrscan;
204 >
205 >        if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
206 >                return(-1);
207 >        if (freadcolrs(clrscan, len, fp) < 0)
208 >                return(-1);
209 >                                        /* convert scanline */
210 >        colr_color(scanline[0], clrscan[0]);
211 >        while (--len > 0) {
212 >                scanline++; clrscan++;
213 >                if (clrscan[0][RED] == clrscan[-1][RED] &&
214 >                            clrscan[0][GRN] == clrscan[-1][GRN] &&
215 >                            clrscan[0][BLU] == clrscan[-1][BLU] &&
216 >                            clrscan[0][EXP] == clrscan[-1][EXP])
217 >                        copycolor(scanline[0], scanline[-1]);
218 >                else
219 >                        colr_color(scanline[0], clrscan[0]);
220          }
221          return(0);
222   }
# Line 363 | Line 261 | register COLR  clr;
261                  col[RED] = (clr[RED] + 0.5)*f;
262                  col[GRN] = (clr[GRN] + 0.5)*f;
263                  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++;
264          }
265   }
266  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines