Beispiel #1
0
        public bool CopyTo(SKBitmap destination, SKColorType colorType)
        {
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            if (colorType == SKColorType.Unknown)
            {
                return(false);
            }

            using var srcPixmap = PeekPixels();
            if (srcPixmap == null)
            {
                return(false);
            }

            using var temp = new SKBitmap();

            var dstInfo = srcPixmap.Info.WithColorType(colorType);

            if (!temp.TryAllocPixels(dstInfo))
            {
                return(false);
            }

            using var canvas = new SKCanvas(temp);

            using var paint = new SKPaint {
                      Shader    = ToShader(),
                      BlendMode = SKBlendMode.Src
                  };

            canvas.DrawPaint(paint);

            destination.Swap(temp);
            return(true);
        }
Beispiel #2
0
        public bool CopyTo(SKBitmap destination, SKColorType colorType)
        {
            if (destination == null)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            if (!CanCopyTo(colorType))
            {
                return(false);
            }

            var srcPM = PeekPixels();

            if (srcPM == null)
            {
                return(false);
            }

            var dstInfo = srcPM.Info.WithColorType(colorType);

            switch (colorType)
            {
            case SKColorType.Rgb565:
                // CopyTo() is not strict on alpha type. Here we set the src to opaque to allow
                // the call to ReadPixels() to succeed and preserve this lenient behavior.
                if (srcPM.AlphaType != SKAlphaType.Opaque)
                {
                    srcPM = srcPM.WithAlphaType(SKAlphaType.Opaque);
                }
                dstInfo.AlphaType = SKAlphaType.Opaque;
                break;

            case SKColorType.RgbaF16:
                // The caller does not have an opportunity to pass a dst color space.
                // Assume that they want linear sRGB.
                dstInfo.ColorSpace = SKColorSpace.CreateSrgbLinear();
                if (srcPM.ColorSpace == null)
                {
                    // We can't do a sane conversion to F16 without a dst color space.
                    // Guess sRGB in this case.
                    srcPM = srcPM.WithColorSpace(SKColorSpace.CreateSrgb());
                }
                break;
            }

            var tmpDst = new SKBitmap();

            if (!tmpDst.TryAllocPixels(dstInfo))
            {
                return(false);
            }

            var dstPM = tmpDst.PeekPixels();

            if (dstPM == null)
            {
                return(false);
            }

            // We can't do a sane conversion from F16 without a src color space. Guess sRGB in this case.
            if (srcPM.ColorType == SKColorType.RgbaF16 && dstPM.ColorSpace == null)
            {
                dstPM = dstPM.WithColorSpace(SKColorSpace.CreateSrgb());
            }

            // ReadPixels does not yet support color spaces with parametric transfer functions. This
            // works around that restriction when the color spaces are equal.
            if (colorType != SKColorType.RgbaF16 && srcPM.ColorType != SKColorType.RgbaF16 && dstPM.ColorSpace == srcPM.ColorSpace)
            {
                dstPM = dstPM.WithColorSpace(null);
                srcPM = srcPM.WithColorSpace(null);
            }

            if (!srcPM.ReadPixels(dstPM))
            {
                return(false);
            }

            destination.Swap(tmpDst);

            return(true);
        }