1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: radcompare.c,v 2.25 2020/07/09 17:29:04 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Compare Radiance files for significant differences |
6 |
* |
7 |
* G. Ward |
8 |
*/ |
9 |
|
10 |
#include <stdlib.h> |
11 |
#include <ctype.h> |
12 |
#include "rtmath.h" |
13 |
#include "platform.h" |
14 |
#include "rtio.h" |
15 |
#include "resolu.h" |
16 |
#include "color.h" |
17 |
#include "depthcodec.h" |
18 |
#include "normcodec.h" |
19 |
#include "lookup.h" |
20 |
/* Reporting levels */ |
21 |
#define REP_QUIET 0 /* no reporting */ |
22 |
#define REP_ERROR 1 /* report errors only */ |
23 |
#define REP_WARN 2 /* report warnings as well */ |
24 |
#define REP_VERBOSE 3 /* verbose reporting */ |
25 |
|
26 |
int report = REP_WARN; /* reporting level */ |
27 |
|
28 |
int ign_header = 0; /* ignore header differences? */ |
29 |
|
30 |
double rel_min = 1e-5; /* positive for relative comparisons */ |
31 |
|
32 |
double rms_lim = 0.01; /* RMS difference limit */ |
33 |
|
34 |
double max_lim = 0.25; /* difference limit if non-negative */ |
35 |
|
36 |
int lin1cnt=0, lin2cnt=0; /* file line position */ |
37 |
|
38 |
int comment_c = '\0'; /* comment delimiter for text files */ |
39 |
|
40 |
const char nsuffix[10][3] = { /* 1st, 2nd, 3rd, etc. */ |
41 |
"th","st","nd","rd","th","th","th","th","th","th" |
42 |
}; |
43 |
#define num_sfx(n) nsuffix[(n)%10] |
44 |
|
45 |
/* file types */ |
46 |
const char *file_type[] = { |
47 |
"Unrecognized", |
48 |
"TEXT_generic", |
49 |
"ascii", |
50 |
COLRFMT, |
51 |
CIEFMT, |
52 |
DEPTH16FMT, |
53 |
NORMAL32FMT, |
54 |
"float", |
55 |
"double", |
56 |
"BSDF_RBFmesh", |
57 |
"Radiance_octree", |
58 |
"Radiance_tmesh", |
59 |
"8-bit_indexed_name", |
60 |
"16-bit_indexed_name", |
61 |
"24-bit_indexed_name", |
62 |
"BINARY_unknown", |
63 |
NULL /* terminator */ |
64 |
}; |
65 |
/* keep consistent with above */ |
66 |
enum {TYP_UNKNOWN, TYP_TEXT, TYP_ASCII, TYP_RGBE, TYP_XYZE, |
67 |
TYP_DEPTH, TYP_NORM, TYP_FLOAT, TYP_DOUBLE, |
68 |
TYP_RBFMESH, TYP_OCTREE, TYP_TMESH, |
69 |
TYP_ID8, TYP_ID16, TYP_ID24, TYP_BINARY}; |
70 |
|
71 |
#define has_header(t) (!( 1L<<(t) & (1L<<TYP_TEXT | 1L<<TYP_BINARY) )) |
72 |
|
73 |
/* header variables to always ignore */ |
74 |
const char *hdr_ignkey[] = { |
75 |
"SOFTWARE", |
76 |
"CAPDATE", |
77 |
"GMT", |
78 |
"FRAME", |
79 |
NULL /* terminator */ |
80 |
}; |
81 |
/* header variable settings */ |
82 |
LUTAB hdr1 = LU_SINIT(free,free); |
83 |
LUTAB hdr2 = LU_SINIT(free,free); |
84 |
|
85 |
/* advance appropriate file line count */ |
86 |
#define adv_linecnt(htp) (lin1cnt += (htp == &hdr1), \ |
87 |
lin2cnt += (htp == &hdr2)) |
88 |
|
89 |
typedef struct { /* dynamic line buffer */ |
90 |
char *str; |
91 |
int len; |
92 |
int siz; |
93 |
} LINEBUF; |
94 |
|
95 |
#define init_line(bp) ((bp)->str = NULL, (bp)->siz = 0) |
96 |
/* 100 MByte limit on line buffer */ |
97 |
#define MAXBUF (100L<<20) |
98 |
|
99 |
/* input files */ |
100 |
char *progname = NULL; |
101 |
const char stdin_name[] = "<stdin>"; |
102 |
const char *f1name=NULL, *f2name=NULL; |
103 |
FILE *f1in=NULL, *f2in=NULL; |
104 |
int f1swap=0, f2swap=0; |
105 |
|
106 |
/* running real differences */ |
107 |
double diff2sum = 0; |
108 |
long nsum = 0; |
109 |
|
110 |
/* Report usage and exit */ |
111 |
static void |
112 |
usage() |
113 |
{ |
114 |
fputs("Usage: ", stderr); |
115 |
fputs(progname, stderr); |
116 |
fputs(" [-h][-c#][-s|-w|-v][-rel min_test][-rms epsilon][-max epsilon] reference test\n", |
117 |
stderr); |
118 |
exit(2); |
119 |
} |
120 |
|
121 |
/* Read a text line, increasing buffer size as necessary */ |
122 |
static int |
123 |
read_line(LINEBUF *bp, FILE *fp) |
124 |
{ |
125 |
static int doneWarn = 0; |
126 |
|
127 |
bp->len = 0; |
128 |
if (!bp->str) { |
129 |
bp->str = (char *)malloc(bp->siz = 512); |
130 |
if (!bp->str) |
131 |
goto memerr; |
132 |
} |
133 |
while (fgets(bp->str + bp->len, bp->siz - bp->len, fp)) { |
134 |
bp->len += strlen(bp->str + bp->len); |
135 |
if (bp->str[bp->len-1] == '\n') |
136 |
break; /* found EOL */ |
137 |
if (bp->len < bp->siz - 4) |
138 |
continue; /* at EOF? */ |
139 |
if (bp->siz >= MAXBUF) { |
140 |
if ((report >= REP_WARN) & !doneWarn) { |
141 |
fprintf(stderr, |
142 |
"%s: warning - input line(s) past %ld MByte limit\n", |
143 |
progname, MAXBUF>>20); |
144 |
doneWarn++; |
145 |
} |
146 |
break; /* return MAXBUF partial line */ |
147 |
} |
148 |
if ((bp->siz += bp->siz/2) > MAXBUF) |
149 |
bp->siz = MAXBUF; |
150 |
bp->str = (char *)realloc(bp->str, bp->siz); |
151 |
if (!bp->str) |
152 |
goto memerr; |
153 |
} |
154 |
if (comment_c) { /* elide comment? */ |
155 |
char *cp = sskip2(bp->str,0); |
156 |
if (*cp == comment_c) { |
157 |
*cp++ = '\n'; |
158 |
*cp = '\0'; |
159 |
bp->len = cp - bp->str; |
160 |
} |
161 |
} |
162 |
return(bp->len); |
163 |
memerr: |
164 |
fprintf(stderr, |
165 |
"%s: out of memory in read_line() allocating %d byte buffer\n", |
166 |
progname, bp->siz); |
167 |
exit(2); |
168 |
} |
169 |
|
170 |
/* Free line buffer */ |
171 |
static void |
172 |
free_line(LINEBUF *bp) |
173 |
{ |
174 |
if (bp->str) free(bp->str); |
175 |
init_line(bp); |
176 |
} |
177 |
|
178 |
/* Get type ID from name (or 0 if not found) */ |
179 |
static int |
180 |
xlate_type(const char *nm) |
181 |
{ |
182 |
int i; |
183 |
|
184 |
if (!nm || !*nm) |
185 |
return(TYP_UNKNOWN); |
186 |
for (i = 1; file_type[i]; i++) |
187 |
if (!strcmp(nm, file_type[i])) |
188 |
return(i); |
189 |
return(TYP_UNKNOWN); |
190 |
} |
191 |
|
192 |
/* Compare real values and keep track of differences */ |
193 |
static int |
194 |
real_check(double r1, double r2) |
195 |
{ |
196 |
double diff2 = (r1 - r2)*(r1 - r2); |
197 |
|
198 |
if (rel_min > 0) { /* doing relative differences? */ |
199 |
double av2 = .25*(r1*r1 + 2.*fabs(r1*r2) + r2*r2); |
200 |
if (av2 > rel_min*rel_min) |
201 |
diff2 /= av2; |
202 |
} |
203 |
if (max_lim >= 0 && diff2 > max_lim*max_lim) { |
204 |
if (report != REP_QUIET) |
205 |
printf( |
206 |
"%s: %sdifference between %.8g and %.8g exceeds epsilon of %.8g\n", |
207 |
progname, |
208 |
(rel_min > 0) ? "relative " : "", |
209 |
r1, r2, max_lim); |
210 |
return(0); |
211 |
} |
212 |
diff2sum += diff2; |
213 |
nsum++; |
214 |
return(1); |
215 |
} |
216 |
|
217 |
/* Compare two color values for equivalence */ |
218 |
static int |
219 |
color_check(COLOR c1, COLOR c2) |
220 |
{ |
221 |
int p; |
222 |
|
223 |
if (!real_check((colval(c1,RED)+colval(c1,GRN)+colval(c1,BLU))*(1./3.), |
224 |
(colval(c2,RED)+colval(c2,GRN)+colval(c2,BLU))*(1./3.))) |
225 |
return(0); |
226 |
|
227 |
p = (colval(c1,GRN) > colval(c1,RED)) ? GRN : RED; |
228 |
if (colval(c1,BLU) > colval(c1,p)) p = BLU; |
229 |
|
230 |
return(real_check(colval(c1,p), colval(c2,p))); |
231 |
} |
232 |
|
233 |
/* Compare two normal directions for equivalence */ |
234 |
static int |
235 |
norm_check(FVECT nv1, FVECT nv2) |
236 |
{ |
237 |
double max2 = nv1[2]*nv2[2]; |
238 |
int imax = 2; |
239 |
int i = 2; |
240 |
/* identify largest component */ |
241 |
while (i--) { |
242 |
double tm2 = nv1[i]*nv2[i]; |
243 |
if (tm2 > max2) { |
244 |
imax = i; |
245 |
max2 = tm2; |
246 |
} |
247 |
} |
248 |
i = 3; /* compare smaller components */ |
249 |
while (i--) { |
250 |
if (i == imax) |
251 |
continue; |
252 |
if (!real_check(nv1[i], nv2[i])) |
253 |
return(0); |
254 |
} |
255 |
return(1); |
256 |
} |
257 |
|
258 |
/* Compare two strings for equivalence */ |
259 |
static int |
260 |
equiv_string(char *s1, char *s2) |
261 |
{ |
262 |
#define CLS_STR 0 |
263 |
#define CLS_INT 1 |
264 |
#define CLS_FLT 2 |
265 |
/* skip whitespace at beginning */ |
266 |
while (isspace(*s1)) s1++; |
267 |
while (isspace(*s2)) s2++; |
268 |
while (*s1) { /* check each word */ |
269 |
int inquote; |
270 |
if (!*s2) /* unexpected EOL in s2? */ |
271 |
return(0); |
272 |
inquote = *s1; |
273 |
if ((inquote != '\'') & (inquote != '"')) |
274 |
inquote = 0; |
275 |
if (inquote) { /* quoted text must match exactly */ |
276 |
if (*s1++ != *s2++) |
277 |
return(0); |
278 |
while (*s1 != inquote) { |
279 |
if (!*s1) |
280 |
return(0); |
281 |
if (*s1++ != *s2++) |
282 |
return(0); |
283 |
} |
284 |
s1++; |
285 |
if (*s2++ != inquote) |
286 |
return(0); |
287 |
} else { /* else classify word type */ |
288 |
char *s1s = s1; |
289 |
char *s2s = s2; |
290 |
int cls = CLS_STR; |
291 |
s1 = sskip(s1); |
292 |
s2 = sskip(s2); |
293 |
if (iskip(s1s) == s1) { |
294 |
if (iskip(s2s) == s2) |
295 |
cls = CLS_INT; |
296 |
else if (fskip(s2s) == s2) |
297 |
cls = CLS_FLT; |
298 |
} else if (fskip(s1s) == s1) { |
299 |
if (fskip(s2s) != s2) |
300 |
return(0); |
301 |
cls = CLS_FLT; |
302 |
} |
303 |
switch (cls) { |
304 |
case CLS_INT: /* strncmp() faster */ |
305 |
case CLS_STR: |
306 |
if (s1 - s1s != s2 - s2s) |
307 |
return(0); |
308 |
if (strncmp(s1s, s2s, s1 - s1s)) |
309 |
return(0); |
310 |
break; |
311 |
case CLS_FLT: |
312 |
if (!real_check(atof(s1s), atof(s2s))) |
313 |
return(0); |
314 |
break; |
315 |
} |
316 |
} |
317 |
while (isspace(*s1)) s1++; |
318 |
while (isspace(*s2)) s2++; |
319 |
} |
320 |
return(!*s2); /* match if we reached the end of s2, too */ |
321 |
#undef CLS_STR |
322 |
#undef CLS_INT |
323 |
#undef CLS_FLT |
324 |
} |
325 |
|
326 |
/* Check if string is var=value pair and set if not in ignore list */ |
327 |
static int |
328 |
setheadvar(char *val, void *p) |
329 |
{ |
330 |
char newval[128]; |
331 |
LUTAB *htp = (LUTAB *)p; |
332 |
LUENT *tep; |
333 |
char *key; |
334 |
int kln, vln; |
335 |
int n; |
336 |
|
337 |
adv_linecnt(htp); /* side-effect is to count lines */ |
338 |
if (!isalpha(*val)) /* key must start line */ |
339 |
return(0); |
340 |
/* check if we need to swap binary data */ |
341 |
if ((n = isbigendian(val)) >= 0) { |
342 |
if (nativebigendian() == n) |
343 |
return(0); |
344 |
f1swap += (htp == &hdr1); |
345 |
f2swap += (htp == &hdr2); |
346 |
return(0); |
347 |
} |
348 |
key = val++; |
349 |
while (*val && !isspace(*val) & (*val != '=')) |
350 |
val++; |
351 |
kln = val - key; |
352 |
while (isspace(*val)) /* check for value */ |
353 |
*val++ = '\0'; |
354 |
if (*val != '=') |
355 |
return(0); |
356 |
*val++ = '\0'; |
357 |
while (isspace(*val)) |
358 |
val++; |
359 |
if (!*val) /* nothing set? */ |
360 |
return(0); |
361 |
/* check if key to ignore */ |
362 |
for (n = 0; hdr_ignkey[n]; n++) |
363 |
if (!strcmp(key, hdr_ignkey[n])) |
364 |
return(0); |
365 |
vln = strlen(val); /* eliminate space and newline at end */ |
366 |
while (isspace(val[--vln])) |
367 |
; |
368 |
val[++vln] = '\0'; |
369 |
if (!(tep = lu_find(htp, key))) |
370 |
return(-1); /* memory allocation error */ |
371 |
if (!tep->key) |
372 |
tep->key = strcpy(malloc(kln+1), key); |
373 |
if (tep->data) { /* check for special cases */ |
374 |
if (!strcmp(key, "EXPOSURE")) { |
375 |
sprintf(newval, "%f", atof(tep->data)*atof(val)); |
376 |
vln = strlen(val = newval); |
377 |
} |
378 |
free(tep->data); |
379 |
} |
380 |
tep->data = strcpy(malloc(vln+1), val); |
381 |
return(1); |
382 |
} |
383 |
|
384 |
/* Lookup correspondent in other header */ |
385 |
static int |
386 |
match_val(const LUENT *ep1, void *p2) |
387 |
{ |
388 |
const LUENT *ep2 = lu_find((LUTAB *)p2, ep1->key); |
389 |
if (!ep2 || !ep2->data) { |
390 |
if (report != REP_QUIET) |
391 |
printf("%s: variable '%s' missing in '%s'\n", |
392 |
progname, ep1->key, f2name); |
393 |
return(-1); |
394 |
} |
395 |
if (!equiv_string((char *)ep1->data, (char *)ep2->data)) { |
396 |
if (report != REP_QUIET) { |
397 |
printf("%s: header variable '%s' has different values\n", |
398 |
progname, ep1->key); |
399 |
if (report >= REP_VERBOSE) { |
400 |
printf("%s: %s=%s\n", f1name, |
401 |
ep1->key, (char *)ep1->data); |
402 |
printf("%s: %s=%s\n", f2name, |
403 |
ep2->key, (char *)ep2->data); |
404 |
} |
405 |
} |
406 |
return(-1); |
407 |
} |
408 |
return(1); /* good match */ |
409 |
} |
410 |
|
411 |
/* Compare two sets of header variables */ |
412 |
static int |
413 |
headers_match() |
414 |
{ |
415 |
int ne = lu_doall(&hdr1, match_val, &hdr2); |
416 |
if (ne < 0) |
417 |
return(0); /* something didn't match! */ |
418 |
/* non-fatal if second header has extra */ |
419 |
if (report >= REP_WARN && (ne = lu_doall(&hdr2, NULL, NULL) - ne)) |
420 |
printf("%s: warning - '%s' has %d extra header setting(s)\n", |
421 |
progname, f2name, ne); |
422 |
return(1); /* good match */ |
423 |
} |
424 |
|
425 |
/* Check generic input to determine if it is binary, -1 on error */ |
426 |
static int |
427 |
input_is_binary(FILE *fin) |
428 |
{ |
429 |
int n = 0; |
430 |
int c = 0; |
431 |
|
432 |
while ((c = getc(fin)) != EOF) { |
433 |
++n; |
434 |
if (!c | (c > 127)) |
435 |
break; /* non-ascii character */ |
436 |
if (n >= 10240) |
437 |
break; /* enough to be confident */ |
438 |
} |
439 |
if (!n) |
440 |
return(-1); /* first read failed */ |
441 |
if (fseek(fin, 0L, 0) < 0) |
442 |
return(-1); /* rewind failed */ |
443 |
return(!c | (c > 127)); |
444 |
} |
445 |
|
446 |
/* Identify and return data type from header (if one) */ |
447 |
static int |
448 |
identify_type(const char *name, FILE *fin, LUTAB *htp) |
449 |
{ |
450 |
extern const char HDRSTR[]; |
451 |
int c; |
452 |
/* check magic header start */ |
453 |
if ((c = getc(fin)) != HDRSTR[0]) { |
454 |
if (c == EOF) goto badeof; |
455 |
ungetc(c, fin); |
456 |
c = 0; |
457 |
} else if ((c = getc(fin)) != HDRSTR[1]) { |
458 |
if (c == EOF) goto badeof; |
459 |
ungetc(c, fin); ungetc(HDRSTR[0], fin); |
460 |
c = 0; |
461 |
} |
462 |
if (c) { /* appears to have a header */ |
463 |
char sbuf[32]; |
464 |
if (!fgets(sbuf, sizeof(sbuf), fin)) |
465 |
goto badeof; |
466 |
adv_linecnt(htp); /* for #?ID string */ |
467 |
if (report >= REP_WARN && strncmp(sbuf, "RADIANCE", 8)) { |
468 |
fputs(name, stdout); |
469 |
fputs(": warning - unexpected header ID: ", stdout); |
470 |
fputs(sbuf, stdout); |
471 |
} |
472 |
if (getheader(fin, setheadvar, htp) < 0) { |
473 |
fputs(name, stderr); |
474 |
fputs(": unknown error reading header\n", stderr); |
475 |
return(-1); |
476 |
} |
477 |
adv_linecnt(htp); /* for trailing emtpy line */ |
478 |
return(xlate_type((const char *)lu_find(htp,"FORMAT")->data)); |
479 |
} |
480 |
c = input_is_binary(fin); /* else peek to see if binary */ |
481 |
if (c < 0) { |
482 |
fputs(name, stderr); |
483 |
fputs(": read/seek error\n", stderr); |
484 |
return(-1); |
485 |
} |
486 |
if (c) |
487 |
return(TYP_BINARY); |
488 |
return(TYP_TEXT); |
489 |
badeof: |
490 |
if (report != REP_QUIET) { |
491 |
fputs(name, stdout); |
492 |
fputs(": unexpected end-of-file\n", stdout); |
493 |
} |
494 |
return(-1); |
495 |
} |
496 |
|
497 |
/* Check that overall RMS error is below threshold */ |
498 |
static int |
499 |
good_RMS() |
500 |
{ |
501 |
if (!nsum) |
502 |
return(1); |
503 |
if (diff2sum/(double)nsum > rms_lim*rms_lim) { |
504 |
if (report != REP_QUIET) |
505 |
printf( |
506 |
"%s: %sRMS difference between '%s' and '%s' of %.5g exceeds limit of %.5g\n", |
507 |
progname, |
508 |
(rel_min > 0) ? "relative " : "", |
509 |
f1name, f2name, |
510 |
sqrt(diff2sum/(double)nsum), rms_lim); |
511 |
return(0); |
512 |
} |
513 |
if (report >= REP_VERBOSE) |
514 |
printf("%s: %sRMS difference of reals in '%s' and '%s' is %.5g\n", |
515 |
progname, (rel_min > 0) ? "relative " : "", |
516 |
f1name, f2name, sqrt(diff2sum/(double)nsum)); |
517 |
return(1); |
518 |
} |
519 |
|
520 |
/* Compare two inputs as generic binary files */ |
521 |
static int |
522 |
compare_binary() |
523 |
{ |
524 |
int c1=0, c2=0; |
525 |
|
526 |
if (report >= REP_VERBOSE) { |
527 |
fputs(progname, stdout); |
528 |
fputs(": comparing inputs as binary\n", stdout); |
529 |
} |
530 |
for ( ; ; ) { /* exact byte matching */ |
531 |
c1 = getc(f1in); |
532 |
c2 = getc(f2in); |
533 |
if (c1 == EOF) { |
534 |
if (c2 == EOF) |
535 |
return(1); /* success! */ |
536 |
if (report != REP_QUIET) { |
537 |
fputs(f1name, stdout); |
538 |
fputs(": unexpected end-of-file\n", stdout); |
539 |
} |
540 |
return(0); |
541 |
} |
542 |
if (c2 == EOF) { |
543 |
if (report != REP_QUIET) { |
544 |
fputs(f2name, stdout); |
545 |
fputs(": unexpected end-of-file\n", stdout); |
546 |
} |
547 |
return(0); |
548 |
} |
549 |
if (c1 != c2) |
550 |
break; /* quit and report difference */ |
551 |
} |
552 |
if (report == REP_QUIET) |
553 |
return(0); |
554 |
printf("%s: binary files '%s' and '%s' differ at byte offset %ld|%ld\n", |
555 |
progname, f1name, f2name, ftell(f1in), ftell(f2in)); |
556 |
if (report >= REP_VERBOSE) |
557 |
printf("%s: byte in '%s' is 0x%X, byte in '%s' is 0x%X\n", |
558 |
progname, f1name, c1, f2name, c2); |
559 |
return(0); |
560 |
} |
561 |
|
562 |
/* Compare two inputs as generic text files */ |
563 |
static int |
564 |
compare_text() |
565 |
{ |
566 |
LINEBUF l1buf, l2buf; |
567 |
|
568 |
if (report >= REP_VERBOSE) { |
569 |
fputs(progname, stdout); |
570 |
fputs(": comparing inputs as ASCII text", stdout); |
571 |
if (comment_c) { |
572 |
fputs(", ignoring comments starting with '", stdout); |
573 |
fputc(comment_c, stdout); |
574 |
fputc('\'', stdout); |
575 |
} |
576 |
fputc('\n', stdout); |
577 |
} |
578 |
SET_FILE_TEXT(f1in); /* originally set to binary */ |
579 |
SET_FILE_TEXT(f2in); |
580 |
init_line(&l1buf); init_line(&l2buf); /* compare a line at a time */ |
581 |
while (read_line(&l1buf, f1in)) { |
582 |
lin1cnt++; |
583 |
if (!*sskip2(l1buf.str,0)) |
584 |
continue; /* ignore empty lines */ |
585 |
|
586 |
while (read_line(&l2buf, f2in)) { |
587 |
lin2cnt++; |
588 |
if (*sskip2(l2buf.str,0)) |
589 |
break; /* found other non-empty line */ |
590 |
} |
591 |
if (!l2buf.len) { /* input 2 EOF? */ |
592 |
if (report != REP_QUIET) { |
593 |
fputs(f2name, stdout); |
594 |
fputs(": unexpected end-of-file\n", stdout); |
595 |
} |
596 |
free_line(&l1buf); free_line(&l2buf); |
597 |
return(0); |
598 |
} |
599 |
/* compare non-empty lines */ |
600 |
if (!equiv_string(l1buf.str, l2buf.str)) { |
601 |
if (report != REP_QUIET) { |
602 |
printf("%s: inputs '%s' and '%s' differ at line %d|%d\n", |
603 |
progname, f1name, f2name, |
604 |
lin1cnt, lin2cnt); |
605 |
if ( report >= REP_VERBOSE && |
606 |
(l1buf.len < 256) & |
607 |
(l2buf.len < 256) ) { |
608 |
fputs("------------- Mismatch -------------\n", stdout); |
609 |
printf("%s@%d:\t%s", f1name, |
610 |
lin1cnt, l1buf.str); |
611 |
printf("%s@%d:\t%s", f2name, |
612 |
lin2cnt, l2buf.str); |
613 |
} |
614 |
} |
615 |
free_line(&l1buf); free_line(&l2buf); |
616 |
return(0); |
617 |
} |
618 |
} |
619 |
free_line(&l1buf); /* check for EOF on input 2 */ |
620 |
while (read_line(&l2buf, f2in)) { |
621 |
if (!*sskip2(l2buf.str,0)) |
622 |
continue; |
623 |
if (report != REP_QUIET) { |
624 |
fputs(f1name, stdout); |
625 |
fputs(": unexpected end-of-file\n", stdout); |
626 |
} |
627 |
free_line(&l2buf); |
628 |
return(0); |
629 |
} |
630 |
free_line(&l2buf); |
631 |
return(good_RMS()); /* final check for reals */ |
632 |
} |
633 |
|
634 |
/* Check image/map resolutions */ |
635 |
static int |
636 |
check_resolu(const char *class, RESOLU *r1p, RESOLU *r2p) |
637 |
{ |
638 |
if (r1p->rt != r2p->rt) { |
639 |
if (report != REP_QUIET) |
640 |
printf( |
641 |
"%s: %ss '%s' and '%s' have different pixel ordering\n", |
642 |
progname, class, f1name, f2name); |
643 |
return(0); |
644 |
} |
645 |
if ((r1p->xr != r2p->xr) | (r1p->yr != r2p->yr)) { |
646 |
if (report != REP_QUIET) |
647 |
printf( |
648 |
"%s: %ss '%s' and '%s' are different sizes\n", |
649 |
progname, class, f1name, f2name); |
650 |
return(0); |
651 |
} |
652 |
return(1); |
653 |
} |
654 |
|
655 |
/* Compare two inputs that are known to be RGBE or XYZE images */ |
656 |
static int |
657 |
compare_hdr() |
658 |
{ |
659 |
RESOLU rs1, rs2; |
660 |
COLOR *scan1, *scan2; |
661 |
int x, y; |
662 |
|
663 |
if (report >= REP_VERBOSE) { |
664 |
fputs(progname, stdout); |
665 |
fputs(": comparing inputs as HDR images\n", stdout); |
666 |
} |
667 |
fgetsresolu(&rs1, f1in); |
668 |
fgetsresolu(&rs2, f2in); |
669 |
if (!check_resolu("HDR image", &rs1, &rs2)) |
670 |
return(0); |
671 |
scan1 = (COLOR *)malloc(scanlen(&rs1)*sizeof(COLOR)); |
672 |
scan2 = (COLOR *)malloc(scanlen(&rs2)*sizeof(COLOR)); |
673 |
if (!scan1 | !scan2) { |
674 |
fprintf(stderr, "%s: out of memory in compare_hdr()\n", progname); |
675 |
exit(2); |
676 |
} |
677 |
for (y = 0; y < numscans(&rs1); y++) { |
678 |
if ((freadscan(scan1, scanlen(&rs1), f1in) < 0) | |
679 |
(freadscan(scan2, scanlen(&rs2), f2in) < 0)) { |
680 |
if (report != REP_QUIET) |
681 |
printf("%s: unexpected end-of-file\n", |
682 |
progname); |
683 |
free(scan1); |
684 |
free(scan2); |
685 |
return(0); |
686 |
} |
687 |
for (x = 0; x < scanlen(&rs1); x++) { |
688 |
if (color_check(scan1[x], scan2[x])) |
689 |
continue; |
690 |
if (report != REP_QUIET) { |
691 |
printf( |
692 |
"%s: pixels at scanline %d offset %d differ\n", |
693 |
progname, y, x); |
694 |
if (report >= REP_VERBOSE) { |
695 |
printf("%s: (R,G,B)=(%g,%g,%g)\n", |
696 |
f1name, colval(scan1[x],RED), |
697 |
colval(scan1[x],GRN), |
698 |
colval(scan1[x],BLU)); |
699 |
printf("%s: (R,G,B)=(%g,%g,%g)\n", |
700 |
f2name, colval(scan2[x],RED), |
701 |
colval(scan2[x],GRN), |
702 |
colval(scan2[x],BLU)); |
703 |
} |
704 |
} |
705 |
free(scan1); |
706 |
free(scan2); |
707 |
return(0); |
708 |
} |
709 |
} |
710 |
free(scan1); |
711 |
free(scan2); |
712 |
return(good_RMS()); /* final check of RMS */ |
713 |
} |
714 |
|
715 |
/* Set reference depth based on header variable */ |
716 |
static int |
717 |
set_refdepth(DEPTHCODEC *dcp, LUTAB *htp) |
718 |
{ |
719 |
static char depthvar[] = DEPTHSTR; |
720 |
const char *drval; |
721 |
|
722 |
depthvar[LDEPTHSTR-1] = '\0'; |
723 |
drval = (const char *)lu_find(htp, depthvar)->data; |
724 |
if (!drval) |
725 |
return(0); |
726 |
dcp->refdepth = atof(drval); |
727 |
if (dcp->refdepth <= 0) { |
728 |
if (report != REP_QUIET) { |
729 |
fputs(dcp->inpname, stderr); |
730 |
fputs(": bad reference depth '", stderr); |
731 |
fputs(drval, stderr); |
732 |
fputs("'\n", stderr); |
733 |
} |
734 |
return(-1); |
735 |
} |
736 |
return(1); |
737 |
} |
738 |
|
739 |
/* Compare two encoded depth maps */ |
740 |
static int |
741 |
compare_depth() |
742 |
{ |
743 |
long nread = 0; |
744 |
DEPTHCODEC dc1, dc2; |
745 |
|
746 |
if (report >= REP_VERBOSE) { |
747 |
fputs(progname, stdout); |
748 |
fputs(": comparing inputs as depth maps\n", stdout); |
749 |
} |
750 |
set_dc_defaults(&dc1); |
751 |
dc1.hdrflags = HF_RESIN; |
752 |
dc1.finp = f1in; |
753 |
dc1.inpname = f1name; |
754 |
set_dc_defaults(&dc2); |
755 |
dc2.hdrflags = HF_RESIN; |
756 |
dc2.finp = f2in; |
757 |
dc2.inpname = f2name; |
758 |
if (report != REP_QUIET) { |
759 |
dc1.hdrflags |= HF_STDERR; |
760 |
dc2.hdrflags |= HF_STDERR; |
761 |
} |
762 |
if (!process_dc_header(&dc1, 0, NULL)) |
763 |
return(0); |
764 |
if (!process_dc_header(&dc2, 0, NULL)) |
765 |
return(0); |
766 |
if (!check_resolu("Depth map", &dc1.res, &dc2.res)) |
767 |
return(0); |
768 |
if (set_refdepth(&dc1, &hdr1) < 0) |
769 |
return(0); |
770 |
if (set_refdepth(&dc2, &hdr2) < 0) |
771 |
return(0); |
772 |
while (nread < dc1.res.xr*dc1.res.yr) { |
773 |
double d1 = decode_depth_next(&dc1); |
774 |
double d2 = decode_depth_next(&dc2); |
775 |
if ((d1 < 0) | (d2 < 0)) { |
776 |
if (report != REP_QUIET) |
777 |
printf("%s: unexpected end-of-file\n", |
778 |
progname); |
779 |
return(0); |
780 |
} |
781 |
++nread; |
782 |
if (real_check(d1, d2)) |
783 |
continue; |
784 |
if (report != REP_QUIET) |
785 |
printf("%s: %ld%s depth values differ\n", |
786 |
progname, nread, num_sfx(nread)); |
787 |
return(0); |
788 |
} |
789 |
return(good_RMS()); /* final check of RMS */ |
790 |
} |
791 |
|
792 |
/* Compare two encoded normal maps */ |
793 |
static int |
794 |
compare_norm() |
795 |
{ |
796 |
long nread = 0; |
797 |
NORMCODEC nc1, nc2; |
798 |
|
799 |
if (report >= REP_VERBOSE) { |
800 |
fputs(progname, stdout); |
801 |
fputs(": comparing inputs as normal maps\n", stdout); |
802 |
} |
803 |
set_nc_defaults(&nc1); |
804 |
nc1.hdrflags = HF_RESIN; |
805 |
nc1.finp = f1in; |
806 |
nc1.inpname = f1name; |
807 |
set_nc_defaults(&nc2); |
808 |
nc2.hdrflags = HF_RESIN; |
809 |
nc2.finp = f2in; |
810 |
nc2.inpname = f2name; |
811 |
if (report != REP_QUIET) { |
812 |
nc1.hdrflags |= HF_STDERR; |
813 |
nc2.hdrflags |= HF_STDERR; |
814 |
} |
815 |
if (!process_nc_header(&nc1, 0, NULL)) |
816 |
return(0); |
817 |
if (!process_nc_header(&nc2, 0, NULL)) |
818 |
return(0); |
819 |
if (!check_resolu("Normal map", &nc1.res, &nc2.res)) |
820 |
return(0); |
821 |
while (nread < nc1.res.xr*nc1.res.yr) { |
822 |
FVECT nv1, nv2; |
823 |
int rv1 = decode_normal_next(nv1, &nc1); |
824 |
int rv2 = decode_normal_next(nv2, &nc2); |
825 |
if ((rv1 < 0) | (rv2 < 0)) { |
826 |
if (report != REP_QUIET) |
827 |
printf("%s: unexpected end-of-file\n", |
828 |
progname); |
829 |
return(0); |
830 |
} |
831 |
++nread; |
832 |
if (rv1 == rv2 && (!rv1 || norm_check(nv1, nv2))) |
833 |
continue; |
834 |
if (report != REP_QUIET) |
835 |
printf("%s: %ld%s normal vectors differ\n", |
836 |
progname, nread, num_sfx(nread)); |
837 |
return(0); |
838 |
} |
839 |
return(good_RMS()); /* final check of RMS */ |
840 |
} |
841 |
|
842 |
/* Compare two inputs that are known to be 32-bit floating-point data */ |
843 |
static int |
844 |
compare_float() |
845 |
{ |
846 |
long nread = 0; |
847 |
float f1, f2; |
848 |
|
849 |
if (report >= REP_VERBOSE) { |
850 |
fputs(progname, stdout); |
851 |
fputs(": comparing inputs as 32-bit IEEE floats\n", stdout); |
852 |
} |
853 |
while (getbinary(&f1, sizeof(f1), 1, f1in)) { |
854 |
if (!getbinary(&f2, sizeof(f2), 1, f2in)) |
855 |
goto badeof; |
856 |
++nread; |
857 |
if (f1swap) swap32((char *)&f1, 1); |
858 |
if (f2swap) swap32((char *)&f2, 1); |
859 |
if (real_check(f1, f2)) |
860 |
continue; |
861 |
if (report != REP_QUIET) |
862 |
printf("%s: %ld%s float values differ\n", |
863 |
progname, nread, num_sfx(nread)); |
864 |
return(0); |
865 |
} |
866 |
if (!getbinary(&f2, sizeof(f2), 1, f2in)) |
867 |
return(good_RMS()); /* final check of RMS */ |
868 |
badeof: |
869 |
if (report != REP_QUIET) |
870 |
printf("%s: unexpected end-of-file\n", progname); |
871 |
return(0); |
872 |
} |
873 |
|
874 |
/* Compare two inputs that are known to be 64-bit floating-point data */ |
875 |
static int |
876 |
compare_double() |
877 |
{ |
878 |
long nread = 0; |
879 |
double f1, f2; |
880 |
|
881 |
if (report >= REP_VERBOSE) { |
882 |
fputs(progname, stdout); |
883 |
fputs(": comparing inputs as 64-bit IEEE doubles\n", stdout); |
884 |
} |
885 |
while (getbinary(&f1, sizeof(f1), 1, f1in)) { |
886 |
if (!getbinary(&f2, sizeof(f2), 1, f2in)) |
887 |
goto badeof; |
888 |
++nread; |
889 |
if (f1swap) swap64((char *)&f1, 1); |
890 |
if (f2swap) swap64((char *)&f2, 1); |
891 |
if (real_check(f1, f2)) |
892 |
continue; |
893 |
if (report != REP_QUIET) |
894 |
printf("%s: %ld%s double values differ\n", |
895 |
progname, nread, num_sfx(nread)); |
896 |
return(0); |
897 |
} |
898 |
if (!getbinary(&f2, sizeof(f2), 1, f2in)) |
899 |
return(good_RMS()); /* final check of RMS */ |
900 |
badeof: |
901 |
if (report != REP_QUIET) |
902 |
printf("%s: unexpected end-of-file\n", progname); |
903 |
return(0); |
904 |
} |
905 |
|
906 |
/* Compare two Radiance files for equivalence */ |
907 |
int |
908 |
main(int argc, char *argv[]) |
909 |
{ |
910 |
int typ1, typ2; |
911 |
int a; |
912 |
|
913 |
progname = argv[0]; |
914 |
for (a = 1; a < argc && argv[a][0] == '-'; a++) { |
915 |
switch (argv[a][1]) { |
916 |
case 'h': /* ignore header info. */ |
917 |
ign_header = !ign_header; |
918 |
continue; |
919 |
case 'c': /* ignore comments */ |
920 |
comment_c = argv[a][2]; |
921 |
continue; |
922 |
case 's': /* silent operation */ |
923 |
report = REP_QUIET; |
924 |
continue; |
925 |
case 'w': /* turn off warnings */ |
926 |
if (report > REP_ERROR) |
927 |
report = REP_ERROR; |
928 |
continue; |
929 |
case 'v': /* turn on verbose mode */ |
930 |
report = REP_VERBOSE; |
931 |
continue; |
932 |
case 'm': /* maximum epsilon */ |
933 |
max_lim = atof(argv[++a]); |
934 |
continue; |
935 |
case 'r': |
936 |
if (argv[a][2] == 'e') /* relative difference */ |
937 |
rel_min = atof(argv[++a]); |
938 |
else if (argv[a][2] == 'm') /* RMS limit */ |
939 |
rms_lim = atof(argv[++a]); |
940 |
else |
941 |
usage(); |
942 |
continue; |
943 |
case '\0': /* first file from stdin */ |
944 |
f1in = stdin; |
945 |
f1name = stdin_name; |
946 |
break; |
947 |
default: |
948 |
usage(); |
949 |
} |
950 |
break; |
951 |
} |
952 |
if (a != argc-2) /* make sure of two inputs */ |
953 |
usage(); |
954 |
if (!strcmp(argv[a], argv[a+1])) { /* inputs are same? */ |
955 |
if (report >= REP_WARN) |
956 |
printf("%s: warning - identical inputs given\n", |
957 |
progname); |
958 |
return(0); |
959 |
} |
960 |
if (!f1name) f1name = argv[a]; |
961 |
if (!f2name) f2name = argv[a+1]; |
962 |
/* open inputs */ |
963 |
SET_FILE_BINARY(stdin); /* in case we're using it */ |
964 |
if (!f1in && !(f1in = fopen(f1name, "rb"))) { |
965 |
fprintf(stderr, "%s: cannot open for reading\n", f1name); |
966 |
return(2); |
967 |
} |
968 |
if (!strcmp(f2name, "-")) { |
969 |
f2in = stdin; |
970 |
f2name = stdin_name; |
971 |
} else if (!(f2in = fopen(f2name, "rb"))) { |
972 |
fprintf(stderr, "%s: cannot open for reading\n", f2name); |
973 |
return(2); |
974 |
} |
975 |
/* load headers */ |
976 |
if ((typ1 = identify_type(f1name, f1in, &hdr1)) < 0) |
977 |
return(2); |
978 |
if ((typ2 = identify_type(f2name, f2in, &hdr2)) < 0) |
979 |
return(2); |
980 |
if (typ1 != typ2) { |
981 |
if (report != REP_QUIET) |
982 |
printf("%s: '%s' format is %s and '%s' is %s\n", |
983 |
progname, f1name, file_type[typ1], |
984 |
f2name, file_type[typ2]); |
985 |
return(1); |
986 |
} |
987 |
ign_header |= !has_header(typ1); /* check headers if indicated */ |
988 |
if (!ign_header && !headers_match()) |
989 |
return(1); |
990 |
if (!ign_header & (report >= REP_WARN)) { |
991 |
if (lin1cnt != lin2cnt) |
992 |
printf("%s: warning - headers are different lengths\n", |
993 |
progname); |
994 |
if (typ1 == TYP_UNKNOWN) |
995 |
printf("%s: warning - unrecognized format\n", |
996 |
progname); |
997 |
} |
998 |
if (report >= REP_VERBOSE) { |
999 |
printf("%s: data format is %s\n", progname, file_type[typ1]); |
1000 |
if ((typ1 == TYP_FLOAT) | (typ1 == TYP_DOUBLE)) { |
1001 |
if (f1swap) |
1002 |
printf("%s: input '%s' is byte-swapped\n", |
1003 |
progname, f1name); |
1004 |
if (f2swap) |
1005 |
printf("%s: input '%s' is byte-swapped\n", |
1006 |
progname, f2name); |
1007 |
} |
1008 |
} |
1009 |
switch (typ1) { /* compare based on type */ |
1010 |
case TYP_BINARY: |
1011 |
case TYP_TMESH: |
1012 |
case TYP_OCTREE: |
1013 |
case TYP_RBFMESH: |
1014 |
case TYP_ID8: |
1015 |
case TYP_ID16: |
1016 |
case TYP_ID24: |
1017 |
case TYP_UNKNOWN: |
1018 |
return( !compare_binary() ); |
1019 |
case TYP_TEXT: |
1020 |
case TYP_ASCII: |
1021 |
return( !compare_text() ); |
1022 |
case TYP_RGBE: |
1023 |
case TYP_XYZE: |
1024 |
return( !compare_hdr() ); |
1025 |
case TYP_DEPTH: |
1026 |
return( !compare_depth() ); |
1027 |
case TYP_NORM: |
1028 |
return( !compare_norm() ); |
1029 |
case TYP_FLOAT: |
1030 |
return( !compare_float() ); |
1031 |
case TYP_DOUBLE: |
1032 |
return( !compare_double() ); |
1033 |
} |
1034 |
return(1); |
1035 |
} |