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

# Content
1 #ifndef lint
2 static const char RCSid[] = "$Id: ra_t8.c,v 2.15 2018/03/20 18:45:04 greg Exp $";
3 #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 <math.h>
12
13 #include "platform.h"
14 #include "rtio.h"
15 #include "rtmisc.h"
16 #include "color.h"
17 #include "resolu.h"
18 #include "clrtab.h"
19 #include "targa.h"
20
21
22 #define goodpic(h) (my_imType(h) && my_mapType(h))
23 #define my_imType(h) (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \
24 && (h)->dataBits==8 && (h)->imType==0)
25 #define my_mapType(h) ((h)->mapType==CM_HASMAP && \
26 ((h)->CMapBits==24 || (h)->CMapBits==32))
27
28 #define taralloc(h) (uby8 *)emalloc((h)->x*(h)->y)
29
30 uby8 clrtab[256][3];
31 extern int samplefac;
32 double gamv = 2.2; /* gamv correction */
33 int bradj = 0; /* brightness adjustment */
34 char *progname;
35 char errmsg[128];
36 COLR *inl;
37 uby8 *tarData;
38 int xmax, ymax;
39
40 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 static void writetarga(struct hdStruct *h, uby8 *d, FILE *fp);
50 static void readtarga(struct hdStruct *h, uby8 *data, FILE *fp);
51
52
53 int
54 main(
55 int argc,
56 char *argv[]
57 )
58 {
59 struct hdStruct head;
60 int dither = 1;
61 int reverse = 0;
62 int ncolors = 256;
63 int greyscale = 0;
64 int i;
65 SET_DEFAULT_BINARY();
66 SET_FILE_BINARY(stdin);
67 SET_FILE_BINARY(stdout);
68 progname = argv[0];
69 samplefac = 0;
70
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 gamv = atof(argv[++i]);
79 break;
80 case 'r':
81 reverse = !reverse;
82 break;
83 case 'b':
84 greyscale = 1;
85 break;
86 case 'e':
87 if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
88 goto userr;
89 bradj = atoi(argv[++i]);
90 break;
91 case 'c':
92 ncolors = atoi(argv[++i]);
93 break;
94 case 'n':
95 samplefac = atoi(argv[++i]);
96 break;
97 default:
98 goto userr;
99 }
100 else
101 break;
102
103 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 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 newheader("RADIANCE", stdout);
123 printargs(i, argv, stdout);
124 fputformat(COLRFMT, stdout);
125 putchar('\n');
126 fprtresolu(xmax, ymax, stdout);
127 /* convert file */
128 tg2ra(&head);
129 } else {
130 if (getrhead(&head, stdin) < 0)
131 quiterr("bad Radiance input");
132 /* write header */
133 putthead(&head, NULL, stdout);
134 /* convert file */
135 if (greyscale)
136 getgrey(ncolors);
137 else
138 getmapped(ncolors, dither);
139 /* write data */
140 writetarga(&head, tarData, stdout);
141 }
142 quiterr(NULL);
143 userr:
144 fprintf(stderr,
145 "Usage: %s [-d][-n samp][-c ncolors][-b][-g gamv][-e +/-stops] input [output]\n",
146 progname);
147 fprintf(stderr, " Or: %s -r [-g gamv][-e +/-stops] [input [output]]\n",
148 progname);
149 exit(1);
150 }
151
152
153 static int
154 getint2( /* get a 2-byte positive integer */
155 register FILE *fp
156 )
157 {
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 static void
168 putint2( /* put a 2-byte positive integer */
169 register int i,
170 register FILE *fp
171 )
172 {
173 putc(i&0xff, fp);
174 putc(i>>8&0xff, fp);
175 }
176
177
178 static void
179 quiterr( /* print message and exit */
180 char *err
181 )
182 {
183 if (err != NULL) {
184 fprintf(stderr, "%s: %s\n", progname, err);
185 exit(1);
186 }
187 exit(0);
188 }
189
190
191 void
192 eputs(s)
193 char *s;
194 {
195 fputs(s, stderr);
196 }
197
198
199 void
200 quit(code)
201 int code;
202 {
203 exit(code);
204 }
205
206
207 static int
208 getthead( /* read header from input */
209 struct hdStruct *hp,
210 char *ip,
211 register FILE *fp
212 )
213 {
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 fread((char *)ip, nidbytes, 1, fp);
233 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 static int
243 putthead( /* write header to output */
244 struct hdStruct *hp,
245 char *ip,
246 register FILE *fp
247 )
248 {
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 static int
273 getrhead( /* load RADIANCE input file header */
274 register struct hdStruct *h,
275 FILE *fp
276 )
277 {
278 /* get header info. */
279 if (checkheader(fp, COLRFMT, NULL) < 0 ||
280 fgetresolu(&xmax, &ymax, fp) < 0)
281 return(-1);
282 /* 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 inl = (COLR *)emalloc(xmax*sizeof(COLR));
297 /* allocate targa data */
298 tarData = taralloc(h);
299
300 return(0);
301 }
302
303
304 static void
305 tg2ra( /* targa file to RADIANCE file */
306 struct hdStruct *hp
307 )
308 {
309 union {
310 uby8 c3[256][3];
311 uby8 c4[256][4];
312 } map;
313 COLR ctab[256];
314 COLR *scanline;
315 register int i, j;
316
317 /* get color table */
318 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 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 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 else
331 setcolr(ctab[i],
332 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 if (bradj)
336 shiftcolrs(ctab, 256, bradj);
337 /* 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 free((void *)scanline);
351 free((void *)tarData);
352 }
353
354
355 static void
356 getmapped( /* read in and quantize image */
357 int nc, /* number of colors to use */
358 int dith /* use dithering? */
359 )
360 {
361 long fpos;
362 register int y;
363
364 setcolrgam(gamv);
365 fpos = ftell(stdin);
366 if ((samplefac ? neu_init(xmax*ymax) : new_histo(xmax*ymax)) == -1)
367 quiterr("cannot initialized histogram");
368 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 if (samplefac)
375 neu_colrs(inl, xmax);
376 else
377 cnt_colrs(inl, xmax);
378 }
379 if (fseek(stdin, fpos, 0) == EOF)
380 quiterr("Radiance input must be from a file");
381 if (samplefac) /* map colors */
382 neu_clrtab(nc);
383 else
384 new_clrtab(nc);
385 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 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 else
397 if (dith)
398 dith_colrs(tarData+y*xmax, inl, xmax);
399 else
400 map_colrs(tarData+y*xmax, inl, xmax);
401 }
402 }
403
404
405 static void
406 getgrey( /* read in and convert to greyscale image */
407 int nc /* number of colors to use */
408 )
409 {
410 int y;
411 register uby8 *dp;
412 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 x = xmax;
422 while (x--)
423 inl[x][GRN] = normbright(inl[x]);
424 colrs_gambs(inl, xmax);
425 x = xmax;
426 if (nc < 256)
427 while (x--)
428 *--dp = ((long)inl[x][GRN]*nc+nc/2)>>8;
429 else
430 while (x--)
431 *--dp = inl[x][GRN];
432 }
433 for (x = 0; x < nc; x++)
434 clrtab[x][RED] = clrtab[x][GRN] =
435 clrtab[x][BLU] = ((long)x*256+128)/nc;
436 }
437
438
439 static void
440 writetarga( /* write out targa data */
441 struct hdStruct *h,
442 uby8 *d,
443 FILE *fp
444 )
445 {
446 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 if (h->dataType == IM_CMAP) { /* uncompressed */
452 if (fwrite((char *)d,h->x*sizeof(uby8),h->y,fp) != h->y)
453 quiterr("error writing targa file");
454 return;
455 }
456 quiterr("unsupported output type");
457 }
458
459
460 static void
461 readtarga( /* read in targa data */
462 struct hdStruct *h,
463 uby8 *data,
464 FILE *fp
465 )
466 {
467 register int cnt, c;
468 register uby8 *dp;
469
470 if (h->dataType == IM_CMAP) { /* uncompressed */
471 if (fread((char *)data,h->x*sizeof(uby8),h->y,fp) != h->y)
472 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 }