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