RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
SimpleLUT.h
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2018 Stefan Löffler
5 Copyright (C) 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 "adt/Bit.h"
25#include <algorithm>
26#include <cassert>
27#include <functional>
28#include <iterator>
29#include <type_traits>
30#include <vector>
31
32namespace rawspeed {
33
34template <typename T, int TableBitWidth> class SimpleLUT final {
35public:
36 using value_type = T;
37
38 SimpleLUT() = default;
39
40private:
41 std::vector<value_type> table;
42
43public:
44 template <typename F>
45 requires(
46 !std::is_same_v<SimpleLUT, typename std::remove_cv_t<
47 typename std::remove_reference_t<F>>> &&
48 std::is_convertible_v<
49 F, std::function<value_type(typename decltype(table)::size_type,
50 typename decltype(table)::size_type)>>)
51 explicit SimpleLUT(F f) {
52 const auto fullTableSize = 1U << TableBitWidth;
53 table.reserve(fullTableSize);
54 std::generate_n(std::back_inserter(table), fullTableSize,
55 [&f, table_ = &table]() {
56 // which row [0..fullTableSize) are we filling?
57 const auto i = table_->size();
58 return f(i, fullTableSize);
59 });
60 assert(table.size() == fullTableSize);
61 }
62
63 value_type operator[](int x) const {
64 unsigned clampedX = clampBits(x, TableBitWidth);
65 return table[clampedX];
66 }
67};
68
69} // namespace rawspeed
assert(dim.area() >=area)
dim x
Definition Common.cpp:50
value_type operator[](int x) const
Definition SimpleLUT.h:63
std::vector< value_type > table
Definition SimpleLUT.h:41
throw T(buf.data())
constexpr auto RAWSPEED_READNONE clampBits(T value, unsigned int nBits)
Definition Bit.h:75