| 1 |
greg |
1.1 |
/* Copyright (c) 1991 Regents of the University of California */
|
| 2 |
|
|
|
| 3 |
|
|
#ifndef lint
|
| 4 |
|
|
static char SCCSid[] = "$SunId$ LBL";
|
| 5 |
|
|
#endif
|
| 6 |
|
|
|
| 7 |
|
|
/*
|
| 8 |
|
|
* program to convert between RADIANCE and Poskanzer Pixmaps
|
| 9 |
|
|
*/
|
| 10 |
|
|
|
| 11 |
|
|
#include <stdio.h>
|
| 12 |
|
|
|
| 13 |
|
|
#include <ctype.h>
|
| 14 |
|
|
|
| 15 |
|
|
#include "color.h"
|
| 16 |
|
|
|
| 17 |
greg |
1.2 |
#include "resolu.h"
|
| 18 |
|
|
|
| 19 |
greg |
1.1 |
extern double atof(), pow();
|
| 20 |
|
|
|
| 21 |
|
|
int agryscan(), bgryscan(), aclrscan(), bclrscan();
|
| 22 |
|
|
|
| 23 |
|
|
int bradj = 0; /* brightness adjustment */
|
| 24 |
|
|
|
| 25 |
|
|
int maxval = 255; /* maximum primary value */
|
| 26 |
|
|
|
| 27 |
|
|
char *progname;
|
| 28 |
|
|
|
| 29 |
|
|
int xmax, ymax;
|
| 30 |
|
|
|
| 31 |
|
|
|
| 32 |
|
|
main(argc, argv)
|
| 33 |
|
|
int argc;
|
| 34 |
|
|
char *argv[];
|
| 35 |
|
|
{
|
| 36 |
|
|
double gamma = 2.2;
|
| 37 |
|
|
int binflag = 1;
|
| 38 |
|
|
int reverse = 0;
|
| 39 |
|
|
int ptype;
|
| 40 |
|
|
int i;
|
| 41 |
|
|
|
| 42 |
|
|
progname = argv[0];
|
| 43 |
|
|
|
| 44 |
|
|
for (i = 1; i < argc; i++)
|
| 45 |
|
|
if (argv[i][0] == '-')
|
| 46 |
|
|
switch (argv[i][1]) {
|
| 47 |
|
|
case 'g':
|
| 48 |
|
|
gamma = atof(argv[++i]);
|
| 49 |
|
|
break;
|
| 50 |
|
|
case 'e':
|
| 51 |
|
|
if (argv[i+1][0] != '+' && argv[i+1][0] != '-')
|
| 52 |
|
|
goto userr;
|
| 53 |
|
|
bradj = atoi(argv[++i]);
|
| 54 |
|
|
break;
|
| 55 |
|
|
case 'a':
|
| 56 |
|
|
binflag = 0;
|
| 57 |
|
|
break;
|
| 58 |
|
|
case 'r':
|
| 59 |
|
|
reverse = !reverse;
|
| 60 |
|
|
break;
|
| 61 |
|
|
default:
|
| 62 |
|
|
goto userr;
|
| 63 |
|
|
}
|
| 64 |
|
|
else
|
| 65 |
|
|
break;
|
| 66 |
|
|
|
| 67 |
|
|
if (i < argc-2)
|
| 68 |
|
|
goto userr;
|
| 69 |
|
|
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
|
| 70 |
|
|
fprintf(stderr, "%s: can't open input \"%s\"\n",
|
| 71 |
|
|
progname, argv[i]);
|
| 72 |
|
|
exit(1);
|
| 73 |
|
|
}
|
| 74 |
|
|
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
|
| 75 |
|
|
fprintf(stderr, "can't open output \"%s\"\n",
|
| 76 |
|
|
progname, argv[i+1]);
|
| 77 |
|
|
exit(1);
|
| 78 |
|
|
}
|
| 79 |
|
|
setcolrgam(gamma);
|
| 80 |
|
|
if (reverse) {
|
| 81 |
|
|
/* get header */
|
| 82 |
|
|
if (getc(stdin) != 'P')
|
| 83 |
|
|
quiterr("input not a Poskanzer Pixmap");
|
| 84 |
|
|
ptype = getc(stdin);
|
| 85 |
|
|
xmax = scanint(stdin);
|
| 86 |
|
|
ymax = scanint(stdin);
|
| 87 |
|
|
maxval = scanint(stdin);
|
| 88 |
|
|
/* put header */
|
| 89 |
|
|
printargs(i, argv, stdout);
|
| 90 |
|
|
fputformat(COLRFMT, stdout);
|
| 91 |
|
|
putchar('\n');
|
| 92 |
greg |
1.2 |
fprtresolu(xmax, ymax, stdout);
|
| 93 |
greg |
1.1 |
/* convert file */
|
| 94 |
|
|
switch (ptype) {
|
| 95 |
|
|
case '2':
|
| 96 |
|
|
ppm2ra(agryscan);
|
| 97 |
|
|
break;
|
| 98 |
|
|
case '5':
|
| 99 |
|
|
ppm2ra(bgryscan);
|
| 100 |
|
|
break;
|
| 101 |
|
|
case '3':
|
| 102 |
|
|
ppm2ra(aclrscan);
|
| 103 |
|
|
break;
|
| 104 |
|
|
case '6':
|
| 105 |
|
|
ppm2ra(bclrscan);
|
| 106 |
|
|
break;
|
| 107 |
|
|
default:
|
| 108 |
|
|
quiterr("unsupported Pixmap type");
|
| 109 |
|
|
}
|
| 110 |
|
|
} else {
|
| 111 |
|
|
/* get header info. */
|
| 112 |
|
|
if (checkheader(stdin, COLRFMT, NULL) < 0 ||
|
| 113 |
greg |
1.2 |
fgetresolu(&xmax, &ymax, stdin) < 0)
|
| 114 |
greg |
1.1 |
quiterr("bad picture format");
|
| 115 |
|
|
/* write PPM header */
|
| 116 |
|
|
printf("P%c\n%d %d\n%d\n", binflag ? '6' : '3',
|
| 117 |
|
|
xmax, ymax, maxval);
|
| 118 |
|
|
/* convert file */
|
| 119 |
|
|
ra2ppm(binflag);
|
| 120 |
|
|
}
|
| 121 |
|
|
exit(0);
|
| 122 |
|
|
userr:
|
| 123 |
|
|
fprintf(stderr,
|
| 124 |
|
|
"Usage: %s [-r][-a][-g gamma][-e +/-stops] [input [output]]\n",
|
| 125 |
|
|
progname);
|
| 126 |
|
|
exit(1);
|
| 127 |
|
|
}
|
| 128 |
|
|
|
| 129 |
|
|
|
| 130 |
|
|
quiterr(err) /* print message and exit */
|
| 131 |
|
|
char *err;
|
| 132 |
|
|
{
|
| 133 |
|
|
if (err != NULL) {
|
| 134 |
|
|
fprintf(stderr, "%s: %s\n", progname, err);
|
| 135 |
|
|
exit(1);
|
| 136 |
|
|
}
|
| 137 |
|
|
exit(0);
|
| 138 |
|
|
}
|
| 139 |
|
|
|
| 140 |
|
|
|
| 141 |
|
|
ppm2ra(getscan) /* convert color Pixmap to Radiance picture */
|
| 142 |
|
|
int (*getscan)();
|
| 143 |
|
|
{
|
| 144 |
|
|
COLR *scanout;
|
| 145 |
|
|
register int x;
|
| 146 |
|
|
int y;
|
| 147 |
|
|
/* allocate scanline */
|
| 148 |
|
|
scanout = (COLR *)malloc(xmax*sizeof(COLR));
|
| 149 |
|
|
if (scanout == NULL)
|
| 150 |
|
|
quiterr("out of memory in ppm2ra");
|
| 151 |
|
|
/* convert image */
|
| 152 |
|
|
for (y = ymax-1; y >= 0; y--) {
|
| 153 |
|
|
if ((*getscan)(scanout, xmax, stdin) < 0)
|
| 154 |
|
|
quiterr("error reading Pixmap");
|
| 155 |
|
|
gambs_colrs(scanout, xmax);
|
| 156 |
|
|
if (bradj)
|
| 157 |
|
|
shiftcolrs(scanout, xmax, bradj);
|
| 158 |
|
|
if (fwritecolrs(scanout, xmax, stdout) < 0)
|
| 159 |
|
|
quiterr("error writing Radiance picture");
|
| 160 |
|
|
}
|
| 161 |
|
|
/* free scanline */
|
| 162 |
|
|
free((char *)scanout);
|
| 163 |
|
|
}
|
| 164 |
|
|
|
| 165 |
|
|
|
| 166 |
|
|
ra2ppm(binary) /* convert Radiance picture to Pixmap */
|
| 167 |
|
|
int binary;
|
| 168 |
|
|
{
|
| 169 |
|
|
COLR *scanin;
|
| 170 |
|
|
register int x;
|
| 171 |
|
|
int y;
|
| 172 |
|
|
/* allocate scanline */
|
| 173 |
|
|
scanin = (COLR *)malloc(xmax*sizeof(COLR));
|
| 174 |
|
|
if (scanin == NULL)
|
| 175 |
|
|
quiterr("out of memory in ra2pr");
|
| 176 |
|
|
/* convert image */
|
| 177 |
|
|
for (y = ymax-1; y >= 0; y--) {
|
| 178 |
|
|
if (freadcolrs(scanin, xmax, stdin) < 0)
|
| 179 |
|
|
quiterr("error reading Radiance picture");
|
| 180 |
|
|
if (bradj)
|
| 181 |
|
|
shiftcolrs(scanin, xmax, bradj);
|
| 182 |
|
|
colrs_gambs(scanin, xmax);
|
| 183 |
|
|
if (binary)
|
| 184 |
|
|
for (x = 0; x < xmax; x++) {
|
| 185 |
|
|
putc(scanin[x][RED], stdout);
|
| 186 |
|
|
putc(scanin[x][GRN], stdout);
|
| 187 |
|
|
putc(scanin[x][BLU], stdout);
|
| 188 |
|
|
}
|
| 189 |
|
|
else
|
| 190 |
|
|
for (x = 0; x < xmax; x++)
|
| 191 |
|
|
printf("%d %d %d\n", scanin[x][RED],
|
| 192 |
|
|
scanin[x][GRN],
|
| 193 |
|
|
scanin[x][BLU]);
|
| 194 |
|
|
if (ferror(stdout))
|
| 195 |
|
|
quiterr("error writing Pixmap");
|
| 196 |
|
|
}
|
| 197 |
|
|
/* free scanline */
|
| 198 |
|
|
free((char *)scanin);
|
| 199 |
|
|
}
|
| 200 |
|
|
|
| 201 |
|
|
|
| 202 |
|
|
agryscan(scan, len, fp) /* get an ASCII greyscale scanline */
|
| 203 |
|
|
register COLR *scan;
|
| 204 |
|
|
register int len;
|
| 205 |
|
|
FILE *fp;
|
| 206 |
|
|
{
|
| 207 |
|
|
while (len-- > 0) {
|
| 208 |
|
|
scan[0][RED] =
|
| 209 |
|
|
scan[0][GRN] =
|
| 210 |
|
|
scan[0][BLU] = normval(scanint(fp));
|
| 211 |
|
|
scan++;
|
| 212 |
|
|
}
|
| 213 |
|
|
return(0);
|
| 214 |
|
|
}
|
| 215 |
|
|
|
| 216 |
|
|
|
| 217 |
|
|
bgryscan(scan, len, fp) /* get a binary greyscale scanline */
|
| 218 |
|
|
register COLR *scan;
|
| 219 |
|
|
int len;
|
| 220 |
|
|
register FILE *fp;
|
| 221 |
|
|
{
|
| 222 |
|
|
register int c;
|
| 223 |
|
|
|
| 224 |
|
|
while (len-- > 0) {
|
| 225 |
|
|
if ((c = getc(fp)) == EOF)
|
| 226 |
|
|
return(-1);
|
| 227 |
|
|
if (maxval != 255)
|
| 228 |
|
|
c = normval(c);
|
| 229 |
|
|
scan[0][RED] =
|
| 230 |
|
|
scan[0][GRN] =
|
| 231 |
|
|
scan[0][BLU] = c;
|
| 232 |
|
|
scan++;
|
| 233 |
|
|
}
|
| 234 |
|
|
return(0);
|
| 235 |
|
|
}
|
| 236 |
|
|
|
| 237 |
|
|
|
| 238 |
|
|
aclrscan(scan, len, fp) /* get an ASCII color scanline */
|
| 239 |
|
|
register COLR *scan;
|
| 240 |
|
|
register int len;
|
| 241 |
|
|
FILE *fp;
|
| 242 |
|
|
{
|
| 243 |
|
|
while (len-- > 0) {
|
| 244 |
|
|
scan[0][RED] = normval(scanint(fp));
|
| 245 |
|
|
scan[0][GRN] = normval(scanint(fp));
|
| 246 |
|
|
scan[0][BLU] = normval(scanint(fp));
|
| 247 |
|
|
scan++;
|
| 248 |
|
|
}
|
| 249 |
|
|
return(0);
|
| 250 |
|
|
}
|
| 251 |
|
|
|
| 252 |
|
|
|
| 253 |
|
|
bclrscan(scan, len, fp) /* get a binary color scanline */
|
| 254 |
|
|
register COLR *scan;
|
| 255 |
|
|
int len;
|
| 256 |
|
|
register FILE *fp;
|
| 257 |
|
|
{
|
| 258 |
|
|
int r, g, b;
|
| 259 |
|
|
|
| 260 |
|
|
while (len-- > 0) {
|
| 261 |
|
|
r = getc(fp);
|
| 262 |
|
|
g = getc(fp);
|
| 263 |
|
|
if ((b = getc(fp)) == EOF)
|
| 264 |
|
|
return(-1);
|
| 265 |
|
|
if (maxval == 255) {
|
| 266 |
|
|
scan[0][RED] = r;
|
| 267 |
|
|
scan[0][GRN] = g;
|
| 268 |
|
|
scan[0][BLU] = b;
|
| 269 |
|
|
} else {
|
| 270 |
|
|
scan[0][RED] = normval(r);
|
| 271 |
|
|
scan[0][GRN] = normval(g);
|
| 272 |
|
|
scan[0][BLU] = normval(b);
|
| 273 |
|
|
}
|
| 274 |
|
|
scan++;
|
| 275 |
|
|
}
|
| 276 |
|
|
return(0);
|
| 277 |
|
|
}
|
| 278 |
|
|
|
| 279 |
|
|
|
| 280 |
|
|
int
|
| 281 |
|
|
scanint(fp) /* scan the next positive integer value */
|
| 282 |
|
|
register FILE *fp;
|
| 283 |
|
|
{
|
| 284 |
|
|
register int i, c;
|
| 285 |
|
|
tryagain:
|
| 286 |
|
|
while (isspace(c = getc(fp)))
|
| 287 |
|
|
;
|
| 288 |
|
|
if (c == EOF)
|
| 289 |
|
|
quiterr("unexpected end of file");
|
| 290 |
|
|
if (c == '#') { /* comment */
|
| 291 |
|
|
while ((c = getc(fp)) != EOF && c != '\n')
|
| 292 |
|
|
;
|
| 293 |
|
|
goto tryagain;
|
| 294 |
|
|
}
|
| 295 |
|
|
/* should be integer */
|
| 296 |
|
|
i = 0;
|
| 297 |
|
|
do {
|
| 298 |
|
|
if (!isdigit(c))
|
| 299 |
|
|
quiterr("error reading integer");
|
| 300 |
|
|
i = 10*i + c - '0';
|
| 301 |
|
|
c = getc(fp);
|
| 302 |
|
|
} while (c != EOF && !isspace(c));
|
| 303 |
|
|
return(i);
|
| 304 |
|
|
}
|
| 305 |
|
|
|
| 306 |
|
|
|
| 307 |
|
|
int
|
| 308 |
|
|
normval(v) /* normalize a value to [0,255] */
|
| 309 |
|
|
register int v;
|
| 310 |
|
|
{
|
| 311 |
|
|
if (v >= maxval)
|
| 312 |
|
|
return(255);
|
| 313 |
|
|
if (maxval == 255)
|
| 314 |
|
|
return(v);
|
| 315 |
|
|
return(v*255L/maxval);
|
| 316 |
|
|
}
|