这是indexloc提供的服务,不要输入任何密码
Skip to content

When running on Flutter web, the TextureLoader doesn't know how to read the dimensions of a PNG image if the TextureLoader.fromBytes constructor is used, thus throwing an exception. #63

@MihMihai

Description

@MihMihai

Hi!

I've noticed that when running on flutter web and loading a texture using the TextureLoader.fromBytes constructor, the current algorithm uses the JPEG header (SOI marker) descriptor to figure out the dimensions of the image based on the data in the bytes array. However, if the image is a PNG file, the algorithm does nothing and the ImageElement width and height remain at 0, which throws an exception when applying a logarithmic operation on 0.

The issue lies in the image_loader_web at line 70: https://github.com/Knightro63/three_js/blob/main/packages/three_js_core_loaders/lib/ImageLoader/image_loader_web.dart#L70. The JPEG header is different to that of a PNG.

From my understanding and from what I could find on the web, it seems that it's possible to retrieve the image's dimensions, irrespective of its type (as long as the format is supported) like so:

final codec = await ui.instantiateImageCodec(bytes);
final frameInfo = await codec.getNextFrame();
width = frameInfo.image.width;
height = frameInfo.image.height;
frameInfo.image.dispose();

And the code can be run from both web and mobile.
The documentation of the instantiateImageCodec doesn't say anything about it being an expensive operation, so I assume it's safe to use it in this case.

Furthermore, I haven't found a PNG specification or some code that would be able to retrieve the dimensions of a PNG image from its bytes array (similar to the JPEG code you have), but maybe you will be luckier or maybe you have more insights into this.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions