Coverage Report

Created: 2025-08-12 07:37

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