ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_t8.c
Revision: 2.16
Committed: Sat Dec 28 18:05:14 2019 UTC (4 years, 4 months ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R3
Changes since 2.15: +1 -4 lines
Log Message:
Removed redundant include files

File Contents

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