ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_t16.c
Revision: 2.10
Committed: Sat Dec 28 18:05:14 2019 UTC (4 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, rad5R3, HEAD
Changes since 2.9: +1 -4 lines
Log Message:
Removed redundant include files

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: ra_t16.c,v 2.9 2018/03/20 18:45:04 greg Exp $";
3 #endif
4 /*
5 * ra_t16.c - program to convert between RADIANCE and
6 * Targa 16, 24 and 32-bit images.
7 *
8 * 11/2/88 Adapted from ra_t8.c
9 */
10
11 #include <math.h>
12
13 #include "platform.h"
14 #include "rtio.h"
15 #include "rtmisc.h"
16 #include "color.h"
17 #include "resolu.h"
18 #include "random.h"
19 #include "targa.h"
20
21 #define goodpic(h) (((h)->dataType==IM_RGB || (h)->dataType==IM_CRGB) \
22 && ((h)->dataBits==16 || (h)->dataBits==24))
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 ? \
27 readt16(h,(unsigned short*)d,f) : readt24(h,d,f))
28
29 #define writetarga(h,d,f) ((h)->dataBits==16 ? \
30 writet16(h,(unsigned short*)d,f) : writet24(h,d,f))
31
32 double gamcor = 2.2; /* gamma correction */
33 int bradj = 0; /* brightness adjustment */
34 char *progname;
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
50 int
51 main(int argc, char *argv[])
52 {
53 struct hdStruct head;
54 int reverse = 0;
55 int i;
56 SET_DEFAULT_BINARY();
57 SET_FILE_BINARY(stdin);
58 SET_FILE_BINARY(stdout);
59 progname = argv[0];
60
61 head.dataBits = 16;
62 for (i = 1; i < argc; i++)
63 if (argv[i][0] == '-')
64 switch (argv[i][1]) {
65 case 'g':
66 gamcor = atof(argv[++i]);
67 break;
68 case 'r':
69 reverse = !reverse;
70 break;
71 case '2':
72 head.dataBits = 16;
73 break;
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 }
85 else
86 break;
87
88 if (i < argc-2)
89 goto userr;
90 /* open input file */
91 if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
92 sprintf(msg, "can't open input \"%s\"", argv[i]);
93 quiterr(msg);
94 }
95 /* open output file */
96 if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
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 */
105 if (getthead(&head, NULL, stdin) < 0)
106 quiterr("bad targa file");
107 if (!goodpic(&head))
108 quiterr("incompatible format");
109 /* put header */
110 newheader("RADIANCE", stdout);
111 printargs(i, argv, stdout);
112 fputformat(COLRFMT, stdout);
113 putchar('\n');
114 fprtresolu(head.x, head.y, stdout);
115 /* convert file */
116 tg2ra(&head);
117 } else {
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;
123 head.mapType = CM_NOMAP;
124 head.dataType = IM_RGB;
125 head.XOffset = 0;
126 head.YOffset = 0;
127 head.imType = 0;
128 /* write header */
129 putthead(&head, NULL, stdout);
130 /* convert file */
131 ra2tg(&head);
132 }
133 exit(0);
134 userr:
135 fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma][-e +/-stops] [input [output]]\n",
136 progname);
137 exit(1);
138 }
139
140
141 static int
142 getint2( /* get a 2-byte positive integer */
143 register FILE *fp
144 )
145 {
146 register int b1, b2;
147
148 if ((b1 = getc(fp)) == EOF || (b2 = getc(fp)) == EOF)
149 quiterr("read error");
150
151 return(b1 | b2<<8);
152 }
153
154
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 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 void
177 eputs(
178 char *s
179 )
180 {
181 fputs(s, stderr);
182 }
183
184
185 void
186 quit(
187 int code
188 )
189 {
190 exit(code);
191 }
192
193
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
203 if ((nidbytes = getc(fp)) == EOF)
204 return(-1);
205 hp->mapType = getc(fp);
206 hp->dataType = getc(fp);
207 hp->mapOrig = getint2(fp);
208 hp->mapLength = getint2(fp);
209 hp->CMapBits = getc(fp);
210 hp->XOffset = getint2(fp);
211 hp->YOffset = getint2(fp);
212 hp->x = getint2(fp);
213 hp->y = getint2(fp);
214 hp->dataBits = getc(fp);
215 hp->imType = getc(fp);
216
217 if (ip != NULL)
218 if (nidbytes)
219 fread((char *)ip, nidbytes, 1, fp);
220 else
221 *ip = '\0';
222 else if (nidbytes)
223 fseek(fp, (long)nidbytes, 1);
224
225 return(feof(fp) || ferror(fp) ? -1 : 0);
226 }
227
228
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);
238 else
239 putc(0, fp);
240 putc(hp->mapType, fp);
241 putc(hp->dataType, fp);
242 putint2(hp->mapOrig, fp);
243 putint2(hp->mapLength, fp);
244 putc(hp->CMapBits, fp);
245 putint2(hp->XOffset, fp);
246 putint2(hp->YOffset, fp);
247 putint2(hp->x, fp);
248 putint2(hp->y, fp);
249 putc(hp->dataBits, fp);
250 putc(hp->imType, fp);
251
252 if (ip != NULL)
253 fputs(ip, fp);
254
255 return(ferror(fp) ? -1 : 0);
256 }
257
258
259 static void
260 tg2ra( /* targa file to RADIANCE file */
261 struct hdStruct *hp
262 )
263 {
264 COLR *scanline;
265 unsigned char *tarData;
266 register int i, j;
267 /* skip color table */
268 if (hp->mapType == CM_HASMAP)
269 fseek(stdin, (long)hp->mapLength*hp->CMapBits/8, 1);
270 /* allocate targa data */
271 tarData = taralloc(hp);
272 /* get data */
273 readtarga(hp, tarData, stdin);
274 /* allocate input scanline */
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 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 scanline[j][RED] = dp[2];
292 scanline[j][GRN] = dp[1];
293 scanline[j][BLU] = dp[0];
294 dp += 3;
295 }
296 }
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((void *)scanline);
304 free((void *)tarData);
305 }
306
307
308 static void
309 ra2tg( /* convert radiance to targa file */
310 struct hdStruct *hp
311 )
312 {
313 register int i, j;
314 unsigned char *tarData;
315 COLR *inl;
316 /* allocate space for data */
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 (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 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++ = 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((void *)inl);
355 free((void *)tarData);
356 }
357
358
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((char *)d, 3*h->x, h->y, fp) != h->y)
368 quiterr("error writing targa file");
369 return;
370 }
371 quiterr("unsupported output type");
372 }
373
374
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
384 if (h->dataType == IM_RGB) { /* uncompressed */
385 for (cnt = h->x*h->y; cnt-- > 0; )
386 putint2(*d++, fp);
387 if (ferror(fp))
388 quiterr("error writing targa file");
389 return;
390 }
391 quiterr("unsupported output type");
392 }
393
394
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((char *)data, 3*h->x, h->y, fp) != h->y)
408 goto readerr;
409 return;
410 }
411 for (dp = data; dp < data+3*h->x*h->y; ) {
412 if ((c = getc(fp)) == EOF)
413 goto readerr;
414 cnt = (c & 0x7f) + 1;
415 if (c & 0x80) { /* repeated pixel */
416 b = getc(fp); g = getc(fp);
417 if ((r = getc(fp)) == EOF)
418 goto readerr;
419 while (cnt--) {
420 *dp++ = b;
421 *dp++ = g;
422 *dp++ = r;
423 }
424 } else /* non-repeating pixels */
425 while (cnt--) {
426 *dp++ = getc(fp); *dp++ = getc(fp);
427 if ((r = getc(fp)) == EOF)
428 goto readerr;
429 *dp++ = r;
430 }
431 }
432 return;
433 readerr:
434 quiterr("error reading targa file");
435 }
436
437
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;
447
448 if (h->dataType == IM_RGB) { /* uncompressed */
449 dp = data;
450 for (cnt = h->x*h->y; cnt-- > 0; )
451 *dp++ = getint2(fp);
452 return;
453 }
454 for (dp = data; dp < data+h->x*h->y; ) {
455 if ((c = getc(fp)) == EOF)
456 goto readerr;
457 cnt = (c & 0x7f) + 1;
458 if (c & 0x80) { /* repeated pixel */
459 c = getint2(fp);
460 while (cnt--)
461 *dp++ = c;
462 } else /* non-repeating pixels */
463 while (cnt--)
464 *dp++ = getint2(fp);
465 }
466 return;
467 readerr:
468 quiterr("error reading targa file");
469 }