image icon indicating copy to clipboard operation
image copied to clipboard

Improve performance of grayscale function and better clone standards

Open MASACR99 opened this issue 2 years ago • 3 comments

Hey! I was working with your library and found out that the grayscale function was too slow, specially, compared to the examples you use, for example: DynamicImage::ImageLuma8(image.into_luma8());

After some investigation I found the issue inside the grayscale function, when using the code above there's a call to the dynamic map function which works much faster than the imageops::grayscale generic function.

I also changed a few unneeded conversions to just clone since the type is already the same (in this case for example ImageLumaA8 was being converted with imageops even though it's not needed)

After those changes the DynamicImage grayscale function is now faster than the example code above.

Pictures proving the new grayscale is faster:

Old performance: OldPerformance

New performance: NewPerformance

Edit: Forgot to remove comment from license agreement

I license past and future contributions under the dual MIT/Apache-2.0 license, allowing licensees to choose either at their option.

MASACR99 avatar Jan 18 '23 11:01 MASACR99

I'd feel better about this if we had some theory for why convert is faster than imageops::grayscale. Are we sure they're producing identical results?

fintelia avatar Jan 20 '23 06:01 fintelia

The conceptual difference: imageops::grayscale does an external loop over pixels, convert is the interior iteration version of this loop. Due imageops being generic it can't statically know the best iteration strategy for the layout, nor can it communicate the same bounds checks to the compiler. It's not very surprising to me.

HeroicKatora avatar Jan 20 '23 13:01 HeroicKatora

@fintelia Just ran a few tests to check that the resulting images of grayscale are correct. They are! I'll add a few images of the result of the "new" function grayscale_function_1 grayscale_function grayscale_function

MASACR99 avatar Jan 22 '23 20:01 MASACR99