簡體   English   中英

將 exr/pfm 保存為小端

[英]Save exr/pfm as little endian

我將一個 bmp 文件加載到CImg對象中,並將其保存到pfm文件中。 成功的。 我將這個.pfm文件用於另一個庫,但該庫不接受 big-endian ,只接受little endian

    CImg<float> image;
    image.load_bmp(_T("D:\\Temp\\memorial.bmp"));
    image.normalize(0.0, 1.0);
    image.save_pfm(_T("D:\\Temp\\memorial.pfm"));

那么,我怎樣才能將bmp 文件保存為 pfm 文件作為小端而不是大端.. 有可能嗎?

后期編輯:

我已經檢查了 .pfm 頭文件中的前 5 個元素。 這是沒有 invert_endianness的結果:

CImg<float> image;
image.load_bmp(_T("D:\\Temp\\memorial.bmp"));
image.normalize(0.0, 1.0);
image.save_pfm(_T("D:\\Temp\\memorial.pfm"));

PF
512
768
1.0
=øøù=€€=‘>

這是invert_endianness的結果:

CImg<float> image;
image.load_bmp(_T("D:\\Temp\\memorial.bmp"));
image.invert_endianness();
image.normalize(0.0, 1.0);
image.save_pfm(_T("D:\\Temp\\memorial.pfm"));

PF
512
768
1.0
?yôx?!ù=‚ì:„ç‹?

結果是一樣的。

這絕對不是一個正確的答案,但暫時可以作為一種解決方法。

我沒有找到如何使用CImg函數正確反轉字節序,所以我修改了結果文件。 這是一個黑客。 結果在 GIMP 中打開得很好,看起來非常接近原始圖像,但我不能說它是否適用於您正在使用的庫。 這可能值得一試。

代碼中的注釋:

#include "CImg/CImg.h"

#include <algorithm>
#include <filesystem> // >= C++17 must be selected as Language Standard
#include <ios>
#include <iostream>
#include <iterator>
#include <fstream>
#include <string>

using namespace cimg_library;
namespace fs = std::filesystem;

// a class to remove temporary files
class remove_after_use {
public:
    remove_after_use(const std::string& filename) : name(filename) {}
    remove_after_use(const remove_after_use&) = delete;
    remove_after_use& operator=(const remove_after_use&) = delete;

    const char* c_str() const { return name.c_str(); }
    operator std::string const& () const { return name; }

    ~remove_after_use() {
        try {
            fs::remove(name);
        }
        catch (const std::exception & ex) {
            std::cerr << "remove_after_use: " << ex.what() << "\n";
        }
    }
private:
    std::string name;
};

// The function to hack the file saved by CImg
template<typename T>
bool save_pfm_endianness_inverted(const T& img, const std::string& filename) {
    remove_after_use tempfile("tmp.pfm");

    // get CImg's endianness inverted image and save it to a temporary file
    img.get_invert_endianness().save_pfm(tempfile.c_str());

    // open the final file
    std::ofstream os(filename, std::ios::binary);

    // read "tmp.pfm" and modify
    // The Scale Factor / Endianness line
    if (std::ifstream is; os && (is = std::ifstream(tempfile, std::ios::binary))) {
        std::string lines[3];
        // Read the 3 PFM header lines as they happen to be formatted by
        // CImg. Will maybe not work with another library.
        size_t co = 0;
        for (; co < std::size(lines) && std::getline(is, lines[co]); ++co);

        if (co == std::size(lines)) { // success
            // write the first two lines back unharmed:
            os << lines[0] << '\n' << lines[1] << '\n';

            if (lines[2].empty()) {
                std::cerr << "something is wrong with the pfm header\n";
                return false;
            }

            // add a '-' if it's missing, remove it if it's there: 
            if (lines[2][0] == '-') {       // remove the - to invert
                os << lines[2].substr(1);
            }
            else {                          // add a - to invert
                os << '-' << lines[2] << '\n';
            }

            // copy all the rest as-is:
            std::copy(std::istreambuf_iterator<char>(is),
                std::istreambuf_iterator<char>{},
                std::ostreambuf_iterator<char>(os));
        }
        else {
            std::cerr << "failed reading pfm header\n";
            return false;
        }
    }
    else {
        std::cerr << "opening files failed\n";
        return false;
    }
    return true;
}

int main()
{
    CImg<float> img("memorial.bmp");
    img.normalize(0.f, 1.f);
    std::cout << "saved ok: " << std::boolalpha
              << save_pfm_endianness_inverted(img, "memorial.pfm") << "\n";
}

想要以經典的 C++ 風格(作為語言的緣故)解決同樣的問題,我寫道:

BOOL CMyDoc::SavePfmEndiannessInverted(CImg<float>& img, const CString sFileName)
{
CString sDrive, sDir;
_splitpath(sFileName, sDrive.GetBuffer(), sDir.GetBuffer(), NULL, NULL);

CString sTemp;
sTemp.Format(_T("%s%sTemp.tmp"), sDrive, sDir);

sDrive.ReleaseBuffer();
sDir.ReleaseBuffer();

CRemoveAfterUse TempFile(sTemp);
img.get_invert_endianness().save_pfm(TempFile.c_str());

CFile fileTemp;
if (! fileTemp.Open(sTemp, CFile::typeBinary))
    return FALSE;

char c;
UINT nRead = 0;
int nCount = 0;
ULONGLONG nPosition = 0;
CString sScale;
CByteArray arrHeader, arrData;
do
{
    nRead = fileTemp.Read((char*)&c, sizeof(char));
    switch (nCount)
    {
    case 0:
    case 1:
        arrHeader.Add(static_cast<BYTE>(c));
        break;
    case 2:             // retrieve the '1.0' string
        sScale += c;
        break;
    }
    if ('\n' == c)      // is new line
    {
        nCount++;
    }
    if (nCount >= 3)    // read the header, so go out
    {
        nPosition = fileTemp.GetPosition();
        break;
    }
}while (nRead > 0);

if (nPosition > 1)
{
    arrData.SetSize(fileTemp.GetLength() - nPosition);
    fileTemp.Read(arrData.GetData(), (UINT)arrData.GetSize());
}

fileTemp.Close();

CFile file;
if (! file.Open(sFileName, CFile::typeBinary | CFile::modeCreate | CFile::modeReadWrite))
    return FALSE;

CByteArray arrTemp;
ConvertCStringToByteArray(sScale, arrTemp);
arrHeader.Append(arrTemp);
arrHeader.Append(arrData);
file.Write(arrHeader.GetData(), (UINT)arrHeader.GetSize());
file.Close();

return TRUE;
}

但似乎不行,因為圖像結果更暗

在此處輸入圖片說明

我在這里做錯了什么? 代碼在我看來很清楚,但仍然沒有按預期工作......

當然,這種方法效率更低,我知道,但正如我之前所說,只是為了語言。

我認為我的代碼沒有問題:)

這是審判:

CImg<float> image;
image.load_bmp(_T("D:\\Temp\\memorial.bmp"));
image.normalize(0.0f, 1.0f);
image.save_pfm(_T("D:\\Temp\\memorial.pfm"));
image.get_invert_endianness().save(_T("D:\\Temp\\memorial_inverted.pfm"));

和 Memorial.pfm 看起來像這樣:

紀念.pfm

和 Memorial_inverted.pfm 看起來像這樣:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM