Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add XMP support in AVIF #104

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/file-parsers/heif.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ export class HeifFileParser extends IsoBmffParser {
//await this.findThumb(meta)
if (this.options.icc.enabled) await this.findIcc(meta)
if (this.options.tiff.enabled) await this.findExif(meta)
if (this.options.xmp.enabled) await this.findXmp(meta)
}

async registerSegment(key, offset, length) {
Expand Down Expand Up @@ -143,6 +144,22 @@ export class HeifFileParser extends IsoBmffParser {
await this.registerSegment('tiff', exifOffset, exifLength)
}

async findXmp(meta) {
let iinf = this.findBox(meta, 'iinf')
if (iinf === undefined) return
let iloc = this.findBox(meta, 'iloc')
if (iloc === undefined) return
let xmpLocId = this.findXmpLocIdInIinf(iinf)
let extent = this.findExtentInIloc(iloc, xmpLocId)
if (extent === undefined) return
let [xmpOffset, xmpLength] = extent
await this.file.ensureChunk(xmpOffset, xmpLength)
let extentContentShift = 4
xmpOffset += extentContentShift
xmpLength -= extentContentShift
await this.registerSegment('xmp', xmpOffset, xmpLength)
}

findExifLocIdInIinf(box) {
this.parseBoxFullHead(box)
let offset = box.start
Expand All @@ -163,6 +180,28 @@ export class HeifFileParser extends IsoBmffParser {
}
}

findXmpLocIdInIinf(box) {
this.parseBoxFullHead(box)
let offset = box.start
let count = this.file.getUint16(offset)
let infe, infeOffset, idSize, name
offset += 2
while (count--) {
infe = this.parseBoxHead(offset)
this.parseBoxFullHead(infe)
infeOffset = infe.start
if (infe.version >= 2) {
idSize = infe.version === 3 ? 4 : 2
const dataLength = infe.length - idSize - infe.start + infe.offset - 2
name = this.file.getString(infeOffset + idSize + 2, dataLength - 1)
if (name === 'mime\0application/rdf+xml') {
return this.file.getUintBytes(infeOffset, idSize)
}
}
offset += infe.length
}
}

get8bits(offset) {
let n = this.file.getUint8(offset)
let n0 = n >> 4
Expand Down
Binary file added test/fixtures/avif/IMG_20180725_163423-2.avif
Binary file not shown.
9 changes: 8 additions & 1 deletion test/formats/avif.spec.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as exifr from '../../src/bundles/full.mjs'
// TODO
describe('AVIF - AvifFileParser', () => {

const options = {tiff: true, icc: true, mergeOutput: false, translateKeys: false, translateValues: false, reviveValues: false}
const options = {tiff: true, icc: true, xmp: true, mergeOutput: false, translateKeys: false, translateValues: false, reviveValues: false}

const MAKE = 271
const MODEL = 272
Expand Down Expand Up @@ -34,4 +34,11 @@ describe('AVIF - AvifFileParser', () => {
assert.equal(output.icc[PROFILE], 'acsp')
})

it(`should extract XMP from fixture3`, async () => {
let input = await getFile('avif/IMG_20180725_163423-2.avif')
let output = await exifr.parse(input, options)
assert.exists(output.xmp, 'output should contain XMP')
assert.equal(output.xmp.CreatorTool, 'HDR+ 1.0.199571065z')
})

})