ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_t16.c
Revision: 1.11
Committed: Thu Apr 18 14:35:42 1991 UTC (33 years ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.10: +3 -2 lines
Log Message:
added format information to headers

File Contents

# Content
1 /* Copyright (c) 1986 Regents of the University of California */
2
3 #ifndef lint
4 static char SCCSid[] = "$SunId$ LBL";
5 #endif
6
7 /*
8 * ra_t16.c - program to convert between RADIANCE and
9 * Targa 16, 24 and 32-bit images.
10 *
11 * 11/2/88 Adapted from ra_t8.c
12 */
13
14 #include <stdio.h>
15
16 #include "color.h"
17
18 #include "random.h"
19
20 #include "targa.h"
21
22 #define goodpic(h) (((h)->dataType==IM_RGB || (h)->dataType==IM_CRGB) \
23 && ((h)->dataBits==16 || (h)->dataBits==24))
24
25 #define taralloc(h) (unsigned char *)emalloc((h)->x*(h)->y*(h)->dataBits/8)
26
27 #define readtarga(h,d,f) ((h)->dataBits==16 ? readt16(h,d,f) : \
28 readt24(h,d,f))
29
30 #define writetarga(h,d,f) ((h)->dataBits==16 ? writet16(h,d,f) : \
31 writet24(h,d,f))
32
33 extern char *ecalloc(), *emalloc();
34
35 extern double atof(), pow();
36
37 double gamma = 2.0; /* gamma correction */
38
39 char *progname;
40
41 char msg[128];
42
43
44 main(argc, argv)
45 int argc;
46 char *argv[];
47 {
48 struct hdStruct head;
49 int reverse = 0;
50 int i;
51
52 progname = argv[0];
53
54 head.dataBits = 16;
55 for (i = 1; i < argc; i++)
56 if (argv[i][0] == '-')
57 switch (argv[i][1]) {
58 case 'g':
59 gamma = atof(argv[++i]);
60 break;
61 case 'r':
62 reverse = !reverse;
63 break;
64 case '2':
65 head.dataBits = 16;
66 break;
67 case '3':
68 head.dataBits = 24;
69 break;
70 default:
71 goto userr;
72 }
73 else
74 break;
75
76 if (i < argc-2)
77 goto userr;
78 /* open input file */
79 if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
80 sprintf(msg, "can't open input \"%s\"", argv[i]);
81 quiterr(msg);
82 }
83 /* open output file */
84 if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
85 sprintf(msg, "can't open output \"%s\"", argv[i+1]);
86 quiterr(msg);
87 }
88 /* set gamma */
89 setcolrgam(gamma);
90 /* convert */
91 if (reverse) {
92 /* get header */
93 if (getthead(&head, NULL, stdin) < 0)
94 quiterr("bad targa file");
95 if (!goodpic(&head))
96 quiterr("incompatible format");
97 /* put header */
98 printargs(i, argv, stdout);
99 fputformat(COLRFMT, stdout);
100 putchar('\n');
101 fputresolu(YMAJOR|YDECR, head.x, head.y, stdout);
102 /* convert file */
103 tg2ra(&head);
104 } else {
105 if (checkheader(stdin, COLRFMT, NULL) < 0 ||
106 fgetresolu(&head.x, &head.y, stdin) != (YMAJOR|YDECR))
107 quiterr("bad picture file");
108 /* assign header */
109 head.textSize = 0;
110 head.mapType = CM_NOMAP;
111 head.dataType = IM_RGB;
112 head.XOffset = 0;
113 head.YOffset = 0;
114 head.imType = 0;
115 /* write header */
116 putthead(&head, NULL, stdout);
117 /* convert file */
118 ra2tg(&head);
119 }
120 exit(0);
121 userr:
122 fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma] [input [output]]\n",
123 progname);
124 exit(1);
125 }
126
127
128 int
129 getint2(fp) /* get a 2-byte positive integer */
130 register FILE *fp;
131 {
132 register int b1, b2;
133
134 if ((b1 = getc(fp)) == EOF || (b2 = getc(fp)) == EOF)
135 quiterr("read error");
136
137 return(b1 | b2<<8);
138 }
139
140
141 putint2(i, fp) /* put a 2-byte positive integer */
142 register int i;
143 register FILE *fp;
144 {
145 putc(i&0xff, fp);
146 putc(i>>8&0xff, fp);
147 }
148
149
150 quiterr(err) /* print message and exit */
151 char *err;
152 {
153 fprintf(stderr, "%s: %s\n", progname, err);
154 exit(1);
155 }
156
157
158 eputs(s)
159 char *s;
160 {
161 fputs(s, stderr);
162 }
163
164
165 quit(code)
166 int code;
167 {
168 exit(code);
169 }
170
171
172 getthead(hp, ip, fp) /* read header from input */
173 struct hdStruct *hp;
174 char *ip;
175 register FILE *fp;
176 {
177 int nidbytes;
178
179 if ((nidbytes = getc(fp)) == EOF)
180 return(-1);
181 hp->mapType = getc(fp);
182 hp->dataType = getc(fp);
183 hp->mapOrig = getint2(fp);
184 hp->mapLength = getint2(fp);
185 hp->CMapBits = getc(fp);
186 hp->XOffset = getint2(fp);
187 hp->YOffset = getint2(fp);
188 hp->x = getint2(fp);
189 hp->y = getint2(fp);
190 hp->dataBits = getc(fp);
191 hp->imType = getc(fp);
192
193 if (ip != NULL)
194 if (nidbytes)
195 fread((char *)ip, nidbytes, 1, fp);
196 else
197 *ip = '\0';
198 else if (nidbytes)
199 fseek(fp, (long)nidbytes, 1);
200
201 return(feof(fp) || ferror(fp) ? -1 : 0);
202 }
203
204
205 putthead(hp, ip, fp) /* write header to output */
206 struct hdStruct *hp;
207 char *ip;
208 register FILE *fp;
209 {
210 if (ip != NULL)
211 putc(strlen(ip), fp);
212 else
213 putc(0, fp);
214 putc(hp->mapType, fp);
215 putc(hp->dataType, fp);
216 putint2(hp->mapOrig, fp);
217 putint2(hp->mapLength, fp);
218 putc(hp->CMapBits, fp);
219 putint2(hp->XOffset, fp);
220 putint2(hp->YOffset, fp);
221 putint2(hp->x, fp);
222 putint2(hp->y, fp);
223 putc(hp->dataBits, fp);
224 putc(hp->imType, fp);
225
226 if (ip != NULL)
227 fputs(ip, fp);
228
229 return(ferror(fp) ? -1 : 0);
230 }
231
232
233 tg2ra(hp) /* targa file to RADIANCE file */
234 struct hdStruct *hp;
235 {
236 COLR *scanline;
237 unsigned char *tarData;
238 register int i, j;
239 /* skip color table */
240 if (hp->mapType == CM_HASMAP)
241 fseek(stdin, (long)hp->mapLength*hp->CMapBits/8, 1);
242 /* allocate targa data */
243 tarData = taralloc(hp);
244 /* get data */
245 readtarga(hp, tarData, stdin);
246 /* allocate input scanline */
247 scanline = (COLR *)emalloc(hp->x*sizeof(COLR));
248 /* convert file */
249 for (i = hp->y-1; i >= 0; i--) {
250 if (hp->dataBits == 16) {
251 register unsigned short *dp;
252 dp = (unsigned short *)tarData + i*hp->x;
253 for (j = 0; j < hp->x; j++) {
254 scanline[j][RED] = *dp>>7 & 0xf8;
255 scanline[j][GRN] = *dp>>2 & 0xf8;
256 scanline[j][BLU] = *dp<<3 & 0xf8;
257 dp++;
258 }
259 } else { /* hp->dataBits == 24 */
260 register unsigned char *dp;
261 dp = (unsigned char *)tarData + i*3*hp->x;
262 for (j = 0; j < hp->x; j++) {
263 scanline[j][RED] = dp[2];
264 scanline[j][GRN] = dp[1];
265 scanline[j][BLU] = dp[0];
266 dp += 3;
267 }
268 }
269 gambs_colrs(scanline, hp->x);
270 if (fwritecolrs(scanline, hp->x, stdout) < 0)
271 quiterr("error writing RADIANCE file");
272 }
273 free((char *)scanline);
274 free((char *)tarData);
275 }
276
277
278 ra2tg(hp) /* convert radiance to targa file */
279 struct hdStruct *hp;
280 {
281 register int i, j;
282 unsigned char *tarData;
283 COLR *inl;
284 /* allocate space for data */
285 inl = (COLR *)emalloc(hp->x*sizeof(COLR));
286 tarData = taralloc(hp);
287 /* convert file */
288 for (j = hp->y-1; j >= 0; j--) {
289 if (freadcolrs(inl, hp->x, stdin) < 0)
290 quiterr("error reading RADIANCE file");
291 colrs_gambs(inl, hp->x);
292 if (hp->dataBits == 16) {
293 register unsigned short *dp;
294 dp = (unsigned short *)tarData + j*hp->x;
295 for (i = 0; i < hp->x; i++) {
296 *dp = ((inl[i][RED]+(random()&7)) & 0xf8)<<7;
297 *dp |= ((inl[i][GRN]+(random()&7)) & 0xf8)<<2;
298 *dp++ |= (inl[i][BLU]+(random()&7))>>3;
299 }
300 } else { /* hp->dataBits == 24 */
301 register unsigned char *dp;
302 dp = (unsigned char *)tarData + j*3*hp->x;
303 for (i = 0; i < hp->x; i++) {
304 *dp++ = inl[i][BLU];
305 *dp++ = inl[i][GRN];
306 *dp++ = inl[i][RED];
307 }
308 }
309 }
310 /* write out targa data */
311 writetarga(hp, tarData, stdout);
312
313 free((char *)inl);
314 free((char *)tarData);
315 }
316
317
318 writet24(h, d, fp) /* write out 24-bit targa data */
319 struct hdStruct *h;
320 unsigned char *d;
321 FILE *fp;
322 {
323 if (h->dataType == IM_RGB) { /* uncompressed */
324 if (fwrite((char *)d, 3*h->x, h->y, fp) != h->y)
325 quiterr("error writing targa file");
326 return;
327 }
328 quiterr("unsupported output type");
329 }
330
331
332 writet16(h, d, fp) /* write out 16-bit targa data */
333 struct hdStruct *h;
334 register unsigned short *d;
335 FILE *fp;
336 {
337 register int cnt;
338
339 if (h->dataType == IM_RGB) { /* uncompressed */
340 for (cnt = h->x*h->y; cnt-- > 0; )
341 putint2(*d++, fp);
342 if (ferror(fp))
343 quiterr("error writing targa file");
344 return;
345 }
346 quiterr("unsupported output type");
347 }
348
349
350 readt24(h, data, fp) /* read in 24-bit targa data */
351 register struct hdStruct *h;
352 unsigned char *data;
353 FILE *fp;
354 {
355 register int cnt, c;
356 register unsigned char *dp;
357 int r, g, b;
358
359 if (h->dataType == IM_RGB) { /* uncompressed */
360 if (fread((char *)data, 3*h->x, h->y, fp) != h->y)
361 goto readerr;
362 return;
363 }
364 for (dp = data; dp < data+3*h->x*h->y; ) {
365 if ((c = getc(fp)) == EOF)
366 goto readerr;
367 cnt = (c & 0x7f) + 1;
368 if (c & 0x80) { /* repeated pixel */
369 b = getc(fp); g = getc(fp);
370 if ((r = getc(fp)) == EOF)
371 goto readerr;
372 while (cnt--) {
373 *dp++ = b;
374 *dp++ = g;
375 *dp++ = r;
376 }
377 } else /* non-repeating pixels */
378 while (cnt--) {
379 *dp++ = getc(fp); *dp++ = getc(fp);
380 if ((r = getc(fp)) == EOF)
381 goto readerr;
382 *dp++ = r;
383 }
384 }
385 return;
386 readerr:
387 quiterr("error reading targa file");
388 }
389
390
391 readt16(h, data, fp) /* read in 16-bit targa data */
392 register struct hdStruct *h;
393 unsigned short *data;
394 FILE *fp;
395 {
396 register int cnt, c;
397 register unsigned short *dp;
398
399 if (h->dataType == IM_RGB) { /* uncompressed */
400 dp = data;
401 for (cnt = h->x*h->y; cnt-- > 0; )
402 *dp++ = getint2(fp);
403 return;
404 }
405 for (dp = data; dp < data+h->x*h->y; ) {
406 if ((c = getc(fp)) == EOF)
407 goto readerr;
408 cnt = (c & 0x7f) + 1;
409 if (c & 0x80) { /* repeated pixel */
410 c = getint2(fp);
411 while (cnt--)
412 *dp++ = c;
413 } else /* non-repeating pixels */
414 while (cnt--)
415 *dp++ = getint2(fp);
416 }
417 return;
418 readerr:
419 quiterr("error reading targa file");
420 }