// Copyright (c) Six Labors. // Licensed under the Apache License, Version 2.0. using System; using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.PixelFormats; using Xunit; namespace SixLabors.ImageSharp.Tests { public class ImageCloneTests { [Fact] public void CloneAs_WhenDisposed_Throws() { var image = new Image(5, 5); image.Dispose(); Assert.Throws(() => image.CloneAs()); } [Fact] public void Clone_WhenDisposed_Throws() { var image = new Image(5, 5); image.Dispose(); Assert.Throws(() => image.Clone()); } [Theory] [WithTestPatternImages(9, 9, PixelTypes.Rgba32)] public void CloneAs_ToBgra32(TestImageProvider provider) { using (Image image = provider.GetImage()) using (Image clone = image.CloneAs()) { for (int y = 0; y < image.Height; y++) { Span row = image.GetPixelRowSpan(y); Span rowClone = clone.GetPixelRowSpan(y); for (int x = 0; x < image.Width; x++) { Rgba32 expected = row[x]; Bgra32 actual = rowClone[x]; Assert.Equal(expected.R, actual.R); Assert.Equal(expected.G, actual.G); Assert.Equal(expected.B, actual.B); Assert.Equal(expected.A, actual.A); } } } } [Theory] [WithTestPatternImages(9, 9, PixelTypes.Rgba32)] public void CloneAs_ToBgr24(TestImageProvider provider) { using (Image image = provider.GetImage()) using (Image clone = image.CloneAs()) { for (int y = 0; y < image.Height; y++) { Span row = image.GetPixelRowSpan(y); Span rowClone = clone.GetPixelRowSpan(y); for (int x = 0; x < image.Width; x++) { Rgba32 expected = row[x]; Bgr24 actual = rowClone[x]; Assert.Equal(expected.R, actual.R); Assert.Equal(expected.G, actual.G); Assert.Equal(expected.B, actual.B); } } } } [Theory] [WithTestPatternImages(9, 9, PixelTypes.Rgba32)] public void CloneAs_ToArgb32(TestImageProvider provider) { using (Image image = provider.GetImage()) using (Image clone = image.CloneAs()) { for (int y = 0; y < image.Height; y++) { Span row = image.GetPixelRowSpan(y); Span rowClone = clone.GetPixelRowSpan(y); for (int x = 0; x < image.Width; x++) { Rgba32 expected = row[x]; Argb32 actual = rowClone[x]; Assert.Equal(expected.R, actual.R); Assert.Equal(expected.G, actual.G); Assert.Equal(expected.B, actual.B); Assert.Equal(expected.A, actual.A); } } } } [Theory] [WithTestPatternImages(9, 9, PixelTypes.Rgba32)] public void CloneAs_ToRgb24(TestImageProvider provider) { using (Image image = provider.GetImage()) using (Image clone = image.CloneAs()) { for (int y = 0; y < image.Height; y++) { Span row = image.GetPixelRowSpan(y); Span rowClone = clone.GetPixelRowSpan(y); for (int x = 0; x < image.Width; x++) { Rgba32 expected = row[x]; Rgb24 actual = rowClone[x]; Assert.Equal(expected.R, actual.R); Assert.Equal(expected.G, actual.G); Assert.Equal(expected.B, actual.B); } } } } } }