简体   繁体   English

使用C ++读取原始图像文件

[英]Reading a raw image file with C++

I have two identical C++ codes which each read in identical .raw image files as such: 我有两个相同的C ++代码,每个代码都读取相同的.raw图像文件,例如:

this->file_variable = fopen(filename, "r")

They process the information within them as such: 他们按以下方式处理其中的信息:

status = fread ((void *)this->img1,
  sizeof(float),
  (this->width * this->height),
  this->file_variable)
)

The only difference between the two codes is that they were compiled on different boxes, but I'm getting completely different results from the img1 array. 两种代码之间的唯一区别是它们是在不同的盒子上编译的,但是从img1数组中得到的结果完全不同。 I have absolutely no idea how to even start debugging this. 我完全不知道如何甚至开始调试它。 Could anyone please point me in the right direction? 有人能指出我正确的方向吗?

Edit: I'm slowly gaining more information on the files. 编辑:我正在慢慢获得有关文件的更多信息。 They are (width x height) 1800 x 1728 pixels, 1 channel, 8 bits depth. 它们是(宽x高)1800 x 1728像素,1个通道,8位深度。

It sounds like the file was written in binary format, so you need to open it likewise: 听起来文件是用二进制格式编写的,因此您需要同样打开它:

this->file_variable = fopen(filename, "rb")

Without the "b", it's being read as ASCII. 如果没有“ b”,它将被读取为ASCII。

Now I see your problem. 现在,我看到了您的问题。 Your data is stored in big endian and you're reading it on a little endian system. 您的数据存储在big endian中,而您正在使用little endian系统读取它。 You need to simply convert each float by reversing the byte order. 您只需通过反转字节顺序来简单地转换每个浮点数。 Use a function like this (taken from a similar answer elsewhere): 使用如下函数(取自其他地方的类似答案):

float ReverseFloat( const float inFloat )
{
float retVal;
char *floatToConvert = ( char* ) & inFloat;
char *returnFloat = ( char* ) & retVal;

// swap the bytes into a temporary buffer
returnFloat[0] = floatToConvert[3];
returnFloat[1] = floatToConvert[2];
returnFloat[2] = floatToConvert[1];
returnFloat[3] = floatToConvert[0];

return retVal;
}

There is no guarantee that the binary (!) format of values are the same on different machines. 不能保证值的二进制(!)格式在不同的计算机上是相同的。 Where do the other this-> values come from? 其他this->值从何而来?

Here Is A Great Function To Flip The Byte Order Of Any Variable, Which Can Be Easily Used To Change The Endianness. 这是翻转任何变量的字节顺序的强大功能,可以轻松地使用它来更改字节序。

void byteFlip(void* original, size_t numberOfBytes)
{
    char* reversed = (char*) malloc(numberOfBytes);
    for (int i = 0; i < numberOfBytes; i++)
    {
        reversed[i] = ((char*)original)[numberOfBytes - i - 1];
    }
    memcpy(original, reversed, numberOfBytes);
    free(reversed);
}

This Will Flip The Byte Order Of The Variable Used In The 'original' parameter. 这将翻转“原始”参数中使用的变量的字节顺序。 Example: 例:

short a = 512;
//AAAAAAAA BBBBBBBB - 00000010 00000000
cout << a << endl; //outputs 512
byteFlip(&a, sizeof(short)); //flip byte order of 'a'
//BBBBBBBB AAAAAAAA - 00000000 00000010
cout << a << endl; //outputs 2

Explanation: this function takes a pointer to any type of variable, and the size in bytes of that variable. 说明:此函数使用指向任何类型变量的指针,以及该变量的字节大小。 Since we are reversing the byte order, and 1 character = 1 byte, we can therefore treat this the same way as we would reverse a string. 由于我们要反转字节顺序,并且1个字符= 1个字节,因此我们可以像反转字符串一样对待这种方式。 We create a new string the same size as our variable to hold the reversed data. 我们创建一个与变量大小相同的新字符串,以保存反向数据。 Now we use a for loop to copy each byte as a char in reversed order into the reversed character array variable. 现在,我们使用一个for循环将每个字节作为char以相反的顺序复制到反向字符数组变量中。 Once the loop has completed we can copy the memory of our reversed string into the original pointer. 循环完成后,我们可以将反向字符串的内存复制到原始指针中。 Finally we free the memory of the reversed string variable since it has been copied into the original pointer, and therefore no longer needed. 最后,我们释放了反向字符串变量的内存,因为它已被复制到原始指针中,因此不再需要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM