Coverage Report

Created: 2025-09-08 07:52

/src/libjxl/lib/jxl/image.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_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
1.88M
      : xsize_(0),
38
1.88M
        ysize_(0),
39
1.88M
        orig_xsize_(0),
40
1.88M
        orig_ysize_(0),
41
1.88M
        bytes_per_row_(0),
42
1.88M
        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
62.8M
  PlaneBase(PlaneBase&& other) noexcept = default;
51
52
  // Move assignment (required for std::vector)
53
8.71M
  PlaneBase& operator=(PlaneBase&& other) noexcept = default;
54
55
95.5M
  ~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
0
  Status ShrinkTo(const size_t xsize, const size_t ysize) {
64
0
    JXL_ENSURE(xsize <= orig_xsize_);
65
0
    JXL_ENSURE(ysize <= orig_ysize_);
66
0
    xsize_ = static_cast<uint32_t>(xsize);
67
0
    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
0
    return true;
71
0
  }
72
73
  // How many pixels.
74
19.9M
  JXL_INLINE size_t xsize() const { return xsize_; }
75
5.71M
  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
93.1M
  JXL_INLINE size_t bytes_per_row() const { return bytes_per_row_; }
79
80
78.2k
  JXL_INLINE JxlMemoryManager* memory_manager() const {
81
78.2k
    return bytes_.memory_manager();
82
78.2k
  }
83
84
  // Raw access to byte contents, for interfacing with other libraries.
85
  // Unsigned char instead of char to avoid surprises (sign extension).
86
3.37M
  JXL_INLINE uint8_t* bytes() {
87
3.37M
    uint8_t* p = bytes_.address<uint8_t>();
88
3.37M
    return static_cast<uint8_t * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(p, 64));
89
3.37M
  }
90
89.7M
  JXL_INLINE const uint8_t* bytes() const {
91
89.7M
    const uint8_t* p = bytes_.address<uint8_t>();
92
89.7M
    return static_cast<const uint8_t * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(p, 64));
93
89.7M
  }
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
206M
  JXL_INLINE void* VoidRow(const size_t y) const {
101
206M
    JXL_DASSERT(y < ysize_);
102
206M
    uint8_t* row = bytes_.address<uint8_t>() + y * bytes_per_row_;
103
206M
    return JXL_ASSUME_ALIGNED(row, 64);
104
206M
  }
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
1.88M
  Plane() = default;
jxl::Plane<float>::Plane()
Line
Count
Source
145
1.29M
  Plane() = default;
jxl::Plane<int>::Plane()
Line
Count
Source
145
484k
  Plane() = default;
jxl::Plane<unsigned char>::Plane()
Line
Count
Source
145
19.7k
  Plane() = default;
jxl::Plane<signed char>::Plane()
Line
Count
Source
145
27.3k
  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
57.9k
  Plane() = default;
146
147
  static StatusOr<Plane> Create(JxlMemoryManager* memory_manager,
148
                                const size_t xsize, const size_t ysize,
149
30.7M
                                const size_t pre_padding = 0) {
150
30.7M
    static_assert(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 ||
151
30.7M
                  sizeof(T) == 8);
152
30.7M
    uint32_t xsize32 = static_cast<uint32_t>(xsize);
153
30.7M
    uint32_t ysize32 = static_cast<uint32_t>(ysize);
154
30.7M
    JXL_ENSURE(xsize32 == xsize);
155
30.7M
    JXL_ENSURE(ysize32 == ysize);
156
30.7M
    Plane plane(xsize32, ysize32, sizeof(T));
157
30.7M
    JXL_RETURN_IF_ERROR(plane.Allocate(memory_manager, pre_padding));
158
30.7M
    return plane;
159
30.7M
  }
jxl::Plane<float>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long, unsigned long)
Line
Count
Source
149
30.3M
                                const size_t pre_padding = 0) {
150
30.3M
    static_assert(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 ||
151
30.3M
                  sizeof(T) == 8);
152
30.3M
    uint32_t xsize32 = static_cast<uint32_t>(xsize);
153
30.3M
    uint32_t ysize32 = static_cast<uint32_t>(ysize);
154
30.3M
    JXL_ENSURE(xsize32 == xsize);
155
30.3M
    JXL_ENSURE(ysize32 == ysize);
156
30.3M
    Plane plane(xsize32, ysize32, sizeof(T));
157
30.3M
    JXL_RETURN_IF_ERROR(plane.Allocate(memory_manager, pre_padding));
158
30.3M
    return plane;
159
30.3M
  }
jxl::Plane<int>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long, unsigned long)
Line
Count
Source
149
373k
                                const size_t pre_padding = 0) {
150
373k
    static_assert(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 ||
151
373k
                  sizeof(T) == 8);
152
373k
    uint32_t xsize32 = static_cast<uint32_t>(xsize);
153
373k
    uint32_t ysize32 = static_cast<uint32_t>(ysize);
154
373k
    JXL_ENSURE(xsize32 == xsize);
155
373k
    JXL_ENSURE(ysize32 == ysize);
156
373k
    Plane plane(xsize32, ysize32, sizeof(T));
157
373k
    JXL_RETURN_IF_ERROR(plane.Allocate(memory_manager, pre_padding));
158
373k
    return plane;
159
373k
  }
jxl::Plane<unsigned char>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long, unsigned long)
Line
Count
Source
149
32.0k
                                const size_t pre_padding = 0) {
150
32.0k
    static_assert(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 ||
151
32.0k
                  sizeof(T) == 8);
152
32.0k
    uint32_t xsize32 = static_cast<uint32_t>(xsize);
153
32.0k
    uint32_t ysize32 = static_cast<uint32_t>(ysize);
154
32.0k
    JXL_ENSURE(xsize32 == xsize);
155
32.0k
    JXL_ENSURE(ysize32 == ysize);
156
32.0k
    Plane plane(xsize32, ysize32, sizeof(T));
157
32.0k
    JXL_RETURN_IF_ERROR(plane.Allocate(memory_manager, pre_padding));
158
32.0k
    return plane;
159
32.0k
  }
jxl::Plane<signed char>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long, unsigned long)
Line
Count
Source
149
21.3k
                                const size_t pre_padding = 0) {
150
21.3k
    static_assert(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 ||
151
21.3k
                  sizeof(T) == 8);
152
21.3k
    uint32_t xsize32 = static_cast<uint32_t>(xsize);
153
21.3k
    uint32_t ysize32 = static_cast<uint32_t>(ysize);
154
21.3k
    JXL_ENSURE(xsize32 == xsize);
155
21.3k
    JXL_ENSURE(ysize32 == ysize);
156
21.3k
    Plane plane(xsize32, ysize32, sizeof(T));
157
21.3k
    JXL_RETURN_IF_ERROR(plane.Allocate(memory_manager, pre_padding));
158
21.3k
    return plane;
159
21.3k
  }
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
14.4k
                                const size_t pre_padding = 0) {
150
14.4k
    static_assert(sizeof(T) == 1 || sizeof(T) == 2 || sizeof(T) == 4 ||
151
14.4k
                  sizeof(T) == 8);
152
14.4k
    uint32_t xsize32 = static_cast<uint32_t>(xsize);
153
14.4k
    uint32_t ysize32 = static_cast<uint32_t>(ysize);
154
14.4k
    JXL_ENSURE(xsize32 == xsize);
155
14.4k
    JXL_ENSURE(ysize32 == ysize);
156
14.4k
    Plane plane(xsize32, ysize32, sizeof(T));
157
14.4k
    JXL_RETURN_IF_ERROR(plane.Allocate(memory_manager, pre_padding));
158
14.4k
    return plane;
159
14.4k
  }
160
161
199M
  JXL_INLINE T* Row(const size_t y) { return static_cast<T*>(VoidRow(y)); }
jxl::Plane<float>::Row(unsigned long)
Line
Count
Source
161
185M
  JXL_INLINE T* Row(const size_t y) { return static_cast<T*>(VoidRow(y)); }
jxl::Plane<unsigned char>::Row(unsigned long)
Line
Count
Source
161
92.8k
  JXL_INLINE T* Row(const size_t y) { return static_cast<T*>(VoidRow(y)); }
jxl::Plane<int>::Row(unsigned long)
Line
Count
Source
161
10.7M
  JXL_INLINE T* Row(const size_t y) { return static_cast<T*>(VoidRow(y)); }
jxl::Plane<signed char>::Row(unsigned long)
Line
Count
Source
161
3.10M
  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)
162
163
  // Returns pointer to const (see above).
164
5.00M
  JXL_INLINE const T* Row(const size_t y) const {
165
5.00M
    return static_cast<const T*>(VoidRow(y));
166
5.00M
  }
jxl::Plane<int>::Row(unsigned long) const
Line
Count
Source
164
4.74M
  JXL_INLINE const T* Row(const size_t y) const {
165
4.74M
    return static_cast<const T*>(VoidRow(y));
166
4.74M
  }
jxl::Plane<float>::Row(unsigned long) const
Line
Count
Source
164
261k
  JXL_INLINE const T* Row(const size_t y) const {
165
261k
    return static_cast<const T*>(VoidRow(y));
166
261k
  }
Unexecuted instantiation: jxl::Plane<unsigned char>::Row(unsigned long) const
167
168
  // Documents that the access is const.
169
1.94M
  JXL_INLINE const T* ConstRow(const size_t y) const {
170
1.94M
    return static_cast<const T*>(VoidRow(y));
171
1.94M
  }
jxl::Plane<float>::ConstRow(unsigned long) const
Line
Count
Source
169
854k
  JXL_INLINE const T* ConstRow(const size_t y) const {
170
854k
    return static_cast<const T*>(VoidRow(y));
171
854k
  }
jxl::Plane<unsigned char>::ConstRow(unsigned long) const
Line
Count
Source
169
72.1k
  JXL_INLINE const T* ConstRow(const size_t y) const {
170
72.1k
    return static_cast<const T*>(VoidRow(y));
171
72.1k
  }
jxl::Plane<int>::ConstRow(unsigned long) const
Line
Count
Source
169
993k
  JXL_INLINE const T* ConstRow(const size_t y) const {
170
993k
    return static_cast<const T*>(VoidRow(y));
171
993k
  }
jxl::Plane<signed char>::ConstRow(unsigned long) const
Line
Count
Source
169
24.1k
  JXL_INLINE const T* ConstRow(const size_t y) const {
170
24.1k
    return static_cast<const T*>(VoidRow(y));
171
24.1k
  }
172
173
  // Returns number of pixels (some of which are padding) per row. Useful for
174
  // computing other rows via pointer arithmetic. WARNING: this must
175
  // NOT be used to determine xsize.
176
414k
  JXL_INLINE intptr_t PixelsPerRow() const {
177
414k
    return static_cast<intptr_t>(bytes_per_row_ / sizeof(T));
178
414k
  }
jxl::Plane<unsigned char>::PixelsPerRow() const
Line
Count
Source
176
16.5k
  JXL_INLINE intptr_t PixelsPerRow() const {
177
16.5k
    return static_cast<intptr_t>(bytes_per_row_ / sizeof(T));
178
16.5k
  }
jxl::Plane<int>::PixelsPerRow() const
Line
Count
Source
176
238k
  JXL_INLINE intptr_t PixelsPerRow() const {
177
238k
    return static_cast<intptr_t>(bytes_per_row_ / sizeof(T));
178
238k
  }
jxl::Plane<float>::PixelsPerRow() const
Line
Count
Source
176
159k
  JXL_INLINE intptr_t PixelsPerRow() const {
177
159k
    return static_cast<intptr_t>(bytes_per_row_ / sizeof(T));
178
159k
  }
Unexecuted instantiation: jxl::Plane<short>::PixelsPerRow() const
179
180
 private:
181
  Plane(uint32_t xsize, uint32_t ysize, size_t sizeof_t)
182
30.7M
      : detail::PlaneBase(xsize, ysize, sizeof_t) {}
jxl::Plane<float>::Plane(unsigned int, unsigned int, unsigned long)
Line
Count
Source
182
30.3M
      : detail::PlaneBase(xsize, ysize, sizeof_t) {}
jxl::Plane<int>::Plane(unsigned int, unsigned int, unsigned long)
Line
Count
Source
182
373k
      : detail::PlaneBase(xsize, ysize, sizeof_t) {}
jxl::Plane<unsigned char>::Plane(unsigned int, unsigned int, unsigned long)
Line
Count
Source
182
32.0k
      : detail::PlaneBase(xsize, ysize, sizeof_t) {}
jxl::Plane<signed char>::Plane(unsigned int, unsigned int, unsigned long)
Line
Count
Source
182
21.3k
      : 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
182
14.4k
      : detail::PlaneBase(xsize, ysize, sizeof_t) {}
183
};
184
185
using ImageSB = Plane<int8_t>;
186
using ImageB = Plane<uint8_t>;
187
using ImageS = Plane<int16_t>;  // signed integer or half-float
188
using ImageU = Plane<uint16_t>;
189
using ImageI = Plane<int32_t>;
190
using ImageF = Plane<float>;
191
using ImageD = Plane<double>;
192
193
// Currently, we abuse Image to either refer to an image that owns its storage
194
// or one that doesn't. In similar vein, we abuse Image* function parameters to
195
// either mean "assign to me" or "fill the provided image with data".
196
// Hopefully, the "assign to me" meaning will go away and most images in the
197
// codebase will not be backed by own storage. When this happens we can redesign
198
// Image to be a non-storage-holding view class and introduce BackedImage in
199
// those places that actually need it.
200
201
// NOTE: we can't use Image as a view because invariants are violated
202
// (alignment and the presence of padding before/after each "row").
203
204
// A bundle of 3 same-sized images. Typically constructed by moving from three
205
// rvalue references to Image. To overwrite an existing Image3 using
206
// single-channel producers, we also need access to Image*. Constructing
207
// temporary non-owning Image pointing to one plane of an existing Image3 risks
208
// dangling references, especially if the wrapper is moved. Therefore, we
209
// store an array of Image (which are compact enough that size is not a concern)
210
// and provide Plane+Row accessors.
211
template <typename ComponentType>
212
class Image3 {
213
 public:
214
  using T = ComponentType;
215
  using PlaneT = jxl::Plane<T>;
216
  static constexpr size_t kNumPlanes = 3;
217
218
268k
  Image3() : planes_{PlaneT(), PlaneT(), PlaneT()} {}
jxl::Image3<float>::Image3()
Line
Count
Source
218
124k
  Image3() : planes_{PlaneT(), PlaneT(), PlaneT()} {}
jxl::Image3<int>::Image3()
Line
Count
Source
218
139k
  Image3() : planes_{PlaneT(), PlaneT(), PlaneT()} {}
jxl::Image3<short>::Image3()
Line
Count
Source
218
4.82k
  Image3() : planes_{PlaneT(), PlaneT(), PlaneT()} {}
219
220
  // Copy construction/assignment is forbidden to avoid inadvertent copies,
221
  // which can be very expensive. Use CopyImageTo instead.
222
  Image3(const Image3& other) = delete;
223
  Image3& operator=(const Image3& other) = delete;
224
225
83.9k
  Image3(Image3&& other) noexcept {
226
335k
    for (size_t i = 0; i < kNumPlanes; i++) {
227
251k
      planes_[i] = std::move(other.planes_[i]);
228
251k
    }
229
83.9k
  }
jxl::Image3<float>::Image3(jxl::Image3<float>&&)
Line
Count
Source
225
60.2k
  Image3(Image3&& other) noexcept {
226
241k
    for (size_t i = 0; i < kNumPlanes; i++) {
227
180k
      planes_[i] = std::move(other.planes_[i]);
228
180k
    }
229
60.2k
  }
jxl::Image3<int>::Image3(jxl::Image3<int>&&)
Line
Count
Source
225
14.0k
  Image3(Image3&& other) noexcept {
226
56.1k
    for (size_t i = 0; i < kNumPlanes; i++) {
227
42.1k
      planes_[i] = std::move(other.planes_[i]);
228
42.1k
    }
229
14.0k
  }
jxl::Image3<short>::Image3(jxl::Image3<short>&&)
Line
Count
Source
225
9.65k
  Image3(Image3&& other) noexcept {
226
38.6k
    for (size_t i = 0; i < kNumPlanes; i++) {
227
28.9k
      planes_[i] = std::move(other.planes_[i]);
228
28.9k
    }
229
9.65k
  }
Unexecuted instantiation: jxl::Image3<unsigned char>::Image3(jxl::Image3<unsigned char>&&)
230
134k
  Image3& operator=(Image3&& other) noexcept {
231
537k
    for (size_t i = 0; i < kNumPlanes; i++) {
232
403k
      planes_[i] = std::move(other.planes_[i]);
233
403k
    }
234
134k
    return *this;
235
134k
  }
jxl::Image3<float>::operator=(jxl::Image3<float>&&)
Line
Count
Source
230
122k
  Image3& operator=(Image3&& other) noexcept {
231
490k
    for (size_t i = 0; i < kNumPlanes; i++) {
232
367k
      planes_[i] = std::move(other.planes_[i]);
233
367k
    }
234
122k
    return *this;
235
122k
  }
jxl::Image3<int>::operator=(jxl::Image3<int>&&)
Line
Count
Source
230
7.01k
  Image3& operator=(Image3&& other) noexcept {
231
28.0k
    for (size_t i = 0; i < kNumPlanes; i++) {
232
21.0k
      planes_[i] = std::move(other.planes_[i]);
233
21.0k
    }
234
7.01k
    return *this;
235
7.01k
  }
jxl::Image3<short>::operator=(jxl::Image3<short>&&)
Line
Count
Source
230
4.82k
  Image3& operator=(Image3&& other) noexcept {
231
19.3k
    for (size_t i = 0; i < kNumPlanes; i++) {
232
14.4k
      planes_[i] = std::move(other.planes_[i]);
233
14.4k
    }
234
4.82k
    return *this;
235
4.82k
  }
236
237
  static StatusOr<Image3> Create(JxlMemoryManager* memory_manager,
238
41.9k
                                 const size_t xsize, const size_t ysize) {
239
41.9k
    JXL_ASSIGN_OR_RETURN(PlaneT plane0,
240
41.9k
                         PlaneT::Create(memory_manager, xsize, ysize));
241
41.9k
    JXL_ASSIGN_OR_RETURN(PlaneT plane1,
242
41.9k
                         PlaneT::Create(memory_manager, xsize, ysize));
243
41.9k
    JXL_ASSIGN_OR_RETURN(PlaneT plane2,
244
41.9k
                         PlaneT::Create(memory_manager, xsize, ysize));
245
41.9k
    return Image3(std::move(plane0), std::move(plane1), std::move(plane2));
246
41.9k
  }
jxl::Image3<float>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long)
Line
Count
Source
238
30.1k
                                 const size_t xsize, const size_t ysize) {
239
30.1k
    JXL_ASSIGN_OR_RETURN(PlaneT plane0,
240
30.1k
                         PlaneT::Create(memory_manager, xsize, ysize));
241
30.1k
    JXL_ASSIGN_OR_RETURN(PlaneT plane1,
242
30.1k
                         PlaneT::Create(memory_manager, xsize, ysize));
243
30.1k
    JXL_ASSIGN_OR_RETURN(PlaneT plane2,
244
30.1k
                         PlaneT::Create(memory_manager, xsize, ysize));
245
30.1k
    return Image3(std::move(plane0), std::move(plane1), std::move(plane2));
246
30.1k
  }
jxl::Image3<int>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long)
Line
Count
Source
238
7.02k
                                 const size_t xsize, const size_t ysize) {
239
7.02k
    JXL_ASSIGN_OR_RETURN(PlaneT plane0,
240
7.02k
                         PlaneT::Create(memory_manager, xsize, ysize));
241
7.02k
    JXL_ASSIGN_OR_RETURN(PlaneT plane1,
242
7.02k
                         PlaneT::Create(memory_manager, xsize, ysize));
243
7.02k
    JXL_ASSIGN_OR_RETURN(PlaneT plane2,
244
7.02k
                         PlaneT::Create(memory_manager, xsize, ysize));
245
7.02k
    return Image3(std::move(plane0), std::move(plane1), std::move(plane2));
246
7.02k
  }
jxl::Image3<short>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long)
Line
Count
Source
238
4.82k
                                 const size_t xsize, const size_t ysize) {
239
4.82k
    JXL_ASSIGN_OR_RETURN(PlaneT plane0,
240
4.82k
                         PlaneT::Create(memory_manager, xsize, ysize));
241
4.82k
    JXL_ASSIGN_OR_RETURN(PlaneT plane1,
242
4.82k
                         PlaneT::Create(memory_manager, xsize, ysize));
243
4.82k
    JXL_ASSIGN_OR_RETURN(PlaneT plane2,
244
4.82k
                         PlaneT::Create(memory_manager, xsize, ysize));
245
4.82k
    return Image3(std::move(plane0), std::move(plane1), std::move(plane2));
246
4.82k
  }
Unexecuted instantiation: jxl::Image3<unsigned char>::Create(JxlMemoryManagerStruct*, unsigned long, unsigned long)
247
248
  // Returns row pointer; usage: PlaneRow(idx_plane, y)[x] = val.
249
3.37M
  JXL_INLINE T* PlaneRow(const size_t c, const size_t y) {
250
    // Custom implementation instead of calling planes_[c].Row ensures only a
251
    // single multiplication is needed for PlaneRow(0..2, y).
252
3.37M
    PlaneRowBoundsCheck(c, y);
253
3.37M
    const size_t row_offset = y * planes_[0].bytes_per_row();
254
3.37M
    void* row = planes_[c].bytes() + row_offset;
255
3.37M
    return static_cast<T * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(row, 64));
256
3.37M
  }
jxl::Image3<int>::PlaneRow(unsigned long, unsigned long)
Line
Count
Source
249
36.3k
  JXL_INLINE T* PlaneRow(const size_t c, const size_t y) {
250
    // Custom implementation instead of calling planes_[c].Row ensures only a
251
    // single multiplication is needed for PlaneRow(0..2, y).
252
36.3k
    PlaneRowBoundsCheck(c, y);
253
36.3k
    const size_t row_offset = y * planes_[0].bytes_per_row();
254
36.3k
    void* row = planes_[c].bytes() + row_offset;
255
36.3k
    return static_cast<T * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(row, 64));
256
36.3k
  }
jxl::Image3<float>::PlaneRow(unsigned long, unsigned long)
Line
Count
Source
249
3.33M
  JXL_INLINE T* PlaneRow(const size_t c, const size_t y) {
250
    // Custom implementation instead of calling planes_[c].Row ensures only a
251
    // single multiplication is needed for PlaneRow(0..2, y).
252
3.33M
    PlaneRowBoundsCheck(c, y);
253
3.33M
    const size_t row_offset = y * planes_[0].bytes_per_row();
254
3.33M
    void* row = planes_[c].bytes() + row_offset;
255
3.33M
    return static_cast<T * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(row, 64));
256
3.33M
  }
Unexecuted instantiation: jxl::Image3<short>::PlaneRow(unsigned long, unsigned long)
Unexecuted instantiation: jxl::Image3<unsigned char>::PlaneRow(unsigned long, unsigned long)
257
258
  // Returns const row pointer; usage: val = PlaneRow(idx_plane, y)[x].
259
89.7M
  JXL_INLINE const T* PlaneRow(const size_t c, const size_t y) const {
260
89.7M
    PlaneRowBoundsCheck(c, y);
261
89.7M
    const size_t row_offset = y * planes_[0].bytes_per_row();
262
89.7M
    const void* row = planes_[c].bytes() + row_offset;
263
89.7M
    return static_cast<const T * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(row, 64));
264
89.7M
  }
jxl::Image3<int>::PlaneRow(unsigned long, unsigned long) const
Line
Count
Source
259
17.7k
  JXL_INLINE const T* PlaneRow(const size_t c, const size_t y) const {
260
17.7k
    PlaneRowBoundsCheck(c, y);
261
17.7k
    const size_t row_offset = y * planes_[0].bytes_per_row();
262
17.7k
    const void* row = planes_[c].bytes() + row_offset;
263
17.7k
    return static_cast<const T * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(row, 64));
264
17.7k
  }
jxl::Image3<float>::PlaneRow(unsigned long, unsigned long) const
Line
Count
Source
259
89.7M
  JXL_INLINE const T* PlaneRow(const size_t c, const size_t y) const {
260
89.7M
    PlaneRowBoundsCheck(c, y);
261
89.7M
    const size_t row_offset = y * planes_[0].bytes_per_row();
262
89.7M
    const void* row = planes_[c].bytes() + row_offset;
263
89.7M
    return static_cast<const T * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(row, 64));
264
89.7M
  }
Unexecuted instantiation: jxl::Image3<short>::PlaneRow(unsigned long, unsigned long) const
Unexecuted instantiation: jxl::Image3<unsigned char>::PlaneRow(unsigned long, unsigned long) const
265
266
  // Returns const row pointer, even if called from a non-const Image3.
267
89.7M
  JXL_INLINE const T* ConstPlaneRow(const size_t c, const size_t y) const {
268
89.7M
    PlaneRowBoundsCheck(c, y);
269
89.7M
    return PlaneRow(c, y);
270
89.7M
  }
jxl::Image3<float>::ConstPlaneRow(unsigned long, unsigned long) const
Line
Count
Source
267
89.7M
  JXL_INLINE const T* ConstPlaneRow(const size_t c, const size_t y) const {
268
89.7M
    PlaneRowBoundsCheck(c, y);
269
89.7M
    return PlaneRow(c, y);
270
89.7M
  }
jxl::Image3<int>::ConstPlaneRow(unsigned long, unsigned long) const
Line
Count
Source
267
17.7k
  JXL_INLINE const T* ConstPlaneRow(const size_t c, const size_t y) const {
268
17.7k
    PlaneRowBoundsCheck(c, y);
269
17.7k
    return PlaneRow(c, y);
270
17.7k
  }
Unexecuted instantiation: jxl::Image3<unsigned char>::ConstPlaneRow(unsigned long, unsigned long) const
271
272
36.9k
  JXL_INLINE const PlaneT& Plane(size_t idx) const { return planes_[idx]; }
273
274
0
  JXL_INLINE PlaneT& Plane(size_t idx) { return planes_[idx]; }
Unexecuted instantiation: jxl::Image3<float>::Plane(unsigned long)
Unexecuted instantiation: jxl::Image3<int>::Plane(unsigned long)
Unexecuted instantiation: jxl::Image3<short>::Plane(unsigned long)
275
276
6
  void Swap(Image3& other) {
277
24
    for (size_t c = 0; c < 3; ++c) {
278
18
      other.planes_[c].Swap(planes_[c]);
279
18
    }
280
6
  }
281
282
  // Useful for pre-allocating image with some padding for alignment purposes
283
  // and later reporting the actual valid dimensions. May also be used to
284
  // un-shrink the image. Caller is responsible for ensuring xsize/ysize are <=
285
  // the original dimensions.
286
0
  Status ShrinkTo(const size_t xsize, const size_t ysize) {
287
0
    for (PlaneT& plane : planes_) {
288
0
      JXL_RETURN_IF_ERROR(plane.ShrinkTo(xsize, ysize));
289
0
    }
290
0
    return true;
291
0
  }
292
293
  // Sizes of all three images are guaranteed to be equal.
294
0
  JXL_INLINE JxlMemoryManager* memory_manager() const {
295
0
    return planes_[0].memory_manager();
296
0
  }
Unexecuted instantiation: jxl::Image3<float>::memory_manager() const
Unexecuted instantiation: jxl::Image3<unsigned char>::memory_manager() const
297
961k
  JXL_INLINE size_t xsize() const { return planes_[0].xsize(); }
jxl::Image3<float>::xsize() const
Line
Count
Source
297
949k
  JXL_INLINE size_t xsize() const { return planes_[0].xsize(); }
jxl::Image3<int>::xsize() const
Line
Count
Source
297
7.54k
  JXL_INLINE size_t xsize() const { return planes_[0].xsize(); }
jxl::Image3<short>::xsize() const
Line
Count
Source
297
4.82k
  JXL_INLINE size_t xsize() const { return planes_[0].xsize(); }
Unexecuted instantiation: jxl::Image3<unsigned char>::xsize() const
298
567k
  JXL_INLINE size_t ysize() const { return planes_[0].ysize(); }
jxl::Image3<float>::ysize() const
Line
Count
Source
298
567k
  JXL_INLINE size_t ysize() const { return planes_[0].ysize(); }
jxl::Image3<int>::ysize() const
Line
Count
Source
298
20
  JXL_INLINE size_t ysize() const { return planes_[0].ysize(); }
Unexecuted instantiation: jxl::Image3<short>::ysize() const
Unexecuted instantiation: jxl::Image3<unsigned char>::ysize() const
299
  // Returns offset [bytes] from one row to the next row of the same plane.
300
  // WARNING: this must NOT be used to determine xsize, nor for copying rows -
301
  // the valid xsize may be much less.
302
  JXL_INLINE size_t bytes_per_row() const { return planes_[0].bytes_per_row(); }
303
  // Returns number of pixels (some of which are padding) per row. Useful for
304
  // computing other rows via pointer arithmetic. WARNING: this must NOT be used
305
  // to determine xsize.
306
18.5k
  JXL_INLINE intptr_t PixelsPerRow() const { return planes_[0].PixelsPerRow(); }
jxl::Image3<int>::PixelsPerRow() const
Line
Count
Source
306
12.3k
  JXL_INLINE intptr_t PixelsPerRow() const { return planes_[0].PixelsPerRow(); }
jxl::Image3<float>::PixelsPerRow() const
Line
Count
Source
306
6.17k
  JXL_INLINE intptr_t PixelsPerRow() const { return planes_[0].PixelsPerRow(); }
Unexecuted instantiation: jxl::Image3<short>::PixelsPerRow() const
307
308
 private:
309
41.9k
  Image3(PlaneT&& plane0, PlaneT&& plane1, PlaneT&& plane2) {
310
41.9k
    planes_[0] = std::move(plane0);
311
41.9k
    planes_[1] = std::move(plane1);
312
41.9k
    planes_[2] = std::move(plane2);
313
41.9k
  }
jxl::Image3<float>::Image3(jxl::Plane<float>&&, jxl::Plane<float>&&, jxl::Plane<float>&&)
Line
Count
Source
309
30.1k
  Image3(PlaneT&& plane0, PlaneT&& plane1, PlaneT&& plane2) {
310
30.1k
    planes_[0] = std::move(plane0);
311
30.1k
    planes_[1] = std::move(plane1);
312
30.1k
    planes_[2] = std::move(plane2);
313
30.1k
  }
jxl::Image3<int>::Image3(jxl::Plane<int>&&, jxl::Plane<int>&&, jxl::Plane<int>&&)
Line
Count
Source
309
7.02k
  Image3(PlaneT&& plane0, PlaneT&& plane1, PlaneT&& plane2) {
310
7.02k
    planes_[0] = std::move(plane0);
311
7.02k
    planes_[1] = std::move(plane1);
312
7.02k
    planes_[2] = std::move(plane2);
313
7.02k
  }
jxl::Image3<short>::Image3(jxl::Plane<short>&&, jxl::Plane<short>&&, jxl::Plane<short>&&)
Line
Count
Source
309
4.82k
  Image3(PlaneT&& plane0, PlaneT&& plane1, PlaneT&& plane2) {
310
4.82k
    planes_[0] = std::move(plane0);
311
4.82k
    planes_[1] = std::move(plane1);
312
4.82k
    planes_[2] = std::move(plane2);
313
4.82k
  }
Unexecuted instantiation: jxl::Image3<unsigned char>::Image3(jxl::Plane<unsigned char>&&, jxl::Plane<unsigned char>&&, jxl::Plane<unsigned char>&&)
314
315
182M
  void PlaneRowBoundsCheck(const size_t c, const size_t y) const {
316
182M
    JXL_DASSERT(c < kNumPlanes && y < ysize());
317
182M
  }
jxl::Image3<int>::PlaneRowBoundsCheck(unsigned long, unsigned long) const
Line
Count
Source
315
71.8k
  void PlaneRowBoundsCheck(const size_t c, const size_t y) const {
316
71.8k
    JXL_DASSERT(c < kNumPlanes && y < ysize());
317
71.8k
  }
jxl::Image3<float>::PlaneRowBoundsCheck(unsigned long, unsigned long) const
Line
Count
Source
315
182M
  void PlaneRowBoundsCheck(const size_t c, const size_t y) const {
316
182M
    JXL_DASSERT(c < kNumPlanes && y < ysize());
317
182M
  }
Unexecuted instantiation: jxl::Image3<short>::PlaneRowBoundsCheck(unsigned long, unsigned long) const
Unexecuted instantiation: jxl::Image3<unsigned char>::PlaneRowBoundsCheck(unsigned long, unsigned long) const
318
319
  PlaneT planes_[kNumPlanes];
320
};
321
322
using Image3B = Image3<uint8_t>;
323
using Image3S = Image3<int16_t>;
324
using Image3U = Image3<uint16_t>;
325
using Image3I = Image3<int32_t>;
326
using Image3F = Image3<float>;
327
using Image3D = Image3<double>;
328
329
}  // namespace jxl
330
331
#endif  // LIB_JXL_IMAGE_H_