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