SkiaSharp 使用 ICC 配置文件保存图像

Dou*_*g S 2 c# .net-core skiasharp

在SkiaSharp中,当您从现有图像创建新图像时(例如调整大小时),如何使用原始图像的ICC配置文件保存新图像?

Dou*_*g S 5

所以答案是:如果ColorSpace源图像和目标图像之间都设置并维护了ICC 配置文件,Skia 将自动应用 ICC 配置文件。

必须ColorSpace在源对象和目标对象(SKBitmap、SKImage、SKSurface 等)上设置。这样 Skia 就可以知道如何在源和目标之间转换颜色。如果ColorSpace其中任何一个都没有设置,或者其中一个ColorSpace丢失了(在创建新对象时很容易发生这种情况),Skia 将使用默认设置,这可能会扭曲颜色转换。

维护色彩空间的正确方法示例:

using (SKData origData = SKData.Create(imgStream)) // convert the stream into SKData
using (SKImage srcImg = SKImage.FromEncodedData(origData))
    // srcImg now contains the original ColorSpace (e.g. CMYK)
{
    SKImageInfo info = new SKImageInfo(resizeWidth, resizeHeight,
        SKImageInfo.PlatformColorType, SKAlphaType.Premul, SKColorSpace.CreateSrgb());
    // this is the important part. set the destination ColorSpace as
    // `SKColorSpace.CreateSrgb()`. Skia will then be able to automatically convert
    // the original CMYK colorspace, to this new sRGB colorspace.

    using (SKImage newImg = SKImage.Create(info)) // new image. ColorSpace set via `info`
    {
        srcImg.ScalePixels(newImg.PeekPixels(), SKFilterQuality.None);
        // now when doing this resize, Skia knows the original ColorSpace, and the
        // destination ColorSpace, and converts the colors from CMYK to sRGB.
    }
}
Run Code Online (Sandbox Code Playgroud)

维护 ColorSpace 的另一个示例:

using (SKCodec codec = SKCodec.Create(imgStream)) // create a codec with the imgStream
{
    SKImageInfo info = new SKImageInfo(codec.Info.Width, codec.Info.Height,
        SKImageInfo.PlatformColorType, SKAlphaType.Premul, SKColorSpace.CreateSrgb());
    // set the destination ColorSpace via SKColorSpace.CreateSrgb()

    SKBitmap srcImg = SKBitmap.Decode(codec, info);
    // Skia creates a new bitmap, converting the codec ColorSpace (e.g. CMYK) to the
    // destination ColorSpace (sRGB)
}
Run Code Online (Sandbox Code Playgroud)

有关 Skia 色彩校正的其他非常有用的信息:https://skia.org/user/sample/color?cl=9919