Coverage Report

Created: 2026-02-14 07:11

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