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