Skip to content

Commit a48434e

Browse files
committed
Array2DRef: wrap width/height into a getters
1 parent 664dd8b commit a48434e

27 files changed

+170
-156
lines changed

fuzz/librawspeed/common/DngOpcodes.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) {
6161
rawspeed::Array2DRef<uint16_t> img =
6262
mRaw->getU16DataAsUncroppedArray2DRef();
6363
const uint16_t fill = bs.getU16();
64-
for (auto row = 0; row < img.height; ++row) {
65-
for (auto col = 0; col < img.width; ++col) {
64+
for (auto row = 0; row < img.height(); ++row) {
65+
for (auto col = 0; col < img.width(); ++col) {
6666
img(row, col) = fill;
6767
}
6868
}
@@ -71,8 +71,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) {
7171
case rawspeed::RawImageType::F32: {
7272
rawspeed::Array2DRef<float> img = mRaw->getF32DataAsUncroppedArray2DRef();
7373
const float fill = bs.getFloat();
74-
for (auto row = 0; row < img.height; ++row) {
75-
for (auto col = 0; col < img.width; ++col) {
74+
for (auto row = 0; row < img.height(); ++row) {
75+
for (auto col = 0; col < img.width(); ++col) {
7676
img(row, col) = fill;
7777
}
7878
}

src/librawspeed/adt/Array2DRef.h

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ template <class T> class Array2DRef final {
3535
Array1DRef<T> data;
3636
int _pitch;
3737

38+
int _width;
39+
int _height;
40+
3841
friend Array2DRef<const T>; // We need to be able to convert to const version.
3942

4043
// We need to be able to convert to std::byte.
@@ -49,8 +52,8 @@ template <class T> class Array2DRef final {
4952
using value_type = T;
5053
using cvless_value_type = std::remove_cv_t<value_type>;
5154

52-
int width;
53-
int height;
55+
[[nodiscard]] int RAWSPEED_READONLY width() const;
56+
[[nodiscard]] int RAWSPEED_READONLY height() const;
5457

5558
Array2DRef() = delete;
5659

@@ -75,8 +78,8 @@ template <class T> class Array2DRef final {
7578
requires(!std::is_const_v<T2> && std::is_const_v<T> &&
7679
std::is_same_v<std::remove_const_t<T>, std::remove_const_t<T2>>)
7780
Array2DRef(Array2DRef<T2> RHS) // NOLINT google-explicit-constructor
78-
: data(RHS.data), _pitch(RHS._pitch), width(RHS.width),
79-
height(RHS.height) {}
81+
: data(RHS.data), _pitch(RHS._pitch), _width(RHS.width()),
82+
_height(RHS.height()) {}
8083

8184
// Const-preserving conversion from Array2DRef<T> to Array2DRef<std::byte>.
8285
template <typename T2>
@@ -86,7 +89,7 @@ template <class T> class Array2DRef final {
8689
std::is_same_v<std::remove_const_t<T>, std::byte>)
8790
Array2DRef(Array2DRef<T2> RHS) // NOLINT google-explicit-constructor
8891
: data(RHS.data), _pitch(sizeof(T2) * RHS._pitch),
89-
width(sizeof(T2) * RHS.width), height(RHS.height) {}
92+
_width(sizeof(T2) * RHS.width()), _height(RHS.height()) {}
9093

9194
template <typename AllocatorType =
9295
typename std::vector<cvless_value_type>::allocator_type>
@@ -121,19 +124,19 @@ explicit Array2DRef(T* data, int width, int height) -> Array2DRef<T>;
121124
template <class T>
122125
inline void Array2DRef<T>::establishClassInvariants() const noexcept {
123126
data.establishClassInvariants();
124-
invariant(width >= 0);
125-
invariant(height >= 0);
127+
invariant(_width >= 0);
128+
invariant(_height >= 0);
126129
invariant(_pitch != 0);
127130
invariant(_pitch >= 0);
128-
invariant(_pitch >= width);
129-
invariant((width == 0) == (height == 0));
130-
invariant(data.size() == _pitch * height);
131+
invariant(_pitch >= _width);
132+
invariant((_width == 0) == (_height == 0));
133+
invariant(data.size() == _pitch * _height);
131134
}
132135

133136
template <class T>
134137
Array2DRef<T>::Array2DRef(Array1DRef<T> data_, const int width_,
135138
const int height_, const int pitch_)
136-
: data(data_), _pitch(pitch_), width(width_), height(height_) {
139+
: data(data_), _pitch(pitch_), _width(width_), _height(height_) {
137140
establishClassInvariants();
138141
}
139142

@@ -150,28 +153,38 @@ Array2DRef<T>::Array2DRef(T* data_, const int width_, const int height_)
150153
establishClassInvariants();
151154
}
152155

156+
template <class T> inline int Array2DRef<T>::width() const {
157+
establishClassInvariants();
158+
return _width;
159+
}
160+
161+
template <class T> inline int Array2DRef<T>::height() const {
162+
establishClassInvariants();
163+
return _height;
164+
}
165+
153166
template <class T>
154167
[[nodiscard]] inline Optional<Array1DRef<T>>
155168
Array2DRef<T>::getAsArray1DRef() const {
156169
establishClassInvariants();
157-
if (height == 1 || _pitch == width)
158-
return data.getCrop(/*offset=*/0, width * height).getAsArray1DRef();
170+
if (height() == 1 || _pitch == width())
171+
return data.getCrop(/*offset=*/0, width() * height()).getAsArray1DRef();
159172
return std::nullopt;
160173
}
161174

162175
template <class T>
163176
inline Array1DRef<T> Array2DRef<T>::operator[](const int row) const {
164177
establishClassInvariants();
165178
invariant(row >= 0);
166-
invariant(row < height);
167-
return data.getCrop(row * _pitch, width).getAsArray1DRef();
179+
invariant(row < height());
180+
return data.getCrop(row * _pitch, width()).getAsArray1DRef();
168181
}
169182

170183
template <class T>
171184
inline T& Array2DRef<T>::operator()(const int row, const int col) const {
172185
establishClassInvariants();
173186
invariant(col >= 0);
174-
invariant(col < width);
187+
invariant(col < width());
175188
return (operator[](row))(col);
176189
}
177190

src/librawspeed/adt/CroppedArray2DRef.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ template <class T> class CroppedArray2DRef final {
6060

6161
// Conversion from Array2DRef<T> to CroppedArray2DRef<T>.
6262
CroppedArray2DRef(Array2DRef<T> RHS) // NOLINT google-explicit-constructor
63-
: base(RHS), offsetCols(0), offsetRows(0), croppedWidth(base.width),
64-
croppedHeight(base.height) {}
63+
: base(RHS), offsetCols(0), offsetRows(0), croppedWidth(base.width()),
64+
croppedHeight(base.height()) {}
6565

6666
CroppedArray2DRef(Array2DRef<T> base_, int offsetCols_, int offsetRows_,
6767
int croppedWidth_, int croppedHeight_);
@@ -94,12 +94,12 @@ inline void CroppedArray2DRef<T>::establishClassInvariants() const noexcept {
9494
invariant(offsetRows >= 0);
9595
invariant(croppedWidth >= 0);
9696
invariant(croppedHeight >= 0);
97-
invariant(offsetCols <= base.width);
98-
invariant(offsetRows <= base.height);
99-
invariant(croppedWidth <= base.width);
100-
invariant(croppedHeight <= base.height);
101-
invariant(offsetCols + croppedWidth <= base.width);
102-
invariant(offsetRows + croppedHeight <= base.height);
97+
invariant(offsetCols <= base.width());
98+
invariant(offsetRows <= base.height());
99+
invariant(croppedWidth <= base.width());
100+
invariant(croppedHeight <= base.height());
101+
invariant(offsetCols + croppedWidth <= base.width());
102+
invariant(offsetRows + croppedHeight <= base.height());
103103
}
104104

105105
template <class T>

src/librawspeed/common/BayerPhase.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ inline std::array<T, 4> applyPhaseShift(std::array<T, 4> srcData,
8484

8585
std::array<T, 4> tgtData;
8686
const Array2DRef<T> tgt(tgtData.data(), 2, 2);
87-
for (int row = 0; row < tgt.height; ++row) {
88-
for (int col = 0; col < tgt.width; ++col) {
87+
for (int row = 0; row < tgt.height(); ++row) {
88+
for (int col = 0; col < tgt.width(); ++col) {
8989
tgt(row, col) = src((coordOffset.y + row) % 2, (coordOffset.x + col) % 2);
9090
}
9191
}
@@ -133,8 +133,8 @@ inline Optional<BayerPhase> getAsBayerPhase(const ColorFilterArray& CFA) {
133133

134134
std::array<CFAColor, 4> patData;
135135
const Array2DRef<CFAColor> pat(patData.data(), 2, 2);
136-
for (int row = 0; row < pat.height; ++row) {
137-
for (int col = 0; col < pat.width; ++col) {
136+
for (int row = 0; row < pat.height(); ++row) {
137+
for (int col = 0; col < pat.width(); ++col) {
138138
pat(row, col) = CFA.getColorAt(col, row);
139139
}
140140
}

src/librawspeed/common/Common.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,19 @@ inline void copyPixelsImpl(Array1DRef<std::byte> dest,
6161

6262
inline void copyPixelsImpl(Array2DRef<std::byte> dest,
6363
Array2DRef<const std::byte> src) {
64-
invariant(src.width > 0);
65-
invariant(src.height > 0);
66-
invariant(dest.width > 0);
67-
invariant(dest.height > 0);
68-
invariant(src.height == dest.height);
69-
invariant(src.width == dest.width);
64+
invariant(src.width() > 0);
65+
invariant(src.height() > 0);
66+
invariant(dest.width() > 0);
67+
invariant(dest.height() > 0);
68+
invariant(src.height() == dest.height());
69+
invariant(src.width() == dest.width());
7070
if (auto [destAsStrip, srcAsStrip] =
7171
std::make_tuple(dest.getAsArray1DRef(), src.getAsArray1DRef());
7272
destAsStrip && srcAsStrip) {
7373
copyPixelsImpl(*destAsStrip, *srcAsStrip);
7474
return;
7575
}
76-
for (int row = 0; row != src.height; ++row)
76+
for (int row = 0; row != src.height(); ++row)
7777
copyPixelsImpl(dest[row], src[row]);
7878
}
7979

src/librawspeed/common/RawImageDataFloat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ void RawImageDataFloat::scaleValues(int start_y, int end_y) {
153153
int gw = dim.x * cpp;
154154
std::array<float, 4> mul;
155155
std::array<float, 4> sub;
156-
assert(blackLevelSeparate->width == 2 && blackLevelSeparate->height == 2);
156+
assert(blackLevelSeparate->width() == 2 && blackLevelSeparate->height() == 2);
157157
auto blackLevelSeparate1D = *blackLevelSeparate->getAsArray1DRef();
158158
for (int i = 0; i < 4; i++) {
159159
int v = i;

src/librawspeed/common/RawImageDataU16.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ void RawImageDataU16::calculateBlackAreas() {
6262
std::vector<uint16_t> histogramStorage;
6363
auto histogram = Array2DRef<uint16_t>::create(histogramStorage, 65536, 4);
6464

65-
for (int row = 0; row != histogram.height; ++row) {
66-
for (int col = 0; col != histogram.width; ++col) {
65+
for (int row = 0; row != histogram.height(); ++row) {
66+
for (int col = 0; col != histogram.width(); ++col) {
6767
histogram(row, col) = 0;
6868
}
6969
}
@@ -203,7 +203,7 @@ void RawImageDataU16::scaleValues(int start_y, int end_y) {
203203

204204
#ifdef WITH_SSE2
205205
void RawImageDataU16::scaleValues_SSE2(int start_y, int end_y) {
206-
assert(blackLevelSeparate->width == 2 && blackLevelSeparate->height == 2);
206+
assert(blackLevelSeparate->width() == 2 && blackLevelSeparate->height() == 2);
207207
auto blackLevelSeparate1D = *blackLevelSeparate->getAsArray1DRef();
208208

209209
int depth_values = whitePoint - blackLevelSeparate1D(0);
@@ -344,7 +344,7 @@ void RawImageDataU16::scaleValues_SSE2(int start_y, int end_y) {
344344
void RawImageDataU16::scaleValues_plain(int start_y, int end_y) {
345345
const CroppedArray2DRef<uint16_t> img(getU16DataAsCroppedArray2DRef());
346346

347-
assert(blackLevelSeparate->width == 2 && blackLevelSeparate->height == 2);
347+
assert(blackLevelSeparate->width() == 2 && blackLevelSeparate->height() == 2);
348348
auto blackLevelSeparate1D = *blackLevelSeparate->getAsArray1DRef();
349349

350350
int depth_values = whitePoint - blackLevelSeparate1D(0);

src/librawspeed/common/XTransPhase.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@ inline std::array<T, 6 * 6> applyPhaseShift(std::array<T, 6 * 6> srcData,
4949

5050
std::array<T, 6 * 6> tgtData;
5151
const Array2DRef<T> tgt(tgtData.data(), 6, 6);
52-
for (int row = 0; row < tgt.height; ++row) {
53-
for (int col = 0; col < tgt.width; ++col) {
52+
for (int row = 0; row < tgt.height(); ++row) {
53+
for (int col = 0; col < tgt.width(); ++col) {
5454
tgt(row, col) = src((coordOffset.y + row) % 6, (coordOffset.x + col) % 6);
5555
}
5656
}
@@ -76,15 +76,15 @@ inline Optional<XTransPhase> getAsXTransPhase(const ColorFilterArray& CFA) {
7676

7777
std::array<CFAColor, 6 * 6> patData;
7878
const Array2DRef<CFAColor> pat(patData.data(), 6, 6);
79-
for (int row = 0; row < pat.height; ++row) {
80-
for (int col = 0; col < pat.width; ++col) {
79+
for (int row = 0; row < pat.height(); ++row) {
80+
for (int col = 0; col < pat.width(); ++col) {
8181
pat(row, col) = CFA.getColorAt(col, row);
8282
}
8383
}
8484

8585
iPoint2D off;
86-
for (off.y = 0; off.y < pat.height; ++off.y) {
87-
for (off.x = 0; off.x < pat.width; ++off.x) {
86+
for (off.y = 0; off.y < pat.height(); ++off.y) {
87+
for (off.x = 0; off.x < pat.width(); ++off.x) {
8888
if (getAsCFAColors(off) == patData)
8989
return off;
9090
}

src/librawspeed/decoders/ArwDecoder.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -409,12 +409,12 @@ void ArwDecoder::PostProcessLJpeg() {
409409
#ifdef HAVE_OPENMP
410410
#pragma omp parallel for schedule(static) default(none) firstprivate(in, out)
411411
#endif
412-
for (int inRow = 0; inRow < in.height; ++inRow) {
412+
for (int inRow = 0; inRow < in.height(); ++inRow) {
413413
static constexpr iPoint2D inMCUSize = {4, 1};
414414
static constexpr iPoint2D outMCUSize = {2, 2};
415415

416-
invariant(in.width % inMCUSize.x == 0);
417-
for (int MCUIdx = 0, numMCUsPerRow = in.width / inMCUSize.x;
416+
invariant(in.width() % inMCUSize.x == 0);
417+
for (int MCUIdx = 0, numMCUsPerRow = in.width() / inMCUSize.x;
418418
MCUIdx < numMCUsPerRow; ++MCUIdx) {
419419
for (int outMCURow = 0; outMCURow != outMCUSize.y; ++outMCURow) {
420420
for (int outMCUСol = 0; outMCUСol != outMCUSize.x; ++outMCUСol) {

src/librawspeed/decoders/IiqDecoder.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,9 @@ void IiqDecoder::CorrectQuadrantMultipliersCombined(ByteStream data,
380380
const std::vector<uint16_t> curve = s.calculateCurve();
381381

382382
int row_start = quadRow == 0 ? 0 : split_row;
383-
int row_end = quadRow == 0 ? split_row : img.height;
383+
int row_end = quadRow == 0 ? split_row : img.height();
384384
int col_start = quadCol == 0 ? 0 : split_col;
385-
int col_end = quadCol == 0 ? split_col : img.width;
385+
int col_end = quadCol == 0 ? split_col : img.width();
386386

387387
for (int row = row_start; row < row_end; row++) {
388388
for (int col = col_start; col < col_end; col++) {

0 commit comments

Comments
 (0)