Hallo Zusammen,
you might guess, i´m interested in decoding raw jpeg data, i got in an byte[], to a SKBitmap.
I currently use the SKBitmap.Decode like :
bitmap = SKBitmap.Decode(buffer);
This does work fine and is gorgeous simple, but i´d like to have more performance. It´s not cirtical for smaller Images. But with larger Images like 800*1400, the decoding needs something between 30 and 100 ms on a faster device like the Samsung S7.
Do you know a better way to decode, when i exactly know the dimensions, colordepth and used jpeg codec ?
thanks in advance
Rouven
edit:
Okay, i checked the SKCodec, but it does not give my any Advantage, when i try to decode a larger Image at once.
I tried something like this:
using (SKData data = new SKData(buffer)) { using (SKCodec codec = SKCodec.Create(data)) { var info = codec.Info; bitmap = new SKBitmap(info.Width, info.Height, info.ColorType, info.IsOpaque ? SKAlphaType.Opaque : SKAlphaType.Premul); codec.GetPixels(bitmap.Info, bitmap.GetPixels()); } }
I guess, under the hood, it´s pretty much the same like:
bitmap = SKBitmap.Decode(buffer);
Any ideas, what i can do, to optimize decoding ?
@Rouven Using a codec directly allows you to decode a reduced image, nearest to your supported size:
int desiredWidth = 200; // create the codec SKCodec codec = SKCodec.Create(stream); SKImageInfo info = codec.Info; // get the scale that is nearest to what we want (eg: jpg returned 512) SKSizeI supportedScale = codec.GetScaledDimensions((float)desiredWidth / info.Width); // decode the bitmap at the nearest size SKImageInfo nearest = new SKImageInfo(supportedScale.Width, supportedScale.Height); SKBitmap bmp = SKBitmap.Decode(codec, nearest); // now scale that to the size that we want float realScale = (float)info.Height / info.Width; SKImageInfo desired = new SKImageInfo(desiredWidth, (int)(realScale * desiredWidth); bmp = bmp.Resize(desired), SKBitmapResizeMethod.Lanczos3);
Google does this for Android: https://github.com/google/skia/blob/master/src/codec/SkSampledCodec.cpp
I must still bind/implement this, but it may give some hints if the above doesn't help.
Answers
@Rouven Using a codec directly allows you to decode a reduced image, nearest to your supported size:
Google does this for Android: https://github.com/google/skia/blob/master/src/codec/SkSampledCodec.cpp
I must still bind/implement this, but it may give some hints if the above doesn't help.
Thank you, this is giving another option, to check out.
I'm having the same problem with slow loading.
SKCodec.GetPixels seems to be the one causing the long load time.