| 1 |
/* Copyright (c) 1990 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 24-bit rasterfiles.
|
| 9 |
*/
|
| 10 |
|
| 11 |
#include <stdio.h>
|
| 12 |
|
| 13 |
#include "rasterfile.h"
|
| 14 |
|
| 15 |
#include "color.h"
|
| 16 |
|
| 17 |
extern double atof(), pow();
|
| 18 |
|
| 19 |
double gamma = 2.0; /* gamma correction */
|
| 20 |
|
| 21 |
char *progname;
|
| 22 |
|
| 23 |
int xmax, ymax;
|
| 24 |
|
| 25 |
|
| 26 |
main(argc, argv)
|
| 27 |
int argc;
|
| 28 |
char *argv[];
|
| 29 |
{
|
| 30 |
struct rasterfile head;
|
| 31 |
int reverse = 0;
|
| 32 |
int i;
|
| 33 |
|
| 34 |
progname = argv[0];
|
| 35 |
|
| 36 |
for (i = 1; i < argc; i++)
|
| 37 |
if (argv[i][0] == '-')
|
| 38 |
switch (argv[i][1]) {
|
| 39 |
case 'g':
|
| 40 |
gamma = atof(argv[++i]);
|
| 41 |
break;
|
| 42 |
case 'r':
|
| 43 |
reverse = !reverse;
|
| 44 |
break;
|
| 45 |
default:
|
| 46 |
goto userr;
|
| 47 |
}
|
| 48 |
else
|
| 49 |
break;
|
| 50 |
|
| 51 |
if (i < argc-2)
|
| 52 |
goto userr;
|
| 53 |
if (i <= argc-1 && freopen(argv[i], "r", stdin) == NULL) {
|
| 54 |
fprintf(stderr, "%s: can't open input \"%s\"\n",
|
| 55 |
progname, argv[i]);
|
| 56 |
exit(1);
|
| 57 |
}
|
| 58 |
if (i == argc-2 && freopen(argv[i+1], "w", stdout) == NULL) {
|
| 59 |
fprintf(stderr, "can't open output \"%s\"\n",
|
| 60 |
progname, argv[i+1]);
|
| 61 |
exit(1);
|
| 62 |
}
|
| 63 |
setcolrgam(gamma);
|
| 64 |
if (reverse) {
|
| 65 |
/* get header */
|
| 66 |
if (fread((char *)&head, sizeof(head), 1, stdin) != 1)
|
| 67 |
quiterr("missing header");
|
| 68 |
if (head.ras_magic != RAS_MAGIC)
|
| 69 |
quiterr("bad raster format");
|
| 70 |
xmax = head.ras_width;
|
| 71 |
ymax = head.ras_height;
|
| 72 |
if (head.ras_type != RT_STANDARD ||
|
| 73 |
head.ras_maptype != RMT_NONE ||
|
| 74 |
head.ras_depth != 24)
|
| 75 |
quiterr("incompatible format");
|
| 76 |
/* put header */
|
| 77 |
printargs(i, argv, stdout);
|
| 78 |
putchar('\n');
|
| 79 |
fputresolu(YMAJOR|YDECR, xmax, ymax, stdout);
|
| 80 |
/* convert file */
|
| 81 |
pr2ra();
|
| 82 |
} else {
|
| 83 |
/* discard input header */
|
| 84 |
getheader(stdin, NULL);
|
| 85 |
/* get resolution */
|
| 86 |
if (fgetresolu(&xmax, &ymax, stdin) != (YMAJOR|YDECR))
|
| 87 |
quiterr("bad picture size");
|
| 88 |
/* write rasterfile header */
|
| 89 |
head.ras_magic = RAS_MAGIC;
|
| 90 |
head.ras_width = xmax;
|
| 91 |
head.ras_height = ymax;
|
| 92 |
head.ras_depth = 24;
|
| 93 |
head.ras_length = xmax*ymax*3;
|
| 94 |
head.ras_type = RT_STANDARD;
|
| 95 |
head.ras_maptype = RMT_NONE;
|
| 96 |
head.ras_maplength = 0;
|
| 97 |
fwrite((char *)&head, sizeof(head), 1, stdout);
|
| 98 |
/* convert file */
|
| 99 |
ra2pr();
|
| 100 |
}
|
| 101 |
exit(0);
|
| 102 |
userr:
|
| 103 |
fprintf(stderr, "Usage: %s [-r][-g gamma] [input [output]]\n",
|
| 104 |
progname);
|
| 105 |
exit(1);
|
| 106 |
}
|
| 107 |
|
| 108 |
|
| 109 |
quiterr(err) /* print message and exit */
|
| 110 |
char *err;
|
| 111 |
{
|
| 112 |
if (err != NULL) {
|
| 113 |
fprintf(stderr, "%s: %s\n", progname, err);
|
| 114 |
exit(1);
|
| 115 |
}
|
| 116 |
exit(0);
|
| 117 |
}
|
| 118 |
|
| 119 |
|
| 120 |
pr2ra() /* convert 24-bit scanlines to Radiance picture */
|
| 121 |
{
|
| 122 |
COLR *scanout;
|
| 123 |
register int x;
|
| 124 |
int y;
|
| 125 |
/* allocate scanline */
|
| 126 |
scanout = (COLR *)malloc(xmax*sizeof(COLR));
|
| 127 |
if (scanout == NULL)
|
| 128 |
quiterr("out of memory in pr2ra");
|
| 129 |
/* convert image */
|
| 130 |
for (y = ymax-1; y >= 0; y--) {
|
| 131 |
for (x = 0; x < xmax; x++) {
|
| 132 |
scanout[x][BLU] = getc(stdin);
|
| 133 |
scanout[x][GRN] = getc(stdin);
|
| 134 |
scanout[x][RED] = getc(stdin);
|
| 135 |
}
|
| 136 |
if (feof(stdin) || ferror(stdin))
|
| 137 |
quiterr("error reading rasterfile");
|
| 138 |
gambs_colrs(scanout, xmax);
|
| 139 |
if (fwritecolrs(scanout, xmax, stdout) < 0)
|
| 140 |
quiterr("error writing Radiance picture");
|
| 141 |
}
|
| 142 |
/* free scanline */
|
| 143 |
free((char *)scanout);
|
| 144 |
}
|
| 145 |
|
| 146 |
|
| 147 |
ra2pr() /* convert Radiance scanlines to 24-bit rasterfile */
|
| 148 |
{
|
| 149 |
COLR *scanin;
|
| 150 |
register int x;
|
| 151 |
int y;
|
| 152 |
/* allocate scanline */
|
| 153 |
scanin = (COLR *)malloc(xmax*sizeof(COLR));
|
| 154 |
if (scanin == NULL)
|
| 155 |
quiterr("out of memory in pr2ra");
|
| 156 |
/* convert image */
|
| 157 |
for (y = ymax-1; y >= 0; y--) {
|
| 158 |
if (freadcolrs(scanin, xmax, stdin) < 0)
|
| 159 |
quiterr("error reading Radiance picture");
|
| 160 |
colrs_gambs(scanin, xmax);
|
| 161 |
for (x = 0; x < xmax; x++) {
|
| 162 |
putc(scanin[x][BLU], stdout);
|
| 163 |
putc(scanin[x][GRN], stdout);
|
| 164 |
putc(scanin[x][RED], stdout);
|
| 165 |
}
|
| 166 |
if (ferror(stdout))
|
| 167 |
quiterr("error writing rasterfile");
|
| 168 |
}
|
| 169 |
/* free scanline */
|
| 170 |
free((char *)scanin);
|
| 171 |
}
|