Problem scaling a grayscale JPEG

Hi. I'm having an issue trying to scale down a grayscale JPEG (please see attached image). Windows platform.

The following code yields a null value. When a similar RGB image is used, it scales without a problem. Do you have any suggestion? Thanks.

        using (var input = File.OpenRead(@"c:\temp\grayscale.jpg"))
        {
            using (var inputStream = new SKManagedStream(input))
            {
                using (var originalBitmap = SKBitmap.Decode(inputStream))
                {
                    int width = 200;
                    int height = 274;

                    var scaledBitmap = originalBitmap.Resize(new SKImageInfo(width, height), SKBitmapResizeMethod.Lanczos3);
                    // NULL :-(
                }
            }
        }

Posts

  • mattleibowmattleibow ZAXamarin Team Xamurai

    Unfortunately, I am unable to reproduce this. It may be something that is done with this forum, but both images are BGRA 8888, and both load and scale fine.

    What is the ColorType and AlphaType of originalBitmap.Info when you decode the image that doesn't scale? Mine is Bgra8888 and Opaque, respectively.

  • zzpluralzzplural GBMember ✭✭

    @mattleibow

    Yes, very odd. It's not exactly the picture I uploaded. Try this one, uploaded to TinyPic, to provoke the problem...

    http://tinypic.com/r/161gac3/9

    originalBitmap.info has a ColorType of Gray8 and an AlphaType of Opaque.

  • mattleibowmattleibow ZAXamarin Team Xamurai
    That might be the problem... gray8. SkiaSharp is not really a image PROCESSING library, but a vector DRAWING library. The various color types are more for being able to load. I would suggest loading the image as the native color type.
  • zzpluralzzplural GBMember ✭✭

    Yes. A pity. My workaround is to turn it into a colour image first before scaling. Thanks.

  • mattleibowmattleibow ZAXamarin Team Xamurai

    This may be helpful:

    int width = 200;
    int height = 274;
    
    // this contains the size and the platform color type
    var desired = new SKImageInfo(width, height);
    
    using (var input = File.OpenRead(@"c:\temp\grayscale.jpg"))
    using (var originalBitmap = SKBitmap.Decode(input, desired)) {
        // resize if needed
        if (originalBitmap.Width != desired.Width || originalBitmap.Height != desired.Height) {
            var scaledBitmap = originalBitmap.Resize(desired, SKBitmapResizeMethod.Lanczos3);
        }
    }
    

    This may be better since we are providing, not only the platform color type, but also the desired size. Some codecs can actually do the sizing for you (or at least get you near enough)

  • FilipeEschFilipeEsch Member
    edited July 2018

    I am having the same problem on resizing. When I call the Resize() method rhe return is NULL. My original image information is:

    AlphaType: Opaque
    ByteCount: 1334000
    Bytes: {byte[1334000]}
    BytesPerPixel: 1
    ColorSpace: null
    ColorTable: null
    ColorType: Gray8
    DrawsNothing: false
    Handle: {2041734555824}
    Height: 1334
    Info: {SkiaSharp.SKImageInfo}
    IsEmpty: false
    IsImmutable: false
    IsNull: false
    IsVolatile: false
    OwnsHandle: true
    Pixels: {SkiaSharp.SKColor[1334000]}
    ReadyToDraw: true
    RowBytes: 1000
    Width: 1000
    

    My code is:

        public override void ResizeImage(ResizeImageArguments args)
        {
            if (args == null)
                throw new ArgumentNullException(nameof(args));
    
            using (var input = new FileStream(args.SourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
            using (var inputStream = new SKManagedStream(input))
            using (var original = SKBitmap.Decode(inputStream))
            {
                var newSize = this.CalculateNewSize(new Size(original.Width, original.Height), args.Size);
    
                using (var resized = original.Resize(
                    new SKImageInfo(newSize.Width, newSize.Height),
                    SKBitmapResizeMethod.Lanczos3))
                {
                    if (resized == null)
                    {
                        if (original.ColorType == SKColorType.Gray8)
                            throw new Exception("Error on image resize: original image is a grayscale image - " + JsonConvert.SerializeObject(args));
    
                        throw new Exception("Error on image resize: " + JsonConvert.SerializeObject(args));
                    }
    
                    using (var image = SKImage.FromBitmap(resized))
                    using (var output = File.OpenWrite(args.DestinationFile))
                    {
                        image
                            .Encode(SKEncodedImageFormat.Jpeg, args.Quality)
                            .SaveTo(output);
                    }
                }
            }
        }
    
Sign In or Register to comment.