You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
changed the title [-]If the image is png format, the function GetScaledDimensions of SKCodec always return the original size?[/-][+]How to decode and scale down a png image?[/+]on Feb 9, 2017
It might be the case that the PNG decoder can't actually do downscaling during the load operation. So, to scale an image you will have to load it and then resize it:
@mattleibow
Thanks for your reply.
But what I want is loading a scale down image into memory.
If I do as you said
"var bitmap = SKBitmap.Decode("file.png");
var scaled = bitmap.Resize(new SKImageInfo(newWidth, newHeight), SKBitmapResizeMethod.Lanczos3);"
and if the png file is very big, it may cause "OutOfMemoryException "
I just want to put this here for the record. This is how you can scale some codecs (.ico, .jpg, .raw, .webp) when loading:
intdesiredWidth=200;// create the codecSKCodeccodec=SKCodec.Create(stream);SKImageInfoinfo=codec.Info;// get the scale that is nearest to what we want (eg: jpg returned 512)SKSizeIsupportedScale=codec.GetScaledDimensions((float)desiredWidth/info.Width);// decode the bitmap at the nearest sizeSKImageInfonearest=newSKImageInfo(supportedScale.Width,supportedScale.Height);SKBitmapbmp=SKBitmap.Decode(codec,nearest);// now scale that to the size that we wantfloatrealScale=(float)info.Height/info.Width;SKImageInfodesired=newSKImageInfo(desiredWidth,(int)(realScale*desiredWidth);bmp=bmp.Resize(desired), SKBitmapResizeMethod.Lanczos3);
@JessieZM I am still looking to see what can be done, but I am not too hopeful since it has to be supported by the underlying image and the codec.
From that thread, it looks like Android's Codec has an option to limit the sampling on decoding for PNG:
SkCodec supports scaled jpeg and webp decodes because the native decoding libraries (libjpeg-turbo, libwebp) support downscaling. Ex: the jpeg library can perform a faster decode when downscaling because it will drop some of the DCT coefficients. There's no similar option for png.
@deakjahn This is due to the limitations of some of the codec decoding processes not being able to resize the image at the same time. @mattleibow is going to investigate change the decode to apply the resize after the image is decoded.
Ohh, that was some time ago. I switched to only using the dimensions returned by codec.GetScaledDimensions() and those always work. Not necessarily the same 256×256 or whatever I originally wanted, but close enough for me.
The point of the process is to have a cheaper decoding process (less time, less memory), so doing a full decode and then resize is not something Matthew should do, I guess. That goes against the benefit we can get from the process.
Activity
[-]If the image is png format, the function GetScaledDimensions of SKCodec always return the original size?[/-][+]How to decode and scale down a png image?[/+]mattleibow commentedon Feb 9, 2017
It might be the case that the PNG decoder can't actually do downscaling during the load operation. So, to scale an image you will have to load it and then resize it:
There is more info on the docs website:
https://developer.xamarin.com/api/member/SkiaSharp.SKBitmap.Resize/p/SkiaSharp.SKImageInfo/SkiaSharp.SKBitmapResizeMethod/
AwsomeCode commentedon Feb 9, 2017
@mattleibow are this resized images can be saved to disk also?
mattleibow commentedon Feb 9, 2017
@AwsomeCode Yes:
JessieZM commentedon Feb 10, 2017
@mattleibow
Thanks for your reply.
But what I want is loading a scale down image into memory.
If I do as you said
"var bitmap = SKBitmap.Decode("file.png");
var scaled = bitmap.Resize(new SKImageInfo(newWidth, newHeight), SKBitmapResizeMethod.Lanczos3);"
and if the png file is very big, it may cause "OutOfMemoryException "
Could you please have a look at this article https://developer.xamarin.com/recipes/android/resources/general/load_large_bitmaps_efficiently/
mattleibow commentedon Feb 11, 2017
I just want to put this here for the record. This is how you can scale some codecs (.ico, .jpg, .raw, .webp) when loading:
@JessieZM I am still looking to see what can be done, but I am not too hopeful since it has to be supported by the underlying image and the codec.
mattleibow commentedon Feb 11, 2017
@JessieZM I am going to as on the forums: https://groups.google.com/forum/#!forum/skia-discuss
But I don't think this is possible for .png as not even Android does it. My code:
Android docs: https://developer.android.com/topic/performance/graphics/load-bitmap.html
mattleibow commentedon Feb 11, 2017
@JessieZM I created a post here: https://groups.google.com/forum/#!topic/skia-discuss/_sfXByrGL08
Watch that and see what the experts say. I will of course bind any members that you need.
migueldeicaza commentedon Mar 29, 2017
From that thread, it looks like Android's Codec has an option to limit the sampling on decoding for PNG:
deakjahn commentedon Jul 4, 2018
Is this supposed to still work in the current version? With
all I get is a null for bitmap. Without the extra info, it works all right. Images are JPGs in the 3,000-4,000 pixel wide range.
newky2k commentedon Oct 26, 2018
@deakjahn This is due to the limitations of some of the codec decoding processes not being able to resize the image at the same time. @mattleibow is going to investigate change the decode to apply the resize after the image is decoded.
Regards
deakjahn commentedon Oct 26, 2018
Ohh, that was some time ago. I switched to only using the dimensions returned by
codec.GetScaledDimensions()and those always work. Not necessarily the same 256×256 or whatever I originally wanted, but close enough for me.The point of the process is to have a cheaper decoding process (less time, less memory), so doing a full decode and then resize is not something Matthew should do, I guess. That goes against the benefit we can get from the process.
newky2k commentedon Nov 14, 2018
Seems to be resolved now