RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
HasselbladDecompressor.cpp
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2009-2014 Klaus Post
5 Copyright (C) 2017 Axel Waggershauser
6 Copyright (C) 2023 Roman Lebedev
7
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
12
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public
19 License along with this library; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21*/
22
24#include "adt/Array1DRef.h"
25#include "adt/Array2DRef.h"
26#include "adt/Invariant.h"
27#include "adt/Point.h"
30#include "common/RawImage.h"
32#include "io/ByteStream.h"
33#include <cstdint>
34#include <utility>
35
36namespace rawspeed {
37
39 const PerComponentRecipe& rec_,
41 : mRaw(std::move(mRaw_)), rec(rec_), input(input_) {
42 if (mRaw->getDataType() != RawImageType::UINT16)
43 ThrowRDE("Unexpected data type");
44
45 if (mRaw->getCpp() != 1 || mRaw->getBpp() != sizeof(uint16_t))
46 ThrowRDE("Unexpected cpp: %u", mRaw->getCpp());
47
48 // FIXME: could be wrong. max "active pixels" - "100 MP"
49 if (!mRaw->dim.hasPositiveArea() || mRaw->dim.x % 2 != 0 ||
50 mRaw->dim.x > 12000 || mRaw->dim.y > 8842) {
51 ThrowRDE("Unexpected image dimensions found: (%d; %d)", mRaw->dim.x,
52 mRaw->dim.y);
53 }
54
55 if (rec.ht.isFullDecode())
56 ThrowRDE("Huffman table is of a full decoding variety");
57}
58
59// Returns len bits as a signed value.
60// Highest bit is a sign bit
62 if (!len)
63 return 0;
64 int diff = bs.getBits(len);
65 diff = PrefixCodeDecoder<>::extend(diff, len);
66 if (diff == 65535)
67 return -32768;
68 return diff;
69}
70
72 const Array2DRef<uint16_t> out(mRaw->getU16DataAsUncroppedArray2DRef());
73
74 invariant(out.height() > 0);
75 invariant(out.width() > 0);
76 invariant(out.width() % 2 == 0);
77
78 const auto ht = rec.ht;
79 ht.verifyCodeValuesAsDiffLengths();
80
81 BitStreamerMSB32 bitStreamer(input);
82 // Pixels are packed two at a time, not like LJPEG:
83 // [p1_length_as_huffman][p2_length_as_huffman][p0_diff_with_length][p1_diff_with_length]|NEXT
84 // PIXELS
85 for (int row = 0; row < out.height(); row++) {
86 int p1 = rec.initPred;
87 int p2 = rec.initPred;
88 for (int col = 0; col < out.width(); col += 2) {
89 int len1 = ht.decodeCodeValue(bitStreamer);
90 int len2 = ht.decodeCodeValue(bitStreamer);
91 p1 += getBits(bitStreamer, len1);
92 p2 += getBits(bitStreamer, len2);
93 // NOTE: this is rather unusual and weird, but appears to be correct.
94 // clampBits(p, 16) results in completely garbled images.
95 out(row, col) = uint16_t(p1);
96 out(row, col + 1) = uint16_t(p2);
97 }
98 }
99 return bitStreamer.getStreamPosition();
100}
101
102} // namespace rawspeed
#define invariant(expr)
Definition Invariant.h:27
#define ThrowRDE(...)
static int RAWSPEED_READNONE extend(uint32_t diff, uint32_t len)
int RAWSPEED_READONLY height() const
int RAWSPEED_READONLY width() const
size_type getStreamPosition() const
uint32_t getBits(int nbits)
uint32_t size_type
Definition Buffer.h:49
static int getBits(BitStreamerMSB32 &bs, int len)
HasselbladDecompressor(RawImage mRaw, const PerComponentRecipe &rec, Array1DRef< const uint8_t > input)
const Array1DRef< const uint8_t > input