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