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

# User Rev Content
1 greg 1.1 #ifndef lint
2 schorsch 2.12 static const char RCSid[] = "$Id: ra_t8.c,v 2.11 2003/05/13 17:58:33 greg Exp $";
3 greg 1.1 #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 greg 2.10 #include <time.h>
13 schorsch 2.12 #include <math.h>
14 greg 2.10
15 schorsch 2.12 #include "platform.h"
16 greg 1.1 #include "color.h"
17 greg 1.12 #include "resolu.h"
18 greg 1.1 #include "targa.h"
19    
20    
21 greg 2.3 #define goodpic(h) (my_imType(h) && my_mapType(h))
22     #define my_imType(h) (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \
23 greg 1.1 && (h)->dataBits==8 && (h)->imType==0)
24 greg 2.3 #define my_mapType(h) ((h)->mapType==CM_HASMAP && \
25 greg 1.1 ((h)->CMapBits==24 || (h)->CMapBits==32))
26    
27 greg 2.4 #define taralloc(h) (BYTE *)emalloc((h)->x*(h)->y)
28 greg 1.1
29 greg 2.7 BYTE clrtab[256][3];
30 greg 1.1
31 greg 2.7 extern int samplefac;
32    
33 greg 1.1 extern char *ecalloc(), *emalloc();
34    
35     extern long ftell();
36    
37 greg 2.4 double gamv = 2.2; /* gamv correction */
38 greg 1.1
39 greg 1.10 int bradj = 0; /* brightness adjustment */
40    
41 greg 1.1 char *progname;
42    
43     char errmsg[128];
44    
45 greg 1.7 COLR *inl;
46 greg 1.1
47 greg 2.4 BYTE *tarData;
48 greg 1.1
49     int xmax, ymax;
50    
51    
52     main(argc, argv)
53     int argc;
54     char *argv[];
55     {
56 greg 2.3 struct hdStruct head;
57 greg 1.1 int dither = 1;
58     int reverse = 0;
59     int ncolors = 256;
60     int greyscale = 0;
61     int i;
62 schorsch 2.12 SET_DEFAULT_BINARY();
63     SET_FILE_BINARY(stdin);
64     SET_FILE_BINARY(stdout);
65 greg 1.1 progname = argv[0];
66 greg 2.7 samplefac = 0;
67 greg 1.1
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 greg 2.4 gamv = atof(argv[++i]);
76 greg 1.1 break;
77     case 'r':
78     reverse = !reverse;
79     break;
80     case 'b':
81     greyscale = 1;
82     break;
83 greg 1.10 case 'e':
84     if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
85     goto userr;
86     bradj = atoi(argv[++i]);
87     break;
88 greg 1.1 case 'c':
89     ncolors = atoi(argv[++i]);
90     break;
91 greg 2.7 case 'n':
92     samplefac = atoi(argv[++i]);
93     break;
94 greg 1.1 default:
95     goto userr;
96     }
97     else
98     break;
99    
100 greg 2.4 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 greg 1.1 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 greg 2.6 newheader("RADIANCE", stdout);
120 greg 1.6 printargs(i, argv, stdout);
121 greg 1.9 fputformat(COLRFMT, stdout);
122 greg 1.1 putchar('\n');
123 greg 1.12 fprtresolu(xmax, ymax, stdout);
124 greg 1.1 /* convert file */
125     tg2ra(&head);
126     } else {
127 greg 2.4 if (getrhead(&head, stdin) < 0)
128     quiterr("bad Radiance input");
129 greg 1.1 /* write header */
130     putthead(&head, NULL, stdout);
131     /* convert file */
132     if (greyscale)
133 greg 2.4 getgrey(ncolors);
134 greg 1.1 else
135 greg 2.4 getmapped(ncolors, dither);
136 greg 1.1 /* write data */
137     writetarga(&head, tarData, stdout);
138     }
139     quiterr(NULL);
140     userr:
141     fprintf(stderr,
142 greg 2.8 "Usage: %s [-d][-n samp][-c ncolors][-b][-g gamv][-e +/-stops] input [output]\n",
143 greg 1.1 progname);
144 greg 2.4 fprintf(stderr, " Or: %s -r [-g gamv][-e +/-stops] [input [output]]\n",
145 greg 1.1 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 greg 2.10 void
184 greg 1.1 eputs(s)
185     char *s;
186     {
187     fputs(s, stderr);
188     }
189    
190    
191 greg 2.10 void
192 greg 1.1 quit(code)
193     int code;
194     {
195     exit(code);
196     }
197    
198    
199     getthead(hp, ip, fp) /* read header from input */
200 greg 2.3 struct hdStruct *hp;
201 greg 1.1 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 greg 1.4 fread((char *)ip, nidbytes, 1, fp);
223 greg 1.1 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 greg 2.3 struct hdStruct *hp;
234 greg 1.1 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 greg 2.4 getrhead(h, fp) /* load RADIANCE input file header */
261 greg 1.1 register struct hdStruct *h;
262 greg 2.4 FILE *fp;
263 greg 1.1 {
264 greg 1.9 /* get header info. */
265 greg 2.4 if (checkheader(fp, COLRFMT, NULL) < 0 ||
266     fgetresolu(&xmax, &ymax, fp) < 0)
267     return(-1);
268 greg 1.1 /* 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 greg 1.7 inl = (COLR *)emalloc(xmax*sizeof(COLR));
283 greg 1.1 /* allocate targa data */
284     tarData = taralloc(h);
285    
286 greg 2.4 return(0);
287 greg 1.1 }
288    
289    
290     tg2ra(hp) /* targa file to RADIANCE file */
291 greg 2.3 struct hdStruct *hp;
292 greg 1.1 {
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 greg 1.12 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 greg 1.1 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 greg 2.4 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 greg 1.1 else
315     setcolr(ctab[i],
316 greg 2.4 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 greg 1.10 if (bradj)
320     shiftcolrs(ctab, 256, bradj);
321 greg 1.1 /* 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 greg 2.10 free((void *)scanline);
335     free((void *)tarData);
336 greg 1.1 }
337    
338    
339 greg 2.4 getmapped(nc, dith) /* read in and quantize image */
340     int nc; /* number of colors to use */
341     int dith; /* use dithering? */
342 greg 1.1 {
343 greg 2.4 long fpos;
344     register int y;
345 greg 1.1
346 greg 2.4 setcolrgam(gamv);
347     fpos = ftell(stdin);
348 greg 2.7 if ((samplefac ? neu_init(xmax*ymax) : new_histo(xmax*ymax)) == -1)
349     quiterr("cannot initialized histogram");
350 greg 2.4 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 greg 2.7 if (samplefac)
357     neu_colrs(inl, xmax);
358     else
359     cnt_colrs(inl, xmax);
360 greg 1.1 }
361 greg 2.4 if (fseek(stdin, fpos, 0) == EOF)
362     quiterr("Radiance input must be from a file");
363 greg 2.7 if (samplefac) /* map colors */
364     neu_clrtab(nc);
365     else
366     new_clrtab(nc);
367 greg 2.4 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 greg 2.7 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 greg 2.4 else
379 greg 2.7 if (dith)
380     dith_colrs(tarData+y*xmax, inl, xmax);
381     else
382     map_colrs(tarData+y*xmax, inl, xmax);
383 greg 2.4 }
384 greg 1.1 }
385    
386    
387 greg 2.4 getgrey(nc) /* read in and convert to greyscale image */
388     int nc; /* number of colors to use */
389 greg 1.1 {
390 greg 2.4 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 greg 2.5 x = xmax;
402     while (x--)
403     inl[x][GRN] = normbright(inl[x]);
404 greg 2.4 colrs_gambs(inl, xmax);
405     x = xmax;
406     if (nc < 256)
407     while (x--)
408 greg 2.5 *--dp = ((long)inl[x][GRN]*nc+nc/2)>>8;
409 greg 2.4 else
410     while (x--)
411 greg 2.5 *--dp = inl[x][GRN];
412 greg 2.4 }
413     for (x = 0; x < nc; x++)
414     clrtab[x][RED] = clrtab[x][GRN] =
415 greg 2.5 clrtab[x][BLU] = ((long)x*256+128)/nc;
416 greg 1.1 }
417    
418    
419     writetarga(h, d, fp) /* write out targa data */
420 greg 2.3 struct hdStruct *h;
421 greg 2.4 BYTE *d;
422 greg 1.1 FILE *fp;
423     {
424 greg 2.4 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 greg 1.1 if (h->dataType == IM_CMAP) { /* uncompressed */
430 greg 2.4 if (fwrite((char *)d,h->x*sizeof(BYTE),h->y,fp) != h->y)
431 greg 1.1 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 greg 2.3 struct hdStruct *h;
440 greg 2.4 BYTE *data;
441 greg 1.1 FILE *fp;
442     {
443     register int cnt, c;
444 greg 2.4 register BYTE *dp;
445 greg 1.1
446     if (h->dataType == IM_CMAP) { /* uncompressed */
447 greg 2.4 if (fread((char *)data,h->x*sizeof(BYTE),h->y,fp) != h->y)
448 greg 1.1 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     }