RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
TiledArray2DRef.h
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2025 Roman Lebedev
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/Array2DRef.h"
26#include "adt/Invariant.h"
27#include <cstddef>
28#include <type_traits>
29
30namespace rawspeed {
31
32template <class T> class TiledArray2DRef final {
34
37
38 friend TiledArray2DRef<const T>; // We need to be able to convert to const
39 // version.
40
41 // We need to be able to convert to std::byte.
44
45public:
46 void establishClassInvariants() const noexcept;
47
49
50 using value_type = T;
51 using cvless_value_type = std::remove_cv_t<value_type>;
52
53 [[nodiscard]] int RAWSPEED_READONLY numRows() const;
54 [[nodiscard]] int RAWSPEED_READONLY numCols() const;
55
56 TiledArray2DRef() = delete;
57
58 // Can not cast away constness.
59 template <typename T2>
60 requires(std::is_const_v<T2> && !std::is_const_v<T>)
62
63 // Can not change type to non-byte.
64 template <typename T2>
65 requires(!(std::is_const_v<T2> && !std::is_const_v<T>) &&
66 !std::is_same_v<std::remove_const_t<T>, std::remove_const_t<T2>> &&
67 !std::is_same_v<std::remove_const_t<T>, std::byte>)
69
70 // Conversion from TiledArray2DRef<T> to TiledArray2DRef<const T>.
71 template <typename T2>
72 requires(!std::is_const_v<T2> && std::is_const_v<T> &&
73 std::is_same_v<std::remove_const_t<T>, std::remove_const_t<T2>>)
74 TiledArray2DRef( // NOLINT(google-explicit-constructor)
75 TiledArray2DRef<T2> RHS)
76 : TiledArray2DRef(RHS.data, RHS._width, RHS._height, RHS._pitch) {}
77
78 // Const-preserving conversion from TiledArray2DRef<T> to
79 // TiledArray2DRef<std::byte>.
80 template <typename T2>
81 requires(
82 !(std::is_const_v<T2> && !std::is_const_v<T>) &&
83 !(std::is_same_v<std::remove_const_t<T>, std::remove_const_t<T2>>) &&
84 std::is_same_v<std::remove_const_t<T>, std::byte>)
85 TiledArray2DRef( // NOLINT(google-explicit-constructor)
87 : TiledArray2DRef(RHS.data, sizeof(T2) * RHS._width, RHS._height,
88 sizeof(T2) * RHS._pitch) {}
89
90 CroppedArray2DRef<T> operator()(int row, int col) const;
91};
92
93// CTAD deduction guide
94template <typename T>
95explicit TiledArray2DRef(Array2DRef<T> data, int tileWidth,
96 int tileHeight) -> TiledArray2DRef<T>;
97
98template <class T>
99__attribute__((always_inline)) inline void
102 invariant(tileWidth > 0);
103 invariant(tileHeight > 0);
104 invariant(tileWidth <= data.width());
105 invariant(tileHeight <= data.height());
106 invariant(data.width() % tileWidth == 0);
107 invariant(data.height() % tileHeight == 0);
108}
109
110template <class T>
111inline TiledArray2DRef<T>::TiledArray2DRef(Array2DRef<T> data_,
112 const int tileWidth_,
113 const int tileHeight_)
114 : data(data_), tileWidth(tileWidth_), tileHeight(tileHeight_) {
115 establishClassInvariants();
116}
117
118template <class T>
119__attribute__((always_inline)) inline int TiledArray2DRef<T>::numCols() const {
120 establishClassInvariants();
121 return data.width() / tileWidth;
122}
123
124template <class T>
125__attribute__((always_inline)) inline int TiledArray2DRef<T>::numRows() const {
126 establishClassInvariants();
127 return data.height() / tileHeight;
128}
129
130template <class T>
131__attribute__((always_inline)) inline CroppedArray2DRef<T>
132TiledArray2DRef<T>::operator()(const int row, const int col) const {
133 establishClassInvariants();
134 invariant(col >= 0);
135 invariant(col < numCols());
136 invariant(row >= 0);
137 invariant(row < numRows());
138 return CroppedArray2DRef(data,
139 /*offsetCols=*/tileWidth * col,
140 /*offsetRows=*/tileHeight * row,
141 /*croppedWidth=*/tileWidth,
142 /*croppedHeight=*/tileHeight);
143}
144
145} // namespace rawspeed
#define invariant(expr)
Definition Invariant.h:27
bool RAWSPEED_READNONE __attribute__((visibility("default"))) benchmarkDryRun()
Definition Common.cpp:35
void establishClassInvariants() const noexcept
TiledArray2DRef(Array2DRef< T > data, int tileWidth, int tileHeight)
TiledArray2DRef(TiledArray2DRef< T2 > RHS)
int RAWSPEED_READONLY numCols() const
std::remove_cv_t< value_type > cvless_value_type
int RAWSPEED_READONLY numRows() const
CroppedArray2DRef< T > operator()(int row, int col) const
TiledArray2DRef(Array2DRef< T > data, int tileWidth, int tileHeight) -> TiledArray2DRef< T >
throw T(buf.data())
__attribute__((noinline)) __attribute__((visibility("default"))) JPEGStuffedByteStreamGenerator
CroppedArray2DRef(Array2DRef< T > base_, int offsetCols_, int offsetRows_, int croppedWidth_, int croppedHeight_) -> CroppedArray2DRef< typename Array2DRef< T >::value_type >