SaveLossyImage and threading

Hi @CvK,

we’d have to have a detailed look, but what you write seems consistent with the measurements I’ve done for this post. With your roughly 26 MPixel images I’d expect roughly 200 ms on my machine for saving these (haven’t tested so far), which is somewhat more than the 100ms you’re measuring.

Saving bitmap files internally uses C++'s std::fstream for input/output. On VC these are unfortunaly slower than the C methods fopen and its cousins (interestingly, this doesn’t seem to be the case for the gcc) which is probably your limiting factor for BMP writing - at least in those scenarios where the memory layout allows for direct streaming. As soon as the memory has to be reordered, reordering becomes the bottleneck.

If copying from the :cvb: image to the System.Drawing.Bitmap now is your bottleneck there is in fact a way for your code to become faster: Even in the case of linear VPATs the CSIMG2Bitmap tutorial copies pixel by pixel. However, there are scenarios where you can get a lot faster by copying entire lines or even the whole block of pixels (basically the x increment has to equal the pixel’s size and the y increment has to equal the target bitmap’s stride for this to be possible - so you’ll need to check the return values of GetLinearAccess for compliance with these requirements). Copying blocks of unmanaged memory in C# requires some additional DllImport magic but it’s feasible and I’d expect the copying of the pixel data to become faster faster by a factor of somewhere between 2 and 3 if you can at least do it line by line.

2 Likes