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