Skip to content

Commit 1495a71

Browse files
authored
impr: Better image size detection for texture.write() (#1893)
1 parent 18f7ac5 commit 1495a71

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

packages/typegpu/src/core/texture/textureUtils.ts

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,36 @@ import type { ExternalImageSource } from './texture.ts';
77
export function getImageSourceDimensions(
88
source: ExternalImageSource,
99
): { width: number; height: number } {
10-
if ('displayWidth' in source && 'displayHeight' in source) {
11-
return { width: source.displayWidth, height: source.displayHeight };
10+
// We used to do it this way but it fails on React Native (ImageBitmap is probably a proxy there)
11+
// if ('displayWidth' in source && 'displayHeight' in source) {
12+
// return { width: source.displayWidth, height: source.displayHeight };
13+
// }
14+
15+
const { videoWidth, videoHeight } = source as HTMLVideoElement;
16+
if (videoWidth && videoHeight) {
17+
return { width: videoWidth, height: videoHeight };
1218
}
13-
return { width: source.width as number, height: source.height as number };
19+
20+
const { naturalWidth, naturalHeight } = source as HTMLImageElement;
21+
if (naturalWidth && naturalHeight) {
22+
return { width: naturalWidth, height: naturalHeight };
23+
}
24+
25+
const { codedWidth, codedHeight } = source as VideoFrame;
26+
if (codedWidth && codedHeight) {
27+
return { width: codedWidth, height: codedHeight };
28+
}
29+
30+
const { width, height } = source as ImageBitmap || HTMLCanvasElement ||
31+
OffscreenCanvas || HTMLImageElement || ImageData;
32+
33+
if (!width || !height) {
34+
throw new Error(
35+
'Cannot determine dimensions of the provided image source.',
36+
);
37+
}
38+
39+
return { width, height };
1440
}
1541

1642
type CachedResources = {

0 commit comments

Comments
 (0)