/src/libjxl/lib/jxl/modular/modular_image.cc
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 | | #include "lib/jxl/modular/modular_image.h" |
7 | | |
8 | | #include <jxl/memory_manager.h> |
9 | | |
10 | | #include <cstddef> |
11 | | #include <utility> |
12 | | |
13 | | #include "lib/jxl/base/data_parallel.h" |
14 | | #include "lib/jxl/image.h" |
15 | | |
16 | | #if JXL_DEBUG_V_LEVEL >= 1 |
17 | | #include <sstream> |
18 | | #endif |
19 | | |
20 | | #include "lib/jxl/base/status.h" |
21 | | #include "lib/jxl/image_ops.h" |
22 | | #include "lib/jxl/modular/transform/transform.h" |
23 | | |
24 | | namespace jxl { |
25 | | |
26 | | StatusOr<Channel> Channel::Create(JxlMemoryManager *memory_manager, size_t iw, |
27 | 276k | size_t ih, int hsh, int vsh) { |
28 | 276k | JXL_ASSIGN_OR_RETURN(Plane<pixel_type> plane, |
29 | 276k | Plane<pixel_type>::Create(memory_manager, iw, ih)); |
30 | 276k | return Channel(std::move(plane), iw, ih, hsh, vsh); |
31 | 276k | } |
32 | | |
33 | | void Image::undo_transforms(const weighted::Header &wp_header, |
34 | 25.1k | jxl::ThreadPool *pool) { |
35 | 38.1k | while (!transform.empty()) { |
36 | 13.0k | Transform t = transform.back(); |
37 | 13.0k | JXL_DEBUG_V(4, "Undoing transform"); |
38 | 13.0k | Status result = t.Inverse(*this, wp_header, pool); |
39 | 13.0k | if (result == false) { |
40 | 0 | JXL_NOTIFY_ERROR("Error while undoing transform."); |
41 | 0 | error = true; |
42 | 0 | return; |
43 | 0 | } |
44 | 13.0k | JXL_DEBUG_V(8, "Undoing transform: done"); |
45 | 13.0k | transform.pop_back(); |
46 | 13.0k | } |
47 | 25.1k | } |
48 | | |
49 | | Image::Image(JxlMemoryManager *memory_manager, size_t iw, size_t ih, |
50 | | int bitdepth) |
51 | 81.2k | : w(iw), |
52 | 81.2k | h(ih), |
53 | 81.2k | bitdepth(bitdepth), |
54 | 81.2k | nb_meta_channels(0), |
55 | 81.2k | error(false), |
56 | 81.2k | memory_manager_(memory_manager) {} |
57 | | |
58 | | StatusOr<Image> Image::Create(JxlMemoryManager *memory_manager, size_t iw, |
59 | 81.2k | size_t ih, int bitdepth, int nb_chans) { |
60 | 81.2k | Image result(memory_manager, iw, ih, bitdepth); |
61 | 177k | for (int i = 0; i < nb_chans; i++) { |
62 | 95.8k | JXL_ASSIGN_OR_RETURN(Channel c, Channel::Create(memory_manager, iw, ih)); |
63 | 95.8k | result.channel.emplace_back(std::move(c)); |
64 | 95.8k | } |
65 | 81.2k | return result; |
66 | 81.2k | } |
67 | | |
68 | | Image::Image(JxlMemoryManager *memory_manager) |
69 | 71.3k | : w(0), |
70 | 71.3k | h(0), |
71 | 71.3k | bitdepth(8), |
72 | 71.3k | nb_meta_channels(0), |
73 | 71.3k | error(true), |
74 | 71.3k | memory_manager_(memory_manager) {} |
75 | | |
76 | 44.5k | Image &Image::operator=(Image &&other) noexcept { |
77 | 44.5k | w = other.w; |
78 | 44.5k | h = other.h; |
79 | 44.5k | bitdepth = other.bitdepth; |
80 | 44.5k | nb_meta_channels = other.nb_meta_channels; |
81 | 44.5k | error = other.error; |
82 | 44.5k | channel = std::move(other.channel); |
83 | 44.5k | transform = std::move(other.transform); |
84 | 44.5k | return *this; |
85 | 44.5k | } |
86 | | |
87 | 0 | StatusOr<Image> Image::Clone(const Image &that) { |
88 | 0 | JxlMemoryManager *memory_manager = that.memory_manager(); |
89 | 0 | Image clone(memory_manager, that.w, that.h, that.bitdepth); |
90 | 0 | clone.nb_meta_channels = that.nb_meta_channels; |
91 | 0 | clone.error = that.error; |
92 | 0 | clone.transform = that.transform; |
93 | 0 | for (const Channel &ch : that.channel) { |
94 | 0 | JXL_ASSIGN_OR_RETURN(Channel a, Channel::Create(memory_manager, ch.w, ch.h, |
95 | 0 | ch.hshift, ch.vshift)); |
96 | 0 | JXL_RETURN_IF_ERROR(CopyImageTo(ch.plane, &a.plane)); |
97 | 0 | clone.channel.push_back(std::move(a)); |
98 | 0 | } |
99 | 0 | return clone; |
100 | 0 | } |
101 | | |
102 | | #if JXL_DEBUG_V_LEVEL >= 1 |
103 | | std::string Image::DebugString() const { |
104 | | std::ostringstream os; |
105 | | os << w << "x" << h << ", depth: " << bitdepth; |
106 | | if (!channel.empty()) { |
107 | | os << ", channels:"; |
108 | | for (size_t i = 0; i < channel.size(); ++i) { |
109 | | os << " " << channel[i].w << "x" << channel[i].h |
110 | | << "(shift: " << channel[i].hshift << "," << channel[i].vshift << ")"; |
111 | | if (i < nb_meta_channels) os << "*"; |
112 | | } |
113 | | } |
114 | | return os.str(); |
115 | | } |
116 | | #endif |
117 | | |
118 | | } // namespace jxl |