public void SameColorSpaceCreatedDifferentWaysAreTheSameObject() { var colorspace1 = SKColorSpace.CreateSrgbLinear(); Assert.Equal("SkiaSharp.SKColorSpace+SKColorSpaceStatic", colorspace1.GetType().FullName); Assert.Equal(2, colorspace1.GetReferenceCount()); var colorspace2 = SKColorSpace.CreateRgb(SKNamedGamma.Linear, SKColorSpaceGamut.Srgb); Assert.Equal("SkiaSharp.SKColorSpace+SKColorSpaceStatic", colorspace2.GetType().FullName); Assert.Equal(2, colorspace2.GetReferenceCount()); Assert.Same(colorspace1, colorspace2); var colorspace3 = SKColorSpace.CreateRgb( new SKColorSpaceTransferFn { A = 0.6f, B = 0.5f, C = 0.4f, D = 0.3f, E = 0.2f, F = 0.1f }, SKMatrix44.CreateIdentity()); Assert.NotSame(colorspace1, colorspace3); colorspace3.Dispose(); Assert.True(colorspace3.IsDisposed); Assert.Equal(2, colorspace1.GetReferenceCount()); colorspace2.Dispose(); Assert.False(colorspace2.IsDisposed); Assert.Equal(2, colorspace1.GetReferenceCount()); colorspace1.Dispose(); Assert.False(colorspace1.IsDisposed); Assert.Equal(2, colorspace1.GetReferenceCount()); }
public MainViewModel() { var color = SKImageInfo.PlatformColorType; var colorString = color == SKColorType.Bgra8888 ? "BGRA" : "RGBA"; ColorCombinations = new ColorCombination[] { new ColorCombination(colorString, color, null), new ColorCombination($"{colorString} (sRGB)", color, SKColorSpace.CreateSrgb()), new ColorCombination("F16 (sRGB Linear)", SKColorType.RgbaF16, SKColorSpace.CreateSrgbLinear()), }; CompilationMessages = new ObservableRangeCollection <CompilationMessage>(); var skiaAss = typeof(SKSurface).Assembly; if (skiaAss.GetCustomAttribute(typeof(AssemblyInformationalVersionAttribute)) is AssemblyInformationalVersionAttribute informational) { SkiaSharpVersion = informational.InformationalVersion; } else if (skiaAss.GetCustomAttribute(typeof(AssemblyFileVersionAttribute)) is AssemblyFileVersionAttribute fileVersion) { SkiaSharpVersion = fileVersion.Version; } else if (skiaAss.GetCustomAttribute(typeof(AssemblyVersionAttribute)) is AssemblyVersionAttribute version) { SkiaSharpVersion = version.Version; } else { SkiaSharpVersion = "<unknown>"; } }
public void LinearSrgbColorSpaceIsCorrect() { var colorspace = SKColorSpace.CreateSrgbLinear(); Assert.False(colorspace.IsSrgb); Assert.False(colorspace.GammaIsCloseToSrgb); Assert.True(colorspace.GammaIsLinear); Assert.Equal(SKColorSpaceTransferFn.Linear, colorspace.GetNumericalTransferFunction()); Assert.Equal(SKColorSpaceXyz.Srgb, colorspace.ToColorSpaceXyz()); }
public void MakeSureColorsWithColorSpacesWork() { var color = new SKColorF(0.3f, 0, 0, 0.6f); var paint = new SKPaint(); paint.SetColor(color, SKColorSpace.CreateSrgbLinear()); Assert.NotEqual(color, paint.ColorF); }
public MainViewModel() { var color = SKImageInfo.PlatformColorType; var colorString = color == SKColorType.Bgra8888 ? "BGRA" : "RGBA"; ColorCombinations = new ColorCombination[] { new ColorCombination(colorString, color, null), new ColorCombination($"{colorString} (sRGB)", color, SKColorSpace.CreateSrgb()), new ColorCombination("F16 (sRGB Linear)", SKColorType.RgbaF16, SKColorSpace.CreateSrgbLinear()), }; CompilationMessages = new ObservableRangeCollection <CompilationMessage>(); }
public SKBitmap ImageBufferToSKBitmap(IImageBuffer imageBuffer) { if (imageBuffer == null) { return(null); } imageBuffer = imageBuffer.ToU8(); SKBitmap bitmap = null; SKColorSpace colorSpace = null; if (imageBuffer.Format.ColorSpace == ColorSpace.Srgb) { colorSpace = SKColorSpace.CreateSrgb(); } else if (imageBuffer.Format.ColorSpace == ColorSpace.LinearRgb) { colorSpace = SKColorSpace.CreateSrgbLinear(); } try { // for RGBA-images if (imageBuffer.Format.PixelType == PixelType.U8C3) { // convert to 32bpp var src = imageBuffer is I <byte>?((I <byte>)imageBuffer).Data.Buffer : imageBuffer.ToByteArray(); var dst = new byte[imageBuffer.Height * imageBuffer.Width * 4]; int i = 0, j = 0; while (i < dst.Length) { dst[i + 0] = src[j + 0]; dst[i + 1] = src[j + 1]; dst[i + 2] = src[j + 2]; dst[i + 3] = 0xff; i += 4; j += 3; } SKColorType colorType = SKColorType.Rgba8888; switch (imageBuffer.Format.PixelChannels) { case PixelChannels.Rgb: colorType = SKColorType.Rgba8888; break; case PixelChannels.Bgra: colorType = SKColorType.Bgra8888; break; } using (var pinnedImage = new Utilities.PinnedGCHandle(dst)) { var sourceInfo = new SKImageInfo(imageBuffer.Width, imageBuffer.Height, colorType, SKAlphaType.Opaque); if (colorSpace != null) { sourceInfo.ColorSpace = colorSpace; } bitmap = new SKBitmap(); Debug.Assert(sourceInfo.RowBytes == imageBuffer.Width * 4); if (!bitmap.InstallPixels(sourceInfo, pinnedImage.Pointer)) { throw new Exception("InstallPixels poperation of Skia bitmap failed."); } } } // for RGBA-images else if (imageBuffer.Format.PixelType == PixelType.U8C4) { // try direct copy using (var pinnedImage = imageBuffer.Pin()) { SKAlphaType alphaType = SKAlphaType.Unknown; SKColorType colorType = SKColorType.Rgba8888; switch (imageBuffer.Format.PixelChannels) { case PixelChannels.Rgbx: alphaType = SKAlphaType.Opaque; colorType = SKColorType.Rgba8888; break; case PixelChannels.Rgba: alphaType = SKAlphaType.Unpremul; colorType = SKColorType.Rgba8888; break; case PixelChannels.Bgrx: alphaType = SKAlphaType.Opaque; colorType = SKColorType.Bgra8888; break; case PixelChannels.Bgra: alphaType = SKAlphaType.Unpremul; colorType = SKColorType.Bgra8888; break; } var sourceInfo = new SKImageInfo(imageBuffer.Width, imageBuffer.Height, colorType, alphaType); Debug.Assert(sourceInfo.RowBytes == imageBuffer.Width * 4); if (colorSpace != null) { sourceInfo.ColorSpace = colorSpace; } bitmap = new SKBitmap(); if (!bitmap.InstallPixels(sourceInfo, pinnedImage.Pointer)) { throw new Exception("InstallPixels poperation of Skia bitmap failed."); } } } // for gray-scale else if (imageBuffer.Format.PixelType == PixelType.U8C1) { // try direct copy using (var pinnedImage = imageBuffer.Pin()) { var sourceInfo = new SKImageInfo(imageBuffer.Width, imageBuffer.Height, SKColorType.Gray8); Debug.Assert(sourceInfo.RowBytes == imageBuffer.Width); bitmap = new SKBitmap(); if (!bitmap.InstallPixels(sourceInfo, pinnedImage.Pointer)) { throw new Exception("InstallPixels poperation of Skia bitmap failed."); } } } else { throw new Exception("PixelFormat not yet implemented for preview image."); } SKBitmap result = bitmap; bitmap = null; colorSpace = null; return(result); } finally { if (bitmap != null) { bitmap.Dispose(); if (colorSpace != null) { colorSpace.Dispose(); } } } }
public ISkiaGpuRenderSession BeginRenderingSession() { if (_currentSize != _data.Size || _surface == null) { _data.Texture?.Dispose(); _surface?.Dispose(); _surface = null; _data.Texture = null; var game = AvaloniaLocator.Current.GetService <IGame>(); var gameSettings = game.Services.GetService <IGameSettingsService>().Settings; var device = game.GraphicsDevice; StrideDispatcher.StrideThread.InvokeAsync(() => { _data.Texture = Texture.New2D(device, _data.Size.Width, _data.Size.Height, PixelFormat.B8G8R8A8_UNorm); }).Wait(1000); if (gameSettings.Configurations.Get <RenderingSettings>().ColorSpace == ColorSpace.Linear) { _surface = SKSurface.Create(new SKImageInfo(_data.Size.Width, _data.Size.Height, SKColorType.Bgra8888, SKAlphaType.Premul, SKColorSpace.CreateSrgbLinear())); } else { _surface = SKSurface.Create(new SKImageInfo(_data.Size.Width, _data.Size.Height, SKColorType.Bgra8888, SKAlphaType.Premul)); } _currentSize = _data.Size; } return(new Session(_data, _surface, _options.WaitCopyTexture)); }