-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathRawImage.h
More file actions
395 lines (333 loc) · 13 KB
/
Copy pathRawImage.h
File metadata and controls
395 lines (333 loc) · 13 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
/*
RawSpeed - RAW file decoder.
Copyright (C) 2009-2014 Klaus Post
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#pragma once
#include "rawspeedconfig.h"
#include "ThreadSafetyAnalysis.h"
#include "adt/AlignedAllocator.h"
#include "adt/Array2DRef.h"
#include "adt/Casts.h"
#include "adt/CroppedArray2DRef.h"
#include "adt/DefaultInitAllocatorAdaptor.h"
#include "adt/Mutex.h"
#include "adt/NotARational.h"
#include "adt/Optional.h"
#include "adt/Point.h"
#include "common/Common.h"
#include "common/ErrorLog.h"
#include "common/TableLookUp.h"
#include "metadata/BlackArea.h"
#include "metadata/ColorFilterArray.h"
#include <array>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>
namespace rawspeed {
class RawImage;
class RawImageData;
enum class RawImageType : uint8_t { UINT16, F32 };
class RawImageWorker final {
public:
enum class RawImageWorkerTask : uint16_t {
SCALE_VALUES = 1,
FIX_BAD_PIXELS = 2,
APPLY_LOOKUP = 3 | 0x1000,
FULL_IMAGE = 0x1000
};
private:
RawImageData* data;
RawImageWorkerTask task;
int start_y;
int end_y;
void performTask() noexcept;
public:
RawImageWorker(RawImageData* img, RawImageWorkerTask task, int start_y,
int end_y) noexcept;
};
class ImageMetaData final {
public:
// Aspect ratio of the pixels, usually 1 but some cameras need scaling
// <1 means the image needs to be stretched vertically, (0.5 means 2x)
// >1 means the image needs to be stretched horizontally (2 mean 2x)
double pixelAspectRatio = 1;
// White balance coefficients of the image
Optional<std::array<float, 4>> wbCoeffs;
// If not empty, a row-major color matrix,
// that converts XYZ values to reference camera native color space values,
// under calibration illuminant 21 (D65).
std::vector<NotARational<int>> colorMatrix;
// How many pixels far down the left edge and far up the right edge the image
// corners are when the image is rotated 45 degrees in Fuji rotated sensors.
uint32_t fujiRotationPos = 0;
iPoint2D subsampling = {1, 1};
std::string make;
std::string model;
std::string mode;
std::string canonical_make;
std::string canonical_model;
std::string canonical_alias;
std::string canonical_id;
// ISO speed. If known the value is set, otherwise it will be '0'.
int isoSpeed = 0;
};
class RawImageData : public ErrorLog {
virtual void anchor() const;
friend class RawImageWorker;
public:
virtual ~RawImageData() = default;
[[nodiscard]] uint32_t RAWSPEED_READONLY getCpp() const { return cpp; }
[[nodiscard]] uint32_t RAWSPEED_READONLY getBpp() const { return bpp; }
void setCpp(uint32_t val);
void createData();
void poisonPadding();
void unpoisonPadding();
[[nodiscard]] rawspeed::RawImageType getDataType() const { return dataType; }
[[nodiscard]] Array2DRef<uint16_t> getU16DataAsUncroppedArray2DRef() noexcept;
[[nodiscard]] CroppedArray2DRef<uint16_t>
getU16DataAsCroppedArray2DRef() noexcept;
[[nodiscard]] Array2DRef<float> getF32DataAsUncroppedArray2DRef() noexcept;
[[nodiscard]] CroppedArray2DRef<float>
getF32DataAsCroppedArray2DRef() noexcept;
// WARNING: this is most certainly not what you want!
[[nodiscard]] Array2DRef<std::byte>
getByteDataAsUncroppedArray2DRef() noexcept;
void subFrame(iRectangle2D cropped);
void clearArea(iRectangle2D area);
[[nodiscard]] iPoint2D RAWSPEED_READONLY getUncroppedDim() const;
[[nodiscard]] iPoint2D RAWSPEED_READONLY getCropOffset() const;
virtual void scaleBlackWhite() = 0;
virtual void calculateBlackAreas() = 0;
virtual void setWithLookUp(uint16_t value, std::byte* dst,
uint32_t* random) = 0;
void sixteenBitLookup();
void transferBadPixelsToMap() REQUIRES(!mBadPixelMutex);
void fixBadPixels() REQUIRES(!mBadPixelMutex);
void setTable(const std::vector<uint16_t>& table_, bool dither);
void setTable(std::unique_ptr<TableLookUp> t);
[[nodiscard]] bool isAllocated() const { return !data.empty(); }
void createBadPixelMap();
iPoint2D dim;
int pitch = 0;
// padding is the size of the area after last pixel of line n
// and before the first pixel of line n+1
uint32_t padding = 0;
bool isCFA{true};
ColorFilterArray cfa;
int blackLevel = -1;
std::array<int, 4> blackLevelSeparateStorage;
Optional<Array2DRef<int>> blackLevelSeparate;
// A white level of the image, if known.
// NOTE: it is always correct to divide the pixel by `float(whiteLevel)`,
// to normalize the image.
// NOTE: for floating-point images, the white level is never non-integral,
// and thus >= 1.0f
Optional<int> whitePoint;
std::vector<BlackArea> blackAreas;
/* Vector containing the positions of bad pixels */
/* Format is x | (y << 16), so maximum pixel position is 65535 */
// Positions of zeroes that must be interpolated
std::vector<uint32_t> mBadPixelPositions GUARDED_BY(mBadPixelMutex);
std::vector<uint8_t, AlignedAllocator<uint8_t, 16>> mBadPixelMap;
uint32_t mBadPixelMapPitch = 0;
bool mDitherScale =
true; // Should upscaling be done with dither to minimize banding?
ImageMetaData metadata;
Mutex mBadPixelMutex; // Mutex for 'mBadPixelPositions, must be used if more
// than 1 thread is accessing vector
protected:
RawImageType dataType;
RawImageData() = default;
RawImageData(RawImageType type, const iPoint2D& dim, int bpp, int cpp = 1);
virtual void scaleValues(int start_y, int end_y) = 0;
virtual void doLookup(int start_y, int end_y) = 0;
virtual void fixBadPixel(uint32_t x, uint32_t y, int component = 0) = 0;
void fixBadPixelsThread(int start_y, int end_y);
void startWorker(RawImageWorker::RawImageWorkerTask task, bool cropped);
std::vector<uint8_t, DefaultInitAllocatorAdaptor<
uint8_t, AlignedAllocator<uint8_t, 16>>>
data;
int cpp = 1; // Components per pixel
int bpp = 0; // Bytes per pixel.
friend class RawImage;
iPoint2D mOffset;
iPoint2D uncropped_dim;
std::unique_ptr<TableLookUp> table;
};
class RawImageDataU16 final : public RawImageData {
public:
RawImageDataU16();
explicit RawImageDataU16(const iPoint2D& dim_, uint32_t cpp_ = 1);
void scaleBlackWhite() override;
void calculateBlackAreas() override;
void setWithLookUp(uint16_t value, std::byte* dst, uint32_t* random) override;
private:
void scaleValues_plain(int start_y, int end_y);
#ifdef WITH_SSE2
void scaleValues_SSE2(int start_y, int end_y);
#endif
void scaleValues(int start_y, int end_y) override;
void fixBadPixel(uint32_t x, uint32_t y, int component = 0) override;
void doLookup(int start_y, int end_y) override;
friend class RawImage;
};
class RawImageDataFloat final : public RawImageData {
public:
RawImageDataFloat();
explicit RawImageDataFloat(const iPoint2D& dim_, uint32_t cpp_ = 1);
void scaleBlackWhite() override;
void calculateBlackAreas() override;
void setWithLookUp(uint16_t value, std::byte* dst, uint32_t* random) override;
private:
void scaleValues(int start_y, int end_y) override;
void fixBadPixel(uint32_t x, uint32_t y, int component = 0) override;
[[noreturn]] void doLookup(int start_y, int end_y) override;
friend class RawImage;
};
class RawImage final {
public:
static RawImage create(RawImageType type = RawImageType::UINT16);
static RawImage create(const iPoint2D& dim,
RawImageType type = RawImageType::UINT16,
uint32_t componentsPerPixel = 1);
RawImageData* RAWSPEED_READONLY operator->() const { return &*p_; }
RawImageData& RAWSPEED_READONLY operator*() const { return *p_; }
explicit RawImage(RawImageData* p) = delete;
explicit RawImage(std::shared_ptr<RawImageData> p) : p_(std::move(p)) {}
RawImageData* get() { return &*p_; }
private:
std::shared_ptr<RawImageData> p_; // p_ is never NULL
};
inline RawImage RawImage::create(RawImageType type) {
switch (type) {
case RawImageType::UINT16:
return RawImage(std::make_shared<RawImageDataU16>());
case RawImageType::F32:
return RawImage(std::make_shared<RawImageDataFloat>());
}
writeLog(DEBUG_PRIO::ERROR, "RawImage::create: Unknown Image type!");
__builtin_unreachable();
}
inline RawImage RawImage::create(const iPoint2D& dim, RawImageType type,
uint32_t componentsPerPixel) {
switch (type) {
case RawImageType::UINT16:
return RawImage(std::make_shared<RawImageDataU16>(dim, componentsPerPixel));
case RawImageType::F32:
return RawImage(
std::make_shared<RawImageDataFloat>(dim, componentsPerPixel));
}
writeLog(DEBUG_PRIO::ERROR, "RawImage::create: Unknown Image type!");
__builtin_unreachable();
}
inline Array2DRef<uint16_t>
RawImageData::getU16DataAsUncroppedArray2DRef() noexcept {
assert(dataType == RawImageType::UINT16 &&
"Attempting to access floating-point buffer as uint16_t.");
assert(!data.empty() && "Data not yet allocated.");
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Wunknown-warning-option"
#pragma GCC diagnostic ignored "-Wunsafe-buffer-usage"
return {reinterpret_cast<uint16_t*>(data.data()), cpp * uncropped_dim.x,
uncropped_dim.y, static_cast<int>(pitch / sizeof(uint16_t))};
#pragma GCC diagnostic pop
}
inline CroppedArray2DRef<uint16_t>
RawImageData::getU16DataAsCroppedArray2DRef() noexcept {
return {getU16DataAsUncroppedArray2DRef(), cpp * mOffset.x, mOffset.y,
cpp * dim.x, dim.y};
}
inline Array2DRef<float>
RawImageData::getF32DataAsUncroppedArray2DRef() noexcept {
assert(dataType == RawImageType::F32 &&
"Attempting to access integer buffer as float.");
assert(!data.empty() && "Data not yet allocated.");
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpragmas"
#pragma GCC diagnostic ignored "-Wunknown-warning-option"
#pragma GCC diagnostic ignored "-Wunsafe-buffer-usage"
return {reinterpret_cast<float*>(data.data()), cpp * uncropped_dim.x,
uncropped_dim.y, static_cast<int>(pitch / sizeof(float))};
#pragma GCC diagnostic pop
}
inline CroppedArray2DRef<float>
RawImageData::getF32DataAsCroppedArray2DRef() noexcept {
return {getF32DataAsUncroppedArray2DRef(), cpp * mOffset.x, mOffset.y,
cpp * dim.x, dim.y};
}
inline Array2DRef<std::byte>
RawImageData::getByteDataAsUncroppedArray2DRef() noexcept {
switch (dataType) {
case RawImageType::UINT16:
return getU16DataAsUncroppedArray2DRef();
case RawImageType::F32:
return getF32DataAsUncroppedArray2DRef();
}
__builtin_unreachable();
}
// setWithLookUp will set a single pixel by using the lookup table if supplied,
// You must supply the destination where the value should be written, and a
// pointer to a value that will be used to store a random counter that can be
// reused between calls. this needs to be inline to speed up tight decompressor
// loops
inline void RawImageDataU16::setWithLookUp(uint16_t value, std::byte* dst,
uint32_t* random) {
auto* dest = reinterpret_cast<uint16_t*>(dst);
if (table == nullptr) {
*dest = value;
return;
}
if (table->dither) {
uint32_t base = table->tables[(2 * value) + 0];
uint32_t delta = table->tables[(2 * value) + 1];
uint32_t r = *random;
uint32_t pix = base + ((delta * (r & 2047) + 1024) >> 12);
*random = 15700 * (r & 65535) + (r >> 16);
*dest = implicit_cast<uint16_t>(pix);
return;
}
*dest = table->tables[value];
}
class RawImageCurveGuard final {
const RawImage* mRaw;
const std::vector<uint16_t>& curve;
const bool uncorrectedRawValues;
public:
RawImageCurveGuard() = delete;
RawImageCurveGuard(const RawImageCurveGuard&) = delete;
RawImageCurveGuard(RawImageCurveGuard&&) noexcept = delete;
RawImageCurveGuard& operator=(const RawImageCurveGuard&) noexcept = delete;
RawImageCurveGuard& operator=(RawImageCurveGuard&&) noexcept = delete;
RawImageCurveGuard(const RawImage* raw, const std::vector<uint16_t>& curve_,
bool uncorrectedRawValues_)
: mRaw(raw), curve(curve_), uncorrectedRawValues(uncorrectedRawValues_) {
if (uncorrectedRawValues)
return;
(*mRaw)->setTable(curve, true);
}
~RawImageCurveGuard() {
// Set the table, if it should be needed later.
if (uncorrectedRawValues)
(*mRaw)->setTable(curve, false);
else
(*mRaw)->setTable(nullptr);
}
};
} // namespace rawspeed