Coverage Report

Created: 2026-11-12 07:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/modular/transform/palette.cc
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
#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
5.74k
                  const weighted::Header &wp_header, ThreadPool *pool) {
31
5.74k
  JxlMemoryManager *memory_manager = input.memory_manager();
32
5.74k
  if (input.nb_meta_channels < 1) {
33
0
    return JXL_FAILURE("Error: Palette transform without palette.");
34
0
  }
35
5.74k
  int nb = input.channel[0].h;
36
5.74k
  uint32_t c0 = begin_c + 1;
37
5.74k
  if (c0 >= input.channel.size()) {
38
0
    return JXL_FAILURE("Channel is out of range.");
39
0
  }
40
5.74k
  size_t w = input.channel[c0].w;
41
5.74k
  size_t h = input.channel[c0].h;
42
5.74k
  if (nb < 1) return JXL_FAILURE("Corrupted transforms");
43
13.0k
  for (int i = 1; i < nb; i++) {
44
7.26k
    JXL_ASSIGN_OR_RETURN(Channel c, Channel::Create(memory_manager, w, h,
45
7.26k
                                                    input.channel[c0].hshift,
46
7.26k
                                                    input.channel[c0].vshift));
47
7.26k
    input.channel.insert(input.channel.begin() + c0 + 1, std::move(c));
48
7.26k
  }
49
5.74k
  const Channel &palette = input.channel[0];
50
5.74k
  const pixel_type *JXL_RESTRICT p_palette = input.channel[0].Row(0);
51
5.74k
  ptrdiff_t onerow = input.channel[0].plane.PixelsPerRow();
52
5.74k
  ptrdiff_t onerow_image = input.channel[c0].plane.PixelsPerRow();
53
5.74k
  const int bit_depth = std::min(input.bitdepth, 24);
54
55
5.74k
  if (w == 0) {
56
    // Nothing to do.
57
    // Avoid touching "empty" channels with non-zero height.
58
5.72k
  } else if (nb_deltas == 0 && predictor == Predictor::Zero) {
59
2.06k
    if (nb == 1) {
60
1.13k
      const auto process_row = [&](const uint32_t task,
61
40.8k
                                   size_t /* thread */) -> Status {
62
40.8k
        const size_t y = task;
63
40.8k
        pixel_type *p = input.channel[c0].Row(y);
64
9.55M
        for (size_t x = 0; x < w; x++) {
65
9.51M
          const int index =
66
9.51M
              Clamp1<int>(p[x], 0, static_cast<pixel_type>(palette.w) - 1);
67
9.51M
          p[x] = palette_internal::GetPaletteValue(p_palette, index, /*c=*/0,
68
9.51M
                                                   /*palette_size=*/palette.w,
69
9.51M
                                                   /*onerow=*/onerow,
70
9.51M
                                                   /*bit_depth=*/bit_depth);
71
9.51M
        }
72
40.8k
        return true;
73
40.8k
      };
74
1.13k
      JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, h, ThreadPool::NoInit, process_row,
75
1.13k
                                    "UndoChannelPalette"));
76
1.13k
    } else {
77
935
      const auto process_row = [&](const uint32_t task,
78
122k
                                   size_t /* thread */) -> Status {
79
122k
        const size_t y = task;
80
122k
        std::vector<pixel_type *> p_out(nb);
81
122k
        const pixel_type *p_index = input.channel[c0].Row(y);
82
551k
        for (int c = 0; c < nb; c++) p_out[c] = input.channel[c0 + c].Row(y);
83
22.8M
        for (size_t x = 0; x < w; x++) {
84
22.7M
          const int index = p_index[x];
85
98.5M
          for (int c = 0; c < nb; c++) {
86
75.8M
            p_out[c][x] = palette_internal::GetPaletteValue(
87
75.8M
                p_palette, index, /*c=*/c,
88
75.8M
                /*palette_size=*/palette.w,
89
75.8M
                /*onerow=*/onerow, /*bit_depth=*/bit_depth);
90
75.8M
          }
91
22.7M
        }
92
122k
        return true;
93
122k
      };
94
935
      JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, h, ThreadPool::NoInit, process_row,
95
935
                                    "UndoPalette"));
96
935
    }
97
3.65k
  } else {
98
    // Parallelized per channel.
99
3.65k
    ImageI indices;
100
3.65k
    ImageI &plane = input.channel[c0].plane;
101
3.65k
    JXL_ASSIGN_OR_RETURN(
102
3.65k
        indices, ImageI::Create(memory_manager, plane.xsize(), plane.ysize()));
103
3.65k
    plane.Swap(indices);
104
3.65k
    if (predictor == Predictor::Weighted) {
105
764
      const auto process_channel = [&](const uint32_t c,
106
2.25k
                                       size_t /* thread */) -> Status {
107
2.25k
        Channel &channel = input.channel[c0 + c];
108
2.25k
        weighted::State wp_state(wp_header, channel.w, channel.h);
109
237k
        for (size_t y = 0; y < channel.h; y++) {
110
235k
          pixel_type *JXL_RESTRICT p = channel.Row(y);
111
235k
          const pixel_type *JXL_RESTRICT idx = indices.Row(y);
112
63.0M
          for (size_t x = 0; x < channel.w; x++) {
113
62.8M
            int index = idx[x];
114
62.8M
            pixel_type_w val = 0;
115
62.8M
            const pixel_type palette_entry = palette_internal::GetPaletteValue(
116
62.8M
                p_palette, index, /*c=*/c,
117
62.8M
                /*palette_size=*/palette.w, /*onerow=*/onerow,
118
62.8M
                /*bit_depth=*/bit_depth);
119
62.8M
            PredictionResult pred = PredictNoTreeWP(
120
62.8M
                channel.w, p + x, onerow_image, x, y, predictor, &wp_state);
121
62.8M
            if (index < static_cast<int32_t>(nb_deltas)) {
122
24.2M
              val = pred.guess + palette_entry;
123
38.5M
            } else {
124
38.5M
              val = palette_entry;
125
38.5M
            }
126
62.8M
            p[x] = val;
127
62.8M
            wp_state.UpdateErrors(p[x], x, y, channel.w);
128
62.8M
          }
129
235k
        }
130
2.25k
        return true;
131
2.25k
      };
132
764
      JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, nb, ThreadPool::NoInit,
133
764
                                    process_channel, "UndoDeltaPaletteWP"));
134
2.89k
    } else {
135
2.89k
      const auto process_channel = [&](const uint32_t c,
136
6.30k
                                       size_t /* thread */) -> Status {
137
6.30k
        Channel &channel = input.channel[c0 + c];
138
479k
        for (size_t y = 0; y < channel.h; y++) {
139
473k
          pixel_type *JXL_RESTRICT p = channel.Row(y);
140
473k
          const pixel_type *JXL_RESTRICT idx = indices.Row(y);
141
86.3M
          for (size_t x = 0; x < channel.w; x++) {
142
85.8M
            int index = idx[x];
143
85.8M
            pixel_type_w val = 0;
144
85.8M
            const pixel_type palette_entry = palette_internal::GetPaletteValue(
145
85.8M
                p_palette, index, /*c=*/c,
146
85.8M
                /*palette_size=*/palette.w,
147
85.8M
                /*onerow=*/onerow, /*bit_depth=*/bit_depth);
148
85.8M
            if (index < static_cast<int32_t>(nb_deltas)) {
149
56.7M
              PredictionResult pred = PredictNoTreeNoWP(
150
56.7M
                  channel.w, p + x, onerow_image, x, y, predictor);
151
56.7M
              val = pred.guess + palette_entry;
152
56.7M
            } else {
153
29.0M
              val = palette_entry;
154
29.0M
            }
155
85.8M
            p[x] = val;
156
85.8M
          }
157
473k
        }
158
6.30k
        return true;
159
6.30k
      };
160
2.89k
      JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, nb, ThreadPool::NoInit,
161
2.89k
                                    process_channel, "UndoDeltaPaletteNoWP"));
162
2.89k
    }
163
3.65k
  }
164
5.74k
  if (c0 >= input.nb_meta_channels) {
165
    // Palette was done on normal channels
166
5.50k
    input.nb_meta_channels--;
167
5.50k
  } else {
168
    // Palette was done on metachannels
169
247
    JXL_ENSURE(static_cast<int>(input.nb_meta_channels) >= 2 - nb);
170
247
    input.nb_meta_channels -= 2 - nb;
171
247
    JXL_ENSURE(begin_c + nb - 1 < input.nb_meta_channels);
172
247
  }
173
5.74k
  input.channel.erase(input.channel.begin(), input.channel.begin() + 1);
174
5.74k
  return true;
175
5.74k
}
176
177
Status MetaPalette(Image &input, uint32_t begin_c, uint32_t end_c,
178
6.14k
                   uint32_t nb_colors, uint32_t nb_deltas, bool lossy) {
179
6.14k
  JXL_RETURN_IF_ERROR(CheckEqualChannels(input, begin_c, end_c));
180
6.08k
  JxlMemoryManager *memory_manager = input.memory_manager();
181
182
6.08k
  size_t nb = end_c - begin_c + 1;
183
6.08k
  if (begin_c >= input.nb_meta_channels) {
184
    // Palette was done on normal channels
185
5.82k
    input.nb_meta_channels++;
186
5.82k
  } else {
187
    // Palette was done on metachannels
188
260
    JXL_ENSURE(end_c < input.nb_meta_channels);
189
    // we remove nb-1 metachannels and add one
190
260
    input.nb_meta_channels += 2 - nb;
191
260
  }
192
6.08k
  input.channel.erase(input.channel.begin() + begin_c + 1,
193
6.08k
                      input.channel.begin() + end_c + 1);
194
6.08k
  JXL_ASSIGN_OR_RETURN(
195
6.08k
      Channel pch, Channel::Create(memory_manager, nb_colors + nb_deltas, nb));
196
6.08k
  pch.hshift = -1;
197
6.08k
  pch.vshift = -1;
198
6.08k
  input.channel.insert(input.channel.begin(), std::move(pch));
199
6.08k
  return true;
200
6.08k
}
201
202
}  // namespace jxl