ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_t16.c
Revision: 1.10
Committed: Wed Dec 12 14:40:07 1990 UTC (33 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 1.9: +2 -2 lines
Log Message:
fixed error introduced with byte ordering in ra2tg with 24-bit

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 putchar('\n');
100 fputresolu(YMAJOR|YDECR, head.x, head.y, stdout);
101 /* convert file */
102 tg2ra(&head);
103 } else {
104 getheader(stdin, NULL);
105 if (fgetresolu(&head.x, &head.y, stdin) != (YMAJOR|YDECR))
106 quiterr("bad picture file");
107 /* assign header */
108 head.textSize = 0;
109 head.mapType = CM_NOMAP;
110 head.dataType = IM_RGB;
111 head.XOffset = 0;
112 head.YOffset = 0;
113 head.imType = 0;
114 /* write header */
115 putthead(&head, NULL, stdout);
116 /* convert file */
117 ra2tg(&head);
118 }
119 exit(0);
120 userr:
121 fprintf(stderr, "Usage: %s [-2|-3|-r][-g gamma] [input [output]]\n",
122 progname);
123 exit(1);
124 }
125
126
127 int
128 getint2(fp) /* get a 2-byte positive integer */
129 register FILE *fp;
130 {
131 register int b1, b2;
132
133 if ((b1 = getc(fp)) == EOF || (b2 = getc(fp)) == EOF)
134 quiterr("read error");
135
136 return(b1 | b2<<8);
137 }
138
139
140 putint2(i, fp) /* put a 2-byte positive integer */
141 register int i;
142 register FILE *fp;
143 {
144 putc(i&0xff, fp);
145 putc(i>>8&0xff, fp);
146 }
147
148
149 quiterr(err) /* print message and exit */
150 char *err;
151 {
152 fprintf(stderr, "%s: %s\n", progname, err);
153 exit(1);
154 }
155
156
157 eputs(s)
158 char *s;
159 {
160 fputs(s, stderr);
161 }
162
163
164 quit(code)
165 int code;
166 {
167 exit(code);
168 }
169
170
171 getthead(hp, ip, fp) /* read header from input */
172 struct hdStruct *hp;
173 char *ip;
174 register FILE *fp;
175 {
176 int nidbytes;
177
178 if ((nidbytes = getc(fp)) == EOF)
179 return(-1);
180 hp->mapType = getc(fp);
181 hp->dataType = getc(fp);
182 hp->mapOrig = getint2(fp);
183 hp->mapLength = getint2(fp);
184 hp->CMapBits = getc(fp);
185 hp->XOffset = getint2(fp);
186 hp->YOffset = getint2(fp);
187 hp->x = getint2(fp);
188 hp->y = getint2(fp);
189 hp->dataBits = getc(fp);
190 hp->imType = getc(fp);
191
192 if (ip != NULL)
193 if (nidbytes)
194 fread((char *)ip, nidbytes, 1, fp);
195 else
196 *ip = '\0';
197 else if (nidbytes)
198 fseek(fp, (long)nidbytes, 1);
199
200 return(feof(fp) || ferror(fp) ? -1 : 0);
201 }
202
203
204 putthead(hp, ip, fp) /* write header to output */
205 struct hdStruct *hp;
206 char *ip;
207 register FILE *fp;
208 {
209 if (ip != NULL)
210 putc(strlen(ip), fp);
211 else
212 putc(0, fp);
213 putc(hp->mapType, fp);
214 putc(hp->dataType, fp);
215 putint2(hp->mapOrig, fp);
216 putint2(hp->mapLength, fp);
217 putc(hp->CMapBits, fp);
218 putint2(hp->XOffset, fp);
219 putint2(hp->YOffset, fp);
220 putint2(hp->x, fp);
221 putint2(hp->y, fp);
222 putc(hp->dataBits, fp);
223 putc(hp->imType, fp);
224
225 if (ip != NULL)
226 fputs(ip, fp);
227
228 return(ferror(fp) ? -1 : 0);
229 }
230
231
232 tg2ra(hp) /* targa file to RADIANCE file */
233 struct hdStruct *hp;
234 {
235 COLR *scanline;
236 unsigned char *tarData;
237 register int i, j;
238 /* skip color table */
239 if (hp->mapType == CM_HASMAP)
240 fseek(stdin, (long)hp->mapLength*hp->CMapBits/8, 1);
241 /* allocate targa data */
242 tarData = taralloc(hp);
243 /* get data */
244 readtarga(hp, tarData, stdin);
245 /* allocate input scanline */
246 scanline = (COLR *)emalloc(hp->x*sizeof(COLR));
247 /* convert file */
248 for (i = hp->y-1; i >= 0; i--) {
249 if (hp->dataBits == 16) {
250 register unsigned short *dp;
251 dp = (unsigned short *)tarData + i*hp->x;
252 for (j = 0; j < hp->x; j++) {
253 scanline[j][RED] = *dp>>7 & 0xf8;
254 scanline[j][GRN] = *dp>>2 & 0xf8;
255 scanline[j][BLU] = *dp<<3 & 0xf8;
256 dp++;
257 }
258 } else { /* hp->dataBits == 24 */
259 register unsigned char *dp;
260 dp = (unsigned char *)tarData + i*3*hp->x;
261 for (j = 0; j < hp->x; j++) {
262 scanline[j][RED] = dp[2];
263 scanline[j][GRN] = dp[1];
264 scanline[j][BLU] = dp[0];
265 dp += 3;
266 }
267 }
268 gambs_colrs(scanline, hp->x);
269 if (fwritecolrs(scanline, hp->x, stdout) < 0)
270 quiterr("error writing RADIANCE file");
271 }
272 free((char *)scanline);
273 free((char *)tarData);
274 }
275
276
277 ra2tg(hp) /* convert radiance to targa file */
278 struct hdStruct *hp;
279 {
280 register int i, j;
281 unsigned char *tarData;
282 COLR *inl;
283 /* allocate space for data */
284 inl = (COLR *)emalloc(hp->x*sizeof(COLR));
285 tarData = taralloc(hp);
286 /* convert file */
287 for (j = hp->y-1; j >= 0; j--) {
288 if (freadcolrs(inl, hp->x, stdin) < 0)
289 quiterr("error reading RADIANCE file");
290 colrs_gambs(inl, hp->x);
291 if (hp->dataBits == 16) {
292 register unsigned short *dp;
293 dp = (unsigned short *)tarData + j*hp->x;
294 for (i = 0; i < hp->x; i++) {
295 *dp = ((inl[i][RED]+(random()&7)) & 0xf8)<<7;
296 *dp |= ((inl[i][GRN]+(random()&7)) & 0xf8)<<2;
297 *dp++ |= (inl[i][BLU]+(random()&7))>>3;
298 }
299 } else { /* hp->dataBits == 24 */
300 register unsigned char *dp;
301 dp = (unsigned char *)tarData + j*3*hp->x;
302 for (i = 0; i < hp->x; i++) {
303 *dp++ = inl[i][BLU];
304 *dp++ = inl[i][GRN];
305 *dp++ = inl[i][RED];
306 }
307 }
308 }
309 /* write out targa data */
310 writetarga(hp, tarData, stdout);
311
312 free((char *)inl);
313 free((char *)tarData);
314 }
315
316
317 writet24(h, d, fp) /* write out 24-bit targa data */
318 struct hdStruct *h;
319 unsigned char *d;
320 FILE *fp;
321 {
322 if (h->dataType == IM_RGB) { /* uncompressed */
323 if (fwrite((char *)d, 3*h->x, h->y, fp) != h->y)
324 quiterr("error writing targa file");
325 return;
326 }
327 quiterr("unsupported output type");
328 }
329
330
331 writet16(h, d, fp) /* write out 16-bit targa data */
332 struct hdStruct *h;
333 register unsigned short *d;
334 FILE *fp;
335 {
336 register int cnt;
337
338 if (h->dataType == IM_RGB) { /* uncompressed */
339 for (cnt = h->x*h->y; cnt-- > 0; )
340 putint2(*d++, fp);
341 if (ferror(fp))
342 quiterr("error writing targa file");
343 return;
344 }
345 quiterr("unsupported output type");
346 }
347
348
349 readt24(h, data, fp) /* read in 24-bit targa data */
350 register struct hdStruct *h;
351 unsigned char *data;
352 FILE *fp;
353 {
354 register int cnt, c;
355 register unsigned char *dp;
356 int r, g, b;
357
358 if (h->dataType == IM_RGB) { /* uncompressed */
359 if (fread((char *)data, 3*h->x, h->y, fp) != h->y)
360 goto readerr;
361 return;
362 }
363 for (dp = data; dp < data+3*h->x*h->y; ) {
364 if ((c = getc(fp)) == EOF)
365 goto readerr;
366 cnt = (c & 0x7f) + 1;
367 if (c & 0x80) { /* repeated pixel */
368 b = getc(fp); g = getc(fp);
369 if ((r = getc(fp)) == EOF)
370 goto readerr;
371 while (cnt--) {
372 *dp++ = b;
373 *dp++ = g;
374 *dp++ = r;
375 }
376 } else /* non-repeating pixels */
377 while (cnt--) {
378 *dp++ = getc(fp); *dp++ = getc(fp);
379 if ((r = getc(fp)) == EOF)
380 goto readerr;
381 *dp++ = r;
382 }
383 }
384 return;
385 readerr:
386 quiterr("error reading targa file");
387 }
388
389
390 readt16(h, data, fp) /* read in 16-bit targa data */
391 register struct hdStruct *h;
392 unsigned short *data;
393 FILE *fp;
394 {
395 register int cnt, c;
396 register unsigned short *dp;
397
398 if (h->dataType == IM_RGB) { /* uncompressed */
399 dp = data;
400 for (cnt = h->x*h->y; cnt-- > 0; )
401 *dp++ = getint2(fp);
402 return;
403 }
404 for (dp = data; dp < data+h->x*h->y; ) {
405 if ((c = getc(fp)) == EOF)
406 goto readerr;
407 cnt = (c & 0x7f) + 1;
408 if (c & 0x80) { /* repeated pixel */
409 c = getint2(fp);
410 while (cnt--)
411 *dp++ = c;
412 } else /* non-repeating pixels */
413 while (cnt--)
414 *dp++ = getint2(fp);
415 }
416 return;
417 readerr:
418 quiterr("error reading targa file");
419 }