Coverage Report

Created: 2025-11-14 07:32

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/image.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_IMAGE_H_
7
#define LIB_JXL_IMAGE_H_
8
9
// SIMD/multicore-friendly planar image representation with row accessors.
10
11
#if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
12
    defined(THREAD_SANITIZER)
13
#include <cinttypes>  // PRIu64
14
#endif
15
16
#include <jxl/memory_manager.h>
17
18
#include <algorithm>
19
#include <cstddef>
20
#include <cstdint>
21
#include <cstring>
22
#include <utility>  // std::move
23
24
#include "lib/jxl/base/compiler_specific.h"
25
#include "lib/jxl/base/status.h"
26
#include "lib/jxl/memory_manager_internal.h"
27
28
namespace jxl {
29
30
// DO NOT use PlaneBase outside of image.{h|cc}
31
namespace detail {
32
33
// Type-independent parts of Plane<> - reduces code duplication and facilitates
34
// moving member function implementations to cc file.
35
struct PlaneBase {
36
  PlaneBase()
37
6.58M
      : xsize_(0),
38
6.58M
        ysize_(0),
39
6.58M
        orig_xsize_(0),
40
6.58M
        orig_ysize_(0),
41
6.58M
        bytes_per_row_(0),
42
6.58M
        sizeof_t_(0) {}
43
44
  // Copy construction/assignment is forbidden to avoid inadvertent copies,
45
  // which can be very expensive. Use CopyImageTo() instead.
46
  PlaneBase(const PlaneBase& other) = delete;
47
  PlaneBase& operator=(const PlaneBase& other) = delete;
48
49
  // Move constructor (required for returning Image from function)
50
71.1M
  PlaneBase(PlaneBase&& other) noexcept = default;
51
52
  // Move assignment (required for std::vector)
53
12.6M
  PlaneBase& operator=(PlaneBase&& other) noexcept = default;
54
55
111M
  ~PlaneBase() = default;
56
57
  void Swap(PlaneBase& other);
58
59
  // Useful for pre-allocating image with some padding for alignment purposes
60
  // and later reporting the actual valid dimensions. May also be used to
61
  // un-shrink the image. Caller is responsible for ensuring xsize/ysize are <=
62
  // the original dimensions.
63
115k
  Status ShrinkTo(const size_t xsize, const size_t ysize) {
64
115k
    JXL_ENSURE(xsize <= orig_xsize_);
65
115k
    JXL_ENSURE(ysize <= orig_ysize_);
66
115k
    xsize_ = static_cast<uint32_t>(xsize);
67
115k
    ysize_ = static_cast<uint32_t>(ysize);
68
    // NOTE: we can't recompute bytes_per_row for more compact storage and
69
    // better locality because that would invalidate the image contents.
70
115k
    return true;
71
115k
  }
72
73
  // How many pixels.
74
490M
  JXL_INLINE size_t xsize() const { return xsize_; }
75
417M
  JXL_INLINE size_t ysize() const { return ysize_; }
76
77
  // NOTE: do not use this for copying rows - the valid xsize may be much less.
78
854M
  JXL_INLINE size_t bytes_per_row() const { return bytes_per_row_; }
79
80
353k
  JXL_INLINE JxlMemoryManager* memory_manager() const {
81
353k
    return bytes_.memory_manager();
82
353k
  }
83
84
  // Raw access to byte contents, for interfacing with other libraries.
85
  // Unsigned char instead of char to avoid surprises (sign extension).
86
47.1M
  JXL_INLINE uint8_t* bytes() {
87
47.1M
    uint8_t* p = bytes_.address<uint8_t>();
88
47.1M
    return static_cast<uint8_t * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(p, 64));
89
47.1M
  }
90
807M
  JXL_INLINE const uint8_t* bytes() const {
91
807M
    const uint8_t* p = bytes_.address<uint8_t>();
92
807M
    return static_cast<const uint8_t * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(p, 64));
93
807M
  }
94
95
 protected:
96
  PlaneBase(uint32_t xsize, uint32_t ysize, size_t sizeof_t);
97
  Status Allocate(JxlMemoryManager* memory_manager, size_t pre_padding);
98
99
  // Returns pointer to the start of a row.
100
4.55G
  JXL_INLINE void* VoidRow(const size_t y) const {
101
4.55G
    JXL_DASSERT(y < ysize_);
102
4.55G
    uint8_t* row = bytes_.address<uint8_t>() + y * bytes_per_row_;
103
4.55G
    return JXL_ASSUME_ALIGNED(row, 64);
104
4.55G
  }
105
106
  // (Members are non-const to enable assignment during move-assignment.)
107
  uint32_t xsize_;  // In valid pixels, not including any padding.
108
  uint32_t ysize_;
109
  uint32_t orig_xsize_;
110
  uint32_t orig_ysize_;
111
  size_t bytes_per_row_;  // Includes padding.
112
  AlignedMemory bytes_;
113
  size_t sizeof_t_;
114
};
115
116
}  // namespace detail
117
118
// Single channel, aligned rows separated by padding. T must be POD.
119
//
120
// 'Single channel' (one 2D array per channel) simplifies vectorization
121
// (repeating the same operation on multiple adjacent components) without the
122
// complexity of a hybrid layout (8 R, 8 G, 8 B, ...). In particular, clients
123
// can easily iterate over all components in a row and Image requires no
124
// knowledge of the pixel format beyond the component type "T".
125
//
126
// 'Aligned' means each row is aligned to the L1 cache line size. This prevents
127
// false sharing between two threads operating on adjacent rows.
128
//
129
// 'Padding' is still relevant because vectors could potentially be larger than
130
// a cache line. By rounding up row sizes to the vector size, we allow
131
// reading/writing ALIGNED vectors whose first lane is a valid sample. This
132
// avoids needing a separate loop to handle remaining unaligned lanes.
133
//
134
// This image layout could also be achieved with a vector and a row accessor
135
// function, but a class wrapper with support for "deleter" allows wrapping
136
// existing memory allocated by clients without copying the pixels. It also
137
// provides convenient accessors for xsize/ysize, which shortens function
138
// argument lists. Supports move-construction so it can be stored in containers.
139
template <typename ComponentType>
140
class Plane : public detail::PlaneBase {
141
 public:
142
  using T = ComponentType;
143
  static constexpr size_t kNumPlanes = 1;
144
145
6.58M
  Plane() = default;
jxl::Plane<int>::Plane()
Line
Count
Source
145
1.64M
  Plane() = default;
jxl::Plane<unsigned char>::Plane()
Line
Count
Source
145
124k
  Plane() = default;
jxl::Plane<signed char>::Plane()
Line
Count
Source
145
138k
  Plane() = default;
jxl::Plane<float>::Plane()
Line
Count
Source
145
4.62M
  Plane() = default;
Unexecuted instantiation: jxl::Plane<hwy::float16_t>::Plane()
Unexecuted instantiation: jxl::Plane<unsigned int>::Plane()
jxl::Plane<short>::Plane()
Line
Count
Source
145
51.1k
  Plane() = default;
146
147
  static StatusOr<Plane> Create(JxlMemoryManager* memory_manager,
148
                                const size_t xsize, const size_t ysize,
149
33.6M
                                const size_t pre_padding = 0) {
150
33.6M
    static_assert(
151
33.6M
        sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8,
152
33.6M
        "Only 1/2/4/8-byte samples are supported");
153
33.6M
    uint32_t xsize32 = static_cast<uint32_t>(xsize);
154
33.6M
    uint32_t ysize32 = static_cast<uint32_t>(ysize);
155
33.6M
    JXL_ENSURE(xsize32 == xsize);
156
33.6M
    JXL_ENSURE(ysize32 == ysize);
157
33.6M
    Plane plane(xsize32, ysize32, sizeof(T));
158
33.6M
    JXL_RETURN_IF_ERROR(plane.Allocate(memory_manager, pre_padding));
159
33.6M
    return plane;
160
33.6M
  }
jxl::Plane<float>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long, unsigned long)
Line
Count
Source
149
32.2M
                                const size_t pre_padding = 0) {
150
32.2M
    static_assert(
151
32.2M
        sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8,
152
32.2M
        "Only 1/2/4/8-byte samples are supported");
153
32.2M
    uint32_t xsize32 = static_cast<uint32_t>(xsize);
154
32.2M
    uint32_t ysize32 = static_cast<uint32_t>(ysize);
155
32.2M
    JXL_ENSURE(xsize32 == xsize);
156
32.2M
    JXL_ENSURE(ysize32 == ysize);
157
32.2M
    Plane plane(xsize32, ysize32, sizeof(T));
158
32.2M
    JXL_RETURN_IF_ERROR(plane.Allocate(memory_manager, pre_padding));
159
32.2M
    return plane;
160
32.2M
  }
jxl::Plane<int>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long, unsigned long)
Line
Count
Source
149
1.18M
                                const size_t pre_padding = 0) {
150
1.18M
    static_assert(
151
1.18M
        sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8,
152
1.18M
        "Only 1/2/4/8-byte samples are supported");
153
1.18M
    uint32_t xsize32 = static_cast<uint32_t>(xsize);
154
1.18M
    uint32_t ysize32 = static_cast<uint32_t>(ysize);
155
1.18M
    JXL_ENSURE(xsize32 == xsize);
156
1.18M
    JXL_ENSURE(ysize32 == ysize);
157
1.18M
    Plane plane(xsize32, ysize32, sizeof(T));
158
1.18M
    JXL_RETURN_IF_ERROR(plane.Allocate(memory_manager, pre_padding));
159
1.18M
    return plane;
160
1.18M
  }
jxl::Plane<unsigned char>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long, unsigned long)
Line
Count
Source
149
133k
                                const size_t pre_padding = 0) {
150
133k
    static_assert(
151
133k
        sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8,
152
133k
        "Only 1/2/4/8-byte samples are supported");
153
133k
    uint32_t xsize32 = static_cast<uint32_t>(xsize);
154
133k
    uint32_t ysize32 = static_cast<uint32_t>(ysize);
155
133k
    JXL_ENSURE(xsize32 == xsize);
156
133k
    JXL_ENSURE(ysize32 == ysize);
157
133k
    Plane plane(xsize32, ysize32, sizeof(T));
158
133k
    JXL_RETURN_IF_ERROR(plane.Allocate(memory_manager, pre_padding));
159
133k
    return plane;
160
133k
  }
jxl::Plane<signed char>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long, unsigned long)
Line
Count
Source
149
84.0k
                                const size_t pre_padding = 0) {
150
84.0k
    static_assert(
151
84.0k
        sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8,
152
84.0k
        "Only 1/2/4/8-byte samples are supported");
153
84.0k
    uint32_t xsize32 = static_cast<uint32_t>(xsize);
154
84.0k
    uint32_t ysize32 = static_cast<uint32_t>(ysize);
155
84.0k
    JXL_ENSURE(xsize32 == xsize);
156
84.0k
    JXL_ENSURE(ysize32 == ysize);
157
84.0k
    Plane plane(xsize32, ysize32, sizeof(T));
158
84.0k
    JXL_RETURN_IF_ERROR(plane.Allocate(memory_manager, pre_padding));
159
84.0k
    return plane;
160
84.0k
  }
Unexecuted instantiation: jxl::Plane<hwy::float16_t>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long, unsigned long)
Unexecuted instantiation: jxl::Plane<unsigned int>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long, unsigned long)
jxl::Plane<short>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long, unsigned long)
Line
Count
Source
149
12.7k
                                const size_t pre_padding = 0) {
150
12.7k
    static_assert(
151
12.7k
        sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 || sizeof(T) == 8,
152
12.7k
        "Only 1/2/4/8-byte samples are supported");
153
12.7k
    uint32_t xsize32 = static_cast<uint32_t>(xsize);
154
12.7k
    uint32_t ysize32 = static_cast<uint32_t>(ysize);
155
12.7k
    JXL_ENSURE(xsize32 == xsize);
156
12.7k
    JXL_ENSURE(ysize32 == ysize);
157
12.7k
    Plane plane(xsize32, ysize32, sizeof(T));
158
12.7k
    JXL_RETURN_IF_ERROR(plane.Allocate(memory_manager, pre_padding));
159
12.7k
    return plane;
160
12.7k
  }
161
162
353M
  JXL_INLINE T* Row(const size_t y) { return static_cast<T*>(VoidRow(y)); }
jxl::Plane<unsigned char>::Row(unsigned long)
Line
Count
Source
162
9.03M
  JXL_INLINE T* Row(const size_t y) { return static_cast<T*>(VoidRow(y)); }
jxl::Plane<float>::Row(unsigned long)
Line
Count
Source
162
290M
  JXL_INLINE T* Row(const size_t y) { return static_cast<T*>(VoidRow(y)); }
jxl::Plane<int>::Row(unsigned long)
Line
Count
Source
162
53.2M
  JXL_INLINE T* Row(const size_t y) { return static_cast<T*>(VoidRow(y)); }
jxl::Plane<signed char>::Row(unsigned long)
Line
Count
Source
162
579k
  JXL_INLINE T* Row(const size_t y) { return static_cast<T*>(VoidRow(y)); }
Unexecuted instantiation: jxl::Plane<hwy::float16_t>::Row(unsigned long)
Unexecuted instantiation: jxl::Plane<unsigned int>::Row(unsigned long)
Unexecuted instantiation: jxl::Plane<short>::Row(unsigned long)
163
164
  // Returns pointer to const (see above).
165
2.80G
  JXL_INLINE const T* Row(const size_t y) const {
166
2.80G
    return static_cast<const T*>(VoidRow(y));
167
2.80G
  }
jxl::Plane<float>::Row(unsigned long) const
Line
Count
Source
165
2.67G
  JXL_INLINE const T* Row(const size_t y) const {
166
2.67G
    return static_cast<const T*>(VoidRow(y));
167
2.67G
  }
jxl::Plane<int>::Row(unsigned long) const
Line
Count
Source
165
125M
  JXL_INLINE const T* Row(const size_t y) const {
166
125M
    return static_cast<const T*>(VoidRow(y));
167
125M
  }
Unexecuted instantiation: jxl::Plane<unsigned char>::Row(unsigned long) const
168
169
  // Documents that the access is const.
170
1.40G
  JXL_INLINE const T* ConstRow(const size_t y) const {
171
1.40G
    return static_cast<const T*>(VoidRow(y));
172
1.40G
  }
jxl::Plane<unsigned char>::ConstRow(unsigned long) const
Line
Count
Source
170
42.6M
  JXL_INLINE const T* ConstRow(const size_t y) const {
171
42.6M
    return static_cast<const T*>(VoidRow(y));
172
42.6M
  }
jxl::Plane<float>::ConstRow(unsigned long) const
Line
Count
Source
170
1.35G
  JXL_INLINE const T* ConstRow(const size_t y) const {
171
1.35G
    return static_cast<const T*>(VoidRow(y));
172
1.35G
  }
jxl::Plane<signed char>::ConstRow(unsigned long) const
Line
Count
Source
170
1.71M
  JXL_INLINE const T* ConstRow(const size_t y) const {
171
1.71M
    return static_cast<const T*>(VoidRow(y));
172
1.71M
  }
jxl::Plane<int>::ConstRow(unsigned long) const
Line
Count
Source
170
2.52M
  JXL_INLINE const T* ConstRow(const size_t y) const {
171
2.52M
    return static_cast<const T*>(VoidRow(y));
172
2.52M
  }
173
174
  // Returns number of pixels (some of which are padding) per row. Useful for
175
  // computing other rows via pointer arithmetic. WARNING: this must
176
  // NOT be used to determine xsize.
177
6.50M
  JXL_INLINE ptrdiff_t PixelsPerRow() const {
178
6.50M
    return static_cast<ptrdiff_t>(bytes_per_row_ / sizeof(T));
179
6.50M
  }
jxl::Plane<unsigned char>::PixelsPerRow() const
Line
Count
Source
177
72.6k
  JXL_INLINE ptrdiff_t PixelsPerRow() const {
178
72.6k
    return static_cast<ptrdiff_t>(bytes_per_row_ / sizeof(T));
179
72.6k
  }
jxl::Plane<int>::PixelsPerRow() const
Line
Count
Source
177
4.34M
  JXL_INLINE ptrdiff_t PixelsPerRow() const {
178
4.34M
    return static_cast<ptrdiff_t>(bytes_per_row_ / sizeof(T));
179
4.34M
  }
jxl::Plane<float>::PixelsPerRow() const
Line
Count
Source
177
2.09M
  JXL_INLINE ptrdiff_t PixelsPerRow() const {
178
2.09M
    return static_cast<ptrdiff_t>(bytes_per_row_ / sizeof(T));
179
2.09M
  }
Unexecuted instantiation: jxl::Plane<short>::PixelsPerRow() const
180
181
 private:
182
  Plane(uint32_t xsize, uint32_t ysize, size_t sizeof_t)
183
33.6M
      : detail::PlaneBase(xsize, ysize, sizeof_t) {}
jxl::Plane<int>::Plane(unsigned int, unsigned int, unsigned long)
Line
Count
Source
183
1.18M
      : detail::PlaneBase(xsize, ysize, sizeof_t) {}
jxl::Plane<unsigned char>::Plane(unsigned int, unsigned int, unsigned long)
Line
Count
Source
183
133k
      : detail::PlaneBase(xsize, ysize, sizeof_t) {}
jxl::Plane<float>::Plane(unsigned int, unsigned int, unsigned long)
Line
Count
Source
183
32.2M
      : detail::PlaneBase(xsize, ysize, sizeof_t) {}
jxl::Plane<signed char>::Plane(unsigned int, unsigned int, unsigned long)
Line
Count
Source
183
84.0k
      : detail::PlaneBase(xsize, ysize, sizeof_t) {}
Unexecuted instantiation: jxl::Plane<hwy::float16_t>::Plane(unsigned int, unsigned int, unsigned long)
Unexecuted instantiation: jxl::Plane<unsigned int>::Plane(unsigned int, unsigned int, unsigned long)
jxl::Plane<short>::Plane(unsigned int, unsigned int, unsigned long)
Line
Count
Source
183
12.7k
      : detail::PlaneBase(xsize, ysize, sizeof_t) {}
184
};
185
186
using ImageSB = Plane<int8_t>;
187
using ImageB = Plane<uint8_t>;
188
using ImageS = Plane<int16_t>;  // signed integer or half-float
189
using ImageU = Plane<uint16_t>;
190
using ImageI = Plane<int32_t>;
191
using ImageF = Plane<float>;
192
using ImageD = Plane<double>;
193
194
// Currently, we abuse Image to either refer to an image that owns its storage
195
// or one that doesn't. In similar vein, we abuse Image* function parameters to
196
// either mean "assign to me" or "fill the provided image with data".
197
// Hopefully, the "assign to me" meaning will go away and most images in the
198
// codebase will not be backed by own storage. When this happens we can redesign
199
// Image to be a non-storage-holding view class and introduce BackedImage in
200
// those places that actually need it.
201
202
// NOTE: we can't use Image as a view because invariants are violated
203
// (alignment and the presence of padding before/after each "row").
204
205
// A bundle of 3 same-sized images. Typically constructed by moving from three
206
// rvalue references to Image. To overwrite an existing Image3 using
207
// single-channel producers, we also need access to Image*. Constructing
208
// temporary non-owning Image pointing to one plane of an existing Image3 risks
209
// dangling references, especially if the wrapper is moved. Therefore, we
210
// store an array of Image (which are compact enough that size is not a concern)
211
// and provide Plane+Row accessors.
212
template <typename ComponentType>
213
class Image3 {
214
 public:
215
  using T = ComponentType;
216
  using PlaneT = jxl::Plane<T>;
217
  static constexpr size_t kNumPlanes = 3;
218
219
939k
  Image3() : planes_{PlaneT(), PlaneT(), PlaneT()} {}
jxl::Image3<float>::Image3()
Line
Count
Source
219
432k
  Image3() : planes_{PlaneT(), PlaneT(), PlaneT()} {}
jxl::Image3<int>::Image3()
Line
Count
Source
219
502k
  Image3() : planes_{PlaneT(), PlaneT(), PlaneT()} {}
jxl::Image3<short>::Image3()
Line
Count
Source
219
4.26k
  Image3() : planes_{PlaneT(), PlaneT(), PlaneT()} {}
220
221
  // Copy construction/assignment is forbidden to avoid inadvertent copies,
222
  // which can be very expensive. Use CopyImageTo instead.
223
  Image3(const Image3& other) = delete;
224
  Image3& operator=(const Image3& other) = delete;
225
226
378k
  Image3(Image3&& other) noexcept {
227
1.51M
    for (size_t i = 0; i < kNumPlanes; i++) {
228
1.13M
      planes_[i] = std::move(other.planes_[i]);
229
1.13M
    }
230
378k
  }
jxl::Image3<float>::Image3(jxl::Image3<float>&&)
Line
Count
Source
226
345k
  Image3(Image3&& other) noexcept {
227
1.38M
    for (size_t i = 0; i < kNumPlanes; i++) {
228
1.03M
      planes_[i] = std::move(other.planes_[i]);
229
1.03M
    }
230
345k
  }
jxl::Image3<int>::Image3(jxl::Image3<int>&&)
Line
Count
Source
226
24.0k
  Image3(Image3&& other) noexcept {
227
96.3k
    for (size_t i = 0; i < kNumPlanes; i++) {
228
72.2k
      planes_[i] = std::move(other.planes_[i]);
229
72.2k
    }
230
24.0k
  }
Unexecuted instantiation: jxl::Image3<unsigned char>::Image3(jxl::Image3<unsigned char>&&)
jxl::Image3<short>::Image3(jxl::Image3<short>&&)
Line
Count
Source
226
8.52k
  Image3(Image3&& other) noexcept {
227
34.0k
    for (size_t i = 0; i < kNumPlanes; i++) {
228
25.5k
      planes_[i] = std::move(other.planes_[i]);
229
25.5k
    }
230
8.52k
  }
231
260k
  Image3& operator=(Image3&& other) noexcept {
232
1.04M
    for (size_t i = 0; i < kNumPlanes; i++) {
233
781k
      planes_[i] = std::move(other.planes_[i]);
234
781k
    }
235
260k
    return *this;
236
260k
  }
jxl::Image3<float>::operator=(jxl::Image3<float>&&)
Line
Count
Source
231
244k
  Image3& operator=(Image3&& other) noexcept {
232
976k
    for (size_t i = 0; i < kNumPlanes; i++) {
233
732k
      planes_[i] = std::move(other.planes_[i]);
234
732k
    }
235
244k
    return *this;
236
244k
  }
jxl::Image3<int>::operator=(jxl::Image3<int>&&)
Line
Count
Source
231
12.0k
  Image3& operator=(Image3&& other) noexcept {
232
48.1k
    for (size_t i = 0; i < kNumPlanes; i++) {
233
36.1k
      planes_[i] = std::move(other.planes_[i]);
234
36.1k
    }
235
12.0k
    return *this;
236
12.0k
  }
jxl::Image3<short>::operator=(jxl::Image3<short>&&)
Line
Count
Source
231
4.26k
  Image3& operator=(Image3&& other) noexcept {
232
17.0k
    for (size_t i = 0; i < kNumPlanes; i++) {
233
12.7k
      planes_[i] = std::move(other.planes_[i]);
234
12.7k
    }
235
4.26k
    return *this;
236
4.26k
  }
237
238
  static StatusOr<Image3> Create(JxlMemoryManager* memory_manager,
239
179k
                                 const size_t xsize, const size_t ysize) {
240
179k
    JXL_ASSIGN_OR_RETURN(PlaneT plane0,
241
179k
                         PlaneT::Create(memory_manager, xsize, ysize));
242
179k
    JXL_ASSIGN_OR_RETURN(PlaneT plane1,
243
179k
                         PlaneT::Create(memory_manager, xsize, ysize));
244
179k
    JXL_ASSIGN_OR_RETURN(PlaneT plane2,
245
179k
                         PlaneT::Create(memory_manager, xsize, ysize));
246
179k
    return Image3(std::move(plane0), std::move(plane1), std::move(plane2));
247
179k
  }
jxl::Image3<float>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long)
Line
Count
Source
239
163k
                                 const size_t xsize, const size_t ysize) {
240
163k
    JXL_ASSIGN_OR_RETURN(PlaneT plane0,
241
163k
                         PlaneT::Create(memory_manager, xsize, ysize));
242
163k
    JXL_ASSIGN_OR_RETURN(PlaneT plane1,
243
163k
                         PlaneT::Create(memory_manager, xsize, ysize));
244
163k
    JXL_ASSIGN_OR_RETURN(PlaneT plane2,
245
163k
                         PlaneT::Create(memory_manager, xsize, ysize));
246
163k
    return Image3(std::move(plane0), std::move(plane1), std::move(plane2));
247
163k
  }
jxl::Image3<int>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long)
Line
Count
Source
239
12.0k
                                 const size_t xsize, const size_t ysize) {
240
12.0k
    JXL_ASSIGN_OR_RETURN(PlaneT plane0,
241
12.0k
                         PlaneT::Create(memory_manager, xsize, ysize));
242
12.0k
    JXL_ASSIGN_OR_RETURN(PlaneT plane1,
243
12.0k
                         PlaneT::Create(memory_manager, xsize, ysize));
244
12.0k
    JXL_ASSIGN_OR_RETURN(PlaneT plane2,
245
12.0k
                         PlaneT::Create(memory_manager, xsize, ysize));
246
12.0k
    return Image3(std::move(plane0), std::move(plane1), std::move(plane2));
247
12.0k
  }
Unexecuted instantiation: jxl::Image3<unsigned char>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long)
jxl::Image3<short>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long)
Line
Count
Source
239
4.26k
                                 const size_t xsize, const size_t ysize) {
240
4.26k
    JXL_ASSIGN_OR_RETURN(PlaneT plane0,
241
4.26k
                         PlaneT::Create(memory_manager, xsize, ysize));
242
4.26k
    JXL_ASSIGN_OR_RETURN(PlaneT plane1,
243
4.26k
                         PlaneT::Create(memory_manager, xsize, ysize));
244
4.26k
    JXL_ASSIGN_OR_RETURN(PlaneT plane2,
245
4.26k
                         PlaneT::Create(memory_manager, xsize, ysize));
246
4.26k
    return Image3(std::move(plane0), std::move(plane1), std::move(plane2));
247
4.26k
  }
248
249
  // Returns row pointer; usage: PlaneRow(idx_plane, y)[x] = val.
250
47.1M
  JXL_INLINE T* PlaneRow(const size_t c, const size_t y) {
251
    // Custom implementation instead of calling planes_[c].Row ensures only a
252
    // single multiplication is needed for PlaneRow(0..2, y).
253
47.1M
    PlaneRowBoundsCheck(c, y);
254
47.1M
    const size_t row_offset = y * planes_[0].bytes_per_row();
255
47.1M
    void* row = planes_[c].bytes() + row_offset;
256
47.1M
    return static_cast<T * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(row, 64));
257
47.1M
  }
jxl::Image3<float>::PlaneRow(unsigned long, unsigned long)
Line
Count
Source
250
46.4M
  JXL_INLINE T* PlaneRow(const size_t c, const size_t y) {
251
    // Custom implementation instead of calling planes_[c].Row ensures only a
252
    // single multiplication is needed for PlaneRow(0..2, y).
253
46.4M
    PlaneRowBoundsCheck(c, y);
254
46.4M
    const size_t row_offset = y * planes_[0].bytes_per_row();
255
46.4M
    void* row = planes_[c].bytes() + row_offset;
256
46.4M
    return static_cast<T * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(row, 64));
257
46.4M
  }
jxl::Image3<int>::PlaneRow(unsigned long, unsigned long)
Line
Count
Source
250
749k
  JXL_INLINE T* PlaneRow(const size_t c, const size_t y) {
251
    // Custom implementation instead of calling planes_[c].Row ensures only a
252
    // single multiplication is needed for PlaneRow(0..2, y).
253
749k
    PlaneRowBoundsCheck(c, y);
254
749k
    const size_t row_offset = y * planes_[0].bytes_per_row();
255
749k
    void* row = planes_[c].bytes() + row_offset;
256
749k
    return static_cast<T * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(row, 64));
257
749k
  }
Unexecuted instantiation: jxl::Image3<unsigned char>::PlaneRow(unsigned long, unsigned long)
jxl::Image3<short>::PlaneRow(unsigned long, unsigned long)
Line
Count
Source
250
1.86k
  JXL_INLINE T* PlaneRow(const size_t c, const size_t y) {
251
    // Custom implementation instead of calling planes_[c].Row ensures only a
252
    // single multiplication is needed for PlaneRow(0..2, y).
253
1.86k
    PlaneRowBoundsCheck(c, y);
254
1.86k
    const size_t row_offset = y * planes_[0].bytes_per_row();
255
1.86k
    void* row = planes_[c].bytes() + row_offset;
256
1.86k
    return static_cast<T * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(row, 64));
257
1.86k
  }
258
259
  // Returns const row pointer; usage: val = PlaneRow(idx_plane, y)[x].
260
807M
  JXL_INLINE const T* PlaneRow(const size_t c, const size_t y) const {
261
807M
    PlaneRowBoundsCheck(c, y);
262
807M
    const size_t row_offset = y * planes_[0].bytes_per_row();
263
807M
    const void* row = planes_[c].bytes() + row_offset;
264
807M
    return static_cast<const T * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(row, 64));
265
807M
  }
jxl::Image3<int>::PlaneRow(unsigned long, unsigned long) const
Line
Count
Source
260
612k
  JXL_INLINE const T* PlaneRow(const size_t c, const size_t y) const {
261
612k
    PlaneRowBoundsCheck(c, y);
262
612k
    const size_t row_offset = y * planes_[0].bytes_per_row();
263
612k
    const void* row = planes_[c].bytes() + row_offset;
264
612k
    return static_cast<const T * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(row, 64));
265
612k
  }
jxl::Image3<float>::PlaneRow(unsigned long, unsigned long) const
Line
Count
Source
260
806M
  JXL_INLINE const T* PlaneRow(const size_t c, const size_t y) const {
261
806M
    PlaneRowBoundsCheck(c, y);
262
806M
    const size_t row_offset = y * planes_[0].bytes_per_row();
263
806M
    const void* row = planes_[c].bytes() + row_offset;
264
806M
    return static_cast<const T * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(row, 64));
265
806M
  }
Unexecuted instantiation: jxl::Image3<unsigned char>::PlaneRow(unsigned long, unsigned long) const
Unexecuted instantiation: jxl::Image3<short>::PlaneRow(unsigned long, unsigned long) const
266
267
  // Returns const row pointer, even if called from a non-const Image3.
268
806M
  JXL_INLINE const T* ConstPlaneRow(const size_t c, const size_t y) const {
269
806M
    PlaneRowBoundsCheck(c, y);
270
806M
    return PlaneRow(c, y);
271
806M
  }
jxl::Image3<float>::ConstPlaneRow(unsigned long, unsigned long) const
Line
Count
Source
268
806M
  JXL_INLINE const T* ConstPlaneRow(const size_t c, const size_t y) const {
269
806M
    PlaneRowBoundsCheck(c, y);
270
806M
    return PlaneRow(c, y);
271
806M
  }
Unexecuted instantiation: jxl::Image3<unsigned char>::ConstPlaneRow(unsigned long, unsigned long) const
jxl::Image3<int>::ConstPlaneRow(unsigned long, unsigned long) const
Line
Count
Source
268
590k
  JXL_INLINE const T* ConstPlaneRow(const size_t c, const size_t y) const {
269
590k
    PlaneRowBoundsCheck(c, y);
270
590k
    return PlaneRow(c, y);
271
590k
  }
272
273
442k
  JXL_INLINE const PlaneT& Plane(size_t idx) const { return planes_[idx]; }
274
275
119k
  JXL_INLINE PlaneT& Plane(size_t idx) { return planes_[idx]; }
jxl::Image3<float>::Plane(unsigned long)
Line
Count
Source
275
119k
  JXL_INLINE PlaneT& Plane(size_t idx) { return planes_[idx]; }
Unexecuted instantiation: jxl::Image3<int>::Plane(unsigned long)
Unexecuted instantiation: jxl::Image3<short>::Plane(unsigned long)
276
277
3.22k
  void Swap(Image3& other) {
278
12.9k
    for (size_t c = 0; c < 3; ++c) {
279
9.67k
      other.planes_[c].Swap(planes_[c]);
280
9.67k
    }
281
3.22k
  }
282
283
  // Useful for pre-allocating image with some padding for alignment purposes
284
  // and later reporting the actual valid dimensions. May also be used to
285
  // un-shrink the image. Caller is responsible for ensuring xsize/ysize are <=
286
  // the original dimensions.
287
10.8k
  Status ShrinkTo(const size_t xsize, const size_t ysize) {
288
32.6k
    for (PlaneT& plane : planes_) {
289
32.6k
      JXL_RETURN_IF_ERROR(plane.ShrinkTo(xsize, ysize));
290
32.6k
    }
291
10.8k
    return true;
292
10.8k
  }
293
294
  // Sizes of all three images are guaranteed to be equal.
295
27.6k
  JXL_INLINE JxlMemoryManager* memory_manager() const {
296
27.6k
    return planes_[0].memory_manager();
297
27.6k
  }
jxl::Image3<float>::memory_manager() const
Line
Count
Source
295
27.6k
  JXL_INLINE JxlMemoryManager* memory_manager() const {
296
27.6k
    return planes_[0].memory_manager();
297
27.6k
  }
Unexecuted instantiation: jxl::Image3<unsigned char>::memory_manager() const
298
314M
  JXL_INLINE size_t xsize() const { return planes_[0].xsize(); }
jxl::Image3<float>::xsize() const
Line
Count
Source
298
314M
  JXL_INLINE size_t xsize() const { return planes_[0].xsize(); }
jxl::Image3<int>::xsize() const
Line
Count
Source
298
37.8k
  JXL_INLINE size_t xsize() const { return planes_[0].xsize(); }
Unexecuted instantiation: jxl::Image3<unsigned char>::xsize() const
jxl::Image3<short>::xsize() const
Line
Count
Source
298
4.83k
  JXL_INLINE size_t xsize() const { return planes_[0].xsize(); }
299
189M
  JXL_INLINE size_t ysize() const { return planes_[0].ysize(); }
jxl::Image3<float>::ysize() const
Line
Count
Source
299
189M
  JXL_INLINE size_t ysize() const { return planes_[0].ysize(); }
jxl::Image3<int>::ysize() const
Line
Count
Source
299
109
  JXL_INLINE size_t ysize() const { return planes_[0].ysize(); }
Unexecuted instantiation: jxl::Image3<unsigned char>::ysize() const
jxl::Image3<short>::ysize() const
Line
Count
Source
299
210
  JXL_INLINE size_t ysize() const { return planes_[0].ysize(); }
300
  // Returns offset [bytes] from one row to the next row of the same plane.
301
  // WARNING: this must NOT be used to determine xsize, nor for copying rows -
302
  // the valid xsize may be much less.
303
  JXL_INLINE size_t bytes_per_row() const { return planes_[0].bytes_per_row(); }
304
  // Returns number of pixels (some of which are padding) per row. Useful for
305
  // computing other rows via pointer arithmetic. WARNING: this must NOT be used
306
  // to determine xsize.
307
1.32M
  JXL_INLINE ptrdiff_t PixelsPerRow() const { return planes_[0].PixelsPerRow(); }
jxl::Image3<int>::PixelsPerRow() const
Line
Count
Source
307
18.7k
  JXL_INLINE ptrdiff_t PixelsPerRow() const { return planes_[0].PixelsPerRow(); }
jxl::Image3<float>::PixelsPerRow() const
Line
Count
Source
307
1.30M
  JXL_INLINE ptrdiff_t PixelsPerRow() const { return planes_[0].PixelsPerRow(); }
Unexecuted instantiation: jxl::Image3<short>::PixelsPerRow() const
308
309
 private:
310
179k
  Image3(PlaneT&& plane0, PlaneT&& plane1, PlaneT&& plane2) {
311
179k
    planes_[0] = std::move(plane0);
312
179k
    planes_[1] = std::move(plane1);
313
179k
    planes_[2] = std::move(plane2);
314
179k
  }
jxl::Image3<float>::Image3(jxl::Plane<float>&&, jxl::Plane<float>&&, jxl::Plane<float>&&)
Line
Count
Source
310
163k
  Image3(PlaneT&& plane0, PlaneT&& plane1, PlaneT&& plane2) {
311
163k
    planes_[0] = std::move(plane0);
312
163k
    planes_[1] = std::move(plane1);
313
163k
    planes_[2] = std::move(plane2);
314
163k
  }
jxl::Image3<int>::Image3(jxl::Plane<int>&&, jxl::Plane<int>&&, jxl::Plane<int>&&)
Line
Count
Source
310
12.0k
  Image3(PlaneT&& plane0, PlaneT&& plane1, PlaneT&& plane2) {
311
12.0k
    planes_[0] = std::move(plane0);
312
12.0k
    planes_[1] = std::move(plane1);
313
12.0k
    planes_[2] = std::move(plane2);
314
12.0k
  }
Unexecuted instantiation: jxl::Image3<unsigned char>::Image3(jxl::Plane<unsigned char>&&, jxl::Plane<unsigned char>&&, jxl::Plane<unsigned char>&&)
jxl::Image3<short>::Image3(jxl::Plane<short>&&, jxl::Plane<short>&&, jxl::Plane<short>&&)
Line
Count
Source
310
4.26k
  Image3(PlaneT&& plane0, PlaneT&& plane1, PlaneT&& plane2) {
311
4.26k
    planes_[0] = std::move(plane0);
312
4.26k
    planes_[1] = std::move(plane1);
313
4.26k
    planes_[2] = std::move(plane2);
314
4.26k
  }
315
316
1.66G
  void PlaneRowBoundsCheck(const size_t c, const size_t y) const {
317
1.66G
    JXL_DASSERT(c < kNumPlanes && y < ysize());
318
1.66G
  }
jxl::Image3<float>::PlaneRowBoundsCheck(unsigned long, unsigned long) const
Line
Count
Source
316
1.65G
  void PlaneRowBoundsCheck(const size_t c, const size_t y) const {
317
1.65G
    JXL_DASSERT(c < kNumPlanes && y < ysize());
318
1.65G
  }
jxl::Image3<int>::PlaneRowBoundsCheck(unsigned long, unsigned long) const
Line
Count
Source
316
1.95M
  void PlaneRowBoundsCheck(const size_t c, const size_t y) const {
317
1.95M
    JXL_DASSERT(c < kNumPlanes && y < ysize());
318
1.95M
  }
Unexecuted instantiation: jxl::Image3<unsigned char>::PlaneRowBoundsCheck(unsigned long, unsigned long) const
jxl::Image3<short>::PlaneRowBoundsCheck(unsigned long, unsigned long) const
Line
Count
Source
316
1.86k
  void PlaneRowBoundsCheck(const size_t c, const size_t y) const {
317
1.86k
    JXL_DASSERT(c < kNumPlanes && y < ysize());
318
1.86k
  }
319
320
  PlaneT planes_[kNumPlanes];
321
};
322
323
using Image3B = Image3<uint8_t>;
324
using Image3S = Image3<int16_t>;
325
using Image3U = Image3<uint16_t>;
326
using Image3I = Image3<int32_t>;
327
using Image3F = Image3<float>;
328
using Image3D = Image3<double>;
329
330
}  // namespace jxl
331
332
#endif  // LIB_JXL_IMAGE_H_