Coverage Report

Created: 2026-06-30 07:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libjxl/lib/jxl/enc_external_image.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/enc_external_image.h"
7
8
#include <jxl/memory_manager.h>
9
#include <jxl/types.h>
10
11
#include <cstdint>
12
#include <cstring>
13
#include <utility>
14
15
#include "lib/jxl/base/byte_order.h"
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/float.h"
20
#include "lib/jxl/base/printf_macros.h"
21
#include "lib/jxl/base/span.h"
22
#include "lib/jxl/base/status.h"
23
#include "lib/jxl/color_encoding_internal.h"
24
#include "lib/jxl/image.h"
25
#include "lib/jxl/image_bundle.h"
26
#include "lib/jxl/image_ops.h"
27
28
namespace jxl {
29
namespace {
30
31
11.9k
size_t JxlDataTypeBytes(JxlDataType data_type) {
32
11.9k
  switch (data_type) {
33
4.35k
    case JXL_TYPE_UINT8:
34
4.35k
      return 1;
35
3.52k
    case JXL_TYPE_UINT16:
36
3.52k
      return 2;
37
0
    case JXL_TYPE_FLOAT16:
38
0
      return 2;
39
4.04k
    case JXL_TYPE_FLOAT:
40
4.04k
      return 4;
41
0
    default:
42
0
      return 0;
43
11.9k
  }
44
11.9k
}
45
46
}  // namespace
47
48
Status ConvertFromExternalPlaneNoSizeCheck(const uint8_t* data, size_t xsize,
49
                                           size_t ysize, size_t stride,
50
                                           size_t bits_per_sample,
51
                                           JxlPixelFormat format, size_t c,
52
11.9k
                                           ThreadPool* pool, ImageF* channel) {
53
11.9k
  if (format.data_type == JXL_TYPE_UINT8) {
54
4.35k
    JXL_RETURN_IF_ERROR(bits_per_sample > 0 && bits_per_sample <= 8);
55
7.57k
  } else if (format.data_type == JXL_TYPE_UINT16) {
56
3.52k
    JXL_RETURN_IF_ERROR(bits_per_sample > 8 && bits_per_sample <= 16);
57
4.04k
  } else if (format.data_type != JXL_TYPE_FLOAT16 &&
58
4.04k
             format.data_type != JXL_TYPE_FLOAT) {
59
0
    return JXL_FAILURE("unsupported pixel format data type %d",
60
0
                       format.data_type);
61
0
  }
62
63
11.9k
  JXL_ENSURE(channel->xsize() == xsize);
64
11.9k
  JXL_ENSURE(channel->ysize() == ysize);
65
66
11.9k
  size_t bytes_per_channel = JxlDataTypeBytes(format.data_type);
67
11.9k
  size_t bytes_per_pixel = format.num_channels * bytes_per_channel;
68
11.9k
  size_t bytes_per_row;
69
11.9k
  if (!SafeMul(xsize, bytes_per_pixel, bytes_per_row)) {
70
0
    return JXL_FAILURE("Image dimensions are too large");
71
0
  }
72
11.9k
  JXL_ENSURE(bytes_per_row <= stride);
73
11.9k
  size_t pixel_offset = c * bytes_per_channel;
74
  // Only for uint8/16.
75
11.9k
  float scale = 1.0f / ((1ull << bits_per_sample) - 1);
76
77
11.9k
  const bool little_endian =
78
11.9k
      format.endianness == JXL_LITTLE_ENDIAN ||
79
11.9k
      (format.endianness == JXL_NATIVE_ENDIAN && IsLittleEndian());
80
81
11.9k
  const auto convert_row = [&](const uint32_t task,
82
2.66M
                               size_t /*thread*/) -> Status {
83
2.66M
    const size_t y = task;
84
2.66M
    size_t offset = y * stride + pixel_offset;
85
2.66M
    float* JXL_RESTRICT row_out = channel->Row(y);
86
1.03G
    const auto save_value = [&](size_t index, float value) {
87
1.03G
      row_out[index] = value;
88
1.03G
    };
89
2.66M
    JXL_RETURN_IF_ERROR(LoadFloatRow(data + offset, xsize, bytes_per_pixel,
90
2.66M
                                     format.data_type, little_endian, scale,
91
2.66M
                                     save_value));
92
2.66M
    return true;
93
2.66M
  };
94
11.9k
  JXL_RETURN_IF_ERROR(RunOnPool(pool, 0, static_cast<uint32_t>(ysize),
95
11.9k
                                ThreadPool::NoInit, convert_row,
96
11.9k
                                "ConvertExtraChannel"));
97
11.9k
  return true;
98
11.9k
}
99
100
Status ConvertFromExternal(const uint8_t* data, size_t size, size_t xsize,
101
                           size_t ysize, size_t bits_per_sample,
102
                           JxlPixelFormat format, size_t c, ThreadPool* pool,
103
0
                           ImageF* channel) {
104
0
  size_t bytes_per_channel = JxlDataTypeBytes(format.data_type);
105
0
  size_t bytes_per_pixel;
106
0
  if (!SafeMul(static_cast<size_t>(format.num_channels), bytes_per_channel,
107
0
               bytes_per_pixel)) {
108
0
    return JXL_FAILURE("Invalid format");
109
0
  }
110
0
  size_t last_row_size;
111
0
  if (!SafeMul(xsize, bytes_per_pixel, last_row_size)) {
112
0
    return JXL_FAILURE("Image dimensions are too large");
113
0
  }
114
0
  size_t row_size = last_row_size;
115
0
  if (!SafeRoundUpTo(row_size, format.align, row_size)) {
116
0
    return JXL_FAILURE("Image dimensions are too large");
117
0
  }
118
0
  if (xsize == 0 || ysize == 0) return JXL_FAILURE("Empty image");
119
0
  size_t bytes_to_read;
120
0
  if (!SafeMul(row_size, ysize - 1, bytes_to_read) ||
121
0
      !SafeAdd(bytes_to_read, last_row_size, bytes_to_read)) {
122
0
    return JXL_FAILURE("Image dimensions are too large");
123
0
  }
124
0
  if (size > 0 && size < bytes_to_read) {
125
0
    return JXL_FAILURE("Buffer size is too small, expected: %" PRIuS
126
0
                       " got: %" PRIuS " (Image: %" PRIuS "x%" PRIuS
127
0
                       "x%u, bytes_per_channel: %" PRIuS ")",
128
0
                       bytes_to_read, size, xsize, ysize, format.num_channels,
129
0
                       bytes_per_channel);
130
0
  }
131
  // Too large buffer is likely an application bug, so also fail for that.
132
  // Do allow padding to stride in last row though.
133
0
  if (size > row_size * ysize) {
134
0
    return JXL_FAILURE("Buffer size is too large");
135
0
  }
136
0
  return ConvertFromExternalPlaneNoSizeCheck(
137
0
      data, xsize, ysize, row_size, bits_per_sample, format, c, pool, channel);
138
0
}
139
Status ConvertFromExternal(Span<const uint8_t> bytes, size_t xsize,
140
                           size_t ysize, const ColorEncoding& c_current,
141
                           size_t bits_per_sample, JxlPixelFormat format,
142
0
                           ThreadPool* pool, ImageBundle* ib, bool set_alpha) {
143
0
  JxlMemoryManager* memory_manager = ib->memory_manager();
144
0
  size_t color_channels = c_current.Channels();
145
0
  bool has_alpha = format.num_channels == 2 || format.num_channels == 4;
146
0
  if (format.num_channels < color_channels) {
147
0
    return JXL_FAILURE("Expected %" PRIuS
148
0
                       " color channels, received only %u channels",
149
0
                       color_channels, format.num_channels);
150
0
  }
151
152
0
  JXL_ASSIGN_OR_RETURN(Image3F color,
153
0
                       Image3F::Create(memory_manager, xsize, ysize));
154
0
  for (size_t c = 0; c < color_channels; ++c) {
155
0
    JXL_RETURN_IF_ERROR(ConvertFromExternal(bytes.data(), bytes.size(), xsize,
156
0
                                            ysize, bits_per_sample, format, c,
157
0
                                            pool, &color.Plane(c)));
158
0
  }
159
0
  if (color_channels == 1) {
160
0
    JXL_RETURN_IF_ERROR(CopyImageTo(color.Plane(0), &color.Plane(1)));
161
0
    JXL_RETURN_IF_ERROR(CopyImageTo(color.Plane(0), &color.Plane(2)));
162
0
  }
163
0
  JXL_RETURN_IF_ERROR(ib->SetFromImage(std::move(color), c_current));
164
165
  // Passing an interleaved image with an alpha channel to an image that doesn't
166
  // have alpha channel just discards the passed alpha channel.
167
0
  if (set_alpha) {
168
0
    JXL_ASSIGN_OR_RETURN(ImageF alpha,
169
0
                         ImageF::Create(memory_manager, xsize, ysize));
170
0
    if (has_alpha) {
171
0
      JXL_RETURN_IF_ERROR(ConvertFromExternal(
172
0
          bytes.data(), bytes.size(), xsize, ysize, bits_per_sample, format,
173
0
          format.num_channels - 1, pool, &alpha));
174
0
    } else {
175
      // if alpha is not passed, but it is expected, then assume
176
      // it is all-opaque
177
0
      FillImage(1.0f, &alpha);
178
0
    }
179
0
    JXL_RETURN_IF_ERROR(ib->SetAlpha(std::move(alpha)));
180
0
  }
181
182
0
  return true;
183
0
}
184
185
Status BufferToImageF(const JxlPixelFormat& pixel_format, size_t xsize,
186
                      size_t ysize, const void* buffer, size_t size,
187
0
                      ThreadPool* pool, ImageF* channel) {
188
0
  size_t bitdepth = JxlDataTypeBytes(pixel_format.data_type) * kBitsPerByte;
189
0
  return ConvertFromExternal(reinterpret_cast<const uint8_t*>(buffer), size,
190
0
                             xsize, ysize, bitdepth, pixel_format, 0, pool,
191
0
                             channel);
192
0
}
193
194
Status BufferToImageBundle(const JxlPixelFormat& pixel_format, uint32_t xsize,
195
                           uint32_t ysize, const void* buffer, size_t size,
196
                           jxl::ThreadPool* pool,
197
                           const jxl::ColorEncoding& c_current,
198
0
                           jxl::ImageBundle* ib, bool set_alpha) {
199
0
  size_t bitdepth = JxlDataTypeBytes(pixel_format.data_type) * kBitsPerByte;
200
0
  JXL_RETURN_IF_ERROR(ConvertFromExternal(
201
0
      jxl::Bytes(static_cast<const uint8_t*>(buffer), size), xsize, ysize,
202
0
      c_current, bitdepth, pixel_format, pool, ib, set_alpha));
203
0
  JXL_RETURN_IF_ERROR(ib->VerifyMetadata());
204
205
0
  return true;
206
0
}
207
208
}  // namespace jxl