/src/libjxl/lib/jxl/modular/transform/palette.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/transform/palette.h" |
7 | | |
8 | | #include <jxl/memory_manager.h> |
9 | | |
10 | | #include <algorithm> |
11 | | #include <cstddef> |
12 | | #include <cstdint> |
13 | | #include <utility> |
14 | | #include <vector> |
15 | | |
16 | | #include "lib/jxl/base/common.h" |
17 | | #include "lib/jxl/base/compiler_specific.h" |
18 | | #include "lib/jxl/base/data_parallel.h" |
19 | | #include "lib/jxl/base/status.h" |
20 | | #include "lib/jxl/image.h" |
21 | | #include "lib/jxl/modular/encoding/context_predict.h" |
22 | | #include "lib/jxl/modular/modular_image.h" |
23 | | #include "lib/jxl/modular/options.h" |
24 | | #include "lib/jxl/modular/transform/transform.h" // CheckEqualChannels |
25 | | |
26 | | namespace jxl { |
27 | | |
28 | | Status InvPalette(Image &input, uint32_t begin_c, uint32_t nb_colors, |
29 | | uint32_t nb_deltas, Predictor predictor, |
30 | 676 | const weighted::Header &wp_header, ThreadPool *pool) { |
31 | 676 | JxlMemoryManager *memory_manager = input.memory_manager(); |
32 | 676 | if (input.nb_meta_channels < 1) { |
33 | 0 | return JXL_FAILURE("Error: Palette transform without palette."); |
34 | 0 | } |
35 | 676 | int nb = input.channel[0].h; |
36 | 676 | uint32_t c0 = begin_c + 1; |
37 | 676 | if (c0 >= input.channel.size()) { |
38 | 0 | return JXL_FAILURE("Channel is out of range."); |
39 | 0 | } |
40 | 676 | size_t w = input.channel[c0].w; |
41 | 676 | size_t h = input.channel[c0].h; |
42 | 676 | if (nb < 1) return JXL_FAILURE("Corrupted transforms"); |
43 | 1.09k | for (int i = 1; i < nb; i++) { |
44 | 419 | JXL_ASSIGN_OR_RETURN(Channel c, Channel::Create(memory_manager, w, h, |
45 | 419 | input.channel[c0].hshift, |
46 | 419 | input.channel[c0].vshift)); |
47 | 419 | input.channel.insert(input.channel.begin() + c0 + 1, std::move(c)); |
48 | 419 | } |
49 | 676 | const Channel &palette = input.channel[0]; |
50 | 676 | const pixel_type *JXL_RESTRICT p_palette = input.channel[0].Row(0); |
51 | 676 | intptr_t onerow = input.channel[0].plane.PixelsPerRow(); |
52 | 676 | intptr_t onerow_image = input.channel[c0].plane.PixelsPerRow(); |
53 | 676 | const int bit_depth = std::min(input.bitdepth, 24); |
54 | | |
55 | 676 | if (w == 0) { |
56 | | // Nothing to do. |
57 | | // Avoid touching "empty" channels with non-zero height. |
58 | 660 | } else if (nb_deltas == 0 && predictor == Predictor::Zero) { |
59 | 167 | if (nb == 1) { |
60 | 116 | const auto process_row = [&](const uint32_t task, |
61 | 7.65k | size_t /* thread */) -> Status { |
62 | 7.65k | const size_t y = task; |
63 | 7.65k | pixel_type *p = input.channel[c0].Row(y); |
64 | 2.24M | for (size_t x = 0; x < w; x++) { |
65 | 2.24M | const int index = |
66 | 2.24M | Clamp1<int>(p[x], 0, static_cast<pixel_type>(palette.w) - 1); |
67 | 2.24M | p[x] = palette_internal::GetPaletteValue(p_palette, index, /*c=*/0, |
68 | 2.24M | /*palette_size=*/palette.w, |
69 | 2.24M | /*onerow=*/onerow, |
70 | 2.24M | /*bit_depth=*/bit_depth); |
71 | 2.24M | } |
72 | 7.65k | return true; |
73 | 7.65k | }; |
74 | 116 | JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, h, ThreadPool::NoInit, process_row, |
75 | 116 | "UndoChannelPalette")); |
76 | 116 | } else { |
77 | 51 | const auto process_row = [&](const uint32_t task, |
78 | 920 | size_t /* thread */) -> Status { |
79 | 920 | const size_t y = task; |
80 | 920 | std::vector<pixel_type *> p_out(nb); |
81 | 920 | const pixel_type *p_index = input.channel[c0].Row(y); |
82 | 3.68k | for (int c = 0; c < nb; c++) p_out[c] = input.channel[c0 + c].Row(y); |
83 | 107k | for (size_t x = 0; x < w; x++) { |
84 | 106k | const int index = p_index[x]; |
85 | 453k | for (int c = 0; c < nb; c++) { |
86 | 347k | p_out[c][x] = palette_internal::GetPaletteValue( |
87 | 347k | p_palette, index, /*c=*/c, |
88 | 347k | /*palette_size=*/palette.w, |
89 | 347k | /*onerow=*/onerow, /*bit_depth=*/bit_depth); |
90 | 347k | } |
91 | 106k | } |
92 | 920 | return true; |
93 | 920 | }; |
94 | 51 | JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, h, ThreadPool::NoInit, process_row, |
95 | 51 | "UndoPalette")); |
96 | 51 | } |
97 | 493 | } else { |
98 | | // Parallelized per channel. |
99 | 493 | ImageI indices; |
100 | 493 | ImageI &plane = input.channel[c0].plane; |
101 | 493 | JXL_ASSIGN_OR_RETURN( |
102 | 493 | indices, ImageI::Create(memory_manager, plane.xsize(), plane.ysize())); |
103 | 493 | plane.Swap(indices); |
104 | 493 | if (predictor == Predictor::Weighted) { |
105 | 108 | const auto process_channel = [&](const uint32_t c, |
106 | 170 | size_t /* thread */) -> Status { |
107 | 170 | Channel &channel = input.channel[c0 + c]; |
108 | 170 | weighted::State wp_state(wp_header, channel.w, channel.h); |
109 | 3.03k | for (size_t y = 0; y < channel.h; y++) { |
110 | 2.86k | pixel_type *JXL_RESTRICT p = channel.Row(y); |
111 | 2.86k | const pixel_type *JXL_RESTRICT idx = indices.Row(y); |
112 | 172k | for (size_t x = 0; x < channel.w; x++) { |
113 | 169k | int index = idx[x]; |
114 | 169k | pixel_type_w val = 0; |
115 | 169k | const pixel_type palette_entry = palette_internal::GetPaletteValue( |
116 | 169k | p_palette, index, /*c=*/c, |
117 | 169k | /*palette_size=*/palette.w, /*onerow=*/onerow, |
118 | 169k | /*bit_depth=*/bit_depth); |
119 | 169k | if (index < static_cast<int32_t>(nb_deltas)) { |
120 | 57.4k | PredictionResult pred = PredictNoTreeWP( |
121 | 57.4k | channel.w, p + x, onerow_image, x, y, predictor, &wp_state); |
122 | 57.4k | val = pred.guess + palette_entry; |
123 | 112k | } else { |
124 | 112k | val = palette_entry; |
125 | 112k | } |
126 | 169k | p[x] = val; |
127 | 169k | wp_state.UpdateErrors(p[x], x, y, channel.w); |
128 | 169k | } |
129 | 2.86k | } |
130 | 170 | return true; |
131 | 170 | }; |
132 | 108 | JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, nb, ThreadPool::NoInit, |
133 | 108 | process_channel, "UndoDeltaPaletteWP")); |
134 | 385 | } else { |
135 | 385 | const auto process_channel = [&](const uint32_t c, |
136 | 645 | size_t /* thread */) -> Status { |
137 | 645 | Channel &channel = input.channel[c0 + c]; |
138 | 9.43k | for (size_t y = 0; y < channel.h; y++) { |
139 | 8.78k | pixel_type *JXL_RESTRICT p = channel.Row(y); |
140 | 8.78k | const pixel_type *JXL_RESTRICT idx = indices.Row(y); |
141 | 694k | for (size_t x = 0; x < channel.w; x++) { |
142 | 685k | int index = idx[x]; |
143 | 685k | pixel_type_w val = 0; |
144 | 685k | const pixel_type palette_entry = palette_internal::GetPaletteValue( |
145 | 685k | p_palette, index, /*c=*/c, |
146 | 685k | /*palette_size=*/palette.w, |
147 | 685k | /*onerow=*/onerow, /*bit_depth=*/bit_depth); |
148 | 685k | if (index < static_cast<int32_t>(nb_deltas)) { |
149 | 563k | PredictionResult pred = PredictNoTreeNoWP( |
150 | 563k | channel.w, p + x, onerow_image, x, y, predictor); |
151 | 563k | val = pred.guess + palette_entry; |
152 | 563k | } else { |
153 | 122k | val = palette_entry; |
154 | 122k | } |
155 | 685k | p[x] = val; |
156 | 685k | } |
157 | 8.78k | } |
158 | 645 | return true; |
159 | 645 | }; |
160 | 385 | JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, nb, ThreadPool::NoInit, |
161 | 385 | process_channel, "UndoDeltaPaletteNoWP")); |
162 | 385 | } |
163 | 493 | } |
164 | 676 | if (c0 >= input.nb_meta_channels) { |
165 | | // Palette was done on normal channels |
166 | 640 | input.nb_meta_channels--; |
167 | 640 | } else { |
168 | | // Palette was done on metachannels |
169 | 36 | JXL_ENSURE(static_cast<int>(input.nb_meta_channels) >= 2 - nb); |
170 | 36 | input.nb_meta_channels -= 2 - nb; |
171 | 36 | JXL_ENSURE(begin_c + nb - 1 < input.nb_meta_channels); |
172 | 36 | } |
173 | 676 | input.channel.erase(input.channel.begin(), input.channel.begin() + 1); |
174 | 676 | return true; |
175 | 676 | } |
176 | | |
177 | | Status MetaPalette(Image &input, uint32_t begin_c, uint32_t end_c, |
178 | 1.05k | uint32_t nb_colors, uint32_t nb_deltas, bool lossy) { |
179 | 1.05k | JXL_RETURN_IF_ERROR(CheckEqualChannels(input, begin_c, end_c)); |
180 | 837 | JxlMemoryManager *memory_manager = input.memory_manager(); |
181 | | |
182 | 837 | size_t nb = end_c - begin_c + 1; |
183 | 837 | if (begin_c >= input.nb_meta_channels) { |
184 | | // Palette was done on normal channels |
185 | 788 | input.nb_meta_channels++; |
186 | 788 | } else { |
187 | | // Palette was done on metachannels |
188 | 49 | JXL_ENSURE(end_c < input.nb_meta_channels); |
189 | | // we remove nb-1 metachannels and add one |
190 | 49 | input.nb_meta_channels += 2 - nb; |
191 | 49 | } |
192 | 837 | input.channel.erase(input.channel.begin() + begin_c + 1, |
193 | 837 | input.channel.begin() + end_c + 1); |
194 | 837 | JXL_ASSIGN_OR_RETURN( |
195 | 837 | Channel pch, Channel::Create(memory_manager, nb_colors + nb_deltas, nb)); |
196 | 837 | pch.hshift = -1; |
197 | 837 | pch.vshift = -1; |
198 | 837 | input.channel.insert(input.channel.begin(), std::move(pch)); |
199 | 837 | return true; |
200 | 837 | } |
201 | | |
202 | | } // namespace jxl |