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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines