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.11 by greg, Thu Apr 18 14:35:42 1991 UTC vs.
Revision 2.9 by greg, Tue Mar 20 18:45:04 2018 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  "rtio.h"
18 + #include  "rtmisc.h"
19   #include  "color.h"
20 <
20 > #include  "resolu.h"
21   #include  "random.h"
19
22   #include  "targa.h"
23  
24   #define  goodpic(h)     (((h)->dataType==IM_RGB || (h)->dataType==IM_CRGB) \
# Line 24 | Line 26 | static char SCCSid[] = "$SunId$ LBL";
26  
27   #define  taralloc(h)    (unsigned char *)emalloc((h)->x*(h)->y*(h)->dataBits/8)
28  
29 < #define  readtarga(h,d,f)       ((h)->dataBits==16 ? readt16(h,d,f) : \
30 <                                        readt24(h,d,f))
29 > #define  readtarga(h,d,f)       ((h)->dataBits==16 ? \
30 >                        readt16(h,(unsigned short*)d,f) : readt24(h,d,f))
31  
32 < #define  writetarga(h,d,f)      ((h)->dataBits==16 ? writet16(h,d,f) : \
33 <                                        writet24(h,d,f))
32 > #define  writetarga(h,d,f)      ((h)->dataBits==16 ? \
33 >                        writet16(h,(unsigned short*)d,f) : writet24(h,d,f))
34  
35 < extern char     *ecalloc(), *emalloc();
36 <
35 < extern double  atof(), pow();
36 <
37 < double  gamma = 2.0;                    /* gamma correction */
38 <
35 > double  gamcor = 2.2;                   /* gamma correction */
36 > int  bradj = 0;                         /* brightness adjustment */
37   char  *progname;
40
38   char  msg[128];
39  
40 + static int getint2(FILE *fp);
41 + static void putint2(int i, FILE *fp);
42 + static void quiterr(char *err);
43 + static int getthead(struct hdStruct *hp, char *ip, FILE *fp);
44 + static int putthead(struct hdStruct *hp, char *ip, FILE *fp);
45 + static void tg2ra(struct hdStruct *hp);
46 + static void ra2tg(struct hdStruct *hp);
47 + static void writet24(struct hdStruct *h, unsigned char *d, FILE *fp);
48 + static void writet16(struct hdStruct *h, unsigned short *d, FILE *fp);
49 + static void readt24(struct hdStruct *h, unsigned char *data, FILE *fp);
50 + static void readt16(struct hdStruct *h, unsigned short *data, FILE *fp);
51  
52 < main(argc, argv)
53 < int  argc;
54 < char  *argv[];
52 >
53 > int
54 > main(int  argc, char  *argv[])
55   {
56          struct hdStruct  head;
57          int  reverse = 0;
58          int  i;
59 <        
59 >        SET_DEFAULT_BINARY();
60 >        SET_FILE_BINARY(stdin);
61 >        SET_FILE_BINARY(stdout);
62          progname = argv[0];
63  
64          head.dataBits = 16;
# Line 56 | Line 66 | char  *argv[];
66                  if (argv[i][0] == '-')
67                          switch (argv[i][1]) {
68                          case 'g':
69 <                                gamma = atof(argv[++i]);
69 >                                gamcor = atof(argv[++i]);
70                                  break;
71                          case 'r':
72                                  reverse = !reverse;
# Line 67 | Line 77 | char  *argv[];
77                          case '3':
78                                  head.dataBits = 24;
79                                  break;
80 +                        case 'e':
81 +                                if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
82 +                                        goto userr;
83 +                                bradj = atoi(argv[++i]);
84 +                                break;
85                          default:
86                                  goto userr;
87                          }
# Line 86 | Line 101 | char  *argv[];
101                  quiterr(msg);
102          }
103                                          /* set gamma */
104 <        setcolrgam(gamma);
104 >        setcolrgam(gamcor);
105                                          /* convert */
106          if (reverse) {
107                                          /* get header */
# Line 95 | Line 110 | char  *argv[];
110                  if (!goodpic(&head))
111                          quiterr("incompatible format");
112                                          /* put header */
113 +                newheader("RADIANCE", stdout);
114                  printargs(i, argv, stdout);
115                  fputformat(COLRFMT, stdout);
116                  putchar('\n');
117 <                fputresolu(YMAJOR|YDECR, head.x, head.y, stdout);
117 >                fprtresolu(head.x, head.y, stdout);
118                                          /* convert file */
119                  tg2ra(&head);
120          } else {
121                  if (checkheader(stdin, COLRFMT, NULL) < 0 ||
122 <                        fgetresolu(&head.x, &head.y, stdin) != (YMAJOR|YDECR))
122 >                                fgetresolu(&head.x, &head.y, stdin) < 0)
123                          quiterr("bad picture file");
124                                          /* assign header */
125                  head.textSize = 0;
# Line 119 | Line 135 | char  *argv[];
135          }
136          exit(0);
137   userr:
138 <        fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma] [input [output]]\n",
138 >        fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma][-e +/-stops] [input [output]]\n",
139                          progname);
140          exit(1);
141   }
142  
143  
144 < int
145 < getint2(fp)                     /* get a 2-byte positive integer */
146 < register FILE   *fp;
144 > static int
145 > getint2(                        /* get a 2-byte positive integer */
146 >        register FILE   *fp
147 > )
148   {
149          register int  b1, b2;
150  
# Line 138 | Line 155 | register FILE  *fp;
155   }
156  
157  
158 < putint2(i, fp)                  /* put a 2-byte positive integer */
159 < register int  i;
160 < register FILE   *fp;
158 > static void
159 > putint2(                        /* put a 2-byte positive integer */
160 >        register int  i,
161 >        register FILE   *fp
162 > )
163   {
164          putc(i&0xff, fp);
165          putc(i>>8&0xff, fp);
166   }
167  
168  
169 < quiterr(err)            /* print message and exit */
170 < char  *err;
169 > static void
170 > quiterr(                /* print message and exit */
171 >        char  *err
172 > )
173   {
174          fprintf(stderr, "%s: %s\n", progname, err);
175          exit(1);
176   }
177  
178  
179 < eputs(s)
180 < char *s;
179 > void
180 > eputs(
181 >        char *s
182 > )
183   {
184          fputs(s, stderr);
185   }
186  
187  
188 < quit(code)
189 < int code;
188 > void
189 > quit(
190 >        int code
191 > )
192   {
193          exit(code);
194   }
195  
196  
197 < getthead(hp, ip, fp)            /* read header from input */
198 < struct hdStruct  *hp;
199 < char  *ip;
200 < register FILE  *fp;
197 > static int
198 > getthead(               /* read header from input */
199 >        struct hdStruct  *hp,
200 >        char  *ip,
201 >        register FILE  *fp
202 > )
203   {
204          int     nidbytes;
205  
# Line 202 | Line 229 | register FILE  *fp;
229   }
230  
231  
232 < putthead(hp, ip, fp)            /* write header to output */
233 < struct hdStruct  *hp;
234 < char  *ip;
235 < register FILE  *fp;
232 > static int
233 > putthead(               /* write header to output */
234 >        struct hdStruct  *hp,
235 >        char  *ip,
236 >        register FILE  *fp
237 > )
238   {
239          if (ip != NULL)
240                  putc(strlen(ip), fp);
# Line 230 | Line 259 | register FILE  *fp;
259   }
260  
261  
262 < tg2ra(hp)                       /* targa file to RADIANCE file */
263 < struct hdStruct  *hp;
262 > static void
263 > tg2ra(                  /* targa file to RADIANCE file */
264 >        struct hdStruct  *hp
265 > )
266   {
267          COLR  *scanline;
268          unsigned char  *tarData;
# Line 267 | Line 298 | struct hdStruct  *hp;
298                          }
299                  }
300                  gambs_colrs(scanline, hp->x);
301 +                if (bradj)
302 +                        shiftcolrs(scanline, hp->x, bradj);
303                  if (fwritecolrs(scanline, hp->x, stdout) < 0)
304                          quiterr("error writing RADIANCE file");
305          }
306 <        free((char *)scanline);
307 <        free((char *)tarData);
306 >        free((void *)scanline);
307 >        free((void *)tarData);
308   }
309  
310  
311 < ra2tg(hp)                       /* convert radiance to targa file */
312 < struct hdStruct  *hp;
311 > static void
312 > ra2tg(                  /* convert radiance to targa file */
313 >        struct hdStruct  *hp
314 > )
315   {
316          register int    i, j;
317          unsigned char  *tarData;
# Line 288 | Line 323 | struct hdStruct  *hp;
323          for (j = hp->y-1; j >= 0; j--) {
324                  if (freadcolrs(inl, hp->x, stdin) < 0)
325                          quiterr("error reading RADIANCE file");
326 +                if (bradj)
327 +                        shiftcolrs(inl, hp->x, bradj);
328                  colrs_gambs(inl, hp->x);
329                  if (hp->dataBits == 16) {
330                          register unsigned short  *dp;
331 +                        register int  v;
332                          dp = (unsigned short *)tarData + j*hp->x;
333                          for (i = 0; i < hp->x; i++) {
334 <                                *dp = ((inl[i][RED]+(random()&7)) & 0xf8)<<7;
335 <                                *dp |= ((inl[i][GRN]+(random()&7)) & 0xf8)<<2;
336 <                                *dp++ |= (inl[i][BLU]+(random()&7))>>3;
334 >                                v = inl[i][RED] + (random()&7);
335 >                                if (v > 255) v = 255;
336 >                                *dp = (v&0xf8)<<7;
337 >                                v = inl[i][GRN] + (random()&7);
338 >                                if (v > 255) v = 255;
339 >                                *dp |= (v&0xf8)<<2;
340 >                                v = inl[i][BLU] + (random()&7);
341 >                                if (v > 255) v = 255;
342 >                                *dp++ |= v>>3;
343                          }
344                  } else {        /* hp->dataBits == 24 */
345                          register unsigned char  *dp;
# Line 310 | Line 354 | struct hdStruct  *hp;
354                                                  /* write out targa data */
355          writetarga(hp, tarData, stdout);
356  
357 <        free((char *)inl);
358 <        free((char *)tarData);
357 >        free((void *)inl);
358 >        free((void *)tarData);
359   }
360  
361  
362 < writet24(h, d, fp)              /* write out 24-bit targa data */
363 < struct hdStruct  *h;
364 < unsigned char  *d;
365 < FILE  *fp;
362 > static void
363 > writet24(               /* write out 24-bit targa data */
364 >        struct hdStruct  *h,
365 >        unsigned char  *d,
366 >        FILE  *fp
367 > )
368   {
369          if (h->dataType == IM_RGB) {            /* uncompressed */
370                  if (fwrite((char *)d, 3*h->x, h->y, fp) != h->y)
# Line 329 | Line 375 | FILE  *fp;
375   }
376  
377  
378 < writet16(h, d, fp)              /* write out 16-bit targa data */
379 < struct hdStruct  *h;
380 < register unsigned short  *d;
381 < FILE  *fp;
378 > static void
379 > writet16(               /* write out 16-bit targa data */
380 >        struct hdStruct  *h,
381 >        register unsigned short  *d,
382 >        FILE  *fp
383 > )
384   {
385          register int  cnt;
386  
# Line 347 | Line 395 | FILE  *fp;
395   }
396  
397  
398 < readt24(h, data, fp)            /* read in 24-bit targa data */
399 < register struct hdStruct  *h;
400 < unsigned char  *data;
401 < FILE  *fp;
398 > static void
399 > readt24(                /* read in 24-bit targa data */
400 >        register struct hdStruct  *h,
401 >        unsigned char  *data,
402 >        FILE  *fp
403 > )
404   {
405          register int  cnt, c;
406          register unsigned char  *dp;
# Line 388 | Line 438 | readerr:
438   }
439  
440  
441 < readt16(h, data, fp)            /* read in 16-bit targa data */
442 < register struct hdStruct  *h;
443 < unsigned short  *data;
444 < FILE  *fp;
441 > static void
442 > readt16(                /* read in 16-bit targa data */
443 >        register struct hdStruct  *h,
444 >        unsigned short  *data,
445 >        FILE  *fp
446 > )
447   {
448          register int  cnt, c;
449          register unsigned short  *dp;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines