ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_t8.c
Revision: 2.19
Committed: Sat Jun 7 05:09:46 2025 UTC (2 weeks, 5 days ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: HEAD
Changes since 2.18: +1 -2 lines
Log Message:
refactor: Put some declarations into "paths.h" and included in "platform.h"

File Contents

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