逐字节读取 HEIF/HEIC 图像 XMP 元数据

发布于 2025-01-15 07:56:27 字数 1154 浏览 5 评论 0原文

我正在尝试构建一个本机字节解析器,给定 HEIF 图像,它返回其元数据(主要是图像的宽度和高度)。 我目前正在努力寻找正确的文档和规范来解析此类信息。我必须对 XMP 和 EXIF 元数据执行此类操作,但现在我们只关注 XMP。

我需要的是在哪里找到什么的确切字节结构。根据 HEIF 国际标准文档(此处):

对于图像项,XMP 元数据应存储为 item_type 值“mime”和内容类型“application/rdf+xml”的项。项目的主体应是 XML 格式的有效 XMP 文档。

完美,如果我分析示例图像,我可以找到这样的标记:

在此处输入图像描述

从现在开始,我找不到任何地方如何获取我需要的信息。我希望有这样的内容:“前 2 个字节是标头,标记为 0xFF 0xCE(只是一个示例),接下来的 2 个字节是宽度,后面的 2 个字节是高度......等等”。 就我而言,我是凭直觉行事。我的示例图像的尺寸为 8736x5856。如果在工具中查找 Big-Endian 2 字节整数 8736,我可以找到它:

在此处输入图像描述

嘿,2 个字节后还有 5856 高度:

在此处输入图像描述

但是,我再次凭借运气和直觉来到这里。我需要一个正确的模式来告诉我在哪里可以找到什么,以便我可以将其转换为代码。

I am trying to build a native byte parser that given an HEIF image it returns back its metadata (mainly width and height of the image).
I am struggling a lot at the moment finding the right documentation and specs to use for parsing such info. I have to do such thing for both XMP and EXIF metadata, but let's focus only on XMP for now.

What I need is the exact byte structure of where to find what. According to the HEIF international standard doc (here):

For image items, XMP metadata shall be stored as an item of item_type value 'mime' and content type'application/rdf+xml'. The body of the item shall be a valid XMP document, in XML form.

Perfect, if I analyse a sample image I can find such marker:

enter image description here

From now on I can't find anywhere how to get the info I need. I would expect something saying "the first 2 bytes are the header, with marker 0xFF 0xCE (just an example), the next 2 bytes are the width, and following 2 bytes the height...etc".
In my case I am going by intuition. My sample image is of dimensions 8736x5856. If in the tool I look for Big-Endian 2 byte integer 8736, I can find it:

enter image description here

And hey, 2 bytes later there is the 5856 height as well:

enter image description here

But again, I arrived here by luck and intuition. I need a proper schema that tells me where to find what in such a way that I can traslate it to code.

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

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

发布评论

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

评论(1

oО清风挽发oО 2025-01-22 07:56:27

我认为您看到的是“mime”和“ispe”mp4 盒,因为 HEIF 是基于 ISOBMFF 的。我建议使用支持 mp4 的工具查看该文件,例如 mp4dump、HexFiend 或 fq (注意:我的工具)。 “ispe”(图像空间范围)框可能是您想要阅读的内容。

fq 尚不支持 ispe 框,但您可以像这样阅读它:

$ fq 'grep_by(.type=="ispe").data | tobytes | [.[-8:-4], .[-4:] | tonumber]' file.heif
[
  8736,
  5856
]

所以您需要的可能是一个基本的 ISOBMFF 阅读器,然后查找“ispe”框并对其进行解码。如果您只查找特定框的第一个,您可能可以忽略 ISOBMFF 是树结构。

What I think you'r seeing is a "mime" and "ispe" mp4 box as HEIF is ISOBMFF based. I would recommend looking at the file using a mp4 capable tool like mp4dump, HexFiend or fq (note: my tool). The "ispe" (Image Spatial Extents) box i probably what you want to read.

fq does no support ispe box yet but you could read it like this:

$ fq 'grep_by(.type=="ispe").data | tobytes | [.[-8:-4], .[-4:] | tonumber]' file.heif
[
  8736,
  5856
]

So what you need is probably a basic ISOBMFF reader and then look for the "ispe" box and decode it. If you'r only looking for the first of a specific box you can probably ignore that ISOBMFF is a tree structure.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文