Its working now

This commit is contained in:
Surma
2021-05-18 20:02:43 +01:00
parent eb8204d69b
commit c417bd0a7a
3 changed files with 66 additions and 9 deletions

View File

@@ -192,17 +192,35 @@ interface DrawableToImageDataOptions {
sh?: number;
}
function getWidth(
drawable: ImageBitmap | HTMLImageElement | VideoFrame,
): number {
if ('displayWidth' in drawable) {
return drawable.displayWidth;
}
return drawable.width;
}
function getHeight(
drawable: ImageBitmap | HTMLImageElement | VideoFrame,
): number {
if ('displayHeight' in drawable) {
return drawable.displayHeight;
}
return drawable.height;
}
export function drawableToImageData(
drawable: ImageBitmap | HTMLImageElement,
drawable: ImageBitmap | HTMLImageElement | VideoFrame,
opts: DrawableToImageDataOptions = {},
): ImageData {
const {
width = drawable.width,
height = drawable.height,
width = getWidth(drawable),
height = getHeight(drawable),
sx = 0,
sy = 0,
sw = drawable.width,
sh = drawable.height,
sw = getWidth(drawable),
sh = getHeight(drawable),
} = opts;
// Make canvas same size as image

View File

@@ -21,6 +21,6 @@ export async function decode(
// Non-obvious way to turn an Blob into a ReadableStream
data: new Response(blob).body!,
});
const result = await decoder.decode();
return drawableToImageData(result.image);
const { image } = await decoder.decode();
return drawableToImageData(image);
}

View File

@@ -18,8 +18,47 @@ interface ImageDecodeResult {
complete: boolean;
}
// Absolutely not correct, but its all we need and Im lazy.
type VideoFrame = ImageBitmap;
// I didnt do all the types because the class is kinda complex.
// I focused on what we need.
declare class VideoFrame {
codedWidth: number;
codedHeight: number;
cropLeft: number;
cropTop: number;
cropWidth: number;
cropHeight: number;
displayWidth: number;
displayHeight: number;
clone(): VideoFrame;
close(): void;
}
interface CanvasDrawImage {
drawImage(
image: CanvasImageSource | VideoFrame,
dx: number,
dy: number,
): void;
drawImage(
image: CanvasImageSource | VideoFrame,
dx: number,
dy: number,
dw: number,
dh: number,
): void;
drawImage(
image: CanvasImageSource | VideoFrame,
sx: number,
sy: number,
sw: number,
sh: number,
dx: number,
dy: number,
dw: number,
dh: number,
): void;
}
declare class ImageDecoder {
static isTypeSupported(type: string): Promise<boolean>;