-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathscm-sample.cpp
181 lines (142 loc) · 5.2 KB
/
scm-sample.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright (C) 2011-2012 Robert Kooima
//
// LIBSCM is free software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITH-
// OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
#include <cstdlib>
#include <cmath>
#include "util3d/math3d.h"
#include "scm-sample.hpp"
#include "scm-index.hpp"
#include "scm-file.hpp"
#include "scm-log.hpp"
//------------------------------------------------------------------------------
/// Create a new SCM TIFF file sampler
///
/// The given scm_file object includes the path and parameters of the TIFF
/// file. Open that TIFF and prepare to make cached access to it.
scm_sample::scm_sample(scm_file *file) : file(file)
{
last_v[0] = 0;
last_v[1] = 0;
last_v[2] = 0;
last_k = 0;
last_p = 0;
last_o = 0;
last_i0 = (tsize_t) (-1);
last_i1 = (tsize_t) (-1);
if ((tiff = TIFFOpen(file->get_path(), "r")))
{
tsize_t N = TIFFNumberOfStrips(tiff);
tsize_t S = TIFFStripSize (tiff);
last_p = (uint8 *) malloc(S * N);
scm_log("scm_sample constructor %s", file->get_path());
}
}
// Release the TIFF
scm_sample::~scm_sample()
{
scm_log("scm_sample destructor");
free(last_p);
if (tiff) TIFFClose(tiff);
}
//------------------------------------------------------------------------------
/// Decode the raw data in the current page cache, returning a pixel value.
///
/// @param y pixel row
/// @param x pixel column
float scm_sample::lookup(int y, int x) const
{
int w = file->get_w();
int c = file->get_c();
switch (file->get_b())
{
case 8: return ((unsigned char *) last_p)[(w * y + x) * c] / 255.f;
case 16: return ((unsigned short *) last_p)[(w * y + x) * c] / 65535.f;
case 32: return (( float *) last_p)[(w * y + x) * c];
default: return 1.f;
}
}
/// Perform a sample along a vector
///
/// Seek the deepest page that contains the given vector and return a linearly-
/// filtered sample of it. In the interest of performance, cache the page and
/// cache the most recent result. In the interest of minimizing latency, read
/// only a single scanline at a time.
///
/// @param v Vector from the center of the sphere to the sample point
float scm_sample::get(const double *v)
{
if (file && tiff)
{
if (v[0] != last_v[0] || v[1] != last_v[1] || v[2] != last_v[2])
{
// Locate the face and coordinates of vector v.
long long a;
double y;
double x;
scm_locate(&a, &y, &x, v);
x = 1 - x;
// Find the deepest page covering this location.
uint64 o = file->find_page(a, y, x);
// Convert the root face coordinate to a local face coordinate.
double r = y * (file->get_h() - 2.0) + 0.5;
double c = x * (file->get_w() - 2.0) + 0.5;
int r0 = int(floor(r)), r1 = r0 + 1;
int c0 = int(floor(c)), c1 = c0 + 1;
tsize_t i0 = TIFFComputeStrip(tiff, r0, 0);
tsize_t i1 = TIFFComputeStrip(tiff, r1, 0);
// If the required page is not current, set it.
if (last_o != o)
{
if (TIFFSetSubDirectory(tiff, o))
{
last_i0 = (tsize_t) -1;
last_i1 = (tsize_t) -1;
last_o = o;
}
}
// If the required data is not cached, load it.
if (last_o == o)
{
if (last_i0 != i0 || last_i1 != i1)
{
tsize_t S = TIFFStripSize(tiff);
if (i0 == i1)
TIFFReadEncodedStrip(tiff, i0, last_p + i0 * S, S);
else
{
TIFFReadEncodedStrip(tiff, i0, last_p + i0 * S, S);
TIFFReadEncodedStrip(tiff, i1, last_p + i1 * S, S);
}
last_i0 = i0;
last_i1 = i1;
}
if (last_i0 == i0 && last_i1 == i1)
{
// Sample the cache with linear filtering.
float s00 = lookup(r0, c0);
float s01 = lookup(r0, c1);
float s10 = lookup(r1, c0);
float s11 = lookup(r1, c1);
double rr = r - floor(r);
double cc = c - floor(c);
// Cache the request and its result.
last_v[0] = v[0];
last_v[1] = v[1];
last_v[2] = v[2];
last_k = float(lerp(lerp(s00, s01, cc),
lerp(s10, s11, cc), rr));
}
}
}
}
return last_k;
}
//------------------------------------------------------------------------------