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 #include <inttypes.h>
12 #include <stddef.h>
13 #include <stdint.h>
14 #include <string.h>
15
16 #include <algorithm>
17 #include <utility> // std::move
18
19 #include "lib/jxl/base/cache_aligned.h"
20 #include "lib/jxl/base/compiler_specific.h"
21 #include "lib/jxl/base/status.h"
22 #include "lib/jxl/common.h"
23
24 namespace jxl {
25
26 // Type-independent parts of Plane<> - reduces code duplication and facilitates
27 // moving member function implementations to cc file.
28 struct PlaneBase {
PlaneBasePlaneBase29 PlaneBase()
30 : xsize_(0),
31 ysize_(0),
32 orig_xsize_(0),
33 orig_ysize_(0),
34 bytes_per_row_(0),
35 bytes_(nullptr) {}
36 PlaneBase(size_t xsize, size_t ysize, size_t sizeof_t);
37
38 // Copy construction/assignment is forbidden to avoid inadvertent copies,
39 // which can be very expensive. Use CopyImageTo() instead.
40 PlaneBase(const PlaneBase& other) = delete;
41 PlaneBase& operator=(const PlaneBase& other) = delete;
42
43 // Move constructor (required for returning Image from function)
44 PlaneBase(PlaneBase&& other) noexcept = default;
45
46 // Move assignment (required for std::vector)
47 PlaneBase& operator=(PlaneBase&& other) noexcept = default;
48
49 void Swap(PlaneBase& other);
50
51 // Useful for pre-allocating image with some padding for alignment purposes
52 // and later reporting the actual valid dimensions. May also be used to
53 // un-shrink the image. Caller is responsible for ensuring xsize/ysize are <=
54 // the original dimensions.
ShrinkToPlaneBase55 void ShrinkTo(const size_t xsize, const size_t ysize) {
56 JXL_CHECK(xsize <= orig_xsize_);
57 JXL_CHECK(ysize <= orig_ysize_);
58 xsize_ = static_cast<uint32_t>(xsize);
59 ysize_ = static_cast<uint32_t>(ysize);
60 // NOTE: we can't recompute bytes_per_row for more compact storage and
61 // better locality because that would invalidate the image contents.
62 }
63
64 // How many pixels.
xsizePlaneBase65 JXL_INLINE size_t xsize() const { return xsize_; }
ysizePlaneBase66 JXL_INLINE size_t ysize() const { return ysize_; }
67
68 // NOTE: do not use this for copying rows - the valid xsize may be much less.
bytes_per_rowPlaneBase69 JXL_INLINE size_t bytes_per_row() const { return bytes_per_row_; }
70
71 // Raw access to byte contents, for interfacing with other libraries.
72 // Unsigned char instead of char to avoid surprises (sign extension).
bytesPlaneBase73 JXL_INLINE uint8_t* bytes() {
74 void* p = bytes_.get();
75 return static_cast<uint8_t * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(p, 64));
76 }
bytesPlaneBase77 JXL_INLINE const uint8_t* bytes() const {
78 const void* p = bytes_.get();
79 return static_cast<const uint8_t * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(p, 64));
80 }
81
82 protected:
83 // Returns pointer to the start of a row.
VoidRowPlaneBase84 JXL_INLINE void* VoidRow(const size_t y) const {
85 #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
86 defined(THREAD_SANITIZER)
87 if (y >= ysize_) {
88 JXL_ABORT("Row(%" PRIu64 ") in (%u x %u) image\n", y, xsize_, ysize_);
89 }
90 #endif
91
92 void* row = bytes_.get() + y * bytes_per_row_;
93 return JXL_ASSUME_ALIGNED(row, 64);
94 }
95
96 enum class Padding {
97 // Allow Load(d, row + x) for x = 0; x < xsize(); x += Lanes(d). Default.
98 kRoundUp,
99 // Allow LoadU(d, row + x) for x = xsize() - 1. This requires an extra
100 // vector to be initialized. If done by default, this would suppress
101 // legitimate msan warnings. We therefore require users to explicitly call
102 // InitializePadding before using unaligned loads (e.g. convolution).
103 kUnaligned
104 };
105
106 // Initializes the minimum bytes required to suppress msan warnings from
107 // legitimate (according to Padding mode) vector loads/stores on the right
108 // border, where some lanes are uninitialized and assumed to be unused.
109 void InitializePadding(size_t sizeof_t, Padding padding);
110
111 // (Members are non-const to enable assignment during move-assignment.)
112 uint32_t xsize_; // In valid pixels, not including any padding.
113 uint32_t ysize_;
114 uint32_t orig_xsize_;
115 uint32_t orig_ysize_;
116 size_t bytes_per_row_; // Includes padding.
117 CacheAlignedUniquePtr bytes_;
118 };
119
120 // Single channel, aligned rows separated by padding. T must be POD.
121 //
122 // 'Single channel' (one 2D array per channel) simplifies vectorization
123 // (repeating the same operation on multiple adjacent components) without the
124 // complexity of a hybrid layout (8 R, 8 G, 8 B, ...). In particular, clients
125 // can easily iterate over all components in a row and Image requires no
126 // knowledge of the pixel format beyond the component type "T".
127 //
128 // 'Aligned' means each row is aligned to the L1 cache line size. This prevents
129 // false sharing between two threads operating on adjacent rows.
130 //
131 // 'Padding' is still relevant because vectors could potentially be larger than
132 // a cache line. By rounding up row sizes to the vector size, we allow
133 // reading/writing ALIGNED vectors whose first lane is a valid sample. This
134 // avoids needing a separate loop to handle remaining unaligned lanes.
135 //
136 // This image layout could also be achieved with a vector and a row accessor
137 // function, but a class wrapper with support for "deleter" allows wrapping
138 // existing memory allocated by clients without copying the pixels. It also
139 // provides convenient accessors for xsize/ysize, which shortens function
140 // argument lists. Supports move-construction so it can be stored in containers.
141 template <typename ComponentType>
142 class Plane : public PlaneBase {
143 public:
144 using T = ComponentType;
145 static constexpr size_t kNumPlanes = 1;
146
147 Plane() = default;
Plane(const size_t xsize,const size_t ysize)148 Plane(const size_t xsize, const size_t ysize)
149 : PlaneBase(xsize, ysize, sizeof(T)) {}
150
InitializePaddingForUnalignedAccesses()151 void InitializePaddingForUnalignedAccesses() {
152 InitializePadding(sizeof(T), Padding::kUnaligned);
153 }
154
Row(const size_t y)155 JXL_INLINE T* Row(const size_t y) { return static_cast<T*>(VoidRow(y)); }
156
157 // Returns pointer to const (see above).
Row(const size_t y)158 JXL_INLINE const T* Row(const size_t y) const {
159 return static_cast<const T*>(VoidRow(y));
160 }
161
162 // Documents that the access is const.
ConstRow(const size_t y)163 JXL_INLINE const T* ConstRow(const size_t y) const {
164 return static_cast<const T*>(VoidRow(y));
165 }
166
167 // Returns number of pixels (some of which are padding) per row. Useful for
168 // computing other rows via pointer arithmetic. WARNING: this must
169 // NOT be used to determine xsize.
PixelsPerRow()170 JXL_INLINE intptr_t PixelsPerRow() const {
171 return static_cast<intptr_t>(bytes_per_row_ / sizeof(T));
172 }
173 };
174
175 using ImageSB = Plane<int8_t>;
176 using ImageB = Plane<uint8_t>;
177 using ImageS = Plane<int16_t>; // signed integer or half-float
178 using ImageU = Plane<uint16_t>;
179 using ImageI = Plane<int32_t>;
180 using ImageF = Plane<float>;
181 using ImageD = Plane<double>;
182
183 // Also works for Image3 and mixed argument types.
184 template <class Image1, class Image2>
SameSize(const Image1 & image1,const Image2 & image2)185 bool SameSize(const Image1& image1, const Image2& image2) {
186 return image1.xsize() == image2.xsize() && image1.ysize() == image2.ysize();
187 }
188
189 template <typename T>
190 class Image3;
191
192 // Rectangular region in image(s). Factoring this out of Image instead of
193 // shifting the pointer by x0/y0 allows this to apply to multiple images with
194 // different resolutions (e.g. color transform and quantization field).
195 // Can compare using SameSize(rect1, rect2).
196 class Rect {
197 public:
198 // Most windows are xsize_max * ysize_max, except those on the borders where
199 // begin + size_max > end.
Rect(size_t xbegin,size_t ybegin,size_t xsize_max,size_t ysize_max,size_t xend,size_t yend)200 constexpr Rect(size_t xbegin, size_t ybegin, size_t xsize_max,
201 size_t ysize_max, size_t xend, size_t yend)
202 : x0_(xbegin),
203 y0_(ybegin),
204 xsize_(ClampedSize(xbegin, xsize_max, xend)),
205 ysize_(ClampedSize(ybegin, ysize_max, yend)) {}
206
207 // Construct with origin and known size (typically from another Rect).
Rect(size_t xbegin,size_t ybegin,size_t xsize,size_t ysize)208 constexpr Rect(size_t xbegin, size_t ybegin, size_t xsize, size_t ysize)
209 : x0_(xbegin), y0_(ybegin), xsize_(xsize), ysize_(ysize) {}
210
211 // Construct a rect that covers a whole image/plane/ImageBundle etc.
212 template <typename Image>
Rect(const Image & image)213 explicit Rect(const Image& image)
214 : Rect(0, 0, image.xsize(), image.ysize()) {}
215
Rect()216 Rect() : Rect(0, 0, 0, 0) {}
217
218 Rect(const Rect&) = default;
219 Rect& operator=(const Rect&) = default;
220
221 // Construct a subrect that resides in an image/plane/ImageBundle etc.
222 template <typename Image>
Crop(const Image & image)223 Rect Crop(const Image& image) const {
224 return Rect(x0_, y0_, xsize_, ysize_, image.xsize(), image.ysize());
225 }
226
227 // Construct a subrect that resides in the [0, ysize) x [0, xsize) region of
228 // the current rect.
Crop(size_t area_xsize,size_t area_ysize)229 Rect Crop(size_t area_xsize, size_t area_ysize) const {
230 return Rect(x0_, y0_, xsize_, ysize_, area_xsize, area_ysize);
231 }
232
233 // Returns a rect that only contains `num` lines with offset `y` from `y0()`.
Lines(size_t y,size_t num)234 Rect Lines(size_t y, size_t num) const {
235 JXL_DASSERT(y + num <= ysize_);
236 return Rect(x0_, y0_ + y, xsize_, num);
237 }
238
Line(size_t y)239 Rect Line(size_t y) const { return Lines(y, 1); }
240
Intersection(const Rect & other)241 JXL_MUST_USE_RESULT Rect Intersection(const Rect& other) const {
242 return Rect(std::max(x0_, other.x0_), std::max(y0_, other.y0_), xsize_,
243 ysize_, std::min(x0_ + xsize_, other.x0_ + other.xsize_),
244 std::min(y0_ + ysize_, other.y0_ + other.ysize_));
245 }
246
Translate(int64_t x_offset,int64_t y_offset)247 JXL_MUST_USE_RESULT Rect Translate(int64_t x_offset, int64_t y_offset) const {
248 return Rect(x0_ + x_offset, y0_ + y_offset, xsize_, ysize_);
249 }
250
251 template <typename T>
Row(Plane<T> * image,size_t y)252 T* Row(Plane<T>* image, size_t y) const {
253 return image->Row(y + y0_) + x0_;
254 }
255
256 template <typename T>
Row(const Plane<T> * image,size_t y)257 const T* Row(const Plane<T>* image, size_t y) const {
258 return image->Row(y + y0_) + x0_;
259 }
260
261 template <typename T>
PlaneRow(Image3<T> * image,const size_t c,size_t y)262 T* PlaneRow(Image3<T>* image, const size_t c, size_t y) const {
263 return image->PlaneRow(c, y + y0_) + x0_;
264 }
265
266 template <typename T>
ConstRow(const Plane<T> & image,size_t y)267 const T* ConstRow(const Plane<T>& image, size_t y) const {
268 return image.ConstRow(y + y0_) + x0_;
269 }
270
271 template <typename T>
ConstPlaneRow(const Image3<T> & image,size_t c,size_t y)272 const T* ConstPlaneRow(const Image3<T>& image, size_t c, size_t y) const {
273 return image.ConstPlaneRow(c, y + y0_) + x0_;
274 }
275
IsInside(const Rect & other)276 bool IsInside(const Rect& other) const {
277 return x0_ >= other.x0() && x0_ + xsize_ <= other.x0() + other.xsize_ &&
278 y0_ >= other.y0() && y0_ + ysize_ <= other.y0() + other.ysize();
279 }
280
281 // Returns true if this Rect fully resides in the given image. ImageT could be
282 // Plane<T> or Image3<T>; however if ImageT is Rect, results are nonsensical.
283 template <class ImageT>
IsInside(const ImageT & image)284 bool IsInside(const ImageT& image) const {
285 return (x0_ + xsize_ <= image.xsize()) && (y0_ + ysize_ <= image.ysize());
286 }
287
x0()288 size_t x0() const { return x0_; }
y0()289 size_t y0() const { return y0_; }
xsize()290 size_t xsize() const { return xsize_; }
ysize()291 size_t ysize() const { return ysize_; }
292
293 private:
294 // Returns size_max, or whatever is left in [begin, end).
ClampedSize(size_t begin,size_t size_max,size_t end)295 static constexpr size_t ClampedSize(size_t begin, size_t size_max,
296 size_t end) {
297 return (begin + size_max <= end) ? size_max
298 : (end > begin ? end - begin : 0);
299 }
300
301 size_t x0_;
302 size_t y0_;
303
304 size_t xsize_;
305 size_t ysize_;
306 };
307
308 // Currently, we abuse Image to either refer to an image that owns its storage
309 // or one that doesn't. In similar vein, we abuse Image* function parameters to
310 // either mean "assign to me" or "fill the provided image with data".
311 // Hopefully, the "assign to me" meaning will go away and most images in the
312 // codebase will not be backed by own storage. When this happens we can redesign
313 // Image to be a non-storage-holding view class and introduce BackedImage in
314 // those places that actually need it.
315
316 // NOTE: we can't use Image as a view because invariants are violated
317 // (alignment and the presence of padding before/after each "row").
318
319 // A bundle of 3 same-sized images. Typically constructed by moving from three
320 // rvalue references to Image. To overwrite an existing Image3 using
321 // single-channel producers, we also need access to Image*. Constructing
322 // temporary non-owning Image pointing to one plane of an existing Image3 risks
323 // dangling references, especially if the wrapper is moved. Therefore, we
324 // store an array of Image (which are compact enough that size is not a concern)
325 // and provide Plane+Row accessors.
326 template <typename ComponentType>
327 class Image3 {
328 public:
329 using T = ComponentType;
330 using PlaneT = jxl::Plane<T>;
331 static constexpr size_t kNumPlanes = 3;
332
Image3()333 Image3() : planes_{PlaneT(), PlaneT(), PlaneT()} {}
334
Image3(const size_t xsize,const size_t ysize)335 Image3(const size_t xsize, const size_t ysize)
336 : planes_{PlaneT(xsize, ysize), PlaneT(xsize, ysize),
337 PlaneT(xsize, ysize)} {}
338
Image3(Image3 && other)339 Image3(Image3&& other) noexcept {
340 for (size_t i = 0; i < kNumPlanes; i++) {
341 planes_[i] = std::move(other.planes_[i]);
342 }
343 }
344
Image3(PlaneT && plane0,PlaneT && plane1,PlaneT && plane2)345 Image3(PlaneT&& plane0, PlaneT&& plane1, PlaneT&& plane2) {
346 JXL_CHECK(SameSize(plane0, plane1));
347 JXL_CHECK(SameSize(plane0, plane2));
348 planes_[0] = std::move(plane0);
349 planes_[1] = std::move(plane1);
350 planes_[2] = std::move(plane2);
351 }
352
353 // Copy construction/assignment is forbidden to avoid inadvertent copies,
354 // which can be very expensive. Use CopyImageTo instead.
355 Image3(const Image3& other) = delete;
356 Image3& operator=(const Image3& other) = delete;
357
358 Image3& operator=(Image3&& other) noexcept {
359 for (size_t i = 0; i < kNumPlanes; i++) {
360 planes_[i] = std::move(other.planes_[i]);
361 }
362 return *this;
363 }
364
365 // Returns row pointer; usage: PlaneRow(idx_plane, y)[x] = val.
PlaneRow(const size_t c,const size_t y)366 JXL_INLINE T* PlaneRow(const size_t c, const size_t y) {
367 // Custom implementation instead of calling planes_[c].Row ensures only a
368 // single multiplication is needed for PlaneRow(0..2, y).
369 PlaneRowBoundsCheck(c, y);
370 const size_t row_offset = y * planes_[0].bytes_per_row();
371 void* row = planes_[c].bytes() + row_offset;
372 return static_cast<T * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(row, 64));
373 }
374
375 // Returns const row pointer; usage: val = PlaneRow(idx_plane, y)[x].
PlaneRow(const size_t c,const size_t y)376 JXL_INLINE const T* PlaneRow(const size_t c, const size_t y) const {
377 PlaneRowBoundsCheck(c, y);
378 const size_t row_offset = y * planes_[0].bytes_per_row();
379 const void* row = planes_[c].bytes() + row_offset;
380 return static_cast<const T * JXL_RESTRICT>(JXL_ASSUME_ALIGNED(row, 64));
381 }
382
383 // Returns const row pointer, even if called from a non-const Image3.
ConstPlaneRow(const size_t c,const size_t y)384 JXL_INLINE const T* ConstPlaneRow(const size_t c, const size_t y) const {
385 PlaneRowBoundsCheck(c, y);
386 return PlaneRow(c, y);
387 }
388
Plane(size_t idx)389 JXL_INLINE const PlaneT& Plane(size_t idx) const { return planes_[idx]; }
390
Plane(size_t idx)391 JXL_INLINE PlaneT& Plane(size_t idx) { return planes_[idx]; }
392
Swap(Image3 & other)393 void Swap(Image3& other) {
394 for (size_t c = 0; c < 3; ++c) {
395 other.planes_[c].Swap(planes_[c]);
396 }
397 }
398
399 // Useful for pre-allocating image with some padding for alignment purposes
400 // and later reporting the actual valid dimensions. May also be used to
401 // un-shrink the image. Caller is responsible for ensuring xsize/ysize are <=
402 // the original dimensions.
ShrinkTo(const size_t xsize,const size_t ysize)403 void ShrinkTo(const size_t xsize, const size_t ysize) {
404 for (PlaneT& plane : planes_) {
405 plane.ShrinkTo(xsize, ysize);
406 }
407 }
408
409 // Sizes of all three images are guaranteed to be equal.
xsize()410 JXL_INLINE size_t xsize() const { return planes_[0].xsize(); }
ysize()411 JXL_INLINE size_t ysize() const { return planes_[0].ysize(); }
412 // Returns offset [bytes] from one row to the next row of the same plane.
413 // WARNING: this must NOT be used to determine xsize, nor for copying rows -
414 // the valid xsize may be much less.
bytes_per_row()415 JXL_INLINE size_t bytes_per_row() const { return planes_[0].bytes_per_row(); }
416 // Returns number of pixels (some of which are padding) per row. Useful for
417 // computing other rows via pointer arithmetic. WARNING: this must NOT be used
418 // to determine xsize.
PixelsPerRow()419 JXL_INLINE intptr_t PixelsPerRow() const { return planes_[0].PixelsPerRow(); }
420
421 private:
PlaneRowBoundsCheck(const size_t c,const size_t y)422 void PlaneRowBoundsCheck(const size_t c, const size_t y) const {
423 #if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
424 defined(THREAD_SANITIZER)
425 if (c >= kNumPlanes || y >= ysize()) {
426 JXL_ABORT("PlaneRow(%" PRIu64 ", %" PRIu64 ") in (%" PRIu64 " x %" PRIu64
427 ") image\n",
428 static_cast<uint64_t>(c), static_cast<uint64_t>(y),
429 static_cast<uint64_t>(xsize()), static_cast<uint64_t>(ysize()));
430 }
431 #endif
432 }
433
434 private:
435 PlaneT planes_[kNumPlanes];
436 };
437
438 using Image3B = Image3<uint8_t>;
439 using Image3S = Image3<int16_t>;
440 using Image3U = Image3<uint16_t>;
441 using Image3I = Image3<int32_t>;
442 using Image3F = Image3<float>;
443 using Image3D = Image3<double>;
444
445 } // namespace jxl
446
447 #endif // LIB_JXL_IMAGE_H_
448