Coverage Report

Created: 2026-03-12 07:14

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