Repository navigation
Expand file tree
/
Copy pathimage_data.cc
More file actions
402 lines (350 loc) · 11.8 KB
/
Copy pathimage_data.cc
File metadata and controls
402 lines (350 loc) · 11.8 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
#include "image_data.h"
#include "csv_data.h"
#include "mod_data.h"
#include "pgm_data.h"
#include "vicar_data.h"
#include <stdexcept>
#include <algorithm>
#include <atomic>
#include <cctype>
#include <cmath>
#include <cstdio>
namespace rsvp
{
namespace
{
// Starts at 1 so that a cache stamped with 0 is one that has never
// been computed. It would take billions of moves to wrap back onto a
// value a live cache is holding, which no run comes close to.
std::atomic<unsigned long> &geometry_counter()
{
static std::atomic<unsigned long> counter(1);
return counter;
}
}
std::string get_file_extension(const std::string &path)
{
const size_t dot = path.find_last_of('.');
const size_t slash = path.find_last_of('/');
if (dot == std::string::npos ||
(slash != std::string::npos && dot < slash))
{
// No extension, or a dot in a directory name rather than the file
// name
return std::string();
}
std::string extension = path.substr(dot + 1);
std::transform(extension.begin(),
extension.end(),
extension.begin(),
[](unsigned char c) { return std::tolower(c); });
return extension;
}
unsigned long geometry_version()
{
return geometry_counter().load(std::memory_order_relaxed);
}
void invalidate_geometry()
{
geometry_counter().fetch_add(1, std::memory_order_relaxed);
}
ImageData::ImageData() :
alpha_band(-1),
interpolate(true)
{
}
ImageData::~ImageData() = default;
std::shared_ptr<ImageData> ImageData::read(const std::string &filename)
{
std::shared_ptr<ImageData> return_value;
const std::string extension = get_file_extension(filename);
if (extension == "mod" || extension == "mod_tc")
{
return_value = ModData::read_modfile(filename);
}
else if (extension == "img" || extension == "vic")
{
return_value = VicarData::read_vicarfile(filename);
}
else if (extension == "ht" || extension == "tc")
{
return_value = ModData::read_bare_vicarfile(filename);
}
else if (extension == "csv")
{
return_value = CSVData::read_csv(filename);
}
else if (extension == "pgm")
{
return_value = PGMData::read_pgm(filename);
}
else
{
throw std::runtime_error("Unrecognized file type: " + filename);
}
if (!return_value)
{
throw std::runtime_error("Reached parsing of invalid " + filename +
".");
}
return return_value;
}
bool ImageData::get_interpolated_pixel_double(double &value,
const double x,
const double y,
const int band) const
{
// If we've been asked not to interpolate data, snap to the nearest
// whole pixel and return that data
if (!get_interpolating())
{
return get_pixel_double(
value, round_to_int(x), round_to_int(y), band);
}
const BilinearCorners corners = bilinear_corners(x, y);
// Upper left (x, y)
double ul = 0.0;
if (!get_pixel_double(ul, corners.x0, corners.y0, band))
{
return false;
}
// Upper right (x+1, y)
double ur = 0.0;
if (!get_pixel_double(ur, corners.x1, corners.y0, band))
{
return false;
}
// Lower left (x, y+1)
double ll = 0.0;
if (!get_pixel_double(ll, corners.x0, corners.y1, band))
{
return false;
}
// Lower right (x+1, y+1)
double lr = 0.0;
if (!get_pixel_double(lr, corners.x1, corners.y1, band))
{
return false;
}
// Perform bilinear interpolation
value = ul * corners.weight_ul + ur * corners.weight_ur +
ll * corners.weight_ll + lr * corners.weight_lr;
return true;
}
bool ImageData::get_interpolated_bands_double(double *values,
const int *bands,
const int count,
const double x,
const double y) const
{
for (int i = 0; i < count; i++)
{
if (!get_interpolated_pixel_double(values[i], x, y, bands[i]))
{
return false;
}
}
return true;
}
bool ImageData::get_clamped_pixel_double(double &value,
double &weight,
const double x,
const double y,
const int band) const
{
const double last_x = get_width() - 1;
const double last_y = get_height() - 1;
if (last_x < 0.0 || last_y < 0.0)
{
// No pixels to clamp to
return false;
}
const double clamped_x = std::min(std::max(x, 0.0), last_x);
const double clamped_y = std::min(std::max(y, 0.0), last_y);
const double weight_x = 1.0 - fabs(x - clamped_x);
const double weight_y = 1.0 - fabs(y - clamped_y);
if (weight_x <= 0.0 || weight_y <= 0.0)
{
// More than a pixel outside the image, so this image has no say in
// what the value at (x, y) should be.
return false;
}
if (!get_interpolated_pixel_double(value, clamped_x, clamped_y, band))
{
return false;
}
weight = weight_x * weight_y;
return true;
}
// Provide a default implementation to get the value as a double and round
// it
bool ImageData::get_pixel_int(int &value,
const int x,
const int y,
const int band) const
{
double result = 0.0;
if (!get_pixel_double(result, x, y, band))
{
return false;
}
value = round_to_int(result);
return true;
}
// Provide a default implementation to get the value as a double and round
// it
bool ImageData::get_interpolated_pixel_int(int &value,
const double x,
const double y,
const int band) const
{
double result = 0.0;
if (!get_interpolated_pixel_double(result, x, y, band))
{
return false;
}
value = round_to_int(result);
return true;
}
void ImageData::set_interpolating(bool enable)
{
interpolate = enable;
}
int ImageData::get_interpolating() const
{
return interpolate;
}
void ImageData::set_alpha_band(int band)
{
alpha_band = band;
// A container caches which band of each child carries alpha, and that
// answer just changed
invalidate_geometry();
}
int ImageData::get_alpha_band() const
{
return alpha_band;
}
void ImageData::get_image_data(uint32_t *data_ptr,
const std::string &color,
uint8_t &pix_bytes) const
{
const int width = get_width();
const int height = get_height();
const int number_of_bands = get_bands();
if (color == "BAYER" || color == "RGB")
{
struct pixel_t
{
uint8_t red;
uint8_t green;
uint8_t blue;
} *data = reinterpret_cast<struct pixel_t *>(data_ptr);
pix_bytes = 4;
// If there are not enough bands to fill the bayer,
// these values will default to 0.
if (number_of_bands < 3)
{
fprintf(stderr,
"Requesting bayer image coloring with only "
"%d bands\n",
number_of_bands);
}
// Bayer format is RGGB with each field being 8 bits
// Green takes up 2 fields
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
pixel_t &pixel = data[y * width + x];
pixel.red = 0;
pixel.green = 0;
pixel.blue = 0;
// RED
if (number_of_bands >= 1)
{
int red_raw;
get_pixel_int(red_raw, x, y, 0);
pixel.red = red_raw & 0xff;
}
// GREEN_GREEN
if (number_of_bands >= 2)
{
int green_raw;
get_pixel_int(green_raw, x, y, 1);
pixel.green = green_raw & 0xff;
}
// BLUE
if (number_of_bands >= 3)
{
int blue_raw;
get_pixel_int(blue_raw, x, y, 2);
pixel.blue = blue_raw & 0xff;
}
}
}
}
else if (color == "PANCHROMATIC")
{
auto *data = reinterpret_cast<uint16_t *>(data_ptr);
pix_bytes = 2;
// We will average all bands (PANCHROMATIC)
// Single band images will simply copy their data
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int band_sum = 0;
for (int band = 0; band < number_of_bands; band++)
{
int pix_band = 0;
get_pixel_int(pix_band, x, y, band);
band_sum += pix_band;
}
int panchromatic_value = band_sum / number_of_bands;
data[y * width + x] = panchromatic_value;
}
}
}
else if (color == "BLUE" || color == "RED" || color == "GREEN")
{
auto *data = reinterpret_cast<uint16_t *>(data_ptr);
pix_bytes = 2;
int chosen_band = 0;
if (number_of_bands == 2)
{
// This happens with intensity+alpha
// images. The data band is '0'
}
else if (number_of_bands >= 3)
{
if (color == "RED")
{
chosen_band = 0;
}
else if (color == "GREEN")
{
chosen_band = 1;
}
else if (color == "BLUE")
{
chosen_band = 2;
}
}
// We assume the only band is BLUE
for (int y = 0; y < height; y++)
{
for (int x = 0; x < width; x++)
{
int pix_val;
get_pixel_int(pix_val, x, y, chosen_band);
data[y * width + x] = pix_val & 0xffff;
}
}
}
else
{
throw std::runtime_error("Invalid image color encoding: " + color);
}
}
}