ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/radiance/ray/src/common/normcodec.c
Revision: 2.2
Committed: Fri Jul 26 17:04:12 2019 UTC (5 years, 11 months ago) by greg
Content type: text/plain
Branch: MAIN
Changes since 2.1: +33 -19 lines
Log Message:
Added control for stderr output

File Contents

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