Merge pull request #31186 from guptaaarushg/fix-pfm-locale-independent-header-5x

Port the PFM locale fix from #31071 to 5.x, as requested

When the application's C++ locale uses digit grouping, a dimension of 1234 can be written as 1.234 and decoded as 1. Use std::to_string(int) for the integer header fields without changing any locale settings.

This PR only changes the PFM encoder. No new tests are added, following the review of #31071.

Tested on Windows x64 with MSVC 19.42 in Release mode. The existing PFM test passes.
This commit is contained in:
Abhishek Gola
2026-11-30 16:44:42 +05:30
committed by GitHub

View File

@@ -5,7 +5,7 @@
#include "precomp.hpp"
#include "utils.hpp"
#include "grfmt_pfm.hpp"
#include <iostream>
#include <string>
#ifdef HAVE_IMGCODEC_PFM
@@ -64,11 +64,10 @@ T read_number(cv::RLByteStream& strm)
return atoT<T>(str);
}
template<typename T> bool write_anything(cv::WLByteStream& strm, const T& t)
bool write_integer(cv::WLByteStream& strm, int value)
{
std::ostringstream ss;
ss << t;
return strm.putBytes(ss.str().c_str(), static_cast<int>(ss.str().size()));
const std::string text = std::to_string(value);
return strm.putBytes(text.c_str(), static_cast<int>(text.size()));
}
}
@@ -220,20 +219,19 @@ bool PFMEncoder::write(const Mat& img, const std::vector<int>& params)
CHECK_WRITE(strm.putByte('\n'));
CHECK_WRITE(write_anything(strm, float_img.cols));
CHECK_WRITE(write_integer(strm, float_img.cols));
CHECK_WRITE(strm.putByte(' '));
CHECK_WRITE(write_anything(strm, float_img.rows));
CHECK_WRITE(write_integer(strm, float_img.rows));
CHECK_WRITE(strm.putByte('\n'));
#ifdef WORDS_BIGENDIAN
CHECK_WRITE(write_anything(strm, 1.0));
CHECK_WRITE(write_integer(strm, 1));
#else
CHECK_WRITE(write_anything(strm, -1.0));
CHECK_WRITE(write_integer(strm, -1));
#endif
CHECK_WRITE(strm.putByte('\n'));
// Comments are not officially supported in this file format.
// write_anything(strm, "# Generated by OpenCV " CV_VERSION "\n");
for (int y = float_img.rows - 1; y >= 0; --y)
{