Coverage Report

Created: 2025-11-16 07:22

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