RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
Common.h
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2009-2014 Klaus Post
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Lesser General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library 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 GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19*/
20
21#pragma once
22
23#include "rawspeedconfig.h"
24#include "adt/Array1DRef.h"
25#include "adt/Array2DRef.h"
26#include "adt/Bit.h"
27#include "adt/Invariant.h"
28#include <algorithm>
29#include <array>
30#include <cassert>
31#include <cstddef>
32#include <cstdint>
33#include <cstring>
34#include <initializer_list>
35#include <string>
36#include <string_view>
37#include <tuple>
38#include <type_traits>
39#include <vector>
40
42
43namespace rawspeed {
44
45enum class DEBUG_PRIO {
46 ERROR = 0x10,
47 WARNING = 0x100,
48 INFO = 0x1000,
49 EXTRA = 0x10000
50};
51
52void writeLog(DEBUG_PRIO priority, const char* format, ...)
53 __attribute__((format(printf, 2, 3)));
54
55inline void copyPixelsImpl(Array1DRef<std::byte> dest,
56 Array1DRef<const std::byte> src) {
57 invariant(src.size() == dest.size());
58 std::copy(src.begin(), src.end(), dest.begin());
59}
60
63 invariant(src.width() > 0);
64 invariant(src.height() > 0);
65 invariant(dest.width() > 0);
66 invariant(dest.height() > 0);
67 invariant(src.height() == dest.height());
68 invariant(src.width() == dest.width());
69 if (auto [destAsStrip, srcAsStrip] =
70 std::make_tuple(dest.getAsArray1DRef(), src.getAsArray1DRef());
71 destAsStrip && srcAsStrip) {
72 copyPixelsImpl(*destAsStrip, *srcAsStrip);
73 return;
74 }
75 for (int row = 0; row != src.height(); ++row)
76 copyPixelsImpl(dest[row], src[row]);
77}
78
79inline void copyPixels(std::byte* destPtr, int dstPitch,
80 const std::byte* srcPtr, int srcPitch, int rowSize,
81 int height) {
82 invariant(destPtr);
83 invariant(dstPitch > 0);
84 invariant(srcPtr);
85 invariant(srcPitch > 0);
86 invariant(rowSize > 0);
87 invariant(height > 0);
88 invariant(rowSize <= srcPitch);
89 invariant(rowSize <= dstPitch);
90 auto dest = Array2DRef(destPtr, rowSize, height, dstPitch);
91 auto src = Array2DRef(srcPtr, rowSize, height, srcPitch);
92 copyPixelsImpl(dest, src);
93}
94
95template <typename T>
96 requires std::is_pointer_v<T>
97constexpr uint64_t RAWSPEED_READNONE getMisalignmentOffset(T value,
98 uint64_t multiple) {
99 if (multiple == 0)
100 return 0;
101 static_assert(bitwidth<uintptr_t>() >= bitwidth<T>(),
102 "uintptr_t can not represent all pointer values?");
103 return reinterpret_cast<uintptr_t>(value) % multiple;
104}
105
106template <typename T>
107 requires std::is_integral_v<T>
108constexpr uint64_t RAWSPEED_READNONE getMisalignmentOffset(T value,
109 uint64_t multiple) {
110 if (multiple == 0)
111 return 0;
112 return value % multiple;
113}
114
115template <typename T>
116constexpr T RAWSPEED_READNONE roundToMultiple(T value, uint64_t multiple,
117 bool roundDown) {
118 uint64_t offset = getMisalignmentOffset(value, multiple);
119 if (offset == 0)
120 return value;
121 // Drop remainder.
122 T roundedDown = value - offset;
123 if (roundDown) // If we were rounding down, then that's it.
124 return roundedDown;
125 // Else, just add one multiple.
126 return roundedDown + multiple;
127}
128
129constexpr uint64_t RAWSPEED_READNONE roundDown(uint64_t value,
130 uint64_t multiple) {
131 return roundToMultiple(value, multiple, /*roundDown=*/true);
132}
133
134constexpr uint64_t RAWSPEED_READNONE roundUp(uint64_t value,
135 uint64_t multiple) {
136 return roundToMultiple(value, multiple, /*roundDown=*/false);
137}
138
139constexpr uint64_t RAWSPEED_READNONE roundUpDivision(uint64_t value,
140 uint64_t div) {
141 invariant(div != 0);
142 return roundUp(value, div) / div;
143}
144
145constexpr uint64_t RAWSPEED_READNONE roundUpDivisionSafe(uint64_t value,
146 uint64_t div) {
147 return (value != 0) ? (1 + ((value - 1) / div)) : 0;
148}
149
150template <class T>
151constexpr RAWSPEED_READNONE bool isAligned(T value, size_t multiple) {
152 return (multiple == 0) || (getMisalignmentOffset(value, multiple) == 0);
153}
154
155template <typename T, typename T2>
156bool RAWSPEED_READONLY isIn(const T value,
157 const std::initializer_list<T2>& list) {
158 return std::any_of(list.begin(), list.end(),
159 [value](const T2& t) { return t == value; });
160}
161
162// Trim both leading and trailing spaces from the string
163inline std::string trimSpaces(std::string_view str) {
164 // Find the first character position after excluding leading blank spaces
165 size_t startpos = str.find_first_not_of(" \t");
166
167 // Find the first character position from reverse af
168 size_t endpos = str.find_last_not_of(" \t");
169
170 // if all spaces or empty return an empty string
171 if ((startpos == std::string::npos) || (endpos == std::string::npos))
172 return "";
173
174 str = str.substr(startpos, endpos - startpos + 1);
175 return {str.begin(), str.end()};
176}
177
178inline std::vector<std::string> splitString(const std::string& input,
179 char c = ' ') {
180 std::vector<std::string> result;
181
182 std::string_view str = input;
183 while (!str.empty()) {
184 std::string_view::size_type pos = str.find_first_of(c);
185
186 if (pos == std::string_view::npos)
187 pos = str.size();
188
189 auto substr = str.substr(/*pos=*/0, /*n=*/pos);
190
191 if (!substr.empty())
192 result.emplace_back(substr);
193
194 str.remove_prefix(std::min(str.size(), 1 + substr.size()));
195 }
196
197 return result;
198}
199
200template <int N, typename T>
201inline std::array<T, N> to_array(const std::vector<T>& v) {
202 std::array<T, N> a;
203 assert(v.size() == N && "Size mismatch");
204 std::move(v.begin(), v.end(), a.begin());
205 return a;
206}
207
208} // namespace rawspeed
#define invariant(expr)
Definition Invariant.h:27
bool RAWSPEED_READNONE __attribute__((visibility("default"))) benchmarkDryRun()
Definition Common.cpp:35
assert(dim.area() >=area)
int RAWSPEED_READONLY height() const
Optional< Array1DRef< T > > getAsArray1DRef() const
int RAWSPEED_READONLY width() const
constexpr T RAWSPEED_READNONE roundToMultiple(T value, uint64_t multiple, bool roundDown)
Definition Common.h:116
constexpr uint64_t RAWSPEED_READNONE roundUpDivisionSafe(uint64_t value, uint64_t div)
Definition Common.h:145
constexpr uint64_t RAWSPEED_READNONE roundUp(uint64_t value, uint64_t multiple)
Definition Common.h:134
std::string trimSpaces(std::string_view str)
Definition Common.h:163
void copyPixels(std::byte *destPtr, int dstPitch, const std::byte *srcPtr, int srcPitch, int rowSize, int height)
Definition Common.h:79
constexpr uint64_t RAWSPEED_READNONE getMisalignmentOffset(T value, uint64_t multiple)
Definition Common.h:97
constexpr uint64_t RAWSPEED_READNONE roundUpDivision(uint64_t value, uint64_t div)
Definition Common.h:139
DEBUG_PRIO
Definition Common.h:45
constexpr uint64_t RAWSPEED_READNONE roundDown(uint64_t value, uint64_t multiple)
Definition Common.h:129
throw T(buf.data())
void void copyPixelsImpl(Array1DRef< std::byte > dest, Array1DRef< const std::byte > src)
Definition Common.h:55
void writeLog(DEBUG_PRIO priority, const char *format,...)
Definition Common.cpp:37
Array2DRef(Array1DRef< T > data, int width, int height, int pitch) -> Array2DRef< T >
bool RAWSPEED_READONLY isIn(const T value, const std::initializer_list< T2 > &list)
Definition Common.h:156
constexpr RAWSPEED_READNONE bool isAligned(T value, size_t multiple)
Definition Common.h:151
constexpr unsigned RAWSPEED_READNONE bitwidth(T unused={})
Definition Bit.h:43
std::vector< std::string > splitString(const std::string &input, char c=' ')
Definition Common.h:178
std::array< T, N > to_array(const std::vector< T > &v)
Definition Common.h:201
int rawspeed_get_number_of_processor_cores()
Definition RawSpeed.cpp:26