Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Encoding an Image from a Source SKImage with ColorType Gray8 returns null #2787

Open
1 task done
RiversJohn opened this issue Mar 6, 2024 · 0 comments
Open
1 task done
Labels

Comments

@RiversJohn
Copy link

Description

Dropdown doesn't have our version so here goes:
Occurs on: SkiaSharp 2.88.6
Last good: Unknown

The title pretty much describes the issue, could also be related to the other ticket regarding SKColorType.Gray8 considering sampling the image implies it needs to decode the image aswell.

Workaround for anyone who needs it, don't access the source image but call it on SKImage.Create(sourceImage.Info.WithColorType(someOtherColorType) and use that as your sampling source.

Code

Example method, obviously neither GetTrimmingRect() (get trimmable area) or ToSKStream() (wrapper for SKStream with disposeunderlying false) are part of Skia but these methods have been tested with ungodly amounts of throughput already.

    /// <inheritdoc/>
    public Stream TrimImageBorders(Stream imageStream)
    {
        using var headers = SKCodec.Create(imageStream.ToSKStream());

        using var sourceImage = SKImage.FromEncodedData(imageStream.ToSKStream());

        using var sourceBitmap = SKBitmap.FromImage(sourceImage);

        var trimmingRect = GetTrimmingRect(sourceBitmap);

        // In the unlikely event of a bug, ensure the rectangle can never be greater than the sourceRect
        trimmingRect.Intersect(sourceImage.Info.Rect);

        // If no trimming would occur, return the original stream seeked to start
        if (trimmingRect == sourceImage.Info.Rect)
        {
            imageStream.Seek(0, SeekOrigin.Begin);

            return imageStream;
        }

        using var trimmedImage = sourceImage.Subset(trimmingRect);

        using var trimmedData = trimmedImage.Encode(headers.EncodedFormat, 100); // Results in null when source has SKColorType.Gray8

        var memStream = new MemoryStream();

        trimmedData.AsStream().CopyTo(memStream);

        imageStream.Seek(0, SeekOrigin.Begin);

        memStream.Seek(0, SeekOrigin.Begin);

        return memStream;
    }

Expected Behavior

Image is Encoded correctly correctly

Skia obviously has the ability to decode Gray8 as sampling the image with another SKColorType that maps the correct bitstep works just fine so i'd expect this is some internal logic glitchy or the source image isn't what it claims to be and isn't self-evidently obvious to me from reviewing the input source data

Actual Behavior

Unable to Encode or otherwise sample an image with Gray8 as it's SKColorType

Version of SkiaSharp

Other (Please indicate in the description)

Last Known Good Version of SkiaSharp

Other (Please indicate in the description)

IDE / Editor

Visual Studio (Windows)

Platform / Operating System

Linux, Windows

Platform / Operating System Version

Ubuntu 22.04 running in Docker environment with native assets:

<PackageReference Include="SkiaSharp.NativeAssets.Linux.NoDependencies" Version="2.88.6" />

Devices

Any running the above docker image with this code

Relevant Screenshots

skiagray8bug

Relevant Log Output

Relevant logs have far too much sensitive information to effectively sanitize sorry.

The StackTrace consists essentially just of the code that has the comment for this issue being null

Code of Conduct

  • I agree to follow this project's Code of Conduct
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Ready For Work
Development

No branches or pull requests

1 participant