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

Comparing ray/src/px/ra_t16.c (file contents):
Revision 1.2 by greg, Wed May 31 17:27:18 1989 UTC vs.
Revision 2.5 by greg, Sun Feb 27 10:17:20 1994 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 13 | Line 13 | static char SCCSid[] = "$SunId$ LBL";
13  
14   #include  <stdio.h>
15  
16 + #ifdef MSDOS
17 + #include  <fcntl.h>
18 + #endif
19 +
20 + #include  <math.h>
21 +
22   #include  "color.h"
23  
24 + #include  "resolu.h"
25 +
26 + #include  "random.h"
27 +
28   #include  "targa.h"
29  
30   #define  goodpic(h)     (((h)->dataType==IM_RGB || (h)->dataType==IM_CRGB) \
# Line 30 | Line 40 | static char SCCSid[] = "$SunId$ LBL";
40  
41   extern char     *ecalloc(), *emalloc();
42  
43 < extern double  atof(), pow();
43 > double  gamcor = 2.2;                   /* gamma correction */
44  
45 < double  gamma = 2.0;                    /* gamma correction */
45 > int  bradj = 0;                         /* brightness adjustment */
46  
47   char  *progname;
48  
# Line 46 | Line 56 | char  *argv[];
56          struct hdStruct  head;
57          int  reverse = 0;
58          int  i;
59 <        
59 > #ifdef MSDOS
60 >        extern int  _fmode;
61 >        _fmode = O_BINARY;
62 >        setmode(fileno(stdin), O_BINARY);
63 >        setmode(fileno(stdout), O_BINARY);
64 > #endif
65          progname = argv[0];
66  
67          head.dataBits = 16;
# Line 54 | Line 69 | char  *argv[];
69                  if (argv[i][0] == '-')
70                          switch (argv[i][1]) {
71                          case 'g':
72 <                                gamma = atof(argv[++i]);
72 >                                gamcor = atof(argv[++i]);
73                                  break;
74                          case 'r':
75                                  reverse = !reverse;
# Line 65 | Line 80 | char  *argv[];
80                          case '3':
81                                  head.dataBits = 24;
82                                  break;
83 +                        case 'e':
84 +                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
85 +                                        goto userr;
86 +                                bradj = atoi(argv[++i]);
87 +                                break;
88                          default:
89                                  goto userr;
90                          }
# Line 83 | Line 103 | char  *argv[];
103                  sprintf(msg, "can't open output \"%s\"", argv[i+1]);
104                  quiterr(msg);
105          }
106 +                                        /* set gamma */
107 +        setcolrgam(gamcor);
108                                          /* convert */
109          if (reverse) {
110                                          /* get header */
# Line 91 | Line 113 | char  *argv[];
113                  if (!goodpic(&head))
114                          quiterr("incompatible format");
115                                          /* put header */
116 <                printargs(argc, argv, stdout);
116 >                newheader("RADIANCE", stdout);
117 >                printargs(i, argv, stdout);
118 >                fputformat(COLRFMT, stdout);
119                  putchar('\n');
120 <                printf("-Y %d +X %d\n", head.y, head.x);
120 >                fprtresolu(head.x, head.y, stdout);
121                                          /* convert file */
122                  tg2ra(&head);
123          } else {
124 <                getheader(stdin, NULL);
125 <                if (scanf("-Y %d +X %d\n", &head.y, &head.x) != 2)
124 >                if (checkheader(stdin, COLRFMT, NULL) < 0 ||
125 >                                fgetresolu(&head.x, &head.y, stdin) < 0)
126                          quiterr("bad picture file");
127                                          /* assign header */
128                  head.textSize = 0;
# Line 114 | Line 138 | char  *argv[];
138          }
139          exit(0);
140   userr:
141 <        fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma] [input [output]]\n",
141 >        fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma][-e +/-stops] [input [output]]\n",
142                          progname);
143          exit(1);
144   }
# Line 187 | Line 211 | register FILE  *fp;
211  
212          if (ip != NULL)
213                  if (nidbytes)
214 <                        fread(ip, nidbytes, 1, fp);
214 >                        fread((char *)ip, nidbytes, 1, fp);
215                  else
216                          *ip = '\0';
217          else if (nidbytes)
# Line 228 | Line 252 | register FILE  *fp;
252   tg2ra(hp)                       /* targa file to RADIANCE file */
253   struct hdStruct  *hp;
254   {
255 <        float  gmap[256];
232 <        COLOR  *scanline;
255 >        COLR  *scanline;
256          unsigned char  *tarData;
257          register int  i, j;
235                                        /* set up gamma correction */
236        for (i = 0; i < 256; i++)
237                gmap[i] = pow((i+.5)/256., gamma);
258                                          /* skip color table */
259          if (hp->mapType == CM_HASMAP)
260                  fseek(stdin, (long)hp->mapLength*hp->CMapBits/8, 1);
# Line 243 | Line 263 | struct hdStruct  *hp;
263                                          /* get data */
264          readtarga(hp, tarData, stdin);
265                                          /* allocate input scanline */
266 <        scanline = (COLOR *)emalloc(hp->x*sizeof(COLOR));
266 >        scanline = (COLR *)emalloc(hp->x*sizeof(COLR));
267                                          /* convert file */
268          for (i = hp->y-1; i >= 0; i--) {
269                  if (hp->dataBits == 16) {
270                          register unsigned short  *dp;
271                          dp = (unsigned short *)tarData + i*hp->x;
272                          for (j = 0; j < hp->x; j++) {
273 <                                setcolor(scanline[j], gmap[*dp>>7 & 0xf8],
274 <                                                gmap[*dp>>2 & 0xf8],
275 <                                                gmap[*dp<<3 & 0xf8]);
273 >                                scanline[j][RED] = *dp>>7 & 0xf8;
274 >                                scanline[j][GRN] = *dp>>2 & 0xf8;
275 >                                scanline[j][BLU] = *dp<<3 & 0xf8;
276                                  dp++;
277                          }
278                  } else {        /* hp->dataBits == 24 */
279                          register unsigned char  *dp;
280                          dp = (unsigned char *)tarData + i*3*hp->x;
281                          for (j = 0; j < hp->x; j++) {
282 <                                setcolor(scanline[j], gmap[dp[2]],
283 <                                                gmap[dp[1]],
284 <                                                gmap[dp[0]]);
282 >                                scanline[j][RED] = dp[2];
283 >                                scanline[j][GRN] = dp[1];
284 >                                scanline[j][BLU] = dp[0];
285                                  dp += 3;
286                          }
287                  }
288 <                if (fwritescan(scanline, hp->x, stdout) < 0)
288 >                gambs_colrs(scanline, hp->x);
289 >                if (bradj)
290 >                        shiftcolrs(scanline, hp->x, bradj);
291 >                if (fwritecolrs(scanline, hp->x, stdout) < 0)
292                          quiterr("error writing RADIANCE file");
293          }
294          free((char *)scanline);
# Line 276 | Line 299 | struct hdStruct  *hp;
299   ra2tg(hp)                       /* convert radiance to targa file */
300   struct hdStruct  *hp;
301   {
302 <        unsigned char   gmap[1024];
280 <        register int    i, j, c;
302 >        register int    i, j;
303          unsigned char  *tarData;
304 <        COLOR   *inline;
283 <                                        /* set up gamma correction */
284 <        for (i = 0; i < 1024; i++)
285 <                gmap[i] = 256.*pow((i+.5)/1024., 1./gamma);
304 >        COLR    *inl;
305                                          /* allocate space for data */
306 <        inline = (COLOR *)emalloc(hp->x*sizeof(COLOR));
306 >        inl = (COLR *)emalloc(hp->x*sizeof(COLR));
307          tarData = taralloc(hp);
308                                          /* convert file */
309          for (j = hp->y-1; j >= 0; j--) {
310 <                if (freadscan(inline, hp->x, stdin) < 0)
310 >                if (freadcolrs(inl, hp->x, stdin) < 0)
311                          quiterr("error reading RADIANCE file");
312 +                if (bradj)
313 +                        shiftcolrs(inl, hp->x, bradj);
314 +                colrs_gambs(inl, hp->x);
315                  if (hp->dataBits == 16) {
316                          register unsigned short  *dp;
317 +                        register int  v;
318                          dp = (unsigned short *)tarData + j*hp->x;
319                          for (i = 0; i < hp->x; i++) {
320 <                                c = 1024.*colval(inline[i],RED);
321 <                                if (c > 1023) c = 1023;
322 <                                *dp = (gmap[c] & 0xf8)<<7;
323 <                                c = 1024.*colval(inline[i],GRN);
324 <                                if (c > 1023) c = 1023;
325 <                                *dp |= (gmap[c] & 0xf8)<<2;
326 <                                c = 1024.*colval(inline[i],BLU);
327 <                                if (c > 1023) c = 1023;
328 <                                *dp++ |= gmap[c]>>3;
320 >                                v = inl[i][RED] + (random()&7);
321 >                                if (v > 255) v = 255;
322 >                                *dp = (v&0xf8)<<7;
323 >                                v = inl[i][GRN] + (random()&7);
324 >                                if (v > 255) v = 255;
325 >                                *dp |= (v&0xf8)<<2;
326 >                                v = inl[i][BLU] + (random()&7);
327 >                                if (v > 255) v = 255;
328 >                                *dp++ |= v>>3;
329                          }
330                  } else {        /* hp->dataBits == 24 */
331                          register unsigned char  *dp;
332                          dp = (unsigned char *)tarData + j*3*hp->x;
333                          for (i = 0; i < hp->x; i++) {
334 <                                c = 1024.*colval(inline[i],BLU);
335 <                                if (c > 1023) c = 1023;
336 <                                *dp++ = gmap[c];
314 <                                c = 1024.*colval(inline[i],GRN);
315 <                                if (c > 1023) c = 1023;
316 <                                *dp++ = gmap[c];
317 <                                c = 1024.*colval(inline[i],RED);
318 <                                if (c > 1023) c = 1023;
319 <                                *dp++ = gmap[c];
334 >                                *dp++ = inl[i][BLU];
335 >                                *dp++ = inl[i][GRN];
336 >                                *dp++ = inl[i][RED];
337                          }
338                  }
339          }
340                                                  /* write out targa data */
341          writetarga(hp, tarData, stdout);
342  
343 <        free((char *)inline);
343 >        free((char *)inl);
344          free((char *)tarData);
345   }
346  
# Line 334 | Line 351 | unsigned char  *d;
351   FILE  *fp;
352   {
353          if (h->dataType == IM_RGB) {            /* uncompressed */
354 <                if (fwrite(d, 3*h->x, h->y, fp) != h->y)
354 >                if (fwrite((char *)d, 3*h->x, h->y, fp) != h->y)
355                          quiterr("error writing targa file");
356                  return;
357          }
# Line 370 | Line 387 | FILE  *fp;
387          int  r, g, b;
388  
389          if (h->dataType == IM_RGB) {            /* uncompressed */
390 <                if (fread(data, 3*h->x, h->y, fp) != h->y)
390 >                if (fread((char *)data, 3*h->x, h->y, fp) != h->y)
391                          goto readerr;
392                  return;
393          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines