// Copyright (c) the JPEG XL Project Authors. All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. #ifndef LIB_JXL_BASE_RECT_H_ #define LIB_JXL_BASE_RECT_H_ #include #include #include #include #include #include #include // std::move #include "lib/jxl/base/compiler_specific.h" #include "lib/jxl/base/status.h" namespace jxl { // Rectangular region in image(s). Factoring this out of Image instead of // shifting the pointer by x0/y0 allows this to apply to multiple images with // different resolutions (e.g. color transform and quantization field). // Can compare using SameSize(rect1, rect2). template class RectT { public: // Most windows are xsize_max * ysize_max, except those on the borders where // begin + size_max > end. constexpr RectT(T xbegin, T ybegin, size_t xsize_max, size_t ysize_max, T xend, T yend) : x0_(xbegin), y0_(ybegin), xsize_(ClampedSize(xbegin, xsize_max, xend)), ysize_(ClampedSize(ybegin, ysize_max, yend)) {} // Construct with origin and known size (typically from another Rect). constexpr RectT(T xbegin, T ybegin, size_t xsize, size_t ysize) : x0_(xbegin), y0_(ybegin), xsize_(xsize), ysize_(ysize) {} // Construct a rect that covers a whole image/plane/ImageBundle etc. template explicit RectT(const ImageT& image) : RectT(0, 0, image.xsize(), image.ysize()) {} RectT() : RectT(0, 0, 0, 0) {} RectT(const RectT&) = default; RectT& operator=(const RectT&) = default; // Construct a subrect that resides in an image/plane/ImageBundle etc. template RectT Crop(const ImageT& image) const { return Intersection(RectT(image)); } // Construct a subrect that resides in the [0, ysize) x [0, xsize) region of // the current rect. RectT Crop(size_t area_xsize, size_t area_ysize) const { return Intersection(RectT(0, 0, area_xsize, area_ysize)); } // Returns a rect that only contains `num` lines with offset `y` from `y0()`. RectT Lines(size_t y, size_t num) const { JXL_DASSERT(y + num <= ysize_); return RectT(x0_, y0_ + y, xsize_, num); } RectT Line(size_t y) const { return Lines(y, 1); } JXL_MUST_USE_RESULT RectT Intersection(const RectT& other) const { return RectT(std::max(x0_, other.x0_), std::max(y0_, other.y0_), xsize_, ysize_, std::min(x1(), other.x1()), std::min(y1(), other.y1())); } JXL_MUST_USE_RESULT RectT Translate(int64_t x_offset, int64_t y_offset) const { return RectT(x0_ + x_offset, y0_ + y_offset, xsize_, ysize_); } template