RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
AbstractPrefixCodeTranscoder.h
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2017 Axel Waggershauser
5 Copyright (C) 2017-2024 Roman Lebedev
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20*/
21
22#pragma once
23
24#include "rawspeedconfig.h"
25#include "adt/Invariant.h"
27#include "codes/PrefixCode.h"
29#include <algorithm>
30#include <cassert>
31#include <cstddef>
32
33namespace rawspeed {
34
35template <typename CodeTag> class AbstractPrefixCodeTranscoder {
36 bool fullDecode = true;
37 bool fixDNGBug16 = false;
38
39public:
40 using Tag = CodeTag;
44
46
48 : code(std::move(code_)) {}
49
51 for (const auto cValue : code.Base::codeValues) {
52 if (cValue <= Traits::MaxDiffLength)
53 continue;
54 ThrowRDE("Corrupt Huffman code: difference length %u longer than %u",
55 cValue, Traits::MaxDiffLength);
56 }
57 assert(maxCodePlusDiffLength() <= 32U);
58 }
59
60protected:
61 [[nodiscard]] size_t RAWSPEED_READONLY maxCodeLength() const {
62 return code.nCodesPerLength.size() - 1;
63 }
64
65 [[nodiscard]] size_t RAWSPEED_READONLY __attribute__((pure))
66 maxCodePlusDiffLength() const {
67 return maxCodeLength() + *(std::max_element(code.Base::codeValues.cbegin(),
68 code.Base::codeValues.cend()));
69 }
70
71 void setup(bool fullDecode_, bool fixDNGBug16_) {
72 invariant(!fullDecode_ || Traits::SupportsFullDecode);
73
74 this->fullDecode = fullDecode_;
75 this->fixDNGBug16 = fixDNGBug16_;
76
77 if (fullDecode) {
78 // If we are in a full-decoding mode, we will be interpreting code values
79 // as bit length of the following difference, which incurs hard limit
80 // of 16 (since we want to need to read at most 32 bits max for a symbol
81 // plus difference). Though we could enforce it per-code instead?
83 }
84 }
85
86public:
87 [[nodiscard]] bool RAWSPEED_READONLY isFullDecode() const {
88 return fullDecode;
89 }
90 [[nodiscard]] bool RAWSPEED_READONLY handleDNGBug16() const {
91 return fixDNGBug16;
92 }
93
94 bool operator==(const AbstractPrefixCodeTranscoder& other) const {
95 return code.symbols == other.code.symbols &&
96 code.Base::codeValues == other.code.Base::codeValues;
97 }
98};
99
100} // namespace rawspeed
#define invariant(expr)
Definition Invariant.h:27
#define ThrowRDE(...)
assert(dim.area() >=area)
typename AbstractPrefixCode< CodeTag >::Traits Traits
bool operator==(const AbstractPrefixCodeTranscoder &other) const
typename AbstractPrefixCode< CodeTag >::CodeSymbol CodeSymbol
size_t RAWSPEED_READONLY __attribute__((pure)) maxCodePlusDiffLength() const
AbstractPrefixCodeTranscoder(PrefixCode< CodeTag > code_)
void setup(bool fullDecode_, bool fixDNGBug16_)