Coverage Report

Created: 2026-01-20 07:37

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/base/rect.h
Line
Count
Source
1
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
2
//
3
// Use of this source code is governed by a BSD-style
4
// license that can be found in the LICENSE file.
5
6
#ifndef LIB_JXL_BASE_RECT_H_
7
#define LIB_JXL_BASE_RECT_H_
8
9
#include <algorithm>
10
#include <cstddef>
11
#include <cstdint>
12
#include <cstring>
13
#include <sstream>
14
#include <string>
15
#include <utility>  // std::move
16
17
#include "lib/jxl/base/compiler_specific.h"
18
#include "lib/jxl/base/status.h"
19
20
namespace jxl {
21
22
// Rectangular region in image(s). Factoring this out of Image instead of
23
// shifting the pointer by x0/y0 allows this to apply to multiple images with
24
// different resolutions (e.g. color transform and quantization field).
25
// Can compare using SameSize(rect1, rect2).
26
template <typename T>
27
class RectT {
28
 public:
29
  // Most windows are xsize_max * ysize_max, except those on the borders where
30
  // begin + size_max > end.
31
  constexpr RectT(T xbegin, T ybegin, size_t xsize_max, size_t ysize_max,
32
                  T xend, T yend)
33
19.8M
      : x0_(xbegin),
34
19.8M
        y0_(ybegin),
35
19.8M
        xsize_(ClampedSize(xbegin, xsize_max, xend)),
36
19.8M
        ysize_(ClampedSize(ybegin, ysize_max, yend)) {}
jxl::RectT<unsigned long>::RectT(unsigned long, unsigned long, unsigned long, unsigned long, unsigned long, unsigned long)
Line
Count
Source
33
19.8M
      : x0_(xbegin),
34
19.8M
        y0_(ybegin),
35
19.8M
        xsize_(ClampedSize(xbegin, xsize_max, xend)),
36
19.8M
        ysize_(ClampedSize(ybegin, ysize_max, yend)) {}
jxl::RectT<long>::RectT(long, long, unsigned long, unsigned long, long, long)
Line
Count
Source
33
60.2k
      : x0_(xbegin),
34
60.2k
        y0_(ybegin),
35
60.2k
        xsize_(ClampedSize(xbegin, xsize_max, xend)),
36
60.2k
        ysize_(ClampedSize(ybegin, ysize_max, yend)) {}
37
38
  // Construct with origin and known size (typically from another Rect).
39
  constexpr RectT(T xbegin, T ybegin, size_t xsize, size_t ysize)
40
7.87M
      : x0_(xbegin), y0_(ybegin), xsize_(xsize), ysize_(ysize) {}
jxl::RectT<unsigned long>::RectT(unsigned long, unsigned long, unsigned long, unsigned long)
Line
Count
Source
40
6.42M
      : x0_(xbegin), y0_(ybegin), xsize_(xsize), ysize_(ysize) {}
jxl::RectT<long>::RectT(long, long, unsigned long, unsigned long)
Line
Count
Source
40
1.45M
      : x0_(xbegin), y0_(ybegin), xsize_(xsize), ysize_(ysize) {}
41
42
  // Construct a rect that covers a whole image/plane/ImageBundle etc.
43
  template <typename ImageT>
44
  explicit RectT(const ImageT& image)
45
854k
      : RectT(0, 0, image.xsize(), image.ysize()) {}
jxl::RectT<unsigned long>::RectT<jxl::Plane<float> >(jxl::Plane<float> const&)
Line
Count
Source
45
672k
      : RectT(0, 0, image.xsize(), image.ysize()) {}
jxl::RectT<unsigned long>::RectT<jxl::Image3<float> >(jxl::Image3<float> const&)
Line
Count
Source
45
19.7k
      : RectT(0, 0, image.xsize(), image.ysize()) {}
jxl::RectT<unsigned long>::RectT<jxl::Plane<unsigned char> >(jxl::Plane<unsigned char> const&)
Line
Count
Source
45
17.5k
      : RectT(0, 0, image.xsize(), image.ysize()) {}
jxl::RectT<unsigned long>::RectT<jxl::Plane<int> >(jxl::Plane<int> const&)
Line
Count
Source
45
144k
      : RectT(0, 0, image.xsize(), image.ysize()) {}
46
47
2.04M
  RectT() : RectT(0, 0, 0, 0) {}
48
49
  RectT(const RectT&) = default;
50
  RectT& operator=(const RectT&) = default;
51
52
  // Construct a subrect that resides in an image/plane/ImageBundle etc.
53
  template <typename ImageT>
54
111k
  RectT Crop(const ImageT& image) const {
55
111k
    return Intersection(RectT(image));
56
111k
  }
57
58
  // Construct a subrect that resides in the [0, ysize) x [0, xsize) region of
59
  // the current rect.
60
77.2k
  RectT Crop(size_t area_xsize, size_t area_ysize) const {
61
77.2k
    return Intersection(RectT(0, 0, area_xsize, area_ysize));
62
77.2k
  }
63
64
248k
  JXL_MUST_USE_RESULT RectT Intersection(const RectT& other) const {
65
248k
    return RectT(std::max(x0_, other.x0_), std::max(y0_, other.y0_), xsize_,
66
248k
                 ysize_, std::min(x1(), other.x1()),
67
248k
                 std::min(y1(), other.y1()));
68
248k
  }
jxl::RectT<unsigned long>::Intersection(jxl::RectT<unsigned long> const&) const
Line
Count
Source
64
188k
  JXL_MUST_USE_RESULT RectT Intersection(const RectT& other) const {
65
188k
    return RectT(std::max(x0_, other.x0_), std::max(y0_, other.y0_), xsize_,
66
188k
                 ysize_, std::min(x1(), other.x1()),
67
188k
                 std::min(y1(), other.y1()));
68
188k
  }
jxl::RectT<long>::Intersection(jxl::RectT<long> const&) const
Line
Count
Source
64
60.2k
  JXL_MUST_USE_RESULT RectT Intersection(const RectT& other) const {
65
60.2k
    return RectT(std::max(x0_, other.x0_), std::max(y0_, other.y0_), xsize_,
66
60.2k
                 ysize_, std::min(x1(), other.x1()),
67
60.2k
                 std::min(y1(), other.y1()));
68
60.2k
  }
69
70
  JXL_MUST_USE_RESULT RectT Translate(int64_t x_offset,
71
585k
                                      int64_t y_offset) const {
72
585k
    return RectT(x0_ + x_offset, y0_ + y_offset, xsize_, ysize_);
73
585k
  }
jxl::RectT<unsigned long>::Translate(long, long) const
Line
Count
Source
71
10.7k
                                      int64_t y_offset) const {
72
10.7k
    return RectT(x0_ + x_offset, y0_ + y_offset, xsize_, ysize_);
73
10.7k
  }
jxl::RectT<long>::Translate(long, long) const
Line
Count
Source
71
574k
                                      int64_t y_offset) const {
72
574k
    return RectT(x0_ + x_offset, y0_ + y_offset, xsize_, ysize_);
73
574k
  }
74
75
  template <template <class> class P, typename V>
76
57.8M
  V* Row(P<V>* image, size_t y) const {
77
57.8M
    JXL_DASSERT(y + y0_ >= 0);
78
57.8M
    return image->Row(y + y0_) + x0_;
79
57.8M
  }
unsigned char* jxl::RectT<unsigned long>::Row<jxl::Plane, unsigned char>(jxl::Plane<unsigned char>*, unsigned long) const
Line
Count
Source
76
1.22M
  V* Row(P<V>* image, size_t y) const {
77
1.22M
    JXL_DASSERT(y + y0_ >= 0);
78
1.22M
    return image->Row(y + y0_) + x0_;
79
1.22M
  }
float* jxl::RectT<unsigned long>::Row<jxl::Plane, float>(jxl::Plane<float>*, unsigned long) const
Line
Count
Source
76
49.7M
  V* Row(P<V>* image, size_t y) const {
77
49.7M
    JXL_DASSERT(y + y0_ >= 0);
78
49.7M
    return image->Row(y + y0_) + x0_;
79
49.7M
  }
int* jxl::RectT<unsigned long>::Row<jxl::Plane, int>(jxl::Plane<int>*, unsigned long) const
Line
Count
Source
76
6.57M
  V* Row(P<V>* image, size_t y) const {
77
6.57M
    JXL_DASSERT(y + y0_ >= 0);
78
6.57M
    return image->Row(y + y0_) + x0_;
79
6.57M
  }
signed char* jxl::RectT<unsigned long>::Row<jxl::Plane, signed char>(jxl::Plane<signed char>*, unsigned long) const
Line
Count
Source
76
58.3k
  V* Row(P<V>* image, size_t y) const {
77
58.3k
    JXL_DASSERT(y + y0_ >= 0);
78
58.3k
    return image->Row(y + y0_) + x0_;
79
58.3k
  }
float* jxl::RectT<long>::Row<jxl::Plane, float>(jxl::Plane<float>*, unsigned long) const
Line
Count
Source
76
257k
  V* Row(P<V>* image, size_t y) const {
77
257k
    JXL_DASSERT(y + y0_ >= 0);
78
257k
    return image->Row(y + y0_) + x0_;
79
257k
  }
80
81
  template <template <class> class P, typename V>
82
3.59G
  const V* Row(const P<V>* image, size_t y) const {
83
3.59G
    JXL_DASSERT(y + y0_ >= 0);
84
3.59G
    return image->Row(y + y0_) + x0_;
85
3.59G
  }
86
87
  template <template <class> class MP, typename V>
88
1.95M
  V* PlaneRow(MP<V>* image, const size_t c, size_t y) const {
89
1.95M
    JXL_DASSERT(y + y0_ >= 0);
90
1.95M
    return image->PlaneRow(c, y + y0_) + x0_;
91
1.95M
  }
92
93
  template <template <class> class P, typename V>
94
527M
  const V* ConstRow(const P<V>& image, size_t y) const {
95
527M
    JXL_DASSERT(y + y0_ >= 0);
96
527M
    return image.ConstRow(y + y0_) + x0_;
97
527M
  }
float const* jxl::RectT<unsigned long>::ConstRow<jxl::Plane, float>(jxl::Plane<float> const&, unsigned long) const
Line
Count
Source
94
522M
  const V* ConstRow(const P<V>& image, size_t y) const {
95
522M
    JXL_DASSERT(y + y0_ >= 0);
96
522M
    return image.ConstRow(y + y0_) + x0_;
97
522M
  }
signed char const* jxl::RectT<unsigned long>::ConstRow<jxl::Plane, signed char>(jxl::Plane<signed char> const&, unsigned long) const
Line
Count
Source
94
542k
  const V* ConstRow(const P<V>& image, size_t y) const {
95
542k
    JXL_DASSERT(y + y0_ >= 0);
96
542k
    return image.ConstRow(y + y0_) + x0_;
97
542k
  }
int const* jxl::RectT<unsigned long>::ConstRow<jxl::Plane, int>(jxl::Plane<int> const&, unsigned long) const
Line
Count
Source
94
2.69M
  const V* ConstRow(const P<V>& image, size_t y) const {
95
2.69M
    JXL_DASSERT(y + y0_ >= 0);
96
2.69M
    return image.ConstRow(y + y0_) + x0_;
97
2.69M
  }
unsigned char const* jxl::RectT<unsigned long>::ConstRow<jxl::Plane, unsigned char>(jxl::Plane<unsigned char> const&, unsigned long) const
Line
Count
Source
94
1.08M
  const V* ConstRow(const P<V>& image, size_t y) const {
95
1.08M
    JXL_DASSERT(y + y0_ >= 0);
96
1.08M
    return image.ConstRow(y + y0_) + x0_;
97
1.08M
  }
98
99
  template <template <class> class MP, typename V>
100
902M
  const V* ConstPlaneRow(const MP<V>& image, size_t c, size_t y) const {
101
902M
    JXL_DASSERT(y + y0_ >= 0);
102
902M
    return image.ConstPlaneRow(c, y + y0_) + x0_;
103
902M
  }
104
105
813k
  bool IsInside(const RectT& other) const {
106
813k
    return x0_ >= other.x0() && x1() <= other.x1() && y0_ >= other.y0() &&
107
813k
           y1() <= other.y1();
108
813k
  }
109
110
  bool IsSame(const RectT& other) const {
111
    return x0_ == other.x0_ && xsize_ == other.xsize_ && y0_ == other.y0_ &&
112
           ysize_ <= other.ysize_;
113
  }
114
115
  // Returns true if this Rect fully resides in the given image. ImageT could be
116
  // Plane<T> or Image3<T>; however if ImageT is Rect, results are nonsensical.
117
  template <class ImageT>
118
675k
  bool IsInside(const ImageT& image) const {
119
675k
    return IsInside(RectT(image));
120
675k
  }
bool jxl::RectT<unsigned long>::IsInside<jxl::Plane<float> >(jxl::Plane<float> const&) const
Line
Count
Source
118
668k
  bool IsInside(const ImageT& image) const {
119
668k
    return IsInside(RectT(image));
120
668k
  }
bool jxl::RectT<unsigned long>::IsInside<jxl::Plane<int> >(jxl::Plane<int> const&) const
Line
Count
Source
118
7.16k
  bool IsInside(const ImageT& image) const {
119
7.16k
    return IsInside(RectT(image));
120
7.16k
  }
121
122
213M
  T x0() const { return x0_; }
jxl::RectT<unsigned long>::x0() const
Line
Count
Source
122
213M
  T x0() const { return x0_; }
jxl::RectT<long>::x0() const
Line
Count
Source
122
40.7k
  T x0() const { return x0_; }
123
163M
  T y0() const { return y0_; }
jxl::RectT<unsigned long>::y0() const
Line
Count
Source
123
163M
  T y0() const { return y0_; }
jxl::RectT<long>::y0() const
Line
Count
Source
123
19.5k
  T y0() const { return y0_; }
124
2.67G
  size_t xsize() const { return xsize_; }
jxl::RectT<unsigned long>::xsize() const
Line
Count
Source
124
2.67G
  size_t xsize() const { return xsize_; }
jxl::RectT<long>::xsize() const
Line
Count
Source
124
51.6k
  size_t xsize() const { return xsize_; }
125
393M
  size_t ysize() const { return ysize_; }
jxl::RectT<unsigned long>::ysize() const
Line
Count
Source
125
393M
  size_t ysize() const { return ysize_; }
jxl::RectT<long>::ysize() const
Line
Count
Source
125
37.5k
  size_t ysize() const { return ysize_; }
126
14.5M
  T x1() const { return x0_ + xsize_; }
jxl::RectT<unsigned long>::x1() const
Line
Count
Source
126
14.4M
  T x1() const { return x0_ + xsize_; }
jxl::RectT<long>::x1() const
Line
Count
Source
126
161k
  T x1() const { return x0_ + xsize_; }
127
53.9M
  T y1() const { return y0_ + ysize_; }
jxl::RectT<unsigned long>::y1() const
Line
Count
Source
127
53.8M
  T y1() const { return y0_ + ysize_; }
jxl::RectT<long>::y1() const
Line
Count
Source
127
140k
  T y1() const { return y0_ + ysize_; }
128
129
334k
  RectT<T> ShiftLeft(size_t shiftx, size_t shifty) const {
130
334k
    return RectT<T>(x0_ * (1 << shiftx), y0_ * (1 << shifty), xsize_ << shiftx,
131
334k
                    ysize_ << shifty);
132
334k
  }
jxl::RectT<long>::ShiftLeft(unsigned long, unsigned long) const
Line
Count
Source
129
257k
  RectT<T> ShiftLeft(size_t shiftx, size_t shifty) const {
130
257k
    return RectT<T>(x0_ * (1 << shiftx), y0_ * (1 << shifty), xsize_ << shiftx,
131
257k
                    ysize_ << shifty);
132
257k
  }
jxl::RectT<unsigned long>::ShiftLeft(unsigned long, unsigned long) const
Line
Count
Source
129
77.2k
  RectT<T> ShiftLeft(size_t shiftx, size_t shifty) const {
130
77.2k
    return RectT<T>(x0_ * (1 << shiftx), y0_ * (1 << shifty), xsize_ << shiftx,
131
77.2k
                    ysize_ << shifty);
132
77.2k
  }
133
334k
  RectT<T> ShiftLeft(size_t shift) const { return ShiftLeft(shift, shift); }
jxl::RectT<long>::ShiftLeft(unsigned long) const
Line
Count
Source
133
257k
  RectT<T> ShiftLeft(size_t shift) const { return ShiftLeft(shift, shift); }
jxl::RectT<unsigned long>::ShiftLeft(unsigned long) const
Line
Count
Source
133
77.2k
  RectT<T> ShiftLeft(size_t shift) const { return ShiftLeft(shift, shift); }
134
135
  // Requires x0(), y0() to be multiples of 1<<shiftx, 1<<shifty.
136
721k
  StatusOr<RectT<T>> CeilShiftRight(std::pair<size_t, size_t> shift) const {
137
721k
    size_t shiftx = shift.first;
138
721k
    size_t shifty = shift.second;
139
721k
    JXL_ENSURE((x0_ % (1 << shiftx) == 0) && (y0_ % (1 << shifty) == 0));
140
721k
    return RectT<T>(x0_ / (1 << shiftx), y0_ / (1 << shifty),
141
721k
                    DivCeil(xsize_, T{1} << shiftx),
142
721k
                    DivCeil(ysize_, T{1} << shifty));
143
721k
  }
jxl::RectT<long>::CeilShiftRight(std::__1::pair<unsigned long, unsigned long>) const
Line
Count
Source
136
257k
  StatusOr<RectT<T>> CeilShiftRight(std::pair<size_t, size_t> shift) const {
137
257k
    size_t shiftx = shift.first;
138
257k
    size_t shifty = shift.second;
139
257k
    JXL_ENSURE((x0_ % (1 << shiftx) == 0) && (y0_ % (1 << shifty) == 0));
140
257k
    return RectT<T>(x0_ / (1 << shiftx), y0_ / (1 << shifty),
141
257k
                    DivCeil(xsize_, T{1} << shiftx),
142
257k
                    DivCeil(ysize_, T{1} << shifty));
143
257k
  }
jxl::RectT<unsigned long>::CeilShiftRight(std::__1::pair<unsigned long, unsigned long>) const
Line
Count
Source
136
464k
  StatusOr<RectT<T>> CeilShiftRight(std::pair<size_t, size_t> shift) const {
137
464k
    size_t shiftx = shift.first;
138
464k
    size_t shifty = shift.second;
139
464k
    JXL_ENSURE((x0_ % (1 << shiftx) == 0) && (y0_ % (1 << shifty) == 0));
140
464k
    return RectT<T>(x0_ / (1 << shiftx), y0_ / (1 << shifty),
141
464k
                    DivCeil(xsize_, T{1} << shiftx),
142
464k
                    DivCeil(ysize_, T{1} << shifty));
143
464k
  }
144
145
10.9k
  RectT<T> Extend(T border, RectT<T> parent) const {
146
10.9k
    T new_x0 = x0() > parent.x0() + border ? x0() - border : parent.x0();
147
10.9k
    T new_y0 = y0() > parent.y0() + border ? y0() - border : parent.y0();
148
10.9k
    T new_x1 = x1() + border > parent.x1() ? parent.x1() : x1() + border;
149
10.9k
    T new_y1 = y1() + border > parent.y1() ? parent.y1() : y1() + border;
150
10.9k
    return RectT<T>(new_x0, new_y0, new_x1 - new_x0, new_y1 - new_y0);
151
10.9k
  }
152
153
  template <typename U>
154
257k
  RectT<U> As() const {
155
257k
    return RectT<U>(static_cast<U>(x0_), static_cast<U>(y0_),
156
257k
                    static_cast<U>(xsize_), static_cast<U>(ysize_));
157
257k
  }
158
159
 private:
160
  // Returns size_max, or whatever is left in [begin, end).
161
39.7M
  static constexpr size_t ClampedSize(T begin, size_t size_max, T end) {
162
39.7M
    return (static_cast<T>(begin + size_max) <= end)
163
39.7M
               ? size_max
164
39.7M
               : (end > begin ? end - begin : 0);
165
39.7M
  }
jxl::RectT<unsigned long>::ClampedSize(unsigned long, unsigned long, unsigned long)
Line
Count
Source
161
39.6M
  static constexpr size_t ClampedSize(T begin, size_t size_max, T end) {
162
39.6M
    return (static_cast<T>(begin + size_max) <= end)
163
39.6M
               ? size_max
164
39.6M
               : (end > begin ? end - begin : 0);
165
39.6M
  }
jxl::RectT<long>::ClampedSize(long, unsigned long, long)
Line
Count
Source
161
120k
  static constexpr size_t ClampedSize(T begin, size_t size_max, T end) {
162
120k
    return (static_cast<T>(begin + size_max) <= end)
163
120k
               ? size_max
164
120k
               : (end > begin ? end - begin : 0);
165
120k
  }
166
167
  T x0_;
168
  T y0_;
169
170
  size_t xsize_;
171
  size_t ysize_;
172
};
173
174
template <typename T>
175
std::string Description(RectT<T> r) {
176
  std::ostringstream os;
177
  os << "[" << r.x0() << ".." << r.x1() << ")x"
178
     << "[" << r.y0() << ".." << r.y1() << ")";
179
  return os.str();
180
}
181
182
using Rect = RectT<size_t>;
183
184
}  // namespace jxl
185
186
#endif  // LIB_JXL_BASE_RECT_H_