1 |
– |
/* Copyright (c) 1991 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* ra_t8.c - program to convert between RADIANCE and |
6 |
|
* Targa 8-bit color-mapped images. |
9 |
|
*/ |
10 |
|
|
11 |
|
#include <stdio.h> |
12 |
+ |
#include <string.h> |
13 |
+ |
#include <time.h> |
14 |
+ |
#include <math.h> |
15 |
|
|
16 |
+ |
#include "platform.h" |
17 |
+ |
#include "rtio.h" |
18 |
+ |
#include "rtmisc.h" |
19 |
|
#include "color.h" |
20 |
< |
|
21 |
< |
#include "pic.h" |
19 |
< |
|
20 |
> |
#include "resolu.h" |
21 |
> |
#include "clrtab.h" |
22 |
|
#include "targa.h" |
23 |
|
|
22 |
– |
#ifndef BSD |
23 |
– |
#define bcopy(s,d,n) (void)memcpy(d,s,n) |
24 |
– |
extern char *memcpy(); |
25 |
– |
#endif |
26 |
– |
/* descriptor for a picture file or frame buffer */ |
27 |
– |
typedef struct { |
28 |
– |
char *name; /* file name */ |
29 |
– |
FILE *fp; /* file pointer */ |
30 |
– |
int nexty; /* file positioning */ |
31 |
– |
int bytes_line; /* 0 == variable length lines */ |
32 |
– |
union { |
33 |
– |
long b; /* initial scanline */ |
34 |
– |
long *y; /* individual scanline */ |
35 |
– |
} pos; /* position(s) */ |
36 |
– |
} pic; |
24 |
|
|
25 |
< |
#define goodpic(h) (my_imType(h) && my_mapType(h)) |
26 |
< |
#define my_imType(h) (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \ |
25 |
> |
#define goodpic(h) (my_imType(h) && my_mapType(h)) |
26 |
> |
#define my_imType(h) (((h)->dataType==IM_CMAP || (h)->dataType==IM_CCMAP) \ |
27 |
|
&& (h)->dataBits==8 && (h)->imType==0) |
28 |
< |
#define my_mapType(h) ((h)->mapType==CM_HASMAP && \ |
28 |
> |
#define my_mapType(h) ((h)->mapType==CM_HASMAP && \ |
29 |
|
((h)->CMapBits==24 || (h)->CMapBits==32)) |
30 |
|
|
31 |
< |
#define taralloc(h) (pixel *)emalloc((h)->x*(h)->y*sizeof(pixel)) |
31 |
> |
#define taralloc(h) (uby8 *)emalloc((h)->x*(h)->y) |
32 |
|
|
33 |
< |
extern pic *openinput(); |
34 |
< |
|
35 |
< |
extern char *ecalloc(), *emalloc(); |
49 |
< |
|
50 |
< |
extern long ftell(); |
51 |
< |
|
52 |
< |
extern double atof(), pow(); |
53 |
< |
|
54 |
< |
double gamma = 2.0; /* gamma correction */ |
55 |
< |
|
33 |
> |
uby8 clrtab[256][3]; |
34 |
> |
extern int samplefac; |
35 |
> |
double gamv = 2.2; /* gamv correction */ |
36 |
|
int bradj = 0; /* brightness adjustment */ |
57 |
– |
|
58 |
– |
pic *inpic; |
59 |
– |
|
37 |
|
char *progname; |
61 |
– |
|
38 |
|
char errmsg[128]; |
63 |
– |
|
39 |
|
COLR *inl; |
40 |
< |
|
66 |
< |
pixel *tarData; |
67 |
< |
|
40 |
> |
uby8 *tarData; |
41 |
|
int xmax, ymax; |
42 |
|
|
43 |
+ |
static int getint2(FILE *fp); |
44 |
+ |
static void putint2(int i, FILE *fp); |
45 |
+ |
static void quiterr(char *err); |
46 |
+ |
static int getthead(struct hdStruct *hp, char *ip, FILE *fp); |
47 |
+ |
static int putthead(struct hdStruct *hp, char *ip, FILE *fp); |
48 |
+ |
static int getrhead(struct hdStruct *h, FILE *fp); |
49 |
+ |
static void tg2ra(struct hdStruct *hp); |
50 |
+ |
static void getmapped(int nc, int dith); |
51 |
+ |
static void getgrey(int nc); |
52 |
+ |
static void writetarga(struct hdStruct *h, uby8 *d, FILE *fp); |
53 |
+ |
static void readtarga(struct hdStruct *h, uby8 *data, FILE *fp); |
54 |
|
|
55 |
< |
main(argc, argv) |
56 |
< |
int argc; |
57 |
< |
char *argv[]; |
55 |
> |
|
56 |
> |
int |
57 |
> |
main( |
58 |
> |
int argc, |
59 |
> |
char *argv[] |
60 |
> |
) |
61 |
|
{ |
62 |
< |
colormap rasmap; |
76 |
< |
struct hdStruct head; |
62 |
> |
struct hdStruct head; |
63 |
|
int dither = 1; |
64 |
|
int reverse = 0; |
65 |
|
int ncolors = 256; |
66 |
|
int greyscale = 0; |
67 |
|
int i; |
68 |
< |
|
68 |
> |
SET_DEFAULT_BINARY(); |
69 |
> |
SET_FILE_BINARY(stdin); |
70 |
> |
SET_FILE_BINARY(stdout); |
71 |
|
progname = argv[0]; |
72 |
+ |
samplefac = 0; |
73 |
|
|
74 |
|
for (i = 1; i < argc; i++) |
75 |
|
if (argv[i][0] == '-') |
78 |
|
dither = !dither; |
79 |
|
break; |
80 |
|
case 'g': |
81 |
< |
gamma = atof(argv[++i]); |
81 |
> |
gamv = atof(argv[++i]); |
82 |
|
break; |
83 |
|
case 'r': |
84 |
|
reverse = !reverse; |
94 |
|
case 'c': |
95 |
|
ncolors = atoi(argv[++i]); |
96 |
|
break; |
97 |
+ |
case 'n': |
98 |
+ |
samplefac = atoi(argv[++i]); |
99 |
+ |
break; |
100 |
|
default: |
101 |
|
goto userr; |
102 |
|
} |
103 |
|
else |
104 |
|
break; |
105 |
|
|
106 |
+ |
if (i < argc-2) |
107 |
+ |
goto userr; |
108 |
+ |
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
109 |
+ |
sprintf(errmsg, "cannot open input \"%s\"", argv[i]); |
110 |
+ |
quiterr(errmsg); |
111 |
+ |
} |
112 |
+ |
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
113 |
+ |
sprintf(errmsg, "cannot open output \"%s\"", argv[i+1]); |
114 |
+ |
quiterr(errmsg); |
115 |
+ |
} |
116 |
|
if (reverse) { |
115 |
– |
if (i < argc-2) |
116 |
– |
goto userr; |
117 |
– |
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
118 |
– |
sprintf(errmsg, "can't open input \"%s\"", argv[i]); |
119 |
– |
quiterr(errmsg); |
120 |
– |
} |
117 |
|
/* get header */ |
118 |
|
if (getthead(&head, NULL, stdin) < 0) |
119 |
|
quiterr("bad targa file"); |
121 |
|
quiterr("incompatible format"); |
122 |
|
xmax = head.x; |
123 |
|
ymax = head.y; |
128 |
– |
/* open output file */ |
129 |
– |
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
130 |
– |
sprintf(errmsg, "can't open output \"%s\"", argv[i+1]); |
131 |
– |
quiterr(errmsg); |
132 |
– |
} |
124 |
|
/* put header */ |
125 |
+ |
newheader("RADIANCE", stdout); |
126 |
|
printargs(i, argv, stdout); |
127 |
|
fputformat(COLRFMT, stdout); |
128 |
|
putchar('\n'); |
129 |
< |
fputresolu(YMAJOR|YDECR, xmax, ymax, stdout); |
129 |
> |
fprtresolu(xmax, ymax, stdout); |
130 |
|
/* convert file */ |
131 |
|
tg2ra(&head); |
132 |
|
} else { |
133 |
< |
if (i < argc-2 || (!greyscale && i > argc-1)) |
134 |
< |
goto userr; |
143 |
< |
if ((inpic = openinput(argv[i], &head)) == NULL) { |
144 |
< |
sprintf(errmsg, "can't open input \"%s\"", argv[i]); |
145 |
< |
quiterr(errmsg); |
146 |
< |
} |
147 |
< |
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
148 |
< |
sprintf(errmsg, "can't open output \"%s\"", argv[i+1]); |
149 |
< |
quiterr(errmsg); |
150 |
< |
} |
133 |
> |
if (getrhead(&head, stdin) < 0) |
134 |
> |
quiterr("bad Radiance input"); |
135 |
|
/* write header */ |
136 |
|
putthead(&head, NULL, stdout); |
137 |
|
/* convert file */ |
138 |
|
if (greyscale) |
139 |
< |
biq(dither,ncolors,1,rasmap); |
139 |
> |
getgrey(ncolors); |
140 |
|
else |
141 |
< |
ciq(dither,ncolors,1,rasmap); |
141 |
> |
getmapped(ncolors, dither); |
142 |
|
/* write data */ |
143 |
|
writetarga(&head, tarData, stdout); |
144 |
|
} |
145 |
|
quiterr(NULL); |
146 |
|
userr: |
147 |
|
fprintf(stderr, |
148 |
< |
"Usage: %s [-d][-c ncolors][-b][-g gamma][-e +/-stops] input [output]\n", |
148 |
> |
"Usage: %s [-d][-n samp][-c ncolors][-b][-g gamv][-e +/-stops] input [output]\n", |
149 |
|
progname); |
150 |
< |
fprintf(stderr, " Or: %s -r [-g gamma][-e +/-stops] [input [output]]\n", |
150 |
> |
fprintf(stderr, " Or: %s -r [-g gamv][-e +/-stops] [input [output]]\n", |
151 |
|
progname); |
152 |
|
exit(1); |
153 |
|
} |
154 |
|
|
155 |
|
|
156 |
< |
int |
157 |
< |
getint2(fp) /* get a 2-byte positive integer */ |
158 |
< |
register FILE *fp; |
156 |
> |
static int |
157 |
> |
getint2( /* get a 2-byte positive integer */ |
158 |
> |
register FILE *fp |
159 |
> |
) |
160 |
|
{ |
161 |
|
register int b1, b2; |
162 |
|
|
167 |
|
} |
168 |
|
|
169 |
|
|
170 |
< |
putint2(i, fp) /* put a 2-byte positive integer */ |
171 |
< |
register int i; |
172 |
< |
register FILE *fp; |
170 |
> |
static void |
171 |
> |
putint2( /* put a 2-byte positive integer */ |
172 |
> |
register int i, |
173 |
> |
register FILE *fp |
174 |
> |
) |
175 |
|
{ |
176 |
|
putc(i&0xff, fp); |
177 |
|
putc(i>>8&0xff, fp); |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
< |
quiterr(err) /* print message and exit */ |
182 |
< |
char *err; |
181 |
> |
static void |
182 |
> |
quiterr( /* print message and exit */ |
183 |
> |
char *err |
184 |
> |
) |
185 |
|
{ |
186 |
|
if (err != NULL) { |
187 |
|
fprintf(stderr, "%s: %s\n", progname, err); |
191 |
|
} |
192 |
|
|
193 |
|
|
194 |
+ |
void |
195 |
|
eputs(s) |
196 |
|
char *s; |
197 |
|
{ |
199 |
|
} |
200 |
|
|
201 |
|
|
202 |
+ |
void |
203 |
|
quit(code) |
204 |
|
int code; |
205 |
|
{ |
207 |
|
} |
208 |
|
|
209 |
|
|
210 |
< |
getthead(hp, ip, fp) /* read header from input */ |
211 |
< |
struct hdStruct *hp; |
212 |
< |
char *ip; |
213 |
< |
register FILE *fp; |
210 |
> |
static int |
211 |
> |
getthead( /* read header from input */ |
212 |
> |
struct hdStruct *hp, |
213 |
> |
char *ip, |
214 |
> |
register FILE *fp |
215 |
> |
) |
216 |
|
{ |
217 |
|
int nidbytes; |
218 |
|
|
242 |
|
} |
243 |
|
|
244 |
|
|
245 |
< |
putthead(hp, ip, fp) /* write header to output */ |
246 |
< |
struct hdStruct *hp; |
247 |
< |
char *ip; |
248 |
< |
register FILE *fp; |
245 |
> |
static int |
246 |
> |
putthead( /* write header to output */ |
247 |
> |
struct hdStruct *hp, |
248 |
> |
char *ip, |
249 |
> |
register FILE *fp |
250 |
> |
) |
251 |
|
{ |
252 |
|
if (ip != NULL) |
253 |
|
putc(strlen(ip), fp); |
272 |
|
} |
273 |
|
|
274 |
|
|
275 |
< |
pic * |
276 |
< |
openinput(fname, h) /* open RADIANCE input file */ |
277 |
< |
char *fname; |
278 |
< |
register struct hdStruct *h; |
275 |
> |
static int |
276 |
> |
getrhead( /* load RADIANCE input file header */ |
277 |
> |
register struct hdStruct *h, |
278 |
> |
FILE *fp |
279 |
> |
) |
280 |
|
{ |
285 |
– |
register pic *p; |
286 |
– |
|
287 |
– |
p = (pic *)emalloc(sizeof(pic)); |
288 |
– |
p->name = fname; |
289 |
– |
if (fname == NULL) |
290 |
– |
p->fp = stdin; |
291 |
– |
else if ((p->fp = fopen(fname, "r")) == NULL) |
292 |
– |
return(NULL); |
281 |
|
/* get header info. */ |
282 |
< |
if (checkheader(p->fp, COLRFMT, NULL) < 0 || |
283 |
< |
fgetresolu(&xmax, &ymax, p->fp) != (YMAJOR|YDECR)) |
284 |
< |
quiterr("bad picture format"); |
297 |
< |
p->nexty = 0; |
298 |
< |
p->bytes_line = 0; /* variable length lines */ |
299 |
< |
p->pos.y = (long *)ecalloc(ymax, sizeof(long)); |
300 |
< |
p->pos.y[0] = ftell(p->fp); |
282 |
> |
if (checkheader(fp, COLRFMT, NULL) < 0 || |
283 |
> |
fgetresolu(&xmax, &ymax, fp) < 0) |
284 |
> |
return(-1); |
285 |
|
/* assign header */ |
286 |
|
h->textSize = 0; |
287 |
|
h->mapType = CM_HASMAP; |
300 |
|
/* allocate targa data */ |
301 |
|
tarData = taralloc(h); |
302 |
|
|
303 |
< |
return(p); |
303 |
> |
return(0); |
304 |
|
} |
305 |
|
|
306 |
|
|
307 |
< |
tg2ra(hp) /* targa file to RADIANCE file */ |
308 |
< |
struct hdStruct *hp; |
307 |
> |
static void |
308 |
> |
tg2ra( /* targa file to RADIANCE file */ |
309 |
> |
struct hdStruct *hp |
310 |
> |
) |
311 |
|
{ |
312 |
|
union { |
313 |
< |
BYTE c3[256][3]; |
314 |
< |
BYTE c4[256][4]; |
313 |
> |
uby8 c3[256][3]; |
314 |
> |
uby8 c4[256][4]; |
315 |
|
} map; |
316 |
|
COLR ctab[256]; |
317 |
|
COLR *scanline; |
318 |
|
register int i, j; |
319 |
|
|
320 |
|
/* get color table */ |
321 |
< |
if ((hp->CMapBits==24 ? fread((char *)map.c3,sizeof(map.c3),1,stdin) : |
322 |
< |
fread((char *)map.c4,sizeof(map.c4),1,stdin)) != 1) |
321 |
> |
if ((hp->CMapBits==24 ? fread((char *)(map.c3+hp->mapOrig), |
322 |
> |
3*hp->mapLength,1,stdin) : |
323 |
> |
fread((char *)(map.c4+hp->mapOrig), |
324 |
> |
4*hp->mapLength,1,stdin)) != 1) |
325 |
|
quiterr("error reading color table"); |
326 |
|
/* convert table */ |
327 |
|
for (i = hp->mapOrig; i < hp->mapOrig+hp->mapLength; i++) |
328 |
|
if (hp->CMapBits == 24) |
329 |
|
setcolr(ctab[i], |
330 |
< |
pow((map.c3[i][2]+.5)/256.,gamma), |
331 |
< |
pow((map.c3[i][1]+.5)/256.,gamma), |
332 |
< |
pow((map.c3[i][0]+.5)/256.,gamma)); |
330 |
> |
pow((map.c3[i][2]+.5)/256.,gamv), |
331 |
> |
pow((map.c3[i][1]+.5)/256.,gamv), |
332 |
> |
pow((map.c3[i][0]+.5)/256.,gamv)); |
333 |
|
else |
334 |
|
setcolr(ctab[i], |
335 |
< |
pow((map.c4[i][3]+.5)/256.,gamma), |
336 |
< |
pow((map.c4[i][2]+.5)/256.,gamma), |
337 |
< |
pow((map.c4[i][1]+.5)/256.,gamma)); |
335 |
> |
pow((map.c4[i][3]+.5)/256.,gamv), |
336 |
> |
pow((map.c4[i][2]+.5)/256.,gamv), |
337 |
> |
pow((map.c4[i][1]+.5)/256.,gamv)); |
338 |
|
if (bradj) |
339 |
|
shiftcolrs(ctab, 256, bradj); |
340 |
|
/* allocate targa data */ |
350 |
|
if (fwritecolrs(scanline, xmax, stdout) < 0) |
351 |
|
quiterr("error writing RADIANCE file"); |
352 |
|
} |
353 |
< |
free((char *)scanline); |
354 |
< |
free((char *)tarData); |
353 |
> |
free((void *)scanline); |
354 |
> |
free((void *)tarData); |
355 |
|
} |
356 |
|
|
357 |
|
|
358 |
< |
picreadline3(y, l3) /* read in 3-byte scanline */ |
359 |
< |
int y; |
360 |
< |
register rgbpixel *l3; |
358 |
> |
static void |
359 |
> |
getmapped( /* read in and quantize image */ |
360 |
> |
int nc, /* number of colors to use */ |
361 |
> |
int dith /* use dithering? */ |
362 |
> |
) |
363 |
|
{ |
364 |
< |
register int i; |
364 |
> |
long fpos; |
365 |
> |
register int y; |
366 |
|
|
367 |
< |
if (inpic->nexty != y) { /* find scanline */ |
368 |
< |
if (inpic->bytes_line == 0) { |
369 |
< |
if (inpic->pos.y[y] == 0) { |
370 |
< |
while (inpic->nexty < y) { |
371 |
< |
if (freadcolrs(inl, xmax, inpic->fp) < 0) |
372 |
< |
quiterr("read error in picreadline3"); |
373 |
< |
inpic->pos.y[++inpic->nexty] = ftell(inpic->fp); |
374 |
< |
} |
375 |
< |
} else if (fseek(inpic->fp, inpic->pos.y[y], 0) == EOF) |
376 |
< |
quiterr("seek error in picreadline3"); |
377 |
< |
} else if (fseek(inpic->fp, y*inpic->bytes_line+inpic->pos.b, 0) == EOF) |
378 |
< |
quiterr("seek error in picreadline3"); |
379 |
< |
} else if (inpic->bytes_line == 0 && inpic->pos.y[inpic->nexty] == 0) |
380 |
< |
inpic->pos.y[inpic->nexty] = ftell(inpic->fp); |
390 |
< |
if (freadcolrs(inl, xmax, inpic->fp) < 0) /* read scanline */ |
391 |
< |
quiterr("read error in picreadline3"); |
392 |
< |
inpic->nexty = y+1; |
393 |
< |
/* convert scanline */ |
394 |
< |
normcolrs(inl, xmax, bradj); |
395 |
< |
for (i = 0; i < xmax; i++) { |
396 |
< |
l3[i].r = inl[i][RED]; |
397 |
< |
l3[i].g = inl[i][GRN]; |
398 |
< |
l3[i].b = inl[i][BLU]; |
367 |
> |
setcolrgam(gamv); |
368 |
> |
fpos = ftell(stdin); |
369 |
> |
if ((samplefac ? neu_init(xmax*ymax) : new_histo(xmax*ymax)) == -1) |
370 |
> |
quiterr("cannot initialized histogram"); |
371 |
> |
for (y = ymax-1; y >= 0; y--) { |
372 |
> |
if (freadcolrs(inl, xmax, stdin) < 0) |
373 |
> |
quiterr("error reading Radiance input"); |
374 |
> |
if (bradj) |
375 |
> |
shiftcolrs(inl, xmax, bradj); |
376 |
> |
colrs_gambs(inl, xmax); |
377 |
> |
if (samplefac) |
378 |
> |
neu_colrs(inl, xmax); |
379 |
> |
else |
380 |
> |
cnt_colrs(inl, xmax); |
381 |
|
} |
382 |
+ |
if (fseek(stdin, fpos, 0) == EOF) |
383 |
+ |
quiterr("Radiance input must be from a file"); |
384 |
+ |
if (samplefac) /* map colors */ |
385 |
+ |
neu_clrtab(nc); |
386 |
+ |
else |
387 |
+ |
new_clrtab(nc); |
388 |
+ |
for (y = ymax-1; y >= 0; y--) { |
389 |
+ |
if (freadcolrs(inl, xmax, stdin) < 0) |
390 |
+ |
quiterr("error reading Radiance input"); |
391 |
+ |
if (bradj) |
392 |
+ |
shiftcolrs(inl, xmax, bradj); |
393 |
+ |
colrs_gambs(inl, xmax); |
394 |
+ |
if (samplefac) |
395 |
+ |
if (dith) |
396 |
+ |
neu_dith_colrs(tarData+y*xmax, inl, xmax); |
397 |
+ |
else |
398 |
+ |
neu_map_colrs(tarData+y*xmax, inl, xmax); |
399 |
+ |
else |
400 |
+ |
if (dith) |
401 |
+ |
dith_colrs(tarData+y*xmax, inl, xmax); |
402 |
+ |
else |
403 |
+ |
map_colrs(tarData+y*xmax, inl, xmax); |
404 |
+ |
} |
405 |
|
} |
406 |
|
|
407 |
|
|
408 |
< |
picwriteline(y, l) /* save output scanline */ |
409 |
< |
int y; |
410 |
< |
pixel *l; |
408 |
> |
static void |
409 |
> |
getgrey( /* read in and convert to greyscale image */ |
410 |
> |
int nc /* number of colors to use */ |
411 |
> |
) |
412 |
|
{ |
413 |
< |
bcopy((char *)l, (char *)&tarData[(ymax-1-y)*xmax], xmax*sizeof(pixel)); |
413 |
> |
int y; |
414 |
> |
register uby8 *dp; |
415 |
> |
register int x; |
416 |
> |
|
417 |
> |
setcolrgam(gamv); |
418 |
> |
dp = tarData+xmax*ymax;; |
419 |
> |
for (y = ymax-1; y >= 0; y--) { |
420 |
> |
if (freadcolrs(inl, xmax, stdin) < 0) |
421 |
> |
quiterr("error reading Radiance input"); |
422 |
> |
if (bradj) |
423 |
> |
shiftcolrs(inl, xmax, bradj); |
424 |
> |
x = xmax; |
425 |
> |
while (x--) |
426 |
> |
inl[x][GRN] = normbright(inl[x]); |
427 |
> |
colrs_gambs(inl, xmax); |
428 |
> |
x = xmax; |
429 |
> |
if (nc < 256) |
430 |
> |
while (x--) |
431 |
> |
*--dp = ((long)inl[x][GRN]*nc+nc/2)>>8; |
432 |
> |
else |
433 |
> |
while (x--) |
434 |
> |
*--dp = inl[x][GRN]; |
435 |
> |
} |
436 |
> |
for (x = 0; x < nc; x++) |
437 |
> |
clrtab[x][RED] = clrtab[x][GRN] = |
438 |
> |
clrtab[x][BLU] = ((long)x*256+128)/nc; |
439 |
|
} |
440 |
|
|
441 |
|
|
442 |
< |
writetarga(h, d, fp) /* write out targa data */ |
443 |
< |
struct hdStruct *h; |
444 |
< |
pixel *d; |
445 |
< |
FILE *fp; |
442 |
> |
static void |
443 |
> |
writetarga( /* write out targa data */ |
444 |
> |
struct hdStruct *h, |
445 |
> |
uby8 *d, |
446 |
> |
FILE *fp |
447 |
> |
) |
448 |
|
{ |
449 |
+ |
register int i, j; |
450 |
+ |
|
451 |
+ |
for (i = 0; i < h->mapLength; i++) /* write color map */ |
452 |
+ |
for (j = 2; j >= 0; j--) |
453 |
+ |
putc(clrtab[i][j], fp); |
454 |
|
if (h->dataType == IM_CMAP) { /* uncompressed */ |
455 |
< |
if (fwrite((char *)d,h->x*sizeof(pixel),h->y,fp) != h->y) |
455 |
> |
if (fwrite((char *)d,h->x*sizeof(uby8),h->y,fp) != h->y) |
456 |
|
quiterr("error writing targa file"); |
457 |
|
return; |
458 |
|
} |
460 |
|
} |
461 |
|
|
462 |
|
|
463 |
< |
readtarga(h, data, fp) /* read in targa data */ |
464 |
< |
struct hdStruct *h; |
465 |
< |
pixel *data; |
466 |
< |
FILE *fp; |
463 |
> |
static void |
464 |
> |
readtarga( /* read in targa data */ |
465 |
> |
struct hdStruct *h, |
466 |
> |
uby8 *data, |
467 |
> |
FILE *fp |
468 |
> |
) |
469 |
|
{ |
470 |
|
register int cnt, c; |
471 |
< |
register pixel *dp; |
471 |
> |
register uby8 *dp; |
472 |
|
|
473 |
|
if (h->dataType == IM_CMAP) { /* uncompressed */ |
474 |
< |
if (fread((char *)data,h->x*sizeof(pixel),h->y,fp) != h->y) |
474 |
> |
if (fread((char *)data,h->x*sizeof(uby8),h->y,fp) != h->y) |
475 |
|
goto readerr; |
476 |
|
return; |
477 |
|
} |
494 |
|
return; |
495 |
|
readerr: |
496 |
|
quiterr("error reading targa file"); |
457 |
– |
} |
458 |
– |
|
459 |
– |
|
460 |
– |
picwritecm(cm) /* write out color map */ |
461 |
– |
colormap cm; |
462 |
– |
{ |
463 |
– |
register int i, j; |
464 |
– |
|
465 |
– |
for (j = 0; j < 256; j++) |
466 |
– |
for (i = 2; i >= 0; i--) |
467 |
– |
putc(cm[i][j], stdout); |
468 |
– |
} |
469 |
– |
|
470 |
– |
|
471 |
– |
picreadcm(map) /* do gamma correction if requested */ |
472 |
– |
colormap map; |
473 |
– |
{ |
474 |
– |
register int i, val; |
475 |
– |
|
476 |
– |
for (i = 0; i < 256; i++) { |
477 |
– |
val = pow((i+0.5)/256.0, 1.0/gamma) * 256.0; |
478 |
– |
map[0][i] = map[1][i] = map[2][i] = val; |
479 |
– |
} |
497 |
|
} |