读取PNG标题的宽度和高度(Reading width and height of PNG header)

编程入门 行业动态 更新时间:2024-10-27 17:18:54
读取PNG标题的宽度和高度(Reading width and height of PNG header)

我正在尝试读取PNG文件的宽度和高度。 这是我的代码:

struct TImageSize { int width; int height; }; bool getPngSize(const char *fileName, TImageSize &is) { std::ifstream file(fileName, std::ios_base::binary | std::ios_base::in); if (!file.is_open() || !file) { file.close(); return false; } // Skip PNG file signature file.seekg(9, std::ios_base::cur); // First chunk: IHDR image header // Skip Chunk Length file.seekg(4, std::ios_base::cur); // Skip Chunk Type file.seekg(4, std::ios_base::cur); __int32 width, height; file.read((char*)&width, 4); file.read((char*)&height, 4); std::cout << file.tellg(); is.width = width; is.height = height; file.close(); return true; }

如果我尝试从维基百科的这幅图像读取例子,我得到这些错误的值:

252097920(应该是800) 139985408(应该是600)

请注意,函数返回false,因此宽度和高度变量的内容必须来自文件。

I am experimenting with reading the width and height of a PNG file. This is my code:

struct TImageSize { int width; int height; }; bool getPngSize(const char *fileName, TImageSize &is) { std::ifstream file(fileName, std::ios_base::binary | std::ios_base::in); if (!file.is_open() || !file) { file.close(); return false; } // Skip PNG file signature file.seekg(9, std::ios_base::cur); // First chunk: IHDR image header // Skip Chunk Length file.seekg(4, std::ios_base::cur); // Skip Chunk Type file.seekg(4, std::ios_base::cur); __int32 width, height; file.read((char*)&width, 4); file.read((char*)&height, 4); std::cout << file.tellg(); is.width = width; is.height = height; file.close(); return true; }

If I try to read for example from this image from Wikipedia, I'm getting these wrong values:

252097920 (should be 800) 139985408 (should be 600)

Note that the function is not returning false so the contents of the width and height variables must come from the file.

最满意答案

它看起来像你关了一个字节:

// Skip PNG file signature file.seekg(9, std::ios_base::cur);

PNG规范说头标长度为8个字节,所以你希望“9”代替“8”。 职位从0开始。

还要注意规范说明整数是网络(big-endian)顺序 ,所以如果你使用的是小端系统,你可能需要或需要使用ntohl()或者转换字节顺序。

可能值得使用libpng或stb_image或类似的东西,而不是尝试自己解析png,除非你这样做是为了学习。

It looks like you're off by a byte:

// Skip PNG file signature file.seekg(9, std::ios_base::cur);

The PNG Specification says the header is 8 bytes long, so you want that "9" to be an "8" instead. Positions start at 0.

Also note that the spec says that integers are in network (big-endian) order, so you may want or need to use ntohl() or otherwise convert byte order if you're on a little-endian system.

It's probably worth using libpng or stb_image or something similar rather than attempting to parse the png yourself, though -- unless you're doing this to learn.

更多推荐

本文发布于:2023-04-28 16:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1332449.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:宽度   高度   标题   PNG   header

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!