Skip to content

Commit 131c543

Browse files
committed
Support interleaved CFA encoding
1 parent d7f32a8 commit 131c543

File tree

6 files changed

+42
-9
lines changed

6 files changed

+42
-9
lines changed

src/librawspeed/decoders/DngDecoder.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
#include <vector>
5555

5656
using std::map;
57+
using std::pair;
5758
using std::vector;
5859

5960
namespace rawspeed {
@@ -385,6 +386,13 @@ void DngDecoder::decodeData(const TiffIFD* raw, uint32_t sample_format) const {
385386
if (raw->hasEntry(TiffTag::PREDICTOR))
386387
predictor = raw->getEntry(TiffTag::PREDICTOR)->getU32();
387388

389+
pair<uint32_t, uint32_t> interleave{1U, 1U};
390+
if (raw->hasEntry(TiffTag::ROWINTERLEAVEFACTOR))
391+
interleave.first = raw->getEntry(TiffTag::ROWINTERLEAVEFACTOR)->getU32();
392+
if (raw->hasEntry(TiffTag::COLUMNINTERLEAVEFACTOR))
393+
interleave.second =
394+
raw->getEntry(TiffTag::COLUMNINTERLEAVEFACTOR)->getU32();
395+
388396
if (mRaw->getDataType() == RawImageType::UINT16) {
389397
// Default white level is (2 ** BitsPerSample) - 1
390398
mRaw->whitePoint = implicit_cast<int>((1UL << *bps) - 1UL);
@@ -414,7 +422,7 @@ void DngDecoder::decodeData(const TiffIFD* raw, uint32_t sample_format) const {
414422
}
415423

416424
AbstractDngDecompressor slices(mRaw, getTilingDescription(raw), compression,
417-
mFixLjpeg, *bps, predictor);
425+
mFixLjpeg, *bps, predictor, interleave);
418426

419427
slices.slices.reserve(slices.dsc.numTiles);
420428

src/librawspeed/decompressors/AbstractDngDecompressor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ void AbstractDngDecompressor::decompressThread<0xcd42>() const noexcept {
214214
for (const auto& e :
215215
Array1DRef(slices.data(), implicit_cast<int>(slices.size()))) {
216216
try {
217-
JpegXLDecompressor j(e.bs.peekBuffer(e.bs.getRemainSize()), mRaw);
217+
JpegXLDecompressor j(e.bs.peekBuffer(e.bs.getRemainSize()), mRaw,
218+
mInterleave);
218219
j.decode(e.offX, e.offY);
219220
} catch (const RawDecoderException& err) {
220221
mRaw->setError(err.what());

src/librawspeed/decompressors/AbstractDngDecompressor.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,11 @@ class AbstractDngDecompressor final : public AbstractDecompressor {
134134
public:
135135
AbstractDngDecompressor(RawImage img, const DngTilingDescription& dsc_,
136136
int compression_, bool mFixLjpeg_, uint32_t mBps_,
137-
uint32_t mPredictor_)
137+
uint32_t mPredictor_,
138+
std::pair<uint32_t, uint32_t> mInterleave_)
138139
: mRaw(std::move(img)), dsc(dsc_), compression(compression_),
139-
mFixLjpeg(mFixLjpeg_), mBps(mBps_), mPredictor(mPredictor_) {}
140+
mFixLjpeg(mFixLjpeg_), mBps(mBps_), mPredictor(mPredictor_),
141+
mInterleave(std::move(mInterleave_)) {}
140142

141143
void decompress() const;
142144

@@ -148,6 +150,7 @@ class AbstractDngDecompressor final : public AbstractDecompressor {
148150
const bool mFixLjpeg = false;
149151
const uint32_t mBps;
150152
const uint32_t mPredictor;
153+
const std::pair<uint32_t, uint32_t> mInterleave;
151154
};
152155

153156
} // namespace rawspeed

src/librawspeed/decompressors/JpegXLDecompressor.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <jxl/codestream_header.h>
3333
#include <jxl/decode.h>
3434
#include <jxl/types.h>
35+
#include <utility>
3536
#include <vector>
3637

3738
using std::min;
@@ -46,6 +47,13 @@ void JpegXLDecompressor::decode(
4647
if (signature != JXL_SIG_CODESTREAM && signature != JXL_SIG_CONTAINER)
4748
ThrowRDE("Unable to verify JPEG XL signature");
4849

50+
if (mInterleave != std::pair(1U, 1U) && mInterleave != std::pair(2U, 2U))
51+
ThrowRDE("Invalid interleave factors");
52+
53+
if (mInterleave == std::pair(2U, 2U) && mRaw->getCpp() != 1)
54+
ThrowRDE(
55+
"Invalid combination of interleave factors and components per pixel");
56+
4957
JxlDecoder* decoder = JxlDecoderCreate(nullptr);
5058

5159
if (!decoder)
@@ -129,18 +137,27 @@ void JpegXLDecompressor::decode(
129137

130138
const Array2DRef<uint16_t> tmp(complete_buffer.data(),
131139
basicinfo.num_color_channels * basicinfo.xsize,
132-
basicinfo.xsize);
140+
basicinfo.ysize);
133141

134142
// Now the image is decoded, and we copy the image data
135143
unsigned int copy_w = min(mRaw->dim.x - offX, basicinfo.xsize);
136144
unsigned int copy_h = min(mRaw->dim.y - offY, basicinfo.ysize);
137145

138146
const Array2DRef<uint16_t> out(mRaw->getU16DataAsUncroppedArray2DRef());
139147
for (unsigned int row = 0; row < copy_h; row++) {
148+
unsigned int rowbuf =
149+
(row / mInterleave.first) +
150+
(row % mInterleave.first) * (basicinfo.ysize / mInterleave.first);
140151
for (unsigned int col = 0; col < basicinfo.num_color_channels * copy_w;
141-
col++)
152+
col++) {
153+
unsigned int colbuf =
154+
(col / mInterleave.second) +
155+
(col % mInterleave.second) *
156+
((basicinfo.num_color_channels * basicinfo.xsize) /
157+
mInterleave.second);
142158
out(offY + row, basicinfo.num_color_channels * offX + col) =
143-
tmp(row, col);
159+
tmp(rowbuf, colbuf);
160+
}
144161
}
145162
}
146163

src/librawspeed/decompressors/JpegXLDecompressor.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,19 @@
2828
#include "decompressors/AbstractDecompressor.h"
2929
#include "io/Buffer.h"
3030
#include <cstdint>
31+
#include <utility>
3132

3233
namespace rawspeed {
3334

3435
class JpegXLDecompressor final : public AbstractDecompressor {
3536
Buffer input;
3637
RawImage mRaw;
38+
std::pair<uint32_t, uint32_t> mInterleave;
3739

3840
public:
39-
JpegXLDecompressor(Buffer bs, RawImage img)
40-
: input(bs), mRaw(std::move(img)) {}
41+
JpegXLDecompressor(Buffer bs, RawImage img,
42+
std::pair<uint32_t, uint32_t> interleave)
43+
: input(bs), mRaw(std::move(img)), mInterleave(std::move(interleave)) {}
4144

4245
void decode(uint32_t offsetX, uint32_t offsetY);
4346
};

src/librawspeed/tiff/TiffTag.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,7 @@ enum class TiffTag : uint16_t {
333333
OPCODELIST2 = 0xC741,
334334
OPCODELIST3 = 0xC742,
335335
NOISEPROFILE = 0xC761,
336+
COLUMNINTERLEAVEFACTOR = 0xCD43,
336337
CANONCR2SLICE = 0xC640, // CANON CR2
337338
CANON_SRAWTYPE = 0xC6C5, // IFD3
338339
CANON_SRAWQUALITY = 0x002E,

0 commit comments

Comments
 (0)