image-rs / image

Encoding and decoding images in Rust
Apache License 2.0
4.93k stars 612 forks source link

Make DynamicImage serializable via serde #1381

Open fintelia opened 3 years ago

fintelia commented 3 years ago

Requested by @GalAster in this comment:

I really need serde features, I'm using bincode to transfer textures.

My current solution is to convert as bytes with img.write_to(&mut buf, ImageOutputFormat::Png) before transmission.

Then import bytes with image::load_from_memory_with_format(buf, ImageFormat::Png) after the transfer is complete.

In most cases, this is lossless, but I hope there is a better solution, and we can experiment with a feature flag that is turned off by default.

slackers123 commented 1 year ago

Are there any plans to continue this?

oovm commented 1 year ago

I added a basic support in #1903.

Png is not always available, so I added a type field to safely convert the byte stream to the corresponding type

oovm commented 1 year ago

I'm not familiar with bytemuck, is there an interface to cast Vec<u8> as Option<Vec16>>?

fintelia commented 1 year ago

Nope. Even if the Vec<u8> is properly aligned, the allocator may treat [u8] and [u16] differently so dropping the Vec<u16> may misbehave (because the slice was allocated with type u8 but freed via the u16 codepath).