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/quantizer.h"
7
8 #include <random>
9
10 #include "gtest/gtest.h"
11 #include "lib/jxl/base/span.h"
12 #include "lib/jxl/common.h"
13 #include "lib/jxl/dec_bit_reader.h"
14 #include "lib/jxl/image_ops.h"
15 #include "lib/jxl/image_test_utils.h"
16
17 namespace jxl {
18 namespace {
19
TestEquivalence(int qxsize,int qysize,const Quantizer & quantizer1,const Quantizer & quantizer2)20 void TestEquivalence(int qxsize, int qysize, const Quantizer& quantizer1,
21 const Quantizer& quantizer2) {
22 ASSERT_NEAR(quantizer1.inv_quant_dc(), quantizer2.inv_quant_dc(), 1e-7);
23 }
24
TEST(QuantizerTest,QuantizerParams)25 TEST(QuantizerTest, QuantizerParams) {
26 for (uint32_t i = 1; i < 10000; ++i) {
27 QuantizerParams p;
28 p.global_scale = i;
29 size_t extension_bits = 0, total_bits = 0;
30 EXPECT_TRUE(Bundle::CanEncode(p, &extension_bits, &total_bits));
31 EXPECT_EQ(0u, extension_bits);
32 EXPECT_GE(total_bits, 4u);
33 }
34 }
35
TEST(QuantizerTest,BitStreamRoundtripSameQuant)36 TEST(QuantizerTest, BitStreamRoundtripSameQuant) {
37 const int qxsize = 8;
38 const int qysize = 8;
39 DequantMatrices dequant;
40 Quantizer quantizer1(&dequant);
41 ImageI raw_quant_field(qxsize, qysize);
42 quantizer1.SetQuant(0.17f, 0.17f, &raw_quant_field);
43 BitWriter writer;
44 EXPECT_TRUE(quantizer1.Encode(&writer, 0, nullptr));
45 writer.ZeroPadToByte();
46 const size_t bits_written = writer.BitsWritten();
47 Quantizer quantizer2(&dequant, qxsize, qysize);
48 BitReader reader(writer.GetSpan());
49 EXPECT_TRUE(quantizer2.Decode(&reader));
50 EXPECT_TRUE(reader.JumpToByteBoundary());
51 EXPECT_EQ(reader.TotalBitsConsumed(), bits_written);
52 EXPECT_TRUE(reader.Close());
53 TestEquivalence(qxsize, qysize, quantizer1, quantizer2);
54 }
55
TEST(QuantizerTest,BitStreamRoundtripRandomQuant)56 TEST(QuantizerTest, BitStreamRoundtripRandomQuant) {
57 const int qxsize = 8;
58 const int qysize = 8;
59 DequantMatrices dequant;
60 Quantizer quantizer1(&dequant);
61 ImageI raw_quant_field(qxsize, qysize);
62 quantizer1.SetQuant(0.17f, 0.17f, &raw_quant_field);
63 std::mt19937_64 rng;
64 std::uniform_int_distribution<> uniform(1, 256);
65 float quant_dc = 0.17f;
66 ImageF qf(qxsize, qysize);
67 RandomFillImage(&qf, 1.0f);
68 quantizer1.SetQuantField(quant_dc, qf, &raw_quant_field);
69 BitWriter writer;
70 EXPECT_TRUE(quantizer1.Encode(&writer, 0, nullptr));
71 writer.ZeroPadToByte();
72 const size_t bits_written = writer.BitsWritten();
73 Quantizer quantizer2(&dequant, qxsize, qysize);
74 BitReader reader(writer.GetSpan());
75 EXPECT_TRUE(quantizer2.Decode(&reader));
76 EXPECT_TRUE(reader.JumpToByteBoundary());
77 EXPECT_EQ(reader.TotalBitsConsumed(), bits_written);
78 EXPECT_TRUE(reader.Close());
79 TestEquivalence(qxsize, qysize, quantizer1, quantizer2);
80 }
81 } // namespace
82 } // namespace jxl
83