Coverage Report

Created: 2025-06-13 07:37

/src/libjxl/lib/jxl/base/rect.h
Line
Count
Source (jump to first uncovered line)
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
1.99M
      : x0_(xbegin),
34
1.99M
        y0_(ybegin),
35
1.99M
        xsize_(ClampedSize(xbegin, xsize_max, xend)),
36
1.99M
        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
1.91M
      : x0_(xbegin),
34
1.91M
        y0_(ybegin),
35
1.91M
        xsize_(ClampedSize(xbegin, xsize_max, xend)),
36
1.91M
        ysize_(ClampedSize(ybegin, ysize_max, yend)) {}
jxl::RectT<long>::RectT(long, long, unsigned long, unsigned long, long, long)
Line
Count
Source
33
79.9k
      : x0_(xbegin),
34
79.9k
        y0_(ybegin),
35
79.9k
        xsize_(ClampedSize(xbegin, xsize_max, xend)),
36
79.9k
        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
9.62M
      : 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
7.30M
      : x0_(xbegin), y0_(ybegin), xsize_(xsize), ysize_(ysize) {}
jxl::RectT<long>::RectT(long, long, unsigned long, unsigned long)
Line
Count
Source
40
2.32M
      : 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
753k
      : RectT(0, 0, image.xsize(), image.ysize()) {}
jxl::RectT<unsigned long>::RectT<jxl::Plane<float> >(jxl::Plane<float> const&)
Line
Count
Source
45
328k
      : RectT(0, 0, image.xsize(), image.ysize()) {}
jxl::RectT<unsigned long>::RectT<jxl::Plane<int> >(jxl::Plane<int> const&)
Line
Count
Source
45
422k
      : RectT(0, 0, image.xsize(), image.ysize()) {}
jxl::RectT<unsigned long>::RectT<jxl::Image3<float> >(jxl::Image3<float> const&)
Line
Count
Source
45
1.34k
      : RectT(0, 0, image.xsize(), image.ysize()) {}
Unexecuted instantiation: jxl::RectT<unsigned long>::RectT<jxl::ImageBundle>(jxl::ImageBundle const&)
jxl::RectT<unsigned long>::RectT<jxl::Plane<unsigned char> >(jxl::Plane<unsigned char> const&)
Line
Count
Source
45
1.07k
      : RectT(0, 0, image.xsize(), image.ysize()) {}
46
47
2.72M
  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
319k
  RectT Crop(const ImageT& image) const {
55
319k
    return Intersection(RectT(image));
56
319k
  }
57
58
  // Construct a subrect that resides in the [0, ysize) x [0, xsize) region of
59
  // the current rect.
60
111k
  RectT Crop(size_t area_xsize, size_t area_ysize) const {
61
111k
    return Intersection(RectT(0, 0, area_xsize, area_ysize));
62
111k
  }
63
64
510k
  JXL_MUST_USE_RESULT RectT Intersection(const RectT& other) const {
65
510k
    return RectT(std::max(x0_, other.x0_), std::max(y0_, other.y0_), xsize_,
66
510k
                 ysize_, std::min(x1(), other.x1()),
67
510k
                 std::min(y1(), other.y1()));
68
510k
  }
jxl::RectT<unsigned long>::Intersection(jxl::RectT<unsigned long> const&) const
Line
Count
Source
64
430k
  JXL_MUST_USE_RESULT RectT Intersection(const RectT& other) const {
65
430k
    return RectT(std::max(x0_, other.x0_), std::max(y0_, other.y0_), xsize_,
66
430k
                 ysize_, std::min(x1(), other.x1()),
67
430k
                 std::min(y1(), other.y1()));
68
430k
  }
jxl::RectT<long>::Intersection(jxl::RectT<long> const&) const
Line
Count
Source
64
79.9k
  JXL_MUST_USE_RESULT RectT Intersection(const RectT& other) const {
65
79.9k
    return RectT(std::max(x0_, other.x0_), std::max(y0_, other.y0_), xsize_,
66
79.9k
                 ysize_, std::min(x1(), other.x1()),
67
79.9k
                 std::min(y1(), other.y1()));
68
79.9k
  }
69
70
  JXL_MUST_USE_RESULT RectT Translate(int64_t x_offset,
71
920k
                                      int64_t y_offset) const {
72
920k
    return RectT(x0_ + x_offset, y0_ + y_offset, xsize_, ysize_);
73
920k
  }
jxl::RectT<unsigned long>::Translate(long, long) const
Line
Count
Source
71
268
                                      int64_t y_offset) const {
72
268
    return RectT(x0_ + x_offset, y0_ + y_offset, xsize_, ysize_);
73
268
  }
jxl::RectT<long>::Translate(long, long) const
Line
Count
Source
71
920k
                                      int64_t y_offset) const {
72
920k
    return RectT(x0_ + x_offset, y0_ + y_offset, xsize_, ysize_);
73
920k
  }
74
75
  template <template <class> class P, typename V>
76
47.1M
  V* Row(P<V>* image, size_t y) const {
77
47.1M
    JXL_DASSERT(y + y0_ >= 0);
78
47.1M
    return image->Row(y + y0_) + x0_;
79
47.1M
  }
Unexecuted instantiation: unsigned char* jxl::RectT<unsigned long>::Row<jxl::Plane, unsigned char>(jxl::Plane<unsigned char>*, unsigned long) const
float* jxl::RectT<unsigned long>::Row<jxl::Plane, float>(jxl::Plane<float>*, unsigned long) const
Line
Count
Source
76
27.1M
  V* Row(P<V>* image, size_t y) const {
77
27.1M
    JXL_DASSERT(y + y0_ >= 0);
78
27.1M
    return image->Row(y + y0_) + x0_;
79
27.1M
  }
unsigned char* jxl::RectT<unsigned long>::Row<jxl::Plane, unsigned char>(jxl::Plane<unsigned char>*, unsigned long) const
Line
Count
Source
76
515k
  V* Row(P<V>* image, size_t y) const {
77
515k
    JXL_DASSERT(y + y0_ >= 0);
78
515k
    return image->Row(y + y0_) + x0_;
79
515k
  }
int* jxl::RectT<unsigned long>::Row<jxl::Plane, int>(jxl::Plane<int>*, unsigned long) const
Line
Count
Source
76
18.9M
  V* Row(P<V>* image, size_t y) const {
77
18.9M
    JXL_DASSERT(y + y0_ >= 0);
78
18.9M
    return image->Row(y + y0_) + x0_;
79
18.9M
  }
signed char* jxl::RectT<unsigned long>::Row<jxl::Plane, signed char>(jxl::Plane<signed char>*, unsigned long) const
Line
Count
Source
76
87.8k
  V* Row(P<V>* image, size_t y) const {
77
87.8k
    JXL_DASSERT(y + y0_ >= 0);
78
87.8k
    return image->Row(y + y0_) + x0_;
79
87.8k
  }
float* jxl::RectT<long>::Row<jxl::Plane, float>(jxl::Plane<float>*, unsigned long) const
Line
Count
Source
76
420k
  V* Row(P<V>* image, size_t y) const {
77
420k
    JXL_DASSERT(y + y0_ >= 0);
78
420k
    return image->Row(y + y0_) + x0_;
79
420k
  }
80
81
  template <template <class> class P, typename V>
82
118k
  const V* Row(const P<V>* image, size_t y) const {
83
118k
    JXL_DASSERT(y + y0_ >= 0);
84
118k
    return image->Row(y + y0_) + x0_;
85
118k
  }
86
87
  template <template <class> class MP, typename V>
88
779k
  V* PlaneRow(MP<V>* image, const size_t c, size_t y) const {
89
779k
    JXL_DASSERT(y + y0_ >= 0);
90
779k
    return image->PlaneRow(c, y + y0_) + x0_;
91
779k
  }
92
93
  template <template <class> class P, typename V>
94
5.59M
  const V* ConstRow(const P<V>& image, size_t y) const {
95
5.59M
    JXL_DASSERT(y + y0_ >= 0);
96
5.59M
    return image.ConstRow(y + y0_) + x0_;
97
5.59M
  }
float const* jxl::RectT<unsigned long>::ConstRow<jxl::Plane, float>(jxl::Plane<float> const&, unsigned long) const
Line
Count
Source
94
3.79M
  const V* ConstRow(const P<V>& image, size_t y) const {
95
3.79M
    JXL_DASSERT(y + y0_ >= 0);
96
3.79M
    return image.ConstRow(y + y0_) + x0_;
97
3.79M
  }
int const* jxl::RectT<unsigned long>::ConstRow<jxl::Plane, int>(jxl::Plane<int> const&, unsigned long) const
Line
Count
Source
94
1.61M
  const V* ConstRow(const P<V>& image, size_t y) const {
95
1.61M
    JXL_DASSERT(y + y0_ >= 0);
96
1.61M
    return image.ConstRow(y + y0_) + x0_;
97
1.61M
  }
unsigned char const* jxl::RectT<unsigned long>::ConstRow<jxl::Plane, unsigned char>(jxl::Plane<unsigned char> const&, unsigned long) const
Line
Count
Source
94
177k
  const V* ConstRow(const P<V>& image, size_t y) const {
95
177k
    JXL_DASSERT(y + y0_ >= 0);
96
177k
    return image.ConstRow(y + y0_) + x0_;
97
177k
  }
signed char const* jxl::RectT<unsigned long>::ConstRow<jxl::Plane, signed char>(jxl::Plane<signed char> const&, unsigned long) const
Line
Count
Source
94
1.07k
  const V* ConstRow(const P<V>& image, size_t y) const {
95
1.07k
    JXL_DASSERT(y + y0_ >= 0);
96
1.07k
    return image.ConstRow(y + y0_) + x0_;
97
1.07k
  }
98
99
  template <template <class> class MP, typename V>
100
627k
  const V* ConstPlaneRow(const MP<V>& image, size_t c, size_t y) const {
101
627k
    JXL_DASSERT(y + y0_ >= 0);
102
627k
    return image.ConstPlaneRow(c, y + y0_) + x0_;
103
627k
  }
104
105
471k
  bool IsInside(const RectT& other) const {
106
471k
    return x0_ >= other.x0() && x1() <= other.x1() && y0_ >= other.y0() &&
107
471k
           y1() <= other.y1();
108
471k
  }
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
392k
  bool IsInside(const ImageT& image) const {
119
392k
    return IsInside(RectT(image));
120
392k
  }
bool jxl::RectT<unsigned long>::IsInside<jxl::Plane<float> >(jxl::Plane<float> const&) const
Line
Count
Source
118
328k
  bool IsInside(const ImageT& image) const {
119
328k
    return IsInside(RectT(image));
120
328k
  }
bool jxl::RectT<unsigned long>::IsInside<jxl::Plane<int> >(jxl::Plane<int> const&) const
Line
Count
Source
118
63.6k
  bool IsInside(const ImageT& image) const {
119
63.6k
    return IsInside(RectT(image));
120
63.6k
  }
121
122
69.3M
  T x0() const { return x0_; }
jxl::RectT<unsigned long>::x0() const
Line
Count
Source
122
69.2M
  T x0() const { return x0_; }
jxl::RectT<long>::x0() const
Line
Count
Source
122
55.7k
  T x0() const { return x0_; }
123
71.6M
  T y0() const { return y0_; }
jxl::RectT<unsigned long>::y0() const
Line
Count
Source
123
71.6M
  T y0() const { return y0_; }
jxl::RectT<long>::y0() const
Line
Count
Source
123
24.2k
  T y0() const { return y0_; }
124
110M
  size_t xsize() const { return xsize_; }
jxl::RectT<unsigned long>::xsize() const
Line
Count
Source
124
110M
  size_t xsize() const { return xsize_; }
jxl::RectT<long>::xsize() const
Line
Count
Source
124
72.6k
  size_t xsize() const { return xsize_; }
125
81.9M
  size_t ysize() const { return ysize_; }
jxl::RectT<unsigned long>::ysize() const
Line
Count
Source
125
81.8M
  size_t ysize() const { return ysize_; }
jxl::RectT<long>::ysize() const
Line
Count
Source
125
62.4k
  size_t ysize() const { return ysize_; }
126
2.14M
  T x1() const { return x0_ + xsize_; }
jxl::RectT<unsigned long>::x1() const
Line
Count
Source
126
1.92M
  T x1() const { return x0_ + xsize_; }
jxl::RectT<long>::x1() const
Line
Count
Source
126
215k
  T x1() const { return x0_ + xsize_; }
127
1.99M
  T y1() const { return y0_ + ysize_; }
jxl::RectT<unsigned long>::y1() const
Line
Count
Source
127
1.80M
  T y1() const { return y0_ + ysize_; }
jxl::RectT<long>::y1() const
Line
Count
Source
127
184k
  T y1() const { return y0_ + ysize_; }
128
129
531k
  RectT<T> ShiftLeft(size_t shiftx, size_t shifty) const {
130
531k
    return RectT<T>(x0_ * (1 << shiftx), y0_ * (1 << shifty), xsize_ << shiftx,
131
531k
                    ysize_ << shifty);
132
531k
  }
jxl::RectT<long>::ShiftLeft(unsigned long, unsigned long) const
Line
Count
Source
129
420k
  RectT<T> ShiftLeft(size_t shiftx, size_t shifty) const {
130
420k
    return RectT<T>(x0_ * (1 << shiftx), y0_ * (1 << shifty), xsize_ << shiftx,
131
420k
                    ysize_ << shifty);
132
420k
  }
jxl::RectT<unsigned long>::ShiftLeft(unsigned long, unsigned long) const
Line
Count
Source
129
111k
  RectT<T> ShiftLeft(size_t shiftx, size_t shifty) const {
130
111k
    return RectT<T>(x0_ * (1 << shiftx), y0_ * (1 << shifty), xsize_ << shiftx,
131
111k
                    ysize_ << shifty);
132
111k
  }
133
531k
  RectT<T> ShiftLeft(size_t shift) const { return ShiftLeft(shift, shift); }
jxl::RectT<long>::ShiftLeft(unsigned long) const
Line
Count
Source
133
420k
  RectT<T> ShiftLeft(size_t shift) const { return ShiftLeft(shift, shift); }
jxl::RectT<unsigned long>::ShiftLeft(unsigned long) const
Line
Count
Source
133
111k
  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
1.13M
  StatusOr<RectT<T>> CeilShiftRight(std::pair<size_t, size_t> shift) const {
137
1.13M
    size_t shiftx = shift.first;
138
1.13M
    size_t shifty = shift.second;
139
1.13M
    JXL_ENSURE((x0_ % (1 << shiftx) == 0) && (y0_ % (1 << shifty) == 0));
140
1.13M
    return RectT<T>(x0_ / (1 << shiftx), y0_ / (1 << shifty),
141
1.13M
                    DivCeil(xsize_, T{1} << shiftx),
142
1.13M
                    DivCeil(ysize_, T{1} << shifty));
143
1.13M
  }
jxl::RectT<long>::CeilShiftRight(std::__1::pair<unsigned long, unsigned long>) const
Line
Count
Source
136
420k
  StatusOr<RectT<T>> CeilShiftRight(std::pair<size_t, size_t> shift) const {
137
420k
    size_t shiftx = shift.first;
138
420k
    size_t shifty = shift.second;
139
420k
    JXL_ENSURE((x0_ % (1 << shiftx) == 0) && (y0_ % (1 << shifty) == 0));
140
420k
    return RectT<T>(x0_ / (1 << shiftx), y0_ / (1 << shifty),
141
420k
                    DivCeil(xsize_, T{1} << shiftx),
142
420k
                    DivCeil(ysize_, T{1} << shifty));
143
420k
  }
jxl::RectT<unsigned long>::CeilShiftRight(std::__1::pair<unsigned long, unsigned long>) const
Line
Count
Source
136
713k
  StatusOr<RectT<T>> CeilShiftRight(std::pair<size_t, size_t> shift) const {
137
713k
    size_t shiftx = shift.first;
138
713k
    size_t shifty = shift.second;
139
713k
    JXL_ENSURE((x0_ % (1 << shiftx) == 0) && (y0_ % (1 << shifty) == 0));
140
713k
    return RectT<T>(x0_ / (1 << shiftx), y0_ / (1 << shifty),
141
713k
                    DivCeil(xsize_, T{1} << shiftx),
142
713k
                    DivCeil(ysize_, T{1} << shifty));
143
713k
  }
144
145
536
  RectT<T> Extend(T border, RectT<T> parent) const {
146
536
    T new_x0 = x0() > parent.x0() + border ? x0() - border : parent.x0();
147
536
    T new_y0 = y0() > parent.y0() + border ? y0() - border : parent.y0();
148
536
    T new_x1 = x1() + border > parent.x1() ? parent.x1() : x1() + border;
149
536
    T new_y1 = y1() + border > parent.y1() ? parent.y1() : y1() + border;
150
536
    return RectT<T>(new_x0, new_y0, new_x1 - new_x0, new_y1 - new_y0);
151
536
  }
152
153
  template <typename U>
154
420k
  RectT<U> As() const {
155
420k
    return RectT<U>(static_cast<U>(x0_), static_cast<U>(y0_),
156
420k
                    static_cast<U>(xsize_), static_cast<U>(ysize_));
157
420k
  }
158
159
 private:
160
  // Returns size_max, or whatever is left in [begin, end).
161
3.99M
  static constexpr size_t ClampedSize(T begin, size_t size_max, T end) {
162
3.99M
    return (static_cast<T>(begin + size_max) <= end)
163
3.99M
               ? size_max
164
3.99M
               : (end > begin ? end - begin : 0);
165
3.99M
  }
jxl::RectT<unsigned long>::ClampedSize(unsigned long, unsigned long, unsigned long)
Line
Count
Source
161
3.83M
  static constexpr size_t ClampedSize(T begin, size_t size_max, T end) {
162
3.83M
    return (static_cast<T>(begin + size_max) <= end)
163
3.83M
               ? size_max
164
3.83M
               : (end > begin ? end - begin : 0);
165
3.83M
  }
jxl::RectT<long>::ClampedSize(long, unsigned long, long)
Line
Count
Source
161
159k
  static constexpr size_t ClampedSize(T begin, size_t size_max, T end) {
162
159k
    return (static_cast<T>(begin + size_max) <= end)
163
159k
               ? size_max
164
159k
               : (end > begin ? end - begin : 0);
165
159k
  }
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_