Cleanup webp import code a bit.

This commit is contained in:
comfyanonymous 2023-10-28 12:24:50 -04:00
parent 2a134bfab9
commit aac8fc99d6
1 changed files with 11 additions and 15 deletions

View File

@ -108,29 +108,25 @@ export function getWebpMetadata(file) {
return new Promise((r) => {
const reader = new FileReader();
reader.onload = (event) => {
// Get the PNG data as a Uint8Array
const pngData = new Uint8Array(event.target.result);
const dataView = new DataView(pngData.buffer);
const webp = new Uint8Array(event.target.result);
const dataView = new DataView(webp.buffer);
// Check that the PNG signature is present
// Check that the WEBP signature is present
if (dataView.getUint32(0) !== 0x52494646 || dataView.getUint32(8) !== 0x57454250) {
console.error("Not a valid WEBP file");
r();
return;
}
// Start searching for chunks after the PNG signature
// Start searching for chunks after the WEBP signature
let offset = 12;
let txt_chunks = {};
// Loop through the chunks in the PNG file
while (offset < pngData.length) {
// Get the length of the chunk
const length = dataView.getUint32(offset + 4, true);
// Get the chunk type
const type = String.fromCharCode(...pngData.slice(offset, offset + 4));
if (type === "EXIF") {
// Get the keyword
let data = parseExifData(pngData.slice(offset + 8, offset + 8 + length));
// Loop through the chunks in the WEBP file
while (offset < webp.length) {
const chunk_length = dataView.getUint32(offset + 4, true);
const chunk_type = String.fromCharCode(...webp.slice(offset, offset + 4));
if (chunk_type === "EXIF") {
let data = parseExifData(webp.slice(offset + 8, offset + 8 + chunk_length));
for (var key in data) {
var value = data[key];
let index = value.indexOf(':');
@ -138,7 +134,7 @@ export function getWebpMetadata(file) {
}
}
offset += 8 + length;
offset += 8 + chunk_length;
}
r(txt_chunks);