/src/libjxl/lib/jxl/modular/modular_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_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 | 888k | size_t ih, int hsh = 0, int vsh = 0) { |
44 | 888k | JXL_ASSIGN_OR_RETURN(Plane<pixel_type> plane, |
45 | 888k | Plane<pixel_type>::Create(memory_manager, iw, ih)); |
46 | 888k | return Channel(std::move(plane), iw, ih, hsh, vsh); |
47 | 888k | } |
48 | | |
49 | | // Move assignment |
50 | 27.2M | Channel& operator=(Channel&& other) noexcept { |
51 | 27.2M | w = other.w; |
52 | 27.2M | h = other.h; |
53 | 27.2M | hshift = other.hshift; |
54 | 27.2M | vshift = other.vshift; |
55 | 27.2M | plane = std::move(other.plane); |
56 | 27.2M | return *this; |
57 | 27.2M | } |
58 | | |
59 | | // Move constructor |
60 | 3.02M | Channel(Channel&& other) noexcept = default; |
61 | | |
62 | 310k | JxlMemoryManager* memory_manager() const { return plane.memory_manager(); }; |
63 | | |
64 | 380k | Status shrink() { |
65 | 380k | if (plane.xsize() == w && plane.ysize() == h) return true; |
66 | 310k | JXL_ASSIGN_OR_RETURN(plane, |
67 | 310k | Plane<pixel_type>::Create(memory_manager(), w, h)); |
68 | 310k | return true; |
69 | 310k | } |
70 | 47.6k | Status shrink(int nw, int nh) { |
71 | 47.6k | w = nw; |
72 | 47.6k | h = nh; |
73 | 47.6k | return shrink(); |
74 | 47.6k | } |
75 | | |
76 | 27.3M | JXL_INLINE pixel_type* Row(const size_t y) { return plane.Row(y); } |
77 | 31.8M | JXL_INLINE const pixel_type* Row(const size_t y) const { |
78 | 31.8M | return plane.Row(y); |
79 | 31.8M | } |
80 | | |
81 | | private: |
82 | | Channel(jxl::Plane<pixel_type>&& p, size_t iw, size_t ih, int hsh, int vsh) |
83 | 887k | : plane(std::move(p)), w(iw), h(ih), hshift(hsh), vshift(vsh) {} |
84 | | }; |
85 | | |
86 | | class Transform; |
87 | | |
88 | | class Image { |
89 | | public: |
90 | | // image data, transforms can dramatically change the number of channels and |
91 | | // their semantics |
92 | | std::vector<Channel> channel; |
93 | | // transforms that have been applied (and that have to be undone) |
94 | | std::vector<Transform> transform; |
95 | | |
96 | | // image dimensions (channels may have different dimensions due to transforms) |
97 | | size_t w, h; |
98 | | int bitdepth; |
99 | | size_t nb_meta_channels; // first few channels might contain palette(s) |
100 | | bool error; // true if a fatal error occurred, false otherwise |
101 | | |
102 | | explicit Image(JxlMemoryManager* memory_manager); |
103 | | |
104 | | Image(const Image& other) = delete; |
105 | | Image& operator=(const Image& other) = delete; |
106 | | |
107 | | Image& operator=(Image&& other) noexcept; |
108 | 301k | Image(Image&& other) noexcept = default; |
109 | | |
110 | | static StatusOr<Image> Create(JxlMemoryManager* memory_manager, size_t iw, |
111 | | size_t ih, int bitdepth, int nb_chans); |
112 | | |
113 | 849k | JxlMemoryManager* memory_manager() const { return memory_manager_; } |
114 | | |
115 | 0 | bool empty() const { |
116 | 0 | for (const auto& ch : channel) { |
117 | 0 | if (ch.w && ch.h) return false; |
118 | 0 | } |
119 | 0 | return true; |
120 | 0 | } |
121 | | |
122 | | static StatusOr<Image> Clone(const Image& that); |
123 | | |
124 | | void undo_transforms(const weighted::Header& wp_header, |
125 | | jxl::ThreadPool* pool = nullptr); |
126 | | |
127 | | std::string DebugString() const; |
128 | | |
129 | | private: |
130 | | Image(JxlMemoryManager* memory_manager, size_t iw, size_t ih, int bitdepth); |
131 | | JxlMemoryManager* memory_manager_; |
132 | | }; |
133 | | |
134 | | } // namespace jxl |
135 | | |
136 | | #endif // LIB_JXL_MODULAR_MODULAR_IMAGE_H_ |