-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathAbstractDngDecompressor.cpp
More file actions
290 lines (261 loc) · 8.83 KB
/
Copy pathAbstractDngDecompressor.cpp
File metadata and controls
290 lines (261 loc) · 8.83 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
/*
RawSpeed - RAW file decoder.
Copyright (C) 2009-2014 Klaus Post
Copyright (C) 2017-2018 Roman Lebeedv
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
*/
#include "rawspeedconfig.h"
#include "decompressors/AbstractDngDecompressor.h"
#include "adt/Array1DRef.h"
#include "adt/Casts.h"
#include "adt/Invariant.h"
#include "adt/Point.h"
#include "bitstreams/BitStreams.h"
#include "common/Common.h"
#include "common/RawImage.h"
#include "decoders/RawDecoderException.h"
#include "decompressors/LJpegDecoder.h"
#include "decompressors/UncompressedDecompressor.h"
#include "decompressors/VC5Decompressor.h"
#include "io/ByteStream.h"
#include "io/Endianness.h"
#include "io/IOException.h"
#include <cstdint>
#include <limits>
#include <string>
#include <vector>
#ifdef HAVE_ZLIB
#include "decompressors/DeflateDecompressor.h"
#include <memory>
#endif
#ifdef HAVE_JPEG
#include "decompressors/JpegDecompressor.h"
#endif
#ifdef HAVE_JPEGXL
#include "decompressors/JpegXlDecompressor.h"
#endif
namespace rawspeed {
template <> void AbstractDngDecompressor::decompressThread<1>() const noexcept {
#ifdef HAVE_OPENMP
#pragma omp for schedule(static)
#endif
for (const auto& e :
Array1DRef(slices.data(), implicit_cast<int>(slices.size()))) {
try {
iPoint2D tileSize(e.width, e.height);
iPoint2D pos(e.offX, e.offY);
bool big_endian = e.bs.getByteOrder() == Endianness::big;
// DNG spec says that if not 8/16/32 bit/sample, always use big endian.
// It's not very obvious, but that does not appear to apply to FP.
switch (mBps) {
case 8:
case 16:
case 32:
break;
default:
if (mRaw->getDataType() == RawImageType::UINT16)
big_endian = true;
break;
}
const uint32_t inputPixelBits = mRaw->getCpp() * mBps;
if (e.dsc.tileW > std::numeric_limits<int>::max() / inputPixelBits)
ThrowIOE("Integer overflow when calculating input pitch");
const int inputPitchBits = inputPixelBits * e.dsc.tileW;
invariant(inputPitchBits > 0);
if (inputPitchBits % 8 != 0) {
ThrowRDE("Bad combination of cpp (%u), bps (%u) and width (%u), the "
"pitch is %d bits, which is not a multiple of 8 (1 byte)",
mRaw->getCpp(), mBps, e.width, inputPitchBits);
}
const int inputPitch = inputPitchBits / 8;
if (inputPitch == 0)
ThrowRDE("Data input pitch is too short. Can not decode!");
UncompressedDecompressor decompressor(
e.bs, mRaw, iRectangle2D(pos, tileSize), inputPitch, mBps,
big_endian ? BitOrder::MSB : BitOrder::LSB);
decompressor.readUncompressedRaw();
} catch (const RawDecoderException& err) {
mRaw->setError(err.what());
} catch (const IOException& err) {
mRaw->setError(err.what());
} catch (...) {
// We should not get any other exception type here.
__builtin_unreachable();
}
}
}
template <> void AbstractDngDecompressor::decompressThread<7>() const noexcept {
#ifdef HAVE_OPENMP
#pragma omp for schedule(static)
#endif
for (const auto& e :
Array1DRef(slices.data(), implicit_cast<int>(slices.size()))) {
try {
LJpegDecoder d(e.bs, mRaw);
d.decode(e.offX, e.offY, e.width, e.height,
iPoint2D(e.dsc.tileW, e.dsc.tileH), mFixLjpeg);
} catch (const RawDecoderException& err) {
mRaw->setError(err.what());
} catch (const IOException& err) {
mRaw->setError(err.what());
} catch (...) {
// We should not get any other exception type here.
__builtin_unreachable();
}
}
}
#ifdef HAVE_ZLIB
template <> void AbstractDngDecompressor::decompressThread<8>() const noexcept {
// NOLINTNEXTLINE(modernize-avoid-c-arrays)
std::unique_ptr<unsigned char[]> uBuffer;
#ifdef HAVE_OPENMP
#pragma omp for schedule(static)
#endif
for (const auto& e :
Array1DRef(slices.data(), implicit_cast<int>(slices.size()))) {
try {
DeflateDecompressor z(e.bs.peekBuffer(e.bs.getRemainSize()), mRaw,
mPredictor, mBps);
z.decode(&uBuffer, iPoint2D(mRaw->getCpp() * e.dsc.tileW, e.dsc.tileH),
iPoint2D(mRaw->getCpp() * e.width, e.height),
iPoint2D(mRaw->getCpp() * e.offX, e.offY));
} catch (const RawDecoderException& err) {
mRaw->setError(err.what());
} catch (const IOException& err) {
mRaw->setError(err.what());
} catch (...) {
// We should not get any other exception type here.
__builtin_unreachable();
}
}
}
#endif
template <> void AbstractDngDecompressor::decompressThread<9>() const noexcept {
#ifdef HAVE_OPENMP
#pragma omp for schedule(static)
#endif
for (const auto& e :
Array1DRef(slices.data(), implicit_cast<int>(slices.size()))) {
try {
VC5Decompressor d(e.bs, mRaw);
d.decode(e.offX, e.offY, e.width, e.height);
} catch (const RawDecoderException& err) {
mRaw->setError(err.what());
} catch (const IOException& err) {
mRaw->setError(err.what());
} catch (...) {
// We should not get any other exception type here.
__builtin_unreachable();
}
}
}
#ifdef HAVE_JPEG
template <>
void AbstractDngDecompressor::decompressThread<0x884c>() const noexcept {
#ifdef HAVE_OPENMP
#pragma omp for schedule(static)
#endif
for (const auto& e :
Array1DRef(slices.data(), implicit_cast<int>(slices.size()))) {
try {
JpegDecompressor j(e.bs.peekBuffer(e.bs.getRemainSize()), mRaw);
j.decode(e.offX, e.offY);
} catch (const RawDecoderException& err) {
mRaw->setError(err.what());
} catch (const IOException& err) {
mRaw->setError(err.what());
} catch (...) {
// We should not get any other exception type here.
__builtin_unreachable();
}
}
}
#endif
#ifdef HAVE_JPEGXL
template <>
void AbstractDngDecompressor::decompressThread<52546>() const noexcept {
#ifdef HAVE_OPENMP
#pragma omp for schedule(static)
#endif
for (const auto& e :
Array1DRef(slices.data(), implicit_cast<int>(slices.size()))) {
try {
JpegXlDecompressor j(e.bs.peekBuffer(e.bs.getRemainSize()), mRaw);
j.decode(e.offX, e.offY);
} catch (const RawDecoderException& err) {
mRaw->setError(err.what());
} catch (const IOException& err) {
mRaw->setError(err.what());
} catch (...) {
// We should not get any other exception type here.
__builtin_unreachable();
}
}
}
#endif
void AbstractDngDecompressor::decompressThread() const noexcept {
invariant(mRaw->dim.x > 0);
invariant(mRaw->dim.y > 0);
invariant(mRaw->getCpp() > 0 && mRaw->getCpp() <= 4);
invariant(mBps > 0 && mBps <= 32);
if (compression == 1) {
/* Uncompressed */
decompressThread<1>();
} else if (compression == 7) {
/* Lossless JPEG */
decompressThread<7>();
} else if (compression == 8) {
/* Deflate compression */
#ifdef HAVE_ZLIB
decompressThread<8>();
#else
#pragma message \
"ZLIB is not present! Deflate compression will not be supported!"
mRaw->setError("deflate support is disabled.");
#endif
} else if (compression == 9) {
/* GOPRO VC-5 */
decompressThread<9>();
} else if (compression == 0x884c) {
/* Lossy DNG */
#ifdef HAVE_JPEG
decompressThread<0x884c>();
#else
#pragma message "JPEG is not present! Lossy JPEG DNG will not be supported!"
mRaw->setError("jpeg support is disabled.");
#endif
} else if (compression == 52546) {
/* JPEG XL (DNG 1.7) */
#ifdef HAVE_JPEGXL
decompressThread<52546>();
#else
#pragma message "JPEG XL is not present! DNG JPEG XL will not be supported!"
mRaw->setError("JPEG XL support is disabled.");
#endif
} else {
mRaw->setError("AbstractDngDecompressor: Unknown compression");
}
}
void AbstractDngDecompressor::decompress() const {
#ifdef HAVE_OPENMP
#pragma omp parallel default(none) num_threads( \
rawspeed_get_number_of_processor_cores()) if (slices.size() > 1)
#endif
decompressThread();
std::string firstErr;
if (mRaw->isTooManyErrors(1, &firstErr)) {
ThrowRDE("Too many errors encountered. Giving up. First Error:\n%s",
firstErr.c_str());
}
}
} // namespace rawspeed