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.13 by greg, Fri Oct 19 11:13:02 1990 UTC vs.
Revision 2.6 by greg, Tue Mar 2 09:09:51 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 14 | Line 14 | static char SCCSid[] = "$SunId$ LBL";
14  
15   #include  "color.h"
16  
17 + #define  MINELEN        8       /* minimum scanline length for encoding */
18 + #define  MAXELEN        0x7fff  /* maximum scanline length for encoding */
19 + #define  MINRUN         4       /* minimum run length */
20  
21 + #ifndef frexp
22 + extern double  frexp();
23 + #endif
24 +
25 +
26 + char *
27 + tempbuffer(len)                 /* get a temporary buffer */
28 + unsigned  len;
29 + {
30 +        extern char  *malloc(), *realloc();
31 +        static char  *tempbuf = NULL;
32 +        static unsigned  tempbuflen = 0;
33 +
34 +        if (len > tempbuflen) {
35 +                if (tempbuflen > 0)
36 +                        tempbuf = realloc(tempbuf, len);
37 +                else
38 +                        tempbuf = malloc(len);
39 +                tempbuflen = tempbuf==NULL ? 0 : len;
40 +        }
41 +        return(tempbuf);
42 + }
43 +
44 +
45   fwritecolrs(scanline, len, fp)          /* write out a colr scanline */
46   register COLR  *scanline;
47 < int  len;
47 > unsigned  len;
48   register FILE  *fp;
49   {
50 <        COLR  lastcolr;
51 <        int  rept;
50 >        register int  i, j, beg, cnt;
51 >        int  c2;
52          
53 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
54 <        lastcolr[EXP] = 0;
55 <        rept = 0;
56 <        
57 <        while (len > 0) {
58 <                if (scanline[0][EXP] == lastcolr[EXP] &&
59 <                                scanline[0][RED] == lastcolr[RED] &&
60 <                                scanline[0][GRN] == lastcolr[GRN] &&
61 <                                scanline[0][BLU] == lastcolr[BLU])
62 <                        rept++;
63 <                else {
64 <                        while (rept) {          /* write out count */
65 <                                putc(1, fp);
66 <                                putc(1, fp);
67 <                                putc(1, fp);
68 <                                putc(rept & 255, fp);
69 <                                rept >>= 8;
53 >        if (len < MINELEN | len > MAXELEN)      /* OOBs, write out flat */
54 >                return(fwrite((char *)scanline,sizeof(COLR),len,fp) - len);
55 >                                        /* put magic header */
56 >        putc(2, fp);
57 >        putc(2, fp);
58 >        putc(len>>8, fp);
59 >        putc(len&255, fp);
60 >                                        /* put components seperately */
61 >        for (i = 0; i < 4; i++) {
62 >            for (j = 0; j < len; j += cnt) {    /* find next run */
63 >                for (beg = j; beg < len; beg += cnt) {
64 >                    for (cnt = 1; cnt < 127 && beg+cnt < len &&
65 >                            scanline[beg+cnt][i] == scanline[beg][i]; cnt++)
66 >                        ;
67 >                    if (cnt >= MINRUN)
68 >                        break;                  /* long enough */
69 >                }
70 >                if (beg-j > 1 && beg-j < MINRUN) {
71 >                    c2 = j+1;
72 >                    while (scanline[c2++][i] == scanline[j][i])
73 >                        if (c2 == beg) {        /* short run */
74 >                            putc(128+beg-j, fp);
75 >                            putc(scanline[j][i], fp);
76 >                            j = beg;
77 >                            break;
78                          }
44                        putc(scanline[0][RED], fp);     /* new color */
45                        putc(scanline[0][GRN], fp);
46                        putc(scanline[0][BLU], fp);
47                        putc(scanline[0][EXP], fp);
48                        copycolr(lastcolr, scanline[0]);
49                        rept = 0;
79                  }
80 <                scanline++;
81 <                len--;
80 >                while (j < beg) {               /* write out non-run */
81 >                    if ((c2 = beg-j) > 128) c2 = 128;
82 >                    putc(c2, fp);
83 >                    while (c2--)
84 >                        putc(scanline[j++][i], fp);
85 >                }
86 >                if (cnt >= MINRUN) {            /* write out run */
87 >                    putc(128+cnt, fp);
88 >                    putc(scanline[beg][i], fp);
89 >                } else
90 >                    cnt = 0;
91 >            }
92          }
54        while (rept) {          /* write out count */
55                putc(1, fp);
56                putc(1, fp);
57                putc(1, fp);
58                putc(rept & 255, fp);
59                rept >>= 8;
60        }
93          return(ferror(fp) ? -1 : 0);
94   }
95  
96  
97 < freadcolrs(scanline, len, fp)           /* read in a colr scanline */
97 > freadcolrs(scanline, len, fp)           /* read in an encoded colr scanline */
98   register COLR  *scanline;
99   int  len;
100   register FILE  *fp;
101   {
102 +        register int  i, j;
103 +        int  code, val;
104 +                                        /* determine scanline type */
105 +        if (len < MINELEN | len > MAXELEN)
106 +                return(oldreadcolrs(scanline, len, fp));
107 +        if ((i = getc(fp)) == EOF)
108 +                return(-1);
109 +        if (i != 2) {
110 +                ungetc(i, fp);
111 +                return(oldreadcolrs(scanline, len, fp));
112 +        }
113 +        scanline[0][GRN] = getc(fp);
114 +        scanline[0][BLU] = getc(fp);
115 +        if ((i = getc(fp)) == EOF)
116 +                return(-1);
117 +        if (scanline[0][GRN] != 2 || scanline[0][BLU] & 128) {
118 +                scanline[0][RED] = 2;
119 +                scanline[0][EXP] = i;
120 +                return(oldreadcolrs(scanline+1, len-1, fp));
121 +        }
122 +        if ((scanline[0][BLU]<<8 | i) != len)
123 +                return(-1);             /* length mismatch! */
124 +                                        /* read each component */
125 +        for (i = 0; i < 4; i++)
126 +            for (j = 0; j < len; ) {
127 +                if ((code = getc(fp)) == EOF)
128 +                    return(-1);
129 +                if (code > 128) {       /* run */
130 +                    code &= 127;
131 +                    val = getc(fp);
132 +                    while (code--)
133 +                        scanline[j++][i] = val;
134 +                } else                  /* non-run */
135 +                    while (code--)
136 +                        scanline[j++][i] = getc(fp);
137 +            }
138 +        return(feof(fp) ? -1 : 0);
139 + }
140 +
141 +
142 + oldreadcolrs(scanline, len, fp)         /* read in an old colr scanline */
143 + register COLR  *scanline;
144 + int  len;
145 + register FILE  *fp;
146 + {
147          int  rshift;
148          register int  i;
149          
# Line 101 | Line 178 | register FILE  *fp;
178   fwritescan(scanline, len, fp)           /* write out a scanline */
179   register COLOR  *scanline;
180   int  len;
181 < register FILE  *fp;
181 > FILE  *fp;
182   {
183 <        COLR  lastcolr, thiscolr;
184 <        int  rept;
185 <        
186 <        lastcolr[RED] = lastcolr[GRN] = lastcolr[BLU] = 1;
187 <        lastcolr[EXP] = 0;
188 <        rept = 0;
189 <        
190 <        while (len > 0) {
191 <                setcolr(thiscolr, scanline[0][RED],
183 >        COLR  *clrscan;
184 >        int  n;
185 >        register COLR  *sp;
186 >                                        /* get scanline buffer */
187 >        if ((sp = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
188 >                return(-1);
189 >        clrscan = sp;
190 >                                        /* convert scanline */
191 >        n = len;
192 >        while (n-- > 0) {
193 >                setcolr(sp[0], scanline[0][RED],
194                                    scanline[0][GRN],
195                                    scanline[0][BLU]);
117                if (thiscolr[EXP] == lastcolr[EXP] &&
118                                thiscolr[RED] == lastcolr[RED] &&
119                                thiscolr[GRN] == lastcolr[GRN] &&
120                                thiscolr[BLU] == lastcolr[BLU])
121                        rept++;
122                else {
123                        while (rept) {          /* write out count */
124                                putc(1, fp);
125                                putc(1, fp);
126                                putc(1, fp);
127                                putc(rept & 255, fp);
128                                rept >>= 8;
129                        }
130                        putc(thiscolr[RED], fp);        /* new color */
131                        putc(thiscolr[GRN], fp);
132                        putc(thiscolr[BLU], fp);
133                        putc(thiscolr[EXP], fp);
134                        copycolr(lastcolr, thiscolr);
135                        rept = 0;
136                }
196                  scanline++;
197 <                len--;
197 >                sp++;
198          }
199 <        while (rept) {          /* write out count */
141 <                putc(1, fp);
142 <                putc(1, fp);
143 <                putc(1, fp);
144 <                putc(rept & 255, fp);
145 <                rept >>= 8;
146 <        }
147 <        return(ferror(fp) ? -1 : 0);
199 >        return(fwritecolrs(clrscan, len, fp));
200   }
201  
202  
203   freadscan(scanline, len, fp)            /* read in a scanline */
204   register COLOR  *scanline;
205   int  len;
206 < register FILE  *fp;
206 > FILE  *fp;
207   {
208 <        COLR  thiscolr;
209 <        int  rshift;
210 <        register int  i;
211 <        
212 <        rshift = 0;
213 <        
214 <        while (len > 0) {
215 <                thiscolr[RED] = getc(fp);
216 <                thiscolr[GRN] = getc(fp);
217 <                thiscolr[BLU] = getc(fp);
218 <                thiscolr[EXP] = getc(fp);
219 <                if (feof(fp) || ferror(fp))
220 <                        return(-1);
221 <                if (thiscolr[RED] == 1 &&
222 <                                thiscolr[GRN] == 1 &&
223 <                                thiscolr[BLU] == 1) {
224 <                        for (i = thiscolr[EXP] << rshift; i > 0; i--) {
173 <                                copycolor(scanline[0], scanline[-1]);
174 <                                scanline++;
175 <                                len--;
176 <                        }
177 <                        rshift += 8;
178 <                } else {
179 <                        colr_color(scanline[0], thiscolr);
180 <                        scanline++;
181 <                        len--;
182 <                        rshift = 0;
183 <                }
208 >        register COLR  *clrscan;
209 >
210 >        if ((clrscan = (COLR *)tempbuffer(len*sizeof(COLR))) == NULL)
211 >                return(-1);
212 >        if (freadcolrs(clrscan, len, fp) < 0)
213 >                return(-1);
214 >                                        /* convert scanline */
215 >        colr_color(scanline[0], clrscan[0]);
216 >        while (--len > 0) {
217 >                scanline++; clrscan++;
218 >                if (clrscan[0][RED] == clrscan[-1][RED] &&
219 >                            clrscan[0][GRN] == clrscan[-1][GRN] &&
220 >                            clrscan[0][BLU] == clrscan[-1][BLU] &&
221 >                            clrscan[0][EXP] == clrscan[-1][EXP])
222 >                        copycolor(scanline[0], scanline[-1]);
223 >                else
224 >                        colr_color(scanline[0], clrscan[0]);
225          }
226          return(0);
227   }
# Line 190 | Line 231 | setcolr(clr, r, g, b)          /* assign a short color value *
231   register COLR  clr;
232   double  r, g, b;
233   {
193        double  frexp();
234          double  d;
235          int  e;
236          

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines