| 1 |
greg |
2.1 |
#ifndef lint
|
| 2 |
greg |
2.10 |
static const char RCSid[] = "$Id: normcodec.c,v 2.9 2022/09/22 21:47:13 greg Exp $";
|
| 3 |
greg |
2.1 |
#endif
|
| 4 |
|
|
/*
|
| 5 |
|
|
* Routines to encode/decode 32-bit normals
|
| 6 |
|
|
*/
|
| 7 |
|
|
|
| 8 |
|
|
#include "copyright.h"
|
| 9 |
|
|
|
| 10 |
|
|
#include "rtio.h"
|
| 11 |
greg |
2.10 |
#include "paths.h"
|
| 12 |
greg |
2.1 |
#include "rtmath.h"
|
| 13 |
|
|
#include "normcodec.h"
|
| 14 |
|
|
|
| 15 |
|
|
|
| 16 |
|
|
/* Set codec defaults */
|
| 17 |
|
|
void
|
| 18 |
|
|
set_nc_defaults(NORMCODEC *ncp)
|
| 19 |
|
|
{
|
| 20 |
|
|
memset(ncp, 0, sizeof(NORMCODEC));
|
| 21 |
|
|
ncp->finp = stdin;
|
| 22 |
|
|
ncp->inpname = "<stdin>";
|
| 23 |
|
|
ncp->format = 'a';
|
| 24 |
|
|
ncp->res.rt = PIXSTANDARD;
|
| 25 |
|
|
if (!progname) progname = "norm_codec";
|
| 26 |
|
|
}
|
| 27 |
|
|
|
| 28 |
|
|
|
| 29 |
|
|
/* process header line */
|
| 30 |
|
|
static int
|
| 31 |
|
|
headline(char *s, void *p)
|
| 32 |
|
|
{
|
| 33 |
|
|
NORMCODEC *ncp = (NORMCODEC *)p;
|
| 34 |
greg |
2.3 |
int rv;
|
| 35 |
greg |
2.1 |
|
| 36 |
|
|
if (formatval(ncp->inpfmt, s)) /* don't pass format */
|
| 37 |
|
|
return 0;
|
| 38 |
|
|
|
| 39 |
greg |
2.3 |
if ((rv = isbigendian(s)) >= 0) {
|
| 40 |
|
|
ncp->swapped = (nativebigendian() != rv);
|
| 41 |
|
|
return 0;
|
| 42 |
|
|
}
|
| 43 |
greg |
2.8 |
if (!strncmp(s, "NCOMP=", 6)) {
|
| 44 |
|
|
if (atoi(s+6) != 3) {
|
| 45 |
|
|
if (ncp->hdrflags & HF_STDERR) {
|
| 46 |
|
|
fputs(ncp->inpname, stderr);
|
| 47 |
|
|
fputs(": NCOMP must equal 3\n", stderr);
|
| 48 |
|
|
}
|
| 49 |
|
|
return -1;
|
| 50 |
|
|
}
|
| 51 |
|
|
return 0;
|
| 52 |
|
|
}
|
| 53 |
|
|
if (!strncmp(s, "NROWS=", 6)) {
|
| 54 |
|
|
ncp->res.yr = atoi(s+6);
|
| 55 |
|
|
return 0;
|
| 56 |
|
|
}
|
| 57 |
|
|
if (!strncmp(s, "NCOLS=", 6)) {
|
| 58 |
|
|
ncp->res.xr = atoi(s+6);
|
| 59 |
|
|
return 0;
|
| 60 |
|
|
}
|
| 61 |
greg |
2.1 |
if (ncp->hdrflags & HF_HEADOUT)
|
| 62 |
|
|
fputs(s, stdout); /* copy to standard output */
|
| 63 |
|
|
return 1;
|
| 64 |
|
|
}
|
| 65 |
|
|
|
| 66 |
|
|
|
| 67 |
|
|
/* Load/copy header */
|
| 68 |
|
|
int
|
| 69 |
|
|
process_nc_header(NORMCODEC *ncp, int ac, char *av[])
|
| 70 |
|
|
{
|
| 71 |
|
|
if (ncp->hdrflags & HF_HEADIN &&
|
| 72 |
|
|
getheader(ncp->finp, headline, ncp) < 0) {
|
| 73 |
greg |
2.2 |
if (ncp->hdrflags & HF_STDERR) {
|
| 74 |
|
|
fputs(ncp->inpname, stderr);
|
| 75 |
|
|
fputs(": bad header\n", stderr);
|
| 76 |
|
|
}
|
| 77 |
|
|
return 0;
|
| 78 |
greg |
2.1 |
}
|
| 79 |
greg |
2.6 |
/* get resolution string? */
|
| 80 |
greg |
2.8 |
if (ncp->hdrflags & HF_RESIN &&
|
| 81 |
|
|
(ncp->res.xr <= 0) | (ncp->res.yr <= 0) &&
|
| 82 |
|
|
!fgetsresolu(&ncp->res, ncp->finp)) {
|
| 83 |
greg |
2.6 |
if (ncp->hdrflags & HF_STDERR) {
|
| 84 |
|
|
fputs(ncp->inpname, stderr);
|
| 85 |
|
|
fputs(": bad resolution string\n", stderr);
|
| 86 |
|
|
}
|
| 87 |
|
|
return 0;
|
| 88 |
|
|
}
|
| 89 |
greg |
2.1 |
if (ncp->hdrflags & HF_HEADOUT) { /* finish header */
|
| 90 |
|
|
if (!(ncp->hdrflags & HF_HEADIN))
|
| 91 |
|
|
newheader("RADIANCE", stdout);
|
| 92 |
|
|
if (ac > 0)
|
| 93 |
|
|
printargs(ac, av, stdout);
|
| 94 |
|
|
if (ncp->hdrflags & HF_ENCODE) {
|
| 95 |
|
|
fputformat(NORMAL32FMT, stdout);
|
| 96 |
greg |
2.6 |
} else {
|
| 97 |
|
|
fputs("NCOMP=3\n", stdout);
|
| 98 |
|
|
if ((ncp->hdrflags & (HF_RESIN|HF_RESOUT)) == HF_RESIN)
|
| 99 |
|
|
printf("NCOLS=%d\nNROWS=%d\n",
|
| 100 |
|
|
scanlen(&ncp->res),
|
| 101 |
|
|
numscans(&ncp->res));
|
| 102 |
greg |
2.1 |
switch (ncp->format) {
|
| 103 |
|
|
case 'a':
|
| 104 |
|
|
fputformat("ascii", stdout);
|
| 105 |
|
|
break;
|
| 106 |
|
|
case 'f':
|
| 107 |
greg |
2.3 |
fputendian(stdout);
|
| 108 |
greg |
2.1 |
fputformat("float", stdout);
|
| 109 |
|
|
break;
|
| 110 |
|
|
case 'd':
|
| 111 |
greg |
2.3 |
fputendian(stdout);
|
| 112 |
greg |
2.1 |
fputformat("double", stdout);
|
| 113 |
|
|
break;
|
| 114 |
|
|
}
|
| 115 |
greg |
2.6 |
}
|
| 116 |
greg |
2.1 |
fputc('\n', stdout);
|
| 117 |
|
|
}
|
| 118 |
greg |
2.6 |
if (ncp->hdrflags & HF_RESOUT) /* put resolution string? */
|
| 119 |
greg |
2.1 |
fputsresolu(&ncp->res, stdout);
|
| 120 |
|
|
|
| 121 |
greg |
2.7 |
ncp->dstart = ftell(ncp->finp);
|
| 122 |
greg |
2.1 |
return 1;
|
| 123 |
|
|
}
|
| 124 |
|
|
|
| 125 |
|
|
|
| 126 |
|
|
/* Check that we have what we need to decode normals */
|
| 127 |
|
|
int
|
| 128 |
|
|
check_decode_normals(NORMCODEC *ncp)
|
| 129 |
|
|
{
|
| 130 |
|
|
if (ncp->hdrflags & HF_ENCODE) {
|
| 131 |
greg |
2.2 |
if (ncp->hdrflags & HF_STDERR) {
|
| 132 |
|
|
fputs(progname, stderr);
|
| 133 |
|
|
fputs(": wrong header mode for decode\n", stderr);
|
| 134 |
|
|
}
|
| 135 |
greg |
2.1 |
return 0;
|
| 136 |
|
|
}
|
| 137 |
|
|
if (ncp->inpfmt[0] && strcmp(ncp->inpfmt, NORMAL32FMT)) {
|
| 138 |
greg |
2.2 |
if (ncp->hdrflags & HF_STDERR) {
|
| 139 |
|
|
fputs(ncp->inpname, stderr);
|
| 140 |
|
|
fputs(": unexpected input format: ", stderr);
|
| 141 |
|
|
fputs(ncp->inpfmt, stderr);
|
| 142 |
|
|
fputc('\n', stderr);
|
| 143 |
|
|
}
|
| 144 |
greg |
2.1 |
return 0;
|
| 145 |
|
|
}
|
| 146 |
|
|
return 1;
|
| 147 |
|
|
}
|
| 148 |
|
|
|
| 149 |
|
|
|
| 150 |
|
|
/* Decode next normal from input */
|
| 151 |
|
|
int
|
| 152 |
|
|
decode_normal_next(FVECT nrm, NORMCODEC *ncp)
|
| 153 |
|
|
{
|
| 154 |
greg |
2.5 |
static int32 lastc;
|
| 155 |
|
|
static FVECT lastv;
|
| 156 |
|
|
int32 c = getint(4, ncp->finp);
|
| 157 |
greg |
2.1 |
|
| 158 |
|
|
if (c == EOF && feof(ncp->finp))
|
| 159 |
|
|
return -1;
|
| 160 |
|
|
|
| 161 |
greg |
2.4 |
if (c == lastc) { /* optimization */
|
| 162 |
|
|
VCOPY(nrm, lastv);
|
| 163 |
|
|
} else {
|
| 164 |
|
|
decodedir(nrm, c);
|
| 165 |
|
|
if (c) {
|
| 166 |
|
|
lastc = c;
|
| 167 |
|
|
VCOPY(lastv, nrm);
|
| 168 |
|
|
}
|
| 169 |
|
|
}
|
| 170 |
greg |
2.1 |
return (c != 0);
|
| 171 |
|
|
}
|
| 172 |
|
|
|
| 173 |
|
|
|
| 174 |
|
|
/* Seek to the indicated pixel position */
|
| 175 |
|
|
int
|
| 176 |
|
|
seek_nc_pix(NORMCODEC *ncp, int x, int y)
|
| 177 |
|
|
{
|
| 178 |
|
|
if ((ncp->res.xr <= 0) | (ncp->res.yr <= 0)) {
|
| 179 |
greg |
2.2 |
if (ncp->hdrflags & HF_STDERR) {
|
| 180 |
|
|
fputs(progname, stderr);
|
| 181 |
|
|
fputs(": need map resolution to seek\n", stderr);
|
| 182 |
|
|
}
|
| 183 |
greg |
2.1 |
return -1;
|
| 184 |
|
|
}
|
| 185 |
|
|
if ((x < 0) | (y < 0) ||
|
| 186 |
|
|
(x >= scanlen(&ncp->res)) | (y >= numscans(&ncp->res))) {
|
| 187 |
greg |
2.2 |
if (ncp->hdrflags & HF_STDERR) {
|
| 188 |
|
|
fputs(ncp->inpname, stderr);
|
| 189 |
|
|
fputs(": warning - pixel index off map\n", stderr);
|
| 190 |
|
|
}
|
| 191 |
greg |
2.1 |
return 0;
|
| 192 |
|
|
}
|
| 193 |
greg |
2.7 |
if (fseek(ncp->finp, ncp->dstart + 4*((long)y*scanlen(&ncp->res) + x),
|
| 194 |
|
|
SEEK_SET) == EOF) {
|
| 195 |
greg |
2.2 |
if (ncp->hdrflags & HF_STDERR) {
|
| 196 |
|
|
fputs(ncp->inpname, stderr);
|
| 197 |
|
|
fputs(": seek error\n", stderr);
|
| 198 |
|
|
}
|
| 199 |
greg |
2.1 |
return -1;
|
| 200 |
|
|
}
|
| 201 |
|
|
return 1;
|
| 202 |
|
|
}
|
| 203 |
|
|
|
| 204 |
|
|
|
| 205 |
|
|
/* Read and decode normal for the given pixel */
|
| 206 |
|
|
int
|
| 207 |
|
|
decode_normal_pix(FVECT nrm, NORMCODEC *ncp, int x, int y)
|
| 208 |
|
|
{
|
| 209 |
|
|
int rval = seek_nc_pix(ncp, x, y);
|
| 210 |
|
|
|
| 211 |
|
|
if (rval < 0)
|
| 212 |
|
|
return -1;
|
| 213 |
|
|
|
| 214 |
|
|
if (!rval) {
|
| 215 |
|
|
nrm[0] = nrm[1] = nrm[2] = .0;
|
| 216 |
|
|
return 0;
|
| 217 |
|
|
}
|
| 218 |
|
|
return decode_normal_next(nrm, ncp);
|
| 219 |
|
|
}
|