RawSpeed
fast raw decoding library
Loading...
Searching...
No Matches
CroppedArray2DRef.h
Go to the documentation of this file.
1/*
2 RawSpeed - RAW file decoder.
3
4 Copyright (C) 2021 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 "adt/Array1DRef.h"
24#include "adt/Array2DRef.h"
26#include "adt/Invariant.h"
27#include <type_traits>
28
29namespace rawspeed {
30
31template <class T> class CroppedArray2DRef final {
33
34 // We need to be able to convert to const version.
36
37public:
38 void establishClassInvariants() const noexcept;
39
40 using value_type = T;
41 using cvless_value_type = std::remove_cv_t<value_type>;
42
47
49
50 // Can not cast away constness.
51 template <typename T2>
52 requires(std::is_const_v<T2> && !std::is_const_v<T>)
54
55 // Can not change type.
56 template <typename T2>
57 requires(!(std::is_const_v<T2> && !std::is_const_v<T>) &&
58 !std::is_same_v<std::remove_const_t<T>, std::remove_const_t<T2>>)
60
61 // Conversion from Array2DRef<T> to CroppedArray2DRef<T>.
62 // NOLINTNEXTLINE(google-explicit-constructor)
64 : CroppedArray2DRef(RHS, /*offsetCols=*/0, /*offsetRows=*/0, RHS.width(),
65 RHS.height()) {}
66
67 CroppedArray2DRef(Array2DRef<T> base_, int offsetCols_, int offsetRows_,
68 int croppedWidth_, int croppedHeight_);
69
70 // Conversion from CroppedArray2DRef<T> to CroppedArray2DRef<const T>.
71 template <class 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 CroppedArray2DRef( // NOLINT(google-explicit-constructor)
77 RHS.croppedWidth, RHS.croppedHeight) {}
78
79 [[nodiscard]] Array2DRef<T> getAsArray2DRef() const {
81 return {operator[](0).begin(), croppedWidth, croppedHeight, base.pitch()};
82 }
83
84 CroppedArray1DRef<T> operator[](int row) const;
85
86 T& operator()(int row, int col) const;
87};
88
89// CTAD deduction guide
90template <typename T>
91explicit CroppedArray2DRef(Array2DRef<T> base_, int offsetCols_,
92 int offsetRows_, int croppedWidth_,
93 int croppedHeight_)
95
96template <class T>
97__attribute__((always_inline)) inline void
100 invariant(offsetCols >= 0);
101 invariant(offsetRows >= 0);
102 invariant(croppedWidth >= 0);
103 invariant(croppedHeight >= 0);
104 invariant(offsetCols <= base.width());
105 invariant(offsetRows <= base.height());
106 invariant(croppedWidth <= base.width());
107 invariant(croppedHeight <= base.height());
108 invariant(offsetCols + croppedWidth <= base.width());
109 invariant(offsetRows + croppedHeight <= base.height());
110 invariant((croppedWidth == 0) == (croppedHeight == 0));
111}
112
113template <class T>
114inline CroppedArray2DRef<T>::CroppedArray2DRef(Array2DRef<T> base_,
115 int offsetCols_, int offsetRows_,
116 int croppedWidth_,
117 int croppedHeight_)
118 : base(base_), offsetCols(offsetCols_), offsetRows(offsetRows_),
119 croppedWidth(croppedWidth_), croppedHeight(croppedHeight_) {
120 establishClassInvariants();
121}
122
123template <class T>
124inline CroppedArray1DRef<T>
127 invariant(row >= 0);
129 const Array1DRef<T> fullLine = base.operator[](offsetRows + row);
130 return fullLine.getCrop(offsetCols, croppedWidth);
131}
132
133template <class T>
134inline T& CroppedArray2DRef<T>::operator()(const int row, const int col) const {
136 invariant(col >= 0);
137 invariant(col < croppedWidth);
138 return (operator[](row))(col);
139}
140
141} // namespace rawspeed
#define invariant(expr)
Definition Invariant.h:27
CroppedArray1DRef< T > getCrop(int offset, int numElts) const
Definition Array1DRef.h:110
int RAWSPEED_READONLY height() const
void establishClassInvariants() const noexcept
int RAWSPEED_READONLY width() const
CroppedArray1DRef< T > operator[](int row) const
const Array2DRef< T > base
T & operator()(int row, int col) const
Array2DRef< T > getAsArray2DRef() const
void establishClassInvariants() const noexcept
CroppedArray2DRef(CroppedArray2DRef< T2 > RHS)
CroppedArray2DRef(Array2DRef< T > base_, int offsetCols_, int offsetRows_, int croppedWidth_, int croppedHeight_)
std::remove_cv_t< value_type > cvless_value_type
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 >