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