Update pnginfo.js

- getWebpMetadata: if (data[key]) {} because it can be undefined sometimes because of rebuild tags files by exiftool
- getAvifMetadata: complete overhall + validated test fir rebuild files with exiftool
This commit is contained in:
Eric 2024-05-27 08:11:04 -07:00 committed by GitHub
parent 9c402cfac3
commit 103b9ff0b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -130,11 +130,13 @@ export function getWebpMetadata(file) {
} }
let data = parseExifData(webp.slice(offset + 8, offset + 8 + chunk_length)); let data = parseExifData(webp.slice(offset + 8, offset + 8 + chunk_length));
for (var key in data) { for (var key in data) {
if (data[key]) {
var value = data[key]; var value = data[key];
let index = value.indexOf(':'); let index = value.indexOf(':');
txt_chunks[value.slice(0, index)] = value.slice(index + 1); txt_chunks[value.slice(0, index)] = value.slice(index + 1);
} }
} }
}
offset += 8 + chunk_length; offset += 8 + chunk_length;
} }
@ -146,28 +148,16 @@ export function getWebpMetadata(file) {
}); });
} }
/*
https://aomediacodec.github.io/av1-avif/#brands-overview
https://www.garykessler.net/library/file_sigs.html
heic
[4 byte offset]
66 74 79 70 68 65 69 63
ftypheic
avif
[4 byte offset]
66 74 79 70 61 76 69 66
ftypavif
*/
export function getAvifMetadata(file) { export function getAvifMetadata(file) {
return new Promise((r) => { return new Promise((r) => {
const reader = new FileReader(); const reader = new FileReader();
reader.onload = (event) => { reader.onload = (event) => {
const avif = new Uint8Array(event.target.result); const avif = new Uint8Array(event.target.result);
// console.log('event.target.result',event.target.result); // this is a decimal dump of the file: 0, 0, 0, 32, 102, == 00 00 00 20 ..
// console.log('avif',avif);
const dataView = new DataView(avif.buffer); const dataView = new DataView(avif.buffer);
// console.log('dataView',dataView);
// https://aomediacodec.github.io/av1-avif/#brands-overview
// https://www.garykessler.net/library/file_sigs.html
// Check that the AVIF signature is present: [4 byte offset] ftypheic or ftypavif // Check that the AVIF signature is present: [4 byte offset] ftypheic or ftypavif
// console.log('avif dataView.getUint32(4)',dataView.getUint32(4)); // 1718909296 = 0x66747970 = ftyp // console.log('avif dataView.getUint32(4)',dataView.getUint32(4)); // 1718909296 = 0x66747970 = ftyp
// console.log('avif dataView.getUint32(8)',dataView.getUint32(8)); // 1635150182 = 0x61766966 = avif // console.log('avif dataView.getUint32(8)',dataView.getUint32(8)); // 1635150182 = 0x61766966 = avif
@ -179,6 +169,55 @@ export function getAvifMetadata(file) {
return; return;
} }
// Start searching for Exif chunks after the AVIF signature
/*
we have in this order:
- signature that starts at 0x4 and is 8-byte long: ftypavif most likely
- 4-byte 00 space
- multiple 4-byte words defining how the avif was build, ending with 00 00
- examples above have avifmiflmiaf or avifmiflmiafMA1B
- Then the core image definition length on 2-bytes: examples above are 0x133 nd 0x12B long, ending just before the 8-byte mdat section
- mdat section is 8-byte long
- then we finally have the 6-byte Exif\0\0 followed by the IFD definitions, and then the Exif chunks
we start at offset 4 + 8 + 4 = 0x10
then we slice n 4-byte words until the last one starts with 00 00: `0x10 + 0x4n` = offset for core metadata size (meta)
then we read core metadata size offset as a 4-byte word (meta)
then we add `0x10 + 0x4n + 4 + meta + 8` = offset for Exif\0\0 section!
*/
let metaOffset = 0x10
while (metaOffset < avif.length / 2) {
let word = String.fromCharCode(...avif.slice(metaOffset, metaOffset+4));
if (word.slice(0,2) != "\0\0") {
metaOffset += 4
} else break;
}
let metaSize = dataView.getUint32(metaOffset);
let offset = metaOffset + 4 + metaSize + 8;
// Now we calculate offsetChunk_length = offset for the Exif chunk size
/*
We start from metaOffset + 4 = offset for the whole meta section. As seen above, each section length in meta is defined by the last 1 or 2-byte, more or less.
we set slice = 0xC
as long as the current 4-byte word is not == iloc:
we slice and get the last 2 as length for the next section;
we offset + slice and
if next 4-byte word is not iloc, loop: slice = the last 2
if next 4-byte word is iloc, we offset + 0x2C and this is the offsetChunk_length!
read chunk_length and move on
*/
let offsetChunk_length = metaOffset + 4;
let slice = 0xC;
while (offsetChunk_length < avif.length / 2) {
let word = String.fromCharCode(...avif.slice(offsetChunk_length, offsetChunk_length+4));
if (word != "iloc") {
offsetChunk_length += slice; // next offset to read from
slice = dataView.getUint16(offsetChunk_length - 2); // get new slice length
} else break;
}
offsetChunk_length += 0x2C;
let chunk_length = dataView.getUint32(offsetChunk_length);
/* /*
dataView.getUint32() reads 4 bytes starting at the specified byte offset of DataView dataView.getUint32() reads 4 bytes starting at the specified byte offset of DataView
dataView.getUint16() reads 2 bytes starting at the specified byte offset of DataView dataView.getUint16() reads 2 bytes starting at the specified byte offset of DataView
@ -190,7 +229,7 @@ export function getAvifMetadata(file) {
Some Exif color data starts at 0xCA offset, but since we (we = save image custom nodes) add more Exif metadata, Some Exif color data starts at 0xCA offset, but since we (we = save image custom nodes) add more Exif metadata,
we have our own block starting at 0x15F. I generated many avif and they all start at 0x15F. we have our own block starting at 0x15F. I generated many avif and they all start at 0x15F.
We can assume valid avif should come from those custom nodes anyways. Don't you think? We can assume valid avif should come from those custom nodes anyways. Don't you think?
45 78 69 66 00 00 starts at 0x15F: Exif + \0\0 = 6-bytes 45 78 69 66 00 00 starts at offset = metaOffset + 4 + metaSize + 8: Exif + \0\0 = 6-bytes
E x i f 00 00 E x i f 00 00
Tiff header after is 0xA long (10 bytes) and contains information about byte-order of IFD sections and a pointer to the 0th IFD Tiff header after is 0xA long (10 bytes) and contains information about byte-order of IFD sections and a pointer to the 0th IFD
Tiff header: Tiff header:
@ -198,8 +237,9 @@ export function getAvifMetadata(file) {
-----| | -----| |
49 49| | (II for Intel) if the byte-order is little-endian 49 49| | (II for Intel) if the byte-order is little-endian
4D 4D| | (MM for Motorola) for big-endian 4D 4D| | (MM for Motorola) for big-endian
|00 2A| magic bytes 0x00 0x2A (42, haha...) |00 2A| magic bytes 0x002A (=42, haha...)
|00 2A| | following 4 bytes will tell the offset to the 0th IFD from the start of the TIFF header. | 08| following 4-byte will tell the offset to the 0th IFD from the start of the TIFF header.
| 02 last 2-byte seem like the number of extra Exif IFD, we have indeed only 2 in this example
IFD Fields are 12-byte subsections of IFD sections IFD Fields are 12-byte subsections of IFD sections
For example, if we are looking for only 2 IFD fields with 0x9286/UserComment/prompt and 0x010e/ImageDescription/workflow: For example, if we are looking for only 2 IFD fields with 0x9286/UserComment/prompt and 0x010e/ImageDescription/workflow:
@ -218,32 +258,25 @@ export function getAvifMetadata(file) {
Prompt: {"... } 00 P starts at offset 0x30A1 (don't care) and is length 0x0F7C including 1 last \0 Prompt: {"... } 00 P starts at offset 0x30A1 (don't care) and is length 0x0F7C including 1 last \0
*/ */
// Start searching for Exif chunks after the AVIF signature
// console.log('avif dataView.getUint32(0x15F)',dataView.getUint32(0x15F)); // 1165519206 = 0x45786966 = Exif
let offset = 0x15F;
let txt_chunks = {} let txt_chunks = {}
// Loop through the chunks in the AVIF file // Loop through the chunks in the AVIF file
// while (offset < avif.length) {
const chunk_length = dataView.getUint32(0x8A, true);
// console.log('chunk_length',chunk_length) // 0x3EC0 and 0x3EC2 at 0x8A
// avif clearly are different beasts than webp, there is only one chunck of Exif data at the beginning. // avif clearly are different beasts than webp, there is only one chunck of Exif data at the beginning.
// If we ever come across one that is different, surely it's not been produced by a custom node and surely, the metadata is invalid // If we ever come across one that is different, surely it's not been produced by a custom node and surely, the metadata is invalid
// while (offset < (offset + chunk_length)) { // while (offset < (offset + chunk_length)) { // no need to scan the whole avif file
const chunk_type = String.fromCharCode(...avif.slice(offset, offset + 4)); const chunk_type = String.fromCharCode(...avif.slice(offset, offset + 6));
// console.log('chunk_type',chunk_type) if (chunk_type === "Exif\0\0") {
if (chunk_type === "Exif") {
if (String.fromCharCode(...avif.slice(offset, offset + 6)) == "Exif\0\0") {
offset += 6; offset += 6;
}
// parseExifData must start at the Tiff Header: 0x4949 or 0x4D4D for Big-Endian // parseExifData must start at the Tiff Header: 0x4949 or 0x4D4D for Big-Endian
let data = parseExifData(avif.slice(offset, offset + chunk_length)); let data = parseExifData(avif.slice(offset, offset + chunk_length));
for (var key in data) { for (var key in data) {
if (data[key]) {
var value = data[key]; var value = data[key];
let index = value.indexOf(':'); let index = value.indexOf(':');
txt_chunks[value.slice(0, index)] = value.slice(index + 1); txt_chunks[value.slice(0, index)] = value.slice(index + 1);
} }
} }
}
// offset += chunk_length; // offset += chunk_length;
// } // }