From 8dd16259287f58f9273002717ec4d27e97127719 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 12 Jun 2024 07:43:14 +0200 Subject: Merging upstream version 127.0. Signed-off-by: Daniel Baumann --- third_party/jpeg-xl/lib/jxl/ac_context.h | 11 +- third_party/jpeg-xl/lib/jxl/ac_strategy.h | 3 + third_party/jpeg-xl/lib/jxl/ac_strategy_test.cc | 8 +- third_party/jpeg-xl/lib/jxl/ans_common.h | 11 +- third_party/jpeg-xl/lib/jxl/ans_test.cc | 3 +- .../jpeg-xl/lib/jxl/base/compiler_specific.h | 10 +- third_party/jpeg-xl/lib/jxl/base/exif.h | 6 +- third_party/jpeg-xl/lib/jxl/base/include_jpeglib.h | 20 +++ third_party/jpeg-xl/lib/jxl/base/matrix_ops.h | 6 +- third_party/jpeg-xl/lib/jxl/base/rect.h | 194 +++++++++++++++++++++ third_party/jpeg-xl/lib/jxl/base/span.h | 7 +- third_party/jpeg-xl/lib/jxl/bit_reader_test.cc | 1 + third_party/jpeg-xl/lib/jxl/bits_test.cc | 3 + third_party/jpeg-xl/lib/jxl/blending.cc | 9 + third_party/jpeg-xl/lib/jxl/blending.h | 2 + .../jpeg-xl/lib/jxl/butteraugli/butteraugli.cc | 10 +- third_party/jpeg-xl/lib/jxl/cache_aligned.h | 2 - third_party/jpeg-xl/lib/jxl/chroma_from_luma.h | 7 +- third_party/jpeg-xl/lib/jxl/cms/jxl_cms_internal.h | 16 +- third_party/jpeg-xl/lib/jxl/cms/opsin_params.h | 3 + third_party/jpeg-xl/lib/jxl/cms/tone_mapping.h | 1 + third_party/jpeg-xl/lib/jxl/coeff_order.h | 17 +- third_party/jpeg-xl/lib/jxl/coeff_order_fwd.h | 4 +- .../jpeg-xl/lib/jxl/color_encoding_internal.h | 1 + .../jpeg-xl/lib/jxl/color_management_test.cc | 4 +- third_party/jpeg-xl/lib/jxl/compressed_dc.cc | 1 + third_party/jpeg-xl/lib/jxl/compressed_dc.h | 5 +- third_party/jpeg-xl/lib/jxl/convolve-inl.h | 2 + third_party/jpeg-xl/lib/jxl/convolve.h | 1 + third_party/jpeg-xl/lib/jxl/convolve_separable5.cc | 1 + third_party/jpeg-xl/lib/jxl/convolve_slow.cc | 4 +- third_party/jpeg-xl/lib/jxl/convolve_symmetric3.cc | 1 + third_party/jpeg-xl/lib/jxl/convolve_symmetric5.cc | 1 + third_party/jpeg-xl/lib/jxl/convolve_test.cc | 3 + third_party/jpeg-xl/lib/jxl/dct_for_test.h | 3 +- third_party/jpeg-xl/lib/jxl/dct_util.h | 5 +- third_party/jpeg-xl/lib/jxl/dec_ans.h | 7 +- third_party/jpeg-xl/lib/jxl/dec_frame.cc | 1 + third_party/jpeg-xl/lib/jxl/dec_group.cc | 6 +- third_party/jpeg-xl/lib/jxl/dec_group_border.cc | 7 + third_party/jpeg-xl/lib/jxl/dec_group_border.h | 9 +- third_party/jpeg-xl/lib/jxl/dec_modular.cc | 4 +- third_party/jpeg-xl/lib/jxl/dec_modular.h | 11 +- third_party/jpeg-xl/lib/jxl/dec_noise.cc | 13 +- third_party/jpeg-xl/lib/jxl/dec_noise.h | 6 +- third_party/jpeg-xl/lib/jxl/dec_patch_dictionary.h | 6 +- third_party/jpeg-xl/lib/jxl/dec_transforms-inl.h | 4 +- .../jpeg-xl/lib/jxl/dec_transforms_testonly.cc | 1 - .../jpeg-xl/lib/jxl/dec_transforms_testonly.h | 3 +- third_party/jpeg-xl/lib/jxl/dec_xyb.cc | 5 +- third_party/jpeg-xl/lib/jxl/dec_xyb.h | 2 + third_party/jpeg-xl/lib/jxl/decode_test.cc | 4 +- third_party/jpeg-xl/lib/jxl/enc_ac_strategy.cc | 6 +- third_party/jpeg-xl/lib/jxl/enc_ac_strategy.h | 3 + .../jpeg-xl/lib/jxl/enc_adaptive_quantization.cc | 3 +- .../jpeg-xl/lib/jxl/enc_adaptive_quantization.h | 3 +- .../jpeg-xl/lib/jxl/enc_ar_control_field.cc | 6 +- third_party/jpeg-xl/lib/jxl/enc_ar_control_field.h | 5 +- third_party/jpeg-xl/lib/jxl/enc_cache.cc | 6 +- third_party/jpeg-xl/lib/jxl/enc_cache.h | 5 +- .../jpeg-xl/lib/jxl/enc_chroma_from_luma.cc | 6 +- third_party/jpeg-xl/lib/jxl/enc_chroma_from_luma.h | 2 + third_party/jpeg-xl/lib/jxl/enc_cluster.h | 8 +- third_party/jpeg-xl/lib/jxl/enc_coeff_order.cc | 4 +- third_party/jpeg-xl/lib/jxl/enc_coeff_order.h | 10 +- third_party/jpeg-xl/lib/jxl/enc_comparator.h | 2 + third_party/jpeg-xl/lib/jxl/enc_debug_image.cc | 5 +- third_party/jpeg-xl/lib/jxl/enc_debug_image.h | 4 +- third_party/jpeg-xl/lib/jxl/enc_detect_dots.cc | 4 +- third_party/jpeg-xl/lib/jxl/enc_detect_dots.h | 7 +- third_party/jpeg-xl/lib/jxl/enc_dot_dictionary.cc | 6 +- third_party/jpeg-xl/lib/jxl/enc_dot_dictionary.h | 4 +- third_party/jpeg-xl/lib/jxl/enc_entropy_coder.cc | 12 +- third_party/jpeg-xl/lib/jxl/enc_entropy_coder.h | 11 +- third_party/jpeg-xl/lib/jxl/enc_fast_lossless.cc | 3 +- third_party/jpeg-xl/lib/jxl/enc_fields.cc | 2 +- third_party/jpeg-xl/lib/jxl/enc_frame.cc | 22 ++- third_party/jpeg-xl/lib/jxl/enc_gaborish.cc | 5 +- third_party/jpeg-xl/lib/jxl/enc_gaborish.h | 1 + third_party/jpeg-xl/lib/jxl/enc_gaborish_test.cc | 1 + third_party/jpeg-xl/lib/jxl/enc_group.cc | 2 +- third_party/jpeg-xl/lib/jxl/enc_group.h | 3 +- third_party/jpeg-xl/lib/jxl/enc_heuristics.cc | 5 +- third_party/jpeg-xl/lib/jxl/enc_heuristics.h | 2 +- third_party/jpeg-xl/lib/jxl/enc_image_bundle.cc | 1 + third_party/jpeg-xl/lib/jxl/enc_image_bundle.h | 1 + third_party/jpeg-xl/lib/jxl/enc_linalg_test.cc | 2 + third_party/jpeg-xl/lib/jxl/enc_modular.cc | 9 +- third_party/jpeg-xl/lib/jxl/enc_modular.h | 1 + third_party/jpeg-xl/lib/jxl/enc_optimize_test.cc | 4 + .../jpeg-xl/lib/jxl/enc_patch_dictionary.cc | 5 +- third_party/jpeg-xl/lib/jxl/enc_patch_dictionary.h | 8 +- third_party/jpeg-xl/lib/jxl/enc_photon_noise.h | 4 +- .../jpeg-xl/lib/jxl/enc_photon_noise_test.cc | 1 - third_party/jpeg-xl/lib/jxl/enc_quant_weights.h | 2 + third_party/jpeg-xl/lib/jxl/enc_xyb.cc | 1 + third_party/jpeg-xl/lib/jxl/encode_internal.h | 2 +- third_party/jpeg-xl/lib/jxl/encode_test.cc | 6 +- third_party/jpeg-xl/lib/jxl/epf.cc | 18 +- third_party/jpeg-xl/lib/jxl/epf.h | 2 +- .../lib/jxl/fake_parallel_runner_testonly.h | 2 +- third_party/jpeg-xl/lib/jxl/fields.cc | 2 +- third_party/jpeg-xl/lib/jxl/fields.h | 3 - third_party/jpeg-xl/lib/jxl/fields_test.cc | 11 +- third_party/jpeg-xl/lib/jxl/frame_dimensions.h | 2 +- third_party/jpeg-xl/lib/jxl/frame_header.h | 10 +- third_party/jpeg-xl/lib/jxl/icc_codec.h | 2 +- third_party/jpeg-xl/lib/jxl/image.h | 184 +------------------ third_party/jpeg-xl/lib/jxl/image_bundle.h | 5 +- third_party/jpeg-xl/lib/jxl/image_metadata.h | 1 + third_party/jpeg-xl/lib/jxl/image_ops.h | 1 + third_party/jpeg-xl/lib/jxl/image_ops_test.cc | 6 +- third_party/jpeg-xl/lib/jxl/image_test_utils.h | 8 +- third_party/jpeg-xl/lib/jxl/jpeg/enc_jpeg_data.cc | 2 +- third_party/jpeg-xl/lib/jxl/jxl_test.cc | 78 +++++++-- third_party/jpeg-xl/lib/jxl/lehmer_code_test.cc | 7 +- .../lib/jxl/modular/encoding/context_predict.h | 10 +- .../lib/jxl/modular/encoding/enc_debug_tree.cc | 3 +- third_party/jpeg-xl/lib/jxl/modular/options.h | 4 +- .../lib/jxl/modular/transform/enc_palette.cc | 1 + .../jpeg-xl/lib/jxl/modular/transform/palette.cc | 4 + .../jpeg-xl/lib/jxl/modular/transform/palette.h | 8 +- .../jpeg-xl/lib/jxl/modular/transform/squeeze.h | 4 +- .../jpeg-xl/lib/jxl/modular/transform/transform.cc | 2 +- .../jpeg-xl/lib/jxl/modular/transform/transform.h | 6 +- third_party/jpeg-xl/lib/jxl/opsin_image_test.cc | 2 + third_party/jpeg-xl/lib/jxl/opsin_inverse_test.cc | 2 + third_party/jpeg-xl/lib/jxl/passes_test.cc | 5 +- third_party/jpeg-xl/lib/jxl/preview_test.cc | 2 + third_party/jpeg-xl/lib/jxl/quant_weights.h | 4 +- third_party/jpeg-xl/lib/jxl/quant_weights_test.cc | 7 + third_party/jpeg-xl/lib/jxl/quantizer.cc | 4 +- third_party/jpeg-xl/lib/jxl/quantizer.h | 14 +- third_party/jpeg-xl/lib/jxl/quantizer_test.cc | 8 + .../render_pipeline/low_memory_render_pipeline.cc | 7 + .../render_pipeline/low_memory_render_pipeline.h | 9 +- .../lib/jxl/render_pipeline/render_pipeline.cc | 4 + .../lib/jxl/render_pipeline/render_pipeline.h | 12 +- .../jxl/render_pipeline/render_pipeline_stage.h | 4 +- .../jxl/render_pipeline/render_pipeline_test.cc | 2 +- .../jxl/render_pipeline/simple_render_pipeline.cc | 6 + .../jxl/render_pipeline/simple_render_pipeline.h | 7 +- .../jpeg-xl/lib/jxl/render_pipeline/stage_noise.h | 11 +- .../lib/jxl/render_pipeline/stage_splines.cc | 8 + .../jpeg-xl/lib/jxl/render_pipeline/stage_write.h | 6 +- .../render_pipeline/test_render_pipeline_stages.h | 6 +- third_party/jpeg-xl/lib/jxl/roundtrip_test.cc | 3 +- third_party/jpeg-xl/lib/jxl/sanitizers.h | 8 +- third_party/jpeg-xl/lib/jxl/speed_tier_test.cc | 2 +- third_party/jpeg-xl/lib/jxl/splines.cc | 3 +- third_party/jpeg-xl/lib/jxl/splines.h | 1 + third_party/jpeg-xl/lib/jxl/splines_gbench.cc | 5 +- third_party/jpeg-xl/lib/jxl/splines_test.cc | 1 + third_party/jpeg-xl/lib/jxl/test_image.cc | 3 + third_party/jpeg-xl/lib/jxl/test_image.h | 3 +- third_party/jpeg-xl/lib/jxl/test_utils.h | 12 ++ third_party/jpeg-xl/lib/jxl/xorshift128plus-inl.h | 4 +- 157 files changed, 762 insertions(+), 489 deletions(-) create mode 100644 third_party/jpeg-xl/lib/jxl/base/include_jpeglib.h create mode 100644 third_party/jpeg-xl/lib/jxl/base/rect.h (limited to 'third_party/jpeg-xl/lib/jxl') diff --git a/third_party/jpeg-xl/lib/jxl/ac_context.h b/third_party/jpeg-xl/lib/jxl/ac_context.h index a2b9e046d1..6529a9bb88 100644 --- a/third_party/jpeg-xl/lib/jxl/ac_context.h +++ b/third_party/jpeg-xl/lib/jxl/ac_context.h @@ -62,7 +62,8 @@ static JXL_INLINE size_t ZeroDensityContext(size_t nonzeros_left, size_t k, size_t covered_blocks, size_t log2_covered_blocks, size_t prev) { - JXL_DASSERT((1u << log2_covered_blocks) == covered_blocks); + JXL_DASSERT((static_cast(1) << log2_covered_blocks) == + covered_blocks); nonzeros_left = (nonzeros_left + covered_blocks - 1) >> log2_covered_blocks; k >>= log2_covered_blocks; JXL_DASSERT(k > 0); @@ -109,7 +110,8 @@ struct BlockCtxMap { // Non-zero context is based on number of non-zeros and block context. // For better clustering, contexts with same number of non-zeros are grouped. constexpr uint32_t ZeroDensityContextsOffset(uint32_t block_ctx) const { - return num_ctxs * kNonZeroBuckets + kZeroDensityContextCount * block_ctx; + return static_cast(num_ctxs * kNonZeroBuckets + + kZeroDensityContextCount * block_ctx); } // Context map for AC coefficients consists of 2 blocks: @@ -121,7 +123,8 @@ struct BlockCtxMap { // number of non-zeros left and // index in scan order constexpr uint32_t NumACContexts() const { - return num_ctxs * (kNonZeroBuckets + kZeroDensityContextCount); + return static_cast(num_ctxs * + (kNonZeroBuckets + kZeroDensityContextCount)); } // Non-zero context is based on number of non-zeros and block context. @@ -134,7 +137,7 @@ struct BlockCtxMap { } else { ctx = 4 + non_zeros / 2; } - return ctx * num_ctxs + block_ctx; + return static_cast(ctx * num_ctxs + block_ctx); } BlockCtxMap() { diff --git a/third_party/jpeg-xl/lib/jxl/ac_strategy.h b/third_party/jpeg-xl/lib/jxl/ac_strategy.h index 9e5917ff1b..fd40b0ced8 100644 --- a/third_party/jpeg-xl/lib/jxl/ac_strategy.h +++ b/third_party/jpeg-xl/lib/jxl/ac_strategy.h @@ -11,9 +11,12 @@ #include // kMaxVectorSize +#include "lib/jxl/base/compiler_specific.h" +#include "lib/jxl/base/rect.h" #include "lib/jxl/base/status.h" #include "lib/jxl/coeff_order_fwd.h" #include "lib/jxl/frame_dimensions.h" +#include "lib/jxl/image.h" #include "lib/jxl/image_ops.h" // Defines the different kinds of transforms, and heuristics to choose between diff --git a/third_party/jpeg-xl/lib/jxl/ac_strategy_test.cc b/third_party/jpeg-xl/lib/jxl/ac_strategy_test.cc index b1d9103466..dc25c89898 100644 --- a/third_party/jpeg-xl/lib/jxl/ac_strategy_test.cc +++ b/third_party/jpeg-xl/lib/jxl/ac_strategy_test.cc @@ -5,16 +5,14 @@ #include "lib/jxl/ac_strategy.h" -#include - -#include +#include +#include #include #include // HWY_ALIGN_MAX #include -#include #include "lib/jxl/base/random.h" -#include "lib/jxl/dct_scales.h" +#include "lib/jxl/coeff_order_fwd.h" #include "lib/jxl/dec_transforms_testonly.h" #include "lib/jxl/enc_transforms.h" #include "lib/jxl/simd_util.h" diff --git a/third_party/jpeg-xl/lib/jxl/ans_common.h b/third_party/jpeg-xl/lib/jxl/ans_common.h index 44b8e3fba1..8236bb20ec 100644 --- a/third_party/jpeg-xl/lib/jxl/ans_common.h +++ b/third_party/jpeg-xl/lib/jxl/ans_common.h @@ -6,23 +6,24 @@ #ifndef LIB_JXL_ANS_COMMON_H_ #define LIB_JXL_ANS_COMMON_H_ -#include - #include +#include +#include +#include +#include #include // Prefetch #include #include "lib/jxl/ans_params.h" #include "lib/jxl/base/byte_order.h" #include "lib/jxl/base/compiler_specific.h" -#include "lib/jxl/base/status.h" namespace jxl { // Returns the precision (number of bits) that should be used to store // a histogram count such that Log2Floor(count) == logcount. -static JXL_INLINE uint32_t GetPopulationCountPrecision(uint32_t logcount, - uint32_t shift) { +static JXL_MAYBE_UNUSED JXL_INLINE uint32_t +GetPopulationCountPrecision(uint32_t logcount, uint32_t shift) { int32_t r = std::min( logcount, static_cast(shift) - static_cast((ANS_LOG_TAB_SIZE - logcount) >> 1)); diff --git a/third_party/jpeg-xl/lib/jxl/ans_test.cc b/third_party/jpeg-xl/lib/jxl/ans_test.cc index 5d6a5ef090..83a2e732f8 100644 --- a/third_party/jpeg-xl/lib/jxl/ans_test.cc +++ b/third_party/jpeg-xl/lib/jxl/ans_test.cc @@ -10,11 +10,10 @@ #include "lib/jxl/ans_params.h" #include "lib/jxl/base/random.h" -#include "lib/jxl/base/span.h" +#include "lib/jxl/base/status.h" #include "lib/jxl/dec_ans.h" #include "lib/jxl/dec_bit_reader.h" #include "lib/jxl/enc_ans.h" -#include "lib/jxl/enc_aux_out.h" #include "lib/jxl/enc_bit_writer.h" #include "lib/jxl/testing.h" diff --git a/third_party/jpeg-xl/lib/jxl/base/compiler_specific.h b/third_party/jpeg-xl/lib/jxl/base/compiler_specific.h index 702ff8e058..52f88c50f8 100644 --- a/third_party/jpeg-xl/lib/jxl/base/compiler_specific.h +++ b/third_party/jpeg-xl/lib/jxl/base/compiler_specific.h @@ -8,7 +8,6 @@ // Macros for compiler version + nonstandard keywords, e.g. __builtin_expect. -#include #include #include "lib/jxl/base/sanitizer_definitions.h" @@ -97,6 +96,11 @@ #define JXL_UNLIKELY(expr) __builtin_expect(!!(expr), 0) #endif +#if JXL_COMPILER_MSVC +#include +using ssize_t = intptr_t; +#endif + // Returns a void* pointer which the compiler then assumes is N-byte aligned. // Example: float* JXL_RESTRICT aligned = (float*)JXL_ASSUME_ALIGNED(in, 32); // @@ -150,8 +154,4 @@ #define JXL_FORMAT(idx_fmt, idx_arg) #endif -#if JXL_COMPILER_MSVC -using ssize_t = intptr_t; -#endif - #endif // LIB_JXL_BASE_COMPILER_SPECIFIC_H_ diff --git a/third_party/jpeg-xl/lib/jxl/base/exif.h b/third_party/jpeg-xl/lib/jxl/base/exif.h index a3574a16ff..acaa1a1ce4 100644 --- a/third_party/jpeg-xl/lib/jxl/base/exif.h +++ b/third_party/jpeg-xl/lib/jxl/base/exif.h @@ -3,8 +3,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -#ifndef LIB_JXL_EXIF_H_ -#define LIB_JXL_EXIF_H_ +#ifndef LIB_JXL_BASE_EXIF_H_ +#define LIB_JXL_BASE_EXIF_H_ // Basic parsing of Exif (just enough for the render-impacting things // like orientation) @@ -87,4 +87,4 @@ JXL_INLINE void InterpretExif(const std::vector& exif, } // namespace jxl -#endif // LIB_JXL_EXIF_H_ +#endif // LIB_JXL_BASE_EXIF_H_ diff --git a/third_party/jpeg-xl/lib/jxl/base/include_jpeglib.h b/third_party/jpeg-xl/lib/jxl/base/include_jpeglib.h new file mode 100644 index 0000000000..f72d13d04b --- /dev/null +++ b/third_party/jpeg-xl/lib/jxl/base/include_jpeglib.h @@ -0,0 +1,20 @@ +// 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_INCLUDE_JPEGLIB_H_ +#define LIB_JXL_BASE_INCLUDE_JPEGLIB_H_ + +// Using this header ensures that includes go in the right order, +// not alphabetically sorted. + +// NOLINTBEGIN +/* clang-format off */ +#include // IWYU pragma: keep +#include // IWYU pragma: keep +#include // IWYU pragma: keep +/* clang-format on */ +// NOLINTEND + +#endif // LIB_JXL_BASE_INCLUDE_JPEGLIB_H_ diff --git a/third_party/jpeg-xl/lib/jxl/base/matrix_ops.h b/third_party/jpeg-xl/lib/jxl/base/matrix_ops.h index cde6a64b1e..e1f8753932 100644 --- a/third_party/jpeg-xl/lib/jxl/base/matrix_ops.h +++ b/third_party/jpeg-xl/lib/jxl/base/matrix_ops.h @@ -3,8 +3,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -#ifndef LIB_JXL_MATRIX_OPS_H_ -#define LIB_JXL_MATRIX_OPS_H_ +#ifndef LIB_JXL_BASE_MATRIX_OPS_H_ +#define LIB_JXL_BASE_MATRIX_OPS_H_ // 3x3 matrix operations. @@ -83,4 +83,4 @@ Status Inv3x3Matrix(Matrix& matrix) { } // namespace jxl -#endif // LIB_JXL_MATRIX_OPS_H_ +#endif // LIB_JXL_BASE_MATRIX_OPS_H_ diff --git a/third_party/jpeg-xl/lib/jxl/base/rect.h b/third_party/jpeg-xl/lib/jxl/base/rect.h new file mode 100644 index 0000000000..666c3d73ec --- /dev/null +++ b/third_party/jpeg-xl/lib/jxl/base/rect.h @@ -0,0 +1,194 @@ +// 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