RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
TableLookUp.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 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#include "common/TableLookUp.h"
23#include "adt/Array1DRef.h"
24#include "adt/Array2DRef.h"
25#include "adt/Bit.h"
26#include "adt/Casts.h"
27#include "adt/Invariant.h"
29#include <algorithm>
30#include <cassert>
31#include <cstdint>
32#include <limits>
33#include <vector>
34
35namespace rawspeed {
36
37// How many different values can uint16_t represent?
38constexpr int TABLE_MAX_ELTS = std::numeric_limits<uint16_t>::max() + 1;
39constexpr int TABLE_SIZE = TABLE_MAX_ELTS * 2;
40
41// Creates n numre of tables.
42TableLookUp::TableLookUp(int _ntables, bool _dither)
43 : ntables(_ntables), dither(_dither) {
44 if (ntables < 1) {
45 ThrowRDE("Cannot construct 0 tables");
46 }
47 tables.resize(ntables * TABLE_SIZE, uint16_t(0));
48}
49
50void TableLookUp::setTable(int ntable, const std::vector<uint16_t>& table) {
51 assert(!table.empty());
52
53 const auto nfilled = implicit_cast<int>(table.size());
54 if (nfilled > TABLE_MAX_ELTS)
55 ThrowRDE("Table lookup with %i entries is unsupported", nfilled);
56
57 if (ntable > ntables) {
58 ThrowRDE("Table lookup with number greater than number of tables.");
59 }
60
61 auto t = Array2DRef(tables.data(), TABLE_SIZE, ntables)[ntable];
62 if (!dither) {
63 for (int i = 0; i < TABLE_MAX_ELTS; i++) {
64 t(i) = (i < nfilled) ? table[i] : table[nfilled - 1];
65 }
66 return;
67 }
68 for (int i = 0; i < nfilled; i++) {
69 int center = table[i];
70 int lower = i > 0 ? table[i - 1] : center;
71 int upper = i < (nfilled - 1) ? table[i + 1] : center;
72 // Non-monotonic LUT handling: don't interpolate across the cross-over.
73 lower = std::min(lower, center);
74 upper = std::max(upper, center);
75 int delta = upper - lower;
76 invariant(delta >= 0);
77 t(i * 2) = clampBits(center - ((upper - lower + 2) / 4), 16);
78 t((i * 2) + 1) = implicit_cast<uint16_t>(delta);
79 }
80
81 for (int i = nfilled; i < TABLE_MAX_ELTS; i++) {
82 t(i * 2) = table[nfilled - 1];
83 t((i * 2) + 1) = 0;
84 }
85}
86
88 if (n > ntables) {
89 ThrowRDE("Table lookup with number greater than number of tables.");
90 }
91 return Array2DRef(tables.data(), TABLE_SIZE, ntables)[n];
92}
93
94} // namespace rawspeed
#define invariant(expr)
Definition Invariant.h:27
#define ThrowRDE(...)
assert(dim.area() >=area)
void setTable(int ntable, const std::vector< uint16_t > &table)
Array1DRef< uint16_t > getTable(int n)
std::vector< uint16_t > tables
Definition TableLookUp.h:37
TableLookUp(int ntables, bool dither)
constexpr RAWSPEED_READNONE Ttgt implicit_cast(Tsrc value)
Definition Casts.h:32
constexpr int TABLE_SIZE
Array2DRef(Array1DRef< T > data, int width, int height, int pitch) -> Array2DRef< T >
constexpr int TABLE_MAX_ELTS
constexpr auto RAWSPEED_READNONE clampBits(T value, unsigned int nBits)
Definition Bit.h:75