I want to convert an 8-bit grayscale JPEG to a 32-bit argb JPEG using a GPU function, while not using CPU resources.
Below is my SharpDX code which is 8-bit JPEG to 32-bit JPEG convert using a CPU function and I want to replace it with a GPU function.
using (MemoryStream ms = new MemoryStream(viewModelResultImage.JpegData))
{
var decoder = new SharpDX.WIC.BitmapDecoder(_wicFactory, ms, DecodeOptions.CacheOnLoad);
var frame = decoder.GetFrame(0);
var formatConverter = new FormatConverter(_wicFactory);
formatConverter.Initialize(frame, SharpDX.WIC.PixelFormat.Format32bppPBGRA);
BitmapUp1Channel0 = D2D1.Bitmap.FromWicBitmap(_renderTarget, formatConverter);
}
I used a CPU function before and it took 300ms to convert a 9344 X 7000 8-bit JPEG to 32-bit.
EDIT : it's 8bit > 32bit argb so I changed context (I noted earlier 8bit > 24bit)
Whenever you are concerned about performance the first step should be to measure. You have apparently done that, but it is also very useful to do some profiling to figure out what part takes time.
My guess is that it is not the actual 8bit -> 24bit conversion that takes time, since this is very simple work. My own multi threaded but non-SIMD code does that on the order of 10ms. A Well optimized SIMD version would likely be bandwidth limited rather than compute limited. So using a GPU will likely be slower due to the extra time transferring the image to GPU memory and back again.
If you include the decoding/encoding time in your measurements this is likely where most of the time is spent. So can the GPU assist in jpeg encoding/decoding? Kind of. Data compression is particularly unsuited for GPUs since there is an inherent data dependency. But media compression usually include a transformation step, often a Discrete cosine transform, that is suitable for GPUs. So a hybrid approach may be feasible.
A quick google for "gpu jpeg library" suggest that there are several alternatives. see https://softwarerecs.stackexchange.com/ if you want recommendations.
The downside with any GPU solution is that compatibility issues are common. So I would at least look for faster CPU based libraries, maybe https://libjpeg-turbo.org/?
There might also be ways convert the file without actually decoding the data. Jpeg should use the YCbCr color space, so a grayscale image should simply have empty Cb & Cr channels. But you will likely need to study the jpeg format on how to do this correctly.