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.9 by greg, Mon Nov 6 15:10:24 1989 UTC vs.
Revision 2.7 by greg, Fri Jun 4 14:51:39 1993 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 12 | Line 12 | static char SCCSid[] = "$SunId$ LBL";
12  
13   #include  <stdio.h>
14  
15 + #include  <math.h>
16 +
17   #include  "color.h"
18  
19 < #ifdef  SPEC_RGB
20 < /*
21 < *      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 < */
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  
24 #define  STARTWL        380             /* starting wavelength (nanometers) */
25 #define  INCWL          10              /* wavelength increment */
26 #define  NINC           40              /* # of values */
23  
24 < static BYTE  chroma[3][NINC] = {
25 <        {                                                       /* X */
26 <                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;
24 > char *
25 > tempbuffer(len)                 /* get a temporary buffer */
26 > unsigned  len;
27   {
28 <        COLOR  ciecolor;
28 >        extern char  *malloc(), *realloc();
29 >        static char  *tempbuf = NULL;
30 >        static unsigned  tempbuflen = 0;
31  
32 <        spec_cie(ciecolor, s, e);
33 <        cie_rgb(col, ciecolor);
32 >        if (len > tempbuflen) {
33 >                if (tempbuflen > 0)
34 >                        tempbuf = realloc(tempbuf, len);
35 >                else
36 >                        tempbuf = malloc(len);
37 >                tempbuflen = tempbuf==NULL ? 0 : len;
38 >        }
39 >        return(tempbuf);
40   }
41  
42  
43 < spec_cie(col, s, e)             /* compute a color from a spectral range */
44 < COLOR  col;             /* returned color */
45 < int  s, e;              /* starting and ending wavelengths */
43 > fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
44 > register COLR  *scanline;
45 > unsigned  len;
46 > register FILE  *fp;
47   {
48 <        register int  i, d, r;
48 >        register int  i, j, beg, cnt;
49 >        int  c2;
50          
51 <        s -= STARTWL;
52 <        if (s < 0)
53 <                s = 0;
54 <
55 <        e -= STARTWL;
56 <        if (e >= INCWL*(NINC - 1))
57 <                e = INCWL*(NINC - 1) - 1;
58 <
59 <        d = e / INCWL;                  /* interpolate values */
60 <        r = e % INCWL;
61 <        for (i = 0; i < 3; i++)
62 <                col[i] = chroma[i][d]*(INCWL - r) + chroma[i][d + 1]*r;
63 <
64 <        d = s / INCWL;
65 <        r = s % INCWL;
66 <        for (i = 0; i < 3; i++)
67 <                col[i] -= chroma[i][d]*(INCWL - r) - chroma[i][d + 1]*r;
68 <
69 <        col[RED] = (col[RED] + 0.5) / (256*INCWL);
70 <        col[GRN] = (col[GRN] + 0.5) / (256*INCWL);
71 <        col[BLU] = (col[BLU] + 0.5) / (256*INCWL);
72 < }
73 <
74 <
75 < cie_rgb(rgbcolor, ciecolor)             /* convert CIE to RGB (NTSC) */
76 < register COLOR  rgbcolor, ciecolor;
77 < {
78 <        static float  cmat[3][3] = {
79 <                1.73, -.48, -.26,
80 <                -.81, 1.65, -.02,
81 <                 .08, -.17, 1.28,
82 <        };
83 <        register int  i;
84 <
85 <        for (i = 0; i < 3; i++) {
86 <                rgbcolor[i] =   cmat[i][0]*ciecolor[0] +
87 <                                cmat[i][1]*ciecolor[1] +
88 <                                cmat[i][2]*ciecolor[2] ;
89 <                if (rgbcolor[i] < 0.0)
104 <                        rgbcolor[i] = 0.0;
51 >        if (len < MINELEN | len > MAXELEN)      /* OOBs, write out flat */
52 >                return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len);
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 */
59 >        for (i = 0; i < 4; i++) {
60 >            for (j = 0; j < len; j += cnt) {    /* find next run */
61 >                for (beg = j; beg < len; beg += cnt) {
62 >                    for (cnt = 1; cnt < 127 && beg+cnt < len &&
63 >                            scanline[beg+cnt][i] == scanline[beg][i]; cnt++)
64 >                        ;
65 >                    if (cnt >= MINRUN)
66 >                        break;                  /* long enough */
67 >                }
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--)
82 >                        putc(scanline[j++][i], fp);
83 >                }
84 >                if (cnt >= MINRUN) {            /* write out run */
85 >                    putc(128+cnt, fp);
86 >                    putc(scanline[beg][i], fp);
87 >                } else
88 >                    cnt = 0;
89 >            }
90          }
91 +        return(ferror(fp) ? -1 : 0);
92   }
107 #endif
93  
94  
95 < 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 */
95 > freadcolrs(scanline, len, fp)           /* read in an encoded colr scanline */
96   register COLR  *scanline;
97   int  len;
98   register FILE  *fp;
99   {
100 <        COLR  lastcolr;
101 <        int  rept;
102 <        
103 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
104 <        lastcolr[EXP] = 0;
105 <        rept = 0;
106 <        
107 <        while (len > 0) {
108 <                if (scanline[0][EXP] == lastcolr[EXP] &&
109 <                                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--;
100 >        register int  i, j;
101 >        int  code, val;
102 >                                        /* determine scanline type */
103 >        if (len < MINELEN | len > MAXELEN)
104 >                return(oldreadcolrs(scanline, len, fp));
105 >        if ((i = getc(fp)) == EOF)
106 >                return(-1);
107 >        if (i != 2) {
108 >                ungetc(i, fp);
109 >                return(oldreadcolrs(scanline, len, fp));
110          }
111 <        while (rept) {          /* write out count */
112 <                putc(1, fp);
113 <                putc(1, fp);
114 <                putc(1, fp);
115 <                putc(rept & 255, fp);
116 <                rept >>= 8;
111 >        scanline[0][GRN] = getc(fp);
112 >        scanline[0][BLU] = getc(fp);
113 >        if ((i = getc(fp)) == EOF)
114 >                return(-1);
115 >        if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) {
116 >                scanline[0][RED] = 2;
117 >                scanline[0][EXP] = i;
118 >                return(oldreadcolrs(scanline+1, len-1, fp));
119          }
120 <        return(ferror(fp) ? -1 : 0);
120 >        if ((scanline[0][BLU]<<8 | i) != len)
121 >                return(-1);             /* length mismatch! */
122 >                                        /* read each component */
123 >        for (i = 0; i < 4; i++)
124 >            for (j = 0; j < len; ) {
125 >                if ((code = getc(fp)) == EOF)
126 >                    return(-1);
127 >                if (code > 128) {       /* run */
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);
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                                shift--;
386                                c = (scan[0][RED]<<1 | 1) << shift;
387                                scan[0][RED] = c > 255 ? 255 : c;
388                                c = (scan[0][GRN]<<1 | 1) << shift;
389                                scan[0][GRN] = c > 255 ? 255 : c;
390                                c = (scan[0][BLU]<<1 | 1) << shift;
391                                scan[0][BLU] = c > 255 ? 255 : c;
392                        }
393                } else if (shift < 0) {
394                        if (shift < -8) {
395                                scan[0][RED] =
396                                scan[0][GRN] =
397                                scan[0][BLU] = 0;
398                        } else {
399                                shift = -1-shift;
400                                scan[0][RED] = ((scan[0][RED]>>shift)+1)>>1;
401                                scan[0][GRN] = ((scan[0][GRN]>>shift)+1)>>1;
402                                scan[0][BLU] = ((scan[0][BLU]>>shift)+1)>>1;
403                        }
404                }
405                scan[0][EXP] = COLXS;
406                scan++;
266          }
267   }
268  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines