/src/libjxl/lib/jxl/modular/modular_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_MODULAR_MODULAR_IMAGE_H_ |
7 | | #define LIB_JXL_MODULAR_MODULAR_IMAGE_H_ |
8 | | |
9 | | #include <jxl/memory_manager.h> |
10 | | |
11 | | #include <cstddef> |
12 | | #include <cstdint> |
13 | | #include <cstring> |
14 | | #include <string> |
15 | | #include <utility> |
16 | | #include <vector> |
17 | | |
18 | | #include "lib/jxl/base/compiler_specific.h" |
19 | | #include "lib/jxl/base/data_parallel.h" |
20 | | #include "lib/jxl/base/status.h" |
21 | | #include "lib/jxl/image.h" |
22 | | |
23 | | namespace jxl { |
24 | | |
25 | | typedef int32_t pixel_type; // can use int16_t if it's only for 8-bit images. |
26 | | // Need some wiggle room for YCoCg / Squeeze etc |
27 | | |
28 | | typedef int64_t pixel_type_w; |
29 | | |
30 | | namespace weighted { |
31 | | struct Header; |
32 | | } |
33 | | |
34 | | class Channel { |
35 | | public: |
36 | | jxl::Plane<pixel_type> plane; |
37 | | size_t w, h; |
38 | | int hshift, vshift; // w ~= image.w >> hshift; h ~= image.h >> vshift |
39 | | Channel(const Channel& other) = delete; |
40 | | Channel& operator=(const Channel& other) = delete; |
41 | | |
42 | | static StatusOr<Channel> Create(JxlMemoryManager* memory_manager, size_t iw, |
43 | | size_t ih, int hsh = 0, int vsh = 0); |
44 | | |
45 | | // Move assignment |
46 | 1.16M | Channel& operator=(Channel&& other) noexcept { |
47 | 1.16M | w = other.w; |
48 | 1.16M | h = other.h; |
49 | 1.16M | hshift = other.hshift; |
50 | 1.16M | vshift = other.vshift; |
51 | 1.16M | plane = std::move(other.plane); |
52 | 1.16M | return *this; |
53 | 1.16M | } |
54 | | |
55 | | // Move constructor |
56 | 947k | Channel(Channel&& other) noexcept = default; |
57 | | |
58 | 82.1k | JxlMemoryManager* memory_manager() const { return plane.memory_manager(); }; |
59 | | |
60 | 102k | Status shrink() { |
61 | 102k | if (plane.xsize() == w && plane.ysize() == h) return true; |
62 | 80.2k | JXL_ASSIGN_OR_RETURN(plane, |
63 | 80.2k | Plane<pixel_type>::Create(memory_manager(), w, h)); |
64 | 80.2k | return true; |
65 | 80.2k | } |
66 | 11.4k | Status shrink(int nw, int nh) { |
67 | 11.4k | w = nw; |
68 | 11.4k | h = nh; |
69 | 11.4k | return shrink(); |
70 | 11.4k | } |
71 | | |
72 | 8.00M | JXL_INLINE pixel_type* Row(const size_t y) { return plane.Row(y); } |
73 | 9.15M | JXL_INLINE const pixel_type* Row(const size_t y) const { |
74 | 9.15M | return plane.Row(y); |
75 | 9.15M | } |
76 | | |
77 | | private: |
78 | | Channel(jxl::Plane<pixel_type>&& p, size_t iw, size_t ih, int hsh, int vsh) |
79 | 276k | : plane(std::move(p)), w(iw), h(ih), hshift(hsh), vshift(vsh) {} |
80 | | }; |
81 | | |
82 | | class Transform; |
83 | | |
84 | | class Image { |
85 | | public: |
86 | | // image data, transforms can dramatically change the number of channels and |
87 | | // their semantics |
88 | | std::vector<Channel> channel; |
89 | | // transforms that have been applied (and that have to be undone) |
90 | | std::vector<Transform> transform; |
91 | | |
92 | | // image dimensions (channels may have different dimensions due to transforms) |
93 | | size_t w, h; |
94 | | int bitdepth; |
95 | | size_t nb_meta_channels; // first few channels might contain palette(s) |
96 | | bool error; // true if a fatal error occurred, false otherwise |
97 | | |
98 | | explicit Image(JxlMemoryManager* memory_manager); |
99 | | |
100 | | Image(const Image& other) = delete; |
101 | | Image& operator=(const Image& other) = delete; |
102 | | |
103 | | Image& operator=(Image&& other) noexcept; |
104 | 171k | Image(Image&& other) noexcept = default; |
105 | | |
106 | | static StatusOr<Image> Create(JxlMemoryManager* memory_manager, size_t iw, |
107 | | size_t ih, int bitdepth, int nb_chans); |
108 | | |
109 | 273k | JxlMemoryManager* memory_manager() const { return memory_manager_; } |
110 | | |
111 | 13.5k | bool empty() const { |
112 | 13.5k | for (const auto& ch : channel) { |
113 | 1.44k | if (ch.w && ch.h) return false; |
114 | 1.44k | } |
115 | 12.1k | return true; |
116 | 13.5k | } |
117 | | |
118 | | static StatusOr<Image> Clone(const Image& that); |
119 | | |
120 | | void undo_transforms(const weighted::Header& wp_header, |
121 | | jxl::ThreadPool* pool = nullptr); |
122 | | |
123 | | std::string DebugString() const; |
124 | | |
125 | | private: |
126 | | Image(JxlMemoryManager* memory_manager, size_t iw, size_t ih, int bitdepth); |
127 | | JxlMemoryManager* memory_manager_; |
128 | | }; |
129 | | |
130 | | } // namespace jxl |
131 | | |
132 | | #endif // LIB_JXL_MODULAR_MODULAR_IMAGE_H_ |