Jer*_*fin 12
宽度是一个4字节的整数,从文件中的偏移量16开始.高度是从偏移量20开始的另一个4字节整数.它们都是网络顺序,因此您需要转换为主机顺序才能正确解释它们.
#include <fstream>
#include <iostream>
#include <winsock.h>
int main(int argc, char **argv) {
std::ifstream in(argv[1]);
unsigned int width, height;
in.seekg(16);
in.read((char *)&width, 4);
in.read((char *)&height, 4);
width = ntohl(width);
height = ntohl(height);
std::cout << argv[1] << " is " << width << " pixels wide and " << height << " pixels high.\n";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
不,如果不阅读部分文件,你就无法做到.幸运的是,如果您不需要读取实际的图像数据,那么文件头很简单,您可以在没有库的情况下读取它们.
如果你确定你有一个有效的PNG文件,你可以从偏移16和20(每个4字节,big-endian)读取宽度和高度,但验证前8个字节也是一个好主意.该文件正好是"89 50 4E 47 0D 0A 1A 0A"(十六进制),字节12-15正好是"49 48 44 52"(ASCII中的"IHDR").
| 归档时间: |
|
| 查看次数: |
12517 次 |
| 最近记录: |