feat: add view transform, photo loading and download helpers
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0a97cb88af
commit
99cafc0b55
5 files changed
+102
No files matched your search
@@ -0,0 +1,21 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user