| 1 |
greg |
1.1 |
#ifndef lint
|
| 2 |
greg |
2.9 |
static const char RCSid[] = "$Id$";
|
| 3 |
greg |
1.1 |
#endif
|
| 4 |
|
|
/*
|
| 5 |
|
|
* program to convert between RADIANCE and 24-bit rasterfiles.
|
| 6 |
|
|
*/
|
| 7 |
|
|
|
| 8 |
|
|
#include <stdio.h>
|
| 9 |
|
|
|
| 10 |
greg |
2.3 |
#ifdef MSDOS
|
| 11 |
|
|
#include <fcntl.h>
|
| 12 |
|
|
#endif
|
| 13 |
|
|
|
| 14 |
greg |
2.9 |
#include <time.h>
|
| 15 |
|
|
|
| 16 |
greg |
2.5 |
#include <math.h>
|
| 17 |
|
|
|
| 18 |
greg |
1.1 |
#include "rasterfile.h"
|
| 19 |
|
|
|
| 20 |
|
|
#include "color.h"
|
| 21 |
|
|
|
| 22 |
greg |
1.11 |
#include "resolu.h"
|
| 23 |
greg |
1.1 |
|
| 24 |
greg |
2.6 |
double gamcor = 2.2; /* gamma correction */
|
| 25 |
greg |
1.1 |
|
| 26 |
greg |
1.7 |
int bradj = 0; /* brightness adjustment */
|
| 27 |
|
|
|
| 28 |
greg |
1.1 |
char *progname;
|
| 29 |
|
|
|
| 30 |
|
|
int xmax, ymax;
|
| 31 |
|
|
|
| 32 |
|
|
|
| 33 |
|
|
main(argc, argv)
|
| 34 |
|
|
int argc;
|
| 35 |
|
|
char *argv[];
|
| 36 |
|
|
{
|
| 37 |
|
|
struct rasterfile head;
|
| 38 |
|
|
int reverse = 0;
|
| 39 |
|
|
int i;
|
| 40 |
greg |
2.3 |
#ifdef MSDOS
|
| 41 |
|
|
extern int _fmode;
|
| 42 |
|
|
_fmode = O_BINARY;
|
| 43 |
|
|
setmode(fileno(stdin), O_BINARY);
|
| 44 |
|
|
setmode(fileno(stdout), O_BINARY);
|
| 45 |
|
|
#endif
|
| 46 |
greg |
1.1 |
progname = argv[0];
|
| 47 |
|
|
|
| 48 |
greg |
1.9 |
head.ras_type = RT_STANDARD;
|
| 49 |
greg |
1.1 |
for (i = 1; i < argc; i++)
|
| 50 |
|
|
if (argv[i][0] == '-')
|
| 51 |
|
|
switch (argv[i][1]) {
|
| 52 |
|
|
case 'g':
|
| 53 |
greg |
2.6 |
gamcor = atof(argv[++i]);
|
| 54 |
greg |
1.1 |
break;
|
| 55 |
greg |
1.7 |
case 'e':
|
| 56 |
|
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
|
| 57 |
|
|
goto userr;
|
| 58 |
|
|
bradj = atoi(argv[++i]);
|
| 59 |
|
|
break;
|
| 60 |
greg |
1.1 |
case 'r':
|
| 61 |
greg |
1.9 |
if (!strcmp(argv[i], "-rgb"))
|
| 62 |
|
|
head.ras_type = RT_FORMAT_RGB;
|
| 63 |
|
|
else
|
| 64 |
|
|
reverse = 1;
|
| 65 |
greg |
1.1 |
break;
|
| 66 |
|
|
default:
|
| 67 |
|
|
goto userr;
|
| 68 |
|
|
}
|
| 69 |
|
|
else
|
| 70 |
|
|
break;
|
| 71 |
|
|
|
| 72 |
|
|
if (i < argc-2)
|
| 73 |
|
|
goto userr;
|
| 74 |
|
|
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
|
| 75 |
|
|
fprintf(stderr, "%s: can't open input \"%s\"\n",
|
| 76 |
|
|
progname, argv[i]);
|
| 77 |
|
|
exit(1);
|
| 78 |
|
|
}
|
| 79 |
|
|
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
|
| 80 |
greg |
2.7 |
fprintf(stderr, "%s: can't open output \"%s\"\n",
|
| 81 |
greg |
1.1 |
progname, argv[i+1]);
|
| 82 |
|
|
exit(1);
|
| 83 |
|
|
}
|
| 84 |
greg |
2.6 |
setcolrgam(gamcor);
|
| 85 |
greg |
1.1 |
if (reverse) {
|
| 86 |
|
|
/* get header */
|
| 87 |
|
|
if (fread((char *)&head, sizeof(head), 1, stdin) != 1)
|
| 88 |
|
|
quiterr("missing header");
|
| 89 |
|
|
if (head.ras_magic != RAS_MAGIC)
|
| 90 |
|
|
quiterr("bad raster format");
|
| 91 |
|
|
xmax = head.ras_width;
|
| 92 |
|
|
ymax = head.ras_height;
|
| 93 |
greg |
1.6 |
if ((head.ras_type != RT_STANDARD
|
| 94 |
|
|
&& head.ras_type != RT_FORMAT_RGB)
|
| 95 |
|
|
|| head.ras_maptype != RMT_NONE
|
| 96 |
|
|
|| head.ras_depth != 24)
|
| 97 |
greg |
1.1 |
quiterr("incompatible format");
|
| 98 |
|
|
/* put header */
|
| 99 |
greg |
2.8 |
newheader("RADIANCE", stdout);
|
| 100 |
greg |
1.1 |
printargs(i, argv, stdout);
|
| 101 |
greg |
1.5 |
fputformat(COLRFMT, stdout);
|
| 102 |
greg |
1.1 |
putchar('\n');
|
| 103 |
greg |
1.11 |
fprtresolu(xmax, ymax, stdout);
|
| 104 |
greg |
1.1 |
/* convert file */
|
| 105 |
greg |
2.4 |
pr2ra(head.ras_type, head.ras_length/ymax - xmax*3);
|
| 106 |
greg |
1.1 |
} else {
|
| 107 |
greg |
1.5 |
/* get header info. */
|
| 108 |
|
|
if (checkheader(stdin, COLRFMT, NULL) < 0 ||
|
| 109 |
greg |
1.11 |
fgetresolu(&xmax, &ymax, stdin) < 0)
|
| 110 |
greg |
1.5 |
quiterr("bad picture format");
|
| 111 |
greg |
1.1 |
/* write rasterfile header */
|
| 112 |
|
|
head.ras_magic = RAS_MAGIC;
|
| 113 |
greg |
2.4 |
head.ras_width = xmax + (xmax&1);
|
| 114 |
greg |
1.1 |
head.ras_height = ymax;
|
| 115 |
|
|
head.ras_depth = 24;
|
| 116 |
greg |
2.4 |
head.ras_length = head.ras_width*head.ras_height*3;
|
| 117 |
greg |
1.1 |
head.ras_maptype = RMT_NONE;
|
| 118 |
|
|
head.ras_maplength = 0;
|
| 119 |
|
|
fwrite((char *)&head, sizeof(head), 1, stdout);
|
| 120 |
|
|
/* convert file */
|
| 121 |
greg |
2.4 |
ra2pr(head.ras_type, head.ras_length/ymax - xmax*3);
|
| 122 |
greg |
1.1 |
}
|
| 123 |
|
|
exit(0);
|
| 124 |
|
|
userr:
|
| 125 |
greg |
1.7 |
fprintf(stderr, "Usage: %s [-r][-g gamma][-e +/-stops] [input [output]]\n",
|
| 126 |
greg |
1.1 |
progname);
|
| 127 |
|
|
exit(1);
|
| 128 |
|
|
}
|
| 129 |
|
|
|
| 130 |
|
|
|
| 131 |
|
|
quiterr(err) /* print message and exit */
|
| 132 |
|
|
char *err;
|
| 133 |
|
|
{
|
| 134 |
|
|
if (err != NULL) {
|
| 135 |
|
|
fprintf(stderr, "%s: %s\n", progname, err);
|
| 136 |
|
|
exit(1);
|
| 137 |
|
|
}
|
| 138 |
|
|
exit(0);
|
| 139 |
|
|
}
|
| 140 |
|
|
|
| 141 |
|
|
|
| 142 |
greg |
2.4 |
pr2ra(rf, pad) /* convert 24-bit scanlines to Radiance picture */
|
| 143 |
greg |
1.6 |
int rf;
|
| 144 |
greg |
2.4 |
int pad;
|
| 145 |
greg |
1.1 |
{
|
| 146 |
greg |
1.3 |
COLR *scanout;
|
| 147 |
greg |
1.1 |
register int x;
|
| 148 |
|
|
int y;
|
| 149 |
|
|
/* allocate scanline */
|
| 150 |
greg |
1.3 |
scanout = (COLR *)malloc(xmax*sizeof(COLR));
|
| 151 |
greg |
1.1 |
if (scanout == NULL)
|
| 152 |
|
|
quiterr("out of memory in pr2ra");
|
| 153 |
|
|
/* convert image */
|
| 154 |
|
|
for (y = ymax-1; y >= 0; y--) {
|
| 155 |
greg |
1.9 |
if (rf == RT_FORMAT_RGB)
|
| 156 |
|
|
for (x = 0; x < xmax; x++) {
|
| 157 |
greg |
1.6 |
scanout[x][RED] = getc(stdin);
|
| 158 |
|
|
scanout[x][GRN] = getc(stdin);
|
| 159 |
|
|
scanout[x][BLU] = getc(stdin);
|
| 160 |
greg |
1.9 |
}
|
| 161 |
|
|
else
|
| 162 |
|
|
for (x = 0; x < xmax; x++) {
|
| 163 |
greg |
1.6 |
scanout[x][BLU] = getc(stdin);
|
| 164 |
|
|
scanout[x][GRN] = getc(stdin);
|
| 165 |
|
|
scanout[x][RED] = getc(stdin);
|
| 166 |
|
|
}
|
| 167 |
greg |
2.4 |
for (x = pad; x--; getc(stdin));
|
| 168 |
greg |
1.3 |
if (feof(stdin) || ferror(stdin))
|
| 169 |
|
|
quiterr("error reading rasterfile");
|
| 170 |
|
|
gambs_colrs(scanout, xmax);
|
| 171 |
greg |
1.7 |
if (bradj)
|
| 172 |
|
|
shiftcolrs(scanout, xmax, bradj);
|
| 173 |
greg |
1.3 |
if (fwritecolrs(scanout, xmax, stdout) < 0)
|
| 174 |
greg |
1.1 |
quiterr("error writing Radiance picture");
|
| 175 |
|
|
}
|
| 176 |
|
|
/* free scanline */
|
| 177 |
greg |
2.9 |
free((void *)scanout);
|
| 178 |
greg |
1.1 |
}
|
| 179 |
|
|
|
| 180 |
|
|
|
| 181 |
greg |
2.4 |
ra2pr(rf, pad) /* convert Radiance scanlines to 24-bit rasterfile */
|
| 182 |
|
|
int rf;
|
| 183 |
|
|
int pad;
|
| 184 |
greg |
1.1 |
{
|
| 185 |
greg |
2.4 |
int ord[3];
|
| 186 |
greg |
1.3 |
COLR *scanin;
|
| 187 |
greg |
1.1 |
register int x;
|
| 188 |
|
|
int y;
|
| 189 |
|
|
/* allocate scanline */
|
| 190 |
greg |
1.3 |
scanin = (COLR *)malloc(xmax*sizeof(COLR));
|
| 191 |
greg |
1.1 |
if (scanin == NULL)
|
| 192 |
greg |
1.8 |
quiterr("out of memory in ra2pr");
|
| 193 |
greg |
2.4 |
if (rf == RT_FORMAT_RGB) {
|
| 194 |
|
|
ord[0] = RED; ord[1] = GRN; ord[2] = BLU;
|
| 195 |
|
|
} else {
|
| 196 |
|
|
ord[0] = BLU; ord[1] = GRN; ord[2] = RED;
|
| 197 |
|
|
}
|
| 198 |
greg |
1.1 |
/* convert image */
|
| 199 |
|
|
for (y = ymax-1; y >= 0; y--) {
|
| 200 |
greg |
1.3 |
if (freadcolrs(scanin, xmax, stdin) < 0)
|
| 201 |
greg |
1.1 |
quiterr("error reading Radiance picture");
|
| 202 |
greg |
1.7 |
if (bradj)
|
| 203 |
|
|
shiftcolrs(scanin, xmax, bradj);
|
| 204 |
greg |
1.3 |
colrs_gambs(scanin, xmax);
|
| 205 |
greg |
1.9 |
if (rf == RT_FORMAT_RGB)
|
| 206 |
|
|
for (x = 0; x < xmax; x++) {
|
| 207 |
|
|
putc(scanin[x][RED], stdout);
|
| 208 |
|
|
putc(scanin[x][GRN], stdout);
|
| 209 |
|
|
putc(scanin[x][BLU], stdout);
|
| 210 |
|
|
}
|
| 211 |
|
|
else
|
| 212 |
|
|
for (x = 0; x < xmax; x++) {
|
| 213 |
|
|
putc(scanin[x][BLU], stdout);
|
| 214 |
|
|
putc(scanin[x][GRN], stdout);
|
| 215 |
|
|
putc(scanin[x][RED], stdout);
|
| 216 |
|
|
}
|
| 217 |
greg |
2.4 |
for (x = 0; x < pad; x++)
|
| 218 |
|
|
putc(scanin[xmax-1][ord[x%3]], stdout);
|
| 219 |
greg |
1.1 |
if (ferror(stdout))
|
| 220 |
|
|
quiterr("error writing rasterfile");
|
| 221 |
|
|
}
|
| 222 |
|
|
/* free scanline */
|
| 223 |
greg |
2.9 |
free((void *)scanin);
|
| 224 |
greg |
1.1 |
}
|