这是我到目前为止使用搜索功能的第一篇文章。但是现在我在以下问题上浪费了一整天:
我录制了一个 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个“字符”并将它们作为一个“短”处理?我还在每一行之后尝试了一个空格,但这没有任何改变,所以我决定发布这个更短的代码。
谢谢你的帮助!