| 1 |
#ifndef lint
|
| 2 |
static const char RCSid[] = "$Id: pextrem.c,v 2.15 2022/02/04 20:11:49 greg Exp $";
|
| 3 |
#endif
|
| 4 |
/*
|
| 5 |
* Find extrema points in a Radiance picture (RGBE, XYZE, or HyperSpectral)
|
| 6 |
*/
|
| 7 |
|
| 8 |
#include <math.h>
|
| 9 |
|
| 10 |
#include "rtio.h"
|
| 11 |
#include "platform.h"
|
| 12 |
#include "color.h"
|
| 13 |
#include "resolu.h"
|
| 14 |
|
| 15 |
|
| 16 |
int orig = 0;
|
| 17 |
|
| 18 |
COLOR expos = WHTCOLOR;
|
| 19 |
|
| 20 |
char fmt[MAXFMTLEN];
|
| 21 |
|
| 22 |
static gethfunc headline;
|
| 23 |
|
| 24 |
|
| 25 |
static int
|
| 26 |
headline( /* check header line */
|
| 27 |
char *s,
|
| 28 |
void *p
|
| 29 |
)
|
| 30 |
{
|
| 31 |
double d;
|
| 32 |
COLOR ctmp;
|
| 33 |
|
| 34 |
if (formatval(fmt, s)) /* format */
|
| 35 |
return(0);
|
| 36 |
if (iswlsplit(s)) { /* wavelength splits */
|
| 37 |
wlsplitval(WLPART, s);
|
| 38 |
return(0);
|
| 39 |
}
|
| 40 |
if (isncomp(s)) { /* # spectral components */
|
| 41 |
NCSAMP = ncompval(s);
|
| 42 |
return(0);
|
| 43 |
}
|
| 44 |
if (!orig) /* don't undo exposure? */
|
| 45 |
return(0);
|
| 46 |
if (isexpos(s)) { /* exposure */
|
| 47 |
d = exposval(s);
|
| 48 |
scalecolor(expos, d);
|
| 49 |
} else if (iscolcor(s)) { /* color correction */
|
| 50 |
colcorval(ctmp, s);
|
| 51 |
multcolor(expos, ctmp);
|
| 52 |
}
|
| 53 |
return(0);
|
| 54 |
}
|
| 55 |
|
| 56 |
|
| 57 |
int
|
| 58 |
main(
|
| 59 |
int argc,
|
| 60 |
char *argv[]
|
| 61 |
)
|
| 62 |
{
|
| 63 |
int i;
|
| 64 |
int xres, yres;
|
| 65 |
int y;
|
| 66 |
int x;
|
| 67 |
COLRV *scan;
|
| 68 |
SCOLR cmin, cmax;
|
| 69 |
COLOR tcol;
|
| 70 |
int xmin, ymin, xmax, ymax;
|
| 71 |
|
| 72 |
SET_DEFAULT_BINARY();
|
| 73 |
SET_FILE_BINARY(stdin);
|
| 74 |
for (i = 1; i < argc; i++) /* get options */
|
| 75 |
if (!strcmp(argv[i], "-o"))
|
| 76 |
orig = 1;
|
| 77 |
else if (!strcmp(argv[i], "-O"))
|
| 78 |
orig = -1;
|
| 79 |
else
|
| 80 |
break;
|
| 81 |
|
| 82 |
if (i == argc-1 && freopen(argv[i], "r", stdin) == NULL) {
|
| 83 |
fprintf(stderr, "%s: can't open input \"%s\"\n",
|
| 84 |
argv[0], argv[i]);
|
| 85 |
return(1);
|
| 86 |
}
|
| 87 |
/* get our header */
|
| 88 |
if (getheader(stdin, headline, NULL) < 0 ||
|
| 89 |
(!globmatch(PICFMT, fmt) && strcmp(fmt, SPECFMT)) ||
|
| 90 |
fgetresolu(&xres, &yres, stdin) < 0) {
|
| 91 |
fprintf(stderr, "%s: bad picture format\n", argv[0]);
|
| 92 |
return(1);
|
| 93 |
}
|
| 94 |
if (setspectrsamp(CNDX, WLPART) < 0) {
|
| 95 |
fprintf(stderr, "%s: bad wavelength split or component count",
|
| 96 |
argv[0]);
|
| 97 |
return(1);
|
| 98 |
}
|
| 99 |
if (orig < 0 && !strcmp(CIEFMT, fmt))
|
| 100 |
scalecolor(expos, 1./WHTEFFICACY);
|
| 101 |
if ((scan = (COLRV *)malloc(xres*sizeof(COLRV)*(NCSAMP+1))) == NULL) {
|
| 102 |
fprintf(stderr, "%s: out of memory\n", argv[0]);
|
| 103 |
return(1);
|
| 104 |
}
|
| 105 |
setscolr(cmin, 1e30, 1e30, 1e30); xmin=ymin=0;
|
| 106 |
scolrblack(cmax); xmax=ymax=0;
|
| 107 |
/* find extrema */
|
| 108 |
for (y = yres-1; y >= 0; y--) {
|
| 109 |
if (freadscolrs(scan, NCSAMP, xres, stdin) < 0) {
|
| 110 |
fprintf(stderr, "%s: read error on input\n", argv[0]);
|
| 111 |
return(1);
|
| 112 |
}
|
| 113 |
for (x = xres; x-- > 0; ) {
|
| 114 |
const COLRV * sclr = scan + x*(NCSAMP+1);
|
| 115 |
if (sclr[CNDX[EXP]] > cmax[CNDX[EXP]] ||
|
| 116 |
(sclr[CNDX[EXP]] == cmax[CNDX[EXP]] &&
|
| 117 |
normpbright(sclr) > normpbright(cmax))) {
|
| 118 |
copyscolr(cmax, sclr);
|
| 119 |
xmax = x; ymax = y;
|
| 120 |
}
|
| 121 |
if (sclr[CNDX[EXP]] < cmin[CNDX[EXP]] ||
|
| 122 |
(sclr[CNDX[EXP]] == cmin[CNDX[EXP]] &&
|
| 123 |
normpbright(sclr) < normpbright(cmin))) {
|
| 124 |
copyscolr(cmin, sclr);
|
| 125 |
xmin = x; ymin = y;
|
| 126 |
}
|
| 127 |
}
|
| 128 |
}
|
| 129 |
free(scan);
|
| 130 |
scolr_color(tcol, cmin);
|
| 131 |
printf("%d %d\t%.2e %.2e %.2e\n", xmin, ymin,
|
| 132 |
colval(tcol,RED)/colval(expos,RED),
|
| 133 |
colval(tcol,GRN)/colval(expos,GRN),
|
| 134 |
colval(tcol,BLU)/colval(expos,BLU));
|
| 135 |
scolr_color(tcol, cmax);
|
| 136 |
printf("%d %d\t%.2e %.2e %.2e\n", xmax, ymax,
|
| 137 |
colval(tcol,RED)/colval(expos,RED),
|
| 138 |
colval(tcol,GRN)/colval(expos,GRN),
|
| 139 |
colval(tcol,BLU)/colval(expos,BLU));
|
| 140 |
return(0);
|
| 141 |
}
|