ndarray icon indicating copy to clipboard operation
ndarray copied to clipboard

Correct (fast) way to convert `Array2<RGBA8>` into `Array3<u8>`?

Open fzyzcjy opened this issue 3 years ago • 2 comments
trafficstars

Hi thanks for the lib!

Context: I have a color image Array2<RGBA8>, and want to visualize it using rust-numpy lib (a binding to python numpy). However, .to_pyarray() complains, so I should convert it into Array3<u8>.

Question: How can I do that - what is the correct/fast way to do so? IMHO the fastest way should even have no memory copy at all, since RGBA8 is compact 4-byte data. But one memory copy is also acceptable.

I have come up with a very slow method but I hope there are other ways: extract Array2<RGBA8> into four Array<u8> for each of 4 channels, and stack! them together. Its performance seems to be quite slow.

fzyzcjy avatar Nov 29 '21 09:11 fzyzcjy

That sounds like a use case for expand that was discussed and prototyped here: https://github.com/rust-ndarray/ndarray/pull/1107#issuecomment-974649266

We'll get this for [T; 4] for sure. Now the question is how to enable the same thing for a type like RGBA8, a third party type. Please mention what crate defines it.

bluss avatar Nov 29 '21 18:11 bluss

@bluss crate: rgb https://docs.rs/rgb/0.8.27/rgb/ . Thanks!

fzyzcjy avatar Nov 30 '21 00:11 fzyzcjy