Skip to content

How to decode and scale down a png image? #236

Closed

Description

@JessieZM

If the image is png format, the function GetScaledDimensions of SKCodec always return the original size?

for eg:

public SKSizeI GetScaledDimensions(float desiredScale) and desiredScale = 0.5f;

but the return is still the original size.

So how we scale down a png image ?

Activity

  1. 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
  2. mattleibow commented on Feb 9, 2017

    @mattleibow
    Contributor

    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:

    var bitmap = SKBitmap.Decode("file.png");
    var scaled = bitmap.Resize(new SKImageInfo(newWidth, newHeight), SKBitmapResizeMethod.Lanczos3);

    There is more info on the docs website:
    https://developer.xamarin.com/api/member/SkiaSharp.SKBitmap.Resize/p/SkiaSharp.SKImageInfo/SkiaSharp.SKBitmapResizeMethod/

  3. AwsomeCode commented on Feb 9, 2017

    @AwsomeCode

    @mattleibow are this resized images can be saved to disk also?

  4. mattleibow commented on Feb 9, 2017

    @mattleibow
    Contributor

    @AwsomeCode Yes:

    SKBitmap bitmap = ...;
    SKImage image = SKImage.FromBitmap(bitmap);
    SKData png = image.Encode(SKImageEncodeFormat.Png, 100);
    
    // save
    using (var filestream = File.OpenWrite("image.png")) {
        png.SaveTo(filestream);
    }
  5. JessieZM commented on Feb 10, 2017

    @JessieZM
    Author

    @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/

  6. mattleibow commented on Feb 11, 2017

    @mattleibow
    Contributor

    I just want to put this here for the record. This is how you can scale some codecs (.ico, .jpg, .raw, .webp) when loading:

    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);

    @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.

  7. mattleibow commented on Feb 11, 2017

    @mattleibow
    Contributor

    @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:

    var input = Assets.Open("baboon.png");
    var reqHeight = 100;
    var reqWidth = 200;
    
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.InJustDecodeBounds = true;
    
    BitmapFactory.DecodeStream(input, null, options);
    int height = options.OutHeight;
    int width = options.OutWidth;
    
    int inSampleSize = 1;
    
    if (height > reqHeight || width > reqWidth)
    {
    
    	int halfHeight = height / 2;
    	int halfWidth = width / 2;
    
    	while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth)
    	{
    		inSampleSize *= 2;
    	}
    }
    
    options.InSampleSize = inSampleSize;
    options.InJustDecodeBounds = false;
    var bitmap = BitmapFactory.DecodeStream(input, null, options);

    Android docs: https://developer.android.com/topic/performance/graphics/load-bitmap.html

  8. mattleibow commented on Feb 11, 2017

    @mattleibow
    Contributor

    @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.

  9. migueldeicaza commented on Mar 29, 2017

    @migueldeicaza
    Contributor

    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.

    However, SkAndroidCodec (wraps SkCodec) allows for decoding any format with a fSampleSize specified in AndroidOptions. fSampleSize=N instructs the codec to sample every Nth pixel as it decodes. This should do what you want. https://github.com/google/skia/blob/master/include/codec/SkAndroidCodec.h

  10. deakjahn commented on Jul 4, 2018

    @deakjahn

    Is this supposed to still work in the current version? With

    var bitmap = SKBitmap.Decode(path, new SKImageInfo(256, 256));
    

    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.

  11. newky2k commented on Oct 26, 2018

    @newky2k
    Contributor

    @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

  12. deakjahn commented on Oct 26, 2018

    @deakjahn

    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.

  13. newky2k commented on Nov 14, 2018

    @newky2k
    Contributor

    Seems to be resolved now

  14. ghost locked as resolved and limited conversation to collaborators on Aug 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions