Chrome/dom/wasm32: fastest way to do [[Color; 1024]; 1024] to image

We have

pub struct Color { r: u8, g: u8, b: u8 }

pub struct Image([[Color; 1024]; 1024])

I am looking for the fastest way to take this Rust Image, convert it to a data url, which I can then load into the webpage via img src = ... .

Is there anything worth looking into besides the crate ?

[I think we might be able to do a bit better here, as compression doesn't really matter: we have a Rust vec; when Chrome displays it, it is probably going to be a flat expanded memory rep too; it seems kind of silly to send it through png or loss-less jpg round trip compression / decompression].


You could look at the way png encodes data and see of you can add an appropriate header to the raw pixel data to make it be considered a valid png file and then convert this to a Blob to use as image source. Or you could use the putImageData method for a canvas 2d context. I don't know which method is the fastest.

Is there an advantage of doing this via .png instead of say, a plain .bmp ?

Image file type and format guide - Web media technologies | MDN says that you should avoid BMP for new content. I think this recommendation is based on the lack of compression (not a problem here) and the fact that Microsoft holds patents on it.

Covered by the Microsoft Open Specification Promise; while Microsoft holds patents against BMP, they have published a promise not to assert its patent rights as long as specific conditions are met. This is not the same as a license, however. BMP is included under the Windows Metafile Format (.wmf).

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.