From 30ef29162cd9d34b7c4e7603b382b4f7106a52fe Mon Sep 17 00:00:00 2001 From: NIKHILA Date: Wed, 2 Dec 2026 15:21:30 +0530 Subject: [PATCH] =?UTF-8?q?imgcodecs:=20scale=20binary=20PNM=20samples=20f?= =?UTF-8?q?or=20non-full=20maxval=20=F0=9F=A4=96=F0=9F=A4=96=F0=9F=A4=96?= =?UTF-8?q?=20(#31209)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Apply the existing lookup table to binary 8-bit PGM/PPM samples when maxval is below 255, clamping out-of-range values as the ASCII path already does. The maxval-255 fast path and 16-bit behavior are unchanged. Add generated PGM and PPM cases comparing ASCII and binary decoding across several maxvals and read modes; no external image data is needed. --- modules/imgcodecs/src/grfmt_pxm.cpp | 5 +++ modules/imgcodecs/test/test_grfmt.cpp | 52 +++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/modules/imgcodecs/src/grfmt_pxm.cpp b/modules/imgcodecs/src/grfmt_pxm.cpp index 3ddab22b56..65ace78a22 100644 --- a/modules/imgcodecs/src/grfmt_pxm.cpp +++ b/modules/imgcodecs/src/grfmt_pxm.cpp @@ -299,6 +299,11 @@ bool PxMDecoder::readData( Mat& img ) else { m_strm.getBytes( src, src_pitch ); + if( bit_depth == 8 && m_maxval < 255 ) + { + for (int x = 0; x < width3; x++) + src[x] = gray_palette[std::min(src[x], m_maxval)]; + } if( bit_depth == 16 && !isBigEndian() ) { for (int x = 0; x < width3; x++) diff --git a/modules/imgcodecs/test/test_grfmt.cpp b/modules/imgcodecs/test/test_grfmt.cpp index 936f9934f3..5a62a32c51 100644 --- a/modules/imgcodecs/test/test_grfmt.cpp +++ b/modules/imgcodecs/test/test_grfmt.cpp @@ -712,6 +712,58 @@ TEST(Imgcodecs, imdecode_user_buffer) EXPECT_TRUE(result.empty()); } +#ifdef HAVE_IMGCODEC_PXM +typedef testing::TestWithParam > Imgcodecs_Pxm_Maxval; + +TEST_P(Imgcodecs_Pxm_Maxval, binary_matches_ascii_maxval) +{ + const int channels = get<0>(GetParam()); + const int maxval = get<1>(GetParam()); + const int mode = get<2>(GetParam()); + + // The final sample exceeds maxval where an 8-bit value permits it. + const int samples[] = {0, maxval / 2, maxval, std::min(maxval + 1, 255)}; + const uchar midpoint = maxval == 1 ? 0 : maxval == 15 ? 119 : 127; + const uchar expected[] = {0, midpoint, 255, 255}; + const std::string header = cv::format("\n4 1\n%d\n", maxval); + std::string ascii = std::string(channels == 1 ? "P2" : "P3") + header; + const std::string binaryHeader = std::string(channels == 1 ? "P5" : "P6") + header; + std::vector binary(binaryHeader.begin(), binaryHeader.end()); + + for (int x = 0; x < 4; ++x) + { + for (int c = 0; c < channels; ++c) + { + ascii += cv::format("%d ", samples[x]); + binary.push_back(static_cast(samples[x])); + } + } + + const std::vector plain(ascii.begin(), ascii.end()); + const Mat decoded[] = {imdecode(plain, mode), imdecode(binary, mode)}; + const int outputChannels = mode == IMREAD_UNCHANGED ? channels : + mode == IMREAD_GRAYSCALE ? 1 : 3; + for (int encoding = 0; encoding < 2; ++encoding) + { + SCOPED_TRACE(encoding == 0 ? "ASCII" : "binary"); + const Mat& image = decoded[encoding]; + ASSERT_FALSE(image.empty()); + ASSERT_EQ(CV_MAKETYPE(CV_8U, outputChannels), image.type()); + ASSERT_EQ(Size(4, 1), image.size()); + for (int x = 0; x < 4; ++x) + for (int c = 0; c < outputChannels; ++c) + EXPECT_EQ(expected[x], image.ptr(0)[x * outputChannels + c]) + << "x=" << x << " channel=" << c; + } +} + +INSTANTIATE_TEST_CASE_P(Encodings, Imgcodecs_Pxm_Maxval, + testing::Combine(testing::Values(1, 3), testing::Values(1, 15, 100, 255), + testing::Values(IMREAD_UNCHANGED, IMREAD_GRAYSCALE, + IMREAD_COLOR, IMREAD_COLOR_RGB))); + +#endif + }} // namespace #if defined(HAVE_OPENEXR) && defined(OPENCV_IMGCODECS_ENABLE_OPENEXR_TESTS)