libdcp
transfer_function.cc
1 /*
2  Copyright (C) 2012-2021 Carl Hetherington <cth@carlh.net>
3 
4  This file is part of libdcp.
5 
6  libdcp is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  libdcp is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with libdcp. If not, see <http://www.gnu.org/licenses/>.
18 
19  In addition, as a special exception, the copyright holders give
20  permission to link the code of portions of this program with the
21  OpenSSL library under certain conditions as described in each
22  individual source file, and distribute linked combinations
23  including the two.
24 
25  You must obey the GNU General Public License in all respects
26  for all of the code used other than OpenSSL. If you modify
27  file(s) with this exception, you may extend this exception to your
28  version of the file(s), but you are not obligated to do so. If you
29  do not wish to do so, delete this exception statement from your
30  version. If you delete this exception statement from all source
31  files in the program, then also delete it here.
32 */
33 
34 
35 /* @file src/transfer_function.cc
36  * @brief TransferFunction
37  */
38 
39 
40 #include "transfer_function.h"
41 #include <cmath>
42 
43 
44 using std::make_pair;
45 using std::pair;
46 using std::pow;
47 using std::shared_ptr;
48 using std::vector;
49 using namespace dcp;
50 
51 
52 vector<double> const&
53 TransferFunction::double_lut(double from, double to, int bit_depth, bool inverse) const
54 {
55  boost::mutex::scoped_lock lm (_mutex);
56  return double_lut_unlocked(from, to, bit_depth, inverse);
57 }
58 
59 
60 vector<double> const&
61 TransferFunction::double_lut_unlocked(double from, double to, int bit_depth, bool inverse) const
62 {
63  auto const descriptor = LUTDescriptor{from, to, bit_depth, inverse, 1};
64 
65  auto i = _double_luts.find(descriptor);
66  if (i != _double_luts.end()) {
67  return i->second;
68  }
69 
70  _double_luts[descriptor] = make_double_lut(from, to, bit_depth, inverse);
71  return _double_luts[descriptor];
72 }
73 
74 
75 vector<int> const&
76 TransferFunction::int_lut(double from, double to, int bit_depth, bool inverse, int scale) const
77 {
78  boost::mutex::scoped_lock lm (_mutex);
79 
80  auto const descriptor = LUTDescriptor{from, to, bit_depth, inverse, scale};
81 
82  auto i = _int_luts.find(descriptor);
83  if (i != _int_luts.end()) {
84  return i->second;
85  }
86 
87  _int_luts[descriptor] = make_int_lut(from, to, bit_depth, inverse, scale);
88  return _int_luts[descriptor];
89 }
90 
91 
92 /* Caller must hold lock on _mutex */
93 vector<int>
94 TransferFunction::make_int_lut(double from, double to, int bit_depth, bool inverse, int scale) const
95 {
96  auto source_lut = double_lut_unlocked(from, to, bit_depth, inverse);
97  auto const size = source_lut.size();
98  vector<int> lut(size);
99  for (size_t i = 0; i < size; ++i) {
100  lut[i] = lrint(source_lut[i] * scale);
101  }
102 
103  return lut;
104 }
105 
106 
107 bool
108 TransferFunction::LUTDescriptor::operator==(TransferFunction::LUTDescriptor const& other) const
109 {
110  return from == other.from && to == other.to && bit_depth == other.bit_depth && inverse == other.inverse && scale == other.scale;
111 }
112 
113 
114 std::size_t
115 TransferFunction::LUTDescriptorHasher::operator()(TransferFunction::LUTDescriptor const& desc) const
116 {
117  return std::hash<double>()(desc.from) ^
118  std::hash<double>()(desc.to) ^
119  std::hash<int>()(desc.bit_depth) ^
120  std::hash<bool>()(desc.inverse) ^
121  std::hash<int>()(desc.scale);
122 }
123 
std::vector< double > const & double_lut(double from, double to, int bit_depth, bool inverse) const
Namespace for everything in libdcp.
Definition: array_data.h:50
TransferFunction class.