summaryrefslogtreecommitdiffstats
path: root/third_party/jpeg-xl/lib/jxl/modular/transform
diff options
context:
space:
mode:
Diffstat (limited to 'third_party/jpeg-xl/lib/jxl/modular/transform')
-rw-r--r--third_party/jpeg-xl/lib/jxl/modular/transform/enc_palette.cc38
-rw-r--r--third_party/jpeg-xl/lib/jxl/modular/transform/enc_squeeze.cc49
-rw-r--r--third_party/jpeg-xl/lib/jxl/modular/transform/palette.cc19
-rw-r--r--third_party/jpeg-xl/lib/jxl/modular/transform/palette.h1
-rw-r--r--third_party/jpeg-xl/lib/jxl/modular/transform/squeeze.cc19
-rw-r--r--third_party/jpeg-xl/lib/jxl/modular/transform/squeeze.h2
-rw-r--r--third_party/jpeg-xl/lib/jxl/modular/transform/transform.h14
7 files changed, 87 insertions, 55 deletions
diff --git a/third_party/jpeg-xl/lib/jxl/modular/transform/enc_palette.cc b/third_party/jpeg-xl/lib/jxl/modular/transform/enc_palette.cc
index f5172aa126..24c64f5aad 100644
--- a/third_party/jpeg-xl/lib/jxl/modular/transform/enc_palette.cc
+++ b/third_party/jpeg-xl/lib/jxl/modular/transform/enc_palette.cc
@@ -10,8 +10,8 @@
#include <set>
#include "lib/jxl/base/common.h"
-#include "lib/jxl/base/data_parallel.h"
#include "lib/jxl/base/status.h"
+#include "lib/jxl/image_ops.h"
#include "lib/jxl/modular/encoding/context_predict.h"
#include "lib/jxl/modular/modular_image.h"
#include "lib/jxl/modular/transform/enc_transform.h"
@@ -34,7 +34,8 @@ float ColorDistance(const std::vector<float> &JXL_RESTRICT a,
if (a.size() >= 3) {
ave3 = (a[0] + b[0] + a[1] + b[1] + a[2] + b[2]) * (1.21f / 3.0f);
}
- float sum_a = 0, sum_b = 0;
+ float sum_a = 0;
+ float sum_b = 0;
for (size_t c = 0; c < a.size(); ++c) {
const float difference =
static_cast<float>(a[c]) - static_cast<float>(b[c]);
@@ -132,7 +133,8 @@ struct PaletteIterationData {
delta_frequency.first[1],
delta_frequency.first[2]};
float delta_distance =
- sqrt(palette_internal::ColorDistance({0, 0, 0}, current_delta)) + 1;
+ std::sqrt(palette_internal::ColorDistance({0, 0, 0}, current_delta)) +
+ 1;
delta_frequency.second *= delta_distance * delta_distance_multiplier;
}
@@ -174,7 +176,8 @@ Status FwdPaletteIteration(Image &input, uint32_t begin_c, uint32_t end_c,
// Channel palette special case
if (nb_colors == 0) return false;
std::vector<pixel_type> lookup;
- pixel_type minval, maxval;
+ pixel_type minval;
+ pixel_type maxval;
compute_minmax(input.channel[begin_c], &minval, &maxval);
size_t lookup_table_size =
static_cast<int64_t>(maxval) - static_cast<int64_t>(minval) + 1;
@@ -189,12 +192,12 @@ Status FwdPaletteIteration(Image &input, uint32_t begin_c, uint32_t end_c,
const bool new_color = chpalette.insert(p[x]).second;
if (new_color) {
idx++;
- if (idx > (int)nb_colors) return false;
+ if (idx > static_cast<int>(nb_colors)) return false;
}
}
}
JXL_DEBUG_V(6, "Channel %i uses only %i colors.", begin_c, idx);
- Channel pch(idx, 1);
+ JXL_ASSIGN_OR_RETURN(Channel pch, Channel::Create(idx, 1));
pch.hshift = -1;
pch.vshift = -1;
nb_colors = idx;
@@ -206,9 +209,12 @@ Status FwdPaletteIteration(Image &input, uint32_t begin_c, uint32_t end_c,
for (size_t y = 0; y < h; y++) {
pixel_type *p = input.channel[begin_c].Row(y);
for (size_t x = 0; x < w; x++) {
- for (idx = 0; p[x] != p_palette[idx] && idx < (int)nb_colors; idx++) {
+ for (idx = 0;
+ p[x] != p_palette[idx] && idx < static_cast<int>(nb_colors);
+ idx++) {
+ // no-op
}
- JXL_DASSERT(idx < (int)nb_colors);
+ JXL_DASSERT(idx < static_cast<int>(nb_colors));
p[x] = idx;
}
}
@@ -226,12 +232,12 @@ Status FwdPaletteIteration(Image &input, uint32_t begin_c, uint32_t end_c,
if (lookup[p[x] - minval] == 0) {
lookup[p[x] - minval] = 1;
idx++;
- if (idx > (int)nb_colors) return false;
+ if (idx > static_cast<int>(nb_colors)) return false;
}
}
}
JXL_DEBUG_V(6, "Channel %i uses only %i colors.", begin_c, idx);
- Channel pch(idx, 1);
+ JXL_ASSIGN_OR_RETURN(Channel pch, Channel::Create(idx, 1));
pch.hshift = -1;
pch.vshift = -1;
nb_colors = idx;
@@ -256,7 +262,8 @@ Status FwdPaletteIteration(Image &input, uint32_t begin_c, uint32_t end_c,
Image quantized_input;
if (lossy) {
- quantized_input = Image(w, h, input.bitdepth, nb);
+ JXL_ASSIGN_OR_RETURN(quantized_input,
+ Image::Create(w, h, input.bitdepth, nb));
for (size_t c = 0; c < nb; c++) {
CopyImageTo(input.channel[begin_c + c].plane,
&quantized_input.channel[c].plane);
@@ -337,7 +344,7 @@ Status FwdPaletteIteration(Image &input, uint32_t begin_c, uint32_t end_c,
JXL_DEBUG_V(6, "Channels %i-%i can be represented using a %i-color palette.",
begin_c, end_c, nb_colors);
- Channel pch(nb_colors, nb);
+ JXL_ASSIGN_OR_RETURN(Channel pch, Channel::Create(nb_colors, nb));
pch.hshift = -1;
pch.vshift = -1;
pixel_type *JXL_RESTRICT p_palette = pch.Row(0);
@@ -361,7 +368,8 @@ Status FwdPaletteIteration(Image &input, uint32_t begin_c, uint32_t end_c,
std::sort(candidate_palette_imageorder.begin(),
candidate_palette_imageorder.end(),
[](std::vector<pixel_type> ap, std::vector<pixel_type> bp) {
- float ay, by;
+ float ay;
+ float by;
ay = (0.299f * ap[0] + 0.587f * ap[1] + 0.114f * ap[2] + 0.1f);
if (ap.size() > 3) ay *= 1.f + ap[3];
by = (0.299f * bp[0] + 0.587f * bp[1] + 0.114f * bp[2] + 0.1f);
@@ -420,7 +428,7 @@ Status FwdPaletteIteration(Image &input, uint32_t begin_c, uint32_t end_c,
for (int diffusion_index = 0; diffusion_index < 2; ++diffusion_index) {
for (size_t c = 0; c < nb; c++) {
color_with_error[c] =
- p_in[c][x] + palette_iteration_data.final_run *
+ p_in[c][x] + (palette_iteration_data.final_run ? 1 : 0) *
kDiffusionMultiplier[diffusion_index] *
error_row[0][c][x + 2];
color[c] = Clamp1(lroundf(color_with_error[c]), 0l,
@@ -503,7 +511,7 @@ Status FwdPaletteIteration(Image &input, uint32_t begin_c, uint32_t end_c,
float local_error = color_with_error[c] - best_val[c];
len_error += local_error * local_error;
}
- len_error = sqrt(len_error);
+ len_error = std::sqrt(len_error);
float modulate = 1.0;
int len_limit = 38 << std::max(0, bit_depth - 8);
if (len_error > len_limit) {
diff --git a/third_party/jpeg-xl/lib/jxl/modular/transform/enc_squeeze.cc b/third_party/jpeg-xl/lib/jxl/modular/transform/enc_squeeze.cc
index 489f72a90d..0d924c0ace 100644
--- a/third_party/jpeg-xl/lib/jxl/modular/transform/enc_squeeze.cc
+++ b/third_party/jpeg-xl/lib/jxl/modular/transform/enc_squeeze.cc
@@ -14,15 +14,20 @@
namespace jxl {
-void FwdHSqueeze(Image &input, int c, int rc) {
+#define AVERAGE(X, Y) (((X) + (Y) + (((X) > (Y)) ? 1 : 0)) >> 1)
+
+Status FwdHSqueeze(Image &input, int c, int rc) {
const Channel &chin = input.channel[c];
JXL_DEBUG_V(4, "Doing horizontal squeeze of channel %i to new channel %i", c,
rc);
- Channel chout((chin.w + 1) / 2, chin.h, chin.hshift + 1, chin.vshift);
- Channel chout_residual(chin.w - chout.w, chout.h, chin.hshift + 1,
- chin.vshift);
+ JXL_ASSIGN_OR_RETURN(
+ Channel chout,
+ Channel::Create((chin.w + 1) / 2, chin.h, chin.hshift + 1, chin.vshift));
+ JXL_ASSIGN_OR_RETURN(
+ Channel chout_residual,
+ Channel::Create(chin.w - chout.w, chout.h, chin.hshift + 1, chin.vshift));
for (size_t y = 0; y < chout.h; y++) {
const pixel_type *JXL_RESTRICT p_in = chin.Row(y);
@@ -31,18 +36,19 @@ void FwdHSqueeze(Image &input, int c, int rc) {
for (size_t x = 0; x < chout_residual.w; x++) {
pixel_type A = p_in[x * 2];
pixel_type B = p_in[x * 2 + 1];
- pixel_type avg = (A + B + (A > B)) >> 1;
+ pixel_type avg = AVERAGE(A, B);
p_out[x] = avg;
pixel_type diff = A - B;
pixel_type next_avg = avg;
if (x + 1 < chout_residual.w) {
- next_avg = (p_in[x * 2 + 2] + p_in[x * 2 + 3] +
- (p_in[x * 2 + 2] > p_in[x * 2 + 3])) >>
- 1; // which will be chout.value(y,x+1)
- } else if (chin.w & 1)
+ pixel_type C = p_in[x * 2 + 2];
+ pixel_type D = p_in[x * 2 + 3];
+ next_avg = AVERAGE(C, D); // which will be chout.value(y,x+1)
+ } else if (chin.w & 1) {
next_avg = p_in[x * 2 + 2];
+ }
pixel_type left = (x > 0 ? p_in[x * 2 - 1] : avg);
pixel_type tendency = SmoothTendency(left, avg, next_avg);
@@ -55,17 +61,21 @@ void FwdHSqueeze(Image &input, int c, int rc) {
}
input.channel[c] = std::move(chout);
input.channel.insert(input.channel.begin() + rc, std::move(chout_residual));
+ return true;
}
-void FwdVSqueeze(Image &input, int c, int rc) {
+Status FwdVSqueeze(Image &input, int c, int rc) {
const Channel &chin = input.channel[c];
JXL_DEBUG_V(4, "Doing vertical squeeze of channel %i to new channel %i", c,
rc);
- Channel chout(chin.w, (chin.h + 1) / 2, chin.hshift, chin.vshift + 1);
- Channel chout_residual(chin.w, chin.h - chout.h, chin.hshift,
- chin.vshift + 1);
+ JXL_ASSIGN_OR_RETURN(
+ Channel chout,
+ Channel::Create(chin.w, (chin.h + 1) / 2, chin.hshift, chin.vshift + 1));
+ JXL_ASSIGN_OR_RETURN(
+ Channel chout_residual,
+ Channel::Create(chin.w, chin.h - chout.h, chin.hshift, chin.vshift + 1));
intptr_t onerow_in = chin.plane.PixelsPerRow();
for (size_t y = 0; y < chout_residual.h; y++) {
const pixel_type *JXL_RESTRICT p_in = chin.Row(y * 2);
@@ -74,16 +84,16 @@ void FwdVSqueeze(Image &input, int c, int rc) {
for (size_t x = 0; x < chout.w; x++) {
pixel_type A = p_in[x];
pixel_type B = p_in[x + onerow_in];
- pixel_type avg = (A + B + (A > B)) >> 1;
+ pixel_type avg = AVERAGE(A, B);
p_out[x] = avg;
pixel_type diff = A - B;
pixel_type next_avg = avg;
if (y + 1 < chout_residual.h) {
- next_avg = (p_in[x + 2 * onerow_in] + p_in[x + 3 * onerow_in] +
- (p_in[x + 2 * onerow_in] > p_in[x + 3 * onerow_in])) >>
- 1; // which will be chout.value(y+1,x)
+ pixel_type C = p_in[x + 2 * onerow_in];
+ pixel_type D = p_in[x + 3 * onerow_in];
+ next_avg = AVERAGE(C, D); // which will be chout.value(y+1,x)
} else if (chin.h & 1) {
next_avg = p_in[x + 2 * onerow_in];
}
@@ -104,6 +114,7 @@ void FwdVSqueeze(Image &input, int c, int rc) {
}
input.channel[c] = std::move(chout);
input.channel.insert(input.channel.begin() + rc, std::move(chout_residual));
+ return true;
}
Status FwdSqueeze(Image &input, std::vector<SqueezeParams> parameters,
@@ -128,9 +139,9 @@ Status FwdSqueeze(Image &input, std::vector<SqueezeParams> parameters,
}
for (uint32_t c = beginc; c <= endc; c++) {
if (horizontal) {
- FwdHSqueeze(input, c, offset + c - beginc);
+ JXL_RETURN_IF_ERROR(FwdHSqueeze(input, c, offset + c - beginc));
} else {
- FwdVSqueeze(input, c, offset + c - beginc);
+ JXL_RETURN_IF_ERROR(FwdVSqueeze(input, c, offset + c - beginc));
}
}
}
diff --git a/third_party/jpeg-xl/lib/jxl/modular/transform/palette.cc b/third_party/jpeg-xl/lib/jxl/modular/transform/palette.cc
index bffbacf160..1ab499ccf6 100644
--- a/third_party/jpeg-xl/lib/jxl/modular/transform/palette.cc
+++ b/third_party/jpeg-xl/lib/jxl/modular/transform/palette.cc
@@ -23,9 +23,11 @@ Status InvPalette(Image &input, uint32_t begin_c, uint32_t nb_colors,
size_t h = input.channel[c0].h;
if (nb < 1) return JXL_FAILURE("Corrupted transforms");
for (int i = 1; i < nb; i++) {
- input.channel.insert(
- input.channel.begin() + c0 + 1,
- Channel(w, h, input.channel[c0].hshift, input.channel[c0].vshift));
+ StatusOr<Channel> channel_or = Channel::Create(
+ w, h, input.channel[c0].hshift, input.channel[c0].vshift);
+ JXL_RETURN_IF_ERROR(channel_or.status());
+ input.channel.insert(input.channel.begin() + c0 + 1,
+ std::move(channel_or).value());
}
const Channel &palette = input.channel[0];
const pixel_type *JXL_RESTRICT p_palette = input.channel[0].Row(0);
@@ -44,7 +46,8 @@ Status InvPalette(Image &input, uint32_t begin_c, uint32_t nb_colors,
const size_t y = task;
pixel_type *p = input.channel[c0].Row(y);
for (size_t x = 0; x < w; x++) {
- const int index = Clamp1<int>(p[x], 0, (pixel_type)palette.w - 1);
+ const int index =
+ Clamp1<int>(p[x], 0, static_cast<pixel_type>(palette.w) - 1);
p[x] = palette_internal::GetPaletteValue(
p_palette, index, /*c=*/0,
/*palette_size=*/palette.w,
@@ -75,8 +78,10 @@ Status InvPalette(Image &input, uint32_t begin_c, uint32_t nb_colors,
}
} else {
// Parallelized per channel.
- ImageI indices = std::move(input.channel[c0].plane);
- input.channel[c0].plane = ImageI(indices.xsize(), indices.ysize());
+ ImageI indices;
+ ImageI &plane = input.channel[c0].plane;
+ JXL_ASSIGN_OR_RETURN(indices, ImageI::Create(plane.xsize(), plane.ysize()));
+ plane.Swap(indices);
if (predictor == Predictor::Weighted) {
JXL_RETURN_IF_ERROR(RunOnPool(
pool, 0, nb, ThreadPool::NoInit,
@@ -167,7 +172,7 @@ Status MetaPalette(Image &input, uint32_t begin_c, uint32_t end_c,
}
input.channel.erase(input.channel.begin() + begin_c + 1,
input.channel.begin() + end_c + 1);
- Channel pch(nb_colors + nb_deltas, nb);
+ JXL_ASSIGN_OR_RETURN(Channel pch, Channel::Create(nb_colors + nb_deltas, nb));
pch.hshift = -1;
pch.vshift = -1;
input.channel.insert(input.channel.begin(), std::move(pch));
diff --git a/third_party/jpeg-xl/lib/jxl/modular/transform/palette.h b/third_party/jpeg-xl/lib/jxl/modular/transform/palette.h
index 279ef04568..e0405a2162 100644
--- a/third_party/jpeg-xl/lib/jxl/modular/transform/palette.h
+++ b/third_party/jpeg-xl/lib/jxl/modular/transform/palette.h
@@ -101,6 +101,7 @@ GetPaletteValue(const pixel_type *const palette, int index, const size_t c,
// index >= kLargeCube ** 3 ?
switch (c) {
case 0:
+ default:
break;
case 1:
index /= kLargeCube;
diff --git a/third_party/jpeg-xl/lib/jxl/modular/transform/squeeze.cc b/third_party/jpeg-xl/lib/jxl/modular/transform/squeeze.cc
index e9892ea48f..580829741a 100644
--- a/third_party/jpeg-xl/lib/jxl/modular/transform/squeeze.cc
+++ b/third_party/jpeg-xl/lib/jxl/modular/transform/squeeze.cc
@@ -113,7 +113,9 @@ Status InvHSqueeze(Image &input, uint32_t c, uint32_t rc, ThreadPool *pool) {
}
// Note: chin.w >= chin_residual.w and at most 1 different.
- Channel chout(chin.w + chin_residual.w, chin.h, chin.hshift - 1, chin.vshift);
+ JXL_ASSIGN_OR_RETURN(Channel chout,
+ Channel::Create(chin.w + chin_residual.w, chin.h,
+ chin.hshift - 1, chin.vshift));
JXL_DEBUG_V(4,
"Undoing horizontal squeeze of channel %i using residuals in "
"channel %i (going from width %" PRIuS " to %" PRIuS ")",
@@ -222,7 +224,9 @@ Status InvVSqueeze(Image &input, uint32_t c, uint32_t rc, ThreadPool *pool) {
}
// Note: chin.h >= chin_residual.h and at most 1 different.
- Channel chout(chin.w, chin.h + chin_residual.h, chin.hshift, chin.vshift - 1);
+ JXL_ASSIGN_OR_RETURN(Channel chout,
+ Channel::Create(chin.w, chin.h + chin_residual.h,
+ chin.hshift, chin.vshift - 1));
JXL_DEBUG_V(
4,
"Undoing vertical squeeze of channel %i using residuals in channel "
@@ -238,7 +242,8 @@ Status InvVSqueeze(Image &input, uint32_t c, uint32_t rc, ThreadPool *pool) {
static constexpr const int kColsPerThread = 64;
const auto unsqueeze_slice = [&](const uint32_t task, size_t /* thread */) {
const size_t x0 = task * kColsPerThread;
- const size_t x1 = std::min((size_t)(task + 1) * kColsPerThread, chin.w);
+ const size_t x1 =
+ std::min(static_cast<size_t>(task + 1) * kColsPerThread, chin.w);
const size_t w = x1 - x0;
// We only iterate up to std::min(chin_residual.h, chin.h) which is
// always chin_residual.h.
@@ -289,7 +294,7 @@ Status InvVSqueeze(Image &input, uint32_t c, uint32_t rc, ThreadPool *pool) {
return true;
}
-Status InvSqueeze(Image &input, std::vector<SqueezeParams> parameters,
+Status InvSqueeze(Image &input, const std::vector<SqueezeParams> &parameters,
ThreadPool *pool) {
for (int i = parameters.size() - 1; i >= 0; i--) {
JXL_RETURN_IF_ERROR(
@@ -340,7 +345,7 @@ HWY_AFTER_NAMESPACE();
namespace jxl {
HWY_EXPORT(InvSqueeze);
-Status InvSqueeze(Image &input, std::vector<SqueezeParams> parameters,
+Status InvSqueeze(Image &input, const std::vector<SqueezeParams> &parameters,
ThreadPool *pool) {
return HWY_DYNAMIC_DISPATCH(InvSqueeze)(input, parameters, pool);
}
@@ -459,8 +464,8 @@ Status MetaSqueeze(Image &image, std::vector<SqueezeParams> *parameters) {
if (image.channel[c].vshift >= 0) image.channel[c].vshift++;
h = h - (h + 1) / 2;
}
- image.channel[c].shrink();
- Channel placeholder(w, h);
+ JXL_RETURN_IF_ERROR(image.channel[c].shrink());
+ JXL_ASSIGN_OR_RETURN(Channel placeholder, Channel::Create(w, h));
placeholder.hshift = image.channel[c].hshift;
placeholder.vshift = image.channel[c].vshift;
diff --git a/third_party/jpeg-xl/lib/jxl/modular/transform/squeeze.h b/third_party/jpeg-xl/lib/jxl/modular/transform/squeeze.h
index 305a0ca3ec..bbd16c59c0 100644
--- a/third_party/jpeg-xl/lib/jxl/modular/transform/squeeze.h
+++ b/third_party/jpeg-xl/lib/jxl/modular/transform/squeeze.h
@@ -81,7 +81,7 @@ Status CheckMetaSqueezeParams(const SqueezeParams &parameter, int num_channels);
Status MetaSqueeze(Image &image, std::vector<SqueezeParams> *parameters);
-Status InvSqueeze(Image &input, std::vector<SqueezeParams> parameters,
+Status InvSqueeze(Image &input, const std::vector<SqueezeParams> &parameters,
ThreadPool *pool);
} // namespace jxl
diff --git a/third_party/jpeg-xl/lib/jxl/modular/transform/transform.h b/third_party/jpeg-xl/lib/jxl/modular/transform/transform.h
index d5d3259f7a..b68861706f 100644
--- a/third_party/jpeg-xl/lib/jxl/modular/transform/transform.h
+++ b/third_party/jpeg-xl/lib/jxl/modular/transform/transform.h
@@ -77,11 +77,13 @@ class Transform : public Fields {
Transform() : Transform(TransformId::kInvalid) {}
Status VisitFields(Visitor *JXL_RESTRICT visitor) override {
- JXL_QUIET_RETURN_IF_ERROR(visitor->U32(
- Val((uint32_t)TransformId::kRCT), Val((uint32_t)TransformId::kPalette),
- Val((uint32_t)TransformId::kSqueeze),
- Val((uint32_t)TransformId::kInvalid), (uint32_t)TransformId::kRCT,
- reinterpret_cast<uint32_t *>(&id)));
+ JXL_QUIET_RETURN_IF_ERROR(
+ visitor->U32(Val(static_cast<uint32_t>(TransformId::kRCT)),
+ Val(static_cast<uint32_t>(TransformId::kPalette)),
+ Val(static_cast<uint32_t>(TransformId::kSqueeze)),
+ Val(static_cast<uint32_t>(TransformId::kInvalid)),
+ static_cast<uint32_t>(TransformId::kRCT),
+ reinterpret_cast<uint32_t *>(&id)));
if (id == TransformId::kInvalid) {
return JXL_FAILURE("Invalid transform ID");
}
@@ -109,7 +111,7 @@ class Transform : public Fields {
visitor->U32(Val(0), BitsOffset(8, 1), BitsOffset(10, 257),
BitsOffset(16, 1281), 0, &nb_deltas));
JXL_QUIET_RETURN_IF_ERROR(
- visitor->Bits(4, (uint32_t)Predictor::Zero,
+ visitor->Bits(4, static_cast<uint32_t>(Predictor::Zero),
reinterpret_cast<uint32_t *>(&predictor)));
if (predictor >= Predictor::Best) {
return JXL_FAILURE("Invalid predictor");