gpt4 book ai didi

c++ - 读取二进制 16 位流文件并写入 16 位 PGM (C++)

转载 作者:行者123 更新时间:2023-11-28 07:15:34 26 4
gpt4 key购买 nike

这是我到目前为止使用搜索功能的第一篇文章。但是现在我在以下问题上浪费了一整天:

我录制了一个 12 位(写为 16 位)灰度视频并将其直接写入二进制流文件(没有标题等)。

现在的任务是读取文件并将每一帧输出为 16 位 pgm。

以下片段说明了我的尝试。输出是有效的 pgm,但带有“白噪声”。

    ...
imageBufferShort = new short[imageWidth*imageHeight* sizeof(short)];
...
streamFileHandle.read(reinterpret_cast<char*>(imageBufferShort),2*imageWidth*imageHeight); //double amount because 8bit chars!
// As .read only takes chars, I thought, that I just read the double amount of char-bytes and when it is interpreted as short (=16bit) everything is ok?!?

...now the pgm output:

std::ofstream f_hnd(fileName,std::ios_base::out |std::ios_base::binary |std::ios_base::trunc);
// write simple header
f_hnd.write("P5\n",3);
f_hnd << imageWidth << " " << imageHeight << "\n4095\n"; //4095 should tell the pgm to use 2 bytes for each pixel

f_hnd.write(reinterpret_cast<char*>(imageBufferShort),2*imageWidth*imageHeight);
f_hnd.close();

同样,该文件已生成并且可以查看,但包含垃圾。初步猜测可以吗?读取 2 个“字符”并将它们作为一个“短字符”处理?我还在每一行之后添加了一个空格,但这没有任何改变,所以我决定发布这个更短的代码。

感谢您的帮助!

最佳答案

正如@Domi 和@JoeZ 所指出的:您的字节序可能搞砸了。意思是,你的字节顺序是错误的。

要解决您的问题,您必须遍历每个像素并交换它的字节,然后再将其写回文件。

关于c++ - 读取二进制 16 位流文件并写入 16 位 PGM (C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20280919/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com