RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
AbstractPrefixCodeDecoder.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-2018 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 <cstdint>
28
29namespace rawspeed {
30
31template <typename CodeTag>
33public:
35
36 using Tag = typename Base::Tag;
37 using Parent = typename Base::Parent;
38 using CodeSymbol = typename Base::CodeSymbol;
39 using Traits = typename Base::Traits;
40
41 using Base::Base;
42
43 template <typename BIT_STREAM, bool FULL_DECODE>
44 int processSymbol(BIT_STREAM& bs, CodeSymbol symbol,
45 typename Traits::CodeValueTy codeValue) const {
46 invariant(symbol.code_len >= 0 &&
47 symbol.code_len <= Traits::MaxCodeLenghtBits);
48
49 // If we were only looking for symbol's code value, then just return it.
50 if constexpr (!FULL_DECODE)
51 return codeValue;
52
53 // Else, treat it as the length of following difference
54 // that we need to read and extend.
55 int diff_l = codeValue;
56 invariant(diff_l >= 0 && diff_l <= 16);
57
58 if (diff_l == 16) {
60 bs.skipBitsNoFill(16);
61 return -32768;
62 }
63
64 invariant(symbol.code_len + diff_l <= 32);
65 return diff_l ? extend(bs.getBitsNoFill(diff_l), diff_l) : 0;
66 }
67
68 // Figure F.12 – Extending the sign bit of a decoded value in V
69 // WARNING: this is *not* your normal 2's complement sign extension!
70 static int RAWSPEED_READNONE extend(uint32_t diff, uint32_t len) {
71 invariant(len > 0);
72 auto ret = static_cast<int32_t>(diff);
73 if ((diff & (1 << (len - 1))) == 0)
74 ret -= (1 << len) - 1;
75 return ret;
76 }
77};
78
79} // namespace rawspeed
#define invariant(expr)
Definition Invariant.h:27
int processSymbol(BIT_STREAM &bs, CodeSymbol symbol, typename Traits::CodeValueTy codeValue) const
static int RAWSPEED_READNONE extend(uint32_t diff, uint32_t len)
AbstractPrefixCodeTranscoder< CodeTag > Base
typename AbstractPrefixCode< CodeTag >::Traits Traits
typename AbstractPrefixCode< CodeTag >::CodeSymbol CodeSymbol
AbstractPrefixCodeTranscoder(PrefixCode< CodeTag > code_)