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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines