mtpng icon indicating copy to clipboard operation
mtpng copied to clipboard

Pass an Iterator as source (for BGR->RGB for example)

Open Zerowalker opened this issue 3 years ago • 1 comments

Is it possible to add the ability to pass an iterator into the encoder (without having an effect on performance)? I tried looking through the code but it was a bit too complex for me.

Why I ask is that in case you have BGR data, you currently have to convert it beforehand. Which means going through everything just to convert it, it's very fast though, but I would imagine it being faster if it could be done when the data is actually being encoded.

Here's an example of BGR to RGB:

pub fn bgr_to_rgb(b: &mut [u8]) {
    // swap R<->B to convert BGR->RGB
    b.chunks_exact_mut(3).for_each(|l| l.swap(0, 2));
}

Now if we could pass an iterator the swap could be done on-the-fly which might be more cache friendly as well.

Thoughts on this?

Zerowalker avatar Jan 11 '23 09:01 Zerowalker

Hmmm, seems plausible in principle! Doing the swizzle while data's hot is good for cache, and should be vectorization-friendly.

Main holdup off the top of my head -- we need random access to run filters in parallel, so the API needs to provide for creating a separate iterator for each chunk.

bvibber avatar Jan 11 '23 11:01 bvibber