1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: ra_t8.c,v 2.16 2019/12/28 18:05:14 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 |
FILE *fp |
156 |
) |
157 |
{ |
158 |
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 |
int i, |
170 |
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(const char *s) |
193 |
{ |
194 |
fputs(s, stderr); |
195 |
} |
196 |
|
197 |
|
198 |
void |
199 |
quit(int code) |
200 |
{ |
201 |
exit(code); |
202 |
} |
203 |
|
204 |
|
205 |
static int |
206 |
getthead( /* read header from input */ |
207 |
struct hdStruct *hp, |
208 |
char *ip, |
209 |
FILE *fp |
210 |
) |
211 |
{ |
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 |
fread((char *)ip, nidbytes, 1, fp); |
231 |
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 |
static int |
241 |
putthead( /* write header to output */ |
242 |
struct hdStruct *hp, |
243 |
char *ip, |
244 |
FILE *fp |
245 |
) |
246 |
{ |
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 |
static int |
271 |
getrhead( /* load RADIANCE input file header */ |
272 |
struct hdStruct *h, |
273 |
FILE *fp |
274 |
) |
275 |
{ |
276 |
/* get header info. */ |
277 |
if (checkheader(fp, COLRFMT, NULL) < 0 || |
278 |
fgetresolu(&xmax, &ymax, fp) < 0) |
279 |
return(-1); |
280 |
/* 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 |
inl = (COLR *)emalloc(xmax*sizeof(COLR)); |
295 |
/* allocate targa data */ |
296 |
tarData = taralloc(h); |
297 |
|
298 |
return(0); |
299 |
} |
300 |
|
301 |
|
302 |
static void |
303 |
tg2ra( /* targa file to RADIANCE file */ |
304 |
struct hdStruct *hp |
305 |
) |
306 |
{ |
307 |
union { |
308 |
uby8 c3[256][3]; |
309 |
uby8 c4[256][4]; |
310 |
} map; |
311 |
COLR ctab[256]; |
312 |
COLR *scanline; |
313 |
int i, j; |
314 |
|
315 |
/* get color table */ |
316 |
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 |
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 |
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 |
else |
329 |
setcolr(ctab[i], |
330 |
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 |
if (bradj) |
334 |
shiftcolrs(ctab, 256, bradj); |
335 |
/* 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 |
free((void *)scanline); |
349 |
free((void *)tarData); |
350 |
} |
351 |
|
352 |
|
353 |
static void |
354 |
getmapped( /* read in and quantize image */ |
355 |
int nc, /* number of colors to use */ |
356 |
int dith /* use dithering? */ |
357 |
) |
358 |
{ |
359 |
long fpos; |
360 |
int y; |
361 |
|
362 |
setcolrgam(gamv); |
363 |
fpos = ftell(stdin); |
364 |
if ((samplefac ? neu_init(xmax*ymax) : new_histo(xmax*ymax)) == -1) |
365 |
quiterr("cannot initialized histogram"); |
366 |
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 |
if (samplefac) |
373 |
neu_colrs(inl, xmax); |
374 |
else |
375 |
cnt_colrs(inl, xmax); |
376 |
} |
377 |
if (fseek(stdin, fpos, 0) == EOF) |
378 |
quiterr("Radiance input must be from a file"); |
379 |
if (samplefac) /* map colors */ |
380 |
neu_clrtab(nc); |
381 |
else |
382 |
new_clrtab(nc); |
383 |
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 |
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 |
else |
395 |
if (dith) |
396 |
dith_colrs(tarData+y*xmax, inl, xmax); |
397 |
else |
398 |
map_colrs(tarData+y*xmax, inl, xmax); |
399 |
} |
400 |
} |
401 |
|
402 |
|
403 |
static void |
404 |
getgrey( /* read in and convert to greyscale image */ |
405 |
int nc /* number of colors to use */ |
406 |
) |
407 |
{ |
408 |
int y; |
409 |
uby8 *dp; |
410 |
int x; |
411 |
|
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 |
x = xmax; |
420 |
while (x--) |
421 |
inl[x][GRN] = normbright(inl[x]); |
422 |
colrs_gambs(inl, xmax); |
423 |
x = xmax; |
424 |
if (nc < 256) |
425 |
while (x--) |
426 |
*--dp = ((long)inl[x][GRN]*nc+nc/2)>>8; |
427 |
else |
428 |
while (x--) |
429 |
*--dp = inl[x][GRN]; |
430 |
} |
431 |
for (x = 0; x < nc; x++) |
432 |
clrtab[x][RED] = clrtab[x][GRN] = |
433 |
clrtab[x][BLU] = ((long)x*256+128)/nc; |
434 |
} |
435 |
|
436 |
|
437 |
static void |
438 |
writetarga( /* write out targa data */ |
439 |
struct hdStruct *h, |
440 |
uby8 *d, |
441 |
FILE *fp |
442 |
) |
443 |
{ |
444 |
int i, j; |
445 |
|
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 |
if (h->dataType == IM_CMAP) { /* uncompressed */ |
450 |
if (fwrite((char *)d,h->x*sizeof(uby8),h->y,fp) != h->y) |
451 |
quiterr("error writing targa file"); |
452 |
return; |
453 |
} |
454 |
quiterr("unsupported output type"); |
455 |
} |
456 |
|
457 |
|
458 |
static void |
459 |
readtarga( /* read in targa data */ |
460 |
struct hdStruct *h, |
461 |
uby8 *data, |
462 |
FILE *fp |
463 |
) |
464 |
{ |
465 |
int cnt, c; |
466 |
uby8 *dp; |
467 |
|
468 |
if (h->dataType == IM_CMAP) { /* uncompressed */ |
469 |
if (fread((char *)data,h->x*sizeof(uby8),h->y,fp) != h->y) |
470 |
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 |
} |