Skip to content

Commit

Permalink
add pngChunks function, remove dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
silverwind committed Feb 15, 2024
1 parent bd88a3a commit e84c41a
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 24 deletions.
17 changes: 0 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"monaco-editor": "0.46.0",
"monaco-editor-webpack-plugin": "7.1.0",
"pdfobject": "2.2.12",
"png-chunks-extract": "1.0.0",
"pretty-ms": "9.0.0",
"sortablejs": "1.15.2",
"swagger-ui-dist": "5.11.3",
Expand Down
1 change: 0 additions & 1 deletion web_src/js/features/comp/ImagePaste.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ const uploadClipboardImage = async (editor, dropzone, e) => {
} else {
text = `![${name}](${url})`;
}

editor.replacePlaceholder(placeholder, text);

const $input = $(`<input name="files" type="hidden">`).attr('id', uuid).val(uuid);
Expand Down
26 changes: 21 additions & 5 deletions web_src/js/utils/image.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
import pngChunksExtract from 'png-chunks-extract';
export function pngChunks(data) {
const view = new DataView(data.buffer, 0);
if (view.getBigUint64(0) !== 9894494448401390090n) throw new Error(`Invalid png header`);

const decoder = new TextDecoder();
const chunks = [];
let index = 8;
while (index < data.length) {
const len = view.getUint32(index);
chunks.push({
name: decoder.decode(data.slice(index + 4, index + 8)),
data: data.slice(index + 8, index + 8 + len),
});
index += len + 12;
}

return chunks;
}

export async function pngInfo(blob) {
let width = 0;
Expand All @@ -8,14 +25,13 @@ export async function pngInfo(blob) {
if (blob.type !== 'image/png') return {width, dppx};

try {
const buffer = await blob.arrayBuffer();
const chunks = pngChunksExtract(new Uint8Array(buffer));
const chunks = pngChunks(new Uint8Array(await blob.arrayBuffer()));

// extract width from mandatory IHDR chunk
const ihdr = chunks.find((chunk) => chunk.name === 'IHDR');
if (ihdr?.data?.length) {
const View = new DataView(ihdr.data.buffer, 0);
width = View.getUint32(0);
const view = new DataView(ihdr.data.buffer, 0);
width = view.getUint32(0);
}

// extract dppx from optional pHYs chunk, assuming pixels are square
Expand Down
10 changes: 10 additions & 0 deletions web_src/js/utils/image.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import {pngChunks} from './image.js';

test('pngChunks', async () => {
const blob = await (await globalThis.fetch('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAADUlEQVQIHQECAP3/AAAAAgABzePRKwAAAABJRU5ErkJggg==')).blob();
expect(pngChunks(new Uint8Array(await blob.arrayBuffer()))).toEqual([
{name: "IHDR", data: new Uint8Array([0, 0, 0, 1, 0, 0, 0, 1, 8, 0, 0, 0, 0])},
{name: "IDAT", data: new Uint8Array([8, 29, 1, 2, 0, 253, 255, 0, 0, 0, 2, 0, 1])},
{name: "IEND", data: new Uint8Array([])},
]);
});

0 comments on commit e84c41a

Please sign in to comment.