1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
// Copyright (c) the JPEG XL Project Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
#include <jxl/codestream_header.h>
#include <jxl/color_encoding.h>
#include <jxl/encode.h>
#include <jxl/types.h>
#include <stddef.h>
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <memory>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#include "lib/extras/common.h"
#include "lib/extras/dec/color_hints.h"
#include "lib/extras/dec/decode.h"
#include "lib/extras/dec/pnm.h"
#include "lib/extras/enc/encode.h"
#include "lib/extras/packed_image.h"
#include "lib/jxl/base/byte_order.h"
#include "lib/jxl/base/data_parallel.h"
#include "lib/jxl/base/random.h"
#include "lib/jxl/base/span.h"
#include "lib/jxl/base/status.h"
#include "lib/jxl/color_encoding_internal.h"
#include "lib/jxl/test_utils.h"
#include "lib/jxl/testing.h"
namespace jxl {
using test::ThreadPoolForTests;
namespace extras {
namespace {
std::string ExtensionFromCodec(Codec codec, const bool is_gray,
const bool has_alpha,
const size_t bits_per_sample) {
switch (codec) {
case Codec::kJPG:
return ".jpg";
case Codec::kPGX:
return ".pgx";
case Codec::kPNG:
return ".png";
case Codec::kPNM:
if (bits_per_sample == 32) return ".pfm";
if (has_alpha) return ".pam";
return is_gray ? ".pgm" : ".ppm";
case Codec::kEXR:
return ".exr";
default:
return std::string();
}
}
void VerifySameImage(const PackedImage& im0, size_t bits_per_sample0,
const PackedImage& im1, size_t bits_per_sample1,
bool lossless = true) {
ASSERT_EQ(im0.xsize, im1.xsize);
ASSERT_EQ(im0.ysize, im1.ysize);
ASSERT_EQ(im0.format.num_channels, im1.format.num_channels);
auto get_factor = [](JxlPixelFormat f, size_t bits) -> double {
return 1.0 / ((1u << std::min(test::GetPrecision(f.data_type), bits)) - 1);
};
double factor0 = get_factor(im0.format, bits_per_sample0);
double factor1 = get_factor(im1.format, bits_per_sample1);
const auto* pixels0 = static_cast<const uint8_t*>(im0.pixels());
const auto* pixels1 = static_cast<const uint8_t*>(im1.pixels());
auto rgba0 =
test::ConvertToRGBA32(pixels0, im0.xsize, im0.ysize, im0.format, factor0);
auto rgba1 =
test::ConvertToRGBA32(pixels1, im1.xsize, im1.ysize, im1.format, factor1);
double tolerance =
lossless ? 0.5 * std::min(factor0, factor1) : 3.0f / 255.0f;
if (bits_per_sample0 == 32 || bits_per_sample1 == 32) {
tolerance = 0.5 * std::max(factor0, factor1);
}
for (size_t y = 0; y < im0.ysize; ++y) {
for (size_t x = 0; x < im0.xsize; ++x) {
for (size_t c = 0; c < im0.format.num_channels; ++c) {
size_t ix = (y * im0.xsize + x) * 4 + c;
double val0 = rgba0[ix];
double val1 = rgba1[ix];
ASSERT_NEAR(val1, val0, tolerance)
<< "y = " << y << " x = " << x << " c = " << c;
}
}
}
}
JxlColorEncoding CreateTestColorEncoding(bool is_gray) {
JxlColorEncoding c;
c.color_space = is_gray ? JXL_COLOR_SPACE_GRAY : JXL_COLOR_SPACE_RGB;
c.white_point = JXL_WHITE_POINT_D65;
c.primaries = JXL_PRIMARIES_P3;
c.rendering_intent = JXL_RENDERING_INTENT_RELATIVE;
c.transfer_function = JXL_TRANSFER_FUNCTION_LINEAR;
// Roundtrip through internal color encoding to fill in primaries and white
// point CIE xy coordinates.
ColorEncoding c_internal;
JXL_CHECK(c_internal.FromExternal(c));
c = c_internal.ToExternal();
return c;
}
std::vector<uint8_t> GenerateICC(JxlColorEncoding color_encoding) {
ColorEncoding c;
JXL_CHECK(c.FromExternal(color_encoding));
JXL_CHECK(!c.ICC().empty());
return c.ICC();
}
void StoreRandomValue(uint8_t* out, Rng* rng, JxlPixelFormat format,
size_t bits_per_sample) {
uint64_t max_val = (1ull << bits_per_sample) - 1;
if (format.data_type == JXL_TYPE_UINT8) {
*out = rng->UniformU(0, max_val);
} else if (format.data_type == JXL_TYPE_UINT16) {
uint32_t val = rng->UniformU(0, max_val);
if (format.endianness == JXL_BIG_ENDIAN) {
StoreBE16(val, out);
} else {
StoreLE16(val, out);
}
} else {
ASSERT_EQ(format.data_type, JXL_TYPE_FLOAT);
float val = rng->UniformF(0.0, 1.0);
uint32_t uval;
memcpy(&uval, &val, 4);
if (format.endianness == JXL_BIG_ENDIAN) {
StoreBE32(uval, out);
} else {
StoreLE32(uval, out);
}
}
}
void FillPackedImage(size_t bits_per_sample, PackedImage* image) {
JxlPixelFormat format = image->format;
size_t bytes_per_channel = PackedImage::BitsPerChannel(format.data_type) / 8;
uint8_t* out = static_cast<uint8_t*>(image->pixels());
size_t stride = image->xsize * format.num_channels * bytes_per_channel;
ASSERT_EQ(image->pixels_size, image->ysize * stride);
Rng rng(129);
for (size_t y = 0; y < image->ysize; ++y) {
for (size_t x = 0; x < image->xsize; ++x) {
for (size_t c = 0; c < format.num_channels; ++c) {
StoreRandomValue(out, &rng, format, bits_per_sample);
out += bytes_per_channel;
}
}
}
}
struct TestImageParams {
Codec codec;
size_t xsize;
size_t ysize;
size_t bits_per_sample;
bool is_gray;
bool add_alpha;
bool big_endian;
bool add_extra_channels;
bool ShouldTestRoundtrip() const {
if (codec == Codec::kPNG) {
return bits_per_sample <= 16;
} else if (codec == Codec::kPNM) {
// TODO(szabadka) Make PNM encoder endianness-aware.
return ((bits_per_sample <= 16 && big_endian) ||
(bits_per_sample == 32 && !add_alpha && !big_endian));
} else if (codec == Codec::kPGX) {
return ((bits_per_sample == 8 || bits_per_sample == 16) && is_gray &&
!add_alpha);
} else if (codec == Codec::kEXR) {
#if defined(ADDRESS_SANITIZER) || defined(MEMORY_SANITIZER) || \
defined(THREAD_SANITIZER)
// OpenEXR 2.3 has a memory leak in IlmThread_2_3::ThreadPool
return false;
#else
return bits_per_sample == 32 && !is_gray;
#endif
} else if (codec == Codec::kJPG) {
return bits_per_sample == 8 && !add_alpha;
} else {
return false;
}
}
JxlPixelFormat PixelFormat() const {
JxlPixelFormat format;
format.num_channels = (is_gray ? 1 : 3) + (add_alpha ? 1 : 0);
format.data_type = (bits_per_sample == 32 ? JXL_TYPE_FLOAT
: bits_per_sample > 8 ? JXL_TYPE_UINT16
: JXL_TYPE_UINT8);
format.endianness = big_endian ? JXL_BIG_ENDIAN : JXL_LITTLE_ENDIAN;
format.align = 0;
return format;
}
std::string DebugString() const {
std::ostringstream os;
os << "bps:" << bits_per_sample << " gr:" << is_gray << " al:" << add_alpha
<< " be: " << big_endian << " ec: " << add_extra_channels;
return os.str();
}
};
void CreateTestImage(const TestImageParams& params, PackedPixelFile* ppf) {
ppf->info.xsize = params.xsize;
ppf->info.ysize = params.ysize;
ppf->info.bits_per_sample = params.bits_per_sample;
ppf->info.exponent_bits_per_sample = params.bits_per_sample == 32 ? 8 : 0;
ppf->info.num_color_channels = params.is_gray ? 1 : 3;
ppf->info.alpha_bits = params.add_alpha ? params.bits_per_sample : 0;
ppf->info.alpha_premultiplied = TO_JXL_BOOL(params.codec == Codec::kEXR);
JxlColorEncoding color_encoding = CreateTestColorEncoding(params.is_gray);
ppf->icc = GenerateICC(color_encoding);
ppf->color_encoding = color_encoding;
JXL_ASSIGN_OR_DIE(
PackedFrame frame,
PackedFrame::Create(params.xsize, params.ysize, params.PixelFormat()));
FillPackedImage(params.bits_per_sample, &frame.color);
if (params.add_extra_channels) {
for (size_t i = 0; i < 7; ++i) {
JxlPixelFormat ec_format = params.PixelFormat();
ec_format.num_channels = 1;
JXL_ASSIGN_OR_DIE(
PackedImage ec,
PackedImage::Create(params.xsize, params.ysize, ec_format));
FillPackedImage(params.bits_per_sample, &ec);
frame.extra_channels.emplace_back(std::move(ec));
PackedExtraChannel pec;
pec.ec_info.bits_per_sample = params.bits_per_sample;
pec.ec_info.type = static_cast<JxlExtraChannelType>(i);
ppf->extra_channels_info.emplace_back(std::move(pec));
}
}
ppf->frames.emplace_back(std::move(frame));
}
// Ensures reading a newly written file leads to the same image pixels.
void TestRoundTrip(const TestImageParams& params, ThreadPool* pool) {
if (!params.ShouldTestRoundtrip()) return;
std::string extension = ExtensionFromCodec(
params.codec, params.is_gray, params.add_alpha, params.bits_per_sample);
printf("Codec %s %s\n", extension.c_str(), params.DebugString().c_str());
PackedPixelFile ppf_in;
CreateTestImage(params, &ppf_in);
EncodedImage encoded;
auto encoder = Encoder::FromExtension(extension);
if (!encoder) {
fprintf(stderr, "Skipping test because of missing codec support.\n");
return;
}
ASSERT_TRUE(encoder->Encode(ppf_in, &encoded, pool));
ASSERT_EQ(encoded.bitstreams.size(), 1);
PackedPixelFile ppf_out;
ColorHints color_hints;
if (params.codec == Codec::kPNM || params.codec == Codec::kPGX) {
color_hints.Add("color_space",
params.is_gray ? "Gra_D65_Rel_SRG" : "RGB_D65_SRG_Rel_SRG");
}
ASSERT_TRUE(DecodeBytes(Bytes(encoded.bitstreams[0]), color_hints, &ppf_out));
if (params.codec == Codec::kPNG && ppf_out.icc.empty()) {
// Decoding a PNG may drop the ICC profile if there's a valid cICP chunk.
// Rendering intent is not preserved in this case.
EXPECT_EQ(ppf_in.color_encoding.color_space,
ppf_out.color_encoding.color_space);
EXPECT_EQ(ppf_in.color_encoding.white_point,
ppf_out.color_encoding.white_point);
if (ppf_in.color_encoding.color_space != JXL_COLOR_SPACE_GRAY) {
EXPECT_EQ(ppf_in.color_encoding.primaries,
ppf_out.color_encoding.primaries);
}
EXPECT_EQ(ppf_in.color_encoding.transfer_function,
ppf_out.color_encoding.transfer_function);
EXPECT_EQ(ppf_out.color_encoding.rendering_intent,
JXL_RENDERING_INTENT_RELATIVE);
} else if (params.codec != Codec::kPNM && params.codec != Codec::kPGX &&
params.codec != Codec::kEXR) {
EXPECT_EQ(ppf_in.icc, ppf_out.icc);
}
ASSERT_EQ(ppf_out.frames.size(), 1);
const auto& frame_in = ppf_in.frames[0];
const auto& frame_out = ppf_out.frames[0];
VerifySameImage(frame_in.color, ppf_in.info.bits_per_sample, frame_out.color,
ppf_out.info.bits_per_sample,
/*lossless=*/params.codec != Codec::kJPG);
ASSERT_EQ(frame_in.extra_channels.size(), frame_out.extra_channels.size());
ASSERT_EQ(ppf_out.extra_channels_info.size(),
frame_out.extra_channels.size());
for (size_t i = 0; i < frame_in.extra_channels.size(); ++i) {
VerifySameImage(frame_in.extra_channels[i], ppf_in.info.bits_per_sample,
frame_out.extra_channels[i], ppf_out.info.bits_per_sample,
/*lossless=*/true);
EXPECT_EQ(ppf_out.extra_channels_info[i].ec_info.type,
ppf_in.extra_channels_info[i].ec_info.type);
}
}
TEST(CodecTest, TestRoundTrip) {
ThreadPoolForTests pool(12);
TestImageParams params;
params.xsize = 7;
params.ysize = 4;
for (Codec codec :
{Codec::kPNG, Codec::kPNM, Codec::kPGX, Codec::kEXR, Codec::kJPG}) {
for (int bits_per_sample : {4, 8, 10, 12, 16, 32}) {
for (bool is_gray : {false, true}) {
for (bool add_alpha : {false, true}) {
for (bool big_endian : {false, true}) {
params.codec = codec;
params.bits_per_sample = static_cast<size_t>(bits_per_sample);
params.is_gray = is_gray;
params.add_alpha = add_alpha;
params.big_endian = big_endian;
params.add_extra_channels = false;
TestRoundTrip(params, pool.get());
if (codec == Codec::kPNM && add_alpha) {
params.add_extra_channels = true;
TestRoundTrip(params, pool.get());
}
}
}
}
}
}
}
TEST(CodecTest, LosslessPNMRoundtrip) {
ThreadPoolForTests pool(12);
static const char* kChannels[] = {"", "g", "ga", "rgb", "rgba"};
static const char* kExtension[] = {"", ".pgm", ".pam", ".ppm", ".pam"};
for (size_t bit_depth = 1; bit_depth <= 16; ++bit_depth) {
for (size_t channels = 1; channels <= 4; ++channels) {
if (bit_depth == 1 && (channels == 2 || channels == 4)) continue;
std::string extension(kExtension[channels]);
std::string filename = "jxl/flower/flower_small." +
std::string(kChannels[channels]) + ".depth" +
std::to_string(bit_depth) + extension;
const std::vector<uint8_t> orig = jxl::test::ReadTestData(filename);
PackedPixelFile ppf;
ColorHints color_hints;
color_hints.Add("color_space",
channels < 3 ? "Gra_D65_Rel_SRG" : "RGB_D65_SRG_Rel_SRG");
ASSERT_TRUE(
DecodeBytes(Bytes(orig.data(), orig.size()), color_hints, &ppf));
EncodedImage encoded;
auto encoder = Encoder::FromExtension(extension);
ASSERT_TRUE(encoder.get());
ASSERT_TRUE(encoder->Encode(ppf, &encoded, pool.get()));
ASSERT_EQ(encoded.bitstreams.size(), 1);
ASSERT_EQ(orig.size(), encoded.bitstreams[0].size());
EXPECT_EQ(0,
memcmp(orig.data(), encoded.bitstreams[0].data(), orig.size()));
}
}
}
TEST(CodecTest, TestPNM) { TestCodecPNM(); }
TEST(CodecTest, FormatNegotiation) {
const std::vector<JxlPixelFormat> accepted_formats = {
{/*num_channels=*/4,
/*data_type=*/JXL_TYPE_UINT16,
/*endianness=*/JXL_NATIVE_ENDIAN,
/*align=*/0},
{/*num_channels=*/3,
/*data_type=*/JXL_TYPE_UINT8,
/*endianness=*/JXL_NATIVE_ENDIAN,
/*align=*/0},
{/*num_channels=*/3,
/*data_type=*/JXL_TYPE_UINT16,
/*endianness=*/JXL_NATIVE_ENDIAN,
/*align=*/0},
{/*num_channels=*/1,
/*data_type=*/JXL_TYPE_UINT8,
/*endianness=*/JXL_NATIVE_ENDIAN,
/*align=*/0},
};
JxlBasicInfo info;
JxlEncoderInitBasicInfo(&info);
info.bits_per_sample = 12;
info.num_color_channels = 2;
JxlPixelFormat format;
EXPECT_FALSE(SelectFormat(accepted_formats, info, &format));
info.num_color_channels = 3;
ASSERT_TRUE(SelectFormat(accepted_formats, info, &format));
EXPECT_EQ(format.num_channels, info.num_color_channels);
// 16 is the smallest accepted format that can accommodate the 12-bit data.
EXPECT_EQ(format.data_type, JXL_TYPE_UINT16);
}
TEST(CodecTest, EncodeToPNG) {
ThreadPool* const pool = nullptr;
std::unique_ptr<Encoder> png_encoder = Encoder::FromExtension(".png");
if (!png_encoder) {
fprintf(stderr, "Skipping test because of missing codec support.\n");
return;
}
const std::vector<uint8_t> original_png = jxl::test::ReadTestData(
"external/wesaturate/500px/tmshre_riaphotographs_srgb8.png");
PackedPixelFile ppf;
ASSERT_TRUE(extras::DecodeBytes(Bytes(original_png), ColorHints(), &ppf));
const JxlPixelFormat& format = ppf.frames.front().color.format;
const auto& format_matcher = [&format](const JxlPixelFormat& candidate) {
return (candidate.num_channels == format.num_channels) &&
(candidate.data_type == format.data_type) &&
(candidate.endianness == format.endianness);
};
const auto formats = png_encoder->AcceptedFormats();
ASSERT_TRUE(std::any_of(formats.begin(), formats.end(), format_matcher));
EncodedImage encoded_png;
ASSERT_TRUE(png_encoder->Encode(ppf, &encoded_png, pool));
EXPECT_TRUE(encoded_png.icc.empty());
ASSERT_EQ(encoded_png.bitstreams.size(), 1);
PackedPixelFile decoded_ppf;
ASSERT_TRUE(extras::DecodeBytes(Bytes(encoded_png.bitstreams.front()),
ColorHints(), &decoded_ppf));
ASSERT_EQ(decoded_ppf.info.bits_per_sample, ppf.info.bits_per_sample);
ASSERT_EQ(decoded_ppf.frames.size(), 1);
VerifySameImage(ppf.frames[0].color, ppf.info.bits_per_sample,
decoded_ppf.frames[0].color,
decoded_ppf.info.bits_per_sample);
}
} // namespace
} // namespace extras
} // namespace jxl
|