mirror of https://github.com/SixLabors/ImageSharp
committed by
GitHub
13 changed files with 504 additions and 250 deletions
@ -1,115 +0,0 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System; |
|||
using System.Collections.Generic; |
|||
|
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
|
|||
namespace SixLabors.ImageSharp |
|||
{ |
|||
/// <summary>
|
|||
/// Encapsulates a collection of <see cref="ImageFrame{T}"/> instances that make up an <see cref="Image{T}"/>.
|
|||
/// </summary>
|
|||
/// <typeparam name="TPixel">The type of the pixel.</typeparam>
|
|||
public interface IImageFrameCollection<TPixel> : IEnumerable<ImageFrame<TPixel>> |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
/// <summary>
|
|||
/// Gets the number of frames.
|
|||
/// </summary>
|
|||
int Count { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the root frame.
|
|||
/// </summary>
|
|||
ImageFrame<TPixel> RootFrame { get; } |
|||
|
|||
/// <summary>
|
|||
/// Gets the <see cref="ImageFrame{TPixel}"/> at the specified index.
|
|||
/// </summary>
|
|||
/// <value>
|
|||
/// The <see cref="ImageFrame{TPixel}"/>.
|
|||
/// </value>
|
|||
/// <param name="index">The index.</param>
|
|||
/// <returns>The <see cref="ImageFrame{TPixel}"/> at the specified index.</returns>
|
|||
ImageFrame<TPixel> this[int index] { get; } |
|||
|
|||
/// <summary>
|
|||
/// Creates an <see cref="Image{T}"/> with only the frame at the specified index
|
|||
/// with the same metadata as the original image.
|
|||
/// </summary>
|
|||
/// <param name="index">The zero-based index of the frame to clone.</param>
|
|||
/// <returns>The new <see cref="Image{TPixel}"/> with the specified frame.</returns>
|
|||
Image<TPixel> CloneFrame(int index); |
|||
|
|||
/// <summary>
|
|||
/// Removes the frame at the specified index and creates a new image with only the removed frame
|
|||
/// with the same metadata as the original image.
|
|||
/// </summary>
|
|||
/// <param name="index">The zero-based index of the frame to export.</param>
|
|||
/// <exception cref="InvalidOperationException">Cannot remove last frame.</exception>
|
|||
/// <returns>The new <see cref="Image{TPixel}"/> with the specified frame.</returns>
|
|||
Image<TPixel> ExportFrame(int index); |
|||
|
|||
/// <summary>
|
|||
/// Removes the frame at the specified index and frees all freeable resources associated with it.
|
|||
/// </summary>
|
|||
/// <param name="index">The zero-based index of the frame to remove.</param>
|
|||
/// <exception cref="InvalidOperationException">Cannot remove last frame.</exception>
|
|||
void RemoveFrame(int index); |
|||
|
|||
/// <summary>
|
|||
/// Creates a new <seealso cref="ImageFrame{TPixel}"/> and appends it to the end of the collection.
|
|||
/// </summary>
|
|||
/// <returns>The new <see cref="ImageFrame{TPixel}"/>.</returns>
|
|||
ImageFrame<TPixel> CreateFrame(); |
|||
|
|||
/// <summary>
|
|||
/// Clones the <paramref name="source"/> frame and appends the clone to the end of the collection.
|
|||
/// </summary>
|
|||
/// <param name="source">The raw pixel data to generate the <seealso cref="ImageFrame{TPixel}"/> from.</param>
|
|||
/// <returns>The cloned <see cref="ImageFrame{TPixel}"/>.</returns>
|
|||
ImageFrame<TPixel> AddFrame(ImageFrame<TPixel> source); |
|||
|
|||
/// <summary>
|
|||
/// Creates a new frame from the pixel data with the same dimensions as the other frames and inserts the
|
|||
/// new frame at the end of the collection.
|
|||
/// </summary>
|
|||
/// <param name="source">The raw pixel data to generate the <seealso cref="ImageFrame{TPixel}"/> from.</param>
|
|||
/// <returns>The new <see cref="ImageFrame{TPixel}"/>.</returns>
|
|||
ImageFrame<TPixel> AddFrame(TPixel[] source); |
|||
|
|||
/// <summary>
|
|||
/// Clones and inserts the <paramref name="source"/> into the <seealso cref="IImageFrameCollection{TPixel}"/> at the specified <paramref name="index"/>.
|
|||
/// </summary>
|
|||
/// <param name="index">The zero-based index to insert the frame at.</param>
|
|||
/// <param name="source">The <seealso cref="ImageFrame{TPixel}"/> to clone and insert into the <seealso cref="IImageFrameCollection{TPixel}"/>.</param>
|
|||
/// <exception cref="ArgumentException">Frame must have the same dimensions as the image.</exception>
|
|||
/// <returns>The cloned <see cref="ImageFrame{TPixel}"/>.</returns>
|
|||
ImageFrame<TPixel> InsertFrame(int index, ImageFrame<TPixel> source); |
|||
|
|||
/// <summary>
|
|||
/// Moves an <seealso cref="ImageFrame{TPixel}"/> from <paramref name="sourceIndex"/> to <paramref name="destinationIndex"/>.
|
|||
/// </summary>
|
|||
/// <param name="sourceIndex">The zero-based index of the frame to move.</param>
|
|||
/// <param name="destinationIndex">The index to move the frame to.</param>
|
|||
void MoveFrame(int sourceIndex, int destinationIndex); |
|||
|
|||
/// <summary>
|
|||
/// Determines the index of a specific <paramref name="frame"/> in the <seealso cref="IImageFrameCollection{TPixel}"/>.
|
|||
/// </summary>
|
|||
/// <param name="frame">The <seealso cref="ImageFrame{TPixel}"/> to locate in the <seealso cref="IImageFrameCollection{TPixel}"/>.</param>
|
|||
/// <returns>The index of item if found in the list; otherwise, -1.</returns>
|
|||
int IndexOf(ImageFrame<TPixel> frame); |
|||
|
|||
/// <summary>
|
|||
/// Determines whether the <seealso cref="IImageFrameCollection{TPixel}"/> contains the <paramref name="frame"/>.
|
|||
/// </summary>
|
|||
/// <param name="frame">The frame.</param>
|
|||
/// <returns>
|
|||
/// <c>true</c> if the <seealso cref="IImageFrameCollection{TPixel}"/> contains the specified frame; otherwise, <c>false</c>.
|
|||
/// </returns>
|
|||
bool Contains(ImageFrame<TPixel> frame); |
|||
} |
|||
} |
|||
@ -1,79 +1,164 @@ |
|||
// Copyright (c) Six Labors and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
|
|||
using System.Numerics; |
|||
using SixLabors.ImageSharp.PixelFormats; |
|||
using SixLabors.ImageSharp.Processing; |
|||
using SixLabors.ImageSharp.Processing.Drawing; |
|||
using SixLabors.ImageSharp.Processing.Overlays; |
|||
using SixLabors.ImageSharp.Primitives; |
|||
using SixLabors.ImageSharp.Processing.Drawing.Brushes; |
|||
using SixLabors.Shapes; |
|||
using Xunit; |
|||
// ReSharper disable InconsistentNaming
|
|||
|
|||
namespace SixLabors.ImageSharp.Tests.Drawing |
|||
{ |
|||
public class FillSolidBrushTests : FileTestBase |
|||
|
|||
|
|||
[GroupOutput("Drawing")] |
|||
public class FillSolidBrushTests |
|||
{ |
|||
[Fact] |
|||
public void ImageShouldBeFloodFilledWithColorOnDefaultBackground() |
|||
[Theory] |
|||
[WithBlankImages(1, 1, PixelTypes.Rgba32)] |
|||
[WithBlankImages(7, 4, PixelTypes.Rgba32)] |
|||
[WithBlankImages(16, 7, PixelTypes.Rgba32)] |
|||
[WithBlankImages(33, 32, PixelTypes.Rgba32)] |
|||
[WithBlankImages(400, 500, PixelTypes.Rgba32)] |
|||
public void DoesNotDependOnSize<TPixel>(TestImageProvider<TPixel> provider) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
string path = TestEnvironment.CreateOutputDirectory("Fill", "SolidBrush"); |
|||
using (var image = new Image<Rgba32>(500, 500)) |
|||
using (Image<TPixel> image = provider.GetImage()) |
|||
{ |
|||
image.Mutate(x => x.Fill(Rgba32.HotPink)); |
|||
image.Save($"{path}/DefaultBack.png"); |
|||
|
|||
using (PixelAccessor<Rgba32> sourcePixels = image.Lock()) |
|||
{ |
|||
Assert.Equal(Rgba32.HotPink, sourcePixels[9, 9]); |
|||
TPixel color = NamedColors<TPixel>.HotPink; |
|||
image.Mutate(c => c.Fill(color)); |
|||
|
|||
Assert.Equal(Rgba32.HotPink, sourcePixels[199, 149]); |
|||
} |
|||
image.DebugSave(provider, appendPixelTypeToFileName: false); |
|||
image.ComparePixelBufferTo(color); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public void ImageShouldBeFloodFilledWithColor() |
|||
[Theory] |
|||
[WithBlankImages(16, 16, PixelTypes.Rgba32 | PixelTypes.Argb32 | PixelTypes.RgbaVector)] |
|||
public void DoesNotDependOnSinglePixelType<TPixel>(TestImageProvider<TPixel> provider) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
string path = TestEnvironment.CreateOutputDirectory("Fill", "SolidBrush"); |
|||
using (var image = new Image<Rgba32>(500, 500)) |
|||
using (Image<TPixel> image = provider.GetImage()) |
|||
{ |
|||
image.Mutate(x => x |
|||
.BackgroundColor(Rgba32.Blue) |
|||
.Fill(Rgba32.HotPink)); |
|||
image.Save($"{path}/Simple.png"); |
|||
TPixel color = NamedColors<TPixel>.HotPink; |
|||
image.Mutate(c => c.Fill(color)); |
|||
|
|||
using (PixelAccessor<Rgba32> sourcePixels = image.Lock()) |
|||
{ |
|||
Assert.Equal(Rgba32.HotPink, sourcePixels[9, 9]); |
|||
|
|||
Assert.Equal(Rgba32.HotPink, sourcePixels[199, 149]); |
|||
} |
|||
image.DebugSave(provider, appendSourceFileOrDescription: false); |
|||
image.ComparePixelBufferTo(color); |
|||
} |
|||
} |
|||
|
|||
[Fact] |
|||
public void ImageShouldBeFloodFilledWithColorOpacity() |
|||
[Theory] |
|||
[WithSolidFilledImages(16, 16, "Red", PixelTypes.Rgba32, "Blue")] |
|||
[WithSolidFilledImages(16, 16, "Yellow", PixelTypes.Rgba32, "Khaki")] |
|||
public void WhenColorIsOpaque_OverridePreviousColor<TPixel>(TestImageProvider<TPixel> provider, string newColorName) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
string path = TestEnvironment.CreateOutputDirectory("Fill", "SolidBrush"); |
|||
using (var image = new Image<Rgba32>(500, 500)) |
|||
using (Image<TPixel> image = provider.GetImage()) |
|||
{ |
|||
var color = new Rgba32(Rgba32.HotPink.R, Rgba32.HotPink.G, Rgba32.HotPink.B, 150); |
|||
TPixel color = TestUtils.GetPixelOfNamedColor<TPixel>(newColorName); |
|||
image.Mutate(c => c.Fill(color)); |
|||
|
|||
image.DebugSave(provider, newColorName, appendPixelTypeToFileName: false, appendSourceFileOrDescription: false); |
|||
image.ComparePixelBufferTo(color); |
|||
} |
|||
} |
|||
|
|||
public static readonly TheoryData<bool, string, float, PixelBlenderMode, float> BlendData = |
|||
new TheoryData<bool, string, float, PixelBlenderMode, float>() |
|||
{ |
|||
{ false, "Blue", 0.5f, PixelBlenderMode.Normal, 1.0f }, |
|||
{ false, "Blue", 1.0f, PixelBlenderMode.Normal, 0.5f }, |
|||
{ false, "Green", 0.5f, PixelBlenderMode.Normal, 0.3f }, |
|||
{ false, "HotPink", 0.8f, PixelBlenderMode.Normal, 0.8f }, |
|||
|
|||
{ false, "Blue", 0.5f, PixelBlenderMode.Multiply, 1.0f }, |
|||
{ false, "Blue", 1.0f, PixelBlenderMode.Multiply, 0.5f }, |
|||
{ false, "Green", 0.5f, PixelBlenderMode.Multiply, 0.3f }, |
|||
{ false, "HotPink", 0.8f, PixelBlenderMode.Multiply, 0.8f }, |
|||
|
|||
{ false, "Blue", 0.5f, PixelBlenderMode.Add, 1.0f }, |
|||
{ false, "Blue", 1.0f, PixelBlenderMode.Add, 0.5f }, |
|||
{ false, "Green", 0.5f, PixelBlenderMode.Add, 0.3f }, |
|||
{ false, "HotPink", 0.8f, PixelBlenderMode.Add, 0.8f }, |
|||
|
|||
image.Mutate(x => x |
|||
.BackgroundColor(Rgba32.Blue) |
|||
.Fill(color)); |
|||
image.Save($"{path}/Opacity.png"); |
|||
{ true, "Blue", 0.5f, PixelBlenderMode.Normal, 1.0f }, |
|||
{ true, "Blue", 1.0f, PixelBlenderMode.Normal, 0.5f }, |
|||
{ true, "Green", 0.5f, PixelBlenderMode.Normal, 0.3f }, |
|||
{ true, "HotPink", 0.8f, PixelBlenderMode.Normal, 0.8f }, |
|||
|
|||
//shift background color towards forground color by the opacity amount
|
|||
var mergedColor = new Rgba32(Vector4.Lerp(Rgba32.Blue.ToVector4(), Rgba32.HotPink.ToVector4(), 150f / 255f)); |
|||
{ true, "Blue", 0.5f, PixelBlenderMode.Multiply, 1.0f }, |
|||
{ true, "Blue", 1.0f, PixelBlenderMode.Multiply, 0.5f }, |
|||
{ true, "Green", 0.5f, PixelBlenderMode.Multiply, 0.3f }, |
|||
{ true, "HotPink", 0.8f, PixelBlenderMode.Multiply, 0.8f }, |
|||
|
|||
{ true, "Blue", 0.5f, PixelBlenderMode.Add, 1.0f }, |
|||
{ true, "Blue", 1.0f, PixelBlenderMode.Add, 0.5f }, |
|||
{ true, "Green", 0.5f, PixelBlenderMode.Add, 0.3f }, |
|||
{ true, "HotPink", 0.8f, PixelBlenderMode.Add, 0.8f }, |
|||
}; |
|||
|
|||
using (PixelAccessor<Rgba32> sourcePixels = image.Lock()) |
|||
[Theory] |
|||
[WithSolidFilledImages(nameof(BlendData), 16, 16, "Red", PixelTypes.Rgba32)] |
|||
public void BlendFillColorOverBackround<TPixel>( |
|||
TestImageProvider<TPixel> provider, |
|||
bool triggerFillRegion, |
|||
string newColorName, |
|||
float alpha, |
|||
PixelBlenderMode blenderMode, |
|||
float blendPercentage) |
|||
where TPixel : struct, IPixel<TPixel> |
|||
{ |
|||
var vec = TestUtils.GetPixelOfNamedColor<RgbaVector>(newColorName).ToVector4(); |
|||
vec.W = alpha; |
|||
|
|||
TPixel fillColor = default; |
|||
fillColor.PackFromVector4(vec); |
|||
|
|||
using (Image<TPixel> image = provider.GetImage()) |
|||
{ |
|||
TPixel bgColor = image[0, 0]; |
|||
|
|||
var options = new GraphicsOptions(false) |
|||
{ |
|||
BlenderMode = blenderMode, |
|||
BlendPercentage = blendPercentage |
|||
}; |
|||
|
|||
if (triggerFillRegion) |
|||
{ |
|||
var region = new ShapeRegion(new RectangularPolygon(0, 0, 16, 16)); |
|||
|
|||
image.Mutate(c => c.Fill(options, new SolidBrush<TPixel>(fillColor), region)); |
|||
} |
|||
else |
|||
{ |
|||
Assert.Equal(mergedColor, sourcePixels[9, 9]); |
|||
Assert.Equal(mergedColor, sourcePixels[199, 149]); |
|||
image.Mutate(c => c.Fill(options, new SolidBrush<TPixel>(fillColor))); |
|||
} |
|||
|
|||
var testOutputDetails = new |
|||
{ |
|||
triggerFillRegion = triggerFillRegion, |
|||
newColorName = newColorName, |
|||
alpha = alpha, |
|||
blenderMode = blenderMode, |
|||
blendPercentage = blendPercentage |
|||
}; |
|||
|
|||
image.DebugSave( |
|||
provider, |
|||
testOutputDetails, |
|||
appendPixelTypeToFileName: false, |
|||
appendSourceFileOrDescription: false); |
|||
|
|||
PixelBlender<TPixel> blender = PixelOperations<TPixel>.Instance.GetPixelBlender(blenderMode); |
|||
TPixel expectedPixel = blender.Blend(bgColor, fillColor, blendPercentage); |
|||
|
|||
image.ComparePixelBufferTo(expectedPixel); |
|||
} |
|||
} |
|||
|
|||
} |
|||
} |
|||
|
|||
Loading…
Reference in new issue