22 lines
826 B
TypeScript
22 lines
826 B
TypeScript
import type { RasterImage } from './types';
|
|
|
|
export const MAX_SIDE = 2000;
|
|
|
|
export function fitSize(width: number, height: number, maxSide: number): { width: number; height: number } {
|
|
const s = Math.min(1, maxSide / Math.max(width, height));
|
|
return { width: Math.round(width * s), height: Math.round(height * s) };
|
|
}
|
|
|
|
export async function loadPhoto(file: Blob): Promise<RasterImage> {
|
|
const bitmap = await createImageBitmap(file, { imageOrientation: 'from-image' });
|
|
try {
|
|
const { width, height } = fitSize(bitmap.width, bitmap.height, MAX_SIDE);
|
|
const canvas = new OffscreenCanvas(width, height);
|
|
const ctx = canvas.getContext('2d')!;
|
|
ctx.drawImage(bitmap, 0, 0, width, height);
|
|
return { width, height, data: ctx.getImageData(0, 0, width, height).data };
|
|
} finally {
|
|
bitmap.close();
|
|
}
|
|
}
|