c++用于读取原始文件流元数据的字节序感知的库?

发布于 2024-08-26 03:28:15 字数 453 浏览 3 评论 0原文

我从图像文件中获取了原始数据流,例如:

vector<char> rawData(fileSize);
ifstream inFile("image.jpg");
inFile.read(&rawData[0]);

我想解析不同图像格式的标题的高度和宽度。是否有一个可移植库可以从缓冲区/流中读取整数、长整型、短整型等,并按指定转换为字节序?

我希望能够执行以下操作: short x = rawData.readLeShort(offset);long y = rawData.readBeLong(offset)

更好的选择将是一个轻量级的可以处理原始图像数据的便携式图像元数据库(没有图像处理库的额外重量)。我发现 Exif 库不支持 pnggif

I've got raw data streams from image files, like:

vector<char> rawData(fileSize);
ifstream inFile("image.jpg");
inFile.read(&rawData[0]);

I want to parse the headers of different image formats for height and width. Is there a portable library that can can read ints, longs, shorts, etc. from the buffer/stream, converting for endianess as specified?

I'd like to be able to do something like: short x = rawData.readLeShort(offset); or long y = rawData.readBeLong(offset)

An even better option would be a lightweight & portable image metadata library (without the extra weight of an image manipulation library) that can work on raw image data. I've found that Exif libraries out there don't support png and gif.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忱杏 2024-09-02 03:28:15

自己做起来并不难。以下是读取小端 32 位数字

unsigned char buffer[4];
inFile.read(buffer, sizeof(buffer));

unsigned int number = buffer[0] +
                      (buffer[1] << 8) +
                      (buffer[2] << 16) +
                      (buffer[3] << 24);

和读取大端 32 位数字的方法:

unsigned char buffer[4];
inFile.read(buffer, sizeof(buffer));

unsigned int number = buffer[3] +
                      (buffer[2] << 8) +
                      (buffer[1] << 16) +
                      (buffer[0] << 24);

It's not that hard to do yourself. Here's how you can read a little endian 32 bit number:

unsigned char buffer[4];
inFile.read(buffer, sizeof(buffer));

unsigned int number = buffer[0] +
                      (buffer[1] << 8) +
                      (buffer[2] << 16) +
                      (buffer[3] << 24);

and to read a big endian 32 bit number:

unsigned char buffer[4];
inFile.read(buffer, sizeof(buffer));

unsigned int number = buffer[3] +
                      (buffer[2] << 8) +
                      (buffer[1] << 16) +
                      (buffer[0] << 24);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文