Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support use of resize() function in Web Workers #612

Closed
852Kerfunkle opened this issue Jul 8, 2022 · 5 comments
Closed

Support use of resize() function in Web Workers #612

852Kerfunkle opened this issue Jul 8, 2022 · 5 comments
Labels
feature New enhancement or request package:functions

Comments

@852Kerfunkle
Copy link

Is your feature request related to a problem? Please describe.
It would be nice to have an alternative for resizing images in a webworker, if that is somehow possible (that's half the problem I'm trying to solve with glTF-Transform).

Describe the solution you'd like
Looking at ndarray-pixels (browser-get-pixels.ts), since it uses Image (not available in web workers) as well as document.createElement('canvas') (can probably be worked around with new OffscreenCanvas()), that might be tricky.

I'd be happy to contribute something (to ndarray-pixels?), if you let me know what the requirements are (regarding dependencies, etc).

@852Kerfunkle 852Kerfunkle added the feature New enhancement or request label Jul 8, 2022
@852Kerfunkle
Copy link
Author

So, there's ImageBitmap and createImageBitmap now.

This works (in chromium at least):

    for (const t of document.getRoot().listTextures()) {
        const image = t.getImage();
        
        if (image) {
            const res = await createImageBitmap(new Blob([image]), {resizeWidth: width, resizeHeight: height});

            const canvas: any = new OffscreenCanvas(width, height);
            const context = canvas.getContext('2d');
            context.drawImage(res, 0, 0);

            const blob: Blob = await canvas.convertToBlob({type: t.getMimeType()});

            t.setImage(new Uint8Array(await blob.arrayBuffer()));
        }
    }

But probably isn't very widely supported.

@donmccurdy
Copy link
Owner

I'd be glad to take a PR on ndarray-pixels for this (or something similar) if you're willing to try that! Otherwise TBD, I've been hoping squoosh/lib would get Web support eventually and would solve some of these issues, but unclear when/if that will happen.

@donmccurdy donmccurdy added this to the Backlog milestone Jul 9, 2022
@donmccurdy donmccurdy changed the title Using textureResize in WebWorker: ReferenceError: Image is not defined Support use of resize() function in Web Workers Jul 9, 2022
@852Kerfunkle
Copy link
Author

852Kerfunkle commented Jul 10, 2022

Sure, gladly would contribute. Would you be alright with throwing exceptions if browser support for required functions (createImageBitmap, OffscreenCanvas, offscreen canvas 2d support, convertToBlob) is missing - for the inside web worker case, that is?

@donmccurdy
Copy link
Owner

Sorry for the delayed reply here! I'd be fine with exceptions being thrown. I'd probably just let the browser hit these errors rather than explicitly checking for support.

@donmccurdy
Copy link
Owner

After #775 we now have two implementations of texture resizing within this project, one that is cross-platform with ndarray-pixels, and another that is much faster but Node.js-only, based on Sharp. I think I've done as much as I am willing to do within this codebase to make this feature cross-platform, but unfortunately none of these implementations support Web Workers today.

I'd still be glad to discuss this on the ndarray-pixels repository, if someone wants to make a PR there. Another option would be too look into replacing sharp (used by textureCompress now) with wasm-vips. That might get us the best of all worlds — good performance and cross-platform support, but I don't know yet whether wasm-vips runs in a Web Worker either.

@donmccurdy donmccurdy closed this as not planned Won't fix, can't repro, duplicate, stale Jan 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New enhancement or request package:functions
Projects
None yet
Development

No branches or pull requests

2 participants