pgx_test.cc (2889B)
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/extras/dec/pgx.h" 7 8 #include <cstdint> 9 #include <cstring> 10 #include <string> 11 12 #include "lib/extras/packed_image.h" 13 #include "lib/extras/packed_image_convert.h" 14 #include "lib/jxl/base/span.h" 15 #include "lib/jxl/image_bundle.h" 16 #include "lib/jxl/image_ops.h" 17 #include "lib/jxl/test_memory_manager.h" 18 #include "lib/jxl/test_utils.h" 19 #include "lib/jxl/testing.h" 20 21 namespace jxl { 22 namespace extras { 23 namespace { 24 25 Span<const uint8_t> MakeSpan(const char* str) { 26 return Bytes(reinterpret_cast<const uint8_t*>(str), strlen(str)); 27 } 28 29 TEST(CodecPGXTest, Test8bits) { 30 std::string pgx = "PG ML + 8 2 3\npixels"; 31 32 PackedPixelFile ppf; 33 ThreadPool* pool = nullptr; 34 35 EXPECT_TRUE(DecodeImagePGX(MakeSpan(pgx.c_str()), ColorHints(), &ppf)); 36 CodecInOut io{jxl::test::MemoryManager()}; 37 EXPECT_TRUE(ConvertPackedPixelFileToCodecInOut(ppf, pool, &io)); 38 39 ScaleImage(255.f, io.Main().color()); 40 41 EXPECT_FALSE(io.metadata.m.bit_depth.floating_point_sample); 42 EXPECT_EQ(8u, io.metadata.m.bit_depth.bits_per_sample); 43 EXPECT_TRUE(io.metadata.m.color_encoding.IsGray()); 44 EXPECT_EQ(2u, io.xsize()); 45 EXPECT_EQ(3u, io.ysize()); 46 47 float eps = 1e-5; 48 EXPECT_NEAR('p', io.Main().color()->Plane(0).Row(0)[0], eps); 49 EXPECT_NEAR('i', io.Main().color()->Plane(0).Row(0)[1], eps); 50 EXPECT_NEAR('x', io.Main().color()->Plane(0).Row(1)[0], eps); 51 EXPECT_NEAR('e', io.Main().color()->Plane(0).Row(1)[1], eps); 52 EXPECT_NEAR('l', io.Main().color()->Plane(0).Row(2)[0], eps); 53 EXPECT_NEAR('s', io.Main().color()->Plane(0).Row(2)[1], eps); 54 } 55 56 TEST(CodecPGXTest, Test16bits) { 57 std::string pgx = "PG ML + 16 2 3\np_i_x_e_l_s_"; 58 59 PackedPixelFile ppf; 60 ThreadPool* pool = nullptr; 61 62 EXPECT_TRUE(DecodeImagePGX(MakeSpan(pgx.c_str()), ColorHints(), &ppf)); 63 CodecInOut io{jxl::test::MemoryManager()}; 64 EXPECT_TRUE(ConvertPackedPixelFileToCodecInOut(ppf, pool, &io)); 65 66 ScaleImage(255.f, io.Main().color()); 67 68 EXPECT_FALSE(io.metadata.m.bit_depth.floating_point_sample); 69 EXPECT_EQ(16u, io.metadata.m.bit_depth.bits_per_sample); 70 EXPECT_TRUE(io.metadata.m.color_encoding.IsGray()); 71 EXPECT_EQ(2u, io.xsize()); 72 EXPECT_EQ(3u, io.ysize()); 73 74 // Comparing ~16-bit numbers in floats, only ~7 bits left. 75 float eps = 1e-3; 76 const auto& plane = io.Main().color()->Plane(0); 77 EXPECT_NEAR(256.0f * 'p' + '_', plane.Row(0)[0] * 257, eps); 78 EXPECT_NEAR(256.0f * 'i' + '_', plane.Row(0)[1] * 257, eps); 79 EXPECT_NEAR(256.0f * 'x' + '_', plane.Row(1)[0] * 257, eps); 80 EXPECT_NEAR(256.0f * 'e' + '_', plane.Row(1)[1] * 257, eps); 81 EXPECT_NEAR(256.0f * 'l' + '_', plane.Row(2)[0] * 257, eps); 82 EXPECT_NEAR(256.0f * 's' + '_', plane.Row(2)[1] * 257, eps); 83 } 84 85 } // namespace 86 } // namespace extras 87 } // namespace jxl