c++ visual studio 2017 通过 header 数据读取 png 维度
c++ visual studio 2017 reading png dimension through header data
您好,我无法正确读出 png 维度大小的数据
unsigned width = 0;
unsigned height = 0;
bool output_json = false;
std::ifstream in(filepath, std::ios_base::binary | std::ios_base::in);
if (in.is_open())
{
in.seekg(16, std::ios_base::cur);
in.read((char *)&width, 4);
in.read((char *)&height, 4);
_byteswap_uint64(width);
_byteswap_uint64(height);
output_json = true;
in.close();
}
宽度应为 155 ,但输出 2600468480
高度应为 80,但输出 1342177280
the width should be 155 , but output 2600468480 the height should be 80, but output 1342177280
字节顺序有问题
2600468480 是十进制形式的 9b000000; 155 是 9b.
所以低位字节/高位字节的顺序被调换了。
尝试交换字节
unsigned w0;
in.read((char *)&w0, 4);
width = ((w0 >> 24) & 0xff) |
((w0 << 8) & 0xff0000) |
((w0 >> 8) & 0xff00) |
((w0 << 24) & 0xff000000);
您好,我无法正确读出 png 维度大小的数据
unsigned width = 0;
unsigned height = 0;
bool output_json = false;
std::ifstream in(filepath, std::ios_base::binary | std::ios_base::in);
if (in.is_open())
{
in.seekg(16, std::ios_base::cur);
in.read((char *)&width, 4);
in.read((char *)&height, 4);
_byteswap_uint64(width);
_byteswap_uint64(height);
output_json = true;
in.close();
}
宽度应为 155 ,但输出 2600468480 高度应为 80,但输出 1342177280
the width should be 155 , but output 2600468480 the height should be 80, but output 1342177280
字节顺序有问题
2600468480 是十进制形式的 9b000000; 155 是 9b.
所以低位字节/高位字节的顺序被调换了。
尝试交换字节
unsigned w0;
in.read((char *)&w0, 4);
width = ((w0 >> 24) & 0xff) |
((w0 << 8) & 0xff0000) |
((w0 >> 8) & 0xff00) |
((w0 << 24) & 0xff000000);