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.6 by greg, Thu Jan 18 23:58:21 1990 UTC vs.
Revision 2.8 by schorsch, Sun Mar 28 20:33:14 2004 UTC

# Line 1 | Line 1
1 /* Copyright (c) 1986 Regents of the University of California */
2
1   #ifndef lint
2 < static char SCCSid[] = "$SunId$ LBL";
2 > static const char       RCSid[] = "$Id$";
3   #endif
6
4   /*
5   *  ra_t16.c - program to convert between RADIANCE and
6   *              Targa 16, 24 and 32-bit images.
# Line 12 | Line 9 | static char SCCSid[] = "$SunId$ LBL";
9   */
10  
11   #include  <stdio.h>
12 + #include  <string.h>
13 + #include  <time.h>
14 + #include  <math.h>
15  
16 + #include  "platform.h"
17 + #include  "rtmisc.h"
18   #include  "color.h"
19 <
19 > #include  "resolu.h"
20   #include  "random.h"
19
21   #include  "targa.h"
22  
23   #define  goodpic(h)     (((h)->dataType==IM_RGB || (h)->dataType==IM_CRGB) \
# Line 24 | Line 25 | static char SCCSid[] = "$SunId$ LBL";
25  
26   #define  taralloc(h)    (unsigned char *)emalloc((h)->x*(h)->y*(h)->dataBits/8)
27  
28 < #define  readtarga(h,d,f)       ((h)->dataBits==16 ? readt16(h,d,f) : \
29 <                                        readt24(h,d,f))
28 > #define  readtarga(h,d,f)       ((h)->dataBits==16 ? \
29 >                        readt16(h,(unsigned short*)d,f) : readt24(h,d,f))
30  
31 < #define  writetarga(h,d,f)      ((h)->dataBits==16 ? writet16(h,d,f) : \
32 <                                        writet24(h,d,f))
31 > #define  writetarga(h,d,f)      ((h)->dataBits==16 ? \
32 >                        writet16(h,(unsigned short*)d,f) : writet24(h,d,f))
33  
34 < extern char     *ecalloc(), *emalloc();
35 <
35 < extern double  atof(), pow();
36 <
37 < double  gamma = 2.0;                    /* gamma correction */
38 <
34 > double  gamcor = 2.2;                   /* gamma correction */
35 > int  bradj = 0;                         /* brightness adjustment */
36   char  *progname;
40
37   char  msg[128];
38  
39 + static int getint2(FILE *fp);
40 + static void putint2(int i, FILE *fp);
41 + static void quiterr(char *err);
42 + static int getthead(struct hdStruct *hp, char *ip, FILE *fp);
43 + static int putthead(struct hdStruct *hp, char *ip, FILE *fp);
44 + static void tg2ra(struct hdStruct *hp);
45 + static void ra2tg(struct hdStruct *hp);
46 + static void writet24(struct hdStruct *h, unsigned char *d, FILE *fp);
47 + static void writet16(struct hdStruct *h, unsigned short *d, FILE *fp);
48 + static void readt24(struct hdStruct *h, unsigned char *data, FILE *fp);
49 + static void readt16(struct hdStruct *h, unsigned short *data, FILE *fp);
50  
51 < main(argc, argv)
52 < int  argc;
53 < char  *argv[];
51 >
52 > int
53 > main(int  argc, char  *argv[])
54   {
55          struct hdStruct  head;
56          int  reverse = 0;
57          int  i;
58 <        
58 >        SET_DEFAULT_BINARY();
59 >        SET_FILE_BINARY(stdin);
60 >        SET_FILE_BINARY(stdout);
61          progname = argv[0];
62  
63          head.dataBits = 16;
# Line 56 | Line 65 | char  *argv[];
65                  if (argv[i][0] == '-')
66                          switch (argv[i][1]) {
67                          case 'g':
68 <                                gamma = atof(argv[++i]);
68 >                                gamcor = atof(argv[++i]);
69                                  break;
70                          case 'r':
71                                  reverse = !reverse;
# Line 67 | Line 76 | char  *argv[];
76                          case '3':
77                                  head.dataBits = 24;
78                                  break;
79 +                        case 'e':
80 +                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
81 +                                        goto userr;
82 +                                bradj = atoi(argv[++i]);
83 +                                break;
84                          default:
85                                  goto userr;
86                          }
# Line 85 | Line 99 | char  *argv[];
99                  sprintf(msg, "can't open output \"%s\"", argv[i+1]);
100                  quiterr(msg);
101          }
102 +                                        /* set gamma */
103 +        setcolrgam(gamcor);
104                                          /* convert */
105          if (reverse) {
106                                          /* get header */
# Line 93 | Line 109 | char  *argv[];
109                  if (!goodpic(&head))
110                          quiterr("incompatible format");
111                                          /* put header */
112 <                printargs(argc, argv, stdout);
112 >                newheader("RADIANCE", stdout);
113 >                printargs(i, argv, stdout);
114 >                fputformat(COLRFMT, stdout);
115                  putchar('\n');
116 <                fputresolu(YMAJOR|YDECR, head.x, head.y, stdout);
116 >                fprtresolu(head.x, head.y, stdout);
117                                          /* convert file */
118                  tg2ra(&head);
119          } else {
120 <                getheader(stdin, NULL);
121 <                if (fgetresolu(&head.x, &head.y, stdin) != (YMAJOR|YDECR))
120 >                if (checkheader(stdin, COLRFMT, NULL) < 0 ||
121 >                                fgetresolu(&head.x, &head.y, stdin) < 0)
122                          quiterr("bad picture file");
123                                          /* assign header */
124                  head.textSize = 0;
# Line 116 | Line 134 | char  *argv[];
134          }
135          exit(0);
136   userr:
137 <        fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma] [input [output]]\n",
137 >        fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma][-e +/-stops] [input [output]]\n",
138                          progname);
139          exit(1);
140   }
141  
142  
143 < int
144 < getint2(fp)                     /* get a 2-byte positive integer */
145 < register FILE   *fp;
143 > static int
144 > getint2(                        /* get a 2-byte positive integer */
145 >        register FILE   *fp
146 > )
147   {
148          register int  b1, b2;
149  
# Line 135 | Line 154 | register FILE  *fp;
154   }
155  
156  
157 < putint2(i, fp)                  /* put a 2-byte positive integer */
158 < register int  i;
159 < register FILE   *fp;
157 > static void
158 > putint2(                        /* put a 2-byte positive integer */
159 >        register int  i,
160 >        register FILE   *fp
161 > )
162   {
163          putc(i&0xff, fp);
164          putc(i>>8&0xff, fp);
165   }
166  
167  
168 < quiterr(err)            /* print message and exit */
169 < char  *err;
168 > static void
169 > quiterr(                /* print message and exit */
170 >        char  *err
171 > )
172   {
173          fprintf(stderr, "%s: %s\n", progname, err);
174          exit(1);
175   }
176  
177  
178 < eputs(s)
179 < char *s;
178 > void
179 > eputs(
180 >        char *s
181 > )
182   {
183          fputs(s, stderr);
184   }
185  
186  
187 < quit(code)
188 < int code;
187 > void
188 > quit(
189 >        int code
190 > )
191   {
192          exit(code);
193   }
194  
195  
196 < getthead(hp, ip, fp)            /* read header from input */
197 < struct hdStruct  *hp;
198 < char  *ip;
199 < register FILE  *fp;
196 > static int
197 > getthead(               /* read header from input */
198 >        struct hdStruct  *hp,
199 >        char  *ip,
200 >        register FILE  *fp
201 > )
202   {
203          int     nidbytes;
204  
# Line 199 | Line 228 | register FILE  *fp;
228   }
229  
230  
231 < putthead(hp, ip, fp)            /* write header to output */
232 < struct hdStruct  *hp;
233 < char  *ip;
234 < register FILE  *fp;
231 > static int
232 > putthead(               /* write header to output */
233 >        struct hdStruct  *hp,
234 >        char  *ip,
235 >        register FILE  *fp
236 > )
237   {
238          if (ip != NULL)
239                  putc(strlen(ip), fp);
# Line 227 | Line 258 | register FILE  *fp;
258   }
259  
260  
261 < tg2ra(hp)                       /* targa file to RADIANCE file */
262 < struct hdStruct  *hp;
261 > static void
262 > tg2ra(                  /* targa file to RADIANCE file */
263 >        struct hdStruct  *hp
264 > )
265   {
266 <        float  gmap[256];
234 <        COLOR  *scanline;
266 >        COLR  *scanline;
267          unsigned char  *tarData;
268          register int  i, j;
237                                        /* set up gamma correction */
238        for (i = 0; i < 256; i++)
239                gmap[i] = pow((i+.5)/256., gamma);
269                                          /* skip color table */
270          if (hp->mapType == CM_HASMAP)
271                  fseek(stdin, (long)hp->mapLength*hp->CMapBits/8, 1);
# Line 245 | Line 274 | struct hdStruct  *hp;
274                                          /* get data */
275          readtarga(hp, tarData, stdin);
276                                          /* allocate input scanline */
277 <        scanline = (COLOR *)emalloc(hp->x*sizeof(COLOR));
277 >        scanline = (COLR *)emalloc(hp->x*sizeof(COLR));
278                                          /* convert file */
279          for (i = hp->y-1; i >= 0; i--) {
280                  if (hp->dataBits == 16) {
281                          register unsigned short  *dp;
282                          dp = (unsigned short *)tarData + i*hp->x;
283                          for (j = 0; j < hp->x; j++) {
284 <                                setcolor(scanline[j], gmap[*dp>>7 & 0xf8],
285 <                                                gmap[*dp>>2 & 0xf8],
286 <                                                gmap[*dp<<3 & 0xf8]);
284 >                                scanline[j][RED] = *dp>>7 & 0xf8;
285 >                                scanline[j][GRN] = *dp>>2 & 0xf8;
286 >                                scanline[j][BLU] = *dp<<3 & 0xf8;
287                                  dp++;
288                          }
289                  } else {        /* hp->dataBits == 24 */
290                          register unsigned char  *dp;
291                          dp = (unsigned char *)tarData + i*3*hp->x;
292                          for (j = 0; j < hp->x; j++) {
293 <                                setcolor(scanline[j], gmap[dp[2]],
294 <                                                gmap[dp[1]],
295 <                                                gmap[dp[0]]);
293 >                                scanline[j][RED] = dp[2];
294 >                                scanline[j][GRN] = dp[1];
295 >                                scanline[j][BLU] = dp[0];
296                                  dp += 3;
297                          }
298                  }
299 <                if (fwritescan(scanline, hp->x, stdout) < 0)
299 >                gambs_colrs(scanline, hp->x);
300 >                if (bradj)
301 >                        shiftcolrs(scanline, hp->x, bradj);
302 >                if (fwritecolrs(scanline, hp->x, stdout) < 0)
303                          quiterr("error writing RADIANCE file");
304          }
305 <        free((char *)scanline);
306 <        free((char *)tarData);
305 >        free((void *)scanline);
306 >        free((void *)tarData);
307   }
308  
309  
310 < ra2tg(hp)                       /* convert radiance to targa file */
311 < struct hdStruct  *hp;
310 > static void
311 > ra2tg(                  /* convert radiance to targa file */
312 >        struct hdStruct  *hp
313 > )
314   {
281 #define  map(v)         (v >= 1.0 ? 1023 : (int)(v*1023.+.5))
282        unsigned char   gmap[1024];
315          register int    i, j;
316          unsigned char  *tarData;
317 <        COLOR   *inline;
286 <                                        /* set up gamma correction */
287 <        for (i = 0; i < 1024; i++) {
288 <                j = 256.*pow((i+.5)/1024., 1./gamma);
289 <                gmap[i] = hp->dataBits == 16 && j > 248 ? 248 : j;
290 <        }
317 >        COLR    *inl;
318                                          /* allocate space for data */
319 <        inline = (COLOR *)emalloc(hp->x*sizeof(COLOR));
319 >        inl = (COLR *)emalloc(hp->x*sizeof(COLR));
320          tarData = taralloc(hp);
321                                          /* convert file */
322          for (j = hp->y-1; j >= 0; j--) {
323 <                if (freadscan(inline, hp->x, stdin) < 0)
323 >                if (freadcolrs(inl, hp->x, stdin) < 0)
324                          quiterr("error reading RADIANCE file");
325 +                if (bradj)
326 +                        shiftcolrs(inl, hp->x, bradj);
327 +                colrs_gambs(inl, hp->x);
328                  if (hp->dataBits == 16) {
329                          register unsigned short  *dp;
330 +                        register int  v;
331                          dp = (unsigned short *)tarData + j*hp->x;
332                          for (i = 0; i < hp->x; i++) {
333 <                                *dp = ((gmap[map(colval(inline[i],RED))]
334 <                                                +(random()&7)) & 0xf8)<<7;
335 <                                *dp |= ((gmap[map(colval(inline[i],GRN))]
336 <                                                +(random()&7)) & 0xf8)<<2;
337 <                                *dp++ |= (gmap[map(colval(inline[i],BLU))]
338 <                                                +(random()&7))>>3;
333 >                                v = inl[i][RED] + (random()&7);
334 >                                if (v > 255) v = 255;
335 >                                *dp = (v&0xf8)<<7;
336 >                                v = inl[i][GRN] + (random()&7);
337 >                                if (v > 255) v = 255;
338 >                                *dp |= (v&0xf8)<<2;
339 >                                v = inl[i][BLU] + (random()&7);
340 >                                if (v > 255) v = 255;
341 >                                *dp++ |= v>>3;
342                          }
343                  } else {        /* hp->dataBits == 24 */
344                          register unsigned char  *dp;
345                          dp = (unsigned char *)tarData + j*3*hp->x;
346                          for (i = 0; i < hp->x; i++) {
347 <                                *dp++ = gmap[map(colval(inline[i],BLU))];
348 <                                *dp++ = gmap[map(colval(inline[i],GRN))];
349 <                                *dp++ = gmap[map(colval(inline[i],RED))];
347 >                                *dp++ = inl[i][BLU];
348 >                                *dp++ = inl[i][GRN];
349 >                                *dp++ = inl[i][RED];
350                          }
351                  }
352          }
353                                                  /* write out targa data */
354          writetarga(hp, tarData, stdout);
355  
356 <        free((char *)inline);
357 <        free((char *)tarData);
324 < #undef  map
356 >        free((void *)inl);
357 >        free((void *)tarData);
358   }
359  
360  
361 < writet24(h, d, fp)              /* write out 24-bit targa data */
362 < struct hdStruct  *h;
363 < unsigned char  *d;
364 < FILE  *fp;
361 > static void
362 > writet24(               /* write out 24-bit targa data */
363 >        struct hdStruct  *h,
364 >        unsigned char  *d,
365 >        FILE  *fp
366 > )
367   {
368          if (h->dataType == IM_RGB) {            /* uncompressed */
369                  if (fwrite((char *)d, 3*h->x, h->y, fp) != h->y)
# Line 339 | Line 374 | FILE  *fp;
374   }
375  
376  
377 < writet16(h, d, fp)              /* write out 16-bit targa data */
378 < struct hdStruct  *h;
379 < register unsigned short  *d;
380 < FILE  *fp;
377 > static void
378 > writet16(               /* write out 16-bit targa data */
379 >        struct hdStruct  *h,
380 >        register unsigned short  *d,
381 >        FILE  *fp
382 > )
383   {
384          register int  cnt;
385  
# Line 357 | Line 394 | FILE  *fp;
394   }
395  
396  
397 < readt24(h, data, fp)            /* read in 24-bit targa data */
398 < register struct hdStruct  *h;
399 < unsigned char  *data;
400 < FILE  *fp;
397 > static void
398 > readt24(                /* read in 24-bit targa data */
399 >        register struct hdStruct  *h,
400 >        unsigned char  *data,
401 >        FILE  *fp
402 > )
403   {
404          register int  cnt, c;
405          register unsigned char  *dp;
# Line 398 | Line 437 | readerr:
437   }
438  
439  
440 < readt16(h, data, fp)            /* read in 16-bit targa data */
441 < register struct hdStruct  *h;
442 < unsigned short  *data;
443 < FILE  *fp;
440 > static void
441 > readt16(                /* read in 16-bit targa data */
442 >        register struct hdStruct  *h,
443 >        unsigned short  *data,
444 >        FILE  *fp
445 > )
446   {
447          register int  cnt, c;
448          register unsigned short  *dp;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines