Skip to content

Commit

Permalink
Cleanup webp import code a bit.
Browse files Browse the repository at this point in the history
  • Loading branch information
comfyanonymous committed Oct 28, 2023
1 parent 2a134bf commit aac8fc9
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions web/scripts/pnginfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,37 +108,33 @@ 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(':');
txt_chunks[value.slice(0, index)] = value.slice(index + 1);
}
}

offset += 8 + length;
offset += 8 + chunk_length;
}

r(txt_chunks);
Expand Down

0 comments on commit aac8fc9

Please sign in to comment.