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, 4 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

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