ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_t8.c
Revision: 2.10
Committed: Sat Feb 22 02:07:28 2003 UTC (21 years, 2 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad3R5
Changes since 2.9: +7 -6 lines
Log Message:
Changes and check-in for 3.5 release
Includes new source files and modifications not recorded for many years
See ray/doc/notes/ReleaseNotes for notes between 3.1 and 3.5 release

File Contents

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