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