| 1 |
greg |
2.1 |
#ifndef lint |
| 2 |
schorsch |
2.11 |
static const char RCSid[] = "$Id: ra_avs.c,v 2.10 2003/06/05 19:29:34 schorsch Exp $"; |
| 3 |
greg |
2.1 |
#endif |
| 4 |
|
|
/* |
| 5 |
|
|
* Convert Radiance file to/from AVS file. |
| 6 |
|
|
*/ |
| 7 |
|
|
|
| 8 |
|
|
#include <stdio.h> |
| 9 |
greg |
2.3 |
#include <math.h> |
| 10 |
greg |
2.9 |
#include <time.h> |
| 11 |
schorsch |
2.10 |
|
| 12 |
|
|
#include "platform.h" |
| 13 |
schorsch |
2.11 |
#include "rtio.h" |
| 14 |
greg |
2.1 |
#include "color.h" |
| 15 |
|
|
#include "resolu.h" |
| 16 |
|
|
|
| 17 |
greg |
2.4 |
double gamcor = 2.2; /* gamma correction */ |
| 18 |
greg |
2.1 |
|
| 19 |
|
|
int bradj = 0; /* brightness adjustment */ |
| 20 |
|
|
|
| 21 |
|
|
char *progname; |
| 22 |
|
|
|
| 23 |
|
|
int xmax, ymax; |
| 24 |
|
|
|
| 25 |
schorsch |
2.11 |
static void quiterr(char *err); |
| 26 |
|
|
static void avs2ra(void); |
| 27 |
|
|
static void ra2avs(void); |
| 28 |
greg |
2.1 |
|
| 29 |
schorsch |
2.11 |
|
| 30 |
|
|
int |
| 31 |
|
|
main( |
| 32 |
|
|
int argc, |
| 33 |
|
|
char *argv[] |
| 34 |
|
|
) |
| 35 |
greg |
2.1 |
{ |
| 36 |
|
|
int reverse = 0; |
| 37 |
|
|
int i; |
| 38 |
|
|
|
| 39 |
|
|
progname = argv[0]; |
| 40 |
|
|
|
| 41 |
|
|
for (i = 1; i < argc; i++) |
| 42 |
|
|
if (argv[i][0] == '-') |
| 43 |
|
|
switch (argv[i][1]) { |
| 44 |
|
|
case 'g': /* gamma correction */ |
| 45 |
greg |
2.4 |
gamcor = atof(argv[++i]); |
| 46 |
greg |
2.1 |
break; |
| 47 |
|
|
case 'e': /* exposure adjustment */ |
| 48 |
|
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
| 49 |
|
|
goto userr; |
| 50 |
|
|
bradj = atoi(argv[++i]); |
| 51 |
|
|
break; |
| 52 |
|
|
case 'r': /* reverse conversion */ |
| 53 |
|
|
reverse = 1; |
| 54 |
|
|
break; |
| 55 |
|
|
default: |
| 56 |
|
|
goto userr; |
| 57 |
|
|
} |
| 58 |
|
|
else |
| 59 |
|
|
break; |
| 60 |
|
|
|
| 61 |
|
|
if (i < argc-2) |
| 62 |
|
|
goto userr; |
| 63 |
|
|
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) { |
| 64 |
|
|
fprintf(stderr, "%s: can't open input \"%s\"\n", |
| 65 |
|
|
progname, argv[i]); |
| 66 |
|
|
exit(1); |
| 67 |
|
|
} |
| 68 |
|
|
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) { |
| 69 |
greg |
2.6 |
fprintf(stderr, "%s: can't open output \"%s\"\n", |
| 70 |
greg |
2.1 |
progname, argv[i+1]); |
| 71 |
|
|
exit(1); |
| 72 |
|
|
} |
| 73 |
schorsch |
2.10 |
SET_FILE_BINARY(stdin); |
| 74 |
|
|
SET_FILE_BINARY(stdout); |
| 75 |
greg |
2.4 |
setcolrgam(gamcor); /* set up gamma correction */ |
| 76 |
greg |
2.1 |
if (reverse) { |
| 77 |
|
|
/* get their image resolution */ |
| 78 |
greg |
2.2 |
xmax = getint(4, stdin); |
| 79 |
|
|
ymax = getint(4, stdin); |
| 80 |
greg |
2.7 |
if (feof(stdin)) |
| 81 |
|
|
quiterr("empty input file"); |
| 82 |
greg |
2.1 |
/* put our header */ |
| 83 |
greg |
2.8 |
newheader("RADIANCE", stdout); |
| 84 |
greg |
2.1 |
printargs(i, argv, stdout); |
| 85 |
|
|
fputformat(COLRFMT, stdout); |
| 86 |
|
|
putchar('\n'); |
| 87 |
|
|
fprtresolu(xmax, ymax, stdout); |
| 88 |
|
|
/* convert file */ |
| 89 |
|
|
avs2ra(); |
| 90 |
|
|
} else { |
| 91 |
|
|
/* get our header */ |
| 92 |
|
|
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
| 93 |
|
|
fgetresolu(&xmax, &ymax, stdin) < 0) |
| 94 |
|
|
quiterr("bad picture format"); |
| 95 |
|
|
/* write their header */ |
| 96 |
greg |
2.2 |
putint((long)xmax, 4, stdout); |
| 97 |
|
|
putint((long)ymax, 4, stdout); |
| 98 |
greg |
2.1 |
/* convert file */ |
| 99 |
|
|
ra2avs(); |
| 100 |
|
|
} |
| 101 |
|
|
exit(0); |
| 102 |
|
|
userr: |
| 103 |
|
|
fprintf(stderr, |
| 104 |
|
|
"Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n", |
| 105 |
|
|
progname); |
| 106 |
|
|
exit(1); |
| 107 |
|
|
} |
| 108 |
|
|
|
| 109 |
|
|
|
| 110 |
schorsch |
2.11 |
static void |
| 111 |
|
|
quiterr( /* print message and exit */ |
| 112 |
|
|
char *err |
| 113 |
|
|
) |
| 114 |
greg |
2.1 |
{ |
| 115 |
|
|
if (err != NULL) { |
| 116 |
|
|
fprintf(stderr, "%s: %s\n", progname, err); |
| 117 |
|
|
exit(1); |
| 118 |
|
|
} |
| 119 |
|
|
exit(0); |
| 120 |
|
|
} |
| 121 |
|
|
|
| 122 |
|
|
|
| 123 |
schorsch |
2.11 |
static void |
| 124 |
|
|
avs2ra(void) /* convert 24-bit scanlines to Radiance picture */ |
| 125 |
greg |
2.1 |
{ |
| 126 |
|
|
COLR *scanout; |
| 127 |
|
|
register int x; |
| 128 |
|
|
int y; |
| 129 |
|
|
/* allocate scanline */ |
| 130 |
|
|
scanout = (COLR *)malloc(xmax*sizeof(COLR)); |
| 131 |
|
|
if (scanout == NULL) |
| 132 |
|
|
quiterr("out of memory in avs2ra"); |
| 133 |
|
|
/* convert image */ |
| 134 |
|
|
for (y = ymax-1; y >= 0; y--) { |
| 135 |
greg |
2.5 |
for (x = 0; x < xmax; x++) { |
| 136 |
|
|
(void)getc(stdin); /* toss alpha */ |
| 137 |
|
|
scanout[x][RED] = getc(stdin); |
| 138 |
|
|
scanout[x][GRN] = getc(stdin); |
| 139 |
|
|
scanout[x][BLU] = getc(stdin); |
| 140 |
|
|
} |
| 141 |
|
|
if (feof(stdin) | ferror(stdin)) |
| 142 |
greg |
2.1 |
quiterr("error reading AVS image"); |
| 143 |
|
|
/* undo gamma */ |
| 144 |
|
|
gambs_colrs(scanout, xmax); |
| 145 |
|
|
if (bradj) /* adjust exposure */ |
| 146 |
|
|
shiftcolrs(scanout, xmax, bradj); |
| 147 |
|
|
if (fwritecolrs(scanout, xmax, stdout) < 0) |
| 148 |
|
|
quiterr("error writing Radiance picture"); |
| 149 |
|
|
} |
| 150 |
|
|
/* free scanline */ |
| 151 |
greg |
2.9 |
free((void *)scanout); |
| 152 |
greg |
2.1 |
} |
| 153 |
|
|
|
| 154 |
|
|
|
| 155 |
schorsch |
2.11 |
static void |
| 156 |
|
|
ra2avs(void) /* convert Radiance scanlines to 24-bit */ |
| 157 |
greg |
2.1 |
{ |
| 158 |
|
|
COLR *scanin; |
| 159 |
|
|
register int x; |
| 160 |
|
|
int y; |
| 161 |
|
|
/* allocate scanline */ |
| 162 |
|
|
scanin = (COLR *)malloc(xmax*sizeof(COLR)); |
| 163 |
|
|
if (scanin == NULL) |
| 164 |
|
|
quiterr("out of memory in ra2avs"); |
| 165 |
|
|
/* convert image */ |
| 166 |
|
|
for (y = ymax-1; y >= 0; y--) { |
| 167 |
|
|
if (freadcolrs(scanin, xmax, stdin) < 0) |
| 168 |
|
|
quiterr("error reading Radiance picture"); |
| 169 |
|
|
if (bradj) /* adjust exposure */ |
| 170 |
|
|
shiftcolrs(scanin, xmax, bradj); |
| 171 |
|
|
colrs_gambs(scanin, xmax); /* gamma correction */ |
| 172 |
|
|
for (x = 0; x < xmax; x++) { |
| 173 |
|
|
putc(0, stdout); /* no alpha */ |
| 174 |
|
|
putc(scanin[x][RED], stdout); |
| 175 |
|
|
putc(scanin[x][GRN], stdout); |
| 176 |
|
|
putc(scanin[x][BLU], stdout); |
| 177 |
|
|
} |
| 178 |
|
|
if (ferror(stdout)) |
| 179 |
|
|
quiterr("error writing AVS file"); |
| 180 |
|
|
} |
| 181 |
|
|
/* free scanline */ |
| 182 |
greg |
2.9 |
free((void *)scanin); |
| 183 |
greg |
2.1 |
} |