ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_t8.c
Revision: 2.12
Committed: Thu Jun 5 19:29:34 2003 UTC (20 years, 10 months ago) by schorsch
Content type: text/plain
Branch: MAIN
Changes since 2.11: +6 -19 lines
Log Message:
Macros for setting binary file mode. Replacing MSDOS by _WIN32.

File Contents

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: ra_t8.c,v 2.11 2003/05/13 17:58:33 greg Exp $";
3 #endif
4 /*
5 * ra_t8.c - program to convert between RADIANCE and
6 * Targa 8-bit color-mapped images.
7 *
8 * 8/22/88 Adapted from ra_pr.c
9 */
10
11 #include <stdio.h>
12 #include <time.h>
13 #include <math.h>
14
15 #include "platform.h"
16 #include "color.h"
17 #include "resolu.h"
18 #include "targa.h"
19
20
21 #define goodpic(h) (my_imType(h) && my_mapType(h))
22 #define my_imType(h) (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \
23 && (h)->dataBits==8 && (h)->imType==0)
24 #define my_mapType(h) ((h)->mapType==CM_HASMAP && \
25 ((h)->CMapBits==24 || (h)->CMapBits==32))
26
27 #define taralloc(h) (BYTE *)emalloc((h)->x*(h)->y)
28
29 BYTE clrtab[256][3];
30
31 extern int samplefac;
32
33 extern char *ecalloc(), *emalloc();
34
35 extern long ftell();
36
37 double gamv = 2.2; /* gamv correction */
38
39 int bradj = 0; /* brightness adjustment */
40
41 char *progname;
42
43 char errmsg[128];
44
45 COLR *inl;
46
47 BYTE *tarData;
48
49 int xmax, ymax;
50
51
52 main(argc, argv)
53 int argc;
54 char *argv[];
55 {
56 struct hdStruct head;
57 int dither = 1;
58 int reverse = 0;
59 int ncolors = 256;
60 int greyscale = 0;
61 int i;
62 SET_DEFAULT_BINARY();
63 SET_FILE_BINARY(stdin);
64 SET_FILE_BINARY(stdout);
65 progname = argv[0];
66 samplefac = 0;
67
68 for (i = 1; i < argc; i++)
69 if (argv[i][0] == '-')
70 switch (argv[i][1]) {
71 case 'd':
72 dither = !dither;
73 break;
74 case 'g':
75 gamv = atof(argv[++i]);
76 break;
77 case 'r':
78 reverse = !reverse;
79 break;
80 case 'b':
81 greyscale = 1;
82 break;
83 case 'e':
84 if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
85 goto userr;
86 bradj = atoi(argv[++i]);
87 break;
88 case 'c':
89 ncolors = atoi(argv[++i]);
90 break;
91 case 'n':
92 samplefac = atoi(argv[++i]);
93 break;
94 default:
95 goto userr;
96 }
97 else
98 break;
99
100 if (i < argc-2)
101 goto userr;
102 if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
103 sprintf(errmsg, "cannot open input \"%s\"", argv[i]);
104 quiterr(errmsg);
105 }
106 if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
107 sprintf(errmsg, "cannot open output \"%s\"", argv[i+1]);
108 quiterr(errmsg);
109 }
110 if (reverse) {
111 /* get header */
112 if (getthead(&head, NULL, stdin) < 0)
113 quiterr("bad targa file");
114 if (!goodpic(&head))
115 quiterr("incompatible format");
116 xmax = head.x;
117 ymax = head.y;
118 /* put header */
119 newheader("RADIANCE", stdout);
120 printargs(i, argv, stdout);
121 fputformat(COLRFMT, stdout);
122 putchar('\n');
123 fprtresolu(xmax, ymax, stdout);
124 /* convert file */
125 tg2ra(&head);
126 } else {
127 if (getrhead(&head, stdin) < 0)
128 quiterr("bad Radiance input");
129 /* write header */
130 putthead(&head, NULL, stdout);
131 /* convert file */
132 if (greyscale)
133 getgrey(ncolors);
134 else
135 getmapped(ncolors, dither);
136 /* write data */
137 writetarga(&head, tarData, stdout);
138 }
139 quiterr(NULL);
140 userr:
141 fprintf(stderr,
142 "Usage: %s [-d][-n samp][-c ncolors][-b][-g gamv][-e +/-stops] input [output]\n",
143 progname);
144 fprintf(stderr, " Or: %s -r [-g gamv][-e +/-stops] [input [output]]\n",
145 progname);
146 exit(1);
147 }
148
149
150 int
151 getint2(fp) /* get a 2-byte positive integer */
152 register FILE *fp;
153 {
154 register int b1, b2;
155
156 if ((b1 = getc(fp)) == EOF || (b2 = getc(fp)) == EOF)
157 quiterr("read error");
158
159 return(b1 | b2<<8);
160 }
161
162
163 putint2(i, fp) /* put a 2-byte positive integer */
164 register int i;
165 register FILE *fp;
166 {
167 putc(i&0xff, fp);
168 putc(i>>8&0xff, fp);
169 }
170
171
172 quiterr(err) /* print message and exit */
173 char *err;
174 {
175 if (err != NULL) {
176 fprintf(stderr, "%s: %s\n", progname, err);
177 exit(1);
178 }
179 exit(0);
180 }
181
182
183 void
184 eputs(s)
185 char *s;
186 {
187 fputs(s, stderr);
188 }
189
190
191 void
192 quit(code)
193 int code;
194 {
195 exit(code);
196 }
197
198
199 getthead(hp, ip, fp) /* read header from input */
200 struct hdStruct *hp;
201 char *ip;
202 register FILE *fp;
203 {
204 int nidbytes;
205
206 if ((nidbytes = getc(fp)) == EOF)
207 return(-1);
208 hp->mapType = getc(fp);
209 hp->dataType = getc(fp);
210 hp->mapOrig = getint2(fp);
211 hp->mapLength = getint2(fp);
212 hp->CMapBits = getc(fp);
213 hp->XOffset = getint2(fp);
214 hp->YOffset = getint2(fp);
215 hp->x = getint2(fp);
216 hp->y = getint2(fp);
217 hp->dataBits = getc(fp);
218 hp->imType = getc(fp);
219
220 if (ip != NULL)
221 if (nidbytes)
222 fread((char *)ip, nidbytes, 1, fp);
223 else
224 *ip = '\0';
225 else if (nidbytes)
226 fseek(fp, (long)nidbytes, 1);
227
228 return(feof(fp) || ferror(fp) ? -1 : 0);
229 }
230
231
232 putthead(hp, ip, fp) /* write header to output */
233 struct hdStruct *hp;
234 char *ip;
235 register FILE *fp;
236 {
237 if (ip != NULL)
238 putc(strlen(ip), fp);
239 else
240 putc(0, fp);
241 putc(hp->mapType, fp);
242 putc(hp->dataType, fp);
243 putint2(hp->mapOrig, fp);
244 putint2(hp->mapLength, fp);
245 putc(hp->CMapBits, fp);
246 putint2(hp->XOffset, fp);
247 putint2(hp->YOffset, fp);
248 putint2(hp->x, fp);
249 putint2(hp->y, fp);
250 putc(hp->dataBits, fp);
251 putc(hp->imType, fp);
252
253 if (ip != NULL)
254 fputs(ip, fp);
255
256 return(ferror(fp) ? -1 : 0);
257 }
258
259
260 getrhead(h, fp) /* load RADIANCE input file header */
261 register struct hdStruct *h;
262 FILE *fp;
263 {
264 /* get header info. */
265 if (checkheader(fp, COLRFMT, NULL) < 0 ||
266 fgetresolu(&xmax, &ymax, fp) < 0)
267 return(-1);
268 /* assign header */
269 h->textSize = 0;
270 h->mapType = CM_HASMAP;
271 h->dataType = IM_CMAP;
272 h->mapOrig = 0;
273 h->mapLength = 256;
274 h->CMapBits = 24;
275 h->XOffset = 0;
276 h->YOffset = 0;
277 h->x = xmax;
278 h->y = ymax;
279 h->dataBits = 8;
280 h->imType = 0;
281 /* allocate scanline */
282 inl = (COLR *)emalloc(xmax*sizeof(COLR));
283 /* allocate targa data */
284 tarData = taralloc(h);
285
286 return(0);
287 }
288
289
290 tg2ra(hp) /* targa file to RADIANCE file */
291 struct hdStruct *hp;
292 {
293 union {
294 BYTE c3[256][3];
295 BYTE c4[256][4];
296 } map;
297 COLR ctab[256];
298 COLR *scanline;
299 register int i, j;
300
301 /* get color table */
302 if ((hp->CMapBits==24 ? fread((char *)(map.c3+hp->mapOrig),
303 3*hp->mapLength,1,stdin) :
304 fread((char *)(map.c4+hp->mapOrig),
305 4*hp->mapLength,1,stdin)) != 1)
306 quiterr("error reading color table");
307 /* convert table */
308 for (i = hp->mapOrig; i < hp->mapOrig+hp->mapLength; i++)
309 if (hp->CMapBits == 24)
310 setcolr(ctab[i],
311 pow((map.c3[i][2]+.5)/256.,gamv),
312 pow((map.c3[i][1]+.5)/256.,gamv),
313 pow((map.c3[i][0]+.5)/256.,gamv));
314 else
315 setcolr(ctab[i],
316 pow((map.c4[i][3]+.5)/256.,gamv),
317 pow((map.c4[i][2]+.5)/256.,gamv),
318 pow((map.c4[i][1]+.5)/256.,gamv));
319 if (bradj)
320 shiftcolrs(ctab, 256, bradj);
321 /* allocate targa data */
322 tarData = taralloc(hp);
323 /* get data */
324 readtarga(hp, tarData, stdin);
325 /* allocate input scanline */
326 scanline = (COLR *)emalloc(xmax*sizeof(COLR));
327 /* convert file */
328 for (i = ymax-1; i >= 0; i--) {
329 for (j = 0; j < xmax; j++)
330 copycolr(scanline[j], ctab[tarData[i*xmax+j]]);
331 if (fwritecolrs(scanline, xmax, stdout) < 0)
332 quiterr("error writing RADIANCE file");
333 }
334 free((void *)scanline);
335 free((void *)tarData);
336 }
337
338
339 getmapped(nc, dith) /* read in and quantize image */
340 int nc; /* number of colors to use */
341 int dith; /* use dithering? */
342 {
343 long fpos;
344 register int y;
345
346 setcolrgam(gamv);
347 fpos = ftell(stdin);
348 if ((samplefac ? neu_init(xmax*ymax) : new_histo(xmax*ymax)) == -1)
349 quiterr("cannot initialized histogram");
350 for (y = ymax-1; y >= 0; y--) {
351 if (freadcolrs(inl, xmax, stdin) < 0)
352 quiterr("error reading Radiance input");
353 if (bradj)
354 shiftcolrs(inl, xmax, bradj);
355 colrs_gambs(inl, xmax);
356 if (samplefac)
357 neu_colrs(inl, xmax);
358 else
359 cnt_colrs(inl, xmax);
360 }
361 if (fseek(stdin, fpos, 0) == EOF)
362 quiterr("Radiance input must be from a file");
363 if (samplefac) /* map colors */
364 neu_clrtab(nc);
365 else
366 new_clrtab(nc);
367 for (y = ymax-1; y >= 0; y--) {
368 if (freadcolrs(inl, xmax, stdin) < 0)
369 quiterr("error reading Radiance input");
370 if (bradj)
371 shiftcolrs(inl, xmax, bradj);
372 colrs_gambs(inl, xmax);
373 if (samplefac)
374 if (dith)
375 neu_dith_colrs(tarData+y*xmax, inl, xmax);
376 else
377 neu_map_colrs(tarData+y*xmax, inl, xmax);
378 else
379 if (dith)
380 dith_colrs(tarData+y*xmax, inl, xmax);
381 else
382 map_colrs(tarData+y*xmax, inl, xmax);
383 }
384 }
385
386
387 getgrey(nc) /* read in and convert to greyscale image */
388 int nc; /* number of colors to use */
389 {
390 int y;
391 register BYTE *dp;
392 register int x;
393
394 setcolrgam(gamv);
395 dp = tarData+xmax*ymax;;
396 for (y = ymax-1; y >= 0; y--) {
397 if (freadcolrs(inl, xmax, stdin) < 0)
398 quiterr("error reading Radiance input");
399 if (bradj)
400 shiftcolrs(inl, xmax, bradj);
401 x = xmax;
402 while (x--)
403 inl[x][GRN] = normbright(inl[x]);
404 colrs_gambs(inl, xmax);
405 x = xmax;
406 if (nc < 256)
407 while (x--)
408 *--dp = ((long)inl[x][GRN]*nc+nc/2)>>8;
409 else
410 while (x--)
411 *--dp = inl[x][GRN];
412 }
413 for (x = 0; x < nc; x++)
414 clrtab[x][RED] = clrtab[x][GRN] =
415 clrtab[x][BLU] = ((long)x*256+128)/nc;
416 }
417
418
419 writetarga(h, d, fp) /* write out targa data */
420 struct hdStruct *h;
421 BYTE *d;
422 FILE *fp;
423 {
424 register int i, j;
425
426 for (i = 0; i < h->mapLength; i++) /* write color map */
427 for (j = 2; j >= 0; j--)
428 putc(clrtab[i][j], fp);
429 if (h->dataType == IM_CMAP) { /* uncompressed */
430 if (fwrite((char *)d,h->x*sizeof(BYTE),h->y,fp) != h->y)
431 quiterr("error writing targa file");
432 return;
433 }
434 quiterr("unsupported output type");
435 }
436
437
438 readtarga(h, data, fp) /* read in targa data */
439 struct hdStruct *h;
440 BYTE *data;
441 FILE *fp;
442 {
443 register int cnt, c;
444 register BYTE *dp;
445
446 if (h->dataType == IM_CMAP) { /* uncompressed */
447 if (fread((char *)data,h->x*sizeof(BYTE),h->y,fp) != h->y)
448 goto readerr;
449 return;
450 }
451 for (dp = data; dp < data+h->x*h->y; ) {
452 if ((c = getc(fp)) == EOF)
453 goto readerr;
454 cnt = (c & 0x7f) + 1;
455 if (c & 0x80) { /* repeated pixel */
456 if ((c = getc(fp)) == EOF)
457 goto readerr;
458 while (cnt--)
459 *dp++ = c;
460 } else /* non-repeating pixels */
461 while (cnt--) {
462 if ((c = getc(fp)) == EOF)
463 goto readerr;
464 *dp++ = c;
465 }
466 }
467 return;
468 readerr:
469 quiterr("error reading targa file");
470 }