mirror of
https://github.com/GoogleChromeLabs/squoosh.git
synced 2025-11-14 09:39:15 +00:00
Decoder works
This commit is contained in:
@@ -1,35 +0,0 @@
|
|||||||
<!doctype html>
|
|
||||||
<input type="file" id="file">
|
|
||||||
<button id="go">Go</button>
|
|
||||||
<script type="module">
|
|
||||||
import encFactory from "./enc/basis_enc.js";
|
|
||||||
import decFactory from "./dec/basis_dec.js";
|
|
||||||
|
|
||||||
const {file, go} = document.all;
|
|
||||||
async function main() {
|
|
||||||
const encoder = await encFactory();
|
|
||||||
const bitmap = await createImageBitmap(file.files[0]);
|
|
||||||
const canvas = new OffscreenCanvas(bitmap.width, bitmap.height);
|
|
||||||
const ctx2 = canvas.getContext("2d");
|
|
||||||
ctx2.drawImage(bitmap, 0, 0);
|
|
||||||
const imgData = ctx2.getImageData(0, 0, canvas.width, canvas.height);
|
|
||||||
const result = encoder.encode(imgData.data, imgData.width, imgData.height);
|
|
||||||
const decoder = await decFactory();
|
|
||||||
console.log({result});
|
|
||||||
const resultImgData = decoder.decode(result);
|
|
||||||
console.log({resultImgData});
|
|
||||||
const cvs = document.createElement("canvas");
|
|
||||||
cvs.width = resultImgData.width;
|
|
||||||
cvs.height = resultImgData.height;
|
|
||||||
const ctx = cvs.getContext("2d");
|
|
||||||
ctx.putImageData(resultImgData, 0, 0);
|
|
||||||
document.body.append(cvs);
|
|
||||||
// const blob = new File([result], "lol.basis");
|
|
||||||
// const u = URL.createObjectURL(blob);
|
|
||||||
// const a = document.createElement("a")
|
|
||||||
// a.download = "lol.basis";
|
|
||||||
// a.href = u;
|
|
||||||
// a.click();
|
|
||||||
}
|
|
||||||
go.onclick = main;
|
|
||||||
</script>
|
|
||||||
@@ -97,6 +97,9 @@ async function decodeImage(
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
if (!canDecode) {
|
if (!canDecode) {
|
||||||
|
if (mimeType === 'image/basisu') {
|
||||||
|
return await workerBridge.basisDecode(signal, blob);
|
||||||
|
}
|
||||||
if (mimeType === 'image/avif') {
|
if (mimeType === 'image/avif') {
|
||||||
return await workerBridge.avifDecode(signal, blob);
|
return await workerBridge.avifDecode(signal, blob);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -152,7 +152,7 @@ const magicNumberMapInput = [
|
|||||||
[/^\x00\x00\x00 ftypavif\x00\x00\x00\x00/, 'image/avif'],
|
[/^\x00\x00\x00 ftypavif\x00\x00\x00\x00/, 'image/avif'],
|
||||||
[/^\xff\x0a/, 'image/jxl'],
|
[/^\xff\x0a/, 'image/jxl'],
|
||||||
[/^\x00\x00\x00\x0cJXL \x0d\x0a\x87\x0a/, 'image/jxl'],
|
[/^\x00\x00\x00\x0cJXL \x0d\x0a\x87\x0a/, 'image/jxl'],
|
||||||
[/^Bs\x10/, 'image/basisu'],
|
[/^sB/, 'image/basisu'],
|
||||||
] as const;
|
] as const;
|
||||||
|
|
||||||
export type ImageMimeTypes = typeof magicNumberMapInput[number][1];
|
export type ImageMimeTypes = typeof magicNumberMapInput[number][1];
|
||||||
|
|||||||
33
src/features/decoders/basis/worker/basisDecode.ts
Normal file
33
src/features/decoders/basis/worker/basisDecode.ts
Normal file
@@ -0,0 +1,33 @@
|
|||||||
|
/**
|
||||||
|
* Copyright 2020 Google Inc. All Rights Reserved.
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
* limitations under the License.
|
||||||
|
*/
|
||||||
|
import type { BasisModule } from 'codecs/basis/dec/basis_dec';
|
||||||
|
import wasmUrl from 'url:codecs/basis/dec/basis_dec.wasm';
|
||||||
|
import { initEmscriptenModule, blobToArrayBuffer } from 'features/worker-utils';
|
||||||
|
|
||||||
|
let emscriptenModule: Promise<BasisModule>;
|
||||||
|
|
||||||
|
export default async function decode(blob: Blob): Promise<ImageData> {
|
||||||
|
if (!emscriptenModule) {
|
||||||
|
const decoder = await import('codecs/basis/dec/basis_dec');
|
||||||
|
emscriptenModule = initEmscriptenModule(decoder.default, wasmUrl);
|
||||||
|
}
|
||||||
|
|
||||||
|
const [module, data] = await Promise.all([
|
||||||
|
emscriptenModule,
|
||||||
|
blobToArrayBuffer(blob),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const result = module.decode(data);
|
||||||
|
if (!result) throw new Error('Decoding error');
|
||||||
|
return result;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user