ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/px/ra_t8.c
Revision: 2.17
Committed: Thu Feb 9 21:54:11 2023 UTC (15 months, 1 week ago) by greg
Content type: text/plain
Branch: MAIN
CVS Tags: rad5R4, HEAD
Changes since 2.16: +17 -19 lines
Log Message:
fix: compile error fixes related to redeclaration of eputs() and wputs()

File Contents

# User Rev Content
1 greg 1.1 #ifndef lint
2 greg 2.17 static const char RCSid[] = "$Id: ra_t8.c,v 2.16 2019/12/28 18:05:14 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 greg 2.17 FILE *fp
156 schorsch 2.13 )
157 greg 1.1 {
158 greg 2.17 int b1, b2;
159 greg 1.1
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 greg 2.17 int i,
170     FILE *fp
171 schorsch 2.13 )
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 2.17 eputs(const char *s)
193 greg 1.1 {
194     fputs(s, stderr);
195     }
196    
197    
198 greg 2.10 void
199 greg 2.17 quit(int code)
200 greg 1.1 {
201     exit(code);
202     }
203    
204    
205 schorsch 2.13 static int
206     getthead( /* read header from input */
207     struct hdStruct *hp,
208     char *ip,
209 greg 2.17 FILE *fp
210 schorsch 2.13 )
211 greg 1.1 {
212     int nidbytes;
213    
214     if ((nidbytes = getc(fp)) == EOF)
215     return(-1);
216     hp->mapType = getc(fp);
217     hp->dataType = getc(fp);
218     hp->mapOrig = getint2(fp);
219     hp->mapLength = getint2(fp);
220     hp->CMapBits = getc(fp);
221     hp->XOffset = getint2(fp);
222     hp->YOffset = getint2(fp);
223     hp->x = getint2(fp);
224     hp->y = getint2(fp);
225     hp->dataBits = getc(fp);
226     hp->imType = getc(fp);
227    
228     if (ip != NULL)
229     if (nidbytes)
230 greg 1.4 fread((char *)ip, nidbytes, 1, fp);
231 greg 1.1 else
232     *ip = '\0';
233     else if (nidbytes)
234     fseek(fp, (long)nidbytes, 1);
235    
236     return(feof(fp) || ferror(fp) ? -1 : 0);
237     }
238    
239    
240 schorsch 2.13 static int
241     putthead( /* write header to output */
242     struct hdStruct *hp,
243     char *ip,
244 greg 2.17 FILE *fp
245 schorsch 2.13 )
246 greg 1.1 {
247     if (ip != NULL)
248     putc(strlen(ip), fp);
249     else
250     putc(0, fp);
251     putc(hp->mapType, fp);
252     putc(hp->dataType, fp);
253     putint2(hp->mapOrig, fp);
254     putint2(hp->mapLength, fp);
255     putc(hp->CMapBits, fp);
256     putint2(hp->XOffset, fp);
257     putint2(hp->YOffset, fp);
258     putint2(hp->x, fp);
259     putint2(hp->y, fp);
260     putc(hp->dataBits, fp);
261     putc(hp->imType, fp);
262    
263     if (ip != NULL)
264     fputs(ip, fp);
265    
266     return(ferror(fp) ? -1 : 0);
267     }
268    
269    
270 schorsch 2.13 static int
271     getrhead( /* load RADIANCE input file header */
272 greg 2.17 struct hdStruct *h,
273 schorsch 2.13 FILE *fp
274     )
275 greg 1.1 {
276 greg 1.9 /* get header info. */
277 greg 2.4 if (checkheader(fp, COLRFMT, NULL) < 0 ||
278     fgetresolu(&xmax, &ymax, fp) < 0)
279     return(-1);
280 greg 1.1 /* assign header */
281     h->textSize = 0;
282     h->mapType = CM_HASMAP;
283     h->dataType = IM_CMAP;
284     h->mapOrig = 0;
285     h->mapLength = 256;
286     h->CMapBits = 24;
287     h->XOffset = 0;
288     h->YOffset = 0;
289     h->x = xmax;
290     h->y = ymax;
291     h->dataBits = 8;
292     h->imType = 0;
293     /* allocate scanline */
294 greg 1.7 inl = (COLR *)emalloc(xmax*sizeof(COLR));
295 greg 1.1 /* allocate targa data */
296     tarData = taralloc(h);
297    
298 greg 2.4 return(0);
299 greg 1.1 }
300    
301    
302 schorsch 2.13 static void
303     tg2ra( /* targa file to RADIANCE file */
304     struct hdStruct *hp
305     )
306 greg 1.1 {
307     union {
308 greg 2.14 uby8 c3[256][3];
309     uby8 c4[256][4];
310 greg 1.1 } map;
311     COLR ctab[256];
312     COLR *scanline;
313 greg 2.17 int i, j;
314 greg 1.1
315     /* get color table */
316 greg 1.12 if ((hp->CMapBits==24 ? fread((char *)(map.c3+hp->mapOrig),
317     3*hp->mapLength,1,stdin) :
318     fread((char *)(map.c4+hp->mapOrig),
319     4*hp->mapLength,1,stdin)) != 1)
320 greg 1.1 quiterr("error reading color table");
321     /* convert table */
322     for (i = hp->mapOrig; i < hp->mapOrig+hp->mapLength; i++)
323     if (hp->CMapBits == 24)
324     setcolr(ctab[i],
325 greg 2.4 pow((map.c3[i][2]+.5)/256.,gamv),
326     pow((map.c3[i][1]+.5)/256.,gamv),
327     pow((map.c3[i][0]+.5)/256.,gamv));
328 greg 1.1 else
329     setcolr(ctab[i],
330 greg 2.4 pow((map.c4[i][3]+.5)/256.,gamv),
331     pow((map.c4[i][2]+.5)/256.,gamv),
332     pow((map.c4[i][1]+.5)/256.,gamv));
333 greg 1.10 if (bradj)
334     shiftcolrs(ctab, 256, bradj);
335 greg 1.1 /* allocate targa data */
336     tarData = taralloc(hp);
337     /* get data */
338     readtarga(hp, tarData, stdin);
339     /* allocate input scanline */
340     scanline = (COLR *)emalloc(xmax*sizeof(COLR));
341     /* convert file */
342     for (i = ymax-1; i >= 0; i--) {
343     for (j = 0; j < xmax; j++)
344     copycolr(scanline[j], ctab[tarData[i*xmax+j]]);
345     if (fwritecolrs(scanline, xmax, stdout) < 0)
346     quiterr("error writing RADIANCE file");
347     }
348 greg 2.10 free((void *)scanline);
349     free((void *)tarData);
350 greg 1.1 }
351    
352    
353 schorsch 2.13 static void
354     getmapped( /* read in and quantize image */
355     int nc, /* number of colors to use */
356     int dith /* use dithering? */
357     )
358 greg 1.1 {
359 greg 2.4 long fpos;
360 greg 2.17 int y;
361 greg 1.1
362 greg 2.4 setcolrgam(gamv);
363     fpos = ftell(stdin);
364 greg 2.7 if ((samplefac ? neu_init(xmax*ymax) : new_histo(xmax*ymax)) == -1)
365     quiterr("cannot initialized histogram");
366 greg 2.4 for (y = ymax-1; y >= 0; y--) {
367     if (freadcolrs(inl, xmax, stdin) < 0)
368     quiterr("error reading Radiance input");
369     if (bradj)
370     shiftcolrs(inl, xmax, bradj);
371     colrs_gambs(inl, xmax);
372 greg 2.7 if (samplefac)
373     neu_colrs(inl, xmax);
374     else
375     cnt_colrs(inl, xmax);
376 greg 1.1 }
377 greg 2.4 if (fseek(stdin, fpos, 0) == EOF)
378     quiterr("Radiance input must be from a file");
379 greg 2.7 if (samplefac) /* map colors */
380     neu_clrtab(nc);
381     else
382     new_clrtab(nc);
383 greg 2.4 for (y = ymax-1; y >= 0; y--) {
384     if (freadcolrs(inl, xmax, stdin) < 0)
385     quiterr("error reading Radiance input");
386     if (bradj)
387     shiftcolrs(inl, xmax, bradj);
388     colrs_gambs(inl, xmax);
389 greg 2.7 if (samplefac)
390     if (dith)
391     neu_dith_colrs(tarData+y*xmax, inl, xmax);
392     else
393     neu_map_colrs(tarData+y*xmax, inl, xmax);
394 greg 2.4 else
395 greg 2.7 if (dith)
396     dith_colrs(tarData+y*xmax, inl, xmax);
397     else
398     map_colrs(tarData+y*xmax, inl, xmax);
399 greg 2.4 }
400 greg 1.1 }
401    
402    
403 schorsch 2.13 static void
404     getgrey( /* read in and convert to greyscale image */
405     int nc /* number of colors to use */
406     )
407 greg 1.1 {
408 greg 2.4 int y;
409 greg 2.17 uby8 *dp;
410     int x;
411 greg 2.4
412     setcolrgam(gamv);
413     dp = tarData+xmax*ymax;;
414     for (y = ymax-1; y >= 0; y--) {
415     if (freadcolrs(inl, xmax, stdin) < 0)
416     quiterr("error reading Radiance input");
417     if (bradj)
418     shiftcolrs(inl, xmax, bradj);
419 greg 2.5 x = xmax;
420     while (x--)
421     inl[x][GRN] = normbright(inl[x]);
422 greg 2.4 colrs_gambs(inl, xmax);
423     x = xmax;
424     if (nc < 256)
425     while (x--)
426 greg 2.5 *--dp = ((long)inl[x][GRN]*nc+nc/2)>>8;
427 greg 2.4 else
428     while (x--)
429 greg 2.5 *--dp = inl[x][GRN];
430 greg 2.4 }
431     for (x = 0; x < nc; x++)
432     clrtab[x][RED] = clrtab[x][GRN] =
433 greg 2.5 clrtab[x][BLU] = ((long)x*256+128)/nc;
434 greg 1.1 }
435    
436    
437 schorsch 2.13 static void
438     writetarga( /* write out targa data */
439     struct hdStruct *h,
440 greg 2.14 uby8 *d,
441 schorsch 2.13 FILE *fp
442     )
443 greg 1.1 {
444 greg 2.17 int i, j;
445 greg 2.4
446     for (i = 0; i < h->mapLength; i++) /* write color map */
447     for (j = 2; j >= 0; j--)
448     putc(clrtab[i][j], fp);
449 greg 1.1 if (h->dataType == IM_CMAP) { /* uncompressed */
450 greg 2.14 if (fwrite((char *)d,h->x*sizeof(uby8),h->y,fp) != h->y)
451 greg 1.1 quiterr("error writing targa file");
452     return;
453     }
454     quiterr("unsupported output type");
455     }
456    
457    
458 schorsch 2.13 static void
459     readtarga( /* read in targa data */
460     struct hdStruct *h,
461 greg 2.14 uby8 *data,
462 schorsch 2.13 FILE *fp
463     )
464 greg 1.1 {
465 greg 2.17 int cnt, c;
466     uby8 *dp;
467 greg 1.1
468     if (h->dataType == IM_CMAP) { /* uncompressed */
469 greg 2.14 if (fread((char *)data,h->x*sizeof(uby8),h->y,fp) != h->y)
470 greg 1.1 goto readerr;
471     return;
472     }
473     for (dp = data; dp < data+h->x*h->y; ) {
474     if ((c = getc(fp)) == EOF)
475     goto readerr;
476     cnt = (c & 0x7f) + 1;
477     if (c & 0x80) { /* repeated pixel */
478     if ((c = getc(fp)) == EOF)
479     goto readerr;
480     while (cnt--)
481     *dp++ = c;
482     } else /* non-repeating pixels */
483     while (cnt--) {
484     if ((c = getc(fp)) == EOF)
485     goto readerr;
486     *dp++ = c;
487     }
488     }
489     return;
490     readerr:
491     quiterr("error reading targa file");
492     }