From 119c1bf70f315fad1e718d267bd38a96c80af672 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 5 May 2017 13:46:06 +1000 Subject: [PATCH 01/34] Remove non-generic Image --- src/ImageSharp.Drawing/Brushes/Brushes.cs | 156 ----------- .../Brushes/Brushes{TPixel}.cs | 96 +++++-- src/ImageSharp.Drawing/Brushes/ImageBrush.cs | 24 -- .../Brushes/PatternBrush.cs | 35 --- .../Brushes/RecolorBrush.cs | 26 -- src/ImageSharp.Drawing/Brushes/SolidBrush.cs | 24 -- src/ImageSharp.Drawing/DrawImage.cs | 4 +- src/ImageSharp.Drawing/Pens/Pen.cs | 55 ---- src/ImageSharp.Drawing/Pens/Pens.cs | 95 ------- src/ImageSharp/Common/Helpers/ImageMaths.cs | 2 +- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 2 +- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 2 +- .../Formats/Jpeg/JpegDecoderCore.cs | 2 +- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 2 +- src/ImageSharp/Image.Create.cs | 62 ----- src/ImageSharp/Image.FromBytes.cs | 214 --------------- src/ImageSharp/Image.FromFile.cs | 215 --------------- src/ImageSharp/Image.FromStream.cs | 249 ------------------ src/ImageSharp/Image.cs | 69 ----- src/ImageSharp/Image/Image{TPixel}.Create.cs | 49 ++++ .../Image{TPixel}.Decode.cs} | 16 +- .../Image/Image{TPixel}.FromBytes.cs | 92 +++++++ .../Image/Image{TPixel}.FromFile.cs | 114 ++++++++ .../Image/Image{TPixel}.FromStream.cs | 144 ++++++++++ src/ImageSharp/Image/Image{TPixel}.cs | 2 +- .../MetaData/Profiles/Exif/ExifProfile.cs | 3 +- .../Processing/Binarization/Dither.cs | 6 +- .../Processing/ColorMatrix/Sepia.cs | 4 +- src/ImageSharp/Processing/Effects/Alpha.cs | 4 +- .../Processing/Effects/BackgroundColor.cs | 4 +- src/ImageSharp/Processing/Effects/Invert.cs | 4 +- .../{ => Processing}/ImageProcessor.cs | 0 .../Binarization/BinaryThresholdProcessor.cs | 2 +- .../Processing/Transforms/AutoOrient.cs | 2 +- src/ImageSharp/Processing/Transforms/Crop.cs | 2 +- .../Processing/Transforms/EntropyCrop.cs | 2 +- src/ImageSharp/Processing/Transforms/Flip.cs | 2 +- .../Processing/Transforms/Rotate.cs | 6 +- .../Processing/Transforms/RotateFlip.cs | 2 +- src/ImageSharp/Processing/Transforms/Skew.cs | 4 +- .../Drawing/DrawBeziers.cs | 5 +- .../Drawing/DrawLines.cs | 6 +- .../Drawing/DrawPolygon.cs | 5 +- .../Drawing/FillPolygon.cs | 6 +- .../Drawing/FillRectangle.cs | 5 +- .../Drawing/FillWithPattern.cs | 9 +- .../ImageSharp.Benchmarks/Image/CopyPixels.cs | 6 +- .../ImageSharp.Benchmarks/Image/DecodeBmp.cs | 5 +- .../Image/DecodeFilteredPng.cs | 3 +- .../ImageSharp.Benchmarks/Image/DecodeGif.cs | 5 +- .../ImageSharp.Benchmarks/Image/DecodeJpeg.cs | 5 +- .../Image/DecodeJpegMultiple.cs | 7 +- .../ImageSharp.Benchmarks/Image/DecodePng.cs | 5 +- .../ImageSharp.Benchmarks/Image/EncodeBmp.cs | 7 +- .../ImageSharp.Benchmarks/Image/EncodeGif.cs | 7 +- .../Image/EncodeIndexedPng.cs | 4 +- .../ImageSharp.Benchmarks/Image/EncodeJpeg.cs | 7 +- .../ImageSharp.Benchmarks/Image/EncodePng.cs | 6 +- .../Image/GetSetPixel.cs | 3 +- .../Image/MultiImageBenchmarkBase.cs | 14 +- .../PixelBlenders/PorterDuffBulkVsPixel.cs | 7 +- tests/ImageSharp.Benchmarks/Samplers/Crop.cs | 5 +- .../Samplers/DetectEdges.cs | 7 +- tests/ImageSharp.Benchmarks/Samplers/Glow.cs | 6 +- .../ImageSharp.Benchmarks/Samplers/Resize.cs | 10 +- .../ImageSharp.Sandbox46.csproj | 3 + tests/ImageSharp.Tests/ConfigurationTests.cs | 8 +- .../ImageSharp.Tests/Drawing/BeziersTests.cs | 4 +- .../ImageSharp.Tests/Drawing/DrawImageTest.cs | 2 +- .../ImageSharp.Tests/Drawing/DrawPathTests.cs | 4 +- .../Drawing/FillPatternTests.cs | 30 +-- .../Drawing/FillRegionProcessorTests.cs | 4 +- .../Drawing/FillSolidBrushTests.cs | 6 +- .../Drawing/LineComplexPolygonTests.cs | 12 +- tests/ImageSharp.Tests/Drawing/LineTests.cs | 24 +- .../Drawing/Paths/DrawBeziersTests.cs | 4 +- .../Drawing/Paths/DrawLinesTests.cs | 4 +- .../Drawing/Paths/DrawPath.cs | 6 +- .../Drawing/Paths/DrawPolygon.cs | 4 +- .../Drawing/Paths/DrawRectangle.cs | 4 +- .../Drawing/Paths/FillPath.cs | 4 +- .../Drawing/Paths/FillPolygon.cs | 6 +- .../Drawing/Paths/FillRectangle.cs | 6 +- .../ImageSharp.Tests/Drawing/PolygonTests.cs | 6 +- .../Drawing/RecolorImageTest.cs | 8 +- .../Drawing/SolidBezierTests.cs | 4 +- .../Drawing/SolidComplexPolygonTests.cs | 6 +- .../Drawing/SolidPolygonTests.cs | 26 +- .../ImageSharp.Tests/Drawing/Text/DrawText.cs | 34 +-- .../Drawing/Text/OutputText.cs | 2 +- .../Formats/Bmp/BmpEncoderTests.cs | 4 +- .../Formats/GeneralFormatTests.cs | 22 +- .../Formats/Gif/GifDecoderTests.cs | 6 +- .../Formats/Gif/GifEncoderTests.cs | 12 +- .../Formats/Jpg/JpegDecoderTests.cs | 8 +- .../Formats/Jpg/JpegEncoderTests.cs | 8 +- .../Formats/Jpg/JpegProfilingBenchmarks.cs | 2 +- .../Formats/Png/PngDecoderTests.cs | 6 +- .../Formats/Png/PngSmokeTests.cs | 6 +- .../ImageSharp.Tests/Image/ImageLoadTests.cs | 80 +++--- .../ImageSharp.Tests/Image/ImageSaveTests.cs | 8 +- tests/ImageSharp.Tests/Image/ImageTests.cs | 25 +- .../MetaData/ImageMetaDataTests.cs | 4 +- .../Profiles/Exif/ExifProfileTests.cs | 22 +- .../MetaData/Profiles/Exif/ExifValueTests.cs | 5 +- .../Processors/Filters/AlphaTest.cs | 6 +- .../Processors/Filters/AutoOrientTests.cs | 5 +- .../Processors/Filters/BackgroundColorTest.cs | 4 +- .../Processors/Filters/BinaryThresholdTest.cs | 6 +- .../Processors/Filters/BlackWhiteTest.cs | 6 +- .../Processors/Filters/BoxBlurTest.cs | 6 +- .../Processors/Filters/BrightnessTest.cs | 6 +- .../Processors/Filters/ColorBlindnessTest.cs | 6 +- .../Processors/Filters/ContrastTest.cs | 6 +- .../Processors/Filters/CropTest.cs | 4 +- .../Processors/Filters/DetectEdgesTest.cs | 6 +- .../Processors/Filters/DitherTest.cs | 9 +- .../Processors/Filters/EntropyCropTest.cs | 4 +- .../Processors/Filters/FlipTests.cs | 5 +- .../Processors/Filters/GlowTest.cs | 8 +- .../Processors/Filters/HueTest.cs | 6 +- .../Processors/Filters/InvertTest.cs | 6 +- .../Processors/Filters/KodachromeTest.cs | 6 +- .../Processors/Filters/LomographTest.cs | 6 +- .../Processors/Filters/OilPaintTest.cs | 6 +- .../Processors/Filters/PadTest.cs | 4 +- .../Processors/Filters/PolaroidTest.cs | 6 +- .../Filters/ResizeProfilingBenchmarks.cs | 2 +- .../Processors/Filters/ResizeTests.cs | 25 +- .../Processors/Filters/RotateFlipTest.cs | 5 +- .../Processors/Filters/RotateTest.cs | 7 +- .../Processors/Filters/SaturationTest.cs | 6 +- .../Processors/Filters/SepiaTest.cs | 6 +- .../Processors/Filters/SkewTest.cs | 4 +- .../Processors/Filters/VignetteTest.cs | 8 +- tests/ImageSharp.Tests/TestFile.cs | 14 +- .../TestUtilities/Factories/GenericFactory.cs | 2 +- .../TestUtilities/Factories/ImageFactory.cs | 7 +- .../TestUtilities/PixelTypes.cs | 2 +- .../Tests/TestImageProviderTests.cs | 4 +- 140 files changed, 949 insertions(+), 1662 deletions(-) delete mode 100644 src/ImageSharp.Drawing/Brushes/Brushes.cs delete mode 100644 src/ImageSharp.Drawing/Brushes/ImageBrush.cs delete mode 100644 src/ImageSharp.Drawing/Brushes/PatternBrush.cs delete mode 100644 src/ImageSharp.Drawing/Brushes/RecolorBrush.cs delete mode 100644 src/ImageSharp.Drawing/Brushes/SolidBrush.cs delete mode 100644 src/ImageSharp.Drawing/Pens/Pen.cs delete mode 100644 src/ImageSharp.Drawing/Pens/Pens.cs delete mode 100644 src/ImageSharp/Image.Create.cs delete mode 100644 src/ImageSharp/Image.FromBytes.cs delete mode 100644 src/ImageSharp/Image.FromFile.cs delete mode 100644 src/ImageSharp/Image.FromStream.cs delete mode 100644 src/ImageSharp/Image.cs create mode 100644 src/ImageSharp/Image/Image{TPixel}.Create.cs rename src/ImageSharp/{Image.Decode.cs => Image/Image{TPixel}.Decode.cs} (84%) create mode 100644 src/ImageSharp/Image/Image{TPixel}.FromBytes.cs create mode 100644 src/ImageSharp/Image/Image{TPixel}.FromFile.cs create mode 100644 src/ImageSharp/Image/Image{TPixel}.FromStream.cs rename src/ImageSharp/{ => Processing}/ImageProcessor.cs (100%) diff --git a/src/ImageSharp.Drawing/Brushes/Brushes.cs b/src/ImageSharp.Drawing/Brushes/Brushes.cs deleted file mode 100644 index 8998c60f6..000000000 --- a/src/ImageSharp.Drawing/Brushes/Brushes.cs +++ /dev/null @@ -1,156 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp.Drawing.Brushes -{ - using ImageSharp.PixelFormats; - - /// - /// A collection of methods for creating brushes. Brushes use for painting. - /// - public class Brushes - { - /// - /// Create as brush that will paint a solid color - /// - /// The color. - /// A Brush - public static SolidBrush Solid(Rgba32 color) - => new SolidBrush(color); - - /// - /// Create as brush that will paint a Percent10 Hatch Pattern with - /// in the specified foreground color and a transparent background - /// - /// Color of the foreground. - /// A Brush - public static PatternBrush Percent10(Rgba32 foreColor) - => new PatternBrush(Brushes.Percent10(foreColor, Rgba32.Transparent)); - - /// - /// Create as brush that will paint a Percent10 Hatch Pattern with - /// in the specified foreground and background colors - /// - /// Color of the foreground. - /// Color of the background. - /// A Brush - public static PatternBrush Percent10(Rgba32 foreColor, Rgba32 backColor) - => new PatternBrush(Brushes.Percent10(foreColor, backColor)); - - /// - /// Create as brush that will paint a Percent20 Hatch Pattern with - /// in the specified foreground color and a transparent background - /// - /// Color of the foreground. - /// A Brush - public static PatternBrush Percent20(Rgba32 foreColor) - => new PatternBrush(Brushes.Percent20(foreColor, Rgba32.Transparent)); - - /// - /// Create as brush that will paint a Percent20 Hatch Pattern with - /// in the specified foreground and background colors - /// - /// Color of the foreground. - /// Color of the background. - /// A Brush - public static PatternBrush Percent20(Rgba32 foreColor, Rgba32 backColor) - => new PatternBrush(Brushes.Percent20(foreColor, backColor)); - - /// - /// Create as brush that will paint a Horizontal Hatch Pattern with - /// in the specified foreground color and a transparent background - /// - /// Color of the foreground. - /// A Brush - public static PatternBrush Horizontal(Rgba32 foreColor) - => new PatternBrush(Brushes.Horizontal(foreColor, Rgba32.Transparent)); - - /// - /// Create as brush that will paint a Horizontal Hatch Pattern with - /// in the specified foreground and background colors - /// - /// Color of the foreground. - /// Color of the background. - /// A Brush - public static PatternBrush Horizontal(Rgba32 foreColor, Rgba32 backColor) - => new PatternBrush(Brushes.Horizontal(foreColor, backColor)); - - /// - /// Create as brush that will paint a Min Hatch Pattern with - /// in the specified foreground color and a transparent background - /// - /// Color of the foreground. - /// A Brush - public static PatternBrush Min(Rgba32 foreColor) - => new PatternBrush(Brushes.Min(foreColor, Rgba32.Transparent)); - - /// - /// Create as brush that will paint a Min Hatch Pattern with - /// in the specified foreground and background colors - /// - /// Color of the foreground. - /// Color of the background. - /// A Brush - public static PatternBrush Min(Rgba32 foreColor, Rgba32 backColor) - => new PatternBrush(Brushes.Min(foreColor, backColor)); - - /// - /// Create as brush that will paint a Vertical Hatch Pattern with - /// in the specified foreground color and a transparent background - /// - /// Color of the foreground. - /// A Brush - public static PatternBrush Vertical(Rgba32 foreColor) - => new PatternBrush(Brushes.Vertical(foreColor, Rgba32.Transparent)); - - /// - /// Create as brush that will paint a Vertical Hatch Pattern with - /// in the specified foreground and background colors - /// - /// Color of the foreground. - /// Color of the background. - /// A Brush - public static PatternBrush Vertical(Rgba32 foreColor, Rgba32 backColor) - => new PatternBrush(Brushes.Vertical(foreColor, backColor)); - - /// - /// Create as brush that will paint a Forward Diagonal Hatch Pattern with - /// in the specified foreground color and a transparent background - /// - /// Color of the foreground. - /// A Brush - public static PatternBrush ForwardDiagonal(Rgba32 foreColor) - => new PatternBrush(Brushes.ForwardDiagonal(foreColor, Rgba32.Transparent)); - - /// - /// Create as brush that will paint a Forward Diagonal Hatch Pattern with - /// in the specified foreground and background colors - /// - /// Color of the foreground. - /// Color of the background. - /// A Brush - public static PatternBrush ForwardDiagonal(Rgba32 foreColor, Rgba32 backColor) - => new PatternBrush(Brushes.ForwardDiagonal(foreColor, backColor)); - - /// - /// Create as brush that will paint a Backward Diagonal Hatch Pattern with - /// in the specified foreground color and a transparent background - /// - /// Color of the foreground. - /// A Brush - public static PatternBrush BackwardDiagonal(Rgba32 foreColor) - => new PatternBrush(Brushes.BackwardDiagonal(foreColor, Rgba32.Transparent)); - - /// - /// Create as brush that will paint a Backward Diagonal Hatch Pattern with - /// in the specified foreground and background colors - /// - /// Color of the foreground. - /// Color of the background. - /// A Brush - public static PatternBrush BackwardDiagonal(Rgba32 foreColor, Rgba32 backColor) - => new PatternBrush(Brushes.BackwardDiagonal(foreColor, backColor)); - } -} \ No newline at end of file diff --git a/src/ImageSharp.Drawing/Brushes/Brushes{TPixel}.cs b/src/ImageSharp.Drawing/Brushes/Brushes{TPixel}.cs index 4b2f6c026..a9b638e8b 100644 --- a/src/ImageSharp.Drawing/Brushes/Brushes{TPixel}.cs +++ b/src/ImageSharp.Drawing/Brushes/Brushes{TPixel}.cs @@ -11,7 +11,7 @@ namespace ImageSharp.Drawing.Brushes /// A collection of methods for creating generic brushes. /// /// The pixel format. - /// A Brush + /// A New public class Brushes where TPixel : struct, IPixel { @@ -98,71 +98,133 @@ namespace ImageSharp.Drawing.Brushes /// Create as brush that will paint a solid color /// /// The color. - /// A Brush + /// A New public static SolidBrush Solid(TPixel color) => new SolidBrush(color); /// - /// Create as brush that will paint a Percent10 Hatch Pattern within the specified colors + /// Create as brush that will paint a Percent10 Hatch Pattern with the specified colors + /// + /// Color of the foreground. + /// A New + public static PatternBrush Percent10(TPixel foreColor) + => new PatternBrush(foreColor, NamedColors.Transparent, Percent10Pattern); + + /// + /// Create as brush that will paint a Percent10 Hatch Pattern with the specified colors /// /// Color of the foreground. /// Color of the background. - /// A Brush + /// A New public static PatternBrush Percent10(TPixel foreColor, TPixel backColor) => new PatternBrush(foreColor, backColor, Percent10Pattern); /// - /// Create as brush that will paint a Percent20 Hatch Pattern within the specified colors + /// Create as brush that will paint a Percent20 Hatch Pattern with the specified foreground color and a + /// transparent background. + /// + /// Color of the foreground. + /// A New + public static PatternBrush Percent20(TPixel foreColor) + => new PatternBrush(foreColor, NamedColors.Transparent, Percent20Pattern); + + /// + /// Create as brush that will paint a Percent20 Hatch Pattern with the specified colors /// /// Color of the foreground. /// Color of the background. - /// A Brush + /// A New public static PatternBrush Percent20(TPixel foreColor, TPixel backColor) => new PatternBrush(foreColor, backColor, Percent20Pattern); /// - /// Create as brush that will paint a Horizontal Hatch Pattern within the specified colors + /// Create as brush that will paint a Horizontal Hatch Pattern with the specified foreground color and a + /// transparent background. + /// + /// Color of the foreground. + /// A New + public static PatternBrush Horizontal(TPixel foreColor) + => new PatternBrush(foreColor, NamedColors.Transparent, HorizontalPattern); + + /// + /// Create as brush that will paint a Horizontal Hatch Pattern with the specified colors /// /// Color of the foreground. /// Color of the background. - /// A Brush + /// A New public static PatternBrush Horizontal(TPixel foreColor, TPixel backColor) => new PatternBrush(foreColor, backColor, HorizontalPattern); /// - /// Create as brush that will paint a Min Hatch Pattern within the specified colors + /// Create as brush that will paint a Min Hatch Pattern with the specified foreground color and a + /// transparent background. + /// + /// Color of the foreground. + /// A New + public static PatternBrush Min(TPixel foreColor) + => new PatternBrush(foreColor, NamedColors.Transparent, MinPattern); + + /// + /// Create as brush that will paint a Min Hatch Pattern with the specified colors /// /// Color of the foreground. /// Color of the background. - /// A Brush + /// A New public static PatternBrush Min(TPixel foreColor, TPixel backColor) => new PatternBrush(foreColor, backColor, MinPattern); /// - /// Create as brush that will paint a Vertical Hatch Pattern within the specified colors + /// Create as brush that will paint a Vertical Hatch Pattern with the specified foreground color and a + /// transparent background. + /// + /// Color of the foreground. + /// A New + public static PatternBrush Vertical(TPixel foreColor) + => new PatternBrush(foreColor, NamedColors.Transparent, VerticalPattern); + + /// + /// Create as brush that will paint a Vertical Hatch Pattern with the specified colors /// /// Color of the foreground. /// Color of the background. - /// A Brush + /// A New public static PatternBrush Vertical(TPixel foreColor, TPixel backColor) => new PatternBrush(foreColor, backColor, VerticalPattern); /// - /// Create as brush that will paint a Forward Diagonal Hatch Pattern within the specified colors + /// Create as brush that will paint a Forward Diagonal Hatch Pattern with the specified foreground color and a + /// transparent background. + /// + /// Color of the foreground. + /// A New + public static PatternBrush ForwardDiagonal(TPixel foreColor) + => new PatternBrush(foreColor, NamedColors.Transparent, ForwardDiagonalPattern); + + /// + /// Create as brush that will paint a Forward Diagonal Hatch Pattern with the specified colors /// /// Color of the foreground. /// Color of the background. - /// A Brush + /// A New public static PatternBrush ForwardDiagonal(TPixel foreColor, TPixel backColor) => new PatternBrush(foreColor, backColor, ForwardDiagonalPattern); /// - /// Create as brush that will paint a Backward Diagonal Hatch Pattern within the specified colors + /// Create as brush that will paint a Backward Diagonal Hatch Pattern with the specified foreground color and a + /// transparent background. + /// + /// Color of the foreground. + /// A New + public static PatternBrush BackwardDiagonal(TPixel foreColor) + => new PatternBrush(foreColor, NamedColors.Transparent, BackwardDiagonalPattern); + + /// + /// Create as brush that will paint a Backward Diagonal Hatch Pattern with the specified colors /// /// Color of the foreground. /// Color of the background. - /// A Brush + /// A New public static PatternBrush BackwardDiagonal(TPixel foreColor, TPixel backColor) => new PatternBrush(foreColor, backColor, BackwardDiagonalPattern); } -} +} \ No newline at end of file diff --git a/src/ImageSharp.Drawing/Brushes/ImageBrush.cs b/src/ImageSharp.Drawing/Brushes/ImageBrush.cs deleted file mode 100644 index 6a3ff1d9d..000000000 --- a/src/ImageSharp.Drawing/Brushes/ImageBrush.cs +++ /dev/null @@ -1,24 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp.Drawing.Brushes -{ - using ImageSharp.PixelFormats; - - /// - /// Provides an implementation of a solid brush for painting with repeating images. The brush uses for painting. - /// - public class ImageBrush : ImageBrush - { - /// - /// Initializes a new instance of the class. - /// - /// The image to paint. - public ImageBrush(IImageBase image) - : base(image) - { - } - } -} diff --git a/src/ImageSharp.Drawing/Brushes/PatternBrush.cs b/src/ImageSharp.Drawing/Brushes/PatternBrush.cs deleted file mode 100644 index f00862fe7..000000000 --- a/src/ImageSharp.Drawing/Brushes/PatternBrush.cs +++ /dev/null @@ -1,35 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp.Drawing.Brushes -{ - using ImageSharp.PixelFormats; - - /// - /// Provides an implementation of a pattern brush for painting patterns. The brush use for painting. - /// - public class PatternBrush : PatternBrush - { - /// - /// Initializes a new instance of the class. - /// - /// Color of the fore. - /// Color of the back. - /// The pattern. - public PatternBrush(Rgba32 foreColor, Rgba32 backColor, bool[,] pattern) - : base(foreColor, backColor, pattern) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The brush. - internal PatternBrush(PatternBrush brush) - : base(brush) - { - } - } -} \ No newline at end of file diff --git a/src/ImageSharp.Drawing/Brushes/RecolorBrush.cs b/src/ImageSharp.Drawing/Brushes/RecolorBrush.cs deleted file mode 100644 index bfe5c01e6..000000000 --- a/src/ImageSharp.Drawing/Brushes/RecolorBrush.cs +++ /dev/null @@ -1,26 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp.Drawing.Brushes -{ - using ImageSharp.PixelFormats; - - /// - /// Provides an implementation of a recolor brush for painting color changes. - /// - public class RecolorBrush : RecolorBrush - { - /// - /// Initializes a new instance of the class. - /// - /// Color of the source. - /// Color of the target. - /// The threshold. - public RecolorBrush(Rgba32 sourceColor, Rgba32 targeTPixel, float threshold) - : base(sourceColor, targeTPixel, threshold) - { - } - } -} diff --git a/src/ImageSharp.Drawing/Brushes/SolidBrush.cs b/src/ImageSharp.Drawing/Brushes/SolidBrush.cs deleted file mode 100644 index 8a3ad50e7..000000000 --- a/src/ImageSharp.Drawing/Brushes/SolidBrush.cs +++ /dev/null @@ -1,24 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp.Drawing.Brushes -{ - using ImageSharp.PixelFormats; - - /// - /// Provides an implementation of a solid brush for painting solid color areas. The brush uses for painting. - /// - public class SolidBrush : SolidBrush - { - /// - /// Initializes a new instance of the class. - /// - /// The color. - public SolidBrush(Rgba32 color) - : base(color) - { - } - } -} diff --git a/src/ImageSharp.Drawing/DrawImage.cs b/src/ImageSharp.Drawing/DrawImage.cs index acc821292..975bce9ed 100644 --- a/src/ImageSharp.Drawing/DrawImage.cs +++ b/src/ImageSharp.Drawing/DrawImage.cs @@ -5,13 +5,11 @@ namespace ImageSharp { - using System; using Drawing.Processors; - using ImageSharp.Drawing; using ImageSharp.PixelFormats; /// - /// Extension methods for the type. + /// Extension methods for the type. /// public static partial class ImageExtensions { diff --git a/src/ImageSharp.Drawing/Pens/Pen.cs b/src/ImageSharp.Drawing/Pens/Pen.cs deleted file mode 100644 index a3cc3cbdf..000000000 --- a/src/ImageSharp.Drawing/Pens/Pen.cs +++ /dev/null @@ -1,55 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp.Drawing.Pens -{ - using ImageSharp.PixelFormats; - - /// - /// Represents a in the color space. - /// - public class Pen : Pen - { - /// - /// Initializes a new instance of the class. - /// - /// The color. - /// The width. - public Pen(Rgba32 color, float width) - : base(color, width) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The brush. - /// The width. - public Pen(IBrush brush, float width) - : base(brush, width) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The brush. - /// The width. - /// The pattern. - public Pen(IBrush brush, float width, float[] pattern) - : base(brush, width, pattern) - { - } - - /// - /// Initializes a new instance of the class. - /// - /// The pen. - internal Pen(Pen pen) - : base(pen) - { - } - } -} \ No newline at end of file diff --git a/src/ImageSharp.Drawing/Pens/Pens.cs b/src/ImageSharp.Drawing/Pens/Pens.cs deleted file mode 100644 index 5c91df226..000000000 --- a/src/ImageSharp.Drawing/Pens/Pens.cs +++ /dev/null @@ -1,95 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp.Drawing.Pens -{ - using ImageSharp.PixelFormats; - - /// - /// Common Pen styles - /// - public class Pens - { - /// - /// Create a solid pen with out any drawing patterns - /// - /// The color. - /// The width. - /// The Pen - public static Pen Solid(Rgba32 color, float width) => new Pen(color, width); - - /// - /// Create a solid pen with out any drawing patterns - /// - /// The brush. - /// The width. - /// The Pen - public static Pen Solid(IBrush brush, float width) => new Pen(brush, width); - - /// - /// Create a pen with a 'Dash' drawing patterns - /// - /// The color. - /// The width. - /// The Pen - public static Pen Dash(Rgba32 color, float width) => new Pen(Pens.Dash(color, width)); - - /// - /// Create a pen with a 'Dash' drawing patterns - /// - /// The brush. - /// The width. - /// The Pen - public static Pen Dash(IBrush brush, float width) => new Pen(Pens.Dash(brush, width)); - - /// - /// Create a pen with a 'Dot' drawing patterns - /// - /// The color. - /// The width. - /// The Pen - public static Pen Dot(Rgba32 color, float width) => new Pen(Pens.Dot(color, width)); - - /// - /// Create a pen with a 'Dot' drawing patterns - /// - /// The brush. - /// The width. - /// The Pen - public static Pen Dot(IBrush brush, float width) => new Pen(Pens.Dot(brush, width)); - - /// - /// Create a pen with a 'Dash Dot' drawing patterns - /// - /// The color. - /// The width. - /// The Pen - public static Pen DashDot(Rgba32 color, float width) => new Pen(Pens.DashDot(color, width)); - - /// - /// Create a pen with a 'Dash Dot' drawing patterns - /// - /// The brush. - /// The width. - /// The Pen - public static Pen DashDot(IBrush brush, float width) => new Pen(Pens.DashDot(brush, width)); - - /// - /// Create a pen with a 'Dash Dot Dot' drawing patterns - /// - /// The color. - /// The width. - /// The Pen - public static Pen DashDotDot(Rgba32 color, float width) => new Pen(Pens.DashDotDot(color, width)); - - /// - /// Create a pen with a 'Dash Dot Dot' drawing patterns - /// - /// The brush. - /// The width. - /// The Pen - public static Pen DashDotDot(IBrush brush, float width) => new Pen(Pens.DashDotDot(brush, width)); - } -} \ No newline at end of file diff --git a/src/ImageSharp/Common/Helpers/ImageMaths.cs b/src/ImageSharp/Common/Helpers/ImageMaths.cs index 25df0ebf0..0bfcec361 100644 --- a/src/ImageSharp/Common/Helpers/ImageMaths.cs +++ b/src/ImageSharp/Common/Helpers/ImageMaths.cs @@ -159,7 +159,7 @@ namespace ImageSharp /// than the given one. /// /// The pixel format. - /// The to search within. + /// The to search within. /// The color component value to remove. /// The channel to test against. /// diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index a9aac5efa..dff53d77f 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -127,7 +127,7 @@ namespace ImageSharp.Formats + $"bigger then the max allowed size '{Image.MaxWidth}x{Image.MaxHeight}'"); } - Image image = Image.Create(this.infoHeader.Width, this.infoHeader.Height, this.configuration); + Image image = Image.Create(this.infoHeader.Width, this.infoHeader.Height, this.configuration); using (PixelAccessor pixels = image.Lock()) { switch (this.infoHeader.Compression) diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 589b7037a..8a37ed7bc 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -366,7 +366,7 @@ namespace ImageSharp.Formats this.metaData.Quality = colorTableLength / 3; // This initializes the image to become fully transparent because the alpha channel is zero. - this.image = Image.Create(imageWidth, imageHeight, this.metaData, this.configuration); + this.image = Image.Create(imageWidth, imageHeight, this.metaData, this.configuration); this.SetFrameMetaData(this.metaData); diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 9df21a3b7..359e345ef 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -482,7 +482,7 @@ namespace ImageSharp.Formats private Image ConvertJpegPixelsToImagePixels(ImageMetaData metadata) where TPixel : struct, IPixel { - Image image = Image.Create(this.ImageWidth, this.ImageHeight, metadata, this.configuration); + Image image = Image.Create(this.ImageWidth, this.ImageHeight, metadata, this.configuration); if (this.grayImage.IsInitialized) { diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 904aa1ff6..f3715d68b 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -335,7 +335,7 @@ namespace ImageSharp.Formats throw new ArgumentOutOfRangeException($"The input png '{this.header.Width}x{this.header.Height}' is bigger than the max allowed size '{Image.MaxWidth}x{Image.MaxHeight}'"); } - image = Image.Create(this.header.Width, this.header.Height, metadata, this.configuration); + image = Image.Create(this.header.Width, this.header.Height, metadata, this.configuration); pixels = image.Lock(); this.bytesPerPixel = this.CalculateBytesPerPixel(); this.bytesPerScanline = this.CalculateScanlineLength(this.header.Width) + 1; diff --git a/src/ImageSharp/Image.Create.cs b/src/ImageSharp/Image.Create.cs deleted file mode 100644 index 3d92cd66b..000000000 --- a/src/ImageSharp/Image.Create.cs +++ /dev/null @@ -1,62 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ - using ImageSharp.PixelFormats; - - /// - /// Represents an image. Each pixel is a made up four 8-bit components red, green, blue, and alpha - /// packed into a single unsigned integer value. - /// - public sealed partial class Image - { - /// - /// Create a new instance of the class - /// with the height and the width of the image. - /// - /// The pixel format. - /// The width of the image in pixels. - /// The height of the image in pixels. - /// The images matadata to preload. - /// - /// The configuration providing initialization code which allows extending the library. - /// - /// - /// A new unless is in which case it returns - /// - internal static Image Create(int width, int height, ImageMetaData metadata, Configuration configuration) - where TPixel : struct, IPixel - { - if (typeof(TPixel) == typeof(Rgba32)) - { - return new Image(width, height, metadata, configuration) as Image; - } - else - { - return new Image(width, height, metadata, configuration); - } - } - - /// - /// Create a new instance of the class - /// with the height and the width of the image. - /// - /// The pixel format. - /// The width of the image in pixels. - /// The height of the image in pixels. - /// - /// The configuration providing initialization code which allows extending the library. - /// - /// - /// A new unless is in which case it returns - /// - internal static Image Create(int width, int height, Configuration configuration) - where TPixel : struct, IPixel - { - return Image.Create(width, height, null, configuration); - } - } -} \ No newline at end of file diff --git a/src/ImageSharp/Image.FromBytes.cs b/src/ImageSharp/Image.FromBytes.cs deleted file mode 100644 index 540eb6016..000000000 --- a/src/ImageSharp/Image.FromBytes.cs +++ /dev/null @@ -1,214 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ - using System; - using System.IO; - using Formats; - - using ImageSharp.PixelFormats; - - /// - /// Represents an image. Each pixel is a made up four 8-bit components red, green, blue, and alpha - /// packed into a single unsigned integer value. - /// - public sealed partial class Image - { - /// - /// Loads the image from the given byte array. - /// - /// The byte array containing image data. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(byte[] data) - { - return Load(null, data, null); - } - - /// - /// Loads the image from the given byte array. - /// - /// The byte array containing image data. - /// The options for the decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(byte[] data, IDecoderOptions options) - { - return Load(null, data, options); - } - - /// - /// Loads the image from the given byte array. - /// - /// The config for the decoder. - /// The byte array containing image data. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Configuration config, byte[] data) - { - return Load(config, data, null); - } - - /// - /// Loads the image from the given byte array. - /// - /// The byte array containing image data. - /// The decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(byte[] data, IImageDecoder decoder) - { - return Load(data, decoder, null); - } - - /// - /// Loads the image from the given byte array. - /// - /// The configuration options. - /// The byte array containing image data. - /// The options for the decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Configuration config, byte[] data, IDecoderOptions options) - { - using (MemoryStream ms = new MemoryStream(data)) - { - return Load(config, ms, options); - } - } - - /// - /// Loads the image from the given byte array. - /// - /// The byte array containing image data. - /// The decoder. - /// The options for the decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(byte[] data, IImageDecoder decoder, IDecoderOptions options) - { - using (MemoryStream ms = new MemoryStream(data)) - { - return Load(ms, decoder, options); - } - } - - /// - /// Loads the image from the given byte array. - /// - /// The pixel format. - /// The byte array containing image data. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(byte[] data) - where TPixel : struct, IPixel - { - return Load(null, data, null); - } - - /// - /// Loads the image from the given byte array. - /// - /// The pixel format. - /// The byte array containing image data. - /// The options for the decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(byte[] data, IDecoderOptions options) - where TPixel : struct, IPixel - { - return Load(null, data, options); - } - - /// - /// Loads the image from the given byte array. - /// - /// The pixel format. - /// The config for the decoder. - /// The byte array containing image data. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Configuration config, byte[] data) - where TPixel : struct, IPixel - { - return Load(config, data, null); - } - - /// - /// Loads the image from the given byte array. - /// - /// The pixel format. - /// The byte array containing image data. - /// The decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(byte[] data, IImageDecoder decoder) - where TPixel : struct, IPixel - { - return Load(data, decoder, null); - } - - /// - /// Loads the image from the given byte array. - /// - /// The pixel format. - /// The configuration options. - /// The byte array containing image data. - /// The options for the decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Configuration config, byte[] data, IDecoderOptions options) - where TPixel : struct, IPixel - { - using (MemoryStream ms = new MemoryStream(data)) - { - return Load(config, ms, options); - } - } - - /// - /// Loads the image from the given byte array. - /// - /// The pixel format. - /// The byte array containing image data. - /// The decoder. - /// The options for the decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(byte[] data, IImageDecoder decoder, IDecoderOptions options) - where TPixel : struct, IPixel - { - using (MemoryStream ms = new MemoryStream(data)) - { - return Load(ms, decoder, options); - } - } - } -} diff --git a/src/ImageSharp/Image.FromFile.cs b/src/ImageSharp/Image.FromFile.cs deleted file mode 100644 index 8f4c9138b..000000000 --- a/src/ImageSharp/Image.FromFile.cs +++ /dev/null @@ -1,215 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ -#if !NETSTANDARD1_1 - using System; - using System.IO; - using Formats; - using ImageSharp.PixelFormats; - - /// - /// Represents an image. Each pixel is a made up four 8-bit components red, green, blue, and alpha - /// packed into a single unsigned integer value. - /// - public sealed partial class Image - { - /// - /// Loads the image from the given file. - /// - /// The file path to the image. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(string path) - { - return Load(null, path, null); - } - - /// - /// Loads the image from the given file. - /// - /// The file path to the image. - /// The options for the decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(string path, IDecoderOptions options) - { - return Load(null, path, options); - } - - /// - /// Loads the image from the given file. - /// - /// The config for the decoder. - /// The file path to the image. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Configuration config, string path) - { - return Load(config, path, null); - } - - /// - /// Loads the image from the given file. - /// - /// The file path to the image. - /// The decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(string path, IImageDecoder decoder) - { - return Load(path, decoder, null); - } - - /// - /// Loads the image from the given file. - /// - /// The file path to the image. - /// The decoder. - /// The options for the decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(string path, IImageDecoder decoder, IDecoderOptions options) - { - return new Image(Load(path, decoder, options)); - } - - /// - /// Loads the image from the given file. - /// - /// The configuration options. - /// The file path to the image. - /// The options for the decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Configuration config, string path, IDecoderOptions options) - { - config = config ?? Configuration.Default; - using (Stream s = config.FileSystem.OpenRead(path)) - { - return Load(config, s, options); - } - } - - /// - /// Loads the image from the given file. - /// - /// The pixel format. - /// The file path to the image. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(string path) - where TPixel : struct, IPixel - { - return Load(null, path, null); - } - - /// - /// Loads the image from the given file. - /// - /// The pixel format. - /// The file path to the image. - /// The options for the decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(string path, IDecoderOptions options) - where TPixel : struct, IPixel - { - return Load(null, path, options); - } - - /// - /// Loads the image from the given file. - /// - /// The pixel format. - /// The config for the decoder. - /// The file path to the image. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Configuration config, string path) - where TPixel : struct, IPixel - { - return Load(config, path, null); - } - - /// - /// Loads the image from the given file. - /// - /// The pixel format. - /// The file path to the image. - /// The decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(string path, IImageDecoder decoder) - where TPixel : struct, IPixel - { - return Load(path, decoder, null); - } - - /// - /// Loads the image from the given file. - /// - /// The pixel format. - /// The configuration options. - /// The file path to the image. - /// The options for the decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Configuration config, string path, IDecoderOptions options) - where TPixel : struct, IPixel - { - config = config ?? Configuration.Default; - using (Stream s = config.FileSystem.OpenRead(path)) - { - return Load(config, s, options); - } - } - - /// - /// Loads the image from the given file. - /// - /// The pixel format. - /// The file path to the image. - /// The decoder. - /// The options for the decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(string path, IImageDecoder decoder, IDecoderOptions options) - where TPixel : struct, IPixel - { - Configuration config = Configuration.Default; - using (Stream s = config.FileSystem.OpenRead(path)) - { - return Load(s, decoder, options); - } - } - } -#endif -} diff --git a/src/ImageSharp/Image.FromStream.cs b/src/ImageSharp/Image.FromStream.cs deleted file mode 100644 index a34c6b7b3..000000000 --- a/src/ImageSharp/Image.FromStream.cs +++ /dev/null @@ -1,249 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ - using System; - using System.IO; - using System.Numerics; - using System.Text; - using Formats; - - using ImageSharp.PixelFormats; - - /// - /// Represents an image. Each pixel is a made up four 8-bit components red, green, blue, and alpha - /// packed into a single unsigned integer value. - /// - public sealed partial class Image - { - /// - /// Loads the image from the given stream. - /// - /// The stream containing image information. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Stream stream) - { - return Load(null, stream, null); - } - - /// - /// Loads the image from the given stream. - /// - /// The stream containing image information. - /// The options for the decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Stream stream, IDecoderOptions options) - { - return Load(null, stream, options); - } - - /// - /// Loads the image from the given stream. - /// - /// The config for the decoder. - /// The stream containing image information. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Configuration config, Stream stream) - { - return Load(config, stream, null); - } - - /// - /// Loads the image from the given stream. - /// - /// The stream containing image information. - /// The decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Stream stream, IImageDecoder decoder) - { - return Load(stream, decoder, null); - } - - /// - /// Loads the image from the given stream. - /// - /// The configuration options. - /// The stream containing image information. - /// The options for the decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Configuration config, Stream stream, IDecoderOptions options) - { - Image image = Load(config, stream, options); - - return image as Image ?? new Image(image); - } - - /// - /// Loads the image from the given stream. - /// - /// The stream containing image information. - /// The decoder. - /// The options for the decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Stream stream, IImageDecoder decoder, IDecoderOptions options) - { - Image image = new Image(Load(stream, decoder, options)); - - return image as Image ?? new Image(image); - } - - /// - /// Loads the image from the given stream. - /// - /// The pixel format. - /// The stream containing image information. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Stream stream) - where TPixel : struct, IPixel - { - return Load(null, stream, null); - } - - /// - /// Loads the image from the given stream. - /// - /// The pixel format. - /// The stream containing image information. - /// The options for the decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Stream stream, IDecoderOptions options) - where TPixel : struct, IPixel - { - return Load(null, stream, options); - } - - /// - /// Loads the image from the given stream. - /// - /// The pixel format. - /// The config for the decoder. - /// The stream containing image information. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Configuration config, Stream stream) - where TPixel : struct, IPixel - { - return Load(config, stream, null); - } - - /// - /// Loads the image from the given stream. - /// - /// The pixel format. - /// The stream containing image information. - /// The decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Stream stream, IImageDecoder decoder) - where TPixel : struct, IPixel - { - return Load(stream, decoder, null); - } - - /// - /// Loads the image from the given stream. - /// - /// The pixel format. - /// The stream containing image information. - /// The decoder. - /// The options for the decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Stream stream, IImageDecoder decoder, IDecoderOptions options) - where TPixel : struct, IPixel - { - return WithSeekableStream(stream, s => decoder.Decode(Configuration.Default, s, options)); - } - - /// - /// Loads the image from the given stream. - /// - /// The pixel format. - /// The configuration options. - /// The stream containing image information. - /// The options for the decoder. - /// - /// Thrown if the stream is not readable nor seekable. - /// - /// The image - public static Image Load(Configuration config, Stream stream, IDecoderOptions options) - where TPixel : struct, IPixel - { - config = config ?? Configuration.Default; - - Image img = WithSeekableStream(stream, s => Decode(s, options, config)); - - if (img != null) - { - return img; - } - - StringBuilder stringBuilder = new StringBuilder(); - stringBuilder.AppendLine("Image cannot be loaded. Available formats:"); - - foreach (IImageFormat format in config.ImageFormats) - { - stringBuilder.AppendLine("-" + format); - } - - throw new NotSupportedException(stringBuilder.ToString()); - } - - private static T WithSeekableStream(Stream stream, Func action) - { - if (!stream.CanRead) - { - throw new NotSupportedException("Cannot read from the stream."); - } - - if (stream.CanSeek) - { - return action(stream); - } - else - { - // We want to be able to load images from things like HttpContext.Request.Body - using (MemoryStream ms = new MemoryStream()) - { - stream.CopyTo(ms); - ms.Position = 0; - - return action(ms); - } - } - } - } -} diff --git a/src/ImageSharp/Image.cs b/src/ImageSharp/Image.cs deleted file mode 100644 index 3d33e6263..000000000 --- a/src/ImageSharp/Image.cs +++ /dev/null @@ -1,69 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ - using System.Diagnostics; - using ImageSharp.PixelFormats; - - /// - /// Represents an image. Each pixel is a made up four 8-bit components red, green, blue, and alpha - /// packed into a single unsigned integer value. - /// - [DebuggerDisplay("Image: {Width}x{Height}")] - public sealed partial class Image : Image - { - /// - /// Initializes a new instance of the class - /// with the height and the width of the image. - /// - /// The width of the image in pixels. - /// The height of the image in pixels. - /// - /// The configuration providing initialization code which allows extending the library. - /// - public Image(int width, int height, Configuration configuration) - : base(width, height, configuration) - { - } - - /// - /// Initializes a new instance of the class - /// with the height and the width of the image. - /// - /// The width of the image in pixels. - /// The height of the image in pixels. - public Image(int width, int height) - : this(width, height, null) - { - } - - /// - /// Initializes a new instance of the class - /// by making a copy from another image. - /// - /// The other image, where the clone should be made from. - /// is null. - public Image(Image other) - : base(other) - { - } - - /// - /// Initializes a new instance of the class - /// with the height and the width of the image. - /// - /// The width of the image in pixels. - /// The height of the image in pixels. - /// The metadata. - /// - /// The configuration providing initialization code which allows extending the library. - /// - internal Image(int width, int height, ImageMetaData metadata, Configuration configuration) - : base(width, height, metadata, configuration) - { - } - } -} \ No newline at end of file diff --git a/src/ImageSharp/Image/Image{TPixel}.Create.cs b/src/ImageSharp/Image/Image{TPixel}.Create.cs new file mode 100644 index 000000000..8a64fd748 --- /dev/null +++ b/src/ImageSharp/Image/Image{TPixel}.Create.cs @@ -0,0 +1,49 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp +{ + using ImageSharp.PixelFormats; + + /// + /// Adds static methods allowing the creation of new images from given dimensions. + /// + public partial class Image + where TPixel : struct, IPixel + { + /// + /// Create a new instance of the class with the given height and the width. + /// + /// The width of the image in pixels. + /// The height of the image in pixels. + /// + /// The configuration providing initialization code which allows extending the library. + /// + /// + /// A new . + /// + internal static Image Create(int width, int height, Configuration configuration) + { + return Create(width, height, null, configuration); + } + + /// + /// Create a new instance of the class with the given height and the width. + /// + /// The width of the image in pixels. + /// The height of the image in pixels. + /// The images matadata to preload. + /// + /// The configuration providing initialization code which allows extending the library. + /// + /// + /// A new . + /// + internal static Image Create(int width, int height, ImageMetaData metadata, Configuration configuration) + { + return new Image(width, height, metadata, configuration); + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Image.Decode.cs b/src/ImageSharp/Image/Image{TPixel}.Decode.cs similarity index 84% rename from src/ImageSharp/Image.Decode.cs rename to src/ImageSharp/Image/Image{TPixel}.Decode.cs index b0e476280..bef55ecda 100644 --- a/src/ImageSharp/Image.Decode.cs +++ b/src/ImageSharp/Image/Image{TPixel}.Decode.cs @@ -12,11 +12,11 @@ namespace ImageSharp using ImageSharp.PixelFormats; - /// - /// Represents an image. Each pixel is a made up four 8-bit components red, green, blue, and alpha - /// packed into a single unsigned integer value. - /// - public sealed partial class Image + /// + /// Adds static methods allowing the decoding of new images. + /// + public partial class Image + where TPixel : struct, IPixel { /// /// By reading the header on the provided stream this calculates the images format. @@ -53,15 +53,13 @@ namespace ImageSharp /// /// Decodes the image stream to the current image. /// - /// The pixel format. /// The stream. /// The options for the decoder. /// the configuration. /// /// The decoded image /// - private static Image Decode(Stream stream, IDecoderOptions options, Configuration config) - where TPixel : struct, IPixel + private static Image Decode(Stream stream, IDecoderOptions options, Configuration config) { IImageFormat format = DiscoverFormat(stream, config); if (format == null) @@ -74,4 +72,4 @@ namespace ImageSharp return img; } } -} +} \ No newline at end of file diff --git a/src/ImageSharp/Image/Image{TPixel}.FromBytes.cs b/src/ImageSharp/Image/Image{TPixel}.FromBytes.cs new file mode 100644 index 000000000..e401d9d58 --- /dev/null +++ b/src/ImageSharp/Image/Image{TPixel}.FromBytes.cs @@ -0,0 +1,92 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp +{ + using System.IO; + using Formats; + + using ImageSharp.PixelFormats; + + /// + /// Adds static methods allowing the creation of new image from a byte array. + /// + public partial class Image + where TPixel : struct, IPixel + { + /// + /// Create a new instance of the class from the given byte array. + /// + /// The byte array containing image data. + /// A new . + public static Image Load(byte[] data) + { + return Load(null, data, null); + } + + /// + /// Create a new instance of the class from the given byte array. + /// + /// The byte array containing image data. + /// The options for the decoder. + /// A new . + public static Image Load(byte[] data, IDecoderOptions options) + { + return Load(null, data, options); + } + + /// + /// Create a new instance of the class from the given byte array. + /// + /// The config for the decoder. + /// The byte array containing image data. + /// A new . + public static Image Load(Configuration config, byte[] data) + { + return Load(config, data, null); + } + + /// + /// Create a new instance of the class from the given byte array. + /// + /// The byte array containing image data. + /// The decoder. + /// A new . + public static Image Load(byte[] data, IImageDecoder decoder) + { + return Load(data, decoder, null); + } + + /// + /// Create a new instance of the class from the given byte array. + /// + /// The configuration options. + /// The byte array containing image data. + /// The options for the decoder. + /// A new . + public static Image Load(Configuration config, byte[] data, IDecoderOptions options) + { + using (MemoryStream ms = new MemoryStream(data)) + { + return Load(config, ms, options); + } + } + + /// + /// Create a new instance of the class from the given byte array. + /// + /// The byte array containing image data. + /// The decoder. + /// The options for the decoder. + /// A new . + public static Image Load(byte[] data, IImageDecoder decoder, IDecoderOptions options) + { + using (MemoryStream ms = new MemoryStream(data)) + { + return Load(ms, decoder, options); + } + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Image/Image{TPixel}.FromFile.cs b/src/ImageSharp/Image/Image{TPixel}.FromFile.cs new file mode 100644 index 000000000..0c6431407 --- /dev/null +++ b/src/ImageSharp/Image/Image{TPixel}.FromFile.cs @@ -0,0 +1,114 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp +{ +#if !NETSTANDARD1_1 + using System; + using System.IO; + using Formats; + using ImageSharp.PixelFormats; + + /// + /// Adds static methods allowing the creation of new image from a given file. + /// + public partial class Image + where TPixel : struct, IPixel + { + /// + /// Create a new instance of the class from the given file. + /// + /// The file path to the image. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// A new . + public static Image Load(string path) + { + return Load(null, path, null); + } + + /// + /// Create a new instance of the class from the given file. + /// + /// The file path to the image. + /// The options for the decoder. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// A new . + public static Image Load(string path, IDecoderOptions options) + { + return Load(null, path, options); + } + + /// + /// Create a new instance of the class from the given file. + /// + /// The config for the decoder. + /// The file path to the image. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// A new . + public static Image Load(Configuration config, string path) + { + return Load(config, path, null); + } + + /// + /// Create a new instance of the class from the given file. + /// + /// The file path to the image. + /// The decoder. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// A new . + public static Image Load(string path, IImageDecoder decoder) + { + return Load(path, decoder, null); + } + + /// + /// Create a new instance of the class from the given file. + /// + /// The configuration options. + /// The file path to the image. + /// The options for the decoder. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// A new . + public static Image Load(Configuration config, string path, IDecoderOptions options) + { + config = config ?? Configuration.Default; + using (Stream s = config.FileSystem.OpenRead(path)) + { + return Load(config, s, options); + } + } + + /// + /// Create a new instance of the class from the given file. + /// + /// The file path to the image. + /// The decoder. + /// The options for the decoder. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// A new . + public static Image Load(string path, IImageDecoder decoder, IDecoderOptions options) + { + Configuration config = Configuration.Default; + using (Stream s = config.FileSystem.OpenRead(path)) + { + return Load(s, decoder, options); + } + } + } +#endif +} \ No newline at end of file diff --git a/src/ImageSharp/Image/Image{TPixel}.FromStream.cs b/src/ImageSharp/Image/Image{TPixel}.FromStream.cs new file mode 100644 index 000000000..fabd02ca8 --- /dev/null +++ b/src/ImageSharp/Image/Image{TPixel}.FromStream.cs @@ -0,0 +1,144 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp +{ + using System; + using System.IO; + using System.Text; + using Formats; + + using ImageSharp.PixelFormats; + + /// + /// Adds static methods allowing the creation of new image from a given stream. + /// + public partial class Image + where TPixel : struct, IPixel + { + /// + /// Create a new instance of the class from the given stream. + /// + /// The stream containing image information. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// The image + public static Image Load(Stream stream) + { + return Load(null, stream, null); + } + + /// + /// Create a new instance of the class from the given stream. + /// + /// The stream containing image information. + /// The options for the decoder. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// The image + public static Image Load(Stream stream, IDecoderOptions options) + { + return Load(null, stream, options); + } + + /// + /// Create a new instance of the class from the given stream. + /// + /// The config for the decoder. + /// The stream containing image information. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// The image + public static Image Load(Configuration config, Stream stream) + { + return Load(config, stream, null); + } + + /// + /// Create a new instance of the class from the given stream. + /// + /// The stream containing image information. + /// The decoder. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// The image + public static Image Load(Stream stream, IImageDecoder decoder) + { + return Load(stream, decoder, null); + } + + /// + /// Create a new instance of the class from the given stream. + /// + /// The stream containing image information. + /// The decoder. + /// The options for the decoder. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// The image + public static Image Load(Stream stream, IImageDecoder decoder, IDecoderOptions options) + { + return WithSeekableStream(stream, s => decoder.Decode(Configuration.Default, s, options)); + } + + /// + /// Create a new instance of the class from the given stream. + /// + /// The configuration options. + /// The stream containing image information. + /// The options for the decoder. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// The image + public static Image Load(Configuration config, Stream stream, IDecoderOptions options) + { + config = config ?? Configuration.Default; + Image img = WithSeekableStream(stream, s => Decode(s, options, config)); + + if (img != null) + { + return img; + } + + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.AppendLine("Image cannot be loaded. Available formats:"); + + foreach (IImageFormat format in config.ImageFormats) + { + stringBuilder.AppendLine("-" + format); + } + + throw new NotSupportedException(stringBuilder.ToString()); + } + + private static T WithSeekableStream(Stream stream, Func action) + { + if (!stream.CanRead) + { + throw new NotSupportedException("Cannot read from the stream."); + } + + if (stream.CanSeek) + { + return action(stream); + } + + // We want to be able to load images from things like HttpContext.Request.Body + using (MemoryStream ms = new MemoryStream()) + { + stream.CopyTo(ms); + ms.Position = 0; + + return action(ms); + } + } + } +} \ No newline at end of file diff --git a/src/ImageSharp/Image/Image{TPixel}.cs b/src/ImageSharp/Image/Image{TPixel}.cs index 9e103c700..ce8aecfea 100644 --- a/src/ImageSharp/Image/Image{TPixel}.cs +++ b/src/ImageSharp/Image/Image{TPixel}.cs @@ -22,7 +22,7 @@ namespace ImageSharp /// /// The pixel format. [DebuggerDisplay("Image: {Width}x{Height}")] - public class Image : ImageBase, IImage + public partial class Image : ImageBase, IImage where TPixel : struct, IPixel { /// diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs index f65c02043..a65a9e80e 100644 --- a/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs +++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs @@ -5,7 +5,6 @@ namespace ImageSharp { - using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.IO; @@ -139,7 +138,7 @@ namespace ImageSharp using (MemoryStream memStream = new MemoryStream(this.data, this.thumbnailOffset, this.thumbnailLength)) { - return Image.Load(memStream); + return Image.Load(memStream); } } diff --git a/src/ImageSharp/Processing/Binarization/Dither.cs b/src/ImageSharp/Processing/Binarization/Dither.cs index 617883d6a..2a359c089 100644 --- a/src/ImageSharp/Processing/Binarization/Dither.cs +++ b/src/ImageSharp/Processing/Binarization/Dither.cs @@ -12,7 +12,7 @@ namespace ImageSharp using ImageSharp.Processing.Processors; /// - /// Extension methods for the type. + /// Extension methods for the type. /// public static partial class ImageExtensions { @@ -40,7 +40,7 @@ namespace ImageSharp /// The structure that specifies the portion of the image object to alter. /// /// The component index to test the threshold against. Must range from 0 to 3. - /// The . + /// The . public static Image Dither(this Image source, IOrderedDither dither, Rectangle rectangle, int index = 0) where TPixel : struct, IPixel { @@ -72,7 +72,7 @@ namespace ImageSharp /// /// The structure that specifies the portion of the image object to alter. /// - /// The . + /// The . public static Image Dither(this Image source, IErrorDiffuser diffuser, float threshold, Rectangle rectangle) where TPixel : struct, IPixel { diff --git a/src/ImageSharp/Processing/ColorMatrix/Sepia.cs b/src/ImageSharp/Processing/ColorMatrix/Sepia.cs index d60ec7165..39eca4e8e 100644 --- a/src/ImageSharp/Processing/ColorMatrix/Sepia.cs +++ b/src/ImageSharp/Processing/ColorMatrix/Sepia.cs @@ -22,7 +22,7 @@ namespace ImageSharp /// /// The pixel format. /// The image this method extends. - /// The . + /// The . public static Image Sepia(this Image source) where TPixel : struct, IPixel { @@ -37,7 +37,7 @@ namespace ImageSharp /// /// The structure that specifies the portion of the image object to alter. /// - /// The . + /// The . public static Image Sepia(this Image source, Rectangle rectangle) where TPixel : struct, IPixel { diff --git a/src/ImageSharp/Processing/Effects/Alpha.cs b/src/ImageSharp/Processing/Effects/Alpha.cs index a2f721cfa..73b682b93 100644 --- a/src/ImageSharp/Processing/Effects/Alpha.cs +++ b/src/ImageSharp/Processing/Effects/Alpha.cs @@ -12,7 +12,7 @@ namespace ImageSharp using Processing.Processors; /// - /// Extension methods for the type. + /// Extension methods for the type. /// public static partial class ImageExtensions { @@ -38,7 +38,7 @@ namespace ImageSharp /// /// The structure that specifies the portion of the image object to alter. /// - /// The . + /// The . public static Image Alpha(this Image source, float percent, Rectangle rectangle) where TPixel : struct, IPixel { diff --git a/src/ImageSharp/Processing/Effects/BackgroundColor.cs b/src/ImageSharp/Processing/Effects/BackgroundColor.cs index f52cf1cb2..975d2c24b 100644 --- a/src/ImageSharp/Processing/Effects/BackgroundColor.cs +++ b/src/ImageSharp/Processing/Effects/BackgroundColor.cs @@ -40,7 +40,7 @@ namespace ImageSharp /// The structure that specifies the portion of the image object to alter. /// /// The options effecting pixel blending. - /// The . + /// The . public static Image BackgroundColor(this Image source, TPixel color, Rectangle rectangle, GraphicsOptions options) where TPixel : struct, IPixel { @@ -70,7 +70,7 @@ namespace ImageSharp /// /// The structure that specifies the portion of the image object to alter. /// - /// The . + /// The . public static Image BackgroundColor(this Image source, TPixel color, Rectangle rectangle) where TPixel : struct, IPixel { diff --git a/src/ImageSharp/Processing/Effects/Invert.cs b/src/ImageSharp/Processing/Effects/Invert.cs index 113d8289e..fe3bb7dc9 100644 --- a/src/ImageSharp/Processing/Effects/Invert.cs +++ b/src/ImageSharp/Processing/Effects/Invert.cs @@ -21,7 +21,7 @@ namespace ImageSharp /// /// The pixel format. /// The image this method extends. - /// The . + /// The . public static Image Invert(this Image source) where TPixel : struct, IPixel { @@ -36,7 +36,7 @@ namespace ImageSharp /// /// The structure that specifies the portion of the image object to alter. /// - /// The . + /// The . public static Image Invert(this Image source, Rectangle rectangle) where TPixel : struct, IPixel { diff --git a/src/ImageSharp/ImageProcessor.cs b/src/ImageSharp/Processing/ImageProcessor.cs similarity index 100% rename from src/ImageSharp/ImageProcessor.cs rename to src/ImageSharp/Processing/ImageProcessor.cs diff --git a/src/ImageSharp/Processing/Processors/Binarization/BinaryThresholdProcessor.cs b/src/ImageSharp/Processing/Processors/Binarization/BinaryThresholdProcessor.cs index 566449b27..5cd67f053 100644 --- a/src/ImageSharp/Processing/Processors/Binarization/BinaryThresholdProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Binarization/BinaryThresholdProcessor.cs @@ -12,7 +12,7 @@ namespace ImageSharp.Processing.Processors /// /// An to perform binary threshold filtering against an - /// . The image will be converted to grayscale before thresholding occurs. + /// . The image will be converted to grayscale before thresholding occurs. /// /// The pixel format. internal class BinaryThresholdProcessor : ImageProcessor diff --git a/src/ImageSharp/Processing/Transforms/AutoOrient.cs b/src/ImageSharp/Processing/Transforms/AutoOrient.cs index de736092d..f9d3a60aa 100644 --- a/src/ImageSharp/Processing/Transforms/AutoOrient.cs +++ b/src/ImageSharp/Processing/Transforms/AutoOrient.cs @@ -22,7 +22,7 @@ namespace ImageSharp /// /// The pixel format. /// The image to auto rotate. - /// The + /// The public static Image AutoOrient(this Image source) where TPixel : struct, IPixel { diff --git a/src/ImageSharp/Processing/Transforms/Crop.cs b/src/ImageSharp/Processing/Transforms/Crop.cs index 073e8136d..1cdef56c4 100644 --- a/src/ImageSharp/Processing/Transforms/Crop.cs +++ b/src/ImageSharp/Processing/Transforms/Crop.cs @@ -38,7 +38,7 @@ namespace ImageSharp /// /// The structure that specifies the portion of the image object to retain. /// - /// The + /// The public static Image Crop(this Image source, Rectangle cropRectangle) where TPixel : struct, IPixel { diff --git a/src/ImageSharp/Processing/Transforms/EntropyCrop.cs b/src/ImageSharp/Processing/Transforms/EntropyCrop.cs index aaafd396f..2f4a8e383 100644 --- a/src/ImageSharp/Processing/Transforms/EntropyCrop.cs +++ b/src/ImageSharp/Processing/Transforms/EntropyCrop.cs @@ -22,7 +22,7 @@ namespace ImageSharp /// The pixel format. /// The image to crop. /// The threshold for entropic density. - /// The + /// The public static Image EntropyCrop(this Image source, float threshold = .5f) where TPixel : struct, IPixel { diff --git a/src/ImageSharp/Processing/Transforms/Flip.cs b/src/ImageSharp/Processing/Transforms/Flip.cs index 41f2e5616..342b4dd46 100644 --- a/src/ImageSharp/Processing/Transforms/Flip.cs +++ b/src/ImageSharp/Processing/Transforms/Flip.cs @@ -23,7 +23,7 @@ namespace ImageSharp /// The pixel format. /// The image to rotate, flip, or both. /// The to perform the flip. - /// The + /// The public static Image Flip(this Image source, FlipType flipType) where TPixel : struct, IPixel { diff --git a/src/ImageSharp/Processing/Transforms/Rotate.cs b/src/ImageSharp/Processing/Transforms/Rotate.cs index b335a3c76..de9dbdc3b 100644 --- a/src/ImageSharp/Processing/Transforms/Rotate.cs +++ b/src/ImageSharp/Processing/Transforms/Rotate.cs @@ -23,7 +23,7 @@ namespace ImageSharp /// The pixel format. /// The image to rotate. /// The angle in degrees to perform the rotation. - /// The + /// The public static Image Rotate(this Image source, float degrees) where TPixel : struct, IPixel { @@ -36,7 +36,7 @@ namespace ImageSharp /// The pixel format. /// The image to rotate. /// The to perform the rotation. - /// The + /// The public static Image Rotate(this Image source, RotateType rotateType) where TPixel : struct, IPixel { @@ -50,7 +50,7 @@ namespace ImageSharp /// The image to rotate. /// The angle in degrees to perform the rotation. /// Whether to expand the image to fit the rotated result. - /// The + /// The public static Image Rotate(this Image source, float degrees, bool expand) where TPixel : struct, IPixel { diff --git a/src/ImageSharp/Processing/Transforms/RotateFlip.cs b/src/ImageSharp/Processing/Transforms/RotateFlip.cs index 396590359..460d004cb 100644 --- a/src/ImageSharp/Processing/Transforms/RotateFlip.cs +++ b/src/ImageSharp/Processing/Transforms/RotateFlip.cs @@ -23,7 +23,7 @@ namespace ImageSharp /// The image to rotate, flip, or both. /// The to perform the rotation. /// The to perform the flip. - /// The + /// The public static Image RotateFlip(this Image source, RotateType rotateType, FlipType flipType) where TPixel : struct, IPixel { diff --git a/src/ImageSharp/Processing/Transforms/Skew.cs b/src/ImageSharp/Processing/Transforms/Skew.cs index 0c9cfbc8e..d42d79225 100644 --- a/src/ImageSharp/Processing/Transforms/Skew.cs +++ b/src/ImageSharp/Processing/Transforms/Skew.cs @@ -23,7 +23,7 @@ namespace ImageSharp /// The image to skew. /// The angle in degrees to perform the rotation along the x-axis. /// The angle in degrees to perform the rotation along the y-axis. - /// The + /// The public static Image Skew(this Image source, float degreesX, float degreesY) where TPixel : struct, IPixel { @@ -38,7 +38,7 @@ namespace ImageSharp /// The angle in degrees to perform the rotation along the x-axis. /// The angle in degrees to perform the rotation along the y-axis. /// Whether to expand the image to fit the skewed result. - /// The + /// The public static Image Skew(this Image source, float degreesX, float degreesY, bool expand) where TPixel : struct, IPixel { diff --git a/tests/ImageSharp.Benchmarks/Drawing/DrawBeziers.cs b/tests/ImageSharp.Benchmarks/Drawing/DrawBeziers.cs index d0bd9f208..183782d91 100644 --- a/tests/ImageSharp.Benchmarks/Drawing/DrawBeziers.cs +++ b/tests/ImageSharp.Benchmarks/Drawing/DrawBeziers.cs @@ -14,9 +14,6 @@ namespace ImageSharp.Benchmarks using ImageSharp.PixelFormats; - using CoreImage = ImageSharp.Image; - using CorePoint = ImageSharp.Point; - public class DrawBeziers : BenchmarkBase { [Benchmark(Baseline = true, Description = "System.Drawing Draw Beziers")] @@ -48,7 +45,7 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp Draw Beziers")] public void DrawLinesCore() { - using (CoreImage image = new CoreImage(800, 800)) + using (Image image = new Image(800, 800)) { image.DrawBeziers( Rgba32.HotPink, diff --git a/tests/ImageSharp.Benchmarks/Drawing/DrawLines.cs b/tests/ImageSharp.Benchmarks/Drawing/DrawLines.cs index 2bd3f4a6a..5ecdec2c2 100644 --- a/tests/ImageSharp.Benchmarks/Drawing/DrawLines.cs +++ b/tests/ImageSharp.Benchmarks/Drawing/DrawLines.cs @@ -14,9 +14,6 @@ namespace ImageSharp.Benchmarks using ImageSharp.PixelFormats; - using CoreImage = ImageSharp.Image; - using CorePoint = ImageSharp.Point; - public class DrawLines : BenchmarkBase { [Benchmark(Baseline = true, Description = "System.Drawing Draw Lines")] @@ -24,7 +21,6 @@ namespace ImageSharp.Benchmarks { using (Bitmap destination = new Bitmap(800, 800)) { - using (Graphics graphics = Graphics.FromImage(destination)) { graphics.InterpolationMode = InterpolationMode.Default; @@ -47,7 +43,7 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp Draw Lines")] public void DrawLinesCore() { - using (CoreImage image = new CoreImage(800, 800)) + using (Image image = new Image(800, 800)) { image.DrawLines( Rgba32.HotPink, diff --git a/tests/ImageSharp.Benchmarks/Drawing/DrawPolygon.cs b/tests/ImageSharp.Benchmarks/Drawing/DrawPolygon.cs index a0f8b21d8..d3ff33956 100644 --- a/tests/ImageSharp.Benchmarks/Drawing/DrawPolygon.cs +++ b/tests/ImageSharp.Benchmarks/Drawing/DrawPolygon.cs @@ -9,8 +9,7 @@ namespace ImageSharp.Benchmarks using System.Drawing.Drawing2D; using BenchmarkDotNet.Attributes; - using CoreImage = ImageSharp.Image; - using CorePoint = ImageSharp.Point; + using System.IO; using System.Numerics; @@ -46,7 +45,7 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp Draw Polygon")] public void DrawPolygonCore() { - using (CoreImage image = new CoreImage(800, 800)) + using (Image image = new Image(800, 800)) { image.DrawPolygon( Rgba32.HotPink, diff --git a/tests/ImageSharp.Benchmarks/Drawing/FillPolygon.cs b/tests/ImageSharp.Benchmarks/Drawing/FillPolygon.cs index ac1082697..deba59554 100644 --- a/tests/ImageSharp.Benchmarks/Drawing/FillPolygon.cs +++ b/tests/ImageSharp.Benchmarks/Drawing/FillPolygon.cs @@ -15,8 +15,6 @@ namespace ImageSharp.Benchmarks using ImageSharp.PixelFormats; - using CoreImage = ImageSharp.Image; - public class FillPolygon : BenchmarkBase { private readonly Polygon shape; @@ -55,7 +53,7 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp Fill Polygon")] public void DrawSolidPolygonCore() { - using (CoreImage image = new CoreImage(800, 800)) + using (Image image = new Image(800, 800)) { image.FillPolygon( Rgba32.HotPink, @@ -75,7 +73,7 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp Fill Polygon - cached shape")] public void DrawSolidPolygonCoreCahced() { - using (CoreImage image = new CoreImage(800, 800)) + using (Image image = new Image(800, 800)) { image.Fill( Rgba32.HotPink, diff --git a/tests/ImageSharp.Benchmarks/Drawing/FillRectangle.cs b/tests/ImageSharp.Benchmarks/Drawing/FillRectangle.cs index 7b21dbdc6..a90a7a4c0 100644 --- a/tests/ImageSharp.Benchmarks/Drawing/FillRectangle.cs +++ b/tests/ImageSharp.Benchmarks/Drawing/FillRectangle.cs @@ -9,7 +9,6 @@ namespace ImageSharp.Benchmarks using System.Drawing.Drawing2D; using BenchmarkDotNet.Attributes; - using CoreImage = ImageSharp.Image; using CoreRectangle = ImageSharp.Rectangle; using CoreSize = ImageSharp.Size; @@ -38,7 +37,7 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp Fill Rectangle")] public CoreSize FillRactangleCore() { - using (CoreImage image = new CoreImage(800, 800)) + using (Image image = new Image(800, 800)) { image.Fill(Rgba32.HotPink, new CoreRectangle(10, 10, 190, 140)); @@ -49,7 +48,7 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp Fill Rectangle - As Polygon")] public CoreSize FillPolygonCore() { - using (CoreImage image = new CoreImage(800, 800)) + using (Image image = new Image(800, 800)) { image.FillPolygon( Rgba32.HotPink, diff --git a/tests/ImageSharp.Benchmarks/Drawing/FillWithPattern.cs b/tests/ImageSharp.Benchmarks/Drawing/FillWithPattern.cs index 580120abd..616140237 100644 --- a/tests/ImageSharp.Benchmarks/Drawing/FillWithPattern.cs +++ b/tests/ImageSharp.Benchmarks/Drawing/FillWithPattern.cs @@ -11,12 +11,9 @@ namespace ImageSharp.Benchmarks using BenchmarkDotNet.Attributes; + using ImageSharp.Drawing.Brushes; using ImageSharp.PixelFormats; - using CoreBrushes = ImageSharp.Drawing.Brushes.Brushes; - - using CoreImage = ImageSharp.Image; - public class FillWithPattern { [Benchmark(Baseline = true, Description = "System.Drawing Fill with Pattern")] @@ -40,9 +37,9 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp Fill with Pattern")] public void DrawPatternPolygon3Core() { - using (CoreImage image = new CoreImage(800, 800)) + using (Image image = new Image(800, 800)) { - image.Fill(CoreBrushes.BackwardDiagonal(Rgba32.HotPink)); + image.Fill(Brushes.BackwardDiagonal(Rgba32.HotPink)); using (MemoryStream ms = new MemoryStream()) { diff --git a/tests/ImageSharp.Benchmarks/Image/CopyPixels.cs b/tests/ImageSharp.Benchmarks/Image/CopyPixels.cs index aade8a8de..5b5d14750 100644 --- a/tests/ImageSharp.Benchmarks/Image/CopyPixels.cs +++ b/tests/ImageSharp.Benchmarks/Image/CopyPixels.cs @@ -11,15 +11,13 @@ namespace ImageSharp.Benchmarks.Image using ImageSharp.PixelFormats; - using CoreImage = ImageSharp.Image; - public class CopyPixels : BenchmarkBase { [Benchmark(Description = "Copy by Pixel")] public Rgba32 CopyByPixel() { - using (CoreImage source = new CoreImage(1024, 768)) - using (CoreImage target = new CoreImage(1024, 768)) + using (Image source = new Image(1024, 768)) + using (Image target = new Image(1024, 768)) { using (PixelAccessor sourcePixels = source.Lock()) using (PixelAccessor targetPixels = target.Lock()) diff --git a/tests/ImageSharp.Benchmarks/Image/DecodeBmp.cs b/tests/ImageSharp.Benchmarks/Image/DecodeBmp.cs index acde8e0db..d14f3c17e 100644 --- a/tests/ImageSharp.Benchmarks/Image/DecodeBmp.cs +++ b/tests/ImageSharp.Benchmarks/Image/DecodeBmp.cs @@ -10,7 +10,8 @@ namespace ImageSharp.Benchmarks.Image using BenchmarkDotNet.Attributes; - using CoreImage = ImageSharp.Image; + using ImageSharp.PixelFormats; + using CoreSize = ImageSharp.Size; public class DecodeBmp : BenchmarkBase @@ -43,7 +44,7 @@ namespace ImageSharp.Benchmarks.Image { using (MemoryStream memoryStream = new MemoryStream(this.bmpBytes)) { - using (CoreImage image = CoreImage.Load(memoryStream)) + using (Image image = Image.Load(memoryStream)) { return new CoreSize(image.Width, image.Height); } diff --git a/tests/ImageSharp.Benchmarks/Image/DecodeFilteredPng.cs b/tests/ImageSharp.Benchmarks/Image/DecodeFilteredPng.cs index 6786cfdc0..fd86324ca 100644 --- a/tests/ImageSharp.Benchmarks/Image/DecodeFilteredPng.cs +++ b/tests/ImageSharp.Benchmarks/Image/DecodeFilteredPng.cs @@ -10,6 +10,7 @@ namespace ImageSharp.Benchmarks.Image using BenchmarkDotNet.Attributes; using ImageSharp; + using ImageSharp.PixelFormats; public class DecodeFilteredPng : BenchmarkBase { @@ -31,7 +32,7 @@ namespace ImageSharp.Benchmarks.Image private Size LoadPng(MemoryStream stream) { - using (Image image = Image.Load(stream)) + using (Image image = Image.Load(stream)) { return new Size(image.Width, image.Height); } diff --git a/tests/ImageSharp.Benchmarks/Image/DecodeGif.cs b/tests/ImageSharp.Benchmarks/Image/DecodeGif.cs index a9bb4c7b3..94b04b904 100644 --- a/tests/ImageSharp.Benchmarks/Image/DecodeGif.cs +++ b/tests/ImageSharp.Benchmarks/Image/DecodeGif.cs @@ -10,7 +10,8 @@ namespace ImageSharp.Benchmarks.Image using BenchmarkDotNet.Attributes; - using CoreImage = ImageSharp.Image; + using ImageSharp.PixelFormats; + using CoreSize = ImageSharp.Size; public class DecodeGif : BenchmarkBase @@ -43,7 +44,7 @@ namespace ImageSharp.Benchmarks.Image { using (MemoryStream memoryStream = new MemoryStream(this.gifBytes)) { - using (CoreImage image = CoreImage.Load(memoryStream)) + using (Image image = Image.Load(memoryStream)) { return new CoreSize(image.Width, image.Height); } diff --git a/tests/ImageSharp.Benchmarks/Image/DecodeJpeg.cs b/tests/ImageSharp.Benchmarks/Image/DecodeJpeg.cs index 6ce230370..7aa98f985 100644 --- a/tests/ImageSharp.Benchmarks/Image/DecodeJpeg.cs +++ b/tests/ImageSharp.Benchmarks/Image/DecodeJpeg.cs @@ -10,7 +10,8 @@ namespace ImageSharp.Benchmarks.Image using BenchmarkDotNet.Attributes; - using CoreImage = ImageSharp.Image; + using ImageSharp.PixelFormats; + using CoreSize = ImageSharp.Size; public class DecodeJpeg : BenchmarkBase @@ -43,7 +44,7 @@ namespace ImageSharp.Benchmarks.Image { using (MemoryStream memoryStream = new MemoryStream(this.jpegBytes)) { - using (CoreImage image = CoreImage.Load(memoryStream)) + using (Image image = Image.Load(memoryStream)) { return new CoreSize(image.Width, image.Height); } diff --git a/tests/ImageSharp.Benchmarks/Image/DecodeJpegMultiple.cs b/tests/ImageSharp.Benchmarks/Image/DecodeJpegMultiple.cs index 5c3c1e115..023740691 100644 --- a/tests/ImageSharp.Benchmarks/Image/DecodeJpegMultiple.cs +++ b/tests/ImageSharp.Benchmarks/Image/DecodeJpegMultiple.cs @@ -8,8 +8,7 @@ namespace ImageSharp.Benchmarks.Image using System.Collections.Generic; using BenchmarkDotNet.Attributes; - using Image = ImageSharp.Image; - using ImageSharpSize = ImageSharp.Size; + using ImageSharp.PixelFormats; [Config(typeof(Config.Short))] public class DecodeJpegMultiple : MultiImageBenchmarkBase @@ -25,8 +24,8 @@ namespace ImageSharp.Benchmarks.Image public void DecodeJpegImageSharp() { this.ForEachStream( - ms => ImageSharp.Image.Load(ms) - ); + ms => Image.Load(ms) + ); } [Benchmark(Baseline = true, Description = "DecodeJpegMultiple - System.Drawing")] diff --git a/tests/ImageSharp.Benchmarks/Image/DecodePng.cs b/tests/ImageSharp.Benchmarks/Image/DecodePng.cs index 620a48a3b..2010b90e1 100644 --- a/tests/ImageSharp.Benchmarks/Image/DecodePng.cs +++ b/tests/ImageSharp.Benchmarks/Image/DecodePng.cs @@ -10,7 +10,8 @@ namespace ImageSharp.Benchmarks.Image using BenchmarkDotNet.Attributes; - using CoreImage = ImageSharp.Image; + using ImageSharp.PixelFormats; + using CoreSize = ImageSharp.Size; public class DecodePng : BenchmarkBase @@ -43,7 +44,7 @@ namespace ImageSharp.Benchmarks.Image { using (MemoryStream memoryStream = new MemoryStream(this.pngBytes)) { - using (CoreImage image = CoreImage.Load(memoryStream)) + using (Image image = Image.Load(memoryStream)) { return new CoreSize(image.Width, image.Height); } diff --git a/tests/ImageSharp.Benchmarks/Image/EncodeBmp.cs b/tests/ImageSharp.Benchmarks/Image/EncodeBmp.cs index 6ed577338..a23fce9ea 100644 --- a/tests/ImageSharp.Benchmarks/Image/EncodeBmp.cs +++ b/tests/ImageSharp.Benchmarks/Image/EncodeBmp.cs @@ -10,14 +10,15 @@ namespace ImageSharp.Benchmarks.Image using System.IO; using BenchmarkDotNet.Attributes; - using CoreImage = ImageSharp.Image; + + using ImageSharp.PixelFormats; public class EncodeBmp : BenchmarkBase { // System.Drawing needs this. private Stream bmpStream; private Image bmpDrawing; - private CoreImage bmpCore; + private Image bmpCore; [Setup] public void ReadImages() @@ -25,7 +26,7 @@ namespace ImageSharp.Benchmarks.Image if (this.bmpStream == null) { this.bmpStream = File.OpenRead("../ImageSharp.Tests/TestImages/Formats/Bmp/Car.bmp"); - this.bmpCore = CoreImage.Load(this.bmpStream); + this.bmpCore = Image.Load(this.bmpStream); this.bmpStream.Position = 0; this.bmpDrawing = Image.FromStream(this.bmpStream); } diff --git a/tests/ImageSharp.Benchmarks/Image/EncodeGif.cs b/tests/ImageSharp.Benchmarks/Image/EncodeGif.cs index fabeba1bd..da22b156c 100644 --- a/tests/ImageSharp.Benchmarks/Image/EncodeGif.cs +++ b/tests/ImageSharp.Benchmarks/Image/EncodeGif.cs @@ -10,14 +10,15 @@ namespace ImageSharp.Benchmarks.Image using System.IO; using BenchmarkDotNet.Attributes; - using CoreImage = ImageSharp.Image; + + using ImageSharp.PixelFormats; public class EncodeGif : BenchmarkBase { // System.Drawing needs this. private Stream bmpStream; private Image bmpDrawing; - private CoreImage bmpCore; + private Image bmpCore; [Setup] public void ReadImages() @@ -25,7 +26,7 @@ namespace ImageSharp.Benchmarks.Image if (this.bmpStream == null) { this.bmpStream = File.OpenRead("../ImageSharp.Tests/TestImages/Formats/Bmp/Car.bmp"); - this.bmpCore = CoreImage.Load(this.bmpStream); + this.bmpCore = Image.Load(this.bmpStream); this.bmpStream.Position = 0; this.bmpDrawing = Image.FromStream(this.bmpStream); } diff --git a/tests/ImageSharp.Benchmarks/Image/EncodeIndexedPng.cs b/tests/ImageSharp.Benchmarks/Image/EncodeIndexedPng.cs index b27ad5fcc..5f3b0e860 100644 --- a/tests/ImageSharp.Benchmarks/Image/EncodeIndexedPng.cs +++ b/tests/ImageSharp.Benchmarks/Image/EncodeIndexedPng.cs @@ -21,7 +21,7 @@ namespace ImageSharp.Benchmarks.Image { // System.Drawing needs this. private Stream bmpStream; - private Image bmpCore; + private Image bmpCore; [Params(false)] public bool LargeImage { get; set; } @@ -35,7 +35,7 @@ namespace ImageSharp.Benchmarks.Image ? "../ImageSharp.Tests/TestImages/Formats/Jpg/baseline/jpeg420exif.jpg" : "../ImageSharp.Tests/TestImages/Formats/Bmp/Car.bmp"; this.bmpStream = File.OpenRead(path); - this.bmpCore = Image.Load(this.bmpStream); + this.bmpCore = Image.Load(this.bmpStream); this.bmpStream.Position = 0; } } diff --git a/tests/ImageSharp.Benchmarks/Image/EncodeJpeg.cs b/tests/ImageSharp.Benchmarks/Image/EncodeJpeg.cs index 7649812ec..7c1fcf662 100644 --- a/tests/ImageSharp.Benchmarks/Image/EncodeJpeg.cs +++ b/tests/ImageSharp.Benchmarks/Image/EncodeJpeg.cs @@ -10,14 +10,15 @@ namespace ImageSharp.Benchmarks.Image using System.IO; using BenchmarkDotNet.Attributes; - using CoreImage = ImageSharp.Image; + + using ImageSharp.PixelFormats; public class EncodeJpeg : BenchmarkBase { // System.Drawing needs this. private Stream bmpStream; private Image bmpDrawing; - private CoreImage bmpCore; + private Image bmpCore; [Setup] public void ReadImages() @@ -25,7 +26,7 @@ namespace ImageSharp.Benchmarks.Image if (this.bmpStream == null) { this.bmpStream = File.OpenRead("../ImageSharp.Tests/TestImages/Formats/Bmp/Car.bmp"); - this.bmpCore = CoreImage.Load(this.bmpStream); + this.bmpCore = Image.Load(this.bmpStream); this.bmpStream.Position = 0; this.bmpDrawing = Image.FromStream(this.bmpStream); } diff --git a/tests/ImageSharp.Benchmarks/Image/EncodePng.cs b/tests/ImageSharp.Benchmarks/Image/EncodePng.cs index 6158e5aac..ac50916bf 100644 --- a/tests/ImageSharp.Benchmarks/Image/EncodePng.cs +++ b/tests/ImageSharp.Benchmarks/Image/EncodePng.cs @@ -15,14 +15,12 @@ namespace ImageSharp.Benchmarks.Image using ImageSharp.PixelFormats; using ImageSharp.Quantizers; - using CoreImage = ImageSharp.Image; - public class EncodePng : BenchmarkBase { // System.Drawing needs this. private Stream bmpStream; private Image bmpDrawing; - private CoreImage bmpCore; + private Image bmpCore; [Params(false)] public bool LargeImage { get; set; } @@ -39,7 +37,7 @@ namespace ImageSharp.Benchmarks.Image ? "../ImageSharp.Tests/TestImages/Formats/Jpg/baseline/jpeg420exif.jpg" : "../ImageSharp.Tests/TestImages/Formats/Bmp/Car.bmp"; this.bmpStream = File.OpenRead(path); - this.bmpCore = CoreImage.Load(this.bmpStream); + this.bmpCore = Image.Load(this.bmpStream); this.bmpStream.Position = 0; this.bmpDrawing = Image.FromStream(this.bmpStream); } diff --git a/tests/ImageSharp.Benchmarks/Image/GetSetPixel.cs b/tests/ImageSharp.Benchmarks/Image/GetSetPixel.cs index 21927c915..28616213b 100644 --- a/tests/ImageSharp.Benchmarks/Image/GetSetPixel.cs +++ b/tests/ImageSharp.Benchmarks/Image/GetSetPixel.cs @@ -11,7 +11,6 @@ namespace ImageSharp.Benchmarks.Image using ImageSharp.PixelFormats; - using CoreImage = ImageSharp.Image; using SystemColor = System.Drawing.Color; public class GetSetPixel : BenchmarkBase @@ -29,7 +28,7 @@ namespace ImageSharp.Benchmarks.Image [Benchmark(Description = "ImageSharp GetSet pixel")] public Rgba32 ResizeCore() { - using (CoreImage image = new CoreImage(400, 400)) + using (Image image = new Image(400, 400)) { using (PixelAccessor imagePixels = image.Lock()) { diff --git a/tests/ImageSharp.Benchmarks/Image/MultiImageBenchmarkBase.cs b/tests/ImageSharp.Benchmarks/Image/MultiImageBenchmarkBase.cs index a084ca025..8cb73d6af 100644 --- a/tests/ImageSharp.Benchmarks/Image/MultiImageBenchmarkBase.cs +++ b/tests/ImageSharp.Benchmarks/Image/MultiImageBenchmarkBase.cs @@ -15,13 +15,13 @@ namespace ImageSharp.Benchmarks.Image using BenchmarkDotNet.Attributes; - using Image = ImageSharp.Image; + using ImageSharp.PixelFormats; public abstract class MultiImageBenchmarkBase : BenchmarkBase { protected Dictionary FileNamesToBytes = new Dictionary(); - protected Dictionary FileNamesToImageSharpImages = new Dictionary(); + protected Dictionary> FileNamesToImageSharpImages = new Dictionary>(); protected Dictionary FileNamesToSystemDrawingImages = new Dictionary(); /// @@ -154,7 +154,7 @@ namespace ImageSharp.Benchmarks.Image using (MemoryStream ms1 = new MemoryStream(bytes)) { - this.FileNamesToImageSharpImages[fn] = Image.Load(ms1); + this.FileNamesToImageSharpImages[fn] = Image.Load(ms1); } @@ -162,7 +162,7 @@ namespace ImageSharp.Benchmarks.Image } } - protected IEnumerable> FileNames2ImageSharpImages + protected IEnumerable>> FileNames2ImageSharpImages => this.EnumeratePairsByBenchmarkSettings( this.FileNamesToImageSharpImages, @@ -176,9 +176,9 @@ namespace ImageSharp.Benchmarks.Image protected virtual int LargeImageThresholdInPixels => 700000; - protected void ForEachImageSharpImage(Func operation) + protected void ForEachImageSharpImage(Func, object> operation) { - foreach (KeyValuePair kv in this.FileNames2ImageSharpImages) + foreach (KeyValuePair> kv in this.FileNames2ImageSharpImages) { try { @@ -194,7 +194,7 @@ namespace ImageSharp.Benchmarks.Image } } - protected void ForEachImageSharpImage(Func operation) + protected void ForEachImageSharpImage(Func, MemoryStream, object> operation) { using (MemoryStream workStream = new MemoryStream()) { diff --git a/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs b/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs index a616733b5..474788b35 100644 --- a/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs +++ b/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs @@ -8,9 +8,6 @@ namespace ImageSharp.Benchmarks using BenchmarkDotNet.Attributes; using ImageSharp.PixelFormats; - using ImageSharp.Drawing; - using ImageSharp.Processing.Processors; - using CoreImage = ImageSharp.Image; using CoreSize = ImageSharp.Size; using System.Numerics; using ImageSharp.PixelFormats.PixelBlenders; @@ -57,7 +54,7 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp BulkVectorConvert")] public CoreSize BulkVectorConvert() { - using (CoreImage image = new CoreImage(800, 800)) + using (Image image = new Image(800, 800)) { Buffer amounts = new Buffer(image.Width); @@ -80,7 +77,7 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp BulkPixelConvert")] public CoreSize BulkPixelConvert() { - using (CoreImage image = new CoreImage(800, 800)) + using (Image image = new Image(800, 800)) { Buffer amounts = new Buffer(image.Width); diff --git a/tests/ImageSharp.Benchmarks/Samplers/Crop.cs b/tests/ImageSharp.Benchmarks/Samplers/Crop.cs index a3cdef92e..9fa979463 100644 --- a/tests/ImageSharp.Benchmarks/Samplers/Crop.cs +++ b/tests/ImageSharp.Benchmarks/Samplers/Crop.cs @@ -10,7 +10,8 @@ namespace ImageSharp.Benchmarks using BenchmarkDotNet.Attributes; - using CoreImage = ImageSharp.Image; + using ImageSharp.PixelFormats; + using CoreSize = ImageSharp.Size; public class Crop : BenchmarkBase @@ -38,7 +39,7 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp Crop")] public CoreSize CropResizeCore() { - using (CoreImage image = new CoreImage(800, 800)) + using (Image image = new Image(800, 800)) { image.Crop(100, 100); return new CoreSize(image.Width, image.Height); diff --git a/tests/ImageSharp.Benchmarks/Samplers/DetectEdges.cs b/tests/ImageSharp.Benchmarks/Samplers/DetectEdges.cs index 28661b9d6..36d985324 100644 --- a/tests/ImageSharp.Benchmarks/Samplers/DetectEdges.cs +++ b/tests/ImageSharp.Benchmarks/Samplers/DetectEdges.cs @@ -9,12 +9,13 @@ namespace ImageSharp.Benchmarks using BenchmarkDotNet.Attributes; - using CoreImage = ImageSharp.Image; + using ImageSharp.PixelFormats; + using Processing; public class DetectEdges : BenchmarkBase { - private CoreImage image; + private Image image; [Setup] public void ReadImage() @@ -23,7 +24,7 @@ namespace ImageSharp.Benchmarks { using (FileStream stream = File.OpenRead("../ImageSharp.Tests/TestImages/Formats/Bmp/Car.bmp")) { - this.image = CoreImage.Load(stream); + this.image = Image.Load(stream); } } } diff --git a/tests/ImageSharp.Benchmarks/Samplers/Glow.cs b/tests/ImageSharp.Benchmarks/Samplers/Glow.cs index 6daf120fa..76a0bc23b 100644 --- a/tests/ImageSharp.Benchmarks/Samplers/Glow.cs +++ b/tests/ImageSharp.Benchmarks/Samplers/Glow.cs @@ -8,9 +8,7 @@ namespace ImageSharp.Benchmarks using BenchmarkDotNet.Attributes; using ImageSharp.PixelFormats; - using ImageSharp.Drawing; using ImageSharp.Processing.Processors; - using CoreImage = ImageSharp.Image; using CoreSize = ImageSharp.Size; using ImageSharp.Processing; using System.Numerics; @@ -32,7 +30,7 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp Glow - Bulk")] public CoreSize GlowBulk() { - using (CoreImage image = new CoreImage(800, 800)) + using (Image image = new Image(800, 800)) { image.ApplyProcessor(bulk, image.Bounds); return new CoreSize(image.Width, image.Height); @@ -42,7 +40,7 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp Glow - Parallel")] public CoreSize GLowSimple() { - using (CoreImage image = new CoreImage(800, 800)) + using (Image image = new Image(800, 800)) { image.ApplyProcessor(parallel, image.Bounds); return new CoreSize(image.Width, image.Height); diff --git a/tests/ImageSharp.Benchmarks/Samplers/Resize.cs b/tests/ImageSharp.Benchmarks/Samplers/Resize.cs index 932c229bd..60c0d31d2 100644 --- a/tests/ImageSharp.Benchmarks/Samplers/Resize.cs +++ b/tests/ImageSharp.Benchmarks/Samplers/Resize.cs @@ -13,8 +13,6 @@ namespace ImageSharp.Benchmarks using ImageSharp.PixelFormats; using CoreSize = ImageSharp.Size; - using CoreImage = ImageSharp.Image; - using CoreImageVector = ImageSharp.Image; public class Resize : BenchmarkBase { @@ -41,7 +39,7 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp Resize")] public CoreSize ResizeCore() { - using (CoreImage image = new CoreImage(2000, 2000)) + using (Image image = new Image(2000, 2000)) { image.Resize(400, 400); return new CoreSize(image.Width, image.Height); @@ -51,7 +49,7 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp Vector Resize")] public CoreSize ResizeCoreVector() { - using (CoreImageVector image = new CoreImageVector(2000, 2000)) + using (Image image = new Image(2000, 2000)) { image.Resize(400, 400); return new CoreSize(image.Width, image.Height); @@ -61,7 +59,7 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp Compand Resize")] public CoreSize ResizeCoreCompand() { - using (CoreImage image = new CoreImage(2000, 2000)) + using (Image image = new Image(2000, 2000)) { image.Resize(400, 400, true); return new CoreSize(image.Width, image.Height); @@ -71,7 +69,7 @@ namespace ImageSharp.Benchmarks [Benchmark(Description = "ImageSharp Vector Compand Resize")] public CoreSize ResizeCoreVectorCompand() { - using (CoreImageVector image = new CoreImageVector(2000, 2000)) + using (Image image = new Image(2000, 2000)) { image.Resize(400, 400, true); return new CoreSize(image.Width, image.Height); diff --git a/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj b/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj index 23a5c59a3..53cdffaea 100644 --- a/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj +++ b/tests/ImageSharp.Sandbox46/ImageSharp.Sandbox46.csproj @@ -29,4 +29,7 @@ + + + \ No newline at end of file diff --git a/tests/ImageSharp.Tests/ConfigurationTests.cs b/tests/ImageSharp.Tests/ConfigurationTests.cs index c749239d7..3c0b7e702 100644 --- a/tests/ImageSharp.Tests/ConfigurationTests.cs +++ b/tests/ImageSharp.Tests/ConfigurationTests.cs @@ -11,6 +11,8 @@ namespace ImageSharp.Tests using ImageSharp.Formats; using ImageSharp.IO; + using ImageSharp.PixelFormats; + using Xunit; /// @@ -236,7 +238,7 @@ namespace ImageSharp.Tests { Configuration.Default.AddImageFormat(new PngFormat()); - Image image = new Image(1, 1); + Image image = new Image(1, 1); Assert.Equal(image.Configuration.ParallelOptions, Configuration.Default.ParallelOptions); Assert.Equal(image.Configuration.ImageFormats, Configuration.Default.ImageFormats); } @@ -249,8 +251,8 @@ namespace ImageSharp.Tests { Configuration.Default.AddImageFormat(new PngFormat()); - Image image = new Image(1, 1); - Image image2 = new Image(image); + Image image = new Image(1, 1); + Image image2 = new Image(image); Assert.Equal(image2.Configuration.ParallelOptions, image.Configuration.ParallelOptions); Assert.True(image2.Configuration.ImageFormats.SequenceEqual(image.Configuration.ImageFormats)); } diff --git a/tests/ImageSharp.Tests/Drawing/BeziersTests.cs b/tests/ImageSharp.Tests/Drawing/BeziersTests.cs index eb3a5de86..ff6892199 100644 --- a/tests/ImageSharp.Tests/Drawing/BeziersTests.cs +++ b/tests/ImageSharp.Tests/Drawing/BeziersTests.cs @@ -22,7 +22,7 @@ namespace ImageSharp.Tests.Drawing public void ImageShouldBeOverlayedByBezierLine() { string path = this.CreateOutputDirectory("Drawing", "BezierLine"); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Simple.png")) { @@ -64,7 +64,7 @@ namespace ImageSharp.Tests.Drawing Rgba32 color = new Rgba32(Rgba32.HotPink.R, Rgba32.HotPink.G, Rgba32.HotPink.B, 150); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Opacity.png")) { diff --git a/tests/ImageSharp.Tests/Drawing/DrawImageTest.cs b/tests/ImageSharp.Tests/Drawing/DrawImageTest.cs index 030034a8f..2e5346fe6 100644 --- a/tests/ImageSharp.Tests/Drawing/DrawImageTest.cs +++ b/tests/ImageSharp.Tests/Drawing/DrawImageTest.cs @@ -37,7 +37,7 @@ namespace ImageSharp.Tests where TPixel : struct, IPixel { using (Image image = provider.GetImage()) - using (Image blend = Image.Load(TestFile.Create(TestImages.Bmp.Car).Bytes)) + using (Image blend = Image.Load(TestFile.Create(TestImages.Bmp.Car).Bytes)) { image.DrawImage(blend, mode, .75f, new Size(image.Width / 2, image.Height / 2), new Point(image.Width / 4, image.Height / 4)) .DebugSave(provider, new { mode }); diff --git a/tests/ImageSharp.Tests/Drawing/DrawPathTests.cs b/tests/ImageSharp.Tests/Drawing/DrawPathTests.cs index 674823d3a..7d54879c3 100644 --- a/tests/ImageSharp.Tests/Drawing/DrawPathTests.cs +++ b/tests/ImageSharp.Tests/Drawing/DrawPathTests.cs @@ -20,7 +20,7 @@ namespace ImageSharp.Tests.Drawing public void ImageShouldBeOverlayedByPath() { string path = this.CreateOutputDirectory("Drawing", "Path"); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { LinearLineSegment linerSegemnt = new LinearLineSegment( new Vector2(10, 10), @@ -74,7 +74,7 @@ namespace ImageSharp.Tests.Drawing ShapePath p = new ShapePath(linerSegemnt, bazierSegment); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Opacity.png")) { diff --git a/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs b/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs index 493bab347..1cd41b7b5 100644 --- a/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs +++ b/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs @@ -18,7 +18,7 @@ namespace ImageSharp.Tests.Drawing private void Test(string name, Rgba32 background, IBrush brush, Rgba32[,] expectedPattern) { string path = this.CreateOutputDirectory("Fill", "PatternBrush"); - using (Image image = new Image(20, 20)) + using (Image image = new Image(20, 20)) { image .Fill(background) @@ -63,7 +63,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithPercent10() { - this.Test("Percent10", Rgba32.Blue, Brushes.Percent10(Rgba32.HotPink, Rgba32.LimeGreen), + this.Test("Percent10", Rgba32.Blue, Brushes.Percent10(Rgba32.HotPink, Rgba32.LimeGreen), new[,] { { Rgba32.HotPink , Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen}, @@ -76,7 +76,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithPercent10Transparent() { - Test("Percent10_Transparent", Rgba32.Blue, Brushes.Percent10(Rgba32.HotPink), + Test("Percent10_Transparent", Rgba32.Blue, Brushes.Percent10(Rgba32.HotPink), new Rgba32[,] { { Rgba32.HotPink , Rgba32.Blue, Rgba32.Blue, Rgba32.Blue}, { Rgba32.Blue, Rgba32.Blue, Rgba32.Blue, Rgba32.Blue}, @@ -88,7 +88,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithPercent20() { - Test("Percent20", Rgba32.Blue, Brushes.Percent20(Rgba32.HotPink, Rgba32.LimeGreen), + Test("Percent20", Rgba32.Blue, Brushes.Percent20(Rgba32.HotPink, Rgba32.LimeGreen), new Rgba32[,] { { Rgba32.HotPink , Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen}, { Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.HotPink , Rgba32.LimeGreen}, @@ -100,7 +100,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithPercent20_transparent() { - Test("Percent20_Transparent", Rgba32.Blue, Brushes.Percent20(Rgba32.HotPink), + Test("Percent20_Transparent", Rgba32.Blue, Brushes.Percent20(Rgba32.HotPink), new Rgba32[,] { { Rgba32.HotPink , Rgba32.Blue, Rgba32.Blue, Rgba32.Blue}, { Rgba32.Blue, Rgba32.Blue, Rgba32.HotPink , Rgba32.Blue}, @@ -112,7 +112,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithHorizontal() { - Test("Horizontal", Rgba32.Blue, Brushes.Horizontal(Rgba32.HotPink, Rgba32.LimeGreen), + Test("Horizontal", Rgba32.Blue, Brushes.Horizontal(Rgba32.HotPink, Rgba32.LimeGreen), new Rgba32[,] { { Rgba32.LimeGreen , Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen}, { Rgba32.HotPink, Rgba32.HotPink, Rgba32.HotPink , Rgba32.HotPink}, @@ -124,7 +124,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithHorizontal_transparent() { - Test("Horizontal_Transparent", Rgba32.Blue, Brushes.Horizontal(Rgba32.HotPink), + Test("Horizontal_Transparent", Rgba32.Blue, Brushes.Horizontal(Rgba32.HotPink), new Rgba32[,] { { Rgba32.Blue , Rgba32.Blue, Rgba32.Blue, Rgba32.Blue}, { Rgba32.HotPink, Rgba32.HotPink, Rgba32.HotPink , Rgba32.HotPink}, @@ -138,7 +138,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithMin() { - Test("Min", Rgba32.Blue, Brushes.Min(Rgba32.HotPink, Rgba32.LimeGreen), + Test("Min", Rgba32.Blue, Brushes.Min(Rgba32.HotPink, Rgba32.LimeGreen), new Rgba32[,] { { Rgba32.LimeGreen , Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen}, { Rgba32.LimeGreen , Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen}, @@ -150,7 +150,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithMin_transparent() { - Test("Min_Transparent", Rgba32.Blue, Brushes.Min(Rgba32.HotPink), + Test("Min_Transparent", Rgba32.Blue, Brushes.Min(Rgba32.HotPink), new Rgba32[,] { { Rgba32.Blue , Rgba32.Blue, Rgba32.Blue, Rgba32.Blue}, { Rgba32.Blue , Rgba32.Blue, Rgba32.Blue, Rgba32.Blue}, @@ -162,7 +162,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithVertical() { - Test("Vertical", Rgba32.Blue, Brushes.Vertical(Rgba32.HotPink, Rgba32.LimeGreen), + Test("Vertical", Rgba32.Blue, Brushes.Vertical(Rgba32.HotPink, Rgba32.LimeGreen), new Rgba32[,] { { Rgba32.LimeGreen, Rgba32.HotPink, Rgba32.LimeGreen, Rgba32.LimeGreen}, { Rgba32.LimeGreen, Rgba32.HotPink, Rgba32.LimeGreen, Rgba32.LimeGreen}, @@ -174,7 +174,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithVertical_transparent() { - Test("Vertical_Transparent", Rgba32.Blue, Brushes.Vertical(Rgba32.HotPink), + Test("Vertical_Transparent", Rgba32.Blue, Brushes.Vertical(Rgba32.HotPink), new Rgba32[,] { { Rgba32.Blue, Rgba32.HotPink, Rgba32.Blue, Rgba32.Blue}, { Rgba32.Blue, Rgba32.HotPink, Rgba32.Blue, Rgba32.Blue}, @@ -186,7 +186,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithForwardDiagonal() { - Test("ForwardDiagonal", Rgba32.Blue, Brushes.ForwardDiagonal(Rgba32.HotPink, Rgba32.LimeGreen), + Test("ForwardDiagonal", Rgba32.Blue, Brushes.ForwardDiagonal(Rgba32.HotPink, Rgba32.LimeGreen), new Rgba32[,] { { Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.HotPink}, { Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.HotPink, Rgba32.LimeGreen}, @@ -198,7 +198,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithForwardDiagonal_transparent() { - Test("ForwardDiagonal_Transparent", Rgba32.Blue, Brushes.ForwardDiagonal(Rgba32.HotPink), + Test("ForwardDiagonal_Transparent", Rgba32.Blue, Brushes.ForwardDiagonal(Rgba32.HotPink), new Rgba32[,] { { Rgba32.Blue, Rgba32.Blue, Rgba32.Blue, Rgba32.HotPink}, { Rgba32.Blue, Rgba32.Blue, Rgba32.HotPink, Rgba32.Blue}, @@ -210,7 +210,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithBackwardDiagonal() { - Test("BackwardDiagonal", Rgba32.Blue, Brushes.BackwardDiagonal(Rgba32.HotPink, Rgba32.LimeGreen), + Test("BackwardDiagonal", Rgba32.Blue, Brushes.BackwardDiagonal(Rgba32.HotPink, Rgba32.LimeGreen), new Rgba32[,] { { Rgba32.HotPink, Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen}, { Rgba32.LimeGreen, Rgba32.HotPink, Rgba32.LimeGreen, Rgba32.LimeGreen}, @@ -222,7 +222,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithBackwardDiagonal_transparent() { - Test("BackwardDiagonal_Transparent", Rgba32.Blue, Brushes.BackwardDiagonal(Rgba32.HotPink), + Test("BackwardDiagonal_Transparent", Rgba32.Blue, Brushes.BackwardDiagonal(Rgba32.HotPink), new Rgba32[,] { { Rgba32.HotPink, Rgba32.Blue, Rgba32.Blue, Rgba32.Blue}, { Rgba32.Blue, Rgba32.HotPink, Rgba32.Blue, Rgba32.Blue}, diff --git a/tests/ImageSharp.Tests/Drawing/FillRegionProcessorTests.cs b/tests/ImageSharp.Tests/Drawing/FillRegionProcessorTests.cs index 9661a41bb..c7b789da0 100644 --- a/tests/ImageSharp.Tests/Drawing/FillRegionProcessorTests.cs +++ b/tests/ImageSharp.Tests/Drawing/FillRegionProcessorTests.cs @@ -16,7 +16,7 @@ namespace ImageSharp.Tests.Drawing [InlineData(true, 2, 4)] [InlineData(true, 5, 5)] [InlineData(true, 8, 8)] - [InlineData(false, 8, 4)] + [InlineData(false, 8, 4)] [InlineData(false, 16, 4)] // we always do 4 sub=pixels when antialising is off. public void MinimumAntialiasSubpixelDepth(bool antialias, int antialiasSubpixelDepth, int expectedAntialiasSubpixelDepth) { @@ -30,7 +30,7 @@ namespace ImageSharp.Tests.Drawing AntialiasSubpixelDepth = 1 }; FillRegionProcessor processor = new FillRegionProcessor(brush.Object, region.Object, options); - Image img = new Image(1, 1); + Image img = new Image(1, 1); processor.Apply(img, bounds); region.Verify(x => x.Scan(It.IsAny(), It.IsAny(), It.IsAny(), It.IsAny()), Times.Exactly(4)); diff --git a/tests/ImageSharp.Tests/Drawing/FillSolidBrushTests.cs b/tests/ImageSharp.Tests/Drawing/FillSolidBrushTests.cs index 4a3c8e305..dc0b83615 100644 --- a/tests/ImageSharp.Tests/Drawing/FillSolidBrushTests.cs +++ b/tests/ImageSharp.Tests/Drawing/FillSolidBrushTests.cs @@ -22,7 +22,7 @@ namespace ImageSharp.Tests.Drawing public void ImageShouldBeFloodFilledWithColorOnDefaultBackground() { string path = this.CreateOutputDirectory("Fill", "SolidBrush"); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/DefaultBack.png")) { @@ -44,7 +44,7 @@ namespace ImageSharp.Tests.Drawing public void ImageShouldBeFloodFilledWithColor() { string path = this.CreateOutputDirectory("Fill", "SolidBrush"); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Simple.png")) { @@ -67,7 +67,7 @@ namespace ImageSharp.Tests.Drawing public void ImageShouldBeFloodFilledWithColorOpacity() { string path = this.CreateOutputDirectory("Fill", "SolidBrush"); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { Rgba32 color = new Rgba32(Rgba32.HotPink.R, Rgba32.HotPink.G, Rgba32.HotPink.B, 150); diff --git a/tests/ImageSharp.Tests/Drawing/LineComplexPolygonTests.cs b/tests/ImageSharp.Tests/Drawing/LineComplexPolygonTests.cs index bded40f32..f3d7d1a20 100644 --- a/tests/ImageSharp.Tests/Drawing/LineComplexPolygonTests.cs +++ b/tests/ImageSharp.Tests/Drawing/LineComplexPolygonTests.cs @@ -32,7 +32,7 @@ namespace ImageSharp.Tests.Drawing new Vector2(93, 85), new Vector2(65, 137))); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Simple.png")) { @@ -82,7 +82,7 @@ namespace ImageSharp.Tests.Drawing new Vector2(263, 25), new Vector2(235, 57))); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/SimpleVanishHole.png")) { @@ -133,7 +133,7 @@ namespace ImageSharp.Tests.Drawing new Vector2(130, 40), new Vector2(65, 137))); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/SimpleOverlapping.png")) { @@ -179,13 +179,13 @@ namespace ImageSharp.Tests.Drawing new Vector2(93, 85), new Vector2(65, 137))); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Dashed.png")) { image .BackgroundColor(Rgba32.Blue) - .Draw(Pens.Dash(Rgba32.HotPink, 5), simplePath.Clip(hole1)) + .Draw(Pens.Dash(Rgba32.HotPink, 5), simplePath.Clip(hole1)) .Save(output); } } @@ -207,7 +207,7 @@ namespace ImageSharp.Tests.Drawing new Vector2(65, 137))); Rgba32 color = new Rgba32(Rgba32.HotPink.R, Rgba32.HotPink.G, Rgba32.HotPink.B, 150); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Opacity.png")) { diff --git a/tests/ImageSharp.Tests/Drawing/LineTests.cs b/tests/ImageSharp.Tests/Drawing/LineTests.cs index 87bda30be..05b102dde 100644 --- a/tests/ImageSharp.Tests/Drawing/LineTests.cs +++ b/tests/ImageSharp.Tests/Drawing/LineTests.cs @@ -21,7 +21,7 @@ namespace ImageSharp.Tests.Drawing public void ImageShouldBeOverlayedByPath() { string path = this.CreateOutputDirectory("Drawing", "Lines"); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Simple.png")) { @@ -51,7 +51,7 @@ namespace ImageSharp.Tests.Drawing public void ImageShouldBeOverlayedByPath_NoAntialias() { string path = this.CreateOutputDirectory("Drawing", "Lines"); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Simple_noantialias.png")) { @@ -82,13 +82,13 @@ namespace ImageSharp.Tests.Drawing public void ImageShouldBeOverlayedByPathDashed() { string path = this.CreateOutputDirectory("Drawing", "Lines"); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Dashed.png")) { image .BackgroundColor(Rgba32.Blue) - .DrawLines(Pens.Dash(Rgba32.HotPink, 5), + .DrawLines(Pens.Dash(Rgba32.HotPink, 5), new[] { new Vector2(10, 10), new Vector2(200, 150), @@ -103,13 +103,13 @@ namespace ImageSharp.Tests.Drawing public void ImageShouldBeOverlayedByPathDotted() { string path = this.CreateOutputDirectory("Drawing", "Lines"); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Dot.png")) { image .BackgroundColor(Rgba32.Blue) - .DrawLines(Pens.Dot(Rgba32.HotPink, 5), + .DrawLines(Pens.Dot(Rgba32.HotPink, 5), new[] { new Vector2(10, 10), new Vector2(200, 150), @@ -124,13 +124,13 @@ namespace ImageSharp.Tests.Drawing public void ImageShouldBeOverlayedByPathDashDot() { string path = this.CreateOutputDirectory("Drawing", "Lines"); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/DashDot.png")) { image .BackgroundColor(Rgba32.Blue) - .DrawLines(Pens.DashDot(Rgba32.HotPink, 5), + .DrawLines(Pens.DashDot(Rgba32.HotPink, 5), new[] { new Vector2(10, 10), new Vector2(200, 150), @@ -145,13 +145,13 @@ namespace ImageSharp.Tests.Drawing public void ImageShouldBeOverlayedByPathDashDotDot() { string path = this.CreateOutputDirectory("Drawing", "Lines"); - Image image = new Image(500, 500); + Image image = new Image(500, 500); using (FileStream output = File.OpenWrite($"{path}/DashDotDot.png")) { image .BackgroundColor(Rgba32.Blue) - .DrawLines(Pens.DashDotDot(Rgba32.HotPink, 5), new[] { + .DrawLines(Pens.DashDotDot(Rgba32.HotPink, 5), new[] { new Vector2(10, 10), new Vector2(200, 150), new Vector2(50, 300) @@ -167,7 +167,7 @@ namespace ImageSharp.Tests.Drawing Rgba32 color = new Rgba32(Rgba32.HotPink.R, Rgba32.HotPink.G, Rgba32.HotPink.B, 150); - Image image = new Image(500, 500); + Image image = new Image(500, 500); using (FileStream output = File.OpenWrite($"{path}/Opacity.png")) @@ -200,7 +200,7 @@ namespace ImageSharp.Tests.Drawing { string path = this.CreateOutputDirectory("Drawing", "Lines"); - Image image = new Image(500, 500); + Image image = new Image(500, 500); using (FileStream output = File.OpenWrite($"{path}/Rectangle.png")) { diff --git a/tests/ImageSharp.Tests/Drawing/Paths/DrawBeziersTests.cs b/tests/ImageSharp.Tests/Drawing/Paths/DrawBeziersTests.cs index 008df9091..02ff92f63 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/DrawBeziersTests.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/DrawBeziersTests.cs @@ -18,8 +18,8 @@ namespace ImageSharp.Tests.Drawing.Paths float thickness = 7.2f; GraphicsOptions noneDefault = new GraphicsOptions(); Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); - Pen pen = new Pen(Rgba32.Firebrick, 99.9f); + SolidBrush brush = Brushes.Solid(Rgba32.HotPink); + Pen pen = new Pen(Rgba32.Firebrick, 99.9f); Vector2[] points = new Vector2[] { new Vector2(10,10), new Vector2(20,10), diff --git a/tests/ImageSharp.Tests/Drawing/Paths/DrawLinesTests.cs b/tests/ImageSharp.Tests/Drawing/Paths/DrawLinesTests.cs index 221cf7f29..4962e8d6f 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/DrawLinesTests.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/DrawLinesTests.cs @@ -17,8 +17,8 @@ namespace ImageSharp.Tests.Drawing.Paths float thickness = 7.2f; GraphicsOptions noneDefault = new GraphicsOptions(); Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); - Pen pen = new Pen(Rgba32.Gray, 99.9f); + SolidBrush brush = Brushes.Solid(Rgba32.HotPink); + Pen pen = new Pen(Rgba32.Gray, 99.9f); Vector2[] points = new Vector2[] { new Vector2(10,10), new Vector2(20,10), diff --git a/tests/ImageSharp.Tests/Drawing/Paths/DrawPath.cs b/tests/ImageSharp.Tests/Drawing/Paths/DrawPath.cs index 07be85b85..df9287bc7 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/DrawPath.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/DrawPath.cs @@ -18,8 +18,8 @@ namespace ImageSharp.Tests.Drawing.Paths float thickness = 7.2f; GraphicsOptions noneDefault = new GraphicsOptions(); Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); - Pen pen = new Pen(Rgba32.Gray, 99.9f); + SolidBrush brush = Brushes.Solid(Rgba32.HotPink); + Pen pen = new Pen(Rgba32.Gray, 99.9f); IPath path = new SixLabors.Shapes.Path(new LinearLineSegment(new Vector2[] { new Vector2(10,10), new Vector2(20,10), @@ -50,7 +50,7 @@ namespace ImageSharp.Tests.Drawing.Paths ShapePath shapepath = Assert.IsType(processor.Path); Assert.Equal(path, shapepath.Path); - + Pen pen = Assert.IsType>(processor.Pen); Assert.Equal(brush, pen.Brush); Assert.Equal(thickness, pen.Width); diff --git a/tests/ImageSharp.Tests/Drawing/Paths/DrawPolygon.cs b/tests/ImageSharp.Tests/Drawing/Paths/DrawPolygon.cs index bd90a460d..357604abf 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/DrawPolygon.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/DrawPolygon.cs @@ -18,8 +18,8 @@ namespace ImageSharp.Tests.Drawing.Paths float thickness = 7.2f; GraphicsOptions noneDefault = new GraphicsOptions(); Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); - Pen pen = new Pen(Rgba32.Gray, 99.9f); + SolidBrush brush = Brushes.Solid(Rgba32.HotPink); + Pen pen = new Pen(Rgba32.Gray, 99.9f); Vector2[] points = new Vector2[] { new Vector2(10,10), new Vector2(20,10), diff --git a/tests/ImageSharp.Tests/Drawing/Paths/DrawRectangle.cs b/tests/ImageSharp.Tests/Drawing/Paths/DrawRectangle.cs index 7ebc6b14f..c588cd705 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/DrawRectangle.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/DrawRectangle.cs @@ -15,8 +15,8 @@ namespace ImageSharp.Tests.Drawing.Paths float thickness = 7.2f; GraphicsOptions noneDefault = new GraphicsOptions(); Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); - Pen pen = new Pen(Rgba32.Gray, 99.9f); + SolidBrush brush = Brushes.Solid(Rgba32.HotPink); + Pen pen = new Pen(Rgba32.Gray, 99.9f); ImageSharp.Rectangle rectangle = new ImageSharp.Rectangle(10, 10, 98, 324); private ProcessorWatchingImage img; diff --git a/tests/ImageSharp.Tests/Drawing/Paths/FillPath.cs b/tests/ImageSharp.Tests/Drawing/Paths/FillPath.cs index a639a70cf..88ad3a91e 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/FillPath.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/FillPath.cs @@ -15,7 +15,7 @@ namespace ImageSharp.Tests.Drawing.Paths { GraphicsOptions noneDefault = new GraphicsOptions(); Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); + SolidBrush brush = Brushes.Solid(Rgba32.HotPink); IPath path = new SixLabors.Shapes.Path(new LinearLineSegment(new Vector2[] { new Vector2(10,10), new Vector2(20,10), @@ -45,7 +45,7 @@ namespace ImageSharp.Tests.Drawing.Paths Assert.Equal(GraphicsOptions.Default, processor.Options); ShapeRegion region = Assert.IsType(processor.Region); - + // path is converted to a polygon before filling Polygon polygon = Assert.IsType(region.Shape); LinearLineSegment segments = Assert.IsType(polygon.LineSegments[0]); diff --git a/tests/ImageSharp.Tests/Drawing/Paths/FillPolygon.cs b/tests/ImageSharp.Tests/Drawing/Paths/FillPolygon.cs index 2935c43a0..5ea1b976b 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/FillPolygon.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/FillPolygon.cs @@ -15,7 +15,7 @@ namespace ImageSharp.Tests.Drawing.Paths { GraphicsOptions noneDefault = new GraphicsOptions(); Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); + SolidBrush brush = Brushes.Solid(Rgba32.HotPink); Vector2[] path = new Vector2[] { new Vector2(10,10), new Vector2(20,10), @@ -47,7 +47,7 @@ namespace ImageSharp.Tests.Drawing.Paths ShapeRegion region = Assert.IsType(processor.Region); Polygon polygon = Assert.IsType(region.Shape); LinearLineSegment segemnt = Assert.IsType(polygon.LineSegments[0]); - + Assert.Equal(brush, processor.Brush); } @@ -72,7 +72,7 @@ namespace ImageSharp.Tests.Drawing.Paths public void CorrectlySetsColorAndPath() { img.FillPolygon(color, path); - + Assert.NotEmpty(img.ProcessorApplications); FillRegionProcessor processor = Assert.IsType>(img.ProcessorApplications[0].processor); diff --git a/tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.cs b/tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.cs index 4657db988..6f8388504 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.cs @@ -14,7 +14,7 @@ namespace ImageSharp.Tests.Drawing.Paths { GraphicsOptions noneDefault = new GraphicsOptions(); Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); + SolidBrush brush = Brushes.Solid(Rgba32.HotPink); ImageSharp.Rectangle rectangle = new ImageSharp.Rectangle(10, 10, 77, 76); private ProcessorWatchingImage img; @@ -45,7 +45,7 @@ namespace ImageSharp.Tests.Drawing.Paths Assert.Equal(rect.Location.Y, rectangle.Y); Assert.Equal(rect.Size.Width, rectangle.Width); Assert.Equal(rect.Size.Height, rectangle.Height); - + Assert.Equal(brush, processor.Brush); } @@ -73,7 +73,7 @@ namespace ImageSharp.Tests.Drawing.Paths public void CorrectlySetsColorAndRectangle() { img.Fill(color, rectangle); - + Assert.NotEmpty(img.ProcessorApplications); FillRegionProcessor processor = Assert.IsType>(img.ProcessorApplications[0].processor); diff --git a/tests/ImageSharp.Tests/Drawing/PolygonTests.cs b/tests/ImageSharp.Tests/Drawing/PolygonTests.cs index 554c5a32e..9bc918d37 100644 --- a/tests/ImageSharp.Tests/Drawing/PolygonTests.cs +++ b/tests/ImageSharp.Tests/Drawing/PolygonTests.cs @@ -22,7 +22,7 @@ namespace ImageSharp.Tests.Drawing { string path = this.CreateOutputDirectory("Drawing", "Polygons"); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Simple.png")) { @@ -62,7 +62,7 @@ namespace ImageSharp.Tests.Drawing Rgba32 color = new Rgba32(Rgba32.HotPink.R, Rgba32.HotPink.G, Rgba32.HotPink.B, 150); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Opacity.png")) { @@ -93,7 +93,7 @@ namespace ImageSharp.Tests.Drawing { string path = this.CreateOutputDirectory("Drawing", "Polygons"); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Rectangle.png")) { diff --git a/tests/ImageSharp.Tests/Drawing/RecolorImageTest.cs b/tests/ImageSharp.Tests/Drawing/RecolorImageTest.cs index d3236ae00..83419caaf 100644 --- a/tests/ImageSharp.Tests/Drawing/RecolorImageTest.cs +++ b/tests/ImageSharp.Tests/Drawing/RecolorImageTest.cs @@ -20,11 +20,11 @@ namespace ImageSharp.Tests { string path = this.CreateOutputDirectory("Drawing", "RecolorImage"); - RecolorBrush brush = new RecolorBrush(Rgba32.Yellow, Rgba32.HotPink, 0.2f); + RecolorBrush brush = new RecolorBrush(Rgba32.Yellow, Rgba32.HotPink, 0.2f); foreach (TestFile file in Files) { - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) { using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) { @@ -40,11 +40,11 @@ namespace ImageSharp.Tests { string path = this.CreateOutputDirectory("Drawing", "RecolorImage"); - RecolorBrush brush = new RecolorBrush(Rgba32.Yellow, Rgba32.HotPink, 0.2f); + RecolorBrush brush = new RecolorBrush(Rgba32.Yellow, Rgba32.HotPink, 0.2f); foreach (TestFile file in Files) { - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) { using (FileStream output = File.OpenWrite($"{path}/Shaped_{file.FileName}")) { diff --git a/tests/ImageSharp.Tests/Drawing/SolidBezierTests.cs b/tests/ImageSharp.Tests/Drawing/SolidBezierTests.cs index 0886aa15a..6cab7778e 100644 --- a/tests/ImageSharp.Tests/Drawing/SolidBezierTests.cs +++ b/tests/ImageSharp.Tests/Drawing/SolidBezierTests.cs @@ -26,7 +26,7 @@ namespace ImageSharp.Tests.Drawing new Vector2(240, 30), new Vector2(300, 400) }; - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Simple.png")) { @@ -61,7 +61,7 @@ namespace ImageSharp.Tests.Drawing }; Rgba32 color = new Rgba32(Rgba32.HotPink.R, Rgba32.HotPink.G, Rgba32.HotPink.B, 150); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Opacity.png")) { diff --git a/tests/ImageSharp.Tests/Drawing/SolidComplexPolygonTests.cs b/tests/ImageSharp.Tests/Drawing/SolidComplexPolygonTests.cs index 1de7e2144..5e0244d02 100644 --- a/tests/ImageSharp.Tests/Drawing/SolidComplexPolygonTests.cs +++ b/tests/ImageSharp.Tests/Drawing/SolidComplexPolygonTests.cs @@ -32,7 +32,7 @@ namespace ImageSharp.Tests.Drawing new Vector2(65, 137))); IPath clipped = simplePath.Clip(hole1); // var clipped = new Rectangle(10, 10, 100, 100).Clip(new Rectangle(20, 0, 20, 20)); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Simple.png")) { @@ -67,7 +67,7 @@ namespace ImageSharp.Tests.Drawing new Vector2(130, 40), new Vector2(65, 137))); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/SimpleOverlapping.png")) { @@ -102,7 +102,7 @@ namespace ImageSharp.Tests.Drawing new Vector2(65, 137))); Rgba32 color = new Rgba32(Rgba32.HotPink.R, Rgba32.HotPink.G, Rgba32.HotPink.B, 150); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Opacity.png")) { diff --git a/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs b/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs index d76dcf023..019c4a2dd 100644 --- a/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs +++ b/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs @@ -29,7 +29,7 @@ namespace ImageSharp.Tests.Drawing new Vector2(50, 300) }; - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Simple.png")) { @@ -55,12 +55,12 @@ namespace ImageSharp.Tests.Drawing new Vector2(50, 300) }; - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Pattern.png")) { image - .FillPolygon(Brushes.Horizontal(Rgba32.HotPink), simplePath, new GraphicsOptions(true)) + .FillPolygon(Brushes.Horizontal(Rgba32.HotPink), simplePath, new GraphicsOptions(true)) .Save(output); } @@ -81,7 +81,7 @@ namespace ImageSharp.Tests.Drawing new Vector2(50, 300) }; - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) using (FileStream output = File.OpenWrite($"{path}/Simple_NoAntialias.png")) { image @@ -112,11 +112,11 @@ namespace ImageSharp.Tests.Drawing new Vector2(50, 300) }; - using (Image brushImage = TestFile.Create(TestImages.Bmp.Car).CreateImage()) - using (Image image = new Image(500, 500)) + using (Image brushImage = TestFile.Create(TestImages.Bmp.Car).CreateImage()) + using (Image image = new Image(500, 500)) using (FileStream output = File.OpenWrite($"{path}/Image.png")) { - ImageBrush brush = new ImageBrush(brushImage); + ImageBrush brush = new ImageBrush(brushImage); image .BackgroundColor(Rgba32.Blue) @@ -136,7 +136,7 @@ namespace ImageSharp.Tests.Drawing }; Rgba32 color = new Rgba32(Rgba32.HotPink.R, Rgba32.HotPink.G, Rgba32.HotPink.B, 150); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Opacity.png")) { @@ -161,7 +161,7 @@ namespace ImageSharp.Tests.Drawing { string path = this.CreateOutputDirectory("Drawing", "FilledPolygons"); - using (Image image = new Image(500, 500)) + using (Image image = new Image(500, 500)) { using (FileStream output = File.OpenWrite($"{path}/Rectangle.png")) { @@ -191,7 +191,7 @@ namespace ImageSharp.Tests.Drawing { string path = this.CreateOutputDirectory("Drawing", "FilledPolygons"); - using (Image image = new Image(100, 100)) + using (Image image = new Image(100, 100)) { using (FileStream output = File.OpenWrite($"{path}/Triangle.png")) { @@ -217,7 +217,7 @@ namespace ImageSharp.Tests.Drawing Configuration config = Configuration.CreateDefaultInstance(); config.ParallelOptions.MaxDegreeOfParallelism = 1; - using (Image image = new Image(100, 100, config)) + using (Image image = new Image(100, 100, config)) { using (FileStream output = File.OpenWrite($"{path}/Septagon.png")) { @@ -236,7 +236,7 @@ namespace ImageSharp.Tests.Drawing Configuration config = Configuration.CreateDefaultInstance(); config.ParallelOptions.MaxDegreeOfParallelism = 1; - using (Image image = new Image(100, 100, config)) + using (Image image = new Image(100, 100, config)) { using (FileStream output = File.OpenWrite($"{path}/ellipse.png")) { @@ -256,7 +256,7 @@ namespace ImageSharp.Tests.Drawing Configuration config = Configuration.CreateDefaultInstance(); config.ParallelOptions.MaxDegreeOfParallelism = 1; - using (Image image = new Image(200, 200, config)) + using (Image image = new Image(200, 200, config)) { using (FileStream output = File.OpenWrite($"{path}/clipped-corner.png")) { diff --git a/tests/ImageSharp.Tests/Drawing/Text/DrawText.cs b/tests/ImageSharp.Tests/Drawing/Text/DrawText.cs index bce493a69..3b3e894f4 100644 --- a/tests/ImageSharp.Tests/Drawing/Text/DrawText.cs +++ b/tests/ImageSharp.Tests/Drawing/Text/DrawText.cs @@ -24,7 +24,7 @@ namespace ImageSharp.Tests.Drawing.Text { Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); + SolidBrush brush = Brushes.Solid(Rgba32.HotPink); IPath path = new SixLabors.Shapes.Path( new LinearLineSegment( @@ -54,7 +54,7 @@ namespace ImageSharp.Tests.Drawing.Text this.img.DrawText( "123", this.Font, - Brushes.Solid(Rgba32.Red), + Brushes.Solid(Rgba32.Red), null, Vector2.Zero, new TextGraphicsOptions(true)); @@ -67,7 +67,7 @@ namespace ImageSharp.Tests.Drawing.Text [Fact] public void FillsForEachACharachterWhenBrushSetAndNotPenDefaultOptions() { - this.img.DrawText("123", this.Font, Brushes.Solid(Rgba32.Red), null, Vector2.Zero); + this.img.DrawText("123", this.Font, Brushes.Solid(Rgba32.Red), null, Vector2.Zero); Assert.NotEmpty(this.img.ProcessorApplications); Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied @@ -77,7 +77,7 @@ namespace ImageSharp.Tests.Drawing.Text [Fact] public void FillsForEachACharachterWhenBrushSet() { - this.img.DrawText("123", this.Font, Brushes.Solid(Rgba32.Red), Vector2.Zero, new TextGraphicsOptions(true)); + this.img.DrawText("123", this.Font, Brushes.Solid(Rgba32.Red), Vector2.Zero, new TextGraphicsOptions(true)); Assert.NotEmpty(this.img.ProcessorApplications); Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied @@ -87,7 +87,7 @@ namespace ImageSharp.Tests.Drawing.Text [Fact] public void FillsForEachACharachterWhenBrushSetDefaultOptions() { - this.img.DrawText("123", this.Font, Brushes.Solid(Rgba32.Red), Vector2.Zero); + this.img.DrawText("123", this.Font, Brushes.Solid(Rgba32.Red), Vector2.Zero); Assert.NotEmpty(this.img.ProcessorApplications); Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied @@ -130,7 +130,7 @@ namespace ImageSharp.Tests.Drawing.Text "123", this.Font, null, - Pens.Dash(Rgba32.Red, 1), + Pens.Dash(Rgba32.Red, 1), Vector2.Zero, new TextGraphicsOptions(true)); @@ -142,7 +142,7 @@ namespace ImageSharp.Tests.Drawing.Text [Fact] public void DrawForEachACharachterWhenPenSetAndNotBrushDefaultOptions() { - this.img.DrawText("123", this.Font, null, Pens.Dash(Rgba32.Red, 1), Vector2.Zero); + this.img.DrawText("123", this.Font, null, Pens.Dash(Rgba32.Red, 1), Vector2.Zero); Assert.NotEmpty(this.img.ProcessorApplications); Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied @@ -152,7 +152,7 @@ namespace ImageSharp.Tests.Drawing.Text [Fact] public void DrawForEachACharachterWhenPenSet() { - this.img.DrawText("123", this.Font, Pens.Dash(Rgba32.Red, 1), Vector2.Zero, new TextGraphicsOptions(true)); + this.img.DrawText("123", this.Font, Pens.Dash(Rgba32.Red, 1), Vector2.Zero, new TextGraphicsOptions(true)); Assert.NotEmpty(this.img.ProcessorApplications); Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied @@ -162,7 +162,7 @@ namespace ImageSharp.Tests.Drawing.Text [Fact] public void DrawForEachACharachterWhenPenSetDefaultOptions() { - this.img.DrawText("123", this.Font, Pens.Dash(Rgba32.Red, 1), Vector2.Zero); + this.img.DrawText("123", this.Font, Pens.Dash(Rgba32.Red, 1), Vector2.Zero); Assert.NotEmpty(this.img.ProcessorApplications); Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied @@ -175,8 +175,8 @@ namespace ImageSharp.Tests.Drawing.Text this.img.DrawText( "123", this.Font, - Brushes.Solid(Rgba32.Red), - Pens.Dash(Rgba32.Red, 1), + Brushes.Solid(Rgba32.Red), + Pens.Dash(Rgba32.Red, 1), Vector2.Zero, new TextGraphicsOptions(true)); @@ -187,7 +187,7 @@ namespace ImageSharp.Tests.Drawing.Text [Fact] public void DrawForEachACharachterWhenPenSetAndFillFroEachWhenBrushSetDefaultOptions() { - this.img.DrawText("123", this.Font, Brushes.Solid(Rgba32.Red), Pens.Dash(Rgba32.Red, 1), Vector2.Zero); + this.img.DrawText("123", this.Font, Brushes.Solid(Rgba32.Red), Pens.Dash(Rgba32.Red, 1), Vector2.Zero); Assert.NotEmpty(this.img.ProcessorApplications); Assert.Equal(6, this.img.ProcessorApplications.Count); @@ -199,8 +199,8 @@ namespace ImageSharp.Tests.Drawing.Text this.img.DrawText( "1", this.Font, - Brushes.Solid(Rgba32.Red), - Pens.Dash(Rgba32.Red, 1), + Brushes.Solid(Rgba32.Red), + Pens.Dash(Rgba32.Red, 1), Vector2.Zero, new TextGraphicsOptions(true)); @@ -213,7 +213,7 @@ namespace ImageSharp.Tests.Drawing.Text [Fact] public void BrushAppliesBeforPenDefaultOptions() { - this.img.DrawText("1", this.Font, Brushes.Solid(Rgba32.Red), Pens.Dash(Rgba32.Red, 1), Vector2.Zero); + this.img.DrawText("1", this.Font, Brushes.Solid(Rgba32.Red), Pens.Dash(Rgba32.Red, 1), Vector2.Zero); Assert.NotEmpty(this.img.ProcessorApplications); Assert.Equal(2, this.img.ProcessorApplications.Count); @@ -226,11 +226,11 @@ namespace ImageSharp.Tests.Drawing.Text { this.img.MetaData.VerticalResolution = 1; this.img.MetaData.HorizontalResolution = 1; - this.img.DrawText("1", this.Font, Brushes.Solid(Rgba32.Red), Vector2.Zero, new TextGraphicsOptions(true) { + this.img.DrawText("1", this.Font, Brushes.Solid(Rgba32.Red), Vector2.Zero, new TextGraphicsOptions(true) { UseImageResolution = false }); - this.img.DrawText("1", this.Font, Brushes.Solid(Rgba32.Red), Vector2.Zero, new TextGraphicsOptions(true) + this.img.DrawText("1", this.Font, Brushes.Solid(Rgba32.Red), Vector2.Zero, new TextGraphicsOptions(true) { UseImageResolution = true }); diff --git a/tests/ImageSharp.Tests/Drawing/Text/OutputText.cs b/tests/ImageSharp.Tests/Drawing/Text/OutputText.cs index 1dbc93b9b..bb9cd264e 100644 --- a/tests/ImageSharp.Tests/Drawing/Text/OutputText.cs +++ b/tests/ImageSharp.Tests/Drawing/Text/OutputText.cs @@ -32,7 +32,7 @@ namespace ImageSharp.Tests.Drawing.Text public void DrawAB() { //draws 2 overlapping triangle glyphs twice 1 set on each line - using (Image img = new Image(100, 200)) + using (Image img = new Image(100, 200)) { img.Fill(Rgba32.DarkBlue) .DrawText("AB\nAB", new Font(this.Font, 50), Rgba32.Red, new Vector2(0, 0)); diff --git a/tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs index 497abb7d5..cf073d3d0 100644 --- a/tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Bmp/BmpEncoderTests.cs @@ -7,6 +7,8 @@ using ImageSharp.Formats; namespace ImageSharp.Tests { + using ImageSharp.PixelFormats; + using Xunit; public class BmpEncoderTests : FileTestBase @@ -27,7 +29,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileNameWithoutExtension(bitsPerPixel); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) { image.Save($"{path}/{filename}.bmp", new BmpEncoderOptions { BitsPerPixel = bitsPerPixel }); } diff --git a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs index 1ecd04690..6cea08cdd 100644 --- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs +++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests { using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class GeneralFormatTests : FileTestBase @@ -18,7 +20,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) { using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) { @@ -37,7 +39,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) { string filename = path + "/" + file.FileNameWithoutExtension + ".txt"; File.WriteAllText(filename, image.ToBase64String()); @@ -52,7 +54,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) { using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) { @@ -69,9 +71,9 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { - using (Image srcImage = file.CreateImage()) + using (Image srcImage = file.CreateImage()) { - using (Image image = new Image(srcImage)) + using (Image image = new Image(srcImage)) { using (FileStream output = File.OpenWrite($"{path}/Octree-{file.FileName}")) { @@ -81,7 +83,7 @@ namespace ImageSharp.Tests } } - using (Image image = new Image(srcImage)) + using (Image image = new Image(srcImage)) { using (FileStream output = File.OpenWrite($"{path}/Wu-{file.FileName}")) { @@ -90,7 +92,7 @@ namespace ImageSharp.Tests } } - using (Image image = new Image(srcImage)) + using (Image image = new Image(srcImage)) { using (FileStream output = File.OpenWrite($"{path}/Palette-{file.FileName}")) { @@ -109,7 +111,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) { using (FileStream output = File.OpenWrite($"{path}/{file.FileNameWithoutExtension}.bmp")) { @@ -142,7 +144,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { byte[] serialized; - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (MemoryStream memoryStream = new MemoryStream()) { image.Save(memoryStream); @@ -150,7 +152,7 @@ namespace ImageSharp.Tests serialized = memoryStream.ToArray(); } - using (Image image2 = Image.Load(serialized)) + using (Image image2 = Image.Load(serialized)) using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) { image2.Save(output); diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs index dd3019029..a5fc92901 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/GifDecoderTests.cs @@ -40,7 +40,7 @@ namespace ImageSharp.Tests TestFile testFile = TestFile.Create(TestImages.Gif.Rings); - using (Image image = testFile.CreateImage(options)) + using (Image image = testFile.CreateImage(options)) { Assert.Equal(1, image.MetaData.Properties.Count); Assert.Equal("Comments", image.MetaData.Properties[0].Name); @@ -58,7 +58,7 @@ namespace ImageSharp.Tests TestFile testFile = TestFile.Create(TestImages.Gif.Rings); - using (Image image = testFile.CreateImage(options)) + using (Image image = testFile.CreateImage(options)) { Assert.Equal(0, image.MetaData.Properties.Count); } @@ -74,7 +74,7 @@ namespace ImageSharp.Tests TestFile testFile = TestFile.Create(TestImages.Gif.Rings); - using (Image image = testFile.CreateImage(options)) + using (Image image = testFile.CreateImage(options)) { Assert.Equal(1, image.MetaData.Properties.Count); Assert.Equal("浉条卥慨灲", image.MetaData.Properties[0].Value); diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs index c657cde96..96dc2ebf7 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs @@ -36,14 +36,14 @@ namespace ImageSharp.Tests TestFile testFile = TestFile.Create(TestImages.Gif.Rings); - using (Image input = testFile.CreateImage()) + using (Image input = testFile.CreateImage()) { using (MemoryStream memStream = new MemoryStream()) { input.Save(memStream, new GifFormat(), options); memStream.Position = 0; - using (Image output = Image.Load(memStream)) + using (Image output = Image.Load(memStream)) { Assert.Equal(1, output.MetaData.Properties.Count); Assert.Equal("Comments", output.MetaData.Properties[0].Name); @@ -63,14 +63,14 @@ namespace ImageSharp.Tests TestFile testFile = TestFile.Create(TestImages.Gif.Rings); - using (Image input = testFile.CreateImage()) + using (Image input = testFile.CreateImage()) { using (MemoryStream memStream = new MemoryStream()) { input.SaveAsGif(memStream, options); memStream.Position = 0; - using (Image output = Image.Load(memStream)) + using (Image output = Image.Load(memStream)) { Assert.Equal(0, output.MetaData.Properties.Count); } @@ -81,7 +81,7 @@ namespace ImageSharp.Tests [Fact] public void Encode_CommentIsToLong_CommentIsTrimmed() { - using (Image input = new Image(1, 1)) + using (Image input = new Image(1, 1)) { string comments = new string('c', 256); input.MetaData.Properties.Add(new ImageProperty("Comments", comments)); @@ -91,7 +91,7 @@ namespace ImageSharp.Tests input.Save(memStream, new GifFormat()); memStream.Position = 0; - using (Image output = Image.Load(memStream)) + using (Image output = Image.Load(memStream)) { Assert.Equal(1, output.MetaData.Properties.Count); Assert.Equal("Comments", output.MetaData.Properties[0].Name); diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs index 1bcc72c43..a6d6d31f3 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegDecoderTests.cs @@ -105,7 +105,7 @@ namespace ImageSharp.Tests [Fact] public void Decoder_Reads_Correct_Resolution_From_Jfif() { - using (Image image = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan).CreateImage()) + using (Image image = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan).CreateImage()) { Assert.Equal(300, image.MetaData.HorizontalResolution); Assert.Equal(300, image.MetaData.VerticalResolution); @@ -115,7 +115,7 @@ namespace ImageSharp.Tests [Fact] public void Decoder_Reads_Correct_Resolution_From_Exif() { - using (Image image = TestFile.Create(TestImages.Jpeg.Baseline.Jpeg420).CreateImage()) + using (Image image = TestFile.Create(TestImages.Jpeg.Baseline.Jpeg420).CreateImage()) { Assert.Equal(72, image.MetaData.HorizontalResolution); Assert.Equal(72, image.MetaData.VerticalResolution); @@ -132,7 +132,7 @@ namespace ImageSharp.Tests TestFile testFile = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan); - using (Image image = testFile.CreateImage(options)) + using (Image image = testFile.CreateImage(options)) { Assert.NotNull(image.MetaData.ExifProfile); } @@ -148,7 +148,7 @@ namespace ImageSharp.Tests TestFile testFile = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan); - using (Image image = testFile.CreateImage(options)) + using (Image image = testFile.CreateImage(options)) { Assert.Null(image.MetaData.ExifProfile); } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.cs index d5f7c2ea3..60a7d7486 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.cs @@ -82,14 +82,14 @@ namespace ImageSharp.Tests TestFile testFile = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan); - using (Image input = testFile.CreateImage()) + using (Image input = testFile.CreateImage()) { using (MemoryStream memStream = new MemoryStream()) { input.Save(memStream, new JpegFormat(), options); memStream.Position = 0; - using (Image output = Image.Load(memStream)) + using (Image output = Image.Load(memStream)) { Assert.NotNull(output.MetaData.ExifProfile); } @@ -107,14 +107,14 @@ namespace ImageSharp.Tests TestFile testFile = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan); - using (Image input = testFile.CreateImage()) + using (Image input = testFile.CreateImage()) { using (MemoryStream memStream = new MemoryStream()) { input.SaveAsJpeg(memStream, options); memStream.Position = 0; - using (Image output = Image.Load(memStream)) + using (Image output = Image.Load(memStream)) { Assert.Null(output.MetaData.ExifProfile); } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegProfilingBenchmarks.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegProfilingBenchmarks.cs index 5150925b4..de16e146a 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegProfilingBenchmarks.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegProfilingBenchmarks.cs @@ -51,7 +51,7 @@ namespace ImageSharp.Tests ExecutionCount, () => { - Image img = Image.Load(bytes); + Image img = Image.Load(bytes); }, // ReSharper disable once ExplicitCallerInfoArgument $"Decode {fileName}"); diff --git a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs index d97b258dd..cf5c1cfa9 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngDecoderTests.cs @@ -42,7 +42,7 @@ namespace ImageSharp.Tests TestFile testFile = TestFile.Create(TestImages.Png.Blur); - using (Image image = testFile.CreateImage(options)) + using (Image image = testFile.CreateImage(options)) { Assert.Equal(1, image.MetaData.Properties.Count); Assert.Equal("Software", image.MetaData.Properties[0].Name); @@ -60,7 +60,7 @@ namespace ImageSharp.Tests TestFile testFile = TestFile.Create(TestImages.Png.Blur); - using (Image image = testFile.CreateImage(options)) + using (Image image = testFile.CreateImage(options)) { Assert.Equal(0, image.MetaData.Properties.Count); } @@ -76,7 +76,7 @@ namespace ImageSharp.Tests TestFile testFile = TestFile.Create(TestImages.Png.Blur); - using (Image image = testFile.CreateImage(options)) + using (Image image = testFile.CreateImage(options)) { Assert.Equal(1, image.MetaData.Properties.Count); Assert.Equal("潓瑦慷敲", image.MetaData.Properties[0].Name); diff --git a/tests/ImageSharp.Tests/Formats/Png/PngSmokeTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngSmokeTests.cs index be965160c..20eb22bfb 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngSmokeTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngSmokeTests.cs @@ -32,7 +32,7 @@ namespace ImageSharp.Tests.Formats.Png image.Save(ms, new PngEncoder()); ms.Position = 0; - using (Image img2 = Image.Load(ms, new PngDecoder())) + using (Image img2 = Image.Load(ms, new PngDecoder())) { // img2.Save(provider.Utility.GetTestOutputFileName("bmp", "_loaded"), new BmpEncoder()); ImageComparer.CheckSimilarity(image, img2); @@ -53,7 +53,7 @@ namespace ImageSharp.Tests.Formats.Png image.MetaData.Quality = 256; image.Save(ms, new PngEncoder()); ms.Position = 0; - using (Image img2 = Image.Load(ms, new PngDecoder())) + using (Image img2 = Image.Load(ms, new PngDecoder())) { // img2.Save(provider.Utility.GetTestOutputFileName("bmp", "_loaded"), new BmpEncoder()); ImageComparer.CheckSimilarity(image, img2, 0.03f); @@ -119,7 +119,7 @@ namespace ImageSharp.Tests.Formats.Png image.Save(ms, new PngEncoder()); ms.Position = 0; - using (Image img2 = Image.Load(ms, new PngDecoder())) + using (Image img2 = Image.Load(ms, new PngDecoder())) { ImageComparer.CheckSimilarity(image, img2); } diff --git a/tests/ImageSharp.Tests/Image/ImageLoadTests.cs b/tests/ImageSharp.Tests/Image/ImageLoadTests.cs index 505074a6a..af1449dae 100644 --- a/tests/ImageSharp.Tests/Image/ImageLoadTests.cs +++ b/tests/ImageSharp.Tests/Image/ImageLoadTests.cs @@ -33,8 +33,8 @@ namespace ImageSharp.Tests public ImageLoadTests() { - this.returnImage = new Image(1, 1); - + this.returnImage = new Image(1, 1); + this.localDecoder = new Mock(); this.localFormat = new Mock(); this.localFormat.Setup(x => x.Decoder).Returns(this.localDecoder.Object); @@ -57,7 +57,7 @@ namespace ImageSharp.Tests .Returns(this.returnImage); this.fileSystem = new Mock(); - + this.LocalConfiguration = new Configuration(this.localFormat.Object) { FileSystem = this.fileSystem.Object @@ -77,7 +77,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromStream() { - Image img = Image.Load(this.DataStream); + Image img = Image.Load(this.DataStream); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat, img.CurrentImageFormat); @@ -91,7 +91,7 @@ namespace ImageSharp.Tests public void LoadFromNoneSeekableStream() { NoneSeekableStream stream = new NoneSeekableStream(this.DataStream); - Image img = Image.Load(stream); + Image img = Image.Load(stream); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat, img.CurrentImageFormat); @@ -104,7 +104,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromStreamWithType() { - Image img = Image.Load(this.DataStream); + Image img = Image.Load(this.DataStream); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat.Sample(), img); @@ -117,7 +117,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromStreamWithOptions() { - Image img = Image.Load(this.DataStream, this.decoderOptions); + Image img = Image.Load(this.DataStream, this.decoderOptions); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat, img.CurrentImageFormat); @@ -129,7 +129,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromStreamWithTypeAndOptions() { - Image img = Image.Load(this.DataStream, this.decoderOptions); + Image img = Image.Load(this.DataStream, this.decoderOptions); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat.Sample(), img); @@ -143,7 +143,7 @@ namespace ImageSharp.Tests public void LoadFromStreamWithConfig() { Stream stream = new MemoryStream(); - Image img = Image.Load(this.LocalConfiguration, stream); + Image img = Image.Load(this.LocalConfiguration, stream); Assert.NotNull(img); Assert.Equal(this.localFormat.Object, img.CurrentImageFormat); @@ -156,7 +156,7 @@ namespace ImageSharp.Tests public void LoadFromStreamWithTypeAndConfig() { Stream stream = new MemoryStream(); - Image img = Image.Load(this.LocalConfiguration, stream); + Image img = Image.Load(this.LocalConfiguration, stream); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -170,7 +170,7 @@ namespace ImageSharp.Tests public void LoadFromStreamWithConfigAndOptions() { Stream stream = new MemoryStream(); - Image img = Image.Load(this.LocalConfiguration, stream, this.decoderOptions); + Image img = Image.Load(this.LocalConfiguration, stream, this.decoderOptions); Assert.NotNull(img); Assert.Equal(this.localFormat.Object, img.CurrentImageFormat); @@ -183,7 +183,7 @@ namespace ImageSharp.Tests public void LoadFromStreamWithTypeAndConfigAndOptions() { Stream stream = new MemoryStream(); - Image img = Image.Load(this.LocalConfiguration, stream, this.decoderOptions); + Image img = Image.Load(this.LocalConfiguration, stream, this.decoderOptions); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -199,7 +199,7 @@ namespace ImageSharp.Tests public void LoadFromStreamWithDecoder() { Stream stream = new MemoryStream(); - Image img = Image.Load(stream, this.localDecoder.Object); + Image img = Image.Load(stream, this.localDecoder.Object); Assert.NotNull(img); this.localDecoder.Verify(x => x.Decode(Configuration.Default, stream, null)); @@ -209,7 +209,7 @@ namespace ImageSharp.Tests public void LoadFromStreamWithTypeAndDecoder() { Stream stream = new MemoryStream(); - Image img = Image.Load(stream, this.localDecoder.Object); + Image img = Image.Load(stream, this.localDecoder.Object); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -220,7 +220,7 @@ namespace ImageSharp.Tests public void LoadFromStreamWithDecoderAndOptions() { Stream stream = new MemoryStream(); - Image img = Image.Load(stream, this.localDecoder.Object, this.decoderOptions); + Image img = Image.Load(stream, this.localDecoder.Object, this.decoderOptions); Assert.NotNull(img); this.localDecoder.Verify(x => x.Decode(Configuration.Default, stream, this.decoderOptions)); @@ -230,7 +230,7 @@ namespace ImageSharp.Tests public void LoadFromStreamWithTypeAndDecoderAndOptions() { Stream stream = new MemoryStream(); - Image img = Image.Load(stream, this.localDecoder.Object, this.decoderOptions); + Image img = Image.Load(stream, this.localDecoder.Object, this.decoderOptions); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -240,7 +240,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytes() { - Image img = Image.Load(this.DataStream.ToArray()); + Image img = Image.Load(this.DataStream.ToArray()); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat, img.CurrentImageFormat); @@ -253,7 +253,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithType() { - Image img = Image.Load(this.DataStream.ToArray()); + Image img = Image.Load(this.DataStream.ToArray()); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat.Sample(), img); @@ -266,7 +266,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithOptions() { - Image img = Image.Load(this.DataStream.ToArray(), this.decoderOptions); + Image img = Image.Load(this.DataStream.ToArray(), this.decoderOptions); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat, img.CurrentImageFormat); @@ -278,7 +278,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithTypeAndOptions() { - Image img = Image.Load(this.DataStream.ToArray(), this.decoderOptions); + Image img = Image.Load(this.DataStream.ToArray(), this.decoderOptions); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat.Sample(), img); @@ -291,7 +291,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithConfig() { - Image img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray()); + Image img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray()); Assert.NotNull(img); Assert.Equal(this.localFormat.Object, img.CurrentImageFormat); @@ -304,7 +304,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithTypeAndConfig() { - Image img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray()); + Image img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray()); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -319,7 +319,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithConfigAndOptions() { - Image img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray(), this.decoderOptions); + Image img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray(), this.decoderOptions); Assert.NotNull(img); Assert.Equal(this.localFormat.Object, img.CurrentImageFormat); @@ -332,7 +332,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithTypeAndConfigAndOptions() { - Image img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray(), this.decoderOptions); + Image img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray(), this.decoderOptions); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -347,7 +347,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithDecoder() { - Image img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object); + Image img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object); Assert.NotNull(img); this.localDecoder.Verify(x => x.Decode(Configuration.Default, It.IsAny(), null)); @@ -357,7 +357,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithTypeAndDecoder() { - Image img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object); + Image img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -368,7 +368,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithDecoderAndOptions() { - Image img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object, this.decoderOptions); + Image img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object, this.decoderOptions); Assert.NotNull(img); this.localDecoder.Verify(x => x.Decode(Configuration.Default, It.IsAny(), this.decoderOptions)); @@ -378,7 +378,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithTypeAndDecoderAndOptions() { - Image img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object, this.decoderOptions); + Image img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object, this.decoderOptions); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -389,7 +389,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFile() { - Image img = Image.Load(this.DataStream); + Image img = Image.Load(this.DataStream); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat, img.CurrentImageFormat); @@ -402,7 +402,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithType() { - Image img = Image.Load(this.DataStream); + Image img = Image.Load(this.DataStream); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat.Sample(), img); @@ -415,7 +415,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithOptions() { - Image img = Image.Load(this.DataStream, this.decoderOptions); + Image img = Image.Load(this.DataStream, this.decoderOptions); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat, img.CurrentImageFormat); @@ -427,7 +427,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithTypeAndOptions() { - Image img = Image.Load(this.DataStream, this.decoderOptions); + Image img = Image.Load(this.DataStream, this.decoderOptions); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat.Sample(), img); @@ -440,7 +440,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithConfig() { - Image img = Image.Load(this.LocalConfiguration, this.FilePath); + Image img = Image.Load(this.LocalConfiguration, this.FilePath); Assert.NotNull(img); Assert.Equal(this.localFormat.Object, img.CurrentImageFormat); @@ -452,7 +452,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithTypeAndConfig() { - Image img = Image.Load(this.LocalConfiguration, this.FilePath); + Image img = Image.Load(this.LocalConfiguration, this.FilePath); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -465,7 +465,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithConfigAndOptions() { - Image img = Image.Load(this.LocalConfiguration, this.FilePath, this.decoderOptions); + Image img = Image.Load(this.LocalConfiguration, this.FilePath, this.decoderOptions); Assert.NotNull(img); Assert.Equal(this.localFormat.Object, img.CurrentImageFormat); @@ -477,7 +477,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithTypeAndConfigAndOptions() { - Image img = Image.Load(this.LocalConfiguration, this.FilePath, this.decoderOptions); + Image img = Image.Load(this.LocalConfiguration, this.FilePath, this.decoderOptions); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -491,7 +491,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithDecoder() { - Image img = Image.Load(this.FilePath, this.localDecoder.Object); + Image img = Image.Load(this.FilePath, this.localDecoder.Object); Assert.NotNull(img); this.localDecoder.Verify(x => x.Decode(Configuration.Default, this.DataStream, null)); @@ -500,7 +500,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithTypeAndDecoder() { - Image img = Image.Load(this.FilePath, this.localDecoder.Object); + Image img = Image.Load(this.FilePath, this.localDecoder.Object); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -510,7 +510,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithDecoderAndOptions() { - Image img = Image.Load(this.FilePath, this.localDecoder.Object, this.decoderOptions); + Image img = Image.Load(this.FilePath, this.localDecoder.Object, this.decoderOptions); Assert.NotNull(img); this.localDecoder.Verify(x => x.Decode(Configuration.Default, this.DataStream, this.decoderOptions)); @@ -519,7 +519,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithTypeAndDecoderAndOptions() { - Image img = Image.Load(this.FilePath, this.localDecoder.Object, this.decoderOptions); + Image img = Image.Load(this.FilePath, this.localDecoder.Object, this.decoderOptions); Assert.NotNull(img); Assert.Equal(this.returnImage, img); diff --git a/tests/ImageSharp.Tests/Image/ImageSaveTests.cs b/tests/ImageSharp.Tests/Image/ImageSaveTests.cs index 902bedb5e..315d67344 100644 --- a/tests/ImageSharp.Tests/Image/ImageSaveTests.cs +++ b/tests/ImageSharp.Tests/Image/ImageSaveTests.cs @@ -20,7 +20,7 @@ namespace ImageSharp.Tests /// public class ImageSaveTests : IDisposable { - private readonly Image Image; + private readonly Image Image; private readonly Mock fileSystem; private readonly Mock format; private readonly Mock formatNotRegistered; @@ -49,7 +49,7 @@ namespace ImageSharp.Tests this.fileSystem = new Mock(); this.encoderOptions = new Mock().Object; - this.Image = new Image(1, 1, new Configuration(this.format.Object) { + this.Image = new Image(1, 1, new Configuration(this.format.Object) { FileSystem = this.fileSystem.Object }); } @@ -71,7 +71,7 @@ namespace ImageSharp.Tests this.fileSystem.Setup(x => x.Create("path.jpg")).Returns(stream); this.Image.Save("path.jpg", this.encoderOptions); - + this.encoder.Verify(x => x.Encode(this.Image, stream, this.encoderOptions)); } @@ -126,7 +126,7 @@ namespace ImageSharp.Tests { Stream stream = new MemoryStream(); this.Image.Save(stream); - + this.encoder.Verify(x => x.Encode(this.Image, stream, null)); } diff --git a/tests/ImageSharp.Tests/Image/ImageTests.cs b/tests/ImageSharp.Tests/Image/ImageTests.cs index 02b0e5ad9..6aa963143 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Tests using System; using ImageSharp.Formats; + using ImageSharp.PixelFormats; using Xunit; @@ -21,11 +22,11 @@ namespace ImageSharp.Tests { Assert.Throws(() => { - Image.Load((byte[])null); + Image.Load((byte[])null); }); TestFile file = TestFile.Create(TestImages.Bmp.Car); - using (Image image = Image.Load(file.Bytes)) + using (Image image = Image.Load(file.Bytes)) { Assert.Equal(600, image.Width); Assert.Equal(450, image.Height); @@ -36,7 +37,7 @@ namespace ImageSharp.Tests public void ConstructorFileSystem() { TestFile file = TestFile.Create(TestImages.Bmp.Car); - using (Image image = Image.Load(file.FilePath)) + using (Image image = Image.Load(file.FilePath)) { Assert.Equal(600, image.Width); Assert.Equal(450, image.Height); @@ -49,7 +50,7 @@ namespace ImageSharp.Tests System.IO.FileNotFoundException ex = Assert.Throws( () => { - Image.Load(Guid.NewGuid().ToString()); + Image.Load(Guid.NewGuid().ToString()); }); } @@ -59,7 +60,7 @@ namespace ImageSharp.Tests ArgumentNullException ex = Assert.Throws( () => { - Image.Load((string) null); + Image.Load((string)null); }); } @@ -68,13 +69,13 @@ namespace ImageSharp.Tests { string file = TestFile.GetPath("../../TestOutput/Save_DetecedEncoding.png"); System.IO.DirectoryInfo dir = System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(file)); - using (Image image = new Image(10, 10)) + using (Image image = new Image(10, 10)) { image.Save(file); } TestFile c = TestFile.Create("../../TestOutput/Save_DetecedEncoding.png"); - using (Image img = c.CreateImage()) + using (Image img = c.CreateImage()) { Assert.IsType(img.CurrentImageFormat); } @@ -87,7 +88,7 @@ namespace ImageSharp.Tests InvalidOperationException ex = Assert.Throws( () => { - using (Image image = new Image(10, 10)) + using (Image image = new Image(10, 10)) { image.Save(file); } @@ -99,13 +100,13 @@ namespace ImageSharp.Tests { string file = TestFile.GetPath("../../TestOutput/Save_SetFormat.dat"); System.IO.DirectoryInfo dir = System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(file)); - using (Image image = new Image(10, 10)) + using (Image image = new Image(10, 10)) { image.Save(file, new PngFormat()); } TestFile c = TestFile.Create("../../TestOutput/Save_SetFormat.dat"); - using (Image img = c.CreateImage()) + using (Image img = c.CreateImage()) { Assert.IsType(img.CurrentImageFormat); } @@ -116,13 +117,13 @@ namespace ImageSharp.Tests { string file = TestFile.GetPath("../../TestOutput/Save_SetEncoding.dat"); System.IO.DirectoryInfo dir = System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(file)); - using (Image image = new Image(10, 10)) + using (Image image = new Image(10, 10)) { image.Save(file, new PngEncoder()); } TestFile c = TestFile.Create("../../TestOutput/Save_SetEncoding.dat"); - using (Image img = c.CreateImage()) + using (Image img = c.CreateImage()) { Assert.IsType(img.CurrentImageFormat); } diff --git a/tests/ImageSharp.Tests/MetaData/ImageMetaDataTests.cs b/tests/ImageSharp.Tests/MetaData/ImageMetaDataTests.cs index 4ef9c57aa..bc64d613a 100644 --- a/tests/ImageSharp.Tests/MetaData/ImageMetaDataTests.cs +++ b/tests/ImageSharp.Tests/MetaData/ImageMetaDataTests.cs @@ -6,6 +6,8 @@ namespace ImageSharp.Tests { using ImageSharp.Formats; + using ImageSharp.PixelFormats; + using Xunit; /// @@ -81,7 +83,7 @@ namespace ImageSharp.Tests exifProfile.SetValue(ExifTag.XResolution, new Rational(200)); exifProfile.SetValue(ExifTag.YResolution, new Rational(300)); - Image image = new Image(1, 1); + Image image = new Image(1, 1); image.MetaData.ExifProfile = exifProfile; image.MetaData.HorizontalResolution = 400; image.MetaData.VerticalResolution = 500; diff --git a/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs b/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs index 6a832859a..1747f34ad 100644 --- a/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs +++ b/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs @@ -20,7 +20,7 @@ namespace ImageSharp.Tests [Fact] public void Constructor() { - Image image = TestFile.Create(TestImages.Jpeg.Baseline.Calliphora).CreateImage(); + Image image = TestFile.Create(TestImages.Jpeg.Baseline.Calliphora).CreateImage(); Assert.Null(image.MetaData.ExifProfile); @@ -70,13 +70,13 @@ namespace ImageSharp.Tests profile.SetValue(ExifTag.ExposureTime, new Rational(exposureTime)); - Image image = new Image(1, 1); + Image image = new Image(1, 1); image.MetaData.ExifProfile = profile; image.SaveAsJpeg(memStream); memStream.Position = 0; - image = Image.Load(memStream); + image = Image.Load(memStream); profile = image.MetaData.ExifProfile; Assert.NotNull(profile); @@ -94,7 +94,7 @@ namespace ImageSharp.Tests image.SaveAsJpeg(memStream); memStream.Position = 0; - image = Image.Load(memStream); + image = Image.Load(memStream); profile = image.MetaData.ExifProfile; Assert.NotNull(profile); @@ -107,7 +107,7 @@ namespace ImageSharp.Tests [Fact] public void ReadWriteInfinity() { - Image image = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan).CreateImage(); + Image image = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan).CreateImage(); image.MetaData.ExifProfile.SetValue(ExifTag.ExposureBiasValue, new SignedRational(double.PositiveInfinity)); image = WriteAndRead(image); @@ -135,7 +135,7 @@ namespace ImageSharp.Tests { Rational[] latitude = new Rational[] { new Rational(12.3), new Rational(4.56), new Rational(789.0) }; - Image image = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan).CreateImage(); + Image image = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan).CreateImage(); image.MetaData.ExifProfile.SetValue(ExifTag.Software, "ImageSharp"); ExifValue value = image.MetaData.ExifProfile.GetValue(ExifTag.Software); @@ -261,7 +261,7 @@ namespace ImageSharp.Tests junk.Append("I"); } - Image image = new Image(100, 100); + Image image = new Image(100, 100); image.MetaData.ExifProfile = new ExifProfile(); image.MetaData.ExifProfile.SetValue(ExifTag.ImageDescription, junk.ToString()); @@ -274,7 +274,7 @@ namespace ImageSharp.Tests [Fact] public void ExifTypeUndefined() { - Image image = TestFile.Create(TestImages.Jpeg.Baseline.Bad.ExifUndefType).CreateImage(); + Image image = TestFile.Create(TestImages.Jpeg.Baseline.Bad.ExifUndefType).CreateImage(); Assert.NotNull(image); ExifProfile profile = image.MetaData.ExifProfile; @@ -291,7 +291,7 @@ namespace ImageSharp.Tests private static ExifProfile GetExifProfile() { - Image image = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan).CreateImage(); + Image image = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan).CreateImage(); ExifProfile profile = image.MetaData.ExifProfile; Assert.NotNull(profile); @@ -299,7 +299,7 @@ namespace ImageSharp.Tests return profile; } - private static Image WriteAndRead(Image image) + private static Image WriteAndRead(Image image) { using (MemoryStream memStream = new MemoryStream()) { @@ -307,7 +307,7 @@ namespace ImageSharp.Tests image.Dispose(); memStream.Position = 0; - return Image.Load(memStream); + return Image.Load(memStream); } } diff --git a/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifValueTests.cs b/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifValueTests.cs index 2014d08dc..a91eb310d 100644 --- a/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifValueTests.cs +++ b/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifValueTests.cs @@ -6,6 +6,9 @@ namespace ImageSharp.Tests { using System.Linq; + + using ImageSharp.PixelFormats; + using Xunit; public class ExifValueTests @@ -13,7 +16,7 @@ namespace ImageSharp.Tests private static ExifValue GetExifValue() { ExifProfile profile; - using (Image image = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan).CreateImage()) + using (Image image = TestFile.Create(TestImages.Jpeg.Baseline.Floorplan).CreateImage()) { profile = image.MetaData.ExifProfile; } diff --git a/tests/ImageSharp.Tests/Processors/Filters/AlphaTest.cs b/tests/ImageSharp.Tests/Processors/Filters/AlphaTest.cs index e1557abca..e40e3a205 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/AlphaTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/AlphaTest.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests { using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class AlphaTest : FileTestBase @@ -27,7 +29,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(value); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Alpha(value).Save(output); @@ -44,7 +46,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(value + "-InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Alpha(value, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/AutoOrientTests.cs b/tests/ImageSharp.Tests/Processors/Filters/AutoOrientTests.cs index ef183480c..470e7150b 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/AutoOrientTests.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/AutoOrientTests.cs @@ -6,6 +6,9 @@ namespace ImageSharp.Tests { using System.IO; + + using ImageSharp.PixelFormats; + using Processing; using Xunit; @@ -33,7 +36,7 @@ namespace ImageSharp.Tests TestFile file = TestFile.Create(TestImages.Bmp.F); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) { image.MetaData.ExifProfile = new ExifProfile(); image.MetaData.ExifProfile.SetValue(ExifTag.Orientation, orientation); diff --git a/tests/ImageSharp.Tests/Processors/Filters/BackgroundColorTest.cs b/tests/ImageSharp.Tests/Processors/Filters/BackgroundColorTest.cs index 4bc39f8ab..d7ca78d1b 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/BackgroundColorTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/BackgroundColorTest.cs @@ -20,7 +20,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) { image.BackgroundColor(Rgba32.HotPink).Save(output); @@ -36,7 +36,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName("-InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.BackgroundColor(Rgba32.HotPink, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/BinaryThresholdTest.cs b/tests/ImageSharp.Tests/Processors/Filters/BinaryThresholdTest.cs index f36014542..4e5e8a82e 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/BinaryThresholdTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/BinaryThresholdTest.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests { using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class BinaryThresholdTest : FileTestBase @@ -27,7 +29,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(value); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.BinaryThreshold(value).Save(output); @@ -44,7 +46,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(value + "-InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.BinaryThreshold(value, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/BlackWhiteTest.cs b/tests/ImageSharp.Tests/Processors/Filters/BlackWhiteTest.cs index 377ae4719..d10698a99 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/BlackWhiteTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/BlackWhiteTest.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests { using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class BlackWhiteTest : FileTestBase @@ -18,7 +20,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) { image.BlackWhite().Save(output); @@ -34,7 +36,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName("-InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.BlackWhite(new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/BoxBlurTest.cs b/tests/ImageSharp.Tests/Processors/Filters/BoxBlurTest.cs index f4f5fb4bb..03226a961 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/BoxBlurTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/BoxBlurTest.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests { using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class BoxBlurTest : FileTestBase @@ -27,7 +29,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(value); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.BoxBlur(value).Save(output); @@ -44,7 +46,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(value + "-InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.BoxBlur(value, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/BrightnessTest.cs b/tests/ImageSharp.Tests/Processors/Filters/BrightnessTest.cs index f59d5be4c..ce434d734 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/BrightnessTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/BrightnessTest.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests { using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class BrightnessTest : FileTestBase @@ -27,7 +29,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(value); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Brightness(value).Save(output); @@ -44,7 +46,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(value + "-InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Brightness(value, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/ColorBlindnessTest.cs b/tests/ImageSharp.Tests/Processors/Filters/ColorBlindnessTest.cs index 5564a77ef..c28732253 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/ColorBlindnessTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/ColorBlindnessTest.cs @@ -8,6 +8,8 @@ namespace ImageSharp.Tests using Processing; using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class ColorBlindnessTest : FileTestBase @@ -34,7 +36,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(colorBlindness); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.ColorBlindness(colorBlindness).Save(output); @@ -51,7 +53,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(colorBlindness + "-InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.ColorBlindness(colorBlindness, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/ContrastTest.cs b/tests/ImageSharp.Tests/Processors/Filters/ContrastTest.cs index 5bbe2338c..4626fbb6e 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/ContrastTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/ContrastTest.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests { using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class ContrastTest : FileTestBase @@ -26,7 +28,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) { image.Contrast(value).Save(output); @@ -43,7 +45,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(value + "-InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Contrast(value, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/CropTest.cs b/tests/ImageSharp.Tests/Processors/Filters/CropTest.cs index 69c9d9372..6713d0d38 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/CropTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/CropTest.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests { using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class CropTest : FileTestBase @@ -18,7 +20,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) { image.Crop(image.Width / 2, image.Height / 2).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/DetectEdgesTest.cs b/tests/ImageSharp.Tests/Processors/Filters/DetectEdgesTest.cs index e12440106..00440e8a5 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/DetectEdgesTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/DetectEdgesTest.cs @@ -8,6 +8,8 @@ namespace ImageSharp.Tests using Processing; using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class DetectEdgesTest : FileTestBase @@ -36,7 +38,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(detector); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.DetectEdges(detector).Save(output); @@ -53,7 +55,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(detector + "-InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.DetectEdges(detector, new Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2)) diff --git a/tests/ImageSharp.Tests/Processors/Filters/DitherTest.cs b/tests/ImageSharp.Tests/Processors/Filters/DitherTest.cs index e89a1b1ec..066e2d134 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/DitherTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/DitherTest.cs @@ -9,6 +9,7 @@ namespace ImageSharp.Tests using ImageSharp.Dithering; using ImageSharp.Dithering.Ordered; + using ImageSharp.PixelFormats; using Xunit; @@ -41,7 +42,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(name); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Dither(ditherer).Save(output); @@ -58,7 +59,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName($"{name}-InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Dither(ditherer, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); @@ -75,7 +76,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(name); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Dither(diffuser, .5F).Save(output); @@ -92,7 +93,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName($"{name}-InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Dither(diffuser, .5F, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/EntropyCropTest.cs b/tests/ImageSharp.Tests/Processors/Filters/EntropyCropTest.cs index 1299d9814..710e23e37 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/EntropyCropTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/EntropyCropTest.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests { using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class EntropyCropTest : FileTestBase @@ -27,7 +29,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(value); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.EntropyCrop(value).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/FlipTests.cs b/tests/ImageSharp.Tests/Processors/Filters/FlipTests.cs index 26bc240d5..87f5e1025 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/FlipTests.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/FlipTests.cs @@ -6,6 +6,9 @@ namespace ImageSharp.Tests { using System.IO; + + using ImageSharp.PixelFormats; + using Processing; using Xunit; @@ -28,7 +31,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(flipType); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Flip(flipType).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/GlowTest.cs b/tests/ImageSharp.Tests/Processors/Filters/GlowTest.cs index ebbfdd5f0..43e45f9ca 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/GlowTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/GlowTest.cs @@ -20,7 +20,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) { image.Glow().Save(output); @@ -36,7 +36,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName("Color"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Glow(Rgba32.HotPink).Save(output); @@ -52,7 +52,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName("Radius"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Glow(image.Width / 4F).Save(output); @@ -68,7 +68,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName("InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Glow(new Rectangle(image.Width / 8, image.Height / 8, image.Width / 2, image.Height / 2)) diff --git a/tests/ImageSharp.Tests/Processors/Filters/HueTest.cs b/tests/ImageSharp.Tests/Processors/Filters/HueTest.cs index 3081c638c..488433931 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/HueTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/HueTest.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests { using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class HueTest : FileTestBase @@ -27,7 +29,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(value); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Hue(value).Save(output); @@ -44,7 +46,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(value + "-InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Hue(value, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/InvertTest.cs b/tests/ImageSharp.Tests/Processors/Filters/InvertTest.cs index da672f830..6d375d09a 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/InvertTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/InvertTest.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests { using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class InvertTest : FileTestBase @@ -17,7 +19,7 @@ namespace ImageSharp.Tests string path = this.CreateOutputDirectory("Invert"); foreach (TestFile file in Files) { - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) { image.Invert().Save(output); @@ -33,7 +35,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName("InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Invert(new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/KodachromeTest.cs b/tests/ImageSharp.Tests/Processors/Filters/KodachromeTest.cs index 870f813a1..29a459f97 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/KodachromeTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/KodachromeTest.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests { using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class KodachromeTest : FileTestBase @@ -18,7 +20,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) { image.Kodachrome().Save(output); @@ -34,7 +36,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName("InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Kodachrome(new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/LomographTest.cs b/tests/ImageSharp.Tests/Processors/Filters/LomographTest.cs index 57ca72d39..6ceedecbd 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/LomographTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/LomographTest.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests { using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class LomographTest : FileTestBase @@ -18,7 +20,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) { image.Lomograph().Save(output); @@ -34,7 +36,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName("InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Lomograph(new Rectangle(image.Width / 4, image.Width / 4, image.Width / 2, image.Height / 2)) diff --git a/tests/ImageSharp.Tests/Processors/Filters/OilPaintTest.cs b/tests/ImageSharp.Tests/Processors/Filters/OilPaintTest.cs index a9b552e21..5facee346 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/OilPaintTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/OilPaintTest.cs @@ -8,6 +8,8 @@ namespace ImageSharp.Tests using System; using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class OilPaintTest : FileTestBase @@ -28,7 +30,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(value); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { if (image.Width > value.Item2 && image.Height > value.Item2) @@ -48,7 +50,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(value + "-InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) { if (image.Width > value.Item2 && image.Height > value.Item2) { diff --git a/tests/ImageSharp.Tests/Processors/Filters/PadTest.cs b/tests/ImageSharp.Tests/Processors/Filters/PadTest.cs index f00cdd4f3..6095410a9 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/PadTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/PadTest.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests { using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class PadTest : FileTestBase @@ -18,7 +20,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) { image.Pad(image.Width + 50, image.Height + 50).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/PolaroidTest.cs b/tests/ImageSharp.Tests/Processors/Filters/PolaroidTest.cs index e9938fb83..df3803255 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/PolaroidTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/PolaroidTest.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests { using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class PolaroidTest : FileTestBase @@ -18,7 +20,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) { image.Polaroid().Save(output); @@ -34,7 +36,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName("InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Polaroid(new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/ResizeProfilingBenchmarks.cs b/tests/ImageSharp.Tests/Processors/Filters/ResizeProfilingBenchmarks.cs index 917bb895c..a743665d4 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/ResizeProfilingBenchmarks.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/ResizeProfilingBenchmarks.cs @@ -27,7 +27,7 @@ namespace ImageSharp.Tests this.Measure(this.ExecutionCount, () => { - using (Image image = new Image(width, height)) + using (Image image = new Image(width, height)) { image.Resize(width / 4, height / 4); } diff --git a/tests/ImageSharp.Tests/Processors/Filters/ResizeTests.cs b/tests/ImageSharp.Tests/Processors/Filters/ResizeTests.cs index 643033f4c..31f4020b6 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/ResizeTests.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/ResizeTests.cs @@ -6,6 +6,9 @@ namespace ImageSharp.Tests { using System; using System.IO; + + using ImageSharp.PixelFormats; + using Processing; using Xunit; @@ -42,7 +45,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(name); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Resize(image.Width / 2, image.Height / 2, sampler, true).Save(output); @@ -61,7 +64,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(name); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { Rectangle sourceRectangle = new Rectangle(image.Width / 8, image.Height / 8, image.Width / 4, image.Height / 4); @@ -82,7 +85,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(name); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Resize(image.Width / 3, 0, sampler, false).Save(output); @@ -101,7 +104,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(name); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Resize(0, image.Height / 3, sampler, false).Save(output); @@ -120,7 +123,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(name); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { ResizeOptions options = new ResizeOptions @@ -145,7 +148,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(name); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { ResizeOptions options = new ResizeOptions @@ -170,7 +173,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(name); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { ResizeOptions options = new ResizeOptions @@ -195,7 +198,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(name); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { ResizeOptions options = new ResizeOptions @@ -221,7 +224,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(name); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { ResizeOptions options = new ResizeOptions @@ -247,7 +250,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(name); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { ResizeOptions options = new ResizeOptions @@ -273,7 +276,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(name); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { ResizeOptions options = new ResizeOptions diff --git a/tests/ImageSharp.Tests/Processors/Filters/RotateFlipTest.cs b/tests/ImageSharp.Tests/Processors/Filters/RotateFlipTest.cs index e235ed229..c4c4fa8d7 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/RotateFlipTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/RotateFlipTest.cs @@ -6,6 +6,9 @@ namespace ImageSharp.Tests { using System.IO; + + using ImageSharp.PixelFormats; + using Processing; using Xunit; @@ -30,7 +33,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(rotateType + "-" + flipType); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.RotateFlip(rotateType, flipType).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/RotateTest.cs b/tests/ImageSharp.Tests/Processors/Filters/RotateTest.cs index a504fd989..b30c795ad 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/RotateTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/RotateTest.cs @@ -6,6 +6,9 @@ namespace ImageSharp.Tests { using System.IO; + + using ImageSharp.PixelFormats; + using Processing; using Xunit; @@ -36,7 +39,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(value); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Rotate(value).Save(output); @@ -53,7 +56,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(value); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Rotate(value).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/SaturationTest.cs b/tests/ImageSharp.Tests/Processors/Filters/SaturationTest.cs index ee24f120c..e28847fa3 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/SaturationTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/SaturationTest.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests { using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class SaturationTest : FileTestBase @@ -27,7 +29,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(value); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Saturation(value).Save(output); @@ -44,7 +46,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(value + "-InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Saturation(value, new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/SepiaTest.cs b/tests/ImageSharp.Tests/Processors/Filters/SepiaTest.cs index 0e1583cc6..490213ce7 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/SepiaTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/SepiaTest.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests { using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class SepiaTest : FileTestBase @@ -18,7 +20,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) { image.Sepia().Save(output); @@ -34,7 +36,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName("InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Sepia(new Rectangle(10, 10, image.Width / 2, image.Height / 2)).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/SkewTest.cs b/tests/ImageSharp.Tests/Processors/Filters/SkewTest.cs index 231f5dae8..d096b3913 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/SkewTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/SkewTest.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests { using System.IO; + using ImageSharp.PixelFormats; + using Xunit; public class SkewTest : FileTestBase @@ -29,7 +31,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName(x + "-" + y); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Skew(x, y).Save(output); diff --git a/tests/ImageSharp.Tests/Processors/Filters/VignetteTest.cs b/tests/ImageSharp.Tests/Processors/Filters/VignetteTest.cs index 89794aeaf..4191ae8fa 100644 --- a/tests/ImageSharp.Tests/Processors/Filters/VignetteTest.cs +++ b/tests/ImageSharp.Tests/Processors/Filters/VignetteTest.cs @@ -20,7 +20,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) { image.Vignette().Save(output); @@ -36,7 +36,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName("Color"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Vignette(Rgba32.HotPink).Save(output); @@ -52,7 +52,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName("Radius"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Vignette(image.Width / 4F, image.Height / 4F).Save(output); @@ -68,7 +68,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { string filename = file.GetFileName("InBox"); - using (Image image = file.CreateImage()) + using (Image image = file.CreateImage()) using (FileStream output = File.OpenWrite($"{path}/{filename}")) { image.Vignette(new Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2)) diff --git a/tests/ImageSharp.Tests/TestFile.cs b/tests/ImageSharp.Tests/TestFile.cs index eedc0d306..d95e9b1ac 100644 --- a/tests/ImageSharp.Tests/TestFile.cs +++ b/tests/ImageSharp.Tests/TestFile.cs @@ -12,6 +12,8 @@ namespace ImageSharp.Tests using System.Linq; using System.Reflection; + using ImageSharp.PixelFormats; + /// /// A test image file. /// @@ -30,7 +32,7 @@ namespace ImageSharp.Tests /// /// The image. /// - private readonly Image image; + private readonly Image image; /// /// The file. @@ -46,7 +48,7 @@ namespace ImageSharp.Tests this.file = file; this.Bytes = File.ReadAllBytes(file); - this.image = Image.Load(this.Bytes); + this.image = Image.Load(this.Bytes); } /// @@ -125,9 +127,9 @@ namespace ImageSharp.Tests /// /// The . /// - public Image CreateImage() + public Image CreateImage() { - return new Image(this.image); + return new Image(this.image); } /// @@ -137,9 +139,9 @@ namespace ImageSharp.Tests /// /// The . /// - public Image CreateImage(IDecoderOptions options) + public Image CreateImage(IDecoderOptions options) { - return Image.Load(this.Bytes, options); + return Image.Load(this.Bytes, options); } /// diff --git a/tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs b/tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs index 4a0950788..cb56d8528 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs @@ -23,7 +23,7 @@ namespace ImageSharp.Tests public virtual Image CreateImage(byte[] bytes) { - return Image.Load(bytes); + return Image.Load(bytes); } public virtual Image CreateImage(Image other) diff --git a/tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs b/tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs index c4d758bd6..b50675edf 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs @@ -9,14 +9,13 @@ namespace ImageSharp.Tests public class ImageFactory : GenericFactory { - public override Image CreateImage(byte[] bytes) => Image.Load(bytes); + public override Image CreateImage(byte[] bytes) => Image.Load(bytes); - public override Image CreateImage(int width, int height) => new Image(width, height); + public override Image CreateImage(int width, int height) => new Image(width, height); public override Image CreateImage(Image other) { - Image img = (Image)other; - return new Image(img); + return new Image(other); } } } diff --git a/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs b/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs index 77c13f125..f64b77271 100644 --- a/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs +++ b/tests/ImageSharp.Tests/TestUtilities/PixelTypes.cs @@ -53,7 +53,7 @@ namespace ImageSharp.Tests Short4 = 1 << 17, /// - /// Triggers instantiating the subclass of + /// Triggers instantiating the subclass of /// StandardImageClass = 1 << 29, diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs index 7d176c1e3..4d3a0d991 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestImageProviderTests.cs @@ -66,7 +66,7 @@ namespace ImageSharp.Tests { Image img = provider.GetImage(); - Assert.IsType(img); + Assert.IsType>(img); } [Theory] @@ -146,7 +146,7 @@ namespace ImageSharp.Tests Assert.Equal(img.Width, 3); if (provider.PixelType == PixelTypes.StandardImageClass) { - Assert.IsType(img); + Assert.IsType>(img); } } From b4739588824b40f7347c928bdd1e71c3419af2b4 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 5 May 2017 16:02:50 +1000 Subject: [PATCH 02/34] Promote Rgba32 to root namespace --- README.md | 23 ++++++++++--------- src/ImageSharp/PixelFormats/README.md | 6 ++++- .../Rgba32.ColorspaceTransforms.cs | 2 +- .../PixelFormats/Rgba32.Definitions.cs | 4 +++- .../PixelFormats/Rgba32.PixelOperations.cs | 4 +++- src/ImageSharp/PixelFormats/Rgba32.cs | 4 +++- 6 files changed, 27 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 4d079de39..cbe3dcb6b 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -# ImageSharp ImageSharp +# ImageSharp ImageSharp **ImageSharp** is a new, fully featured, fully managed, cross-platform, 2D graphics API designed to allow the processing of images without the use of `System.Drawing`. @@ -39,8 +39,8 @@ The **ImageSharp** library is made up of multiple packages. Packages include: - **ImageSharp** - - Contains the Image classes, PixelFormats, Primitives, Configuration, and other core functionality. - - The IImageFormat interface, Jpeg, Png, Bmp, and Gif formats. + - Contains the generic `Image` class, PixelFormats, Primitives, Configuration, and other core functionality. + - The `IImageFormat` interface, Jpeg, Png, Bmp, and Gif formats. - Transform methods like Resize, Crop, Skew, Rotate - Anything that alters the dimensions of the image. - Non-transform methods like Gaussian Blur, Pixelate, Edge Detection - Anything that maintains the original image dimensions. @@ -77,13 +77,15 @@ Without the constraints of `System.Drawing` We have been able to develop somethi Gone are system-wide process-locks; ImageSharp images are thread-safe and fully supported in web environments. -Many `Image` methods are also fluent. +Many `Image` methods are also fluent. Here's an example of the code required to resize an image using the default Bicubic resampler then turn the colors into their grayscale equivalent using the BT709 standard matrix. +`Rgba32` is our default PixelFormat, equivalent to `System.Drawing Color`. + On platforms supporting netstandard 1.3+ ```csharp -using (Image image = Image.Load("foo.jpg")) +using (Image image = Image.Load("foo.jpg")) { image.Resize(image.Width / 2, image.Height / 2) .Grayscale() @@ -94,7 +96,7 @@ on netstandard 1.1 - 1.2 ```csharp using (FileStream stream = File.OpenRead("foo.jpg")) using (FileStream output = File.OpenWrite("bar.jpg")) -using (Image image = Image.Load(stream)) +using (Image image = Image.Load(stream)) { image.Resize(image.Width / 2, image.Height / 2) .Grayscale() @@ -105,15 +107,14 @@ using (Image image = Image.Load(stream)) Setting individual pixel values is perfomed as follows: ```csharp -using (image = new Image(400, 400) -using (var pixels = image.Lock()) +using (Image image = new Image(400, 400) +using (PixelAccessor pixels = image.Lock()) { - // Rgba32 is our default PixelFormat, equivalent to System.Drawing Color pixels[200, 200] = Rgba32.White; } ``` -For advanced usage the `Image` and `PixelAccessor` classes are available allowing developers to implement their own color models in the same manner as Microsoft XNA Game Studio and MonoGame. +For advanced usage there are multiple [PixelFormat implementations](https://github.com/JimBobSquarePants/ImageSharp/tree/master/src/ImageSharp/PixelFormats) available allowing developers to implement their own color models in the same manner as Microsoft XNA Game Studio and MonoGame. All in all this should allow image processing to be much more accessible to developers which has always been my goal from the start. @@ -121,7 +122,7 @@ All in all this should allow image processing to be much more accessible to deve Please... Spread the word, contribute algorithms, submit performance improvements, unit tests. -Performance is a biggie, if you know anything about the new vector types and can apply some fancy new stuff with that it would be awesome. +Performance is a biggie, if you know anything about the `System.Numerics.Vectors` types and can apply some fancy new stuff with that it would be awesome. There's a lot of developers out there who could write this stuff a lot better and faster than I and I would love to see what we collectively can come up with so please, if you can help in any way it would be most welcome and benificial for all. diff --git a/src/ImageSharp/PixelFormats/README.md b/src/ImageSharp/PixelFormats/README.md index 61500de68..c7aa01295 100644 --- a/src/ImageSharp/PixelFormats/README.md +++ b/src/ImageSharp/PixelFormats/README.md @@ -1,3 +1,7 @@ Pixel formats adapted and extended from: -https://github.com/MonoGame/MonoGame \ No newline at end of file +https://github.com/MonoGame/MonoGame + +Rgba32 is our default format. As such it positioned within the ImageSharp root namespace to ensure visibility of the format. + +All other pixel formats should be positioned within ImageSharp.PixelFormats to reduce intellisense burden. \ No newline at end of file diff --git a/src/ImageSharp/PixelFormats/Rgba32.ColorspaceTransforms.cs b/src/ImageSharp/PixelFormats/Rgba32.ColorspaceTransforms.cs index 45d3489b7..1dc2292b1 100644 --- a/src/ImageSharp/PixelFormats/Rgba32.ColorspaceTransforms.cs +++ b/src/ImageSharp/PixelFormats/Rgba32.ColorspaceTransforms.cs @@ -3,7 +3,7 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp.PixelFormats +namespace ImageSharp { using System; using System.Numerics; diff --git a/src/ImageSharp/PixelFormats/Rgba32.Definitions.cs b/src/ImageSharp/PixelFormats/Rgba32.Definitions.cs index be02d0875..ab4c2ea60 100644 --- a/src/ImageSharp/PixelFormats/Rgba32.Definitions.cs +++ b/src/ImageSharp/PixelFormats/Rgba32.Definitions.cs @@ -3,8 +3,10 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp.PixelFormats +namespace ImageSharp { + using ImageSharp.PixelFormats; + /// /// Provides standardized deifinitions for named colors. /// diff --git a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs index ff284e625..9745d0133 100644 --- a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs +++ b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs @@ -3,13 +3,15 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp.PixelFormats +namespace ImageSharp { using System; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; + using ImageSharp.PixelFormats; + /// /// Provides optimized overrides for bulk operations. /// diff --git a/src/ImageSharp/PixelFormats/Rgba32.cs b/src/ImageSharp/PixelFormats/Rgba32.cs index ec9d5157a..15a9ed0fd 100644 --- a/src/ImageSharp/PixelFormats/Rgba32.cs +++ b/src/ImageSharp/PixelFormats/Rgba32.cs @@ -3,12 +3,14 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp.PixelFormats +namespace ImageSharp { using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; + using ImageSharp.PixelFormats; + /// /// Packed pixel type containing four 8-bit unsigned normalized values ranging from 0 to 255. /// The color components are stored in red, green, blue, and alpha order. From 1df28ae430f56f69ca2f7e79f9fa25b6e534551c Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 5 May 2017 16:03:59 +1000 Subject: [PATCH 03/34] Bump version number --- src/ImageSharp.Drawing/ImageSharp.Drawing.csproj | 2 +- src/ImageSharp/ImageSharp.csproj | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj b/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj index 15b7df2a2..a3552a09c 100644 --- a/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj +++ b/src/ImageSharp.Drawing/ImageSharp.Drawing.csproj @@ -2,7 +2,7 @@ An extension to ImageSharp that allows the drawing of images, paths, and text. ImageSharp.Drawing - 1.0.0-alpha8 + 1.0.0-alpha9 James Jackson-South and contributors netstandard1.1 true diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index 16fff3212..0269e770f 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -2,7 +2,7 @@ A cross-platform library for the processing of image files; written in C# ImageSharp - 1.0.0-alpha8 + 1.0.0-alpha9 James Jackson-South and contributors netstandard1.3;netstandard1.1 true From 47710578f042c6fc37035ff7aa65f3630fde5073 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 5 May 2017 16:33:22 +1000 Subject: [PATCH 04/34] Fix tests --- .../TestUtilities/TestUtilityExtensions.cs | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs b/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs index de05e83a9..dfaf1c052 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestUtilityExtensions.cs @@ -25,26 +25,27 @@ namespace ImageSharp.Tests private static readonly Dictionary PixelTypes2ClrTypes = new Dictionary(); private static readonly PixelTypes[] AllConcretePixelTypes = GetAllPixelTypes() - .Except(new [] {PixelTypes.Undefined, PixelTypes.All }) + .Except(new[] { PixelTypes.Undefined, PixelTypes.All }) .ToArray(); static TestUtilityExtensions() { - string nameSpace = typeof(Rgba32).FullName; - nameSpace = nameSpace.Substring(0, nameSpace.Length - typeof(Rgba32).Name.Length - 1); - foreach (PixelTypes pt in AllConcretePixelTypes.Where(pt => pt != PixelTypes.StandardImageClass)) + // Add Rgba32 Our default. + Type defaultPixelFormatType = typeof(Rgba32); + PixelTypes2ClrTypes[PixelTypes.Rgba32] = defaultPixelFormatType; + ClrTypes2PixelTypes[defaultPixelFormatType] = PixelTypes.Rgba32; + + // Add PixelFormat types + string nameSpace = typeof(Alpha8).FullName; + nameSpace = nameSpace.Substring(0, nameSpace.Length - typeof(Alpha8).Name.Length - 1); + foreach (PixelTypes pt in AllConcretePixelTypes.Where(pt => pt != PixelTypes.StandardImageClass && pt != PixelTypes.Rgba32)) { - string typeName = $"{nameSpace}.{pt.ToString()}"; + string typeName = $"{nameSpace}.{pt}"; Type t = ImageSharpAssembly.GetType(typeName); - if (t == null) - { - throw new InvalidOperationException($"Could not find: {typeName}"); - } - - PixelTypes2ClrTypes[pt] = t; + PixelTypes2ClrTypes[pt] = t ?? throw new InvalidOperationException($"Could not find: {typeName}"); ClrTypes2PixelTypes[t] = pt; } - PixelTypes2ClrTypes[PixelTypes.StandardImageClass] = typeof(Rgba32); + PixelTypes2ClrTypes[PixelTypes.StandardImageClass] = defaultPixelFormatType; } public static bool HasFlag(this PixelTypes pixelTypes, PixelTypes flag) => (pixelTypes & flag) == flag; From b9f3b50667ed18a6e26905a746df4ee405072837 Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Fri, 5 May 2017 09:13:39 +0200 Subject: [PATCH 05/34] Fixed namespace --- .../TestUtilities/Tests/TestUtilityExtensionsTests.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs index 29623e1b5..4dcfa31d8 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs @@ -53,7 +53,7 @@ namespace ImageSharp.Tests [Fact] public void Baz() { - Type type = typeof(Rgba32).GetTypeInfo().Assembly.GetType("ImageSharp.PixelFormats.Rgba32"); + Type type = typeof(Rgba32).GetTypeInfo().Assembly.GetType("ImageSharp.Rgba32"); this.Output.WriteLine(type.ToString()); Type fake = typeof(Rgba32).GetTypeInfo().Assembly.GetType("ImageSharp.dsaada_DASqewrr"); @@ -137,4 +137,4 @@ namespace ImageSharp.Tests AssertContainsPixelType(PixelTypes.StandardImageClass, expanded); } } -} \ No newline at end of file +} From 8dbf34b12d39bcd9b057f5b413122b825f17f84d Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 5 May 2017 17:41:36 +1000 Subject: [PATCH 06/34] Use better static format for Load etc. --- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 2 +- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 2 +- .../Formats/Jpeg/JpegDecoderCore.cs | 2 +- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 2 +- ...mage{TPixel}.Create.cs => Image.Create.cs} | 15 ++-- ...mage{TPixel}.Decode.cs => Image.Decode.cs} | 9 ++- ...Pixel}.FromBytes.cs => Image.FromBytes.cs} | 41 ++++++---- ...{TPixel}.FromFile.cs => Image.FromFile.cs} | 39 ++++++---- ...xel}.FromStream.cs => Image.FromStream.cs} | 49 +++++++----- src/ImageSharp/Image/Image{TPixel}.cs | 2 +- .../MetaData/Profiles/Exif/ExifProfile.cs | 2 +- .../ImageSharp.Benchmarks/Image/DecodeBmp.cs | 4 +- .../Image/DecodeFilteredPng.cs | 4 +- .../ImageSharp.Benchmarks/Image/DecodeGif.cs | 4 +- .../ImageSharp.Benchmarks/Image/DecodeJpeg.cs | 4 +- .../Image/DecodeJpegMultiple.cs | 4 +- .../ImageSharp.Benchmarks/Image/DecodePng.cs | 4 +- .../ImageSharp.Benchmarks/Image/EncodeBmp.cs | 4 +- .../ImageSharp.Benchmarks/Image/EncodeGif.cs | 4 +- .../Image/EncodeIndexedPng.cs | 5 +- .../ImageSharp.Benchmarks/Image/EncodeJpeg.cs | 4 +- .../ImageSharp.Benchmarks/Image/EncodePng.cs | 5 +- .../Image/MultiImageBenchmarkBase.cs | 4 +- .../Samplers/DetectEdges.cs | 6 +- .../ImageSharp.Tests/Drawing/DrawImageTest.cs | 2 +- .../Formats/GeneralFormatTests.cs | 2 +- .../Formats/Gif/GifEncoderTests.cs | 6 +- .../Formats/Jpg/JpegEncoderTests.cs | 4 +- .../Formats/Jpg/JpegProfilingBenchmarks.cs | 2 +- .../Formats/Png/PngSmokeTests.cs | 6 +- .../ImageSharp.Tests/Image/ImageLoadTests.cs | 74 +++++++++---------- tests/ImageSharp.Tests/Image/ImageTests.cs | 10 +-- .../Profiles/Exif/ExifProfileTests.cs | 6 +- tests/ImageSharp.Tests/TestFile.cs | 4 +- .../TestUtilities/Factories/GenericFactory.cs | 2 +- .../TestUtilities/Factories/ImageFactory.cs | 2 +- .../Tests/TestUtilityExtensionsTests.cs | 2 +- 37 files changed, 191 insertions(+), 152 deletions(-) rename src/ImageSharp/Image/{Image{TPixel}.Create.cs => Image.Create.cs} (71%) rename src/ImageSharp/Image/{Image{TPixel}.Decode.cs => Image.Decode.cs} (88%) rename src/ImageSharp/Image/{Image{TPixel}.FromBytes.cs => Image.FromBytes.cs} (63%) rename src/ImageSharp/Image/{Image{TPixel}.FromFile.cs => Image.FromFile.cs} (70%) rename src/ImageSharp/Image/{Image{TPixel}.FromStream.cs => Image.FromStream.cs} (70%) diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index dff53d77f..a9aac5efa 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -127,7 +127,7 @@ namespace ImageSharp.Formats + $"bigger then the max allowed size '{Image.MaxWidth}x{Image.MaxHeight}'"); } - Image image = Image.Create(this.infoHeader.Width, this.infoHeader.Height, this.configuration); + Image image = Image.Create(this.infoHeader.Width, this.infoHeader.Height, this.configuration); using (PixelAccessor pixels = image.Lock()) { switch (this.infoHeader.Compression) diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 8a37ed7bc..589b7037a 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -366,7 +366,7 @@ namespace ImageSharp.Formats this.metaData.Quality = colorTableLength / 3; // This initializes the image to become fully transparent because the alpha channel is zero. - this.image = Image.Create(imageWidth, imageHeight, this.metaData, this.configuration); + this.image = Image.Create(imageWidth, imageHeight, this.metaData, this.configuration); this.SetFrameMetaData(this.metaData); diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 359e345ef..9df21a3b7 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -482,7 +482,7 @@ namespace ImageSharp.Formats private Image ConvertJpegPixelsToImagePixels(ImageMetaData metadata) where TPixel : struct, IPixel { - Image image = Image.Create(this.ImageWidth, this.ImageHeight, metadata, this.configuration); + Image image = Image.Create(this.ImageWidth, this.ImageHeight, metadata, this.configuration); if (this.grayImage.IsInitialized) { diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index f3715d68b..904aa1ff6 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -335,7 +335,7 @@ namespace ImageSharp.Formats throw new ArgumentOutOfRangeException($"The input png '{this.header.Width}x{this.header.Height}' is bigger than the max allowed size '{Image.MaxWidth}x{Image.MaxHeight}'"); } - image = Image.Create(this.header.Width, this.header.Height, metadata, this.configuration); + image = Image.Create(this.header.Width, this.header.Height, metadata, this.configuration); pixels = image.Lock(); this.bytesPerPixel = this.CalculateBytesPerPixel(); this.bytesPerScanline = this.CalculateScanlineLength(this.header.Width) + 1; diff --git a/src/ImageSharp/Image/Image{TPixel}.Create.cs b/src/ImageSharp/Image/Image.Create.cs similarity index 71% rename from src/ImageSharp/Image/Image{TPixel}.Create.cs rename to src/ImageSharp/Image/Image.Create.cs index 8a64fd748..6a5762cc9 100644 --- a/src/ImageSharp/Image/Image{TPixel}.Create.cs +++ b/src/ImageSharp/Image/Image.Create.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // @@ -10,8 +10,7 @@ namespace ImageSharp /// /// Adds static methods allowing the creation of new images from given dimensions. /// - public partial class Image - where TPixel : struct, IPixel + public partial class Image { /// /// Create a new instance of the class with the given height and the width. @@ -21,12 +20,14 @@ namespace ImageSharp /// /// The configuration providing initialization code which allows extending the library. /// + /// The pixel format. /// /// A new . /// - internal static Image Create(int width, int height, Configuration configuration) + internal static Image Create(int width, int height, Configuration configuration) + where TPixel : struct, IPixel { - return Create(width, height, null, configuration); + return Create(width, height, null, configuration); } /// @@ -38,10 +39,12 @@ namespace ImageSharp /// /// The configuration providing initialization code which allows extending the library. /// + /// The pixel format. /// /// A new . /// - internal static Image Create(int width, int height, ImageMetaData metadata, Configuration configuration) + internal static Image Create(int width, int height, ImageMetaData metadata, Configuration configuration) + where TPixel : struct, IPixel { return new Image(width, height, metadata, configuration); } diff --git a/src/ImageSharp/Image/Image{TPixel}.Decode.cs b/src/ImageSharp/Image/Image.Decode.cs similarity index 88% rename from src/ImageSharp/Image/Image{TPixel}.Decode.cs rename to src/ImageSharp/Image/Image.Decode.cs index bef55ecda..249740118 100644 --- a/src/ImageSharp/Image/Image{TPixel}.Decode.cs +++ b/src/ImageSharp/Image/Image.Decode.cs @@ -15,8 +15,7 @@ namespace ImageSharp /// /// Adds static methods allowing the decoding of new images. /// - public partial class Image - where TPixel : struct, IPixel + public partial class Image { /// /// By reading the header on the provided stream this calculates the images format. @@ -56,10 +55,12 @@ namespace ImageSharp /// The stream. /// The options for the decoder. /// the configuration. + /// The pixel format. /// - /// The decoded image + /// A new . /// - private static Image Decode(Stream stream, IDecoderOptions options, Configuration config) + private static Image Decode(Stream stream, IDecoderOptions options, Configuration config) + where TPixel : struct, IPixel { IImageFormat format = DiscoverFormat(stream, config); if (format == null) diff --git a/src/ImageSharp/Image/Image{TPixel}.FromBytes.cs b/src/ImageSharp/Image/Image.FromBytes.cs similarity index 63% rename from src/ImageSharp/Image/Image{TPixel}.FromBytes.cs rename to src/ImageSharp/Image/Image.FromBytes.cs index e401d9d58..0cc05c266 100644 --- a/src/ImageSharp/Image/Image{TPixel}.FromBytes.cs +++ b/src/ImageSharp/Image/Image.FromBytes.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // @@ -13,17 +13,18 @@ namespace ImageSharp /// /// Adds static methods allowing the creation of new image from a byte array. /// - public partial class Image - where TPixel : struct, IPixel + public partial class Image { /// /// Create a new instance of the class from the given byte array. /// /// The byte array containing image data. + /// The pixel format. /// A new . - public static Image Load(byte[] data) + public static Image Load(byte[] data) + where TPixel : struct, IPixel { - return Load(null, data, null); + return Load(null, data, null); } /// @@ -31,10 +32,12 @@ namespace ImageSharp /// /// The byte array containing image data. /// The options for the decoder. + /// The pixel format. /// A new . - public static Image Load(byte[] data, IDecoderOptions options) + public static Image Load(byte[] data, IDecoderOptions options) + where TPixel : struct, IPixel { - return Load(null, data, options); + return Load(null, data, options); } /// @@ -42,10 +45,12 @@ namespace ImageSharp /// /// The config for the decoder. /// The byte array containing image data. + /// The pixel format. /// A new . - public static Image Load(Configuration config, byte[] data) + public static Image Load(Configuration config, byte[] data) + where TPixel : struct, IPixel { - return Load(config, data, null); + return Load(config, data, null); } /// @@ -53,10 +58,12 @@ namespace ImageSharp /// /// The byte array containing image data. /// The decoder. + /// The pixel format. /// A new . - public static Image Load(byte[] data, IImageDecoder decoder) + public static Image Load(byte[] data, IImageDecoder decoder) + where TPixel : struct, IPixel { - return Load(data, decoder, null); + return Load(data, decoder, null); } /// @@ -65,12 +72,14 @@ namespace ImageSharp /// The configuration options. /// The byte array containing image data. /// The options for the decoder. + /// The pixel format. /// A new . - public static Image Load(Configuration config, byte[] data, IDecoderOptions options) + public static Image Load(Configuration config, byte[] data, IDecoderOptions options) + where TPixel : struct, IPixel { using (MemoryStream ms = new MemoryStream(data)) { - return Load(config, ms, options); + return Load(config, ms, options); } } @@ -80,12 +89,14 @@ namespace ImageSharp /// The byte array containing image data. /// The decoder. /// The options for the decoder. + /// The pixel format. /// A new . - public static Image Load(byte[] data, IImageDecoder decoder, IDecoderOptions options) + public static Image Load(byte[] data, IImageDecoder decoder, IDecoderOptions options) + where TPixel : struct, IPixel { using (MemoryStream ms = new MemoryStream(data)) { - return Load(ms, decoder, options); + return Load(ms, decoder, options); } } } diff --git a/src/ImageSharp/Image/Image{TPixel}.FromFile.cs b/src/ImageSharp/Image/Image.FromFile.cs similarity index 70% rename from src/ImageSharp/Image/Image{TPixel}.FromFile.cs rename to src/ImageSharp/Image/Image.FromFile.cs index 0c6431407..2dcb26bdb 100644 --- a/src/ImageSharp/Image/Image{TPixel}.FromFile.cs +++ b/src/ImageSharp/Image/Image.FromFile.cs @@ -14,8 +14,7 @@ namespace ImageSharp /// /// Adds static methods allowing the creation of new image from a given file. /// - public partial class Image - where TPixel : struct, IPixel + public partial class Image { /// /// Create a new instance of the class from the given file. @@ -24,10 +23,12 @@ namespace ImageSharp /// /// Thrown if the stream is not readable nor seekable. /// + /// The pixel format. /// A new . - public static Image Load(string path) + public static Image Load(string path) + where TPixel : struct, IPixel { - return Load(null, path, null); + return Load(null, path, null); } /// @@ -38,10 +39,12 @@ namespace ImageSharp /// /// Thrown if the stream is not readable nor seekable. /// + /// The pixel format. /// A new . - public static Image Load(string path, IDecoderOptions options) + public static Image Load(string path, IDecoderOptions options) + where TPixel : struct, IPixel { - return Load(null, path, options); + return Load(null, path, options); } /// @@ -52,10 +55,12 @@ namespace ImageSharp /// /// Thrown if the stream is not readable nor seekable. /// + /// The pixel format. /// A new . - public static Image Load(Configuration config, string path) + public static Image Load(Configuration config, string path) + where TPixel : struct, IPixel { - return Load(config, path, null); + return Load(config, path, null); } /// @@ -66,10 +71,12 @@ namespace ImageSharp /// /// Thrown if the stream is not readable nor seekable. /// + /// The pixel format. /// A new . - public static Image Load(string path, IImageDecoder decoder) + public static Image Load(string path, IImageDecoder decoder) + where TPixel : struct, IPixel { - return Load(path, decoder, null); + return Load(path, decoder, null); } /// @@ -81,13 +88,15 @@ namespace ImageSharp /// /// Thrown if the stream is not readable nor seekable. /// + /// The pixel format. /// A new . - public static Image Load(Configuration config, string path, IDecoderOptions options) + public static Image Load(Configuration config, string path, IDecoderOptions options) + where TPixel : struct, IPixel { config = config ?? Configuration.Default; using (Stream s = config.FileSystem.OpenRead(path)) { - return Load(config, s, options); + return Load(config, s, options); } } @@ -100,13 +109,15 @@ namespace ImageSharp /// /// Thrown if the stream is not readable nor seekable. /// + /// The pixel format. /// A new . - public static Image Load(string path, IImageDecoder decoder, IDecoderOptions options) + public static Image Load(string path, IImageDecoder decoder, IDecoderOptions options) + where TPixel : struct, IPixel { Configuration config = Configuration.Default; using (Stream s = config.FileSystem.OpenRead(path)) { - return Load(s, decoder, options); + return Load(s, decoder, options); } } } diff --git a/src/ImageSharp/Image/Image{TPixel}.FromStream.cs b/src/ImageSharp/Image/Image.FromStream.cs similarity index 70% rename from src/ImageSharp/Image/Image{TPixel}.FromStream.cs rename to src/ImageSharp/Image/Image.FromStream.cs index fabd02ca8..a120346a1 100644 --- a/src/ImageSharp/Image/Image{TPixel}.FromStream.cs +++ b/src/ImageSharp/Image/Image.FromStream.cs @@ -15,8 +15,7 @@ namespace ImageSharp /// /// Adds static methods allowing the creation of new image from a given stream. /// - public partial class Image - where TPixel : struct, IPixel + public partial class Image { /// /// Create a new instance of the class from the given stream. @@ -25,10 +24,12 @@ namespace ImageSharp /// /// Thrown if the stream is not readable nor seekable. /// - /// The image - public static Image Load(Stream stream) + /// The pixel format. + /// A new .> + public static Image Load(Stream stream) + where TPixel : struct, IPixel { - return Load(null, stream, null); + return Load(null, stream, null); } /// @@ -39,10 +40,12 @@ namespace ImageSharp /// /// Thrown if the stream is not readable nor seekable. /// - /// The image - public static Image Load(Stream stream, IDecoderOptions options) + /// The pixel format. + /// A new .> + public static Image Load(Stream stream, IDecoderOptions options) + where TPixel : struct, IPixel { - return Load(null, stream, options); + return Load(null, stream, options); } /// @@ -53,10 +56,12 @@ namespace ImageSharp /// /// Thrown if the stream is not readable nor seekable. /// - /// The image - public static Image Load(Configuration config, Stream stream) + /// The pixel format. + /// A new .> + public static Image Load(Configuration config, Stream stream) + where TPixel : struct, IPixel { - return Load(config, stream, null); + return Load(config, stream, null); } /// @@ -67,10 +72,12 @@ namespace ImageSharp /// /// Thrown if the stream is not readable nor seekable. /// - /// The image - public static Image Load(Stream stream, IImageDecoder decoder) + /// The pixel format. + /// A new .> + public static Image Load(Stream stream, IImageDecoder decoder) + where TPixel : struct, IPixel { - return Load(stream, decoder, null); + return Load(stream, decoder, null); } /// @@ -82,8 +89,10 @@ namespace ImageSharp /// /// Thrown if the stream is not readable nor seekable. /// - /// The image - public static Image Load(Stream stream, IImageDecoder decoder, IDecoderOptions options) + /// The pixel format. + /// A new .> + public static Image Load(Stream stream, IImageDecoder decoder, IDecoderOptions options) + where TPixel : struct, IPixel { return WithSeekableStream(stream, s => decoder.Decode(Configuration.Default, s, options)); } @@ -97,11 +106,13 @@ namespace ImageSharp /// /// Thrown if the stream is not readable nor seekable. /// - /// The image - public static Image Load(Configuration config, Stream stream, IDecoderOptions options) + /// The pixel format. + /// A new .> + public static Image Load(Configuration config, Stream stream, IDecoderOptions options) + where TPixel : struct, IPixel { config = config ?? Configuration.Default; - Image img = WithSeekableStream(stream, s => Decode(s, options, config)); + Image img = WithSeekableStream(stream, s => Decode(s, options, config)); if (img != null) { diff --git a/src/ImageSharp/Image/Image{TPixel}.cs b/src/ImageSharp/Image/Image{TPixel}.cs index ce8aecfea..9e103c700 100644 --- a/src/ImageSharp/Image/Image{TPixel}.cs +++ b/src/ImageSharp/Image/Image{TPixel}.cs @@ -22,7 +22,7 @@ namespace ImageSharp /// /// The pixel format. [DebuggerDisplay("Image: {Width}x{Height}")] - public partial class Image : ImageBase, IImage + public class Image : ImageBase, IImage where TPixel : struct, IPixel { /// diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs index a65a9e80e..b270caf5d 100644 --- a/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs +++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs @@ -138,7 +138,7 @@ namespace ImageSharp using (MemoryStream memStream = new MemoryStream(this.data, this.thumbnailOffset, this.thumbnailLength)) { - return Image.Load(memStream); + return Image.Load(memStream); } } diff --git a/tests/ImageSharp.Benchmarks/Image/DecodeBmp.cs b/tests/ImageSharp.Benchmarks/Image/DecodeBmp.cs index d14f3c17e..87baa8b7e 100644 --- a/tests/ImageSharp.Benchmarks/Image/DecodeBmp.cs +++ b/tests/ImageSharp.Benchmarks/Image/DecodeBmp.cs @@ -10,7 +10,7 @@ namespace ImageSharp.Benchmarks.Image using BenchmarkDotNet.Attributes; - using ImageSharp.PixelFormats; + using CoreImage = ImageSharp.Image; using CoreSize = ImageSharp.Size; @@ -44,7 +44,7 @@ namespace ImageSharp.Benchmarks.Image { using (MemoryStream memoryStream = new MemoryStream(this.bmpBytes)) { - using (Image image = Image.Load(memoryStream)) + using (Image image = CoreImage.Load(memoryStream)) { return new CoreSize(image.Width, image.Height); } diff --git a/tests/ImageSharp.Benchmarks/Image/DecodeFilteredPng.cs b/tests/ImageSharp.Benchmarks/Image/DecodeFilteredPng.cs index fd86324ca..a1fddc502 100644 --- a/tests/ImageSharp.Benchmarks/Image/DecodeFilteredPng.cs +++ b/tests/ImageSharp.Benchmarks/Image/DecodeFilteredPng.cs @@ -10,7 +10,7 @@ namespace ImageSharp.Benchmarks.Image using BenchmarkDotNet.Attributes; using ImageSharp; - using ImageSharp.PixelFormats; + using CoreImage = ImageSharp.Image; public class DecodeFilteredPng : BenchmarkBase { @@ -32,7 +32,7 @@ namespace ImageSharp.Benchmarks.Image private Size LoadPng(MemoryStream stream) { - using (Image image = Image.Load(stream)) + using (Image image = CoreImage.Load(stream)) { return new Size(image.Width, image.Height); } diff --git a/tests/ImageSharp.Benchmarks/Image/DecodeGif.cs b/tests/ImageSharp.Benchmarks/Image/DecodeGif.cs index 94b04b904..02620fe74 100644 --- a/tests/ImageSharp.Benchmarks/Image/DecodeGif.cs +++ b/tests/ImageSharp.Benchmarks/Image/DecodeGif.cs @@ -10,7 +10,7 @@ namespace ImageSharp.Benchmarks.Image using BenchmarkDotNet.Attributes; - using ImageSharp.PixelFormats; + using CoreImage = ImageSharp.Image; using CoreSize = ImageSharp.Size; @@ -44,7 +44,7 @@ namespace ImageSharp.Benchmarks.Image { using (MemoryStream memoryStream = new MemoryStream(this.gifBytes)) { - using (Image image = Image.Load(memoryStream)) + using (Image image = CoreImage.Load(memoryStream)) { return new CoreSize(image.Width, image.Height); } diff --git a/tests/ImageSharp.Benchmarks/Image/DecodeJpeg.cs b/tests/ImageSharp.Benchmarks/Image/DecodeJpeg.cs index 7aa98f985..ab45f95f4 100644 --- a/tests/ImageSharp.Benchmarks/Image/DecodeJpeg.cs +++ b/tests/ImageSharp.Benchmarks/Image/DecodeJpeg.cs @@ -10,7 +10,7 @@ namespace ImageSharp.Benchmarks.Image using BenchmarkDotNet.Attributes; - using ImageSharp.PixelFormats; + using CoreImage = ImageSharp.Image; using CoreSize = ImageSharp.Size; @@ -44,7 +44,7 @@ namespace ImageSharp.Benchmarks.Image { using (MemoryStream memoryStream = new MemoryStream(this.jpegBytes)) { - using (Image image = Image.Load(memoryStream)) + using (Image image = CoreImage.Load(memoryStream)) { return new CoreSize(image.Width, image.Height); } diff --git a/tests/ImageSharp.Benchmarks/Image/DecodeJpegMultiple.cs b/tests/ImageSharp.Benchmarks/Image/DecodeJpegMultiple.cs index 023740691..44c90d253 100644 --- a/tests/ImageSharp.Benchmarks/Image/DecodeJpegMultiple.cs +++ b/tests/ImageSharp.Benchmarks/Image/DecodeJpegMultiple.cs @@ -8,7 +8,7 @@ namespace ImageSharp.Benchmarks.Image using System.Collections.Generic; using BenchmarkDotNet.Attributes; - using ImageSharp.PixelFormats; + using CoreImage = ImageSharp.Image; [Config(typeof(Config.Short))] public class DecodeJpegMultiple : MultiImageBenchmarkBase @@ -24,7 +24,7 @@ namespace ImageSharp.Benchmarks.Image public void DecodeJpegImageSharp() { this.ForEachStream( - ms => Image.Load(ms) + ms => CoreImage.Load(ms) ); } diff --git a/tests/ImageSharp.Benchmarks/Image/DecodePng.cs b/tests/ImageSharp.Benchmarks/Image/DecodePng.cs index 2010b90e1..9b71fd058 100644 --- a/tests/ImageSharp.Benchmarks/Image/DecodePng.cs +++ b/tests/ImageSharp.Benchmarks/Image/DecodePng.cs @@ -10,7 +10,7 @@ namespace ImageSharp.Benchmarks.Image using BenchmarkDotNet.Attributes; - using ImageSharp.PixelFormats; + using CoreImage = ImageSharp.Image; using CoreSize = ImageSharp.Size; @@ -44,7 +44,7 @@ namespace ImageSharp.Benchmarks.Image { using (MemoryStream memoryStream = new MemoryStream(this.pngBytes)) { - using (Image image = Image.Load(memoryStream)) + using (Image image = CoreImage.Load(memoryStream)) { return new CoreSize(image.Width, image.Height); } diff --git a/tests/ImageSharp.Benchmarks/Image/EncodeBmp.cs b/tests/ImageSharp.Benchmarks/Image/EncodeBmp.cs index a23fce9ea..52a039912 100644 --- a/tests/ImageSharp.Benchmarks/Image/EncodeBmp.cs +++ b/tests/ImageSharp.Benchmarks/Image/EncodeBmp.cs @@ -11,7 +11,7 @@ namespace ImageSharp.Benchmarks.Image using BenchmarkDotNet.Attributes; - using ImageSharp.PixelFormats; + using CoreImage = ImageSharp.Image; public class EncodeBmp : BenchmarkBase { @@ -26,7 +26,7 @@ namespace ImageSharp.Benchmarks.Image if (this.bmpStream == null) { this.bmpStream = File.OpenRead("../ImageSharp.Tests/TestImages/Formats/Bmp/Car.bmp"); - this.bmpCore = Image.Load(this.bmpStream); + this.bmpCore = CoreImage.Load(this.bmpStream); this.bmpStream.Position = 0; this.bmpDrawing = Image.FromStream(this.bmpStream); } diff --git a/tests/ImageSharp.Benchmarks/Image/EncodeGif.cs b/tests/ImageSharp.Benchmarks/Image/EncodeGif.cs index da22b156c..5eaa8940b 100644 --- a/tests/ImageSharp.Benchmarks/Image/EncodeGif.cs +++ b/tests/ImageSharp.Benchmarks/Image/EncodeGif.cs @@ -11,7 +11,7 @@ namespace ImageSharp.Benchmarks.Image using BenchmarkDotNet.Attributes; - using ImageSharp.PixelFormats; + using CoreImage = ImageSharp.Image; public class EncodeGif : BenchmarkBase { @@ -26,7 +26,7 @@ namespace ImageSharp.Benchmarks.Image if (this.bmpStream == null) { this.bmpStream = File.OpenRead("../ImageSharp.Tests/TestImages/Formats/Bmp/Car.bmp"); - this.bmpCore = Image.Load(this.bmpStream); + this.bmpCore = CoreImage.Load(this.bmpStream); this.bmpStream.Position = 0; this.bmpDrawing = Image.FromStream(this.bmpStream); } diff --git a/tests/ImageSharp.Benchmarks/Image/EncodeIndexedPng.cs b/tests/ImageSharp.Benchmarks/Image/EncodeIndexedPng.cs index 5f3b0e860..4d25c82ef 100644 --- a/tests/ImageSharp.Benchmarks/Image/EncodeIndexedPng.cs +++ b/tests/ImageSharp.Benchmarks/Image/EncodeIndexedPng.cs @@ -11,9 +11,10 @@ namespace ImageSharp.Benchmarks.Image using ImageSharp; using ImageSharp.Formats; - using ImageSharp.PixelFormats; using ImageSharp.Quantizers; + using CoreImage = ImageSharp.Image; + /// /// Benchmarks saving png files using different quantizers. System.Drawing cannot save indexed png files so we cannot compare. /// @@ -35,7 +36,7 @@ namespace ImageSharp.Benchmarks.Image ? "../ImageSharp.Tests/TestImages/Formats/Jpg/baseline/jpeg420exif.jpg" : "../ImageSharp.Tests/TestImages/Formats/Bmp/Car.bmp"; this.bmpStream = File.OpenRead(path); - this.bmpCore = Image.Load(this.bmpStream); + this.bmpCore = CoreImage.Load(this.bmpStream); this.bmpStream.Position = 0; } } diff --git a/tests/ImageSharp.Benchmarks/Image/EncodeJpeg.cs b/tests/ImageSharp.Benchmarks/Image/EncodeJpeg.cs index 7c1fcf662..efd4e8ac5 100644 --- a/tests/ImageSharp.Benchmarks/Image/EncodeJpeg.cs +++ b/tests/ImageSharp.Benchmarks/Image/EncodeJpeg.cs @@ -11,7 +11,7 @@ namespace ImageSharp.Benchmarks.Image using BenchmarkDotNet.Attributes; - using ImageSharp.PixelFormats; + using CoreImage = ImageSharp.Image; public class EncodeJpeg : BenchmarkBase { @@ -26,7 +26,7 @@ namespace ImageSharp.Benchmarks.Image if (this.bmpStream == null) { this.bmpStream = File.OpenRead("../ImageSharp.Tests/TestImages/Formats/Bmp/Car.bmp"); - this.bmpCore = Image.Load(this.bmpStream); + this.bmpCore = CoreImage.Load(this.bmpStream); this.bmpStream.Position = 0; this.bmpDrawing = Image.FromStream(this.bmpStream); } diff --git a/tests/ImageSharp.Benchmarks/Image/EncodePng.cs b/tests/ImageSharp.Benchmarks/Image/EncodePng.cs index ac50916bf..11182ac48 100644 --- a/tests/ImageSharp.Benchmarks/Image/EncodePng.cs +++ b/tests/ImageSharp.Benchmarks/Image/EncodePng.cs @@ -12,9 +12,10 @@ namespace ImageSharp.Benchmarks.Image using BenchmarkDotNet.Attributes; using ImageSharp.Formats; - using ImageSharp.PixelFormats; using ImageSharp.Quantizers; + using CoreImage = ImageSharp.Image; + public class EncodePng : BenchmarkBase { // System.Drawing needs this. @@ -37,7 +38,7 @@ namespace ImageSharp.Benchmarks.Image ? "../ImageSharp.Tests/TestImages/Formats/Jpg/baseline/jpeg420exif.jpg" : "../ImageSharp.Tests/TestImages/Formats/Bmp/Car.bmp"; this.bmpStream = File.OpenRead(path); - this.bmpCore = Image.Load(this.bmpStream); + this.bmpCore = CoreImage.Load(this.bmpStream); this.bmpStream.Position = 0; this.bmpDrawing = Image.FromStream(this.bmpStream); } diff --git a/tests/ImageSharp.Benchmarks/Image/MultiImageBenchmarkBase.cs b/tests/ImageSharp.Benchmarks/Image/MultiImageBenchmarkBase.cs index 8cb73d6af..dfee978cc 100644 --- a/tests/ImageSharp.Benchmarks/Image/MultiImageBenchmarkBase.cs +++ b/tests/ImageSharp.Benchmarks/Image/MultiImageBenchmarkBase.cs @@ -15,7 +15,7 @@ namespace ImageSharp.Benchmarks.Image using BenchmarkDotNet.Attributes; - using ImageSharp.PixelFormats; + using CoreImage = ImageSharp.Image; public abstract class MultiImageBenchmarkBase : BenchmarkBase { @@ -154,7 +154,7 @@ namespace ImageSharp.Benchmarks.Image using (MemoryStream ms1 = new MemoryStream(bytes)) { - this.FileNamesToImageSharpImages[fn] = Image.Load(ms1); + this.FileNamesToImageSharpImages[fn] = CoreImage.Load(ms1); } diff --git a/tests/ImageSharp.Benchmarks/Samplers/DetectEdges.cs b/tests/ImageSharp.Benchmarks/Samplers/DetectEdges.cs index 36d985324..d4920ff08 100644 --- a/tests/ImageSharp.Benchmarks/Samplers/DetectEdges.cs +++ b/tests/ImageSharp.Benchmarks/Samplers/DetectEdges.cs @@ -9,10 +9,10 @@ namespace ImageSharp.Benchmarks using BenchmarkDotNet.Attributes; - using ImageSharp.PixelFormats; - using Processing; + using CoreImage = ImageSharp.Image; + public class DetectEdges : BenchmarkBase { private Image image; @@ -24,7 +24,7 @@ namespace ImageSharp.Benchmarks { using (FileStream stream = File.OpenRead("../ImageSharp.Tests/TestImages/Formats/Bmp/Car.bmp")) { - this.image = Image.Load(stream); + this.image = CoreImage.Load(stream); } } } diff --git a/tests/ImageSharp.Tests/Drawing/DrawImageTest.cs b/tests/ImageSharp.Tests/Drawing/DrawImageTest.cs index 2e5346fe6..030034a8f 100644 --- a/tests/ImageSharp.Tests/Drawing/DrawImageTest.cs +++ b/tests/ImageSharp.Tests/Drawing/DrawImageTest.cs @@ -37,7 +37,7 @@ namespace ImageSharp.Tests where TPixel : struct, IPixel { using (Image image = provider.GetImage()) - using (Image blend = Image.Load(TestFile.Create(TestImages.Bmp.Car).Bytes)) + using (Image blend = Image.Load(TestFile.Create(TestImages.Bmp.Car).Bytes)) { image.DrawImage(blend, mode, .75f, new Size(image.Width / 2, image.Height / 2), new Point(image.Width / 4, image.Height / 4)) .DebugSave(provider, new { mode }); diff --git a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs index 6cea08cdd..b47df8395 100644 --- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs +++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs @@ -152,7 +152,7 @@ namespace ImageSharp.Tests serialized = memoryStream.ToArray(); } - using (Image image2 = Image.Load(serialized)) + using (Image image2 = Image.Load(serialized)) using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) { image2.Save(output); diff --git a/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs index 96dc2ebf7..b0ffaaf85 100644 --- a/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Gif/GifEncoderTests.cs @@ -43,7 +43,7 @@ namespace ImageSharp.Tests input.Save(memStream, new GifFormat(), options); memStream.Position = 0; - using (Image output = Image.Load(memStream)) + using (Image output = Image.Load(memStream)) { Assert.Equal(1, output.MetaData.Properties.Count); Assert.Equal("Comments", output.MetaData.Properties[0].Name); @@ -70,7 +70,7 @@ namespace ImageSharp.Tests input.SaveAsGif(memStream, options); memStream.Position = 0; - using (Image output = Image.Load(memStream)) + using (Image output = Image.Load(memStream)) { Assert.Equal(0, output.MetaData.Properties.Count); } @@ -91,7 +91,7 @@ namespace ImageSharp.Tests input.Save(memStream, new GifFormat()); memStream.Position = 0; - using (Image output = Image.Load(memStream)) + using (Image output = Image.Load(memStream)) { Assert.Equal(1, output.MetaData.Properties.Count); Assert.Equal("Comments", output.MetaData.Properties[0].Name); diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.cs index 60a7d7486..1b4f3ea78 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegEncoderTests.cs @@ -89,7 +89,7 @@ namespace ImageSharp.Tests input.Save(memStream, new JpegFormat(), options); memStream.Position = 0; - using (Image output = Image.Load(memStream)) + using (Image output = Image.Load(memStream)) { Assert.NotNull(output.MetaData.ExifProfile); } @@ -114,7 +114,7 @@ namespace ImageSharp.Tests input.SaveAsJpeg(memStream, options); memStream.Position = 0; - using (Image output = Image.Load(memStream)) + using (Image output = Image.Load(memStream)) { Assert.Null(output.MetaData.ExifProfile); } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/JpegProfilingBenchmarks.cs b/tests/ImageSharp.Tests/Formats/Jpg/JpegProfilingBenchmarks.cs index de16e146a..b41826e2f 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/JpegProfilingBenchmarks.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/JpegProfilingBenchmarks.cs @@ -51,7 +51,7 @@ namespace ImageSharp.Tests ExecutionCount, () => { - Image img = Image.Load(bytes); + Image img = Image.Load(bytes); }, // ReSharper disable once ExplicitCallerInfoArgument $"Decode {fileName}"); diff --git a/tests/ImageSharp.Tests/Formats/Png/PngSmokeTests.cs b/tests/ImageSharp.Tests/Formats/Png/PngSmokeTests.cs index 20eb22bfb..22bb0b244 100644 --- a/tests/ImageSharp.Tests/Formats/Png/PngSmokeTests.cs +++ b/tests/ImageSharp.Tests/Formats/Png/PngSmokeTests.cs @@ -32,7 +32,7 @@ namespace ImageSharp.Tests.Formats.Png image.Save(ms, new PngEncoder()); ms.Position = 0; - using (Image img2 = Image.Load(ms, new PngDecoder())) + using (Image img2 = Image.Load(ms, new PngDecoder())) { // img2.Save(provider.Utility.GetTestOutputFileName("bmp", "_loaded"), new BmpEncoder()); ImageComparer.CheckSimilarity(image, img2); @@ -53,7 +53,7 @@ namespace ImageSharp.Tests.Formats.Png image.MetaData.Quality = 256; image.Save(ms, new PngEncoder()); ms.Position = 0; - using (Image img2 = Image.Load(ms, new PngDecoder())) + using (Image img2 = Image.Load(ms, new PngDecoder())) { // img2.Save(provider.Utility.GetTestOutputFileName("bmp", "_loaded"), new BmpEncoder()); ImageComparer.CheckSimilarity(image, img2, 0.03f); @@ -119,7 +119,7 @@ namespace ImageSharp.Tests.Formats.Png image.Save(ms, new PngEncoder()); ms.Position = 0; - using (Image img2 = Image.Load(ms, new PngDecoder())) + using (Image img2 = Image.Load(ms, new PngDecoder())) { ImageComparer.CheckSimilarity(image, img2); } diff --git a/tests/ImageSharp.Tests/Image/ImageLoadTests.cs b/tests/ImageSharp.Tests/Image/ImageLoadTests.cs index af1449dae..4cdf529e6 100644 --- a/tests/ImageSharp.Tests/Image/ImageLoadTests.cs +++ b/tests/ImageSharp.Tests/Image/ImageLoadTests.cs @@ -77,7 +77,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromStream() { - Image img = Image.Load(this.DataStream); + Image img = Image.Load(this.DataStream); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat, img.CurrentImageFormat); @@ -91,7 +91,7 @@ namespace ImageSharp.Tests public void LoadFromNoneSeekableStream() { NoneSeekableStream stream = new NoneSeekableStream(this.DataStream); - Image img = Image.Load(stream); + Image img = Image.Load(stream); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat, img.CurrentImageFormat); @@ -104,7 +104,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromStreamWithType() { - Image img = Image.Load(this.DataStream); + Image img = Image.Load(this.DataStream); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat.Sample(), img); @@ -117,7 +117,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromStreamWithOptions() { - Image img = Image.Load(this.DataStream, this.decoderOptions); + Image img = Image.Load(this.DataStream, this.decoderOptions); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat, img.CurrentImageFormat); @@ -129,7 +129,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromStreamWithTypeAndOptions() { - Image img = Image.Load(this.DataStream, this.decoderOptions); + Image img = Image.Load(this.DataStream, this.decoderOptions); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat.Sample(), img); @@ -143,7 +143,7 @@ namespace ImageSharp.Tests public void LoadFromStreamWithConfig() { Stream stream = new MemoryStream(); - Image img = Image.Load(this.LocalConfiguration, stream); + Image img = Image.Load(this.LocalConfiguration, stream); Assert.NotNull(img); Assert.Equal(this.localFormat.Object, img.CurrentImageFormat); @@ -156,7 +156,7 @@ namespace ImageSharp.Tests public void LoadFromStreamWithTypeAndConfig() { Stream stream = new MemoryStream(); - Image img = Image.Load(this.LocalConfiguration, stream); + Image img = Image.Load(this.LocalConfiguration, stream); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -170,7 +170,7 @@ namespace ImageSharp.Tests public void LoadFromStreamWithConfigAndOptions() { Stream stream = new MemoryStream(); - Image img = Image.Load(this.LocalConfiguration, stream, this.decoderOptions); + Image img = Image.Load(this.LocalConfiguration, stream, this.decoderOptions); Assert.NotNull(img); Assert.Equal(this.localFormat.Object, img.CurrentImageFormat); @@ -183,7 +183,7 @@ namespace ImageSharp.Tests public void LoadFromStreamWithTypeAndConfigAndOptions() { Stream stream = new MemoryStream(); - Image img = Image.Load(this.LocalConfiguration, stream, this.decoderOptions); + Image img = Image.Load(this.LocalConfiguration, stream, this.decoderOptions); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -199,7 +199,7 @@ namespace ImageSharp.Tests public void LoadFromStreamWithDecoder() { Stream stream = new MemoryStream(); - Image img = Image.Load(stream, this.localDecoder.Object); + Image img = Image.Load(stream, this.localDecoder.Object); Assert.NotNull(img); this.localDecoder.Verify(x => x.Decode(Configuration.Default, stream, null)); @@ -209,7 +209,7 @@ namespace ImageSharp.Tests public void LoadFromStreamWithTypeAndDecoder() { Stream stream = new MemoryStream(); - Image img = Image.Load(stream, this.localDecoder.Object); + Image img = Image.Load(stream, this.localDecoder.Object); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -220,7 +220,7 @@ namespace ImageSharp.Tests public void LoadFromStreamWithDecoderAndOptions() { Stream stream = new MemoryStream(); - Image img = Image.Load(stream, this.localDecoder.Object, this.decoderOptions); + Image img = Image.Load(stream, this.localDecoder.Object, this.decoderOptions); Assert.NotNull(img); this.localDecoder.Verify(x => x.Decode(Configuration.Default, stream, this.decoderOptions)); @@ -230,7 +230,7 @@ namespace ImageSharp.Tests public void LoadFromStreamWithTypeAndDecoderAndOptions() { Stream stream = new MemoryStream(); - Image img = Image.Load(stream, this.localDecoder.Object, this.decoderOptions); + Image img = Image.Load(stream, this.localDecoder.Object, this.decoderOptions); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -240,7 +240,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytes() { - Image img = Image.Load(this.DataStream.ToArray()); + Image img = Image.Load(this.DataStream.ToArray()); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat, img.CurrentImageFormat); @@ -253,7 +253,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithType() { - Image img = Image.Load(this.DataStream.ToArray()); + Image img = Image.Load(this.DataStream.ToArray()); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat.Sample(), img); @@ -266,7 +266,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithOptions() { - Image img = Image.Load(this.DataStream.ToArray(), this.decoderOptions); + Image img = Image.Load(this.DataStream.ToArray(), this.decoderOptions); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat, img.CurrentImageFormat); @@ -278,7 +278,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithTypeAndOptions() { - Image img = Image.Load(this.DataStream.ToArray(), this.decoderOptions); + Image img = Image.Load(this.DataStream.ToArray(), this.decoderOptions); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat.Sample(), img); @@ -291,7 +291,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithConfig() { - Image img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray()); + Image img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray()); Assert.NotNull(img); Assert.Equal(this.localFormat.Object, img.CurrentImageFormat); @@ -304,7 +304,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithTypeAndConfig() { - Image img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray()); + Image img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray()); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -319,7 +319,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithConfigAndOptions() { - Image img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray(), this.decoderOptions); + Image img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray(), this.decoderOptions); Assert.NotNull(img); Assert.Equal(this.localFormat.Object, img.CurrentImageFormat); @@ -332,7 +332,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithTypeAndConfigAndOptions() { - Image img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray(), this.decoderOptions); + Image img = Image.Load(this.LocalConfiguration, this.DataStream.ToArray(), this.decoderOptions); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -347,7 +347,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithDecoder() { - Image img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object); + Image img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object); Assert.NotNull(img); this.localDecoder.Verify(x => x.Decode(Configuration.Default, It.IsAny(), null)); @@ -357,7 +357,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithTypeAndDecoder() { - Image img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object); + Image img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -368,7 +368,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithDecoderAndOptions() { - Image img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object, this.decoderOptions); + Image img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object, this.decoderOptions); Assert.NotNull(img); this.localDecoder.Verify(x => x.Decode(Configuration.Default, It.IsAny(), this.decoderOptions)); @@ -378,7 +378,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromBytesWithTypeAndDecoderAndOptions() { - Image img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object, this.decoderOptions); + Image img = Image.Load(this.DataStream.ToArray(), this.localDecoder.Object, this.decoderOptions); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -389,7 +389,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFile() { - Image img = Image.Load(this.DataStream); + Image img = Image.Load(this.DataStream); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat, img.CurrentImageFormat); @@ -402,7 +402,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithType() { - Image img = Image.Load(this.DataStream); + Image img = Image.Load(this.DataStream); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat.Sample(), img); @@ -415,7 +415,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithOptions() { - Image img = Image.Load(this.DataStream, this.decoderOptions); + Image img = Image.Load(this.DataStream, this.decoderOptions); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat, img.CurrentImageFormat); @@ -427,7 +427,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithTypeAndOptions() { - Image img = Image.Load(this.DataStream, this.decoderOptions); + Image img = Image.Load(this.DataStream, this.decoderOptions); Assert.NotNull(img); Assert.Equal(TestFormat.GlobalTestFormat.Sample(), img); @@ -440,7 +440,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithConfig() { - Image img = Image.Load(this.LocalConfiguration, this.FilePath); + Image img = Image.Load(this.LocalConfiguration, this.FilePath); Assert.NotNull(img); Assert.Equal(this.localFormat.Object, img.CurrentImageFormat); @@ -452,7 +452,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithTypeAndConfig() { - Image img = Image.Load(this.LocalConfiguration, this.FilePath); + Image img = Image.Load(this.LocalConfiguration, this.FilePath); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -465,7 +465,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithConfigAndOptions() { - Image img = Image.Load(this.LocalConfiguration, this.FilePath, this.decoderOptions); + Image img = Image.Load(this.LocalConfiguration, this.FilePath, this.decoderOptions); Assert.NotNull(img); Assert.Equal(this.localFormat.Object, img.CurrentImageFormat); @@ -477,7 +477,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithTypeAndConfigAndOptions() { - Image img = Image.Load(this.LocalConfiguration, this.FilePath, this.decoderOptions); + Image img = Image.Load(this.LocalConfiguration, this.FilePath, this.decoderOptions); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -491,7 +491,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithDecoder() { - Image img = Image.Load(this.FilePath, this.localDecoder.Object); + Image img = Image.Load(this.FilePath, this.localDecoder.Object); Assert.NotNull(img); this.localDecoder.Verify(x => x.Decode(Configuration.Default, this.DataStream, null)); @@ -500,7 +500,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithTypeAndDecoder() { - Image img = Image.Load(this.FilePath, this.localDecoder.Object); + Image img = Image.Load(this.FilePath, this.localDecoder.Object); Assert.NotNull(img); Assert.Equal(this.returnImage, img); @@ -510,7 +510,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithDecoderAndOptions() { - Image img = Image.Load(this.FilePath, this.localDecoder.Object, this.decoderOptions); + Image img = Image.Load(this.FilePath, this.localDecoder.Object, this.decoderOptions); Assert.NotNull(img); this.localDecoder.Verify(x => x.Decode(Configuration.Default, this.DataStream, this.decoderOptions)); @@ -519,7 +519,7 @@ namespace ImageSharp.Tests [Fact] public void LoadFromFileWithTypeAndDecoderAndOptions() { - Image img = Image.Load(this.FilePath, this.localDecoder.Object, this.decoderOptions); + Image img = Image.Load(this.FilePath, this.localDecoder.Object, this.decoderOptions); Assert.NotNull(img); Assert.Equal(this.returnImage, img); diff --git a/tests/ImageSharp.Tests/Image/ImageTests.cs b/tests/ImageSharp.Tests/Image/ImageTests.cs index 6aa963143..a3ec4cec2 100644 --- a/tests/ImageSharp.Tests/Image/ImageTests.cs +++ b/tests/ImageSharp.Tests/Image/ImageTests.cs @@ -22,11 +22,11 @@ namespace ImageSharp.Tests { Assert.Throws(() => { - Image.Load((byte[])null); + Image.Load((byte[])null); }); TestFile file = TestFile.Create(TestImages.Bmp.Car); - using (Image image = Image.Load(file.Bytes)) + using (Image image = Image.Load(file.Bytes)) { Assert.Equal(600, image.Width); Assert.Equal(450, image.Height); @@ -37,7 +37,7 @@ namespace ImageSharp.Tests public void ConstructorFileSystem() { TestFile file = TestFile.Create(TestImages.Bmp.Car); - using (Image image = Image.Load(file.FilePath)) + using (Image image = Image.Load(file.FilePath)) { Assert.Equal(600, image.Width); Assert.Equal(450, image.Height); @@ -50,7 +50,7 @@ namespace ImageSharp.Tests System.IO.FileNotFoundException ex = Assert.Throws( () => { - Image.Load(Guid.NewGuid().ToString()); + Image.Load(Guid.NewGuid().ToString()); }); } @@ -60,7 +60,7 @@ namespace ImageSharp.Tests ArgumentNullException ex = Assert.Throws( () => { - Image.Load((string)null); + Image.Load((string)null); }); } diff --git a/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs b/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs index 1747f34ad..db22300fa 100644 --- a/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs +++ b/tests/ImageSharp.Tests/MetaData/Profiles/Exif/ExifProfileTests.cs @@ -76,7 +76,7 @@ namespace ImageSharp.Tests image.SaveAsJpeg(memStream); memStream.Position = 0; - image = Image.Load(memStream); + image = Image.Load(memStream); profile = image.MetaData.ExifProfile; Assert.NotNull(profile); @@ -94,7 +94,7 @@ namespace ImageSharp.Tests image.SaveAsJpeg(memStream); memStream.Position = 0; - image = Image.Load(memStream); + image = Image.Load(memStream); profile = image.MetaData.ExifProfile; Assert.NotNull(profile); @@ -307,7 +307,7 @@ namespace ImageSharp.Tests image.Dispose(); memStream.Position = 0; - return Image.Load(memStream); + return Image.Load(memStream); } } diff --git a/tests/ImageSharp.Tests/TestFile.cs b/tests/ImageSharp.Tests/TestFile.cs index d95e9b1ac..f1b78383c 100644 --- a/tests/ImageSharp.Tests/TestFile.cs +++ b/tests/ImageSharp.Tests/TestFile.cs @@ -48,7 +48,7 @@ namespace ImageSharp.Tests this.file = file; this.Bytes = File.ReadAllBytes(file); - this.image = Image.Load(this.Bytes); + this.image = Image.Load(this.Bytes); } /// @@ -141,7 +141,7 @@ namespace ImageSharp.Tests /// public Image CreateImage(IDecoderOptions options) { - return Image.Load(this.Bytes, options); + return Image.Load(this.Bytes, options); } /// diff --git a/tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs b/tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs index cb56d8528..4a0950788 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Factories/GenericFactory.cs @@ -23,7 +23,7 @@ namespace ImageSharp.Tests public virtual Image CreateImage(byte[] bytes) { - return Image.Load(bytes); + return Image.Load(bytes); } public virtual Image CreateImage(Image other) diff --git a/tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs b/tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs index b50675edf..20af430a5 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Factories/ImageFactory.cs @@ -9,7 +9,7 @@ namespace ImageSharp.Tests public class ImageFactory : GenericFactory { - public override Image CreateImage(byte[] bytes) => Image.Load(bytes); + public override Image CreateImage(byte[] bytes) => Image.Load(bytes); public override Image CreateImage(int width, int height) => new Image(width, height); diff --git a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs index 4dcfa31d8..9ff0ca64e 100644 --- a/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs +++ b/tests/ImageSharp.Tests/TestUtilities/Tests/TestUtilityExtensionsTests.cs @@ -59,7 +59,7 @@ namespace ImageSharp.Tests Type fake = typeof(Rgba32).GetTypeInfo().Assembly.GetType("ImageSharp.dsaada_DASqewrr"); Assert.Null(fake); } - + [Theory] [WithFile(TestImages.Bmp.Car, PixelTypes.Rgba32, true)] [WithFile(TestImages.Bmp.Car, PixelTypes.Rgba32, false)] From c320f5052c0d9aa44bab4de25886e0057240c22c Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 5 May 2017 17:42:16 +1000 Subject: [PATCH 07/34] Update readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index cbe3dcb6b..e683fa252 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,7 @@ Here's an example of the code required to resize an image using the default Bicu On platforms supporting netstandard 1.3+ ```csharp -using (Image image = Image.Load("foo.jpg")) +using (Image image = Image.Load("foo.jpg")) { image.Resize(image.Width / 2, image.Height / 2) .Grayscale() @@ -96,7 +96,7 @@ on netstandard 1.1 - 1.2 ```csharp using (FileStream stream = File.OpenRead("foo.jpg")) using (FileStream output = File.OpenWrite("bar.jpg")) -using (Image image = Image.Load(stream)) +using (Image image = Image.Load(stream)) { image.Resize(image.Width / 2, image.Height / 2) .Grayscale() From 482cdf4a3de43c83472e0c01c1f86392e198cf9f Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 5 May 2017 20:45:44 +1000 Subject: [PATCH 08/34] Add Rgba32 specific overloads --- README.md | 4 +- src/ImageSharp/Image/Image.Create.cs | 2 +- src/ImageSharp/Image/Image.Decode.cs | 2 +- src/ImageSharp/Image/Image.FromBytes.cs | 51 +++++++++++++++++- src/ImageSharp/Image/Image.FromFile.cs | 69 +++++++++++++++++++++++- src/ImageSharp/Image/Image.FromStream.cs | 57 +++++++++++++++++++- 6 files changed, 179 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index e683fa252..ca2427546 100644 --- a/README.md +++ b/README.md @@ -85,7 +85,8 @@ Here's an example of the code required to resize an image using the default Bicu On platforms supporting netstandard 1.3+ ```csharp -using (Image image = Image.Load("foo.jpg")) +// Image.Load(string path) is a shortcut for our default type. Other pixel formats use Image.Load(string path)) +using (Image image = Image.Load("foo.jpg")) { image.Resize(image.Width / 2, image.Height / 2) .Grayscale() @@ -94,6 +95,7 @@ using (Image image = Image.Load("foo.jpg")) ``` on netstandard 1.1 - 1.2 ```csharp +// Image.Load(Stream stream) is a shortcut for our default type. Other pixel formats use Image.Load(Stream stream)) using (FileStream stream = File.OpenRead("foo.jpg")) using (FileStream output = File.OpenWrite("bar.jpg")) using (Image image = Image.Load(stream)) diff --git a/src/ImageSharp/Image/Image.Create.cs b/src/ImageSharp/Image/Image.Create.cs index 6a5762cc9..e251167ec 100644 --- a/src/ImageSharp/Image/Image.Create.cs +++ b/src/ImageSharp/Image/Image.Create.cs @@ -10,7 +10,7 @@ namespace ImageSharp /// /// Adds static methods allowing the creation of new images from given dimensions. /// - public partial class Image + public sealed partial class Image { /// /// Create a new instance of the class with the given height and the width. diff --git a/src/ImageSharp/Image/Image.Decode.cs b/src/ImageSharp/Image/Image.Decode.cs index 249740118..df839e9fe 100644 --- a/src/ImageSharp/Image/Image.Decode.cs +++ b/src/ImageSharp/Image/Image.Decode.cs @@ -15,7 +15,7 @@ namespace ImageSharp /// /// Adds static methods allowing the decoding of new images. /// - public partial class Image + public sealed partial class Image { /// /// By reading the header on the provided stream this calculates the images format. diff --git a/src/ImageSharp/Image/Image.FromBytes.cs b/src/ImageSharp/Image/Image.FromBytes.cs index 0cc05c266..7895429a3 100644 --- a/src/ImageSharp/Image/Image.FromBytes.cs +++ b/src/ImageSharp/Image/Image.FromBytes.cs @@ -13,8 +13,57 @@ namespace ImageSharp /// /// Adds static methods allowing the creation of new image from a byte array. /// - public partial class Image + public sealed partial class Image { + /// + /// Create a new instance of the class from the given byte array. + /// + /// The byte array containing image data. + /// A new . + public static Image Load(byte[] data) => Load(null, data, null); + + /// + /// Create a new instance of the class from the given byte array. + /// + /// The byte array containing image data. + /// The options for the decoder. + /// A new . + public static Image Load(byte[] data, IDecoderOptions options) => Load(null, data, options); + + /// + /// Create a new instance of the class from the given byte array. + /// + /// The config for the decoder. + /// The byte array containing image data. + /// A new . + public static Image Load(Configuration config, byte[] data) => Load(config, data, null); + + /// + /// Create a new instance of the class from the given byte array. + /// + /// The byte array containing image data. + /// The decoder. + /// A new . + public static Image Load(byte[] data, IImageDecoder decoder) => Load(data, decoder, null); + + /// + /// Create a new instance of the class from the given byte array. + /// + /// The configuration options. + /// The byte array containing image data. + /// The options for the decoder. + /// A new . + public static Image Load(Configuration config, byte[] data, IDecoderOptions options) => Load(config, data, options); + + /// + /// Create a new instance of the class from the given byte array. + /// + /// The byte array containing image data. + /// The decoder. + /// The options for the decoder. + /// A new . + public static Image Load(byte[] data, IImageDecoder decoder, IDecoderOptions options) => Load(data, decoder, options); + /// /// Create a new instance of the class from the given byte array. /// diff --git a/src/ImageSharp/Image/Image.FromFile.cs b/src/ImageSharp/Image/Image.FromFile.cs index 2dcb26bdb..e7014fe49 100644 --- a/src/ImageSharp/Image/Image.FromFile.cs +++ b/src/ImageSharp/Image/Image.FromFile.cs @@ -14,8 +14,75 @@ namespace ImageSharp /// /// Adds static methods allowing the creation of new image from a given file. /// - public partial class Image + public sealed partial class Image { + /// + /// Create a new instance of the class from the given file. + /// + /// The file path to the image. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// A new . + public static Image Load(string path) => Load(path); + + /// + /// Create a new instance of the class from the given file. + /// + /// The file path to the image. + /// The options for the decoder. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// A new . + public static Image Load(string path, IDecoderOptions options) => Load(path, options); + + /// + /// Create a new instance of the class from the given file. + /// + /// The config for the decoder. + /// The file path to the image. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// A new . + public static Image Load(Configuration config, string path) => Load(config, path); + + /// + /// Create a new instance of the class from the given file. + /// + /// The file path to the image. + /// The decoder. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// A new . + public static Image Load(string path, IImageDecoder decoder) => Load(path, decoder); + + /// + /// Create a new instance of the class from the given file. + /// + /// The configuration options. + /// The file path to the image. + /// The options for the decoder. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// A new . + public static Image Load(Configuration config, string path, IDecoderOptions options) => Load(config, path, options); + + /// + /// Create a new instance of the class from the given file. + /// + /// The file path to the image. + /// The decoder. + /// The options for the decoder. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// A new . + public static Image Load(string path, IImageDecoder decoder, IDecoderOptions options) => Load(path, decoder, options); + /// /// Create a new instance of the class from the given file. /// diff --git a/src/ImageSharp/Image/Image.FromStream.cs b/src/ImageSharp/Image/Image.FromStream.cs index a120346a1..c27762096 100644 --- a/src/ImageSharp/Image/Image.FromStream.cs +++ b/src/ImageSharp/Image/Image.FromStream.cs @@ -15,8 +15,63 @@ namespace ImageSharp /// /// Adds static methods allowing the creation of new image from a given stream. /// - public partial class Image + public sealed partial class Image { + /// + /// Create a new instance of the class from the given stream. + /// + /// The stream containing image information. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// A new .> + public static Image Load(Stream stream) => Load(stream); + + /// + /// Create a new instance of the class from the given stream. + /// + /// The stream containing image information. + /// The options for the decoder. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// A new .> + public static Image Load(Stream stream, IDecoderOptions options) => Load(stream, options); + + /// + /// Create a new instance of the class from the given stream. + /// + /// The stream containing image information. + /// The decoder. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// A new .> + public static Image Load(Stream stream, IImageDecoder decoder) => Load(stream, decoder); + + /// + /// Create a new instance of the class from the given stream. + /// + /// The config for the decoder. + /// The stream containing image information. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// A new .> + public static Image Load(Configuration config, Stream stream) => Load(config, stream); + + /// + /// Create a new instance of the class from the given stream. + /// + /// The stream containing image information. + /// The decoder. + /// The options for the decoder. + /// + /// Thrown if the stream is not readable nor seekable. + /// + /// A new .> + public static Image Load(Stream stream, IImageDecoder decoder, IDecoderOptions options) => Load(stream, decoder, options); + /// /// Create a new instance of the class from the given stream. /// From 8d832b538935276ccbed953c1a3a463a33ce5f26 Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Fri, 5 May 2017 14:58:30 +0200 Subject: [PATCH 09/34] Made the Image class static. --- src/ImageSharp/Image/Image.Create.cs | 2 +- src/ImageSharp/Image/Image.Decode.cs | 2 +- src/ImageSharp/Image/Image.FromBytes.cs | 2 +- src/ImageSharp/Image/Image.FromFile.cs | 2 +- src/ImageSharp/Image/Image.FromStream.cs | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Image/Image.Create.cs b/src/ImageSharp/Image/Image.Create.cs index e251167ec..82ebecd41 100644 --- a/src/ImageSharp/Image/Image.Create.cs +++ b/src/ImageSharp/Image/Image.Create.cs @@ -10,7 +10,7 @@ namespace ImageSharp /// /// Adds static methods allowing the creation of new images from given dimensions. /// - public sealed partial class Image + public static partial class Image { /// /// Create a new instance of the class with the given height and the width. diff --git a/src/ImageSharp/Image/Image.Decode.cs b/src/ImageSharp/Image/Image.Decode.cs index df839e9fe..c162f1772 100644 --- a/src/ImageSharp/Image/Image.Decode.cs +++ b/src/ImageSharp/Image/Image.Decode.cs @@ -15,7 +15,7 @@ namespace ImageSharp /// /// Adds static methods allowing the decoding of new images. /// - public sealed partial class Image + public static partial class Image { /// /// By reading the header on the provided stream this calculates the images format. diff --git a/src/ImageSharp/Image/Image.FromBytes.cs b/src/ImageSharp/Image/Image.FromBytes.cs index 7895429a3..c7309c4b1 100644 --- a/src/ImageSharp/Image/Image.FromBytes.cs +++ b/src/ImageSharp/Image/Image.FromBytes.cs @@ -13,7 +13,7 @@ namespace ImageSharp /// /// Adds static methods allowing the creation of new image from a byte array. /// - public sealed partial class Image + public static partial class Image { /// /// Create a new instance of the class from the given byte array. diff --git a/src/ImageSharp/Image/Image.FromFile.cs b/src/ImageSharp/Image/Image.FromFile.cs index e7014fe49..a135c43f5 100644 --- a/src/ImageSharp/Image/Image.FromFile.cs +++ b/src/ImageSharp/Image/Image.FromFile.cs @@ -14,7 +14,7 @@ namespace ImageSharp /// /// Adds static methods allowing the creation of new image from a given file. /// - public sealed partial class Image + public static partial class Image { /// /// Create a new instance of the class from the given file. diff --git a/src/ImageSharp/Image/Image.FromStream.cs b/src/ImageSharp/Image/Image.FromStream.cs index c27762096..1bcb5adc9 100644 --- a/src/ImageSharp/Image/Image.FromStream.cs +++ b/src/ImageSharp/Image/Image.FromStream.cs @@ -15,7 +15,7 @@ namespace ImageSharp /// /// Adds static methods allowing the creation of new image from a given stream. /// - public sealed partial class Image + public static partial class Image { /// /// Create a new instance of the class from the given stream. From 2ab4f2db6c2f801ca118fe65170a1a68f74ac500 Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Fri, 5 May 2017 15:00:21 +0200 Subject: [PATCH 10/34] Fixed never used warning. --- src/ImageSharp/ImageProcessor.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/ImageSharp/ImageProcessor.cs b/src/ImageSharp/ImageProcessor.cs index 745b25fb6..d42650e56 100644 --- a/src/ImageSharp/ImageProcessor.cs +++ b/src/ImageSharp/ImageProcessor.cs @@ -39,11 +39,13 @@ namespace ImageSharp.Processing this.AfterApply(source, sourceRectangle); } - catch (Exception ex) - { #if DEBUG + catch (Exception) + { throw; #else + catch (Exception ex) + { throw new ImageProcessingException($"An error occured when processing the image using {this.GetType().Name}. See the inner exception for more detail.", ex); #endif } From 65ecb53b6b447711fba7aa6ed85a1fa8c3bdc5f0 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Fri, 5 May 2017 21:35:39 +0100 Subject: [PATCH 11/34] Brush & Pen instead of Brush & Pen --- .../{Brushes{TPixel}.cs => Brushes.cs} | 66 +++++++++++++------ .../Pens/{Pens{TPixel}.cs => Pens.cs} | 46 +++++++++---- src/ImageSharp.Drawing/Text/DrawText.cs | 2 +- .../Drawing/FillWithPattern.cs | 3 +- .../Drawing/FillPatternTests.cs | 28 ++++---- .../Drawing/LineComplexPolygonTests.cs | 2 +- tests/ImageSharp.Tests/Drawing/LineTests.cs | 8 +-- .../Drawing/Paths/DrawBeziersTests.cs | 2 +- .../Drawing/Paths/DrawLinesTests.cs | 2 +- .../Drawing/Paths/DrawPath.cs | 2 +- .../Drawing/Paths/DrawPolygon.cs | 2 +- .../Drawing/Paths/DrawRectangle.cs | 2 +- .../Drawing/Paths/FillPath.cs | 2 +- .../Drawing/Paths/FillPolygon.cs | 2 +- .../Drawing/Paths/FillRectangle.cs | 2 +- .../Drawing/SolidPolygonTests.cs | 2 +- .../ImageSharp.Tests/Drawing/Text/DrawText.cs | 34 +++++----- 17 files changed, 127 insertions(+), 80 deletions(-) rename src/ImageSharp.Drawing/Brushes/{Brushes{TPixel}.cs => Brushes.cs} (74%) rename src/ImageSharp.Drawing/Pens/{Pens{TPixel}.cs => Pens.cs} (64%) diff --git a/src/ImageSharp.Drawing/Brushes/Brushes{TPixel}.cs b/src/ImageSharp.Drawing/Brushes/Brushes.cs similarity index 74% rename from src/ImageSharp.Drawing/Brushes/Brushes{TPixel}.cs rename to src/ImageSharp.Drawing/Brushes/Brushes.cs index a9b638e8b..e39f3dd49 100644 --- a/src/ImageSharp.Drawing/Brushes/Brushes{TPixel}.cs +++ b/src/ImageSharp.Drawing/Brushes/Brushes.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // @@ -10,10 +10,8 @@ namespace ImageSharp.Drawing.Brushes /// /// A collection of methods for creating generic brushes. /// - /// The pixel format. /// A New - public class Brushes - where TPixel : struct, IPixel + public static class Brushes { /// /// Percent10 Hatch Pattern @@ -98,16 +96,20 @@ namespace ImageSharp.Drawing.Brushes /// Create as brush that will paint a solid color /// /// The color. + /// The pixel format. /// A New - public static SolidBrush Solid(TPixel color) + public static SolidBrush Solid(TPixel color) + where TPixel : struct, IPixel => new SolidBrush(color); /// /// Create as brush that will paint a Percent10 Hatch Pattern with the specified colors /// /// Color of the foreground. + /// The pixel format. /// A New - public static PatternBrush Percent10(TPixel foreColor) + public static PatternBrush Percent10(TPixel foreColor) + where TPixel : struct, IPixel => new PatternBrush(foreColor, NamedColors.Transparent, Percent10Pattern); /// @@ -115,8 +117,10 @@ namespace ImageSharp.Drawing.Brushes /// /// Color of the foreground. /// Color of the background. + /// The pixel format. /// A New - public static PatternBrush Percent10(TPixel foreColor, TPixel backColor) + public static PatternBrush Percent10(TPixel foreColor, TPixel backColor) + where TPixel : struct, IPixel => new PatternBrush(foreColor, backColor, Percent10Pattern); /// @@ -124,8 +128,10 @@ namespace ImageSharp.Drawing.Brushes /// transparent background. /// /// Color of the foreground. + /// The pixel format. /// A New - public static PatternBrush Percent20(TPixel foreColor) + public static PatternBrush Percent20(TPixel foreColor) + where TPixel : struct, IPixel => new PatternBrush(foreColor, NamedColors.Transparent, Percent20Pattern); /// @@ -133,8 +139,10 @@ namespace ImageSharp.Drawing.Brushes /// /// Color of the foreground. /// Color of the background. + /// The pixel format. /// A New - public static PatternBrush Percent20(TPixel foreColor, TPixel backColor) + public static PatternBrush Percent20(TPixel foreColor, TPixel backColor) + where TPixel : struct, IPixel => new PatternBrush(foreColor, backColor, Percent20Pattern); /// @@ -142,8 +150,10 @@ namespace ImageSharp.Drawing.Brushes /// transparent background. /// /// Color of the foreground. + /// The pixel format. /// A New - public static PatternBrush Horizontal(TPixel foreColor) + public static PatternBrush Horizontal(TPixel foreColor) + where TPixel : struct, IPixel => new PatternBrush(foreColor, NamedColors.Transparent, HorizontalPattern); /// @@ -151,8 +161,10 @@ namespace ImageSharp.Drawing.Brushes /// /// Color of the foreground. /// Color of the background. + /// The pixel format. /// A New - public static PatternBrush Horizontal(TPixel foreColor, TPixel backColor) + public static PatternBrush Horizontal(TPixel foreColor, TPixel backColor) + where TPixel : struct, IPixel => new PatternBrush(foreColor, backColor, HorizontalPattern); /// @@ -160,8 +172,10 @@ namespace ImageSharp.Drawing.Brushes /// transparent background. /// /// Color of the foreground. + /// The pixel format. /// A New - public static PatternBrush Min(TPixel foreColor) + public static PatternBrush Min(TPixel foreColor) + where TPixel : struct, IPixel => new PatternBrush(foreColor, NamedColors.Transparent, MinPattern); /// @@ -169,8 +183,10 @@ namespace ImageSharp.Drawing.Brushes /// /// Color of the foreground. /// Color of the background. + /// The pixel format. /// A New - public static PatternBrush Min(TPixel foreColor, TPixel backColor) + public static PatternBrush Min(TPixel foreColor, TPixel backColor) + where TPixel : struct, IPixel => new PatternBrush(foreColor, backColor, MinPattern); /// @@ -178,8 +194,10 @@ namespace ImageSharp.Drawing.Brushes /// transparent background. /// /// Color of the foreground. + /// The pixel format. /// A New - public static PatternBrush Vertical(TPixel foreColor) + public static PatternBrush Vertical(TPixel foreColor) + where TPixel : struct, IPixel => new PatternBrush(foreColor, NamedColors.Transparent, VerticalPattern); /// @@ -187,8 +205,10 @@ namespace ImageSharp.Drawing.Brushes /// /// Color of the foreground. /// Color of the background. + /// The pixel format. /// A New - public static PatternBrush Vertical(TPixel foreColor, TPixel backColor) + public static PatternBrush Vertical(TPixel foreColor, TPixel backColor) + where TPixel : struct, IPixel => new PatternBrush(foreColor, backColor, VerticalPattern); /// @@ -196,8 +216,10 @@ namespace ImageSharp.Drawing.Brushes /// transparent background. /// /// Color of the foreground. + /// The pixel format. /// A New - public static PatternBrush ForwardDiagonal(TPixel foreColor) + public static PatternBrush ForwardDiagonal(TPixel foreColor) + where TPixel : struct, IPixel => new PatternBrush(foreColor, NamedColors.Transparent, ForwardDiagonalPattern); /// @@ -205,8 +227,10 @@ namespace ImageSharp.Drawing.Brushes /// /// Color of the foreground. /// Color of the background. + /// The pixel format. /// A New - public static PatternBrush ForwardDiagonal(TPixel foreColor, TPixel backColor) + public static PatternBrush ForwardDiagonal(TPixel foreColor, TPixel backColor) + where TPixel : struct, IPixel => new PatternBrush(foreColor, backColor, ForwardDiagonalPattern); /// @@ -214,8 +238,10 @@ namespace ImageSharp.Drawing.Brushes /// transparent background. /// /// Color of the foreground. + /// The pixel format. /// A New - public static PatternBrush BackwardDiagonal(TPixel foreColor) + public static PatternBrush BackwardDiagonal(TPixel foreColor) + where TPixel : struct, IPixel => new PatternBrush(foreColor, NamedColors.Transparent, BackwardDiagonalPattern); /// @@ -223,8 +249,10 @@ namespace ImageSharp.Drawing.Brushes /// /// Color of the foreground. /// Color of the background. + /// The pixel format. /// A New - public static PatternBrush BackwardDiagonal(TPixel foreColor, TPixel backColor) + public static PatternBrush BackwardDiagonal(TPixel foreColor, TPixel backColor) + where TPixel : struct, IPixel => new PatternBrush(foreColor, backColor, BackwardDiagonalPattern); } } \ No newline at end of file diff --git a/src/ImageSharp.Drawing/Pens/Pens{TPixel}.cs b/src/ImageSharp.Drawing/Pens/Pens.cs similarity index 64% rename from src/ImageSharp.Drawing/Pens/Pens{TPixel}.cs rename to src/ImageSharp.Drawing/Pens/Pens.cs index 5eb78dc44..364115cb7 100644 --- a/src/ImageSharp.Drawing/Pens/Pens{TPixel}.cs +++ b/src/ImageSharp.Drawing/Pens/Pens.cs @@ -1,4 +1,4 @@ -// +// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // @@ -10,9 +10,7 @@ namespace ImageSharp.Drawing.Pens /// /// Common Pen styles /// - /// The type of the color. - public class Pens - where TPixel : struct, IPixel + public static class Pens { private static readonly float[] DashDotPattern = new[] { 3f, 1f, 1f, 1f }; private static readonly float[] DashDotDotPattern = new[] { 3f, 1f, 1f, 1f, 1f, 1f }; @@ -24,8 +22,10 @@ namespace ImageSharp.Drawing.Pens /// /// The color. /// The width. + /// The type of the color. /// The Pen - public static Pen Solid(TPixel color, float width) + public static Pen Solid(TPixel color, float width) + where TPixel : struct, IPixel => new Pen(color, width); /// @@ -33,8 +33,10 @@ namespace ImageSharp.Drawing.Pens /// /// The brush. /// The width. + /// The type of the color. /// The Pen - public static Pen Solid(IBrush brush, float width) + public static Pen Solid(IBrush brush, float width) + where TPixel : struct, IPixel => new Pen(brush, width); /// @@ -42,8 +44,10 @@ namespace ImageSharp.Drawing.Pens /// /// The color. /// The width. + /// The type of the color. /// The Pen - public static Pen Dash(TPixel color, float width) + public static Pen Dash(TPixel color, float width) + where TPixel : struct, IPixel => new Pen(color, width, DashedPattern); /// @@ -51,8 +55,10 @@ namespace ImageSharp.Drawing.Pens /// /// The brush. /// The width. + /// The type of the color. /// The Pen - public static Pen Dash(IBrush brush, float width) + public static Pen Dash(IBrush brush, float width) + where TPixel : struct, IPixel => new Pen(brush, width, DashedPattern); /// @@ -60,8 +66,10 @@ namespace ImageSharp.Drawing.Pens /// /// The color. /// The width. + /// The type of the color. /// The Pen - public static Pen Dot(TPixel color, float width) + public static Pen Dot(TPixel color, float width) + where TPixel : struct, IPixel => new Pen(color, width, DottedPattern); /// @@ -69,8 +77,10 @@ namespace ImageSharp.Drawing.Pens /// /// The brush. /// The width. + /// The type of the color. /// The Pen - public static Pen Dot(IBrush brush, float width) + public static Pen Dot(IBrush brush, float width) + where TPixel : struct, IPixel => new Pen(brush, width, DottedPattern); /// @@ -78,8 +88,10 @@ namespace ImageSharp.Drawing.Pens /// /// The color. /// The width. + /// The type of the color. /// The Pen - public static Pen DashDot(TPixel color, float width) + public static Pen DashDot(TPixel color, float width) + where TPixel : struct, IPixel => new Pen(color, width, DashDotPattern); /// @@ -87,8 +99,10 @@ namespace ImageSharp.Drawing.Pens /// /// The brush. /// The width. + /// The type of the color. /// The Pen - public static Pen DashDot(IBrush brush, float width) + public static Pen DashDot(IBrush brush, float width) + where TPixel : struct, IPixel => new Pen(brush, width, DashDotPattern); /// @@ -96,8 +110,10 @@ namespace ImageSharp.Drawing.Pens /// /// The color. /// The width. + /// The type of the color. /// The Pen - public static Pen DashDotDot(TPixel color, float width) + public static Pen DashDotDot(TPixel color, float width) + where TPixel : struct, IPixel => new Pen(color, width, DashDotDotPattern); /// @@ -105,8 +121,10 @@ namespace ImageSharp.Drawing.Pens /// /// The brush. /// The width. + /// The type of the color. /// The Pen - public static Pen DashDotDot(IBrush brush, float width) + public static Pen DashDotDot(IBrush brush, float width) + where TPixel : struct, IPixel => new Pen(brush, width, DashDotDotPattern); } } \ No newline at end of file diff --git a/src/ImageSharp.Drawing/Text/DrawText.cs b/src/ImageSharp.Drawing/Text/DrawText.cs index 1e87fd008..bd33289fa 100644 --- a/src/ImageSharp.Drawing/Text/DrawText.cs +++ b/src/ImageSharp.Drawing/Text/DrawText.cs @@ -54,7 +54,7 @@ namespace ImageSharp public static Image DrawText(this Image source, string text, Font font, TPixel color, Vector2 location, TextGraphicsOptions options) where TPixel : struct, IPixel { - return source.DrawText(text, font, Brushes.Solid(color), null, location, options); + return source.DrawText(text, font, Brushes.Solid(color), null, location, options); } /// diff --git a/tests/ImageSharp.Benchmarks/Drawing/FillWithPattern.cs b/tests/ImageSharp.Benchmarks/Drawing/FillWithPattern.cs index 616140237..aa97efe00 100644 --- a/tests/ImageSharp.Benchmarks/Drawing/FillWithPattern.cs +++ b/tests/ImageSharp.Benchmarks/Drawing/FillWithPattern.cs @@ -12,6 +12,7 @@ namespace ImageSharp.Benchmarks using BenchmarkDotNet.Attributes; using ImageSharp.Drawing.Brushes; + using CoreBrushes = ImageSharp.Drawing.Brushes.Brushes; using ImageSharp.PixelFormats; public class FillWithPattern @@ -39,7 +40,7 @@ namespace ImageSharp.Benchmarks { using (Image image = new Image(800, 800)) { - image.Fill(Brushes.BackwardDiagonal(Rgba32.HotPink)); + image.Fill(CoreBrushes.BackwardDiagonal(Rgba32.HotPink)); using (MemoryStream ms = new MemoryStream()) { diff --git a/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs b/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs index 1cd41b7b5..254c54ba1 100644 --- a/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs +++ b/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs @@ -63,7 +63,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithPercent10() { - this.Test("Percent10", Rgba32.Blue, Brushes.Percent10(Rgba32.HotPink, Rgba32.LimeGreen), + this.Test("Percent10", Rgba32.Blue, Brushes.Percent10(Rgba32.HotPink, Rgba32.LimeGreen), new[,] { { Rgba32.HotPink , Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen}, @@ -76,7 +76,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithPercent10Transparent() { - Test("Percent10_Transparent", Rgba32.Blue, Brushes.Percent10(Rgba32.HotPink), + Test("Percent10_Transparent", Rgba32.Blue, Brushes.Percent10(Rgba32.HotPink), new Rgba32[,] { { Rgba32.HotPink , Rgba32.Blue, Rgba32.Blue, Rgba32.Blue}, { Rgba32.Blue, Rgba32.Blue, Rgba32.Blue, Rgba32.Blue}, @@ -88,7 +88,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithPercent20() { - Test("Percent20", Rgba32.Blue, Brushes.Percent20(Rgba32.HotPink, Rgba32.LimeGreen), + Test("Percent20", Rgba32.Blue, Brushes.Percent20(Rgba32.HotPink, Rgba32.LimeGreen), new Rgba32[,] { { Rgba32.HotPink , Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen}, { Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.HotPink , Rgba32.LimeGreen}, @@ -100,7 +100,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithPercent20_transparent() { - Test("Percent20_Transparent", Rgba32.Blue, Brushes.Percent20(Rgba32.HotPink), + Test("Percent20_Transparent", Rgba32.Blue, Brushes.Percent20(Rgba32.HotPink), new Rgba32[,] { { Rgba32.HotPink , Rgba32.Blue, Rgba32.Blue, Rgba32.Blue}, { Rgba32.Blue, Rgba32.Blue, Rgba32.HotPink , Rgba32.Blue}, @@ -112,7 +112,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithHorizontal() { - Test("Horizontal", Rgba32.Blue, Brushes.Horizontal(Rgba32.HotPink, Rgba32.LimeGreen), + Test("Horizontal", Rgba32.Blue, Brushes.Horizontal(Rgba32.HotPink, Rgba32.LimeGreen), new Rgba32[,] { { Rgba32.LimeGreen , Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen}, { Rgba32.HotPink, Rgba32.HotPink, Rgba32.HotPink , Rgba32.HotPink}, @@ -124,7 +124,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithHorizontal_transparent() { - Test("Horizontal_Transparent", Rgba32.Blue, Brushes.Horizontal(Rgba32.HotPink), + Test("Horizontal_Transparent", Rgba32.Blue, Brushes.Horizontal(Rgba32.HotPink), new Rgba32[,] { { Rgba32.Blue , Rgba32.Blue, Rgba32.Blue, Rgba32.Blue}, { Rgba32.HotPink, Rgba32.HotPink, Rgba32.HotPink , Rgba32.HotPink}, @@ -138,7 +138,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithMin() { - Test("Min", Rgba32.Blue, Brushes.Min(Rgba32.HotPink, Rgba32.LimeGreen), + Test("Min", Rgba32.Blue, Brushes.Min(Rgba32.HotPink, Rgba32.LimeGreen), new Rgba32[,] { { Rgba32.LimeGreen , Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen}, { Rgba32.LimeGreen , Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen}, @@ -150,7 +150,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithMin_transparent() { - Test("Min_Transparent", Rgba32.Blue, Brushes.Min(Rgba32.HotPink), + Test("Min_Transparent", Rgba32.Blue, Brushes.Min(Rgba32.HotPink), new Rgba32[,] { { Rgba32.Blue , Rgba32.Blue, Rgba32.Blue, Rgba32.Blue}, { Rgba32.Blue , Rgba32.Blue, Rgba32.Blue, Rgba32.Blue}, @@ -162,7 +162,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithVertical() { - Test("Vertical", Rgba32.Blue, Brushes.Vertical(Rgba32.HotPink, Rgba32.LimeGreen), + Test("Vertical", Rgba32.Blue, Brushes.Vertical(Rgba32.HotPink, Rgba32.LimeGreen), new Rgba32[,] { { Rgba32.LimeGreen, Rgba32.HotPink, Rgba32.LimeGreen, Rgba32.LimeGreen}, { Rgba32.LimeGreen, Rgba32.HotPink, Rgba32.LimeGreen, Rgba32.LimeGreen}, @@ -174,7 +174,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithVertical_transparent() { - Test("Vertical_Transparent", Rgba32.Blue, Brushes.Vertical(Rgba32.HotPink), + Test("Vertical_Transparent", Rgba32.Blue, Brushes.Vertical(Rgba32.HotPink), new Rgba32[,] { { Rgba32.Blue, Rgba32.HotPink, Rgba32.Blue, Rgba32.Blue}, { Rgba32.Blue, Rgba32.HotPink, Rgba32.Blue, Rgba32.Blue}, @@ -186,7 +186,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithForwardDiagonal() { - Test("ForwardDiagonal", Rgba32.Blue, Brushes.ForwardDiagonal(Rgba32.HotPink, Rgba32.LimeGreen), + Test("ForwardDiagonal", Rgba32.Blue, Brushes.ForwardDiagonal(Rgba32.HotPink, Rgba32.LimeGreen), new Rgba32[,] { { Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.HotPink}, { Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.HotPink, Rgba32.LimeGreen}, @@ -198,7 +198,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithForwardDiagonal_transparent() { - Test("ForwardDiagonal_Transparent", Rgba32.Blue, Brushes.ForwardDiagonal(Rgba32.HotPink), + Test("ForwardDiagonal_Transparent", Rgba32.Blue, Brushes.ForwardDiagonal(Rgba32.HotPink), new Rgba32[,] { { Rgba32.Blue, Rgba32.Blue, Rgba32.Blue, Rgba32.HotPink}, { Rgba32.Blue, Rgba32.Blue, Rgba32.HotPink, Rgba32.Blue}, @@ -210,7 +210,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithBackwardDiagonal() { - Test("BackwardDiagonal", Rgba32.Blue, Brushes.BackwardDiagonal(Rgba32.HotPink, Rgba32.LimeGreen), + Test("BackwardDiagonal", Rgba32.Blue, Brushes.BackwardDiagonal(Rgba32.HotPink, Rgba32.LimeGreen), new Rgba32[,] { { Rgba32.HotPink, Rgba32.LimeGreen, Rgba32.LimeGreen, Rgba32.LimeGreen}, { Rgba32.LimeGreen, Rgba32.HotPink, Rgba32.LimeGreen, Rgba32.LimeGreen}, @@ -222,7 +222,7 @@ namespace ImageSharp.Tests.Drawing [Fact] public void ImageShouldBeFloodFilledWithBackwardDiagonal_transparent() { - Test("BackwardDiagonal_Transparent", Rgba32.Blue, Brushes.BackwardDiagonal(Rgba32.HotPink), + Test("BackwardDiagonal_Transparent", Rgba32.Blue, Brushes.BackwardDiagonal(Rgba32.HotPink), new Rgba32[,] { { Rgba32.HotPink, Rgba32.Blue, Rgba32.Blue, Rgba32.Blue}, { Rgba32.Blue, Rgba32.HotPink, Rgba32.Blue, Rgba32.Blue}, diff --git a/tests/ImageSharp.Tests/Drawing/LineComplexPolygonTests.cs b/tests/ImageSharp.Tests/Drawing/LineComplexPolygonTests.cs index f3d7d1a20..1f35a3788 100644 --- a/tests/ImageSharp.Tests/Drawing/LineComplexPolygonTests.cs +++ b/tests/ImageSharp.Tests/Drawing/LineComplexPolygonTests.cs @@ -185,7 +185,7 @@ namespace ImageSharp.Tests.Drawing { image .BackgroundColor(Rgba32.Blue) - .Draw(Pens.Dash(Rgba32.HotPink, 5), simplePath.Clip(hole1)) + .Draw(Pens.Dash(Rgba32.HotPink, 5), simplePath.Clip(hole1)) .Save(output); } } diff --git a/tests/ImageSharp.Tests/Drawing/LineTests.cs b/tests/ImageSharp.Tests/Drawing/LineTests.cs index 05b102dde..3396d89c5 100644 --- a/tests/ImageSharp.Tests/Drawing/LineTests.cs +++ b/tests/ImageSharp.Tests/Drawing/LineTests.cs @@ -88,7 +88,7 @@ namespace ImageSharp.Tests.Drawing { image .BackgroundColor(Rgba32.Blue) - .DrawLines(Pens.Dash(Rgba32.HotPink, 5), + .DrawLines(Pens.Dash(Rgba32.HotPink, 5), new[] { new Vector2(10, 10), new Vector2(200, 150), @@ -109,7 +109,7 @@ namespace ImageSharp.Tests.Drawing { image .BackgroundColor(Rgba32.Blue) - .DrawLines(Pens.Dot(Rgba32.HotPink, 5), + .DrawLines(Pens.Dot(Rgba32.HotPink, 5), new[] { new Vector2(10, 10), new Vector2(200, 150), @@ -130,7 +130,7 @@ namespace ImageSharp.Tests.Drawing { image .BackgroundColor(Rgba32.Blue) - .DrawLines(Pens.DashDot(Rgba32.HotPink, 5), + .DrawLines(Pens.DashDot(Rgba32.HotPink, 5), new[] { new Vector2(10, 10), new Vector2(200, 150), @@ -151,7 +151,7 @@ namespace ImageSharp.Tests.Drawing { image .BackgroundColor(Rgba32.Blue) - .DrawLines(Pens.DashDotDot(Rgba32.HotPink, 5), new[] { + .DrawLines(Pens.DashDotDot(Rgba32.HotPink, 5), new[] { new Vector2(10, 10), new Vector2(200, 150), new Vector2(50, 300) diff --git a/tests/ImageSharp.Tests/Drawing/Paths/DrawBeziersTests.cs b/tests/ImageSharp.Tests/Drawing/Paths/DrawBeziersTests.cs index 02ff92f63..a9b2284e8 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/DrawBeziersTests.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/DrawBeziersTests.cs @@ -18,7 +18,7 @@ namespace ImageSharp.Tests.Drawing.Paths float thickness = 7.2f; GraphicsOptions noneDefault = new GraphicsOptions(); Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); + SolidBrush brush = Brushes.Solid(Rgba32.HotPink); Pen pen = new Pen(Rgba32.Firebrick, 99.9f); Vector2[] points = new Vector2[] { new Vector2(10,10), diff --git a/tests/ImageSharp.Tests/Drawing/Paths/DrawLinesTests.cs b/tests/ImageSharp.Tests/Drawing/Paths/DrawLinesTests.cs index 4962e8d6f..3b7ba303d 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/DrawLinesTests.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/DrawLinesTests.cs @@ -17,7 +17,7 @@ namespace ImageSharp.Tests.Drawing.Paths float thickness = 7.2f; GraphicsOptions noneDefault = new GraphicsOptions(); Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); + SolidBrush brush = Brushes.Solid(Rgba32.HotPink); Pen pen = new Pen(Rgba32.Gray, 99.9f); Vector2[] points = new Vector2[] { new Vector2(10,10), diff --git a/tests/ImageSharp.Tests/Drawing/Paths/DrawPath.cs b/tests/ImageSharp.Tests/Drawing/Paths/DrawPath.cs index df9287bc7..0bdcbdc08 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/DrawPath.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/DrawPath.cs @@ -18,7 +18,7 @@ namespace ImageSharp.Tests.Drawing.Paths float thickness = 7.2f; GraphicsOptions noneDefault = new GraphicsOptions(); Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); + SolidBrush brush = Brushes.Solid(Rgba32.HotPink); Pen pen = new Pen(Rgba32.Gray, 99.9f); IPath path = new SixLabors.Shapes.Path(new LinearLineSegment(new Vector2[] { new Vector2(10,10), diff --git a/tests/ImageSharp.Tests/Drawing/Paths/DrawPolygon.cs b/tests/ImageSharp.Tests/Drawing/Paths/DrawPolygon.cs index 357604abf..3474e6f62 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/DrawPolygon.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/DrawPolygon.cs @@ -18,7 +18,7 @@ namespace ImageSharp.Tests.Drawing.Paths float thickness = 7.2f; GraphicsOptions noneDefault = new GraphicsOptions(); Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); + SolidBrush brush = Brushes.Solid(Rgba32.HotPink); Pen pen = new Pen(Rgba32.Gray, 99.9f); Vector2[] points = new Vector2[] { new Vector2(10,10), diff --git a/tests/ImageSharp.Tests/Drawing/Paths/DrawRectangle.cs b/tests/ImageSharp.Tests/Drawing/Paths/DrawRectangle.cs index c588cd705..e08e702c1 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/DrawRectangle.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/DrawRectangle.cs @@ -15,7 +15,7 @@ namespace ImageSharp.Tests.Drawing.Paths float thickness = 7.2f; GraphicsOptions noneDefault = new GraphicsOptions(); Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); + SolidBrush brush = Brushes.Solid(Rgba32.HotPink); Pen pen = new Pen(Rgba32.Gray, 99.9f); ImageSharp.Rectangle rectangle = new ImageSharp.Rectangle(10, 10, 98, 324); diff --git a/tests/ImageSharp.Tests/Drawing/Paths/FillPath.cs b/tests/ImageSharp.Tests/Drawing/Paths/FillPath.cs index 88ad3a91e..eb0127cb1 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/FillPath.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/FillPath.cs @@ -15,7 +15,7 @@ namespace ImageSharp.Tests.Drawing.Paths { GraphicsOptions noneDefault = new GraphicsOptions(); Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); + SolidBrush brush = Brushes.Solid(Rgba32.HotPink); IPath path = new SixLabors.Shapes.Path(new LinearLineSegment(new Vector2[] { new Vector2(10,10), new Vector2(20,10), diff --git a/tests/ImageSharp.Tests/Drawing/Paths/FillPolygon.cs b/tests/ImageSharp.Tests/Drawing/Paths/FillPolygon.cs index 5ea1b976b..3f912fe79 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/FillPolygon.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/FillPolygon.cs @@ -15,7 +15,7 @@ namespace ImageSharp.Tests.Drawing.Paths { GraphicsOptions noneDefault = new GraphicsOptions(); Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); + SolidBrush brush = Brushes.Solid(Rgba32.HotPink); Vector2[] path = new Vector2[] { new Vector2(10,10), new Vector2(20,10), diff --git a/tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.cs b/tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.cs index 6f8388504..1f4774550 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/FillRectangle.cs @@ -14,7 +14,7 @@ namespace ImageSharp.Tests.Drawing.Paths { GraphicsOptions noneDefault = new GraphicsOptions(); Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); + SolidBrush brush = Brushes.Solid(Rgba32.HotPink); ImageSharp.Rectangle rectangle = new ImageSharp.Rectangle(10, 10, 77, 76); private ProcessorWatchingImage img; diff --git a/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs b/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs index 019c4a2dd..9e377a740 100644 --- a/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs +++ b/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs @@ -60,7 +60,7 @@ namespace ImageSharp.Tests.Drawing using (FileStream output = File.OpenWrite($"{path}/Pattern.png")) { image - .FillPolygon(Brushes.Horizontal(Rgba32.HotPink), simplePath, new GraphicsOptions(true)) + .FillPolygon(Brushes.Horizontal(Rgba32.HotPink), simplePath, new GraphicsOptions(true)) .Save(output); } diff --git a/tests/ImageSharp.Tests/Drawing/Text/DrawText.cs b/tests/ImageSharp.Tests/Drawing/Text/DrawText.cs index 3b3e894f4..1516b33d4 100644 --- a/tests/ImageSharp.Tests/Drawing/Text/DrawText.cs +++ b/tests/ImageSharp.Tests/Drawing/Text/DrawText.cs @@ -24,7 +24,7 @@ namespace ImageSharp.Tests.Drawing.Text { Rgba32 color = Rgba32.HotPink; - SolidBrush brush = Brushes.Solid(Rgba32.HotPink); + SolidBrush brush = Brushes.Solid(Rgba32.HotPink); IPath path = new SixLabors.Shapes.Path( new LinearLineSegment( @@ -54,7 +54,7 @@ namespace ImageSharp.Tests.Drawing.Text this.img.DrawText( "123", this.Font, - Brushes.Solid(Rgba32.Red), + Brushes.Solid(Rgba32.Red), null, Vector2.Zero, new TextGraphicsOptions(true)); @@ -67,7 +67,7 @@ namespace ImageSharp.Tests.Drawing.Text [Fact] public void FillsForEachACharachterWhenBrushSetAndNotPenDefaultOptions() { - this.img.DrawText("123", this.Font, Brushes.Solid(Rgba32.Red), null, Vector2.Zero); + this.img.DrawText("123", this.Font, Brushes.Solid(Rgba32.Red), null, Vector2.Zero); Assert.NotEmpty(this.img.ProcessorApplications); Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied @@ -77,7 +77,7 @@ namespace ImageSharp.Tests.Drawing.Text [Fact] public void FillsForEachACharachterWhenBrushSet() { - this.img.DrawText("123", this.Font, Brushes.Solid(Rgba32.Red), Vector2.Zero, new TextGraphicsOptions(true)); + this.img.DrawText("123", this.Font, Brushes.Solid(Rgba32.Red), Vector2.Zero, new TextGraphicsOptions(true)); Assert.NotEmpty(this.img.ProcessorApplications); Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied @@ -87,7 +87,7 @@ namespace ImageSharp.Tests.Drawing.Text [Fact] public void FillsForEachACharachterWhenBrushSetDefaultOptions() { - this.img.DrawText("123", this.Font, Brushes.Solid(Rgba32.Red), Vector2.Zero); + this.img.DrawText("123", this.Font, Brushes.Solid(Rgba32.Red), Vector2.Zero); Assert.NotEmpty(this.img.ProcessorApplications); Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied @@ -130,7 +130,7 @@ namespace ImageSharp.Tests.Drawing.Text "123", this.Font, null, - Pens.Dash(Rgba32.Red, 1), + Pens.Dash(Rgba32.Red, 1), Vector2.Zero, new TextGraphicsOptions(true)); @@ -142,7 +142,7 @@ namespace ImageSharp.Tests.Drawing.Text [Fact] public void DrawForEachACharachterWhenPenSetAndNotBrushDefaultOptions() { - this.img.DrawText("123", this.Font, null, Pens.Dash(Rgba32.Red, 1), Vector2.Zero); + this.img.DrawText("123", this.Font, null, Pens.Dash(Rgba32.Red, 1), Vector2.Zero); Assert.NotEmpty(this.img.ProcessorApplications); Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied @@ -152,7 +152,7 @@ namespace ImageSharp.Tests.Drawing.Text [Fact] public void DrawForEachACharachterWhenPenSet() { - this.img.DrawText("123", this.Font, Pens.Dash(Rgba32.Red, 1), Vector2.Zero, new TextGraphicsOptions(true)); + this.img.DrawText("123", this.Font, Pens.Dash(Rgba32.Red, 1), Vector2.Zero, new TextGraphicsOptions(true)); Assert.NotEmpty(this.img.ProcessorApplications); Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied @@ -162,7 +162,7 @@ namespace ImageSharp.Tests.Drawing.Text [Fact] public void DrawForEachACharachterWhenPenSetDefaultOptions() { - this.img.DrawText("123", this.Font, Pens.Dash(Rgba32.Red, 1), Vector2.Zero); + this.img.DrawText("123", this.Font, Pens.Dash(Rgba32.Red, 1), Vector2.Zero); Assert.NotEmpty(this.img.ProcessorApplications); Assert.Equal(3, this.img.ProcessorApplications.Count); // 3 fills where applied @@ -175,8 +175,8 @@ namespace ImageSharp.Tests.Drawing.Text this.img.DrawText( "123", this.Font, - Brushes.Solid(Rgba32.Red), - Pens.Dash(Rgba32.Red, 1), + Brushes.Solid(Rgba32.Red), + Pens.Dash(Rgba32.Red, 1), Vector2.Zero, new TextGraphicsOptions(true)); @@ -187,7 +187,7 @@ namespace ImageSharp.Tests.Drawing.Text [Fact] public void DrawForEachACharachterWhenPenSetAndFillFroEachWhenBrushSetDefaultOptions() { - this.img.DrawText("123", this.Font, Brushes.Solid(Rgba32.Red), Pens.Dash(Rgba32.Red, 1), Vector2.Zero); + this.img.DrawText("123", this.Font, Brushes.Solid(Rgba32.Red), Pens.Dash(Rgba32.Red, 1), Vector2.Zero); Assert.NotEmpty(this.img.ProcessorApplications); Assert.Equal(6, this.img.ProcessorApplications.Count); @@ -199,8 +199,8 @@ namespace ImageSharp.Tests.Drawing.Text this.img.DrawText( "1", this.Font, - Brushes.Solid(Rgba32.Red), - Pens.Dash(Rgba32.Red, 1), + Brushes.Solid(Rgba32.Red), + Pens.Dash(Rgba32.Red, 1), Vector2.Zero, new TextGraphicsOptions(true)); @@ -213,7 +213,7 @@ namespace ImageSharp.Tests.Drawing.Text [Fact] public void BrushAppliesBeforPenDefaultOptions() { - this.img.DrawText("1", this.Font, Brushes.Solid(Rgba32.Red), Pens.Dash(Rgba32.Red, 1), Vector2.Zero); + this.img.DrawText("1", this.Font, Brushes.Solid(Rgba32.Red), Pens.Dash(Rgba32.Red, 1), Vector2.Zero); Assert.NotEmpty(this.img.ProcessorApplications); Assert.Equal(2, this.img.ProcessorApplications.Count); @@ -226,11 +226,11 @@ namespace ImageSharp.Tests.Drawing.Text { this.img.MetaData.VerticalResolution = 1; this.img.MetaData.HorizontalResolution = 1; - this.img.DrawText("1", this.Font, Brushes.Solid(Rgba32.Red), Vector2.Zero, new TextGraphicsOptions(true) { + this.img.DrawText("1", this.Font, Brushes.Solid(Rgba32.Red), Vector2.Zero, new TextGraphicsOptions(true) { UseImageResolution = false }); - this.img.DrawText("1", this.Font, Brushes.Solid(Rgba32.Red), Vector2.Zero, new TextGraphicsOptions(true) + this.img.DrawText("1", this.Font, Brushes.Solid(Rgba32.Red), Vector2.Zero, new TextGraphicsOptions(true) { UseImageResolution = true }); From f01192c604128f7028a27601e89a6125de07124b Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Fri, 5 May 2017 21:42:19 +0100 Subject: [PATCH 12/34] remove Image.Create --- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 2 +- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 2 +- .../Formats/Jpeg/JpegDecoderCore.cs | 2 +- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 2 +- src/ImageSharp/Image/Image.Create.cs | 52 ------------------- 5 files changed, 4 insertions(+), 56 deletions(-) delete mode 100644 src/ImageSharp/Image/Image.Create.cs diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index a9aac5efa..c0e072098 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -127,7 +127,7 @@ namespace ImageSharp.Formats + $"bigger then the max allowed size '{Image.MaxWidth}x{Image.MaxHeight}'"); } - Image image = Image.Create(this.infoHeader.Width, this.infoHeader.Height, this.configuration); + Image image = new Image(this.infoHeader.Width, this.infoHeader.Height, this.configuration); using (PixelAccessor pixels = image.Lock()) { switch (this.infoHeader.Compression) diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 589b7037a..e11c66248 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -366,7 +366,7 @@ namespace ImageSharp.Formats this.metaData.Quality = colorTableLength / 3; // This initializes the image to become fully transparent because the alpha channel is zero. - this.image = Image.Create(imageWidth, imageHeight, this.metaData, this.configuration); + this.image = new Image(imageWidth, imageHeight, this.metaData, this.configuration); this.SetFrameMetaData(this.metaData); diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 9df21a3b7..ed365ec65 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -482,7 +482,7 @@ namespace ImageSharp.Formats private Image ConvertJpegPixelsToImagePixels(ImageMetaData metadata) where TPixel : struct, IPixel { - Image image = Image.Create(this.ImageWidth, this.ImageHeight, metadata, this.configuration); + Image image = new Image(this.ImageWidth, this.ImageHeight, metadata, this.configuration); if (this.grayImage.IsInitialized) { diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 904aa1ff6..a264c8d0c 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -335,7 +335,7 @@ namespace ImageSharp.Formats throw new ArgumentOutOfRangeException($"The input png '{this.header.Width}x{this.header.Height}' is bigger than the max allowed size '{Image.MaxWidth}x{Image.MaxHeight}'"); } - image = Image.Create(this.header.Width, this.header.Height, metadata, this.configuration); + image = new Image(this.header.Width, this.header.Height, metadata, this.configuration); pixels = image.Lock(); this.bytesPerPixel = this.CalculateBytesPerPixel(); this.bytesPerScanline = this.CalculateScanlineLength(this.header.Width) + 1; diff --git a/src/ImageSharp/Image/Image.Create.cs b/src/ImageSharp/Image/Image.Create.cs deleted file mode 100644 index 82ebecd41..000000000 --- a/src/ImageSharp/Image/Image.Create.cs +++ /dev/null @@ -1,52 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ - using ImageSharp.PixelFormats; - - /// - /// Adds static methods allowing the creation of new images from given dimensions. - /// - public static partial class Image - { - /// - /// Create a new instance of the class with the given height and the width. - /// - /// The width of the image in pixels. - /// The height of the image in pixels. - /// - /// The configuration providing initialization code which allows extending the library. - /// - /// The pixel format. - /// - /// A new . - /// - internal static Image Create(int width, int height, Configuration configuration) - where TPixel : struct, IPixel - { - return Create(width, height, null, configuration); - } - - /// - /// Create a new instance of the class with the given height and the width. - /// - /// The width of the image in pixels. - /// The height of the image in pixels. - /// The images matadata to preload. - /// - /// The configuration providing initialization code which allows extending the library. - /// - /// The pixel format. - /// - /// A new . - /// - internal static Image Create(int width, int height, ImageMetaData metadata, Configuration configuration) - where TPixel : struct, IPixel - { - return new Image(width, height, metadata, configuration); - } - } -} \ No newline at end of file From 41b1833e8f968ce4302f15c1e9b3e046791fd577 Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Fri, 5 May 2017 21:44:27 +0100 Subject: [PATCH 13/34] move Configuration to be fist paramater. Configureatino should be first param to consistency with other apis --- src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs | 2 +- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 2 +- .../Formats/Jpeg/JpegDecoderCore.cs | 2 +- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 2 +- src/ImageSharp/Image/ImageBase{TPixel}.cs | 6 ++--- src/ImageSharp/Image/ImageFrame{TPixel}.cs | 2 +- src/ImageSharp/Image/Image{TPixel}.cs | 22 +++++++++---------- .../Drawing/Paths/ProcessorWatchingImage.cs | 2 +- .../Drawing/SolidPolygonTests.cs | 6 ++--- .../ImageSharp.Tests/Image/ImageSaveTests.cs | 5 +++-- 10 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs index c0e072098..dd91aa11d 100644 --- a/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs +++ b/src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs @@ -127,7 +127,7 @@ namespace ImageSharp.Formats + $"bigger then the max allowed size '{Image.MaxWidth}x{Image.MaxHeight}'"); } - Image image = new Image(this.infoHeader.Width, this.infoHeader.Height, this.configuration); + Image image = new Image(this.configuration, this.infoHeader.Width, this.infoHeader.Height); using (PixelAccessor pixels = image.Lock()) { switch (this.infoHeader.Compression) diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index e11c66248..272e4d075 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -366,7 +366,7 @@ namespace ImageSharp.Formats this.metaData.Quality = colorTableLength / 3; // This initializes the image to become fully transparent because the alpha channel is zero. - this.image = new Image(imageWidth, imageHeight, this.metaData, this.configuration); + this.image = new Image(this.configuration, imageWidth, imageHeight, this.metaData); this.SetFrameMetaData(this.metaData); diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index ed365ec65..22c14ca4e 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -482,7 +482,7 @@ namespace ImageSharp.Formats private Image ConvertJpegPixelsToImagePixels(ImageMetaData metadata) where TPixel : struct, IPixel { - Image image = new Image(this.ImageWidth, this.ImageHeight, metadata, this.configuration); + Image image = new Image(this.configuration, this.ImageWidth, this.ImageHeight, metadata); if (this.grayImage.IsInitialized) { diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index a264c8d0c..67a00efef 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -335,7 +335,7 @@ namespace ImageSharp.Formats throw new ArgumentOutOfRangeException($"The input png '{this.header.Width}x{this.header.Height}' is bigger than the max allowed size '{Image.MaxWidth}x{Image.MaxHeight}'"); } - image = new Image(this.header.Width, this.header.Height, metadata, this.configuration); + image = new Image(this.configuration, this.header.Width, this.header.Height, metadata); pixels = image.Lock(); this.bytesPerPixel = this.CalculateBytesPerPixel(); this.bytesPerScanline = this.CalculateScanlineLength(this.header.Width) + 1; diff --git a/src/ImageSharp/Image/ImageBase{TPixel}.cs b/src/ImageSharp/Image/ImageBase{TPixel}.cs index 7bad03cc8..d5023c4ba 100644 --- a/src/ImageSharp/Image/ImageBase{TPixel}.cs +++ b/src/ImageSharp/Image/ImageBase{TPixel}.cs @@ -59,15 +59,15 @@ namespace ImageSharp /// /// Initializes a new instance of the class. /// - /// The width of the image in pixels. - /// The height of the image in pixels. /// /// The configuration providing initialization code which allows extending the library. /// + /// The width of the image in pixels. + /// The height of the image in pixels. /// /// Thrown if either or are less than or equal to 0. /// - protected ImageBase(int width, int height, Configuration configuration) + protected ImageBase(Configuration configuration, int width, int height) : this(configuration) { Guard.MustBeGreaterThan(width, 0, nameof(width)); diff --git a/src/ImageSharp/Image/ImageFrame{TPixel}.cs b/src/ImageSharp/Image/ImageFrame{TPixel}.cs index 0731b1443..04b9902c6 100644 --- a/src/ImageSharp/Image/ImageFrame{TPixel}.cs +++ b/src/ImageSharp/Image/ImageFrame{TPixel}.cs @@ -26,7 +26,7 @@ namespace ImageSharp /// The configuration providing initialization code which allows extending the library. /// public ImageFrame(int width, int height, Configuration configuration = null) - : base(width, height, configuration) + : base(configuration, width, height) { } diff --git a/src/ImageSharp/Image/Image{TPixel}.cs b/src/ImageSharp/Image/Image{TPixel}.cs index 9e103c700..092706e41 100644 --- a/src/ImageSharp/Image/Image{TPixel}.cs +++ b/src/ImageSharp/Image/Image{TPixel}.cs @@ -29,13 +29,13 @@ namespace ImageSharp /// Initializes a new instance of the class /// with the height and the width of the image. /// - /// The width of the image in pixels. - /// The height of the image in pixels. /// /// The configuration providing initialization code which allows extending the library. /// - public Image(int width, int height, Configuration configuration) - : this(width, height, new ImageMetaData(), configuration) + /// The width of the image in pixels. + /// The height of the image in pixels. + public Image(Configuration configuration, int width, int height) + : this(configuration, width, height, new ImageMetaData()) { } @@ -46,7 +46,7 @@ namespace ImageSharp /// The width of the image in pixels. /// The height of the image in pixels. public Image(int width, int height) - : this(width, height, null) + : this(null, width, height) { } @@ -85,14 +85,14 @@ namespace ImageSharp /// Initializes a new instance of the class /// with the height and the width of the image. /// - /// The width of the image in pixels. - /// The height of the image in pixels. - /// The images metadata. /// /// The configuration providing initialization code which allows extending the library. /// - internal Image(int width, int height, ImageMetaData metadata, Configuration configuration) - : base(width, height, configuration) + /// The width of the image in pixels. + /// The height of the image in pixels. + /// The images metadata. + internal Image(Configuration configuration, int width, int height, ImageMetaData metadata) + : base(configuration, width, height) { if (!this.Configuration.ImageFormats.Any()) { @@ -359,7 +359,7 @@ namespace ImageSharp { scaleFunc = PackedPixelConverterHelper.ComputeScaleFunction(scaleFunc); - Image target = new Image(this.Width, this.Height, this.Configuration); + Image target = new Image(this.Configuration, this.Width, this.Height); target.CopyProperties(this); using (PixelAccessor pixels = this.Lock()) diff --git a/tests/ImageSharp.Tests/Drawing/Paths/ProcessorWatchingImage.cs b/tests/ImageSharp.Tests/Drawing/Paths/ProcessorWatchingImage.cs index d961a5994..c1d34a112 100644 --- a/tests/ImageSharp.Tests/Drawing/Paths/ProcessorWatchingImage.cs +++ b/tests/ImageSharp.Tests/Drawing/Paths/ProcessorWatchingImage.cs @@ -17,7 +17,7 @@ namespace ImageSharp.Tests.Drawing.Paths public List ProcessorApplications { get; } = new List(); public ProcessorWatchingImage(int width, int height) - : base(width, height, Configuration.CreateDefaultInstance()) + : base(Configuration.CreateDefaultInstance(), width, height) { } diff --git a/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs b/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs index 9e377a740..8ffa62d81 100644 --- a/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs +++ b/tests/ImageSharp.Tests/Drawing/SolidPolygonTests.cs @@ -217,7 +217,7 @@ namespace ImageSharp.Tests.Drawing Configuration config = Configuration.CreateDefaultInstance(); config.ParallelOptions.MaxDegreeOfParallelism = 1; - using (Image image = new Image(100, 100, config)) + using (Image image = new Image(config, 100, 100)) { using (FileStream output = File.OpenWrite($"{path}/Septagon.png")) { @@ -236,7 +236,7 @@ namespace ImageSharp.Tests.Drawing Configuration config = Configuration.CreateDefaultInstance(); config.ParallelOptions.MaxDegreeOfParallelism = 1; - using (Image image = new Image(100, 100, config)) + using (Image image = new Image(config, 100, 100)) { using (FileStream output = File.OpenWrite($"{path}/ellipse.png")) { @@ -256,7 +256,7 @@ namespace ImageSharp.Tests.Drawing Configuration config = Configuration.CreateDefaultInstance(); config.ParallelOptions.MaxDegreeOfParallelism = 1; - using (Image image = new Image(200, 200, config)) + using (Image image = new Image(config, 200, 200)) { using (FileStream output = File.OpenWrite($"{path}/clipped-corner.png")) { diff --git a/tests/ImageSharp.Tests/Image/ImageSaveTests.cs b/tests/ImageSharp.Tests/Image/ImageSaveTests.cs index 315d67344..9e9852cb2 100644 --- a/tests/ImageSharp.Tests/Image/ImageSaveTests.cs +++ b/tests/ImageSharp.Tests/Image/ImageSaveTests.cs @@ -49,9 +49,10 @@ namespace ImageSharp.Tests this.fileSystem = new Mock(); this.encoderOptions = new Mock().Object; - this.Image = new Image(1, 1, new Configuration(this.format.Object) { + this.Image = new Image(new Configuration(this.format.Object) + { FileSystem = this.fileSystem.Object - }); + }, 1, 1); } [Fact] From fe5bdf146dfa9b0a8d8689f57f5767dbfb378922 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 6 May 2017 02:40:58 +0200 Subject: [PATCH 14/34] a few more Vector experiments --- .../General/Vectorization/MulFloat.cs | 17 +++- .../General/Vectorization/VectorFetching.cs | 90 +++++++++++++++++++ 2 files changed, 105 insertions(+), 2 deletions(-) create mode 100644 tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs diff --git a/tests/ImageSharp.Benchmarks/General/Vectorization/MulFloat.cs b/tests/ImageSharp.Benchmarks/General/Vectorization/MulFloat.cs index 151145e12..ee33cf210 100644 --- a/tests/ImageSharp.Benchmarks/General/Vectorization/MulFloat.cs +++ b/tests/ImageSharp.Benchmarks/General/Vectorization/MulFloat.cs @@ -24,7 +24,7 @@ namespace ImageSharp.Benchmarks.General.Vectorization for (int i = 0; i < this.InputSize; i++) { - this.input[i] = (uint)i; + this.input[i] = i; } } @@ -39,7 +39,7 @@ namespace ImageSharp.Benchmarks.General.Vectorization } [Benchmark] - public void Simd() + public void SimdMultiplyByVector() { Vector v = new Vector(this.testValue); @@ -50,5 +50,18 @@ namespace ImageSharp.Benchmarks.General.Vectorization a.CopyTo(this.result, i); } } + + [Benchmark] + public void SimdMultiplyByScalar() + { + float v = this.testValue; + + for (int i = 0; i < this.input.Length; i += Vector.Count) + { + Vector a = new Vector(this.input, i); + a = a * v; + a.CopyTo(this.result, i); + } + } } } \ No newline at end of file diff --git a/tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs b/tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs new file mode 100644 index 000000000..93f4095e9 --- /dev/null +++ b/tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs @@ -0,0 +1,90 @@ +namespace ImageSharp.Benchmarks.General.Vectorization +{ + using System.Numerics; + using System.Runtime.CompilerServices; + + using BenchmarkDotNet.Attributes; + + /// + /// This benchmark compares different methods for fetching memory data into + /// checking if JIT has limitations. Normally SIMD acceleration should be here for all methods. + /// + public class VectorFetching + { + private float testValue; + + private float[] data; + + [Params(64)] + public int InputSize { get; set; } + + [Setup] + public void Setup() + { + this.data = new float[this.InputSize]; + this.testValue = 42; + + for (int i = 0; i < this.InputSize; i++) + { + this.data[i] = i; + } + } + + [Benchmark(Baseline = true)] + public void Baseline() + { + float v = this.testValue; + for (int i = 0; i < this.data.Length; i++) + { + this.data[i] = this.data[i] * v; + } + } + + [Benchmark] + public void FetchWithVectorConstructor() + { + Vector v = new Vector(this.testValue); + + for (int i = 0; i < this.data.Length; i += Vector.Count) + { + Vector a = new Vector(this.data, i); + a = a * v; + a.CopyTo(this.data, i); + } + } + + [Benchmark] + public void FetchWithUnsafeCast() + { + Vector v = new Vector(this.testValue); + ref Vector start = ref Unsafe.As>(ref this.data[0]); + + int n = this.InputSize / Vector.Count; + + for (int i = 0; i < n; i++) + { + ref Vector p = ref Unsafe.Add(ref start, i); + + Vector a = p; + a = a * v; + + p = a; + } + } + + [Benchmark] + public void FetchWithUnsafeCastNoTempVector() + { + Vector v = new Vector(this.testValue); + ref Vector start = ref Unsafe.As>(ref this.data[0]); + + int n = this.InputSize / Vector.Count; + + for (int i = 0; i < n; i++) + { + ref Vector a = ref Unsafe.Add(ref start, i); + a = a * v; + } + } + } +} \ No newline at end of file From d5e58192c887c10766f6a032b5b8ca952d9bdbf9 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 6 May 2017 04:07:15 +0200 Subject: [PATCH 15/34] optimization: removed temporal buffer creation from ToVector4SimdAligned() --- src/ImageSharp/Common/Memory/BufferSpan.cs | 14 ++++++ .../PixelFormats/Rgba32.PixelOperations.cs | 43 ++++++++----------- .../Color/Bulk/PackFromXyzw.cs | 2 +- .../Color/Bulk/ToVector4.cs | 2 +- .../ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs | 2 +- .../Color/Bulk/ToXyzw.cs | 4 +- .../General/Vectorization/VectorFetching.cs | 18 ++++++++ .../Colors/PixelOperationsTests.cs | 12 +++--- .../Common/BufferSpanTests.cs | 16 +++++++ 9 files changed, 78 insertions(+), 35 deletions(-) diff --git a/src/ImageSharp/Common/Memory/BufferSpan.cs b/src/ImageSharp/Common/Memory/BufferSpan.cs index c51c110be..f8a5453a8 100644 --- a/src/ImageSharp/Common/Memory/BufferSpan.cs +++ b/src/ImageSharp/Common/Memory/BufferSpan.cs @@ -6,6 +6,7 @@ namespace ImageSharp { using System; + using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -14,6 +15,19 @@ namespace ImageSharp /// internal static class BufferSpan { + /// + /// Fetches a from the beginning of the span. + /// + /// The value type + /// The span to fetch the vector from + /// A reference to the beginning of the span + [MethodImpl(MethodImplOptions.AggressiveInlining)] + public static ref Vector FetchVector(this BufferSpan span) + where T : struct + { + return ref Unsafe.As>(ref span.DangerousGetPinnableReference()); + } + /// /// Copy 'count' number of elements of the same type from 'source' to 'dest' /// diff --git a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs index 9745d0133..372ac2392 100644 --- a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs +++ b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs @@ -61,38 +61,33 @@ namespace ImageSharp int unpackedRawCount = count * 4; - ref uint src = ref Unsafe.As(ref sourceColors.DangerousGetPinnableReference()); + ref uint bSource = ref Unsafe.As(ref sourceColors.DangerousGetPinnableReference()); + ref UnpackedRGBA bDestUnpacked = ref Unsafe.As(ref destVectors.DangerousGetPinnableReference()); + ref Vector bDestUint = ref Unsafe.As>(ref bDestUnpacked); + ref Vector bDestFloat = ref Unsafe.As>(ref bDestUnpacked); - using (Buffer tempBuf = new Buffer( - unpackedRawCount + Vector.Count)) + for (int i = 0; i < count; i++) { - uint[] temp = tempBuf.Array; - float[] fTemp = Unsafe.As(temp); - - ref UnpackedRGBA tempBase = ref Unsafe.As(ref tempBuf[0]); + uint sVal = Unsafe.Add(ref bSource, i); + ref UnpackedRGBA dst = ref Unsafe.Add(ref bDestUnpacked, i); - for (int i = 0; i < count; i++) - { - uint sVal = Unsafe.Add(ref src, i); - ref UnpackedRGBA dst = ref Unsafe.Add(ref tempBase, i); + // This call is the bottleneck now: + dst.Load(sVal); + } - // This call is the bottleneck now: - dst.Load(sVal); - } + int n = unpackedRawCount / vecSize; - for (int i = 0; i < unpackedRawCount; i += vecSize) - { - Vector vi = new Vector(temp, i); + for (int i = 0; i < n; i++) + { + Vector vi = Unsafe.Add(ref bDestUint, i); - vi &= mask; - vi |= magicInt; + vi &= mask; + vi |= magicInt; - Vector vf = Vector.AsVectorSingle(vi); - vf = (vf - magicFloat) * bVec; - vf.CopyTo(fTemp, i); - } + Vector vf = Vector.AsVectorSingle(vi); + vf = (vf - magicFloat) * bVec; - BufferSpan.Copy(tempBuf.Span.AsBytes(), destVectors.AsBytes(), unpackedRawCount * sizeof(uint)); + Unsafe.Add(ref bDestFloat, i) = vf; } } diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs index efec90c99..d363769d0 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs @@ -57,7 +57,7 @@ namespace ImageSharp.Benchmarks.Color.Bulk } } - public class PackFromXyzw_Color : PackFromXyzw + public class PackFromXyzw_Rgba32 : PackFromXyzw { } } \ No newline at end of file diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs index e2c1ac726..cd1797558 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs @@ -57,7 +57,7 @@ namespace ImageSharp.Benchmarks.Color.Bulk } } - public class ToVector4_Color : ToVector4 + public class ToVector4_Rgba32 : ToVector4 { } } \ No newline at end of file diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs index 88dac21cd..663f85fb7 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs @@ -55,7 +55,7 @@ namespace ImageSharp.Benchmarks.Color.Bulk } } - public class ToXyz_Color : ToXyz + public class ToXyz_Rgba32 : ToXyz { } } \ No newline at end of file diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs index 11545d3d9..7ac621113 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs @@ -59,11 +59,11 @@ namespace ImageSharp.Benchmarks.Color.Bulk } } - public class ToXyzw_Color : ToXyzw + public class ToXyzw_Rgba32 : ToXyzw { } - public class ToXyzw_Argb : ToXyzw + public class ToXyzw_Argb32 : ToXyzw { } } diff --git a/tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs b/tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs index 93f4095e9..e6d1a2fd0 100644 --- a/tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs +++ b/tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs @@ -86,5 +86,23 @@ namespace ImageSharp.Benchmarks.General.Vectorization a = a * v; } } + + [Benchmark] + public void FetchWithBufferSpanUtility() + { + Vector v = new Vector(this.testValue); + + BufferSpan span = new BufferSpan(this.data); + + ref Vector start = ref span.FetchVector(); + + int n = this.InputSize / Vector.Count; + + for (int i = 0; i < n; i++) + { + ref Vector a = ref Unsafe.Add(ref start, i); + a = a * v; + } + } } } \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Colors/PixelOperationsTests.cs b/tests/ImageSharp.Tests/Colors/PixelOperationsTests.cs index 3d0392753..4deeb40e8 100644 --- a/tests/ImageSharp.Tests/Colors/PixelOperationsTests.cs +++ b/tests/ImageSharp.Tests/Colors/PixelOperationsTests.cs @@ -12,7 +12,7 @@ namespace ImageSharp.Tests.Colors public class PixelOperationsTests { - public class Color32 : BulkPixelOperationsTests + public class Color32 : PixelOperationsTests { public Color32(ITestOutputHelper output) : base(output) @@ -20,7 +20,7 @@ namespace ImageSharp.Tests.Colors } // For 4.6 test runner MemberData does not work without redeclaring the public field in the derived test class: - //public static new TheoryData ArraySizesData => new TheoryData { 7, 16, 1111 }; + public static new TheoryData ArraySizesData => new TheoryData { 7, 16, 1111 }; [Fact] public void IsSpecialImplementation() @@ -60,7 +60,7 @@ namespace ImageSharp.Tests.Colors } } - public class Argb : BulkPixelOperationsTests + public class Argb : PixelOperationsTests { // For 4.6 test runner MemberData does not work without redeclaring the public field in the derived test class: public Argb(ITestOutputHelper output) @@ -68,7 +68,7 @@ namespace ImageSharp.Tests.Colors { } - //public static new TheoryData ArraySizesData => new TheoryData { 7, 16, 1111 }; + public static new TheoryData ArraySizesData => new TheoryData { 7, 16, 1111 }; } [Theory] @@ -80,10 +80,10 @@ namespace ImageSharp.Tests.Colors } } - public abstract class BulkPixelOperationsTests : MeasureFixture + public abstract class PixelOperationsTests : MeasureFixture where TPixel : struct, IPixel { - protected BulkPixelOperationsTests(ITestOutputHelper output) + protected PixelOperationsTests(ITestOutputHelper output) : base(output) { } diff --git a/tests/ImageSharp.Tests/Common/BufferSpanTests.cs b/tests/ImageSharp.Tests/Common/BufferSpanTests.cs index 3bc59df64..e5fcbf9ff 100644 --- a/tests/ImageSharp.Tests/Common/BufferSpanTests.cs +++ b/tests/ImageSharp.Tests/Common/BufferSpanTests.cs @@ -4,6 +4,7 @@ namespace ImageSharp.Tests.Common { using System; + using System.Numerics; using System.Runtime.CompilerServices; using ImageSharp.PixelFormats; @@ -25,6 +26,21 @@ namespace ImageSharp.Tests.Common } } + [Fact] + public void FetchVector() + { + float[] stuff = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; + + BufferSpan span = new BufferSpan(stuff); + + ref Vector v = ref span.FetchVector(); + + Assert.Equal(0, v[0]); + Assert.Equal(1, v[1]); + Assert.Equal(2, v[2]); + Assert.Equal(3, v[3]); + } + [Fact] public void AsBytes() { From 142e7afe727d11cefab0caeaf0e722257b2de07e Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 6 May 2017 04:14:54 +0200 Subject: [PATCH 16/34] removed unnecessary unsafe modifier [skip ci] --- src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs index 372ac2392..63fbdcae5 100644 --- a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs +++ b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs @@ -39,7 +39,7 @@ namespace ImageSharp /// https://github.com/dotnet/corefx/issues/15957 /// /// - internal static unsafe void ToVector4SimdAligned(BufferSpan sourceColors, BufferSpan destVectors, int count) + internal static void ToVector4SimdAligned(BufferSpan sourceColors, BufferSpan destVectors, int count) { if (!Vector.IsHardwareAccelerated) { From 85fbfaf9dc81011ac554eb59774a62c03ac1e385 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Sat, 6 May 2017 16:32:29 +1000 Subject: [PATCH 17/34] Use var when type is apparent --- .editorconfig | 2 +- ImageSharp.ruleset | 2 +- ImageSharp.sln | 4 +- Rebracer.xml | 249 --------------------------------------------- Settings.StyleCop | 56 ---------- 5 files changed, 3 insertions(+), 310 deletions(-) delete mode 100644 Rebracer.xml delete mode 100644 Settings.StyleCop diff --git a/.editorconfig b/.editorconfig index c3fb970c3..fa43757a9 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,7 +6,7 @@ indent_style = space indent_size = 4 csharp_style_var_for_built_in_types = false:warning csharp_style_var_elsewhere = false:warning -csharp_style_var_when_type_is_apparent = false:warning +csharp_style_var_when_type_is_apparent = true:warning end_of_line = crlf dotnet_sort_system_directives_first = true dotnet_style_predefined_type_for_locals_parameters_members = true:warning diff --git a/ImageSharp.ruleset b/ImageSharp.ruleset index 3f10206fc..0bd9cd11a 100644 --- a/ImageSharp.ruleset +++ b/ImageSharp.ruleset @@ -4,7 +4,7 @@ - + diff --git a/ImageSharp.sln b/ImageSharp.sln index 8ec2cf53b..2e0cbd52e 100644 --- a/ImageSharp.sln +++ b/ImageSharp.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26403.3 +VisualStudioVersion = 15.0.26403.7 MinimumVisualStudioVersion = 10.0.40219.1 Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionItems", "{C317F1B1-D75E-4C6D-83EB-80367343E0D7}" ProjectSection(SolutionItems) = preProject @@ -16,8 +16,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionIt ImageSharp.sln.DotSettings = ImageSharp.sln.DotSettings NuGet.config = NuGet.config README.md = README.md - Rebracer.xml = Rebracer.xml - Settings.StyleCop = Settings.StyleCop EndProjectSection EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Source", "Source", "{815C0625-CD3D-440F-9F80-2D83856AB7AE}" diff --git a/Rebracer.xml b/Rebracer.xml deleted file mode 100644 index 9b0a654d2..000000000 --- a/Rebracer.xml +++ /dev/null @@ -1,249 +0,0 @@ - - - - - - - - - - - - - HACK:2 - TODO:2 - UNDONE:2 - UnresolvedMergeConflict:3 - - false - false - false - - - - - 0 - 0 - 1 - 1 - -1 - -1 - 0 - 1 - 0 - 1 - 1 - 1 - 1 - 1 - 0 - 1 - 1 - 0 - 2 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 1 - 0 - 1 - 0 - 1 - 0 - 0 - 0 - 1 - 1 - 1 - 0 - 0 - 0 - 0 - 0 - 0 - 0 - 1 - 0 - 0 - 0 - 0 - 0 - 0 - 1 - 1 - 0 - 1 - 0 - 0 - 0 - 1 - 1 - - - false - true - true - true - true - Implicit (Windows)|$(VSInstallDir)\JavaScript\References\libhelp.js|$(VSInstallDir)\JavaScript\References\domWindows.js|$(VSInstallDir)\JavaScript\References\underscorefilter.js|$(VSInstallDir)\JavaScript\References\showPlainComments.js;Implicit (Windows 8.1)|$(VSInstallDir)\JavaScript\References\libhelp.js|$(VSInstallDir)\JavaScript\References\sitetypesWindows.js|$(VSInstallDir)\JavaScript\References\domWindows_8.1.js|$(VSInstallDir)\JavaScript\References\underscorefilter.js|$(VSInstallDir)\JavaScript\References\showPlainComments.js;Implicit (Windows Phone 8.1)|$(VSInstallDir)\JavaScript\References\libhelp.js|$(VSInstallDir)\JavaScript\References\sitetypesWindows.js|$(VSInstallDir)\JavaScript\References\domWindowsPhone_8.1.js|$(VSInstallDir)\JavaScript\References\underscorefilter.js|$(VSInstallDir)\JavaScript\References\showPlainComments.js;Implicit (Web)|$(VSInstallDir)\JavaScript\References\libhelp.js|$(VSInstallDir)\JavaScript\References\sitetypesWeb.js|$(VSInstallDir)\JavaScript\References\domWeb.js|$(VSInstallDir)\JavaScript\References\underscorefilter.js|$(VSInstallDir)\JavaScript\References\showPlainComments.js|C:\Users\james.south\AppData\Local\Web Essentials 2015\Modern.Intellisense.js;Dedicated Worker|$(VSInstallDir)\JavaScript\References\libhelp.js|$(VSInstallDir)\JavaScript\References\dedicatedworker.js|$(VSInstallDir)\JavaScript\References\underscorefilter.js|$(VSInstallDir)\JavaScript\References\showPlainComments.js;Generic|$(VSInstallDir)\JavaScript\References\libhelp.js|$(VSInstallDir)\JavaScript\References\underscorefilter.js|$(VSInstallDir)\JavaScript\References\showPlainComments.js; - true - true - true - false - true - true - false - false - true - true - - - true - false - false - true - true - true - 1 - true - false - true - true - false - false - false - false - false - false - false - true - true - false - true - true - true - true - false - true - false - false - true - false - 1 - true - 2 - 2 - false - false - 0 - true - true - 0 - 0 - true - true - false - 0 - 0 - false - 0 - 1 - false - true - false - 2 - true - false - false - false - false - true - true - false - false - false - false - true - true - 2 - 2 - 2 - true - false - false - true - true - false - false - 1 - true - false - false - false - false - false - false - false - false - false - false - false - true - false - false - true - true - - - false - false - true - false - true - true - true - true - true - true - true - false - true - true - false - false - true - true - false - false - false - false - false - false - false - false - - - false - true - true - true - false - true - true - true - - - - \ No newline at end of file diff --git a/Settings.StyleCop b/Settings.StyleCop deleted file mode 100644 index 2716e8d0a..000000000 --- a/Settings.StyleCop +++ /dev/null @@ -1,56 +0,0 @@ - - - - enum - exif - uint - lossy - octree - png - quantizer - unzig - cb - cr - Laplacian - Sobel - Scharr - rgb - rgba - rrggbb - rrggbbaa - scanline - scanlines - png's - codeword - unscaled - zig-zag - crc - zlib - xff - xda - ss - Vol - pp - cmyk - Paeth - th - bool - bools - desensitivity - premultiplied - endianness - thresholding - - - - - - James Jackson-South - - Copyright © James Jackson-South and contributors. - Licensed under the Apache License, Version 2.0. - - - - - \ No newline at end of file From 0a9ed6e59a125dfacb8caacbff0bcd762b98be38 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 6 May 2017 12:22:38 +0200 Subject: [PATCH 18/34] better variable names --- .../PixelFormats/Rgba32.PixelOperations.cs | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs index 63fbdcae5..fd94a3592 100644 --- a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs +++ b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs @@ -44,13 +44,11 @@ namespace ImageSharp if (!Vector.IsHardwareAccelerated) { throw new InvalidOperationException( - "Rgba32.BulkOperations.ToVector4SimdAligned() should not be called when Vector.IsHardwareAccelerated == false!"); + "Rgba32.PixelOperations.ToVector4SimdAligned() should not be called when Vector.IsHardwareAccelerated == false!"); } - int vecSize = Vector.Count; - DebugGuard.IsTrue( - count % vecSize == 0, + count % Vector.Count == 0, nameof(count), "Argument 'count' should divisible by Vector.Count!"); @@ -61,25 +59,25 @@ namespace ImageSharp int unpackedRawCount = count * 4; - ref uint bSource = ref Unsafe.As(ref sourceColors.DangerousGetPinnableReference()); - ref UnpackedRGBA bDestUnpacked = ref Unsafe.As(ref destVectors.DangerousGetPinnableReference()); - ref Vector bDestUint = ref Unsafe.As>(ref bDestUnpacked); - ref Vector bDestFloat = ref Unsafe.As>(ref bDestUnpacked); + ref uint sourceBase = ref Unsafe.As(ref sourceColors.DangerousGetPinnableReference()); + ref UnpackedRGBA destBaseAsUnpacked = ref Unsafe.As(ref destVectors.DangerousGetPinnableReference()); + ref Vector destBaseAsUInt = ref Unsafe.As>(ref destBaseAsUnpacked); + ref Vector destBaseAsFloat = ref Unsafe.As>(ref destBaseAsUnpacked); for (int i = 0; i < count; i++) { - uint sVal = Unsafe.Add(ref bSource, i); - ref UnpackedRGBA dst = ref Unsafe.Add(ref bDestUnpacked, i); + uint sVal = Unsafe.Add(ref sourceBase, i); + ref UnpackedRGBA dst = ref Unsafe.Add(ref destBaseAsUnpacked, i); // This call is the bottleneck now: dst.Load(sVal); } - int n = unpackedRawCount / vecSize; + int numOfVectors = unpackedRawCount / Vector.Count; - for (int i = 0; i < n; i++) + for (int i = 0; i < numOfVectors; i++) { - Vector vi = Unsafe.Add(ref bDestUint, i); + Vector vi = Unsafe.Add(ref destBaseAsUInt, i); vi &= mask; vi |= magicInt; @@ -87,7 +85,7 @@ namespace ImageSharp Vector vf = Vector.AsVectorSingle(vi); vf = (vf - magicFloat) * bVec; - Unsafe.Add(ref bDestFloat, i) = vf; + Unsafe.Add(ref destBaseAsFloat, i) = vf; } } From a8481c80fbd6b5dda642f95415ce6f76432010ef Mon Sep 17 00:00:00 2001 From: Mykhailo Matviiv Date: Fri, 5 May 2017 20:03:18 +0300 Subject: [PATCH 19/34] Remove DecodedBlockArray and replace usages with Buffer to centralize memory management. --- .../Components/Decoder/DecodedBlockArray.cs | 55 ------------------- .../Components/Decoder/JpegBlockProcessor.cs | 8 +-- .../Components/Decoder/JpegScanDecoder.cs | 6 +- .../Formats/Jpeg/JpegDecoderCore.cs | 12 ++-- 4 files changed, 13 insertions(+), 68 deletions(-) delete mode 100644 src/ImageSharp/Formats/Jpeg/Components/Decoder/DecodedBlockArray.cs diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/DecodedBlockArray.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/DecodedBlockArray.cs deleted file mode 100644 index 97a79dd61..000000000 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/DecodedBlockArray.cs +++ /dev/null @@ -1,55 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp.Formats.Jpg -{ - using System; - using System.Buffers; - - /// - /// Because has no information for rented arrays, - /// we need to store the count and the buffer separately when storing pooled arrays. - /// - internal struct DecodedBlockArray : IDisposable - { - /// - /// The used to pool data in . - /// Should always clean arrays when returning! - /// - private static readonly ArrayPool ArrayPool = ArrayPool.Create(); - - /// - /// Initializes a new instance of the struct. Rents a buffer. - /// - /// The number of valid -s - public DecodedBlockArray(int count) - { - this.Count = count; - this.Buffer = ArrayPool.Rent(count); - } - - /// - /// Gets the number of actual -s inside - /// - public int Count { get; } - - /// - /// Gets the rented buffer. - /// - public DecodedBlock[] Buffer { get; private set; } - - /// - /// Returns the rented buffer to the pool. - /// - public void Dispose() - { - if (this.Buffer != null) - { - ArrayPool.Return(this.Buffer, true); - this.Buffer = null; - } - } - } -} \ No newline at end of file diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockProcessor.cs index 0acee3a10..0ce233739 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockProcessor.cs @@ -9,7 +9,7 @@ namespace ImageSharp.Formats.Jpg using System.Runtime.InteropServices; /// - /// Encapsulates the implementation of processing "raw" -s into Jpeg image channels. + /// Encapsulates the implementation of processing "raw" -s into Jpeg image channels. /// [StructLayout(LayoutKind.Sequential)] internal unsafe struct JpegBlockProcessor @@ -47,10 +47,10 @@ namespace ImageSharp.Formats.Jpg /// The instance public void ProcessAllBlocks(JpegDecoderCore decoder) { - DecodedBlockArray blockArray = decoder.DecodedBlocks[this.componentIndex]; - for (int i = 0; i < blockArray.Count; i++) + Buffer blockArray = decoder.DecodedBlocks[this.componentIndex]; + for (int i = 0; i < blockArray.Length; i++) { - this.ProcessBlockColors(decoder, ref blockArray.Buffer[i]); + this.ProcessBlockColors(decoder, ref blockArray[i]); } } diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegScanDecoder.cs index c6362a871..2a9b0c6b2 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegScanDecoder.cs @@ -171,7 +171,7 @@ namespace ImageSharp.Formats.Jpg // Take an existing block (required when progressive): int blockIndex = this.GetBlockIndex(decoder); - this.data.Block = decoder.DecodedBlocks[this.ComponentIndex].Buffer[blockIndex].Block; + this.data.Block = decoder.DecodedBlocks[this.ComponentIndex][blockIndex].Block; if (!decoder.InputProcessor.UnexpectedEndOfStreamReached) { @@ -179,8 +179,8 @@ namespace ImageSharp.Formats.Jpg } // Store the decoded block - DecodedBlockArray blocks = decoder.DecodedBlocks[this.ComponentIndex]; - blocks.Buffer[blockIndex].SaveBlock(this.bx, this.by, ref this.data.Block); + Buffer blocks = decoder.DecodedBlocks[this.ComponentIndex]; + blocks[blockIndex].SaveBlock(this.bx, this.by, ref this.data.Block); } // for j diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 22c14ca4e..da519a6ac 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -111,7 +111,7 @@ namespace ImageSharp.Formats this.QuantizationTables = new Block8x8F[MaxTq + 1]; this.Temp = new byte[2 * Block8x8F.ScalarCount]; this.ComponentArray = new Component[MaxComponents]; - this.DecodedBlocks = new DecodedBlockArray[MaxComponents]; + this.DecodedBlocks = new Buffer[MaxComponents]; } /// @@ -125,12 +125,12 @@ namespace ImageSharp.Formats public HuffmanTree[] HuffmanTrees { get; } /// - /// Gets the array of -s storing the "raw" frequency-domain decoded blocks. + /// Gets the array of -s storing the "raw" frequency-domain decoded blocks. /// We need to apply IDCT, dequantiazition and unzigging to transform them into color-space blocks. /// This is done by . /// When ==true, we are touching these blocks multiple times - each time we process a Scan. /// - public DecodedBlockArray[] DecodedBlocks { get; } + public Buffer[] DecodedBlocks { get; } /// /// Gets the quantization tables, in zigzag order. @@ -216,9 +216,9 @@ namespace ImageSharp.Formats this.HuffmanTrees[i].Dispose(); } - foreach (DecodedBlockArray blockArray in this.DecodedBlocks) + foreach (Buffer blockArray in this.DecodedBlocks) { - blockArray.Dispose(); + blockArray?.Dispose(); } this.ycbcrImage?.Dispose(); @@ -1308,7 +1308,7 @@ namespace ImageSharp.Formats { int count = this.TotalMCUCount * this.ComponentArray[i].HorizontalFactor * this.ComponentArray[i].VerticalFactor; - this.DecodedBlocks[i] = new DecodedBlockArray(count); + this.DecodedBlocks[i] = Buffer.CreateClean(count); } } } From 0e1d40313a3422b2875d7b990bc1e41d92934b5d Mon Sep 17 00:00:00 2001 From: Mykhailo Matviiv Date: Sat, 6 May 2017 15:56:56 +0300 Subject: [PATCH 20/34] Refactor JpegPixelArea to delegate memory management to Buffer2D. --- .../Jpeg/Components/Decoder/JpegPixelArea.cs | 38 ++++++++----------- 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs index 728da8d02..e46994c32 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs @@ -4,7 +4,6 @@ // namespace ImageSharp.Formats.Jpg { - using System.Buffers; using System.Runtime.CompilerServices; /// @@ -15,20 +14,20 @@ namespace ImageSharp.Formats.Jpg /// /// Initializes a new instance of the struct from existing data. /// - /// The pixel array - /// The stride + /// The pixel buffer + /// The stride /// The offset - public JpegPixelArea(byte[] pixels, int striede, int offset) + public JpegPixelArea(Buffer2D pixels, int stride, int offset) { - this.Stride = striede; + this.Stride = stride; this.Pixels = pixels; this.Offset = offset; } /// - /// Gets the pixels. + /// Gets the pixels buffer. /// - public byte[] Pixels { get; private set; } + public Buffer2D Pixels { get; private set; } /// /// Gets a value indicating whether the instance has been initalized. (Is not default(JpegPixelArea)) @@ -36,21 +35,19 @@ namespace ImageSharp.Formats.Jpg public bool IsInitialized => this.Pixels != null; /// - /// Gets or the stride. + /// Gets the stride. /// public int Stride { get; } /// - /// Gets or the offset. + /// Gets the offset. /// public int Offset { get; } /// /// Gets a of bytes to the pixel area /// - public MutableSpan Span => new MutableSpan(this.Pixels, this.Offset); - - private static ArrayPool BytePool => ArrayPool.Shared; + public MutableSpan Span => new MutableSpan(this.Pixels.Array, this.Offset); /// /// Returns the pixel at (x, y) @@ -69,21 +66,16 @@ namespace ImageSharp.Formats.Jpg /// /// Creates a new instance of the struct. - /// Pixel array will be taken from a pool, this instance will be the owner of it's pixel data, therefore - /// should be called when the instance is no longer needed. + /// Pixel array will be handled by , but + /// can be called when the instance is no longer needed. /// /// The width. /// The height. /// A with pooled data - public static JpegPixelArea CreatePooled(int width, int height) - { - int size = width * height; - byte[] pixels = BytePool.Rent(size); - return new JpegPixelArea(pixels, width, 0); - } + public static JpegPixelArea CreatePooled(int width, int height) => new JpegPixelArea(Buffer2D.CreateClean(width, height), width, 0); /// - /// Returns to the pool + /// Dispose . /// public void ReturnPooled() { @@ -92,7 +84,7 @@ namespace ImageSharp.Formats.Jpg return; } - BytePool.Return(this.Pixels); + this.Pixels.Dispose(); this.Pixels = null; } @@ -129,7 +121,7 @@ namespace ImageSharp.Formats.Jpg public unsafe void LoadColorsFrom(Block8x8F* block, Block8x8F* temp) { // Level shift by +128, clip to [0, 255], and write to dst. - block->CopyColorsTo(new MutableSpan(this.Pixels, this.Offset), this.Stride, temp); + block->CopyColorsTo(new MutableSpan(this.Pixels.Array, this.Offset), this.Stride, temp); } } } \ No newline at end of file From ede5e84ae5683eb2725a512821e8c054c27b2d96 Mon Sep 17 00:00:00 2001 From: Mykhailo Matviiv Date: Sun, 7 May 2017 14:29:42 +0300 Subject: [PATCH 21/34] Get rid of CreatePooled and ReturnPooled methods in JpegPixelArea to separate memory management. --- .../Jpeg/Components/Decoder/JpegPixelArea.cs | 34 +++++----------- .../Jpeg/Components/Decoder/YCbCrImage.cs | 18 ++++----- .../Formats/Jpeg/JpegDecoderCore.cs | 40 ++++++++++--------- .../Formats/Jpg/YCbCrImageTests.cs | 6 +-- 4 files changed, 43 insertions(+), 55 deletions(-) diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs index e46994c32..920457a0c 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs @@ -24,6 +24,16 @@ namespace ImageSharp.Formats.Jpg this.Offset = offset; } + /// + /// Initializes a new instance of the struct from existing buffer. + /// will be set to of and will be set to 0. + /// + /// The pixel buffer + public JpegPixelArea(Buffer2D pixels) + : this(pixels, pixels.Width, 0) + { + } + /// /// Gets the pixels buffer. /// @@ -64,30 +74,6 @@ namespace ImageSharp.Formats.Jpg } } - /// - /// Creates a new instance of the struct. - /// Pixel array will be handled by , but - /// can be called when the instance is no longer needed. - /// - /// The width. - /// The height. - /// A with pooled data - public static JpegPixelArea CreatePooled(int width, int height) => new JpegPixelArea(Buffer2D.CreateClean(width, height), width, 0); - - /// - /// Dispose . - /// - public void ReturnPooled() - { - if (this.Pixels == null) - { - return; - } - - this.Pixels.Dispose(); - this.Pixels = null; - } - /// /// Gets the subarea that belongs to the Block8x8 defined by block indices /// diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/YCbCrImage.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/YCbCrImage.cs index a5ca9796b..86192318b 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/YCbCrImage.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/YCbCrImage.cs @@ -17,17 +17,17 @@ namespace ImageSharp.Formats.Jpg /// /// Gets the luminance components channel as . /// - public JpegPixelArea YChannel; + public Buffer2D YChannel; /// /// Gets the blue chroma components channel as . /// - public JpegPixelArea CbChannel; + public Buffer2D CbChannel; /// /// Gets an offseted to the Cr channel /// - public JpegPixelArea CrChannel; + public Buffer2D CrChannel; #pragma warning restore SA1401 /// @@ -44,9 +44,9 @@ namespace ImageSharp.Formats.Jpg this.YStride = width; this.CStride = cSize.Width; - this.YChannel = JpegPixelArea.CreatePooled(width, height); - this.CbChannel = JpegPixelArea.CreatePooled(cSize.Width, cSize.Height); - this.CrChannel = JpegPixelArea.CreatePooled(cSize.Width, cSize.Height); + this.YChannel = Buffer2D.CreateClean(width, height); + this.CbChannel = Buffer2D.CreateClean(cSize.Width, cSize.Height); + this.CrChannel = Buffer2D.CreateClean(cSize.Width, cSize.Height); } /// @@ -106,9 +106,9 @@ namespace ImageSharp.Formats.Jpg /// public void Dispose() { - this.YChannel.ReturnPooled(); - this.CbChannel.ReturnPooled(); - this.CrChannel.ReturnPooled(); + this.YChannel.Dispose(); + this.CbChannel.Dispose(); + this.CrChannel.Dispose(); } /// diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index da519a6ac..92607883d 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -223,8 +223,8 @@ namespace ImageSharp.Formats this.ycbcrImage?.Dispose(); this.InputProcessor.Dispose(); - this.grayImage.ReturnPooled(); - this.blackImage.ReturnPooled(); + this.grayImage.Pixels?.Dispose(); + this.blackImage.Pixels?.Dispose(); } /// @@ -243,11 +243,11 @@ namespace ImageSharp.Formats switch (compIndex) { case 0: - return this.ycbcrImage.YChannel; + return new JpegPixelArea(this.ycbcrImage.YChannel); case 1: - return this.ycbcrImage.CbChannel; + return new JpegPixelArea(this.ycbcrImage.CbChannel); case 2: - return this.ycbcrImage.CrChannel; + return new JpegPixelArea(this.ycbcrImage.CrChannel); case 3: return this.blackImage; default: @@ -586,9 +586,9 @@ namespace ImageSharp.Formats for (int x = 0; x < image.Width; x++) { - byte cyan = this.ycbcrImage.YChannel.Pixels[yo + x]; - byte magenta = this.ycbcrImage.CbChannel.Pixels[co + (x / scale)]; - byte yellow = this.ycbcrImage.CrChannel.Pixels[co + (x / scale)]; + byte cyan = this.ycbcrImage.YChannel[yo + x]; + byte magenta = this.ycbcrImage.CbChannel[co + (x / scale)]; + byte yellow = this.ycbcrImage.CrChannel[co + (x / scale)]; TPixel packed = default(TPixel); this.PackCmyk(ref packed, cyan, magenta, yellow, x, y); @@ -655,9 +655,9 @@ namespace ImageSharp.Formats for (int x = 0; x < image.Width; x++) { - byte red = this.ycbcrImage.YChannel.Pixels[yo + x]; - byte green = this.ycbcrImage.CbChannel.Pixels[co + (x / scale)]; - byte blue = this.ycbcrImage.CrChannel.Pixels[co + (x / scale)]; + byte red = this.ycbcrImage.YChannel[yo + x]; + byte green = this.ycbcrImage.CbChannel[co + (x / scale)]; + byte blue = this.ycbcrImage.CrChannel[co + (x / scale)]; TPixel packed = default(TPixel); packed.PackFromBytes(red, green, blue, 255); @@ -687,9 +687,9 @@ namespace ImageSharp.Formats y => { // TODO. This Parallel loop doesn't give us the boost it should. - ref byte ycRef = ref this.ycbcrImage.YChannel.Pixels[0]; - ref byte cbRef = ref this.ycbcrImage.CbChannel.Pixels[0]; - ref byte crRef = ref this.ycbcrImage.CrChannel.Pixels[0]; + ref byte ycRef = ref this.ycbcrImage.YChannel[0]; + ref byte cbRef = ref this.ycbcrImage.CbChannel[0]; + ref byte crRef = ref this.ycbcrImage.CrChannel[0]; fixed (YCbCrToRgbTables* tables = &yCbCrToRgbTables) { // TODO: Simplify + optimize + share duplicate code across converter methods @@ -737,9 +737,9 @@ namespace ImageSharp.Formats for (int x = 0; x < image.Width; x++) { - byte yy = this.ycbcrImage.YChannel.Pixels[yo + x]; - byte cb = this.ycbcrImage.CbChannel.Pixels[co + (x / scale)]; - byte cr = this.ycbcrImage.CrChannel.Pixels[co + (x / scale)]; + byte yy = this.ycbcrImage.YChannel[yo + x]; + byte cb = this.ycbcrImage.CbChannel[co + (x / scale)]; + byte cr = this.ycbcrImage.CrChannel[co + (x / scale)]; TPixel packed = default(TPixel); this.PackYcck(ref packed, yy, cb, cr, x, y); @@ -787,7 +787,8 @@ namespace ImageSharp.Formats if (this.ComponentCount == 1) { - this.grayImage = JpegPixelArea.CreatePooled(8 * this.MCUCountX, 8 * this.MCUCountY); + Buffer2D buffer = Buffer2D.CreateClean(8 * this.MCUCountX, 8 * this.MCUCountY); + this.grayImage = new JpegPixelArea(buffer); } else { @@ -826,7 +827,8 @@ namespace ImageSharp.Formats int h3 = this.ComponentArray[3].HorizontalFactor; int v3 = this.ComponentArray[3].VerticalFactor; - this.blackImage = JpegPixelArea.CreatePooled(8 * h3 * this.MCUCountX, 8 * v3 * this.MCUCountY); + Buffer2D buffer = Buffer2D.CreateClean(8 * h3 * this.MCUCountX, 8 * v3 * this.MCUCountY); + this.blackImage = new JpegPixelArea(buffer); } } } diff --git a/tests/ImageSharp.Tests/Formats/Jpg/YCbCrImageTests.cs b/tests/ImageSharp.Tests/Formats/Jpg/YCbCrImageTests.cs index ee38f500b..ba55665ca 100644 --- a/tests/ImageSharp.Tests/Formats/Jpg/YCbCrImageTests.cs +++ b/tests/ImageSharp.Tests/Formats/Jpg/YCbCrImageTests.cs @@ -61,9 +61,9 @@ namespace ImageSharp.Tests //this.PrintChannel("Cb", img.CbChannel); //this.PrintChannel("Cr", img.CrChannel); - Assert.Equal(img.YChannel.Stride, 400); - Assert.Equal(img.CbChannel.Stride, 400 / expectedCStrideDiv); - Assert.Equal(img.CrChannel.Stride, 400 / expectedCStrideDiv); + Assert.Equal(img.YChannel.Width, 400); + Assert.Equal(img.CbChannel.Width, 400 / expectedCStrideDiv); + Assert.Equal(img.CrChannel.Width, 400 / expectedCStrideDiv); } } From 9b4bc472a1fcdb2093d902cccdbca68e1d877aad Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 9 May 2017 13:55:54 +1000 Subject: [PATCH 22/34] Use Unsafe.Add + Bufferspan for Png decode filters --- .../Formats/Png/Filters/AverageFilter.cs | 21 ++++--- .../Formats/Png/Filters/NoneFilter.cs | 12 ---- .../Formats/Png/Filters/PaethFilter.cs | 26 +++++---- .../Formats/Png/Filters/SubFilter.cs | 16 +++-- .../Formats/Png/Filters/UpFilter.cs | 14 ++--- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 58 +++++++++---------- 6 files changed, 71 insertions(+), 76 deletions(-) diff --git a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs index b4ec49946..31d80c586 100644 --- a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs @@ -22,18 +22,23 @@ namespace ImageSharp.Formats /// The number of bytes per scanline /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(byte[] scanline, byte[] previousScanline, int bytesPerScanline, int bytesPerPixel) + public static void Decode(ref byte scanline, ref byte previousScanline, int bytesPerScanline, int bytesPerPixel) { // Average(x) + floor((Raw(x-bpp)+Prior(x))/2) - fixed (byte* scan = scanline) - fixed (byte* prev = previousScanline) + for (int x = 1; x < bytesPerScanline; x++) { - for (int x = 1; x < bytesPerScanline; x++) + if (x - bytesPerPixel < 1) { - byte left = (x - bytesPerPixel < 1) ? (byte)0 : scan[x - bytesPerPixel]; - byte above = prev[x]; - - scan[x] = (byte)((scan[x] + Average(left, above)) % 256); + ref byte scan = ref Unsafe.Add(ref scanline, x); + ref byte above = ref Unsafe.Add(ref previousScanline, x); + scan = (byte)((scan + (above >> 1)) % 256); + } + else + { + ref byte scan = ref Unsafe.Add(ref scanline, x); + ref byte left = ref Unsafe.Add(ref scanline, x - bytesPerPixel); + ref byte above = ref Unsafe.Add(ref previousScanline, x); + scan = (byte)((scan + Average(left, above)) % 256); } } } diff --git a/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs b/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs index 5abd89296..0f67ba5fb 100644 --- a/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs @@ -15,18 +15,6 @@ namespace ImageSharp.Formats /// internal static class NoneFilter { - /// - /// Decodes the scanline - /// - /// The scanline to decode - /// The - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static byte[] Decode(byte[] scanline) - { - // No change required. - return scanline; - } - /// /// Encodes the scanline /// diff --git a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs index a43d4f080..986a291fe 100644 --- a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs @@ -5,7 +5,6 @@ namespace ImageSharp.Formats { - using System; using System.Runtime.CompilerServices; /// @@ -24,19 +23,24 @@ namespace ImageSharp.Formats /// The number of bytes per scanline /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(byte[] scanline, byte[] previousScanline, int bytesPerScanline, int bytesPerPixel) + public static void Decode(ref byte scanline, ref byte previousScanline, int bytesPerScanline, int bytesPerPixel) { // Paeth(x) + PaethPredictor(Raw(x-bpp), Prior(x), Prior(x-bpp)) - fixed (byte* scan = scanline) - fixed (byte* prev = previousScanline) + for (int x = 1; x < bytesPerScanline; x++) { - for (int x = 1; x < bytesPerScanline; x++) + if (x - bytesPerPixel < 1) { - byte left = (x - bytesPerPixel < 1) ? (byte)0 : scan[x - bytesPerPixel]; - byte above = prev[x]; - byte upperLeft = (x - bytesPerPixel < 1) ? (byte)0 : prev[x - bytesPerPixel]; - - scan[x] = (byte)((scan[x] + PaethPredicator(left, above, upperLeft)) % 256); + ref byte scan = ref Unsafe.Add(ref scanline, x); + ref byte above = ref Unsafe.Add(ref previousScanline, x); + scan = (byte)((scan + PaethPredicator(0, above, 0)) % 256); + } + else + { + ref byte scan = ref Unsafe.Add(ref scanline, x); + ref byte left = ref Unsafe.Add(ref scanline, x - bytesPerPixel); + ref byte above = ref Unsafe.Add(ref previousScanline, x); + ref byte upperLeft = ref Unsafe.Add(ref previousScanline, x - bytesPerPixel); + scan = (byte)((scan + PaethPredicator(left, above, upperLeft)) % 256); } } } @@ -100,4 +104,4 @@ namespace ImageSharp.Formats return upperLeft; } } -} +} \ No newline at end of file diff --git a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs index 4610ed0ae..1220dc5fb 100644 --- a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs @@ -21,15 +21,21 @@ namespace ImageSharp.Formats /// The number of bytes per scanline /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(byte[] scanline, int bytesPerScanline, int bytesPerPixel) + public static void Decode(ref byte scanline, int bytesPerScanline, int bytesPerPixel) { // Sub(x) + Raw(x-bpp) - fixed (byte* scan = scanline) + for (int x = 1; x < bytesPerScanline; x++) { - for (int x = 1; x < bytesPerScanline; x++) + if (x - bytesPerPixel < 1) + { + ref byte scan = ref Unsafe.Add(ref scanline, x); + scan = (byte)(scan % 256); + } + else { - byte priorRawByte = (x - bytesPerPixel < 1) ? (byte)0 : scan[x - bytesPerPixel]; - scan[x] = (byte)((scan[x] + priorRawByte) % 256); + ref byte scan = ref Unsafe.Add(ref scanline, x); + ref byte prev = ref Unsafe.Add(ref scanline, x - bytesPerPixel); + scan = (byte)((scan + prev) % 256); } } } diff --git a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs index 525f50d0f..ee758f64e 100644 --- a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs @@ -21,18 +21,14 @@ namespace ImageSharp.Formats /// The previous scanline. /// The number of bytes per scanline [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(byte[] scanline, byte[] previousScanline, int bytesPerScanline) + public static void Decode(ref byte scanline, ref byte previousScanline, int bytesPerScanline) { // Up(x) + Prior(x) - fixed (byte* scan = scanline) - fixed (byte* prev = previousScanline) + for (int x = 1; x < bytesPerScanline; x++) { - for (int x = 1; x < bytesPerScanline; x++) - { - byte above = prev[x]; - - scan[x] = (byte)((scan[x] + above) % 256); - } + ref byte scan = ref Unsafe.Add(ref scanline, x); + ref byte above = ref Unsafe.Add(ref previousScanline, x); + scan = (byte)((scan + above) % 256); } } diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 67a00efef..ae6fd859e 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -184,14 +184,14 @@ namespace ImageSharp.Formats public Image Decode(Stream stream) where TPixel : struct, IPixel { - ImageMetaData metadata = new ImageMetaData(); + var metadata = new ImageMetaData(); this.currentStream = stream; this.currentStream.Skip(8); Image image = null; PixelAccessor pixels = null; try { - using (ZlibInflateStream deframeStream = new ZlibInflateStream(this.currentStream)) + using (var deframeStream = new ZlibInflateStream(this.currentStream)) { PngChunk currentChunk; while (!this.isEndChunkReached && (currentChunk = this.ReadChunk()) != null) @@ -437,38 +437,36 @@ namespace ImageSharp.Formats } this.currentRowBytesRead = 0; - FilterType filterType = (FilterType)this.scanline[0]; + + var filterType = (FilterType)this.scanline[0]; + var scanBuffer = new BufferSpan(this.scanline); + ref byte scanPoint = ref scanBuffer.DangerousGetPinnableReference(); + var prevBuffer = new BufferSpan(this.previousScanline); + ref byte prevPoint = ref prevBuffer.DangerousGetPinnableReference(); switch (filterType) { case FilterType.None: - - NoneFilter.Decode(this.scanline); - break; case FilterType.Sub: - SubFilter.Decode(this.scanline, this.bytesPerScanline, this.bytesPerPixel); - + SubFilter.Decode(ref scanPoint, this.bytesPerScanline, this.bytesPerPixel); break; case FilterType.Up: - UpFilter.Decode(this.scanline, this.previousScanline, this.bytesPerScanline); - + UpFilter.Decode(ref scanPoint, ref prevPoint, this.bytesPerScanline); break; case FilterType.Average: - AverageFilter.Decode(this.scanline, this.previousScanline, this.bytesPerScanline, this.bytesPerPixel); - + AverageFilter.Decode(ref scanPoint, ref prevPoint, this.bytesPerScanline, this.bytesPerPixel); break; case FilterType.Paeth: - PaethFilter.Decode(this.scanline, this.previousScanline, this.bytesPerScanline, this.bytesPerPixel); - + PaethFilter.Decode(ref scanPoint, ref prevPoint, this.bytesPerScanline, this.bytesPerPixel); break; default: @@ -517,38 +515,35 @@ namespace ImageSharp.Formats this.currentRowBytesRead = 0; - FilterType filterType = (FilterType)this.scanline[0]; + var filterType = (FilterType)this.scanline[0]; + var scanBuffer = new BufferSpan(this.scanline); + ref byte scanPointer = ref scanBuffer.DangerousGetPinnableReference(); + var prevBuffer = new BufferSpan(this.previousScanline); + ref byte prevPointer = ref prevBuffer.DangerousGetPinnableReference(); switch (filterType) { case FilterType.None: - - NoneFilter.Decode(this.scanline); - break; case FilterType.Sub: - SubFilter.Decode(this.scanline, bytesPerInterlaceScanline, this.bytesPerPixel); - + SubFilter.Decode(ref scanPointer, bytesPerInterlaceScanline, this.bytesPerPixel); break; case FilterType.Up: - UpFilter.Decode(this.scanline, this.previousScanline, bytesPerInterlaceScanline); - + UpFilter.Decode(ref scanPointer, ref prevPointer, bytesPerInterlaceScanline); break; case FilterType.Average: - AverageFilter.Decode(this.scanline, this.previousScanline, bytesPerInterlaceScanline, this.bytesPerPixel); - + AverageFilter.Decode(ref scanPointer, ref prevPointer, bytesPerInterlaceScanline, this.bytesPerPixel); break; case FilterType.Paeth: - PaethFilter.Decode(this.scanline, this.previousScanline, bytesPerInterlaceScanline, this.bytesPerPixel); - + PaethFilter.Decode(ref scanPointer, ref prevPointer, bytesPerInterlaceScanline, this.bytesPerPixel); break; default: @@ -583,9 +578,10 @@ namespace ImageSharp.Formats private void ProcessDefilteredScanline(byte[] defilteredScanline, PixelAccessor pixels) where TPixel : struct, IPixel { - TPixel color = default(TPixel); + var color = default(TPixel); BufferSpan pixelBuffer = pixels.GetRowSpan(this.currentRow); - BufferSpan scanlineBuffer = new BufferSpan(defilteredScanline, 1); + var scanlineBuffer = new BufferSpan(defilteredScanline, 1); + switch (this.PngColorType) { case PngColorType.Grayscale: @@ -646,7 +642,7 @@ namespace ImageSharp.Formats { byte[] newScanline = ToArrayByBitsLength(defilteredScanline, this.bytesPerScanline, this.header.BitDepth); byte[] palette = this.palette; - TPixel color = default(TPixel); + var color = default(TPixel); if (this.paletteAlpha != null && this.paletteAlpha.Length > 0) { @@ -703,7 +699,7 @@ namespace ImageSharp.Formats private void ProcessInterlacedDefilteredScanline(byte[] defilteredScanline, int row, PixelAccessor pixels, int pixelOffset = 0, int increment = 1) where TPixel : struct, IPixel { - TPixel color = default(TPixel); + var color = default(TPixel); switch (this.PngColorType) { @@ -901,7 +897,7 @@ namespace ImageSharp.Formats /// private PngChunk ReadChunk() { - PngChunk chunk = new PngChunk(); + var chunk = new PngChunk(); this.ReadChunkLength(chunk); if (chunk.Length < 0) { From 91626b695ba0eb990c175c2e7ea48e3f1967e04b Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 9 May 2017 22:35:43 +1000 Subject: [PATCH 23/34] Use Unsafe.Add + BufferSpan for png encode filters --- .../Formats/Png/Filters/AverageFilter.cs | 29 ++++++---- .../Formats/Png/Filters/PaethFilter.cs | 31 ++++++---- .../Formats/Png/Filters/SubFilter.cs | 25 +++++--- .../Formats/Png/Filters/UpFilter.cs | 21 +++---- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 28 ++++----- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 58 +++++++++++++------ 6 files changed, 115 insertions(+), 77 deletions(-) diff --git a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs index 31d80c586..4c2b56e51 100644 --- a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs @@ -49,23 +49,30 @@ namespace ImageSharp.Formats /// The scanline to encode /// The previous scanline. /// The filtered scanline result. + /// The number of bytes per scanline /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(byte[] scanline, byte[] previousScanline, byte[] result, int bytesPerPixel) + public static void Encode(ref byte scanline, ref byte previousScanline, ref byte result, int bytesPerScanline, int bytesPerPixel) { // Average(x) = Raw(x) - floor((Raw(x-bpp)+Prior(x))/2) - fixed (byte* scan = scanline) - fixed (byte* prev = previousScanline) - fixed (byte* res = result) - { - res[0] = 3; + result = 3; - for (int x = 0; x < scanline.Length; x++) + for (int x = 0; x < bytesPerScanline; x++) + { + if (x - bytesPerPixel < 0) { - byte left = (x - bytesPerPixel < 0) ? (byte)0 : scan[x - bytesPerPixel]; - byte above = prev[x]; - - res[x + 1] = (byte)((scan[x] - Average(left, above)) % 256); + ref byte scan = ref Unsafe.Add(ref scanline, x); + ref byte above = ref Unsafe.Add(ref previousScanline, x); + ref byte res = ref Unsafe.Add(ref result, x + 1); + res = (byte)((scan - (above >> 1)) % 256); + } + else + { + ref byte scan = ref Unsafe.Add(ref scanline, x); + ref byte left = ref Unsafe.Add(ref scanline, x - bytesPerPixel); + ref byte above = ref Unsafe.Add(ref previousScanline, x); + ref byte res = ref Unsafe.Add(ref result, x + 1); + res = (byte)((scan - Average(left, above)) % 256); } } } diff --git a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs index 986a291fe..79de59cb9 100644 --- a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs @@ -51,24 +51,31 @@ namespace ImageSharp.Formats /// The scanline to encode /// The previous scanline. /// The filtered scanline result. + /// The number of bytes per scanline /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(byte[] scanline, byte[] previousScanline, byte[] result, int bytesPerPixel) + public static void Encode(ref byte scanline, ref byte previousScanline, ref byte result, int bytesPerScanline, int bytesPerPixel) { // Paeth(x) = Raw(x) - PaethPredictor(Raw(x-bpp), Prior(x), Prior(x - bpp)) - fixed (byte* scan = scanline) - fixed (byte* prev = previousScanline) - fixed (byte* res = result) - { - res[0] = 4; + result = 4; - for (int x = 0; x < scanline.Length; x++) + for (int x = 0; x < bytesPerScanline; x++) + { + if (x - bytesPerPixel < 0) { - byte left = (x - bytesPerPixel < 0) ? (byte)0 : scan[x - bytesPerPixel]; - byte above = prev[x]; - byte upperLeft = (x - bytesPerPixel < 0) ? (byte)0 : prev[x - bytesPerPixel]; - - res[x + 1] = (byte)((scan[x] - PaethPredicator(left, above, upperLeft)) % 256); + ref byte scan = ref Unsafe.Add(ref scanline, x); + ref byte above = ref Unsafe.Add(ref previousScanline, x); + ref byte res = ref Unsafe.Add(ref result, x + 1); + res = (byte)((scan - PaethPredicator(0, above, 0)) % 256); + } + else + { + ref byte scan = ref Unsafe.Add(ref scanline, x); + ref byte left = ref Unsafe.Add(ref scanline, x - bytesPerPixel); + ref byte above = ref Unsafe.Add(ref previousScanline, x); + ref byte upperLeft = ref Unsafe.Add(ref previousScanline, x - bytesPerPixel); + ref byte res = ref Unsafe.Add(ref result, x + 1); + res = (byte)((scan - PaethPredicator(left, above, upperLeft)) % 256); } } } diff --git a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs index 1220dc5fb..90d0fa692 100644 --- a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs @@ -45,21 +45,28 @@ namespace ImageSharp.Formats /// /// The scanline to encode /// The filtered scanline result. + /// The number of bytes per scanline /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(byte[] scanline, byte[] result, int bytesPerPixel) + public static void Encode(ref byte scanline, ref byte result, int bytesPerScanline, int bytesPerPixel) { // Sub(x) = Raw(x) - Raw(x-bpp) - fixed (byte* scan = scanline) - fixed (byte* res = result) - { - res[0] = 1; + result = 1; - for (int x = 0; x < scanline.Length; x++) + for (int x = 0; x < bytesPerScanline; x++) + { + if (x - bytesPerPixel < 0) { - byte priorRawByte = (x - bytesPerPixel < 0) ? (byte)0 : scan[x - bytesPerPixel]; - - res[x + 1] = (byte)((scan[x] - priorRawByte) % 256); + ref byte scan = ref Unsafe.Add(ref scanline, x); + ref byte res = ref Unsafe.Add(ref result, x + 1); + res = (byte)(scan % 256); + } + else + { + ref byte scan = ref Unsafe.Add(ref scanline, x); + ref byte prev = ref Unsafe.Add(ref scanline, x - bytesPerPixel); + ref byte res = ref Unsafe.Add(ref result, x + 1); + res = (byte)((scan - prev) % 256); } } } diff --git a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs index ee758f64e..fa6733060 100644 --- a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs @@ -38,22 +38,19 @@ namespace ImageSharp.Formats /// The scanline to encode /// The previous scanline. /// The filtered scanline result. + /// The number of bytes per scanline [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(byte[] scanline, byte[] previousScanline, byte[] result) + public static void Encode(ref byte scanline, ref byte previousScanline, ref byte result, int bytesPerScanline) { // Up(x) = Raw(x) - Prior(x) - fixed (byte* scan = scanline) - fixed (byte* prev = previousScanline) - fixed (byte* res = result) - { - res[0] = 2; - - for (int x = 0; x < scanline.Length; x++) - { - byte above = prev[x]; + result = 2; - res[x + 1] = (byte)((scan[x] - above) % 256); - } + for (int x = 0; x < bytesPerScanline; x++) + { + ref byte scan = ref Unsafe.Add(ref scanline, x); + ref byte above = ref Unsafe.Add(ref previousScanline, x); + ref byte res = ref Unsafe.Add(ref result, x + 1); + res = (byte)((scan - above) % 256); } } } diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index ae6fd859e..f4e1d8261 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -438,11 +438,11 @@ namespace ImageSharp.Formats this.currentRowBytesRead = 0; - var filterType = (FilterType)this.scanline[0]; - var scanBuffer = new BufferSpan(this.scanline); - ref byte scanPoint = ref scanBuffer.DangerousGetPinnableReference(); - var prevBuffer = new BufferSpan(this.previousScanline); - ref byte prevPoint = ref prevBuffer.DangerousGetPinnableReference(); + var scanSpan = new BufferSpan(this.scanline); + var prevSpan = new BufferSpan(this.previousScanline); + ref byte scanPointer = ref scanSpan.DangerousGetPinnableReference(); + ref byte prevPointer = ref prevSpan.DangerousGetPinnableReference(); + var filterType = (FilterType)scanPointer; switch (filterType) { @@ -451,22 +451,22 @@ namespace ImageSharp.Formats case FilterType.Sub: - SubFilter.Decode(ref scanPoint, this.bytesPerScanline, this.bytesPerPixel); + SubFilter.Decode(ref scanPointer, this.bytesPerScanline, this.bytesPerPixel); break; case FilterType.Up: - UpFilter.Decode(ref scanPoint, ref prevPoint, this.bytesPerScanline); + UpFilter.Decode(ref scanPointer, ref prevPointer, this.bytesPerScanline); break; case FilterType.Average: - AverageFilter.Decode(ref scanPoint, ref prevPoint, this.bytesPerScanline, this.bytesPerPixel); + AverageFilter.Decode(ref scanPointer, ref prevPointer, this.bytesPerScanline, this.bytesPerPixel); break; case FilterType.Paeth: - PaethFilter.Decode(ref scanPoint, ref prevPoint, this.bytesPerScanline, this.bytesPerPixel); + PaethFilter.Decode(ref scanPointer, ref prevPointer, this.bytesPerScanline, this.bytesPerPixel); break; default: @@ -515,11 +515,11 @@ namespace ImageSharp.Formats this.currentRowBytesRead = 0; - var filterType = (FilterType)this.scanline[0]; - var scanBuffer = new BufferSpan(this.scanline); - ref byte scanPointer = ref scanBuffer.DangerousGetPinnableReference(); - var prevBuffer = new BufferSpan(this.previousScanline); - ref byte prevPointer = ref prevBuffer.DangerousGetPinnableReference(); + var scanSpan = new BufferSpan(this.scanline); + var prevSpan = new BufferSpan(this.previousScanline); + ref byte scanPointer = ref scanSpan.DangerousGetPinnableReference(); + ref byte prevPointer = ref prevSpan.DangerousGetPinnableReference(); + var filterType = (FilterType)scanPointer; switch (filterType) { diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 31e8cd90e..342067307 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -9,7 +9,7 @@ namespace ImageSharp.Formats using System.Buffers; using System.IO; using System.Linq; - + using System.Runtime.CompilerServices; using ImageSharp.PixelFormats; using Quantizers; @@ -71,6 +71,11 @@ namespace ImageSharp.Formats /// private int bytesPerPixel; + /// + /// The number of bytes per scanline + /// + private int bytesPerScanline; + /// /// The buffer for the sub filter /// @@ -177,7 +182,7 @@ namespace ImageSharp.Formats this.bytesPerPixel = this.CalculateBytesPerPixel(); - PngHeader header = new PngHeader + var header = new PngHeader { Width = image.Width, Height = image.Height, @@ -307,7 +312,7 @@ namespace ImageSharp.Formats where TPixel : struct, IPixel { // We can use the optimized PixelAccessor here and copy the bytes in unmanaged memory. - using (PixelArea pixelRow = new PixelArea(this.width, rawScanline, this.bytesPerPixel == 4 ? ComponentOrder.Xyzw : ComponentOrder.Xyz)) + using (var pixelRow = new PixelArea(this.width, rawScanline, this.bytesPerPixel == 4 ? ComponentOrder.Xyzw : ComponentOrder.Xyz)) { pixels.CopyTo(pixelRow, row); } @@ -354,6 +359,11 @@ namespace ImageSharp.Formats /// The private byte[] GetOptimalFilteredScanline(byte[] rawScanline, byte[] previousScanline, byte[] result) { + var scanSpan = new BufferSpan(rawScanline); + var prevSpan = new BufferSpan(previousScanline); + ref byte scanPointer = ref scanSpan.DangerousGetPinnableReference(); + ref byte prevPointer = ref prevSpan.DangerousGetPinnableReference(); + // Palette images don't compress well with adaptive filtering. if (this.pngColorType == PngColorType.Palette || this.bitDepth < 8) { @@ -363,13 +373,18 @@ namespace ImageSharp.Formats // This order, while different to the enumerated order is more likely to produce a smaller sum // early on which shaves a couple of milliseconds off the processing time. - UpFilter.Encode(rawScanline, previousScanline, this.up); - int currentSum = this.CalculateTotalVariation(this.up, int.MaxValue); + var upSpan = new BufferSpan(this.up); + ref byte upPointer = ref upSpan.DangerousGetPinnableReference(); + UpFilter.Encode(ref scanPointer, ref prevPointer, ref upPointer, this.bytesPerScanline); + + int currentSum = this.CalculateTotalVariation(ref upPointer, int.MaxValue); int lowestSum = currentSum; result = this.up; - PaethFilter.Encode(rawScanline, previousScanline, this.paeth, this.bytesPerPixel); - currentSum = this.CalculateTotalVariation(this.paeth, currentSum); + var paethSpan = new BufferSpan(this.paeth); + ref byte paethPointer = ref paethSpan.DangerousGetPinnableReference(); + PaethFilter.Encode(ref scanPointer, ref prevPointer, ref paethPointer, this.bytesPerScanline, this.bytesPerPixel); + currentSum = this.CalculateTotalVariation(ref paethPointer, currentSum); if (currentSum < lowestSum) { @@ -377,8 +392,10 @@ namespace ImageSharp.Formats result = this.paeth; } - SubFilter.Encode(rawScanline, this.sub, this.bytesPerPixel); - currentSum = this.CalculateTotalVariation(this.sub, int.MaxValue); + var subSpan = new BufferSpan(this.sub); + ref byte subPointer = ref subSpan.DangerousGetPinnableReference(); + SubFilter.Encode(ref scanPointer, ref subPointer, this.bytesPerScanline, this.bytesPerPixel); + currentSum = this.CalculateTotalVariation(ref subPointer, int.MaxValue); if (currentSum < lowestSum) { @@ -386,8 +403,10 @@ namespace ImageSharp.Formats result = this.sub; } - AverageFilter.Encode(rawScanline, previousScanline, this.average, this.bytesPerPixel); - currentSum = this.CalculateTotalVariation(this.average, currentSum); + var averageSpan = new BufferSpan(this.average); + ref byte averagePointer = ref averageSpan.DangerousGetPinnableReference(); + AverageFilter.Encode(ref scanPointer, ref prevPointer, ref averagePointer, this.bytesPerScanline, this.bytesPerPixel); + currentSum = this.CalculateTotalVariation(ref averagePointer, currentSum); if (currentSum < lowestSum) { @@ -404,13 +423,14 @@ namespace ImageSharp.Formats /// The scanline bytes /// The last variation sum /// The - private int CalculateTotalVariation(byte[] scanline, int lastSum) + [MethodImpl(MethodImplOptions.AggressiveInlining)] + private int CalculateTotalVariation(ref byte scanline, int lastSum) { int sum = 0; - for (int i = 1; i < scanline.Length; i++) + for (int i = 1; i < this.bytesPerScanline; i++) { - byte v = scanline[i]; + ref byte v = ref Unsafe.Add(ref scanline, i); sum += v < 128 ? v : 256 - v; // No point continuing if we are larger. @@ -601,10 +621,10 @@ namespace ImageSharp.Formats private void WriteDataChunks(PixelAccessor pixels, Stream stream) where TPixel : struct, IPixel { - int bytesPerScanline = this.width * this.bytesPerPixel; - byte[] previousScanline = new byte[bytesPerScanline]; - byte[] rawScanline = new byte[bytesPerScanline]; - int resultLength = bytesPerScanline + 1; + this.bytesPerScanline = this.width * this.bytesPerPixel; + byte[] previousScanline = new byte[this.bytesPerScanline]; + byte[] rawScanline = new byte[this.bytesPerScanline]; + int resultLength = this.bytesPerScanline + 1; byte[] result = new byte[resultLength]; if (this.pngColorType != PngColorType.Palette) @@ -621,7 +641,7 @@ namespace ImageSharp.Formats try { memoryStream = new MemoryStream(); - using (ZlibDeflateStream deflateStream = new ZlibDeflateStream(memoryStream, this.options.CompressionLevel)) + using (var deflateStream = new ZlibDeflateStream(memoryStream, this.options.CompressionLevel)) { for (int y = 0; y < this.height; y++) { From 0e9a9ed1f77da10e92fdba32a349c0578c063dde Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 10 May 2017 13:37:29 +1000 Subject: [PATCH 24/34] Simplify API --- .../Formats/Png/Filters/AverageFilter.cs | 37 ++++++++++------- .../Formats/Png/Filters/PaethFilter.cs | 41 +++++++++++-------- .../Formats/Png/Filters/SubFilter.cs | 27 +++++++----- .../Formats/Png/Filters/UpFilter.cs | 23 +++++++---- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 24 +++++------ src/ImageSharp/Formats/Png/PngEncoderCore.cs | 29 ++++++------- 6 files changed, 99 insertions(+), 82 deletions(-) diff --git a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs index 4c2b56e51..aabf24b2f 100644 --- a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs @@ -22,22 +22,25 @@ namespace ImageSharp.Formats /// The number of bytes per scanline /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(ref byte scanline, ref byte previousScanline, int bytesPerScanline, int bytesPerPixel) + public static void Decode(BufferSpan scanline, BufferSpan previousScanline, int bytesPerScanline, int bytesPerPixel) { + ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); + ref byte prevPointer = ref previousScanline.DangerousGetPinnableReference(); + // Average(x) + floor((Raw(x-bpp)+Prior(x))/2) for (int x = 1; x < bytesPerScanline; x++) { if (x - bytesPerPixel < 1) { - ref byte scan = ref Unsafe.Add(ref scanline, x); - ref byte above = ref Unsafe.Add(ref previousScanline, x); + ref byte scan = ref Unsafe.Add(ref scanPointer, x); + byte above = Unsafe.Add(ref prevPointer, x); scan = (byte)((scan + (above >> 1)) % 256); } else { - ref byte scan = ref Unsafe.Add(ref scanline, x); - ref byte left = ref Unsafe.Add(ref scanline, x - bytesPerPixel); - ref byte above = ref Unsafe.Add(ref previousScanline, x); + ref byte scan = ref Unsafe.Add(ref scanPointer, x); + byte left = Unsafe.Add(ref scanPointer, x - bytesPerPixel); + byte above = Unsafe.Add(ref prevPointer, x); scan = (byte)((scan + Average(left, above)) % 256); } } @@ -52,26 +55,30 @@ namespace ImageSharp.Formats /// The number of bytes per scanline /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(ref byte scanline, ref byte previousScanline, ref byte result, int bytesPerScanline, int bytesPerPixel) + public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result, int bytesPerScanline, int bytesPerPixel) { + ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); + ref byte prevPointer = ref previousScanline.DangerousGetPinnableReference(); + ref byte resultPointer = ref result.DangerousGetPinnableReference(); + // Average(x) = Raw(x) - floor((Raw(x-bpp)+Prior(x))/2) - result = 3; + resultPointer = 3; for (int x = 0; x < bytesPerScanline; x++) { if (x - bytesPerPixel < 0) { - ref byte scan = ref Unsafe.Add(ref scanline, x); - ref byte above = ref Unsafe.Add(ref previousScanline, x); - ref byte res = ref Unsafe.Add(ref result, x + 1); + byte scan = Unsafe.Add(ref scanPointer, x); + byte above = Unsafe.Add(ref prevPointer, x); + ref byte res = ref Unsafe.Add(ref resultPointer, x + 1); res = (byte)((scan - (above >> 1)) % 256); } else { - ref byte scan = ref Unsafe.Add(ref scanline, x); - ref byte left = ref Unsafe.Add(ref scanline, x - bytesPerPixel); - ref byte above = ref Unsafe.Add(ref previousScanline, x); - ref byte res = ref Unsafe.Add(ref result, x + 1); + byte scan = Unsafe.Add(ref scanPointer, x); + byte left = Unsafe.Add(ref scanPointer, x - bytesPerPixel); + byte above = Unsafe.Add(ref prevPointer, x); + ref byte res = ref Unsafe.Add(ref resultPointer, x + 1); res = (byte)((scan - Average(left, above)) % 256); } } diff --git a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs index 79de59cb9..a8e397a30 100644 --- a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs @@ -23,23 +23,26 @@ namespace ImageSharp.Formats /// The number of bytes per scanline /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(ref byte scanline, ref byte previousScanline, int bytesPerScanline, int bytesPerPixel) + public static void Decode(BufferSpan scanline, BufferSpan previousScanline, int bytesPerScanline, int bytesPerPixel) { + ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); + ref byte prevPointer = ref previousScanline.DangerousGetPinnableReference(); + // Paeth(x) + PaethPredictor(Raw(x-bpp), Prior(x), Prior(x-bpp)) for (int x = 1; x < bytesPerScanline; x++) { if (x - bytesPerPixel < 1) { - ref byte scan = ref Unsafe.Add(ref scanline, x); - ref byte above = ref Unsafe.Add(ref previousScanline, x); + ref byte scan = ref Unsafe.Add(ref scanPointer, x); + byte above = Unsafe.Add(ref prevPointer, x); scan = (byte)((scan + PaethPredicator(0, above, 0)) % 256); } else { - ref byte scan = ref Unsafe.Add(ref scanline, x); - ref byte left = ref Unsafe.Add(ref scanline, x - bytesPerPixel); - ref byte above = ref Unsafe.Add(ref previousScanline, x); - ref byte upperLeft = ref Unsafe.Add(ref previousScanline, x - bytesPerPixel); + ref byte scan = ref Unsafe.Add(ref scanPointer, x); + byte left = Unsafe.Add(ref scanPointer, x - bytesPerPixel); + byte above = Unsafe.Add(ref prevPointer, x); + byte upperLeft = Unsafe.Add(ref prevPointer, x - bytesPerPixel); scan = (byte)((scan + PaethPredicator(left, above, upperLeft)) % 256); } } @@ -54,27 +57,31 @@ namespace ImageSharp.Formats /// The number of bytes per scanline /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(ref byte scanline, ref byte previousScanline, ref byte result, int bytesPerScanline, int bytesPerPixel) + public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result, int bytesPerScanline, int bytesPerPixel) { + ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); + ref byte prevPointer = ref previousScanline.DangerousGetPinnableReference(); + ref byte resultPointer = ref result.DangerousGetPinnableReference(); + // Paeth(x) = Raw(x) - PaethPredictor(Raw(x-bpp), Prior(x), Prior(x - bpp)) - result = 4; + resultPointer = 4; for (int x = 0; x < bytesPerScanline; x++) { if (x - bytesPerPixel < 0) { - ref byte scan = ref Unsafe.Add(ref scanline, x); - ref byte above = ref Unsafe.Add(ref previousScanline, x); - ref byte res = ref Unsafe.Add(ref result, x + 1); + byte scan = Unsafe.Add(ref scanPointer, x); + byte above = Unsafe.Add(ref prevPointer, x); + ref byte res = ref Unsafe.Add(ref resultPointer, x + 1); res = (byte)((scan - PaethPredicator(0, above, 0)) % 256); } else { - ref byte scan = ref Unsafe.Add(ref scanline, x); - ref byte left = ref Unsafe.Add(ref scanline, x - bytesPerPixel); - ref byte above = ref Unsafe.Add(ref previousScanline, x); - ref byte upperLeft = ref Unsafe.Add(ref previousScanline, x - bytesPerPixel); - ref byte res = ref Unsafe.Add(ref result, x + 1); + byte scan = Unsafe.Add(ref scanPointer, x); + byte left = Unsafe.Add(ref scanPointer, x - bytesPerPixel); + byte above = Unsafe.Add(ref prevPointer, x); + byte upperLeft = Unsafe.Add(ref prevPointer, x - bytesPerPixel); + ref byte res = ref Unsafe.Add(ref resultPointer, x + 1); res = (byte)((scan - PaethPredicator(left, above, upperLeft)) % 256); } } diff --git a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs index 90d0fa692..d5ec2e7dc 100644 --- a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs @@ -21,20 +21,22 @@ namespace ImageSharp.Formats /// The number of bytes per scanline /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(ref byte scanline, int bytesPerScanline, int bytesPerPixel) + public static void Decode(BufferSpan scanline, int bytesPerScanline, int bytesPerPixel) { + ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); + // Sub(x) + Raw(x-bpp) for (int x = 1; x < bytesPerScanline; x++) { if (x - bytesPerPixel < 1) { - ref byte scan = ref Unsafe.Add(ref scanline, x); + ref byte scan = ref Unsafe.Add(ref scanPointer, x); scan = (byte)(scan % 256); } else { - ref byte scan = ref Unsafe.Add(ref scanline, x); - ref byte prev = ref Unsafe.Add(ref scanline, x - bytesPerPixel); + ref byte scan = ref Unsafe.Add(ref scanPointer, x); + byte prev = Unsafe.Add(ref scanPointer, x - bytesPerPixel); scan = (byte)((scan + prev) % 256); } } @@ -48,24 +50,27 @@ namespace ImageSharp.Formats /// The number of bytes per scanline /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(ref byte scanline, ref byte result, int bytesPerScanline, int bytesPerPixel) + public static void Encode(BufferSpan scanline, BufferSpan result, int bytesPerScanline, int bytesPerPixel) { + ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); + ref byte resultPointer = ref result.DangerousGetPinnableReference(); + // Sub(x) = Raw(x) - Raw(x-bpp) - result = 1; + resultPointer = 1; for (int x = 0; x < bytesPerScanline; x++) { if (x - bytesPerPixel < 0) { - ref byte scan = ref Unsafe.Add(ref scanline, x); - ref byte res = ref Unsafe.Add(ref result, x + 1); + byte scan = Unsafe.Add(ref scanPointer, x); + ref byte res = ref Unsafe.Add(ref resultPointer, x + 1); res = (byte)(scan % 256); } else { - ref byte scan = ref Unsafe.Add(ref scanline, x); - ref byte prev = ref Unsafe.Add(ref scanline, x - bytesPerPixel); - ref byte res = ref Unsafe.Add(ref result, x + 1); + byte scan = Unsafe.Add(ref scanPointer, x); + byte prev = Unsafe.Add(ref scanPointer, x - bytesPerPixel); + ref byte res = ref Unsafe.Add(ref resultPointer, x + 1); res = (byte)((scan - prev) % 256); } } diff --git a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs index fa6733060..30cdf65a3 100644 --- a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs @@ -21,13 +21,16 @@ namespace ImageSharp.Formats /// The previous scanline. /// The number of bytes per scanline [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(ref byte scanline, ref byte previousScanline, int bytesPerScanline) + public static void Decode(BufferSpan scanline, BufferSpan previousScanline, int bytesPerScanline) { + ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); + ref byte prevPointer = ref previousScanline.DangerousGetPinnableReference(); + // Up(x) + Prior(x) for (int x = 1; x < bytesPerScanline; x++) { - ref byte scan = ref Unsafe.Add(ref scanline, x); - ref byte above = ref Unsafe.Add(ref previousScanline, x); + ref byte scan = ref Unsafe.Add(ref scanPointer, x); + byte above = Unsafe.Add(ref prevPointer, x); scan = (byte)((scan + above) % 256); } } @@ -40,16 +43,20 @@ namespace ImageSharp.Formats /// The filtered scanline result. /// The number of bytes per scanline [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(ref byte scanline, ref byte previousScanline, ref byte result, int bytesPerScanline) + public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result, int bytesPerScanline) { + ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); + ref byte prevPointer = ref previousScanline.DangerousGetPinnableReference(); + ref byte resultPointer = ref result.DangerousGetPinnableReference(); + // Up(x) = Raw(x) - Prior(x) - result = 2; + resultPointer = 2; for (int x = 0; x < bytesPerScanline; x++) { - ref byte scan = ref Unsafe.Add(ref scanline, x); - ref byte above = ref Unsafe.Add(ref previousScanline, x); - ref byte res = ref Unsafe.Add(ref result, x + 1); + byte scan = Unsafe.Add(ref scanPointer, x); + byte above = Unsafe.Add(ref prevPointer, x); + ref byte res = ref Unsafe.Add(ref resultPointer, x + 1); res = (byte)((scan - above) % 256); } } diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index f4e1d8261..b165d0935 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -440,9 +440,7 @@ namespace ImageSharp.Formats var scanSpan = new BufferSpan(this.scanline); var prevSpan = new BufferSpan(this.previousScanline); - ref byte scanPointer = ref scanSpan.DangerousGetPinnableReference(); - ref byte prevPointer = ref prevSpan.DangerousGetPinnableReference(); - var filterType = (FilterType)scanPointer; + var filterType = (FilterType)scanSpan[0]; switch (filterType) { @@ -451,22 +449,22 @@ namespace ImageSharp.Formats case FilterType.Sub: - SubFilter.Decode(ref scanPointer, this.bytesPerScanline, this.bytesPerPixel); + SubFilter.Decode(scanSpan, this.bytesPerScanline, this.bytesPerPixel); break; case FilterType.Up: - UpFilter.Decode(ref scanPointer, ref prevPointer, this.bytesPerScanline); + UpFilter.Decode(scanSpan, prevSpan, this.bytesPerScanline); break; case FilterType.Average: - AverageFilter.Decode(ref scanPointer, ref prevPointer, this.bytesPerScanline, this.bytesPerPixel); + AverageFilter.Decode(scanSpan, prevSpan, this.bytesPerScanline, this.bytesPerPixel); break; case FilterType.Paeth: - PaethFilter.Decode(ref scanPointer, ref prevPointer, this.bytesPerScanline, this.bytesPerPixel); + PaethFilter.Decode(scanSpan, prevSpan, this.bytesPerScanline, this.bytesPerPixel); break; default: @@ -517,9 +515,7 @@ namespace ImageSharp.Formats var scanSpan = new BufferSpan(this.scanline); var prevSpan = new BufferSpan(this.previousScanline); - ref byte scanPointer = ref scanSpan.DangerousGetPinnableReference(); - ref byte prevPointer = ref prevSpan.DangerousGetPinnableReference(); - var filterType = (FilterType)scanPointer; + var filterType = (FilterType)scanSpan[0]; switch (filterType) { @@ -528,22 +524,22 @@ namespace ImageSharp.Formats case FilterType.Sub: - SubFilter.Decode(ref scanPointer, bytesPerInterlaceScanline, this.bytesPerPixel); + SubFilter.Decode(scanSpan, bytesPerInterlaceScanline, this.bytesPerPixel); break; case FilterType.Up: - UpFilter.Decode(ref scanPointer, ref prevPointer, bytesPerInterlaceScanline); + UpFilter.Decode(scanSpan, prevSpan, bytesPerInterlaceScanline); break; case FilterType.Average: - AverageFilter.Decode(ref scanPointer, ref prevPointer, bytesPerInterlaceScanline, this.bytesPerPixel); + AverageFilter.Decode(scanSpan, prevSpan, bytesPerInterlaceScanline, this.bytesPerPixel); break; case FilterType.Paeth: - PaethFilter.Decode(ref scanPointer, ref prevPointer, bytesPerInterlaceScanline, this.bytesPerPixel); + PaethFilter.Decode(scanSpan, prevSpan, bytesPerInterlaceScanline, this.bytesPerPixel); break; default: diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 342067307..2d63539c2 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -361,8 +361,6 @@ namespace ImageSharp.Formats { var scanSpan = new BufferSpan(rawScanline); var prevSpan = new BufferSpan(previousScanline); - ref byte scanPointer = ref scanSpan.DangerousGetPinnableReference(); - ref byte prevPointer = ref prevSpan.DangerousGetPinnableReference(); // Palette images don't compress well with adaptive filtering. if (this.pngColorType == PngColorType.Palette || this.bitDepth < 8) @@ -374,17 +372,15 @@ namespace ImageSharp.Formats // This order, while different to the enumerated order is more likely to produce a smaller sum // early on which shaves a couple of milliseconds off the processing time. var upSpan = new BufferSpan(this.up); - ref byte upPointer = ref upSpan.DangerousGetPinnableReference(); - UpFilter.Encode(ref scanPointer, ref prevPointer, ref upPointer, this.bytesPerScanline); + UpFilter.Encode(scanSpan, prevSpan, upSpan, this.bytesPerScanline); - int currentSum = this.CalculateTotalVariation(ref upPointer, int.MaxValue); + int currentSum = this.CalculateTotalVariation(upSpan, int.MaxValue); int lowestSum = currentSum; result = this.up; var paethSpan = new BufferSpan(this.paeth); - ref byte paethPointer = ref paethSpan.DangerousGetPinnableReference(); - PaethFilter.Encode(ref scanPointer, ref prevPointer, ref paethPointer, this.bytesPerScanline, this.bytesPerPixel); - currentSum = this.CalculateTotalVariation(ref paethPointer, currentSum); + PaethFilter.Encode(scanSpan, prevSpan, paethSpan, this.bytesPerScanline, this.bytesPerPixel); + currentSum = this.CalculateTotalVariation(paethSpan, currentSum); if (currentSum < lowestSum) { @@ -393,9 +389,8 @@ namespace ImageSharp.Formats } var subSpan = new BufferSpan(this.sub); - ref byte subPointer = ref subSpan.DangerousGetPinnableReference(); - SubFilter.Encode(ref scanPointer, ref subPointer, this.bytesPerScanline, this.bytesPerPixel); - currentSum = this.CalculateTotalVariation(ref subPointer, int.MaxValue); + SubFilter.Encode(scanSpan, subSpan, this.bytesPerScanline, this.bytesPerPixel); + currentSum = this.CalculateTotalVariation(subSpan, int.MaxValue); if (currentSum < lowestSum) { @@ -404,9 +399,8 @@ namespace ImageSharp.Formats } var averageSpan = new BufferSpan(this.average); - ref byte averagePointer = ref averageSpan.DangerousGetPinnableReference(); - AverageFilter.Encode(ref scanPointer, ref prevPointer, ref averagePointer, this.bytesPerScanline, this.bytesPerPixel); - currentSum = this.CalculateTotalVariation(ref averagePointer, currentSum); + AverageFilter.Encode(scanSpan, prevSpan, averageSpan, this.bytesPerScanline, this.bytesPerPixel); + currentSum = this.CalculateTotalVariation(averageSpan, currentSum); if (currentSum < lowestSum) { @@ -424,17 +418,18 @@ namespace ImageSharp.Formats /// The last variation sum /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - private int CalculateTotalVariation(ref byte scanline, int lastSum) + private int CalculateTotalVariation(BufferSpan scanline, int lastSum) { + ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); int sum = 0; for (int i = 1; i < this.bytesPerScanline; i++) { - ref byte v = ref Unsafe.Add(ref scanline, i); + byte v = Unsafe.Add(ref scanPointer, i); sum += v < 128 ? v : 256 - v; // No point continuing if we are larger. - if (sum > lastSum) + if (sum >= lastSum) { break; } From b5cf29c8d73d9ddee2f5dfbdecadfbd8c62b5139 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 11 May 2017 09:20:52 +1000 Subject: [PATCH 25/34] Rename base refs --- .../Formats/Png/Filters/AverageFilter.cs | 36 ++++++++--------- .../Formats/Png/Filters/PaethFilter.cs | 40 +++++++++---------- .../Formats/Png/Filters/SubFilter.cs | 24 +++++------ .../Formats/Png/Filters/UpFilter.cs | 22 +++++----- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 4 +- 5 files changed, 63 insertions(+), 63 deletions(-) diff --git a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs index aabf24b2f..fcfe698ba 100644 --- a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs @@ -24,23 +24,23 @@ namespace ImageSharp.Formats [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Decode(BufferSpan scanline, BufferSpan previousScanline, int bytesPerScanline, int bytesPerPixel) { - ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); - ref byte prevPointer = ref previousScanline.DangerousGetPinnableReference(); + ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); + ref byte prevBaseRef = ref previousScanline.DangerousGetPinnableReference(); // Average(x) + floor((Raw(x-bpp)+Prior(x))/2) for (int x = 1; x < bytesPerScanline; x++) { if (x - bytesPerPixel < 1) { - ref byte scan = ref Unsafe.Add(ref scanPointer, x); - byte above = Unsafe.Add(ref prevPointer, x); + ref byte scan = ref Unsafe.Add(ref scanBaseRef, x); + byte above = Unsafe.Add(ref prevBaseRef, x); scan = (byte)((scan + (above >> 1)) % 256); } else { - ref byte scan = ref Unsafe.Add(ref scanPointer, x); - byte left = Unsafe.Add(ref scanPointer, x - bytesPerPixel); - byte above = Unsafe.Add(ref prevPointer, x); + ref byte scan = ref Unsafe.Add(ref scanBaseRef, x); + byte left = Unsafe.Add(ref scanBaseRef, x - bytesPerPixel); + byte above = Unsafe.Add(ref prevBaseRef, x); scan = (byte)((scan + Average(left, above)) % 256); } } @@ -57,28 +57,28 @@ namespace ImageSharp.Formats [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result, int bytesPerScanline, int bytesPerPixel) { - ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); - ref byte prevPointer = ref previousScanline.DangerousGetPinnableReference(); - ref byte resultPointer = ref result.DangerousGetPinnableReference(); + ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); + ref byte prevBaseRef = ref previousScanline.DangerousGetPinnableReference(); + ref byte resultBaseRef = ref result.DangerousGetPinnableReference(); // Average(x) = Raw(x) - floor((Raw(x-bpp)+Prior(x))/2) - resultPointer = 3; + resultBaseRef = 3; for (int x = 0; x < bytesPerScanline; x++) { if (x - bytesPerPixel < 0) { - byte scan = Unsafe.Add(ref scanPointer, x); - byte above = Unsafe.Add(ref prevPointer, x); - ref byte res = ref Unsafe.Add(ref resultPointer, x + 1); + byte scan = Unsafe.Add(ref scanBaseRef, x); + byte above = Unsafe.Add(ref prevBaseRef, x); + ref byte res = ref Unsafe.Add(ref resultBaseRef, x + 1); res = (byte)((scan - (above >> 1)) % 256); } else { - byte scan = Unsafe.Add(ref scanPointer, x); - byte left = Unsafe.Add(ref scanPointer, x - bytesPerPixel); - byte above = Unsafe.Add(ref prevPointer, x); - ref byte res = ref Unsafe.Add(ref resultPointer, x + 1); + byte scan = Unsafe.Add(ref scanBaseRef, x); + byte left = Unsafe.Add(ref scanBaseRef, x - bytesPerPixel); + byte above = Unsafe.Add(ref prevBaseRef, x); + ref byte res = ref Unsafe.Add(ref resultBaseRef, x + 1); res = (byte)((scan - Average(left, above)) % 256); } } diff --git a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs index a8e397a30..b7b3e8123 100644 --- a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs @@ -25,24 +25,24 @@ namespace ImageSharp.Formats [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Decode(BufferSpan scanline, BufferSpan previousScanline, int bytesPerScanline, int bytesPerPixel) { - ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); - ref byte prevPointer = ref previousScanline.DangerousGetPinnableReference(); + ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); + ref byte prevBaseRef = ref previousScanline.DangerousGetPinnableReference(); // Paeth(x) + PaethPredictor(Raw(x-bpp), Prior(x), Prior(x-bpp)) for (int x = 1; x < bytesPerScanline; x++) { if (x - bytesPerPixel < 1) { - ref byte scan = ref Unsafe.Add(ref scanPointer, x); - byte above = Unsafe.Add(ref prevPointer, x); + ref byte scan = ref Unsafe.Add(ref scanBaseRef, x); + byte above = Unsafe.Add(ref prevBaseRef, x); scan = (byte)((scan + PaethPredicator(0, above, 0)) % 256); } else { - ref byte scan = ref Unsafe.Add(ref scanPointer, x); - byte left = Unsafe.Add(ref scanPointer, x - bytesPerPixel); - byte above = Unsafe.Add(ref prevPointer, x); - byte upperLeft = Unsafe.Add(ref prevPointer, x - bytesPerPixel); + ref byte scan = ref Unsafe.Add(ref scanBaseRef, x); + byte left = Unsafe.Add(ref scanBaseRef, x - bytesPerPixel); + byte above = Unsafe.Add(ref prevBaseRef, x); + byte upperLeft = Unsafe.Add(ref prevBaseRef, x - bytesPerPixel); scan = (byte)((scan + PaethPredicator(left, above, upperLeft)) % 256); } } @@ -59,29 +59,29 @@ namespace ImageSharp.Formats [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result, int bytesPerScanline, int bytesPerPixel) { - ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); - ref byte prevPointer = ref previousScanline.DangerousGetPinnableReference(); - ref byte resultPointer = ref result.DangerousGetPinnableReference(); + ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); + ref byte prevBaseRef = ref previousScanline.DangerousGetPinnableReference(); + ref byte resultBaseRef = ref result.DangerousGetPinnableReference(); // Paeth(x) = Raw(x) - PaethPredictor(Raw(x-bpp), Prior(x), Prior(x - bpp)) - resultPointer = 4; + resultBaseRef = 4; for (int x = 0; x < bytesPerScanline; x++) { if (x - bytesPerPixel < 0) { - byte scan = Unsafe.Add(ref scanPointer, x); - byte above = Unsafe.Add(ref prevPointer, x); - ref byte res = ref Unsafe.Add(ref resultPointer, x + 1); + byte scan = Unsafe.Add(ref scanBaseRef, x); + byte above = Unsafe.Add(ref prevBaseRef, x); + ref byte res = ref Unsafe.Add(ref resultBaseRef, x + 1); res = (byte)((scan - PaethPredicator(0, above, 0)) % 256); } else { - byte scan = Unsafe.Add(ref scanPointer, x); - byte left = Unsafe.Add(ref scanPointer, x - bytesPerPixel); - byte above = Unsafe.Add(ref prevPointer, x); - byte upperLeft = Unsafe.Add(ref prevPointer, x - bytesPerPixel); - ref byte res = ref Unsafe.Add(ref resultPointer, x + 1); + byte scan = Unsafe.Add(ref scanBaseRef, x); + byte left = Unsafe.Add(ref scanBaseRef, x - bytesPerPixel); + byte above = Unsafe.Add(ref prevBaseRef, x); + byte upperLeft = Unsafe.Add(ref prevBaseRef, x - bytesPerPixel); + ref byte res = ref Unsafe.Add(ref resultBaseRef, x + 1); res = (byte)((scan - PaethPredicator(left, above, upperLeft)) % 256); } } diff --git a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs index d5ec2e7dc..6c88f385e 100644 --- a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs @@ -23,20 +23,20 @@ namespace ImageSharp.Formats [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Decode(BufferSpan scanline, int bytesPerScanline, int bytesPerPixel) { - ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); + ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); // Sub(x) + Raw(x-bpp) for (int x = 1; x < bytesPerScanline; x++) { if (x - bytesPerPixel < 1) { - ref byte scan = ref Unsafe.Add(ref scanPointer, x); + ref byte scan = ref Unsafe.Add(ref scanBaseRef, x); scan = (byte)(scan % 256); } else { - ref byte scan = ref Unsafe.Add(ref scanPointer, x); - byte prev = Unsafe.Add(ref scanPointer, x - bytesPerPixel); + ref byte scan = ref Unsafe.Add(ref scanBaseRef, x); + byte prev = Unsafe.Add(ref scanBaseRef, x - bytesPerPixel); scan = (byte)((scan + prev) % 256); } } @@ -52,25 +52,25 @@ namespace ImageSharp.Formats [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Encode(BufferSpan scanline, BufferSpan result, int bytesPerScanline, int bytesPerPixel) { - ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); - ref byte resultPointer = ref result.DangerousGetPinnableReference(); + ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); + ref byte resultBaseRef = ref result.DangerousGetPinnableReference(); // Sub(x) = Raw(x) - Raw(x-bpp) - resultPointer = 1; + resultBaseRef = 1; for (int x = 0; x < bytesPerScanline; x++) { if (x - bytesPerPixel < 0) { - byte scan = Unsafe.Add(ref scanPointer, x); - ref byte res = ref Unsafe.Add(ref resultPointer, x + 1); + byte scan = Unsafe.Add(ref scanBaseRef, x); + ref byte res = ref Unsafe.Add(ref resultBaseRef, x + 1); res = (byte)(scan % 256); } else { - byte scan = Unsafe.Add(ref scanPointer, x); - byte prev = Unsafe.Add(ref scanPointer, x - bytesPerPixel); - ref byte res = ref Unsafe.Add(ref resultPointer, x + 1); + byte scan = Unsafe.Add(ref scanBaseRef, x); + byte prev = Unsafe.Add(ref scanBaseRef, x - bytesPerPixel); + ref byte res = ref Unsafe.Add(ref resultBaseRef, x + 1); res = (byte)((scan - prev) % 256); } } diff --git a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs index 30cdf65a3..c1969e8cb 100644 --- a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs @@ -23,14 +23,14 @@ namespace ImageSharp.Formats [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Decode(BufferSpan scanline, BufferSpan previousScanline, int bytesPerScanline) { - ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); - ref byte prevPointer = ref previousScanline.DangerousGetPinnableReference(); + ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); + ref byte prevBaseRef = ref previousScanline.DangerousGetPinnableReference(); // Up(x) + Prior(x) for (int x = 1; x < bytesPerScanline; x++) { - ref byte scan = ref Unsafe.Add(ref scanPointer, x); - byte above = Unsafe.Add(ref prevPointer, x); + ref byte scan = ref Unsafe.Add(ref scanBaseRef, x); + byte above = Unsafe.Add(ref prevBaseRef, x); scan = (byte)((scan + above) % 256); } } @@ -45,18 +45,18 @@ namespace ImageSharp.Formats [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result, int bytesPerScanline) { - ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); - ref byte prevPointer = ref previousScanline.DangerousGetPinnableReference(); - ref byte resultPointer = ref result.DangerousGetPinnableReference(); + ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); + ref byte prevBaseRef = ref previousScanline.DangerousGetPinnableReference(); + ref byte resultBaseRef = ref result.DangerousGetPinnableReference(); // Up(x) = Raw(x) - Prior(x) - resultPointer = 2; + resultBaseRef = 2; for (int x = 0; x < bytesPerScanline; x++) { - byte scan = Unsafe.Add(ref scanPointer, x); - byte above = Unsafe.Add(ref prevPointer, x); - ref byte res = ref Unsafe.Add(ref resultPointer, x + 1); + byte scan = Unsafe.Add(ref scanBaseRef, x); + byte above = Unsafe.Add(ref prevBaseRef, x); + ref byte res = ref Unsafe.Add(ref resultBaseRef, x + 1); res = (byte)((scan - above) % 256); } } diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 2d63539c2..bd3cd5fe7 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -420,12 +420,12 @@ namespace ImageSharp.Formats [MethodImpl(MethodImplOptions.AggressiveInlining)] private int CalculateTotalVariation(BufferSpan scanline, int lastSum) { - ref byte scanPointer = ref scanline.DangerousGetPinnableReference(); + ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); int sum = 0; for (int i = 1; i < this.bytesPerScanline; i++) { - byte v = Unsafe.Add(ref scanPointer, i); + byte v = Unsafe.Add(ref scanBaseRef, i); sum += v < 128 ? v : 256 - v; // No point continuing if we are larger. From cd5de8a694a8259d2fa0ab4e03a5c0f8b4f0940d Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Thu, 11 May 2017 02:17:01 +0200 Subject: [PATCH 26/34] replaced Png byte[] -s with Buffer --- .../Formats/Png/Filters/NoneFilter.cs | 5 +- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 35 ++--- src/ImageSharp/Formats/Png/PngEncoder.cs | 6 +- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 128 +++++++++++------- 4 files changed, 100 insertions(+), 74 deletions(-) diff --git a/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs b/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs index 0f67ba5fb..87d794902 100644 --- a/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs @@ -21,11 +21,12 @@ namespace ImageSharp.Formats /// The scanline to encode /// The filtered scanline result. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(byte[] scanline, byte[] result) + public static void Encode(BufferSpan scanline, BufferSpan result) { // Insert a byte before the data. result[0] = 0; - Buffer.BlockCopy(scanline, 0, result, 1, scanline.Length); + result = result.Slice(1); + BufferSpan.Copy(scanline, result); } } } diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index b165d0935..8153d61bb 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -131,12 +131,12 @@ namespace ImageSharp.Formats /// /// Previous scanline processed /// - private byte[] previousScanline; + private Buffer previousScanline; /// /// The current scanline that is being processed /// - private byte[] scanline; + private Buffer scanline; /// /// The index of the current scanline being processed @@ -252,11 +252,8 @@ namespace ImageSharp.Formats finally { pixels?.Dispose(); - if (this.previousScanline != null) - { - ArrayPool.Shared.Return(this.previousScanline); - ArrayPool.Shared.Return(this.scanline); - } + this.scanline?.Dispose(); + this.previousScanline?.Dispose(); } } @@ -345,12 +342,8 @@ namespace ImageSharp.Formats this.bytesPerSample = this.header.BitDepth / 8; } - this.previousScanline = ArrayPool.Shared.Rent(this.bytesPerScanline); - this.scanline = ArrayPool.Shared.Rent(this.bytesPerScanline); - - // Zero out the scanlines, because the bytes that are rented from the arraypool may not be zero. - Array.Clear(this.scanline, 0, this.bytesPerScanline); - Array.Clear(this.previousScanline, 0, this.bytesPerScanline); + this.previousScanline = Buffer.CreateClean(this.bytesPerScanline); + this.scanline = Buffer.CreateClean(this.bytesPerScanline); } /// @@ -429,7 +422,7 @@ namespace ImageSharp.Formats { while (this.currentRow < this.header.Height) { - int bytesRead = compressedStream.Read(this.scanline, this.currentRowBytesRead, this.bytesPerScanline - this.currentRowBytesRead); + int bytesRead = compressedStream.Read(this.scanline.Array, this.currentRowBytesRead, this.bytesPerScanline - this.currentRowBytesRead); this.currentRowBytesRead += bytesRead; if (this.currentRowBytesRead < this.bytesPerScanline) { @@ -438,8 +431,8 @@ namespace ImageSharp.Formats this.currentRowBytesRead = 0; - var scanSpan = new BufferSpan(this.scanline); - var prevSpan = new BufferSpan(this.previousScanline); + BufferSpan scanSpan = this.scanline.Span; + BufferSpan prevSpan = this.previousScanline.Span; var filterType = (FilterType)scanSpan[0]; switch (filterType) @@ -471,7 +464,7 @@ namespace ImageSharp.Formats throw new ImageFormatException("Unknown filter type."); } - this.ProcessDefilteredScanline(this.scanline, pixels); + this.ProcessDefilteredScanline(this.scanline.Array, pixels); Swap(ref this.scanline, ref this.previousScanline); this.currentRow++; @@ -504,7 +497,7 @@ namespace ImageSharp.Formats while (this.currentRow < this.header.Height) { - int bytesRead = compressedStream.Read(this.scanline, this.currentRowBytesRead, bytesPerInterlaceScanline - this.currentRowBytesRead); + int bytesRead = compressedStream.Read(this.scanline.Array, this.currentRowBytesRead, bytesPerInterlaceScanline - this.currentRowBytesRead); this.currentRowBytesRead += bytesRead; if (this.currentRowBytesRead < bytesPerInterlaceScanline) { @@ -513,8 +506,8 @@ namespace ImageSharp.Formats this.currentRowBytesRead = 0; - var scanSpan = new BufferSpan(this.scanline); - var prevSpan = new BufferSpan(this.previousScanline); + BufferSpan scanSpan = this.scanline.Span; + BufferSpan prevSpan = this.previousScanline.Span; var filterType = (FilterType)scanSpan[0]; switch (filterType) @@ -546,7 +539,7 @@ namespace ImageSharp.Formats throw new ImageFormatException("Unknown filter type."); } - this.ProcessInterlacedDefilteredScanline(this.scanline, this.currentRow, pixels, Adam7FirstColumn[this.pass], Adam7ColumnIncrement[this.pass]); + this.ProcessInterlacedDefilteredScanline(this.scanline.Array, this.currentRow, pixels, Adam7FirstColumn[this.pass], Adam7ColumnIncrement[this.pass]); Swap(ref this.scanline, ref this.previousScanline); diff --git a/src/ImageSharp/Formats/Png/PngEncoder.cs b/src/ImageSharp/Formats/Png/PngEncoder.cs index e7b6bf30e..f89b624f7 100644 --- a/src/ImageSharp/Formats/Png/PngEncoder.cs +++ b/src/ImageSharp/Formats/Png/PngEncoder.cs @@ -33,8 +33,10 @@ namespace ImageSharp.Formats public void Encode(Image image, Stream stream, IPngEncoderOptions options) where TPixel : struct, IPixel { - PngEncoderCore encode = new PngEncoderCore(options); - encode.Encode(image, stream); + using (var encode = new PngEncoderCore(options)) + { + encode.Encode(image, stream); + } } } } diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index bd3cd5fe7..e7ec7d243 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -19,7 +19,7 @@ namespace ImageSharp.Formats /// /// Performs the png encoding operation. /// - internal sealed class PngEncoderCore + internal sealed class PngEncoderCore : IDisposable { /// /// The maximum block size, defaults at 64k for uncompressed blocks. @@ -72,29 +72,44 @@ namespace ImageSharp.Formats private int bytesPerPixel; /// - /// The number of bytes per scanline + /// The number of bytes per scanline. /// private int bytesPerScanline; + /// + /// The previous scanline. + /// + private Buffer previousScanline; + + /// + /// The raw scanline. + /// + private Buffer rawScanline; + + /// + /// The filtered scanline result. + /// + private Buffer result; + /// /// The buffer for the sub filter /// - private byte[] sub; + private Buffer sub; /// /// The buffer for the up filter /// - private byte[] up; + private Buffer up; /// /// The buffer for the average filter /// - private byte[] average; + private Buffer average; /// /// The buffer for the paeth filter /// - private byte[] paeth; + private Buffer paeth; /// /// The quality of output for images. @@ -212,6 +227,20 @@ namespace ImageSharp.Formats stream.Flush(); } + /// + /// Disposes PngEncoderCore instance, disposing it's internal buffers. + /// + public void Dispose() + { + this.previousScanline?.Dispose(); + this.rawScanline?.Dispose(); + this.result?.Dispose(); + this.sub?.Dispose(); + this.up?.Dispose(); + this.average?.Dispose(); + this.paeth?.Dispose(); + } + /// /// Writes an integer to the byte array. /// @@ -273,10 +302,11 @@ namespace ImageSharp.Formats /// The pixel format. /// The image pixels accessor. /// The row index. - /// The raw scanline. - private void CollectGrayscaleBytes(PixelAccessor pixels, int row, byte[] rawScanline) + private void CollectGrayscaleBytes(PixelAccessor pixels, int row) where TPixel : struct, IPixel { + byte[] rawScanlineArray = this.rawScanline.Array; + // Copy the pixels across from the image. // Reuse the chunk type buffer. for (int x = 0; x < this.width; x++) @@ -291,11 +321,11 @@ namespace ImageSharp.Formats { if (i == 0) { - rawScanline[offset] = luminance; + rawScanlineArray[offset] = luminance; } else { - rawScanline[offset + i] = this.chunkTypeBuffer[3]; + rawScanlineArray[offset + i] = this.chunkTypeBuffer[3]; } } } @@ -307,14 +337,18 @@ namespace ImageSharp.Formats /// The pixel format. /// The image pixel accessor. /// The row index. - /// The raw scanline. - private void CollecTPixelBytes(PixelAccessor pixels, int row, byte[] rawScanline) + private void CollecTPixelBytes(PixelAccessor pixels, int row) where TPixel : struct, IPixel { - // We can use the optimized PixelAccessor here and copy the bytes in unmanaged memory. - using (var pixelRow = new PixelArea(this.width, rawScanline, this.bytesPerPixel == 4 ? ComponentOrder.Xyzw : ComponentOrder.Xyz)) + BufferSpan rowSpan = pixels.GetRowSpan(row); + + if (this.bytesPerPixel == 4) + { + PixelOperations.Instance.ToXyzwBytes(rowSpan, this.rawScanline, this.width); + } + else { - pixels.CopyTo(pixelRow, row); + PixelOperations.Instance.ToXyzBytes(rowSpan, this.rawScanline, this.width); } } @@ -325,89 +359,83 @@ namespace ImageSharp.Formats /// The pixel format. /// The image pixel accessor. /// The row. - /// The previous scanline. - /// The raw scanline. - /// The filtered scanline result. /// The - private byte[] EncodePixelRow(PixelAccessor pixels, int row, byte[] previousScanline, byte[] rawScanline, byte[] result) + private Buffer EncodePixelRow(PixelAccessor pixels, int row) where TPixel : struct, IPixel { switch (this.pngColorType) { case PngColorType.Palette: - Buffer.BlockCopy(this.palettePixelData, row * rawScanline.Length, rawScanline, 0, rawScanline.Length); + Buffer.BlockCopy(this.palettePixelData, row * this.rawScanline.Length, this.rawScanline.Array, 0, this.rawScanline.Length); break; case PngColorType.Grayscale: case PngColorType.GrayscaleWithAlpha: - this.CollectGrayscaleBytes(pixels, row, rawScanline); + this.CollectGrayscaleBytes(pixels, row); break; default: - this.CollecTPixelBytes(pixels, row, rawScanline); + this.CollecTPixelBytes(pixels, row); break; } - return this.GetOptimalFilteredScanline(rawScanline, previousScanline, result); + return this.GetOptimalFilteredScanline(); } /// /// Applies all PNG filters to the given scanline and returns the filtered scanline that is deemed /// to be most compressible, using lowest total variation as proxy for compressibility. /// - /// The raw scanline - /// The previous scanline - /// The filtered scanline result. /// The - private byte[] GetOptimalFilteredScanline(byte[] rawScanline, byte[] previousScanline, byte[] result) + private Buffer GetOptimalFilteredScanline() { - var scanSpan = new BufferSpan(rawScanline); - var prevSpan = new BufferSpan(previousScanline); + BufferSpan scanSpan = this.rawScanline.Span; + BufferSpan prevSpan = this.previousScanline.Span; // Palette images don't compress well with adaptive filtering. if (this.pngColorType == PngColorType.Palette || this.bitDepth < 8) { - NoneFilter.Encode(rawScanline, result); - return result; + NoneFilter.Encode(this.rawScanline, this.result); + return this.result; } // This order, while different to the enumerated order is more likely to produce a smaller sum // early on which shaves a couple of milliseconds off the processing time. - var upSpan = new BufferSpan(this.up); + BufferSpan upSpan = this.up.Span; UpFilter.Encode(scanSpan, prevSpan, upSpan, this.bytesPerScanline); int currentSum = this.CalculateTotalVariation(upSpan, int.MaxValue); int lowestSum = currentSum; - result = this.up; + Buffer actualResult = this.up; - var paethSpan = new BufferSpan(this.paeth); + BufferSpan paethSpan = this.paeth.Span; PaethFilter.Encode(scanSpan, prevSpan, paethSpan, this.bytesPerScanline, this.bytesPerPixel); currentSum = this.CalculateTotalVariation(paethSpan, currentSum); if (currentSum < lowestSum) { lowestSum = currentSum; - result = this.paeth; + actualResult = this.paeth; } - var subSpan = new BufferSpan(this.sub); + BufferSpan subSpan = this.sub.Span; SubFilter.Encode(scanSpan, subSpan, this.bytesPerScanline, this.bytesPerPixel); currentSum = this.CalculateTotalVariation(subSpan, int.MaxValue); if (currentSum < lowestSum) { lowestSum = currentSum; - result = this.sub; + actualResult = this.sub; } - var averageSpan = new BufferSpan(this.average); + BufferSpan averageSpan = this.average.Span; AverageFilter.Encode(scanSpan, prevSpan, averageSpan, this.bytesPerScanline, this.bytesPerPixel); currentSum = this.CalculateTotalVariation(averageSpan, currentSum); if (currentSum < lowestSum) { - result = this.average; + actualResult = this.average; } - return result; + return actualResult; } /// @@ -617,17 +645,18 @@ namespace ImageSharp.Formats where TPixel : struct, IPixel { this.bytesPerScanline = this.width * this.bytesPerPixel; - byte[] previousScanline = new byte[this.bytesPerScanline]; - byte[] rawScanline = new byte[this.bytesPerScanline]; int resultLength = this.bytesPerScanline + 1; - byte[] result = new byte[resultLength]; + + this.previousScanline = new Buffer(this.bytesPerScanline); + this.rawScanline = new Buffer(this.bytesPerScanline); + this.result = new Buffer(resultLength); if (this.pngColorType != PngColorType.Palette) { - this.sub = new byte[resultLength]; - this.up = new byte[resultLength]; - this.average = new byte[resultLength]; - this.paeth = new byte[resultLength]; + this.sub = Buffer.CreateClean(resultLength); + this.up = Buffer.CreateClean(resultLength); + this.average = Buffer.CreateClean(resultLength); + this.paeth = Buffer.CreateClean(resultLength); } byte[] buffer; @@ -640,9 +669,10 @@ namespace ImageSharp.Formats { for (int y = 0; y < this.height; y++) { - deflateStream.Write(this.EncodePixelRow(pixels, y, previousScanline, rawScanline, result), 0, resultLength); + Buffer r = this.EncodePixelRow(pixels, y); + deflateStream.Write(r.Array, 0, resultLength); - Swap(ref rawScanline, ref previousScanline); + Swap(ref this.rawScanline, ref this.previousScanline); } } From a2113fab95267f30817474bf5e5c58adab398c5f Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Thu, 11 May 2017 02:53:04 +0200 Subject: [PATCH 27/34] removed bytesPerScanline parameters, added param checking --- src/ImageSharp/Common/Helpers/Guard.cs | 38 +++++++++++++++++++ .../Formats/Png/Filters/AverageFilter.cs | 17 +++++---- .../Formats/Png/Filters/PaethFilter.cs | 15 +++++--- .../Formats/Png/Filters/SubFilter.cs | 12 +++--- .../Formats/Png/Filters/UpFilter.cs | 15 +++++--- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 25 ++++++------ src/ImageSharp/Formats/Png/PngEncoderCore.cs | 20 ++++------ 7 files changed, 91 insertions(+), 51 deletions(-) diff --git a/src/ImageSharp/Common/Helpers/Guard.cs b/src/ImageSharp/Common/Helpers/Guard.cs index cf307e936..41a715af9 100644 --- a/src/ImageSharp/Common/Helpers/Guard.cs +++ b/src/ImageSharp/Common/Helpers/Guard.cs @@ -230,5 +230,43 @@ namespace ImageSharp throw new ArgumentException(message, parameterName); } } + + /// + /// Verifies, that the target span is of same size than the 'other' span. + /// + /// The element type of the spans + /// The target span. + /// The 'other' span to compare 'target' to. + /// The name of the parameter that is to be checked. + /// + /// is true + /// + public static void MustBeSameSized(BufferSpan target, BufferSpan other, string parameterName) + where T : struct + { + if (target.Length != other.Length) + { + throw new ArgumentException("Span-s must be the same size!", parameterName); + } + } + + /// + /// Verifies, that the `target` span has the length of 'minSpan', or longer. + /// + /// The element type of the spans + /// The target span. + /// The 'minSpan' span to compare 'target' to. + /// The name of the parameter that is to be checked. + /// + /// is true + /// + public static void MustBeSizedAtLeast(BufferSpan target, BufferSpan minSpan, string parameterName) + where T : struct + { + if (target.Length < minSpan.Length) + { + throw new ArgumentException($"Span-s must be at least of length {minSpan.Length}!", parameterName); + } + } } } diff --git a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs index fcfe698ba..80bcb653a 100644 --- a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs @@ -12,23 +12,24 @@ namespace ImageSharp.Formats /// the value of a pixel. /// /// - internal static unsafe class AverageFilter + internal static class AverageFilter { /// /// Decodes the scanline /// /// The scanline to decode /// The previous scanline. - /// The number of bytes per scanline /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(BufferSpan scanline, BufferSpan previousScanline, int bytesPerScanline, int bytesPerPixel) + public static void Decode(BufferSpan scanline, BufferSpan previousScanline, int bytesPerPixel) { + Guard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); + ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); ref byte prevBaseRef = ref previousScanline.DangerousGetPinnableReference(); // Average(x) + floor((Raw(x-bpp)+Prior(x))/2) - for (int x = 1; x < bytesPerScanline; x++) + for (int x = 1; x < scanline.Length; x++) { if (x - bytesPerPixel < 1) { @@ -52,11 +53,13 @@ namespace ImageSharp.Formats /// The scanline to encode /// The previous scanline. /// The filtered scanline result. - /// The number of bytes per scanline /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result, int bytesPerScanline, int bytesPerPixel) + public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result, int bytesPerPixel) { + Guard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); + Guard.MustBeSizedAtLeast(result, scanline, nameof(result)); + ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); ref byte prevBaseRef = ref previousScanline.DangerousGetPinnableReference(); ref byte resultBaseRef = ref result.DangerousGetPinnableReference(); @@ -64,7 +67,7 @@ namespace ImageSharp.Formats // Average(x) = Raw(x) - floor((Raw(x-bpp)+Prior(x))/2) resultBaseRef = 3; - for (int x = 0; x < bytesPerScanline; x++) + for (int x = 0; x < scanline.Length; x++) { if (x - bytesPerPixel < 0) { diff --git a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs index b7b3e8123..9f21f0759 100644 --- a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs @@ -20,16 +20,17 @@ namespace ImageSharp.Formats /// /// The scanline to decode /// The previous scanline. - /// The number of bytes per scanline /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(BufferSpan scanline, BufferSpan previousScanline, int bytesPerScanline, int bytesPerPixel) + public static void Decode(BufferSpan scanline, BufferSpan previousScanline, int bytesPerPixel) { + Guard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); + ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); ref byte prevBaseRef = ref previousScanline.DangerousGetPinnableReference(); // Paeth(x) + PaethPredictor(Raw(x-bpp), Prior(x), Prior(x-bpp)) - for (int x = 1; x < bytesPerScanline; x++) + for (int x = 1; x < scanline.Length; x++) { if (x - bytesPerPixel < 1) { @@ -54,11 +55,13 @@ namespace ImageSharp.Formats /// The scanline to encode /// The previous scanline. /// The filtered scanline result. - /// The number of bytes per scanline /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result, int bytesPerScanline, int bytesPerPixel) + public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result, int bytesPerPixel) { + Guard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); + Guard.MustBeSizedAtLeast(result, scanline, nameof(result)); + ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); ref byte prevBaseRef = ref previousScanline.DangerousGetPinnableReference(); ref byte resultBaseRef = ref result.DangerousGetPinnableReference(); @@ -66,7 +69,7 @@ namespace ImageSharp.Formats // Paeth(x) = Raw(x) - PaethPredictor(Raw(x-bpp), Prior(x), Prior(x - bpp)) resultBaseRef = 4; - for (int x = 0; x < bytesPerScanline; x++) + for (int x = 0; x < scanline.Length; x++) { if (x - bytesPerPixel < 0) { diff --git a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs index 6c88f385e..3a5396cb3 100644 --- a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs @@ -18,15 +18,14 @@ namespace ImageSharp.Formats /// Decodes the scanline /// /// The scanline to decode - /// The number of bytes per scanline /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(BufferSpan scanline, int bytesPerScanline, int bytesPerPixel) + public static void Decode(BufferSpan scanline, int bytesPerPixel) { ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); // Sub(x) + Raw(x-bpp) - for (int x = 1; x < bytesPerScanline; x++) + for (int x = 1; x < scanline.Length; x++) { if (x - bytesPerPixel < 1) { @@ -47,18 +46,19 @@ namespace ImageSharp.Formats /// /// The scanline to encode /// The filtered scanline result. - /// The number of bytes per scanline /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(BufferSpan scanline, BufferSpan result, int bytesPerScanline, int bytesPerPixel) + public static void Encode(BufferSpan scanline, BufferSpan result, int bytesPerPixel) { + Guard.MustBeSizedAtLeast(result, scanline, nameof(result)); + ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); ref byte resultBaseRef = ref result.DangerousGetPinnableReference(); // Sub(x) = Raw(x) - Raw(x-bpp) resultBaseRef = 1; - for (int x = 0; x < bytesPerScanline; x++) + for (int x = 0; x < scanline.Length; x++) { if (x - bytesPerPixel < 0) { diff --git a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs index c1969e8cb..b30c49c45 100644 --- a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs @@ -19,15 +19,16 @@ namespace ImageSharp.Formats /// /// The scanline to decode /// The previous scanline. - /// The number of bytes per scanline [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(BufferSpan scanline, BufferSpan previousScanline, int bytesPerScanline) + public static void Decode(BufferSpan scanline, BufferSpan previousScanline) { + Guard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); + ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); ref byte prevBaseRef = ref previousScanline.DangerousGetPinnableReference(); // Up(x) + Prior(x) - for (int x = 1; x < bytesPerScanline; x++) + for (int x = 1; x < scanline.Length; x++) { ref byte scan = ref Unsafe.Add(ref scanBaseRef, x); byte above = Unsafe.Add(ref prevBaseRef, x); @@ -41,10 +42,12 @@ namespace ImageSharp.Formats /// The scanline to encode /// The previous scanline. /// The filtered scanline result. - /// The number of bytes per scanline [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result, int bytesPerScanline) + public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result) { + Guard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); + Guard.MustBeSizedAtLeast(result, scanline, nameof(result)); + ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); ref byte prevBaseRef = ref previousScanline.DangerousGetPinnableReference(); ref byte resultBaseRef = ref result.DangerousGetPinnableReference(); @@ -52,7 +55,7 @@ namespace ImageSharp.Formats // Up(x) = Raw(x) - Prior(x) resultBaseRef = 2; - for (int x = 0; x < bytesPerScanline; x++) + for (int x = 0; x < scanline.Length; x++) { byte scan = Unsafe.Add(ref scanBaseRef, x); byte above = Unsafe.Add(ref prevBaseRef, x); diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 8153d61bb..13b4c1167 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -430,10 +430,7 @@ namespace ImageSharp.Formats } this.currentRowBytesRead = 0; - - BufferSpan scanSpan = this.scanline.Span; - BufferSpan prevSpan = this.previousScanline.Span; - var filterType = (FilterType)scanSpan[0]; + var filterType = (FilterType)this.scanline[0]; switch (filterType) { @@ -442,22 +439,22 @@ namespace ImageSharp.Formats case FilterType.Sub: - SubFilter.Decode(scanSpan, this.bytesPerScanline, this.bytesPerPixel); + SubFilter.Decode(this.scanline, this.bytesPerPixel); break; case FilterType.Up: - UpFilter.Decode(scanSpan, prevSpan, this.bytesPerScanline); + UpFilter.Decode(this.scanline, this.previousScanline); break; case FilterType.Average: - AverageFilter.Decode(scanSpan, prevSpan, this.bytesPerScanline, this.bytesPerPixel); + AverageFilter.Decode(this.scanline, this.previousScanline, this.bytesPerPixel); break; case FilterType.Paeth: - PaethFilter.Decode(scanSpan, prevSpan, this.bytesPerScanline, this.bytesPerPixel); + PaethFilter.Decode(this.scanline, this.previousScanline, this.bytesPerPixel); break; default: @@ -506,8 +503,8 @@ namespace ImageSharp.Formats this.currentRowBytesRead = 0; - BufferSpan scanSpan = this.scanline.Span; - BufferSpan prevSpan = this.previousScanline.Span; + BufferSpan scanSpan = this.scanline.Slice(0, bytesPerInterlaceScanline); + BufferSpan prevSpan = this.previousScanline.Span.Slice(0, bytesPerInterlaceScanline); var filterType = (FilterType)scanSpan[0]; switch (filterType) @@ -517,22 +514,22 @@ namespace ImageSharp.Formats case FilterType.Sub: - SubFilter.Decode(scanSpan, bytesPerInterlaceScanline, this.bytesPerPixel); + SubFilter.Decode(scanSpan, this.bytesPerPixel); break; case FilterType.Up: - UpFilter.Decode(scanSpan, prevSpan, bytesPerInterlaceScanline); + UpFilter.Decode(scanSpan, prevSpan); break; case FilterType.Average: - AverageFilter.Decode(scanSpan, prevSpan, bytesPerInterlaceScanline, this.bytesPerPixel); + AverageFilter.Decode(scanSpan, prevSpan, this.bytesPerPixel); break; case FilterType.Paeth: - PaethFilter.Decode(scanSpan, prevSpan, bytesPerInterlaceScanline, this.bytesPerPixel); + PaethFilter.Decode(scanSpan, prevSpan, this.bytesPerPixel); break; default: diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index e7ec7d243..44f1513ee 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -399,16 +399,14 @@ namespace ImageSharp.Formats // This order, while different to the enumerated order is more likely to produce a smaller sum // early on which shaves a couple of milliseconds off the processing time. - BufferSpan upSpan = this.up.Span; - UpFilter.Encode(scanSpan, prevSpan, upSpan, this.bytesPerScanline); + UpFilter.Encode(scanSpan, prevSpan, this.up); - int currentSum = this.CalculateTotalVariation(upSpan, int.MaxValue); + int currentSum = this.CalculateTotalVariation(this.up, int.MaxValue); int lowestSum = currentSum; Buffer actualResult = this.up; - BufferSpan paethSpan = this.paeth.Span; - PaethFilter.Encode(scanSpan, prevSpan, paethSpan, this.bytesPerScanline, this.bytesPerPixel); - currentSum = this.CalculateTotalVariation(paethSpan, currentSum); + PaethFilter.Encode(scanSpan, prevSpan, this.paeth, this.bytesPerPixel); + currentSum = this.CalculateTotalVariation(this.paeth, currentSum); if (currentSum < lowestSum) { @@ -416,9 +414,8 @@ namespace ImageSharp.Formats actualResult = this.paeth; } - BufferSpan subSpan = this.sub.Span; - SubFilter.Encode(scanSpan, subSpan, this.bytesPerScanline, this.bytesPerPixel); - currentSum = this.CalculateTotalVariation(subSpan, int.MaxValue); + SubFilter.Encode(scanSpan, this.sub, this.bytesPerPixel); + currentSum = this.CalculateTotalVariation(this.sub, int.MaxValue); if (currentSum < lowestSum) { @@ -426,9 +423,8 @@ namespace ImageSharp.Formats actualResult = this.sub; } - BufferSpan averageSpan = this.average.Span; - AverageFilter.Encode(scanSpan, prevSpan, averageSpan, this.bytesPerScanline, this.bytesPerPixel); - currentSum = this.CalculateTotalVariation(averageSpan, currentSum); + AverageFilter.Encode(scanSpan, prevSpan, this.average, this.bytesPerPixel); + currentSum = this.CalculateTotalVariation(this.average, currentSum); if (currentSum < lowestSum) { From 4e0737a0c221a13cead5adbf74cac5da15733a40 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 11 May 2017 11:50:23 +1000 Subject: [PATCH 28/34] Use DebugGuard --- src/ImageSharp/Common/Helpers/DebugGuard.cs | 38 +++++++++++++++++++ src/ImageSharp/Common/Helpers/Guard.cs | 38 ------------------- .../Formats/Png/Filters/PaethFilter.cs | 6 +-- .../Formats/Png/Filters/SubFilter.cs | 2 +- .../Formats/Png/Filters/UpFilter.cs | 6 +-- 5 files changed, 45 insertions(+), 45 deletions(-) diff --git a/src/ImageSharp/Common/Helpers/DebugGuard.cs b/src/ImageSharp/Common/Helpers/DebugGuard.cs index c1fa46191..f7c83452f 100644 --- a/src/ImageSharp/Common/Helpers/DebugGuard.cs +++ b/src/ImageSharp/Common/Helpers/DebugGuard.cs @@ -159,5 +159,43 @@ namespace ImageSharp throw new ArgumentException(message, parameterName); } } + + /// + /// Verifies, that the target span is of same size than the 'other' span. + /// + /// The element type of the spans + /// The target span. + /// The 'other' span to compare 'target' to. + /// The name of the parameter that is to be checked. + /// + /// is true + /// + public static void MustBeSameSized(BufferSpan target, BufferSpan other, string parameterName) + where T : struct + { + if (target.Length != other.Length) + { + throw new ArgumentException("Span-s must be the same size!", parameterName); + } + } + + /// + /// Verifies, that the `target` span has the length of 'minSpan', or longer. + /// + /// The element type of the spans + /// The target span. + /// The 'minSpan' span to compare 'target' to. + /// The name of the parameter that is to be checked. + /// + /// is true + /// + public static void MustBeSizedAtLeast(BufferSpan target, BufferSpan minSpan, string parameterName) + where T : struct + { + if (target.Length < minSpan.Length) + { + throw new ArgumentException($"Span-s must be at least of length {minSpan.Length}!", parameterName); + } + } } } \ No newline at end of file diff --git a/src/ImageSharp/Common/Helpers/Guard.cs b/src/ImageSharp/Common/Helpers/Guard.cs index 41a715af9..cf307e936 100644 --- a/src/ImageSharp/Common/Helpers/Guard.cs +++ b/src/ImageSharp/Common/Helpers/Guard.cs @@ -230,43 +230,5 @@ namespace ImageSharp throw new ArgumentException(message, parameterName); } } - - /// - /// Verifies, that the target span is of same size than the 'other' span. - /// - /// The element type of the spans - /// The target span. - /// The 'other' span to compare 'target' to. - /// The name of the parameter that is to be checked. - /// - /// is true - /// - public static void MustBeSameSized(BufferSpan target, BufferSpan other, string parameterName) - where T : struct - { - if (target.Length != other.Length) - { - throw new ArgumentException("Span-s must be the same size!", parameterName); - } - } - - /// - /// Verifies, that the `target` span has the length of 'minSpan', or longer. - /// - /// The element type of the spans - /// The target span. - /// The 'minSpan' span to compare 'target' to. - /// The name of the parameter that is to be checked. - /// - /// is true - /// - public static void MustBeSizedAtLeast(BufferSpan target, BufferSpan minSpan, string parameterName) - where T : struct - { - if (target.Length < minSpan.Length) - { - throw new ArgumentException($"Span-s must be at least of length {minSpan.Length}!", parameterName); - } - } } } diff --git a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs index 9f21f0759..4226596f8 100644 --- a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs @@ -24,7 +24,7 @@ namespace ImageSharp.Formats [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Decode(BufferSpan scanline, BufferSpan previousScanline, int bytesPerPixel) { - Guard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); + DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); ref byte prevBaseRef = ref previousScanline.DangerousGetPinnableReference(); @@ -59,8 +59,8 @@ namespace ImageSharp.Formats [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result, int bytesPerPixel) { - Guard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); - Guard.MustBeSizedAtLeast(result, scanline, nameof(result)); + DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); + DebugGuard.MustBeSizedAtLeast(result, scanline, nameof(result)); ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); ref byte prevBaseRef = ref previousScanline.DangerousGetPinnableReference(); diff --git a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs index 3a5396cb3..d40f261ed 100644 --- a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs @@ -50,7 +50,7 @@ namespace ImageSharp.Formats [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Encode(BufferSpan scanline, BufferSpan result, int bytesPerPixel) { - Guard.MustBeSizedAtLeast(result, scanline, nameof(result)); + DebugGuard.MustBeSizedAtLeast(result, scanline, nameof(result)); ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); ref byte resultBaseRef = ref result.DangerousGetPinnableReference(); diff --git a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs index b30c49c45..fa3ef57da 100644 --- a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs @@ -22,7 +22,7 @@ namespace ImageSharp.Formats [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Decode(BufferSpan scanline, BufferSpan previousScanline) { - Guard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); + DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); ref byte prevBaseRef = ref previousScanline.DangerousGetPinnableReference(); @@ -45,8 +45,8 @@ namespace ImageSharp.Formats [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result) { - Guard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); - Guard.MustBeSizedAtLeast(result, scanline, nameof(result)); + DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); + DebugGuard.MustBeSizedAtLeast(result, scanline, nameof(result)); ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); ref byte prevBaseRef = ref previousScanline.DangerousGetPinnableReference(); From 1bc7fee3fb8bd262eb58037e72dff76a701cd56d Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 11 May 2017 11:51:41 +1000 Subject: [PATCH 29/34] Add missed file --- src/ImageSharp/Formats/Png/Filters/AverageFilter.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs index 80bcb653a..d3b32d977 100644 --- a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs @@ -23,7 +23,7 @@ namespace ImageSharp.Formats [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Decode(BufferSpan scanline, BufferSpan previousScanline, int bytesPerPixel) { - Guard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); + DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); ref byte prevBaseRef = ref previousScanline.DangerousGetPinnableReference(); @@ -57,8 +57,8 @@ namespace ImageSharp.Formats [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result, int bytesPerPixel) { - Guard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); - Guard.MustBeSizedAtLeast(result, scanline, nameof(result)); + DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); + DebugGuard.MustBeSizedAtLeast(result, scanline, nameof(result)); ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); ref byte prevBaseRef = ref previousScanline.DangerousGetPinnableReference(); From 9984ffa34883bb61ec4a29eeb1fb81169dfe1997 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Fri, 12 May 2017 00:17:05 +1000 Subject: [PATCH 30/34] Replace BufferSpan with Span - Add System.Memory - Update System.Runtime.CompilerServices.Unsafe - Move memory classes/structs to ImageSharp.Memory - Replace BufferSpan with Span and adjust methods accordingly --- .../Brushes/ImageBrush{TPixel}.cs | 9 +- .../Brushes/PatternBrush{TPixel}.cs | 6 +- .../Brushes/Processors/BrushApplicator.cs | 6 +- .../Brushes/RecolorBrush{TPixel}.cs | 7 +- .../Brushes/SolidBrush{TPixel}.cs | 7 +- .../Processors/DrawImageProcessor.cs | 6 +- .../Processors/DrawPathProcessor.cs | 4 +- .../Processors/FillProcessor.cs | 2 + .../Processors/FillRegionProcessor.cs | 2 + src/ImageSharp/Common/Helpers/DebugGuard.cs | 4 +- src/ImageSharp/Common/Memory/BufferSpan{T}.cs | 205 ------------------ .../Dithering/ErrorDiffusion/Atkinson.cs | 2 + .../Dithering/ErrorDiffusion/Burks.cs | 2 + .../Dithering/ErrorDiffusion/ErrorDiffuser.cs | 1 + .../ErrorDiffusion/FloydSteinberg.cs | 2 + .../ErrorDiffusion/JarvisJudiceNinke.cs | 2 + .../Dithering/ErrorDiffusion/Sierra2.cs | 2 + .../Dithering/ErrorDiffusion/Sierra3.cs | 2 + .../Dithering/ErrorDiffusion/SierraLite.cs | 2 + .../Dithering/ErrorDiffusion/Stucki.cs | 2 + src/ImageSharp/Dithering/Ordered/Bayer.cs | 2 + src/ImageSharp/Dithering/Ordered/Ordered.cs | 2 + .../Dithering/Ordered/OrderedDither4x4.cs | 1 + .../Components/Decoder/JpegBlockProcessor.cs | 2 + .../Jpeg/Components/Decoder/JpegPixelArea.cs | 2 + .../Components/Decoder/JpegScanDecoder.cs | 2 + .../Jpeg/Components/Decoder/YCbCrImage.cs | 2 + .../Formats/Jpeg/JpegDecoderCore.cs | 1 + .../Formats/Png/Filters/AverageFilter.cs | 5 +- .../Formats/Png/Filters/NoneFilter.cs | 6 +- .../Formats/Png/Filters/PaethFilter.cs | 5 +- .../Formats/Png/Filters/SubFilter.cs | 5 +- .../Formats/Png/Filters/UpFilter.cs | 5 +- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 9 +- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 14 +- src/ImageSharp/Image/ImageBase{TPixel}.cs | 2 + src/ImageSharp/Image/PixelAccessor{TPixel}.cs | 54 +++-- src/ImageSharp/Image/PixelArea{TPixel}.cs | 8 +- src/ImageSharp/ImageSharp.csproj | 3 +- src/ImageSharp/{Common => }/Memory/Buffer.cs | 31 ++- .../{Common => }/Memory/Buffer2D.cs | 3 +- .../{Common => }/Memory/Buffer2DExtensions.cs | 14 +- .../{Common => }/Memory/Fast2DArray{T}.cs | 6 +- .../{Common => }/Memory/IBuffer2D.cs | 8 +- .../{Common => }/Memory/PixelDataPool{T}.cs | 3 +- .../BufferSpan.cs => Memory/SpanHelper.cs} | 24 +- .../DefaultAddPixelBlender{TPixel}.cs | 10 +- .../DefaultDarkenPixelBlender{TPixel}.cs | 10 +- .../DefaultHardLightPixelBlender{TPixel}.cs | 10 +- .../DefaultLightenPixelBlender{TPixel}.cs | 10 +- .../DefaultMultiplyPixelBlender{TPixel}.cs | 10 +- .../DefaultNormalPixelBlender{TPixel}.cs | 10 +- .../DefaultOverlayPixelBlender{TPixel}.cs | 10 +- .../DefaultScreenPixelBlender{TPixel}.cs | 10 +- .../DefaultSubstractPixelBlender{TPixel}.cs | 10 +- .../PixelFormats/PixelBlender{TPixel}.cs | 4 +- .../PixelFormats/PixelOperations{TPixel}.cs | 75 ++++--- .../PixelFormats/Rgba32.PixelOperations.cs | 35 +-- .../RgbaVector.PixelOperations.cs | 7 +- .../Convolution/BoxBlurProcessor.cs | 1 + .../Convolution/Convolution2DProcessor.cs | 1 + .../Convolution/Convolution2PassProcessor.cs | 1 + .../Convolution/ConvolutionProcessor.cs | 1 + .../EdgeDetection/EdgeDetector2DProcessor.cs | 1 + .../EdgeDetectorCompassProcessor.cs | 1 + .../EdgeDetection/EdgeDetectorProcessor.cs | 1 + .../EdgeDetection/KayyaliProcessor.cs | 1 + .../EdgeDetection/KirschProcessor.cs | 1 + .../EdgeDetection/Laplacian3X3Processor.cs | 1 + .../EdgeDetection/Laplacian5X5Processor.cs | 1 + .../LaplacianOfGaussianProcessor.cs | 1 + .../EdgeDetection/PrewittProcessor.cs | 1 + .../EdgeDetection/RobertsCrossProcessor.cs | 1 + .../EdgeDetection/RobinsonProcessor.cs | 1 + .../EdgeDetection/ScharrProcessor.cs | 1 + .../EdgeDetection/SobelProcessor.cs | 1 + .../Convolution/GaussianBlurProcessor.cs | 1 + .../Convolution/GaussianSharpenProcessor.cs | 1 + .../Effects/BackgroundColorProcessor.cs | 3 +- .../Processors/Overlays/GlowProcessor.cs | 3 +- .../Processors/Overlays/VignetteProcessor.cs | 3 +- .../ResamplingWeightedProcessor.Weights.cs | 16 +- .../Processors/Transforms/ResizeProcessor.cs | 3 +- .../Bulk/PackFromVector4ReferenceVsPointer.cs | 2 +- .../Color/Bulk/PackFromXyzw.cs | 1 + .../Color/Bulk/ToVector4.cs | 1 + .../ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs | 5 +- .../Color/Bulk/ToXyzw.cs | 8 +- .../ImageSharp.Benchmarks/General/Array2D.cs | 2 + .../General/ClearBuffer.cs | 3 +- .../General/IterateArray.cs | 2 + .../General/PixelIndexing.cs | 2 + .../General/Vectorization/VectorFetching.cs | 6 +- .../PixelBlenders/PorterDuffBulkVsPixel.cs | 17 +- tests/ImageSharp.Benchmarks/Samplers/Glow.cs | 2 + .../ImageSharp.Benchmarks/Samplers/Resize.cs | 54 ++--- .../Colors/PixelOperationsTests.cs | 17 +- .../ImageSharp.Tests/Common/Buffer2DTests.cs | 12 +- .../Common/BufferSpanTests.cs | 115 +++++----- tests/ImageSharp.Tests/Common/BufferTests.cs | 30 ++- .../Common/Fast2DArrayTests.cs | 2 + .../Common/PixelDataPoolTests.cs | 1 + .../Drawing/FillPatternTests.cs | 1 + tests/ImageSharp.Tests/ImageComparer.cs | 1 + .../PorterDuffFunctionsTests_TPixel.cs | 22 +- .../TestUtilities/TestPixel.cs | 4 +- 106 files changed, 500 insertions(+), 562 deletions(-) delete mode 100644 src/ImageSharp/Common/Memory/BufferSpan{T}.cs rename src/ImageSharp/{Common => }/Memory/Buffer.cs (88%) rename src/ImageSharp/{Common => }/Memory/Buffer2D.cs (98%) rename src/ImageSharp/{Common => }/Memory/Buffer2DExtensions.cs (71%) rename src/ImageSharp/{Common => }/Memory/Fast2DArray{T}.cs (96%) rename src/ImageSharp/{Common => }/Memory/IBuffer2D.cs (84%) rename src/ImageSharp/{Common => }/Memory/PixelDataPool{T}.cs (98%) rename src/ImageSharp/{Common/Memory/BufferSpan.cs => Memory/SpanHelper.cs} (82%) diff --git a/src/ImageSharp.Drawing/Brushes/ImageBrush{TPixel}.cs b/src/ImageSharp.Drawing/Brushes/ImageBrush{TPixel}.cs index 5038ea01b..6f851e5c3 100644 --- a/src/ImageSharp.Drawing/Brushes/ImageBrush{TPixel}.cs +++ b/src/ImageSharp.Drawing/Brushes/ImageBrush{TPixel}.cs @@ -5,7 +5,10 @@ namespace ImageSharp.Drawing.Brushes { + using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Processors; @@ -114,7 +117,7 @@ namespace ImageSharp.Drawing.Brushes } /// - internal override void Apply(BufferSpan scanline, int x, int y) + internal override void Apply(Span scanline, int x, int y) { // create a span for colors using (Buffer amountBuffer = new Buffer(scanline.Length)) @@ -122,7 +125,7 @@ namespace ImageSharp.Drawing.Brushes { int sourceY = (y - this.offsetY) % this.yLength; int offsetX = x - this.offsetX; - BufferSpan sourceRow = this.source.GetRowSpan(sourceY); + Span sourceRow = this.source.GetRowSpan(sourceY); for (int i = 0; i < scanline.Length; i++) { @@ -133,7 +136,7 @@ namespace ImageSharp.Drawing.Brushes overlay[i] = pixel; } - BufferSpan destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); + Span destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); this.Blender.Blend(destinationRow, destinationRow, overlay, amountBuffer); } } diff --git a/src/ImageSharp.Drawing/Brushes/PatternBrush{TPixel}.cs b/src/ImageSharp.Drawing/Brushes/PatternBrush{TPixel}.cs index dc8a4bc90..90990e54a 100644 --- a/src/ImageSharp.Drawing/Brushes/PatternBrush{TPixel}.cs +++ b/src/ImageSharp.Drawing/Brushes/PatternBrush{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Drawing.Brushes { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Processors; @@ -147,7 +149,7 @@ namespace ImageSharp.Drawing.Brushes } /// - internal override void Apply(BufferSpan scanline, int x, int y) + internal override void Apply(Span scanline, int x, int y) { int patternY = y % this.pattern.Height; using (Buffer amountBuffer = new Buffer(scanline.Length)) @@ -161,7 +163,7 @@ namespace ImageSharp.Drawing.Brushes overlay[i] = this.pattern[patternY, patternX]; } - BufferSpan destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); + Span destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); this.Blender.Blend(destinationRow, destinationRow, overlay, amountBuffer); } } diff --git a/src/ImageSharp.Drawing/Brushes/Processors/BrushApplicator.cs b/src/ImageSharp.Drawing/Brushes/Processors/BrushApplicator.cs index d7c70220c..29629324a 100644 --- a/src/ImageSharp.Drawing/Brushes/Processors/BrushApplicator.cs +++ b/src/ImageSharp.Drawing/Brushes/Processors/BrushApplicator.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Drawing.Processors { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -64,7 +66,7 @@ namespace ImageSharp.Drawing.Processors /// The x position in the target pixel space that the start of the scanline data corresponds to. /// The y position in the target pixel space that whole scanline corresponds to. /// scanlineBuffer will be > scanlineWidth but provide and offset in case we want to share a larger buffer across runs. - internal virtual void Apply(BufferSpan scanline, int x, int y) + internal virtual void Apply(Span scanline, int x, int y) { using (Buffer amountBuffer = new Buffer(scanline.Length)) using (Buffer overlay = new Buffer(scanline.Length)) @@ -79,7 +81,7 @@ namespace ImageSharp.Drawing.Processors overlay[i] = this[x + i, y]; } - BufferSpan destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); + Span destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); this.Blender.Blend(destinationRow, destinationRow, overlay, amountBuffer); } } diff --git a/src/ImageSharp.Drawing/Brushes/RecolorBrush{TPixel}.cs b/src/ImageSharp.Drawing/Brushes/RecolorBrush{TPixel}.cs index a96202b7b..64b91e384 100644 --- a/src/ImageSharp.Drawing/Brushes/RecolorBrush{TPixel}.cs +++ b/src/ImageSharp.Drawing/Brushes/RecolorBrush{TPixel}.cs @@ -5,7 +5,10 @@ namespace ImageSharp.Drawing.Brushes { + using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Processors; @@ -139,7 +142,7 @@ namespace ImageSharp.Drawing.Brushes } /// - internal override void Apply(BufferSpan scanline, int x, int y) + internal override void Apply(Span scanline, int x, int y) { using (Buffer amountBuffer = new Buffer(scanline.Length)) using (Buffer overlay = new Buffer(scanline.Length)) @@ -155,7 +158,7 @@ namespace ImageSharp.Drawing.Brushes overlay[i] = this[offsetX, y]; } - BufferSpan destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); + Span destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); this.Blender.Blend(destinationRow, destinationRow, overlay, amountBuffer); } } diff --git a/src/ImageSharp.Drawing/Brushes/SolidBrush{TPixel}.cs b/src/ImageSharp.Drawing/Brushes/SolidBrush{TPixel}.cs index 71b802136..28f7b0e45 100644 --- a/src/ImageSharp.Drawing/Brushes/SolidBrush{TPixel}.cs +++ b/src/ImageSharp.Drawing/Brushes/SolidBrush{TPixel}.cs @@ -5,7 +5,10 @@ namespace ImageSharp.Drawing.Brushes { + using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Processors; @@ -87,9 +90,9 @@ namespace ImageSharp.Drawing.Brushes } /// - internal override void Apply(BufferSpan scanline, int x, int y) + internal override void Apply(Span scanline, int x, int y) { - BufferSpan destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); + Span destinationRow = this.Target.GetRowSpan(x, y).Slice(0, scanline.Length); using (Buffer amountBuffer = new Buffer(scanline.Length)) { diff --git a/src/ImageSharp.Drawing/Processors/DrawImageProcessor.cs b/src/ImageSharp.Drawing/Processors/DrawImageProcessor.cs index c49631de8..ed45417fc 100644 --- a/src/ImageSharp.Drawing/Processors/DrawImageProcessor.cs +++ b/src/ImageSharp.Drawing/Processors/DrawImageProcessor.cs @@ -8,6 +8,8 @@ namespace ImageSharp.Drawing.Processors using System; using System.Numerics; using System.Threading.Tasks; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using ImageSharp.Processing; @@ -97,8 +99,8 @@ namespace ImageSharp.Drawing.Processors this.ParallelOptions, y => { - BufferSpan background = sourcePixels.GetRowSpan(y).Slice(minX, width); - BufferSpan foreground = toBlendPixels.GetRowSpan(y - this.Location.Y).Slice(0, width); + Span background = sourcePixels.GetRowSpan(y).Slice(minX, width); + Span foreground = toBlendPixels.GetRowSpan(y - this.Location.Y).Slice(0, width); this.blender.Blend(background, background, foreground, amount); }); } diff --git a/src/ImageSharp.Drawing/Processors/DrawPathProcessor.cs b/src/ImageSharp.Drawing/Processors/DrawPathProcessor.cs index 3fd829549..d1332c435 100644 --- a/src/ImageSharp.Drawing/Processors/DrawPathProcessor.cs +++ b/src/ImageSharp.Drawing/Processors/DrawPathProcessor.cs @@ -8,6 +8,8 @@ namespace ImageSharp.Drawing.Processors using System; using System.Numerics; using System.Threading.Tasks; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using ImageSharp.Processing; using Pens; @@ -110,7 +112,7 @@ namespace ImageSharp.Drawing.Processors colors[i] = color.Color; } - BufferSpan destination = sourcePixels.GetRowSpan(offsetY).Slice(minX - startX, width); + Span destination = sourcePixels.GetRowSpan(offsetY).Slice(minX - startX, width); blender.Blend(destination, destination, colors, amount); } }); diff --git a/src/ImageSharp.Drawing/Processors/FillProcessor.cs b/src/ImageSharp.Drawing/Processors/FillProcessor.cs index 25eccd245..fa6f48156 100644 --- a/src/ImageSharp.Drawing/Processors/FillProcessor.cs +++ b/src/ImageSharp.Drawing/Processors/FillProcessor.cs @@ -10,6 +10,8 @@ namespace ImageSharp.Drawing.Processors using System.Threading.Tasks; using Drawing; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using ImageSharp.Processing; diff --git a/src/ImageSharp.Drawing/Processors/FillRegionProcessor.cs b/src/ImageSharp.Drawing/Processors/FillRegionProcessor.cs index 323214fb8..a57be3a5a 100644 --- a/src/ImageSharp.Drawing/Processors/FillRegionProcessor.cs +++ b/src/ImageSharp.Drawing/Processors/FillRegionProcessor.cs @@ -8,6 +8,8 @@ namespace ImageSharp.Drawing.Processors using System; using System.Buffers; using Drawing; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using ImageSharp.Processing; diff --git a/src/ImageSharp/Common/Helpers/DebugGuard.cs b/src/ImageSharp/Common/Helpers/DebugGuard.cs index f7c83452f..096f96f04 100644 --- a/src/ImageSharp/Common/Helpers/DebugGuard.cs +++ b/src/ImageSharp/Common/Helpers/DebugGuard.cs @@ -170,7 +170,7 @@ namespace ImageSharp /// /// is true /// - public static void MustBeSameSized(BufferSpan target, BufferSpan other, string parameterName) + public static void MustBeSameSized(Span target, Span other, string parameterName) where T : struct { if (target.Length != other.Length) @@ -189,7 +189,7 @@ namespace ImageSharp /// /// is true /// - public static void MustBeSizedAtLeast(BufferSpan target, BufferSpan minSpan, string parameterName) + public static void MustBeSizedAtLeast(Span target, Span minSpan, string parameterName) where T : struct { if (target.Length < minSpan.Length) diff --git a/src/ImageSharp/Common/Memory/BufferSpan{T}.cs b/src/ImageSharp/Common/Memory/BufferSpan{T}.cs deleted file mode 100644 index 1b0bef314..000000000 --- a/src/ImageSharp/Common/Memory/BufferSpan{T}.cs +++ /dev/null @@ -1,205 +0,0 @@ -// -// Copyright (c) James Jackson-South and contributors. -// Licensed under the Apache License, Version 2.0. -// - -namespace ImageSharp -{ - using System; - using System.Diagnostics; - using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; - - /// - /// Represents a contiguous region of a pinned managed array. - /// The array is usually owned by a instance. - /// - /// - /// is very similar to corefx System.Span<T>, and we try to maintain a compatible API. - /// There are several differences though: - /// - It's not possible to use it with stack objects or pointers to unmanaged memory, only with managed arrays. - /// - It's possible to retrieve a reference to the array () so we can pass it to API-s like - /// - It's possible to retrieve the pinned pointer. This enables optimized (unchecked) unsafe operations. - /// - There is no bounds checking for performance reasons, only in debug mode. This makes an unsafe type! - /// - /// The type of elements of the array - internal unsafe struct BufferSpan - where T : struct - { - /// - /// Initializes a new instance of the struct from a pinned array and an start. - /// - /// The pinned array - /// The index at which to begin the span. - /// The length - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public BufferSpan(T[] array, int start, int length) - { - GuardArray(array); - - DebugGuard.MustBeLessThanOrEqualTo(start, array.Length, nameof(start)); - DebugGuard.MustBeLessThanOrEqualTo(length, array.Length - start, nameof(length)); - - this.Array = array; - this.Length = length; - this.Start = start; - } - - /// - /// Initializes a new instance of the struct from a pinned array and an start. - /// - /// The pinned array - /// The index at which to begin the span. - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public BufferSpan(T[] array, int start) - { - GuardArray(array); - DebugGuard.MustBeLessThanOrEqualTo(start, array.Length, nameof(start)); - - this.Array = array; - this.Length = array.Length - start; - this.Start = start; - } - - /// - /// Initializes a new instance of the struct from a pinned array. - /// - /// The pinned array - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public BufferSpan(T[] array) - { - GuardArray(array); - - this.Array = array; - this.Start = 0; - this.Length = array.Length; - } - - /// - /// Gets the backing array. - /// - public T[] Array { get; private set; } - - /// - /// Gets the length of the - /// - public int Length { get; private set; } - - /// - /// Gets the start inside - /// - public int Start { get; private set; } - - /// - /// Gets the start inside in bytes. - /// - public int ByteOffset => this.Start * Unsafe.SizeOf(); - - /// - /// Returns a reference to specified element of the span. - /// - /// The index - /// The reference to the specified element - public ref T this[int index] - { - [MethodImpl(MethodImplOptions.AggressiveInlining)] - get - { - DebugGuard.MustBeLessThan(index, this.Length, nameof(index)); - ref T startRef = ref this.DangerousGetPinnableReference(); - return ref Unsafe.Add(ref startRef, index); - } - } - - /// - /// Converts generic to a of bytes - /// setting it's and to correct values. - /// - /// The span of bytes - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public BufferSpan AsBytes() - { - BufferSpan result = default(BufferSpan); - result.Array = Unsafe.As(this.Array); - result.Start = this.Start * Unsafe.SizeOf(); - result.Length = this.Length * Unsafe.SizeOf(); - return result; - } - - /// - /// Returns a reference to the 0th element of the Span. If the Span is empty, returns a reference to the location where the 0th element - /// would have been stored. Such a reference can be used for pinning but must never be dereferenced. - /// - /// The reference to the 0th element - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public ref T DangerousGetPinnableReference() - { - ref T origin = ref this.Array[0]; - return ref Unsafe.Add(ref origin, this.Start); - } - - /// - /// Forms a slice out of the given BufferSpan, beginning at 'start'. - /// - /// TThe index at which to begin this slice. - /// The offseted (sliced) BufferSpan - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public BufferSpan Slice(int start) - { - DebugGuard.MustBeLessThan(start, this.Length, nameof(start)); - - BufferSpan result = default(BufferSpan); - result.Array = this.Array; - result.Start = this.Start + start; - result.Length = this.Length - start; - return result; - } - - /// - /// Forms a slice out of the given BufferSpan, beginning at 'start'. - /// - /// The index at which to begin this slice. - /// The desired length for the slice (exclusive). - /// The sliced BufferSpan - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public BufferSpan Slice(int start, int length) - { - DebugGuard.MustBeLessThanOrEqualTo(start, this.Length, nameof(start)); - DebugGuard.MustBeLessThanOrEqualTo(length, this.Length - start, nameof(length)); - - BufferSpan result = default(BufferSpan); - result.Array = this.Array; - result.Start = this.Start + start; - result.Length = length; - return result; - } - - /// - /// Clears `count` elements from the beginning of the span. - /// - /// The number of elements to clear - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Clear(int count) - { - DebugGuard.MustBeLessThanOrEqualTo(count, this.Length, nameof(count)); - - // TODO: Use Unsafe.InitBlock(ref T) for small arrays, when it get's official - System.Array.Clear(this.Array, this.Start, count); - } - - /// - /// Clears the the span - /// - [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void Clear() - { - this.Clear(this.Length); - } - - [Conditional("DEBUG")] - private static void GuardArray(T[] array) - { - DebugGuard.NotNull(array, nameof(array)); - } - } -} \ No newline at end of file diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/Atkinson.cs b/src/ImageSharp/Dithering/ErrorDiffusion/Atkinson.cs index b94b87255..629944ea1 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/Atkinson.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/Atkinson.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the Atkinson image dithering algorithm. /// diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/Burks.cs b/src/ImageSharp/Dithering/ErrorDiffusion/Burks.cs index 894b6e236..02d41c369 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/Burks.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/Burks.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the Burks image dithering algorithm. /// diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs b/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs index 5d0ecde6b..7a5fabdb3 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Dithering using System.Numerics; using System.Runtime.CompilerServices; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/FloydSteinberg.cs b/src/ImageSharp/Dithering/ErrorDiffusion/FloydSteinberg.cs index f7a93667f..6165da634 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/FloydSteinberg.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/FloydSteinberg.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the Floyd–Steinberg image dithering algorithm. /// diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/JarvisJudiceNinke.cs b/src/ImageSharp/Dithering/ErrorDiffusion/JarvisJudiceNinke.cs index 60fef8121..6daeab32f 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/JarvisJudiceNinke.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/JarvisJudiceNinke.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the JarvisJudiceNinke image dithering algorithm. /// diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/Sierra2.cs b/src/ImageSharp/Dithering/ErrorDiffusion/Sierra2.cs index 4325438e0..0c0944d0e 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/Sierra2.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/Sierra2.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the Sierra2 image dithering algorithm. /// diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/Sierra3.cs b/src/ImageSharp/Dithering/ErrorDiffusion/Sierra3.cs index 25ea70d0a..2e2220846 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/Sierra3.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/Sierra3.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the Sierra3 image dithering algorithm. /// diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/SierraLite.cs b/src/ImageSharp/Dithering/ErrorDiffusion/SierraLite.cs index c7b1d214f..fe4c933a9 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/SierraLite.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/SierraLite.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the SierraLite image dithering algorithm. /// diff --git a/src/ImageSharp/Dithering/ErrorDiffusion/Stucki.cs b/src/ImageSharp/Dithering/ErrorDiffusion/Stucki.cs index 93258c350..b04c16481 100644 --- a/src/ImageSharp/Dithering/ErrorDiffusion/Stucki.cs +++ b/src/ImageSharp/Dithering/ErrorDiffusion/Stucki.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the Stucki image dithering algorithm. /// diff --git a/src/ImageSharp/Dithering/Ordered/Bayer.cs b/src/ImageSharp/Dithering/Ordered/Bayer.cs index 3792c3c02..ded17d1e1 100644 --- a/src/ImageSharp/Dithering/Ordered/Bayer.cs +++ b/src/ImageSharp/Dithering/Ordered/Bayer.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering.Ordered { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the 4x4 Bayer dithering matrix. /// diff --git a/src/ImageSharp/Dithering/Ordered/Ordered.cs b/src/ImageSharp/Dithering/Ordered/Ordered.cs index ae75b87f2..1fd39eb8b 100644 --- a/src/ImageSharp/Dithering/Ordered/Ordered.cs +++ b/src/ImageSharp/Dithering/Ordered/Ordered.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Dithering.Ordered { + using ImageSharp.Memory; + /// /// Applies error diffusion based dithering using the 4x4 ordered dithering matrix. /// diff --git a/src/ImageSharp/Dithering/Ordered/OrderedDither4x4.cs b/src/ImageSharp/Dithering/Ordered/OrderedDither4x4.cs index 917f57318..48d6c3f6a 100644 --- a/src/ImageSharp/Dithering/Ordered/OrderedDither4x4.cs +++ b/src/ImageSharp/Dithering/Ordered/OrderedDither4x4.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Dithering.Ordered { + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockProcessor.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockProcessor.cs index 0ce233739..71472c00f 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockProcessor.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegBlockProcessor.cs @@ -8,6 +8,8 @@ namespace ImageSharp.Formats.Jpg using System.Runtime.CompilerServices; using System.Runtime.InteropServices; + using ImageSharp.Memory; + /// /// Encapsulates the implementation of processing "raw" -s into Jpeg image channels. /// diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs index 920457a0c..342ce299c 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegPixelArea.cs @@ -6,6 +6,8 @@ namespace ImageSharp.Formats.Jpg { using System.Runtime.CompilerServices; + using ImageSharp.Memory; + /// /// Represents an area of a Jpeg subimage (channel) /// diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegScanDecoder.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegScanDecoder.cs index 2a9b0c6b2..7d2e6d441 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegScanDecoder.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/JpegScanDecoder.cs @@ -9,6 +9,8 @@ namespace ImageSharp.Formats.Jpg using System.Runtime.CompilerServices; using System.Runtime.InteropServices; + using ImageSharp.Memory; + /// /// Encapsulates the impementation of Jpeg SOS Huffman decoding. See JpegScanDecoder.md! /// diff --git a/src/ImageSharp/Formats/Jpeg/Components/Decoder/YCbCrImage.cs b/src/ImageSharp/Formats/Jpeg/Components/Decoder/YCbCrImage.cs index 86192318b..89e327f0d 100644 --- a/src/ImageSharp/Formats/Jpeg/Components/Decoder/YCbCrImage.cs +++ b/src/ImageSharp/Formats/Jpeg/Components/Decoder/YCbCrImage.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Formats.Jpg using System; using System.Buffers; + using ImageSharp.Memory; + /// /// Represents an image made up of three color components (luminance, blue chroma, red chroma) /// diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index 92607883d..9d9ecacfe 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -11,6 +11,7 @@ namespace ImageSharp.Formats using System.Threading.Tasks; using ImageSharp.Formats.Jpg; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs index d3b32d977..db2189b3c 100644 --- a/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/AverageFilter.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Formats { + using System; using System.Runtime.CompilerServices; /// @@ -21,7 +22,7 @@ namespace ImageSharp.Formats /// The previous scanline. /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(BufferSpan scanline, BufferSpan previousScanline, int bytesPerPixel) + public static void Decode(Span scanline, Span previousScanline, int bytesPerPixel) { DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); @@ -55,7 +56,7 @@ namespace ImageSharp.Formats /// The filtered scanline result. /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result, int bytesPerPixel) + public static void Encode(Span scanline, Span previousScanline, Span result, int bytesPerPixel) { DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); DebugGuard.MustBeSizedAtLeast(result, scanline, nameof(result)); diff --git a/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs b/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs index 87d794902..ee77d1a5e 100644 --- a/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/NoneFilter.cs @@ -8,6 +8,8 @@ namespace ImageSharp.Formats using System; using System.Runtime.CompilerServices; + using ImageSharp.Memory; + /// /// The None filter, the scanline is transmitted unmodified; it is only necessary to /// insert a filter type byte before the data. @@ -21,12 +23,12 @@ namespace ImageSharp.Formats /// The scanline to encode /// The filtered scanline result. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(BufferSpan scanline, BufferSpan result) + public static void Encode(Span scanline, Span result) { // Insert a byte before the data. result[0] = 0; result = result.Slice(1); - BufferSpan.Copy(scanline, result); + SpanHelper.Copy(scanline, result); } } } diff --git a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs index 4226596f8..ec12eca05 100644 --- a/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/PaethFilter.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Formats { + using System; using System.Runtime.CompilerServices; /// @@ -22,7 +23,7 @@ namespace ImageSharp.Formats /// The previous scanline. /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(BufferSpan scanline, BufferSpan previousScanline, int bytesPerPixel) + public static void Decode(Span scanline, Span previousScanline, int bytesPerPixel) { DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); @@ -57,7 +58,7 @@ namespace ImageSharp.Formats /// The filtered scanline result. /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result, int bytesPerPixel) + public static void Encode(Span scanline, Span previousScanline, Span result, int bytesPerPixel) { DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); DebugGuard.MustBeSizedAtLeast(result, scanline, nameof(result)); diff --git a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs index d40f261ed..f81122ad2 100644 --- a/src/ImageSharp/Formats/Png/Filters/SubFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/SubFilter.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Formats { + using System; using System.Runtime.CompilerServices; /// @@ -20,7 +21,7 @@ namespace ImageSharp.Formats /// The scanline to decode /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(BufferSpan scanline, int bytesPerPixel) + public static void Decode(Span scanline, int bytesPerPixel) { ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); @@ -48,7 +49,7 @@ namespace ImageSharp.Formats /// The filtered scanline result. /// The bytes per pixel. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(BufferSpan scanline, BufferSpan result, int bytesPerPixel) + public static void Encode(Span scanline, Span result, int bytesPerPixel) { DebugGuard.MustBeSizedAtLeast(result, scanline, nameof(result)); diff --git a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs index fa3ef57da..b89a3c5fc 100644 --- a/src/ImageSharp/Formats/Png/Filters/UpFilter.cs +++ b/src/ImageSharp/Formats/Png/Filters/UpFilter.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Formats { + using System; using System.Runtime.CompilerServices; /// @@ -20,7 +21,7 @@ namespace ImageSharp.Formats /// The scanline to decode /// The previous scanline. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Decode(BufferSpan scanline, BufferSpan previousScanline) + public static void Decode(Span scanline, Span previousScanline) { DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); @@ -43,7 +44,7 @@ namespace ImageSharp.Formats /// The previous scanline. /// The filtered scanline result. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Encode(BufferSpan scanline, BufferSpan previousScanline, BufferSpan result) + public static void Encode(Span scanline, Span previousScanline, Span result) { DebugGuard.MustBeSameSized(scanline, previousScanline, nameof(scanline)); DebugGuard.MustBeSizedAtLeast(result, scanline, nameof(result)); diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 13b4c1167..f8be0f6cc 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -12,6 +12,7 @@ namespace ImageSharp.Formats using System.Linq; using System.Runtime.CompilerServices; + using ImageSharp.Memory; using ImageSharp.PixelFormats; using static ComparableExtensions; @@ -503,8 +504,8 @@ namespace ImageSharp.Formats this.currentRowBytesRead = 0; - BufferSpan scanSpan = this.scanline.Slice(0, bytesPerInterlaceScanline); - BufferSpan prevSpan = this.previousScanline.Span.Slice(0, bytesPerInterlaceScanline); + Span scanSpan = this.scanline.Slice(0, bytesPerInterlaceScanline); + Span prevSpan = this.previousScanline.Span.Slice(0, bytesPerInterlaceScanline); var filterType = (FilterType)scanSpan[0]; switch (filterType) @@ -565,8 +566,8 @@ namespace ImageSharp.Formats where TPixel : struct, IPixel { var color = default(TPixel); - BufferSpan pixelBuffer = pixels.GetRowSpan(this.currentRow); - var scanlineBuffer = new BufferSpan(defilteredScanline, 1); + Span pixelBuffer = pixels.GetRowSpan(this.currentRow); + var scanlineBuffer = new Span(defilteredScanline, 1); switch (this.PngColorType) { diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 44f1513ee..49c18db34 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -10,6 +10,8 @@ namespace ImageSharp.Formats using System.IO; using System.Linq; using System.Runtime.CompilerServices; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Quantizers; @@ -340,15 +342,15 @@ namespace ImageSharp.Formats private void CollecTPixelBytes(PixelAccessor pixels, int row) where TPixel : struct, IPixel { - BufferSpan rowSpan = pixels.GetRowSpan(row); + Span rowSpan = pixels.GetRowSpan(row); if (this.bytesPerPixel == 4) { - PixelOperations.Instance.ToXyzwBytes(rowSpan, this.rawScanline, this.width); + PixelOperations.Instance.ToXyzwBytes(rowSpan, this.rawScanline, 0, this.width); } else { - PixelOperations.Instance.ToXyzBytes(rowSpan, this.rawScanline, this.width); + PixelOperations.Instance.ToXyzBytes(rowSpan, this.rawScanline, 0, this.width); } } @@ -387,8 +389,8 @@ namespace ImageSharp.Formats /// The private Buffer GetOptimalFilteredScanline() { - BufferSpan scanSpan = this.rawScanline.Span; - BufferSpan prevSpan = this.previousScanline.Span; + Span scanSpan = this.rawScanline.Span; + Span prevSpan = this.previousScanline.Span; // Palette images don't compress well with adaptive filtering. if (this.pngColorType == PngColorType.Palette || this.bitDepth < 8) @@ -442,7 +444,7 @@ namespace ImageSharp.Formats /// The last variation sum /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - private int CalculateTotalVariation(BufferSpan scanline, int lastSum) + private int CalculateTotalVariation(Span scanline, int lastSum) { ref byte scanBaseRef = ref scanline.DangerousGetPinnableReference(); int sum = 0; diff --git a/src/ImageSharp/Image/ImageBase{TPixel}.cs b/src/ImageSharp/Image/ImageBase{TPixel}.cs index d5023c4ba..4fd9d26cb 100644 --- a/src/ImageSharp/Image/ImageBase{TPixel}.cs +++ b/src/ImageSharp/Image/ImageBase{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp { using System; using System.Diagnostics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Processing; diff --git a/src/ImageSharp/Image/PixelAccessor{TPixel}.cs b/src/ImageSharp/Image/PixelAccessor{TPixel}.cs index 1e46a672e..f4c7c7642 100644 --- a/src/ImageSharp/Image/PixelAccessor{TPixel}.cs +++ b/src/ImageSharp/Image/PixelAccessor{TPixel}.cs @@ -9,6 +9,8 @@ namespace ImageSharp using System.Diagnostics; using System.Runtime.CompilerServices; using System.Threading.Tasks; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -114,7 +116,7 @@ namespace ImageSharp public ParallelOptions ParallelOptions { get; } /// - BufferSpan IBuffer2D.Span => this.pixelBuffer; + Span IBuffer2D.Span => this.pixelBuffer; private static PixelOperations Operations => PixelOperations.Instance; @@ -250,7 +252,7 @@ namespace ImageSharp /// The target pixel buffer accessor. internal void CopyTo(PixelAccessor target) { - BufferSpan.Copy(this.pixelBuffer.Span, target.pixelBuffer.Span); + SpanHelper.Copy(this.pixelBuffer.Span, target.pixelBuffer.Span); } /// @@ -266,8 +268,8 @@ namespace ImageSharp { for (int y = 0; y < height; y++) { - BufferSpan source = area.GetRowSpan(y); - BufferSpan destination = this.GetRowSpan(targetX, targetY + y); + Span source = area.GetRowSpan(y); + Span destination = this.GetRowSpan(targetX, targetY + y); Operations.PackFromZyxBytes(source, destination, width); } @@ -286,8 +288,8 @@ namespace ImageSharp { for (int y = 0; y < height; y++) { - BufferSpan source = area.GetRowSpan(y); - BufferSpan destination = this.GetRowSpan(targetX, targetY + y); + Span source = area.GetRowSpan(y); + Span destination = this.GetRowSpan(targetX, targetY + y); Operations.PackFromZyxwBytes(source, destination, width); } @@ -306,8 +308,8 @@ namespace ImageSharp { for (int y = 0; y < height; y++) { - BufferSpan source = area.GetRowSpan(y); - BufferSpan destination = this.GetRowSpan(targetX, targetY + y); + Span source = area.GetRowSpan(y); + Span destination = this.GetRowSpan(targetX, targetY + y); Operations.PackFromXyzBytes(source, destination, width); } @@ -326,8 +328,8 @@ namespace ImageSharp { for (int y = 0; y < height; y++) { - BufferSpan source = area.GetRowSpan(y); - BufferSpan destination = this.GetRowSpan(targetX, targetY + y); + Span source = area.GetRowSpan(y); + Span destination = this.GetRowSpan(targetX, targetY + y); Operations.PackFromXyzwBytes(source, destination, width); } } @@ -345,9 +347,11 @@ namespace ImageSharp { for (int y = 0; y < height; y++) { - BufferSpan source = this.GetRowSpan(sourceX, sourceY + y); - BufferSpan destination = area.GetRowSpan(y); - Operations.ToZyxBytes(source, destination, width); + Span source = this.GetRowSpan(sourceX, sourceY + y); + using (Buffer destination = new Buffer(area.Bytes)) + { + Operations.ToZyxBytes(source, destination, y * area.RowStride, width); + } } } @@ -364,9 +368,11 @@ namespace ImageSharp { for (int y = 0; y < height; y++) { - BufferSpan source = this.GetRowSpan(sourceX, sourceY + y); - BufferSpan destination = area.GetRowSpan(y); - Operations.ToZyxwBytes(source, destination, width); + Span source = this.GetRowSpan(sourceX, sourceY + y); + using (Buffer destination = new Buffer(area.Bytes)) + { + Operations.ToZyxwBytes(source, destination, y * area.RowStride, width); + } } } @@ -383,9 +389,11 @@ namespace ImageSharp { for (int y = 0; y < height; y++) { - BufferSpan source = this.GetRowSpan(sourceX, sourceY + y); - BufferSpan destination = area.GetRowSpan(y); - Operations.ToXyzBytes(source, destination, width); + Span source = this.GetRowSpan(sourceX, sourceY + y); + using (Buffer destination = new Buffer(area.Bytes)) + { + Operations.ToXyzBytes(source, destination, y * area.RowStride, width); + } } } @@ -402,9 +410,11 @@ namespace ImageSharp { for (int y = 0; y < height; y++) { - BufferSpan source = this.GetRowSpan(sourceX, sourceY + y); - BufferSpan destination = area.GetRowSpan(y); - Operations.ToXyzwBytes(source, destination, width); + Span source = this.GetRowSpan(sourceX, sourceY + y); + using (Buffer destination = new Buffer(area.Bytes)) + { + Operations.ToXyzwBytes(source, destination, y * area.RowStride, width); + } } } diff --git a/src/ImageSharp/Image/PixelArea{TPixel}.cs b/src/ImageSharp/Image/PixelArea{TPixel}.cs index 3dd187768..4ddfcadb7 100644 --- a/src/ImageSharp/Image/PixelArea{TPixel}.cs +++ b/src/ImageSharp/Image/PixelArea{TPixel}.cs @@ -8,6 +8,8 @@ namespace ImageSharp using System; using System.Diagnostics; using System.IO; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -191,11 +193,11 @@ namespace ImageSharp } /// - /// Gets a to the row y. + /// Gets a to the row y. /// /// The y coordinate - /// The - internal BufferSpan GetRowSpan(int y) + /// The + internal Span GetRowSpan(int y) { return this.byteBuffer.Slice(y * this.RowStride); } diff --git a/src/ImageSharp/ImageSharp.csproj b/src/ImageSharp/ImageSharp.csproj index 0269e770f..6194be1bf 100644 --- a/src/ImageSharp/ImageSharp.csproj +++ b/src/ImageSharp/ImageSharp.csproj @@ -38,8 +38,9 @@ All + - + diff --git a/src/ImageSharp/Common/Memory/Buffer.cs b/src/ImageSharp/Memory/Buffer.cs similarity index 88% rename from src/ImageSharp/Common/Memory/Buffer.cs rename to src/ImageSharp/Memory/Buffer.cs index c26b8ea18..a894ea53a 100644 --- a/src/ImageSharp/Common/Memory/Buffer.cs +++ b/src/ImageSharp/Memory/Buffer.cs @@ -3,10 +3,9 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp +namespace ImageSharp.Memory { using System; - using System.Buffers; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -97,9 +96,9 @@ namespace ImageSharp public T[] Array { get; private set; } /// - /// Gets a to the backing buffer. + /// Gets a to the backing buffer. /// - public BufferSpan Span => this; + public Span Span => this; /// /// Returns a reference to specified element of the buffer. @@ -117,13 +116,13 @@ namespace ImageSharp } /// - /// Converts to an . + /// Converts to an . /// /// The to convert. [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static implicit operator BufferSpan(Buffer buffer) + public static implicit operator Span(Buffer buffer) { - return new BufferSpan(buffer.Array, 0, buffer.Length); + return new Span(buffer.Array, 0, buffer.Length); } /// @@ -140,26 +139,26 @@ namespace ImageSharp } /// - /// Gets a to an offseted position inside the buffer. + /// Gets a to an offseted position inside the buffer. /// /// The start - /// The + /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - public BufferSpan Slice(int start) + public Span Slice(int start) { - return new BufferSpan(this.Array, start, this.Length - start); + return new Span(this.Array, start, this.Length - start); } /// - /// Gets a to an offseted position inside the buffer. + /// Gets a to an offsetted position inside the buffer. /// /// The start /// The length of the slice - /// The + /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - public BufferSpan Slice(int start, int length) + public Span Slice(int start, int length) { - return new BufferSpan(this.Array, start, length); + return new Span(this.Array, start, length); } /// @@ -210,7 +209,7 @@ namespace ImageSharp } /// - /// Clears the buffer, filling elements between 0 and -1 with default(T) + /// Clears the contents of this buffer. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Clear() diff --git a/src/ImageSharp/Common/Memory/Buffer2D.cs b/src/ImageSharp/Memory/Buffer2D.cs similarity index 98% rename from src/ImageSharp/Common/Memory/Buffer2D.cs rename to src/ImageSharp/Memory/Buffer2D.cs index c4eb50700..e5ccfbd19 100644 --- a/src/ImageSharp/Common/Memory/Buffer2D.cs +++ b/src/ImageSharp/Memory/Buffer2D.cs @@ -3,9 +3,8 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp +namespace ImageSharp.Memory { - using System; using System.Runtime.CompilerServices; /// diff --git a/src/ImageSharp/Common/Memory/Buffer2DExtensions.cs b/src/ImageSharp/Memory/Buffer2DExtensions.cs similarity index 71% rename from src/ImageSharp/Common/Memory/Buffer2DExtensions.cs rename to src/ImageSharp/Memory/Buffer2DExtensions.cs index 4c3cc4d40..51e558281 100644 --- a/src/ImageSharp/Common/Memory/Buffer2DExtensions.cs +++ b/src/ImageSharp/Memory/Buffer2DExtensions.cs @@ -3,7 +3,7 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp +namespace ImageSharp.Memory { using System; using System.Runtime.CompilerServices; @@ -14,29 +14,29 @@ namespace ImageSharp internal static class Buffer2DExtensions { /// - /// Gets a to the row 'y' beginning from the pixel at 'x'. + /// Gets a to the row 'y' beginning from the pixel at 'x'. /// /// The buffer /// The x coordinate (position in the row) /// The y (row) coordinate /// The element type - /// The + /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static BufferSpan GetRowSpan(this IBuffer2D buffer, int x, int y) + public static Span GetRowSpan(this IBuffer2D buffer, int x, int y) where T : struct { return buffer.Span.Slice((y * buffer.Width) + x, buffer.Width - x); } /// - /// Gets a to the row 'y' beginning from the pixel at 'x'. + /// Gets a to the row 'y' beginning from the pixel at 'x'. /// /// The buffer /// The y (row) coordinate /// The element type - /// The + /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static BufferSpan GetRowSpan(this IBuffer2D buffer, int y) + public static Span GetRowSpan(this IBuffer2D buffer, int y) where T : struct { return buffer.Span.Slice(y * buffer.Width, buffer.Width); diff --git a/src/ImageSharp/Common/Memory/Fast2DArray{T}.cs b/src/ImageSharp/Memory/Fast2DArray{T}.cs similarity index 96% rename from src/ImageSharp/Common/Memory/Fast2DArray{T}.cs rename to src/ImageSharp/Memory/Fast2DArray{T}.cs index 401c83ce6..260c829e2 100644 --- a/src/ImageSharp/Common/Memory/Fast2DArray{T}.cs +++ b/src/ImageSharp/Memory/Fast2DArray{T}.cs @@ -3,7 +3,7 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp +namespace ImageSharp.Memory { using System; using System.Diagnostics; @@ -94,11 +94,11 @@ namespace ImageSharp } /// - /// Performs an implicit conversion from a 2D array to a . + /// Performs an implicit conversion from a 2D array to a . /// /// The source array. /// - /// The represenation on the source data. + /// The represenation on the source data. /// [MethodImpl(MethodImplOptions.AggressiveInlining)] public static implicit operator Fast2DArray(T[,] data) diff --git a/src/ImageSharp/Common/Memory/IBuffer2D.cs b/src/ImageSharp/Memory/IBuffer2D.cs similarity index 84% rename from src/ImageSharp/Common/Memory/IBuffer2D.cs rename to src/ImageSharp/Memory/IBuffer2D.cs index 1ba08e49f..300c29a1b 100644 --- a/src/ImageSharp/Common/Memory/IBuffer2D.cs +++ b/src/ImageSharp/Memory/IBuffer2D.cs @@ -3,8 +3,10 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp +namespace ImageSharp.Memory { + using System; + /// /// An interface that represents a pinned buffer of value type objects /// interpreted as a 2D region of x elements. @@ -24,8 +26,8 @@ namespace ImageSharp int Height { get; } /// - /// Gets a to the backing buffer. + /// Gets a to the backing buffer. /// - BufferSpan Span { get; } + Span Span { get; } } } \ No newline at end of file diff --git a/src/ImageSharp/Common/Memory/PixelDataPool{T}.cs b/src/ImageSharp/Memory/PixelDataPool{T}.cs similarity index 98% rename from src/ImageSharp/Common/Memory/PixelDataPool{T}.cs rename to src/ImageSharp/Memory/PixelDataPool{T}.cs index 0ec367d24..a8b5501cc 100644 --- a/src/ImageSharp/Common/Memory/PixelDataPool{T}.cs +++ b/src/ImageSharp/Memory/PixelDataPool{T}.cs @@ -3,9 +3,8 @@ // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp +namespace ImageSharp.Memory { - using System; using System.Buffers; using ImageSharp.PixelFormats; diff --git a/src/ImageSharp/Common/Memory/BufferSpan.cs b/src/ImageSharp/Memory/SpanHelper.cs similarity index 82% rename from src/ImageSharp/Common/Memory/BufferSpan.cs rename to src/ImageSharp/Memory/SpanHelper.cs index f8a5453a8..57b771591 100644 --- a/src/ImageSharp/Common/Memory/BufferSpan.cs +++ b/src/ImageSharp/Memory/SpanHelper.cs @@ -1,19 +1,18 @@ -// +// // Copyright (c) James Jackson-South and contributors. // Licensed under the Apache License, Version 2.0. // -namespace ImageSharp +namespace ImageSharp.Memory { using System; using System.Numerics; using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; /// - /// Utility methods for + /// Utility methods for /// - internal static class BufferSpan + internal static class SpanHelper { /// /// Fetches a from the beginning of the span. @@ -22,7 +21,7 @@ namespace ImageSharp /// The span to fetch the vector from /// A reference to the beginning of the span [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static ref Vector FetchVector(this BufferSpan span) + public static ref Vector FetchVector(this Span span) where T : struct { return ref Unsafe.As>(ref span.DangerousGetPinnableReference()); @@ -32,11 +31,11 @@ namespace ImageSharp /// Copy 'count' number of elements of the same type from 'source' to 'dest' /// /// The element type. - /// The to copy elements from. - /// The destination . + /// The to copy elements from. + /// The destination . /// The number of elements to copy [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static unsafe void Copy(BufferSpan source, BufferSpan destination, int count) + public static unsafe void Copy(Span source, Span destination, int count) where T : struct { DebugGuard.MustBeLessThanOrEqualTo(count, source.Length, nameof(count)); @@ -48,6 +47,7 @@ namespace ImageSharp int byteCount = Unsafe.SizeOf() * count; // TODO: Use unfixed Unsafe.CopyBlock(ref T, ref T, int) for small blocks, when it gets available! + // This is now available. Check with Anton re intent. Do we replace both ifdefs? fixed (byte* pSrc = &srcRef) fixed (byte* pDest = &destRef) { @@ -64,10 +64,10 @@ namespace ImageSharp /// Copy all elements of 'source' into 'destination'. /// /// The element type. - /// The to copy elements from. - /// The destination . + /// The to copy elements from. + /// The destination . [MethodImpl(MethodImplOptions.AggressiveInlining)] - public static void Copy(BufferSpan source, BufferSpan destination) + public static void Copy(Span source, Span destination) where T : struct { Copy(source, destination, source.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultAddPixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultAddPixelBlender{TPixel}.cs index ab3aee041..261a98674 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultAddPixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultAddPixelBlender{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.PixelFormats.PixelBlenders { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -28,7 +30,7 @@ namespace ImageSharp.PixelFormats.PixelBlenders } /// - public override void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + public override void Blend(Span destination, Span background, Span source, Span amount) { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); @@ -36,9 +38,9 @@ namespace ImageSharp.PixelFormats.PixelBlenders using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultDarkenPixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultDarkenPixelBlender{TPixel}.cs index e0ff80b66..bca99e2f0 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultDarkenPixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultDarkenPixelBlender{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.PixelFormats.PixelBlenders { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -28,7 +30,7 @@ namespace ImageSharp.PixelFormats.PixelBlenders } /// - public override void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + public override void Blend(Span destination, Span background, Span source, Span amount) { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); @@ -36,9 +38,9 @@ namespace ImageSharp.PixelFormats.PixelBlenders using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultHardLightPixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultHardLightPixelBlender{TPixel}.cs index cec0dc0db..646423cff 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultHardLightPixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultHardLightPixelBlender{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.PixelFormats.PixelBlenders { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -28,7 +30,7 @@ namespace ImageSharp.PixelFormats.PixelBlenders } /// - public override void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + public override void Blend(Span destination, Span background, Span source, Span amount) { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); @@ -36,9 +38,9 @@ namespace ImageSharp.PixelFormats.PixelBlenders using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultLightenPixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultLightenPixelBlender{TPixel}.cs index 32cd20650..55ad81e7a 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultLightenPixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultLightenPixelBlender{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.PixelFormats.PixelBlenders { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -28,7 +30,7 @@ namespace ImageSharp.PixelFormats.PixelBlenders } /// - public override void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + public override void Blend(Span destination, Span background, Span source, Span amount) { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); @@ -36,9 +38,9 @@ namespace ImageSharp.PixelFormats.PixelBlenders using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultMultiplyPixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultMultiplyPixelBlender{TPixel}.cs index 7e0137018..e21efaed0 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultMultiplyPixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultMultiplyPixelBlender{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.PixelFormats.PixelBlenders { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -28,7 +30,7 @@ namespace ImageSharp.PixelFormats.PixelBlenders } /// - public override void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + public override void Blend(Span destination, Span background, Span source, Span amount) { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); @@ -36,9 +38,9 @@ namespace ImageSharp.PixelFormats.PixelBlenders using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultNormalPixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultNormalPixelBlender{TPixel}.cs index 47bb084ca..9d63d11e0 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultNormalPixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultNormalPixelBlender{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.PixelFormats.PixelBlenders { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -28,7 +30,7 @@ namespace ImageSharp.PixelFormats.PixelBlenders } /// - public override void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + public override void Blend(Span destination, Span background, Span source, Span amount) { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); @@ -36,9 +38,9 @@ namespace ImageSharp.PixelFormats.PixelBlenders using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultOverlayPixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultOverlayPixelBlender{TPixel}.cs index fcb56e3dc..8172909ec 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultOverlayPixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultOverlayPixelBlender{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.PixelFormats.PixelBlenders { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -28,7 +30,7 @@ namespace ImageSharp.PixelFormats.PixelBlenders } /// - public override void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + public override void Blend(Span destination, Span background, Span source, Span amount) { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); @@ -36,9 +38,9 @@ namespace ImageSharp.PixelFormats.PixelBlenders using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultScreenPixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultScreenPixelBlender{TPixel}.cs index df0de293c..8405c3946 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultScreenPixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultScreenPixelBlender{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.PixelFormats.PixelBlenders { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -28,7 +30,7 @@ namespace ImageSharp.PixelFormats.PixelBlenders } /// - public override void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + public override void Blend(Span destination, Span background, Span source, Span amount) { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); @@ -36,9 +38,9 @@ namespace ImageSharp.PixelFormats.PixelBlenders using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultSubstractPixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultSubstractPixelBlender{TPixel}.cs index 415ac04b2..ab44cb760 100644 --- a/src/ImageSharp/PixelFormats/PixelBlenders/DefaultSubstractPixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlenders/DefaultSubstractPixelBlender{TPixel}.cs @@ -7,6 +7,8 @@ namespace ImageSharp.PixelFormats.PixelBlenders { using System; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -28,7 +30,7 @@ namespace ImageSharp.PixelFormats.PixelBlenders } /// - public override void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + public override void Blend(Span destination, Span background, Span source, Span amount) { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); Guard.MustBeGreaterThanOrEqualTo(source.Length, destination.Length, nameof(source.Length)); @@ -36,9 +38,9 @@ namespace ImageSharp.PixelFormats.PixelBlenders using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); diff --git a/src/ImageSharp/PixelFormats/PixelBlender{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelBlender{TPixel}.cs index 23340a60a..1a1d1cd05 100644 --- a/src/ImageSharp/PixelFormats/PixelBlender{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelBlender{TPixel}.cs @@ -5,6 +5,8 @@ namespace ImageSharp.PixelFormats { + using System; + /// /// Abstract base class for calling pixel composition functions /// @@ -34,6 +36,6 @@ namespace ImageSharp.PixelFormats /// A value between 0 and 1 indicating the weight of the second source vector. /// At amount = 0, "from" is returned, at amount = 1, "to" is returned. /// - public abstract void Blend(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount); + public abstract void Blend(Span destination, Span background, Span source, Span amount); } } diff --git a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs index 207040521..fc1817a89 100644 --- a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs @@ -5,9 +5,12 @@ namespace ImageSharp.PixelFormats { + using System; using System.Numerics; using System.Runtime.CompilerServices; + using ImageSharp.Memory; + /// /// A stateless class implementing Strategy Pattern for batched pixel-data conversion operations /// for pixel buffers of type . @@ -24,10 +27,10 @@ namespace ImageSharp.PixelFormats /// /// Bulk version of /// - /// The to the source vectors. - /// The to the destination colors. + /// The to the source vectors. + /// The to the destination colors. /// The number of pixels to convert. - internal virtual void PackFromVector4(BufferSpan sourceVectors, BufferSpan destColors, int count) + internal virtual void PackFromVector4(Span sourceVectors, Span destColors, int count) { ref Vector4 sourceRef = ref sourceVectors.DangerousGetPinnableReference(); ref TPixel destRef = ref destColors.DangerousGetPinnableReference(); @@ -43,10 +46,10 @@ namespace ImageSharp.PixelFormats /// /// Bulk version of . /// - /// The to the source colors. - /// The to the destination vectors. + /// The to the source colors. + /// The to the destination vectors. /// The number of pixels to convert. - internal virtual void ToVector4(BufferSpan sourceColors, BufferSpan destVectors, int count) + internal virtual void ToVector4(Span sourceColors, Span destVectors, int count) { ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); ref Vector4 destRef = ref destVectors.DangerousGetPinnableReference(); @@ -62,10 +65,10 @@ namespace ImageSharp.PixelFormats /// /// Bulk version of that converts data in . /// - /// The to the source bytes. - /// The to the destination colors. + /// The to the source bytes. + /// The to the destination colors. /// The number of pixels to convert. - internal virtual void PackFromXyzBytes(BufferSpan sourceBytes, BufferSpan destColors, int count) + internal virtual void PackFromXyzBytes(Span sourceBytes, Span destColors, int count) { ref byte sourceRef = ref sourceBytes.DangerousGetPinnableReference(); ref TPixel destRef = ref destColors.DangerousGetPinnableReference(); @@ -85,10 +88,11 @@ namespace ImageSharp.PixelFormats /// /// Bulk version of . /// - /// The to the source colors. - /// The to the destination bytes. + /// The to the source colors. + /// The to the destination bytes. + /// The starting index of the . /// The number of pixels to convert. - internal virtual void ToXyzBytes(BufferSpan sourceColors, BufferSpan destBytes, int count) + internal virtual void ToXyzBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) { ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); byte[] dest = destBytes.Array; @@ -96,17 +100,17 @@ namespace ImageSharp.PixelFormats for (int i = 0; i < count; i++) { ref TPixel sp = ref Unsafe.Add(ref sourceRef, i); - sp.ToXyzBytes(dest, destBytes.Start + (i * 3)); + sp.ToXyzBytes(dest, startIndex + (i * 3)); } } /// /// Bulk version of that converts data in . /// - /// The to the source bytes. - /// The to the destination colors. + /// The to the source bytes. + /// The to the destination colors. /// The number of pixels to convert. - internal virtual void PackFromXyzwBytes(BufferSpan sourceBytes, BufferSpan destColors, int count) + internal virtual void PackFromXyzwBytes(Span sourceBytes, Span destColors, int count) { ref byte sourceRef = ref sourceBytes.DangerousGetPinnableReference(); ref TPixel destRef = ref destColors.DangerousGetPinnableReference(); @@ -126,10 +130,11 @@ namespace ImageSharp.PixelFormats /// /// Bulk version of . /// - /// The to the source colors. - /// The to the destination bytes. + /// The to the source colors. + /// The to the destination bytes. + /// The starting index of the . /// The number of pixels to convert. - internal virtual void ToXyzwBytes(BufferSpan sourceColors, BufferSpan destBytes, int count) + internal virtual void ToXyzwBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) { ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); byte[] dest = destBytes.Array; @@ -137,17 +142,17 @@ namespace ImageSharp.PixelFormats for (int i = 0; i < count; i++) { ref TPixel sp = ref Unsafe.Add(ref sourceRef, i); - sp.ToXyzwBytes(dest, destBytes.Start + (i * 4)); + sp.ToXyzwBytes(dest, startIndex + (i * 4)); } } /// /// Bulk version of that converts data in . /// - /// The to the source bytes. - /// The to the destination colors. + /// The to the source bytes. + /// The to the destination colors. /// The number of pixels to convert. - internal virtual void PackFromZyxBytes(BufferSpan sourceBytes, BufferSpan destColors, int count) + internal virtual void PackFromZyxBytes(Span sourceBytes, Span destColors, int count) { ref byte sourceRef = ref sourceBytes.DangerousGetPinnableReference(); ref TPixel destRef = ref destColors.DangerousGetPinnableReference(); @@ -167,10 +172,11 @@ namespace ImageSharp.PixelFormats /// /// Bulk version of . /// - /// The to the source colors. - /// The to the destination bytes. + /// The to the source colors. + /// The to the destination bytes. + /// The starting index of the . /// The number of pixels to convert. - internal virtual void ToZyxBytes(BufferSpan sourceColors, BufferSpan destBytes, int count) + internal virtual void ToZyxBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) { ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); byte[] dest = destBytes.Array; @@ -178,17 +184,17 @@ namespace ImageSharp.PixelFormats for (int i = 0; i < count; i++) { ref TPixel sp = ref Unsafe.Add(ref sourceRef, i); - sp.ToZyxBytes(dest, destBytes.Start + (i * 3)); + sp.ToZyxBytes(dest, startIndex + (i * 3)); } } /// /// Bulk version of that converts data in . /// - /// The to the source bytes. - /// The to the destination colors. + /// The to the source bytes. + /// The to the destination colors. /// The number of pixels to convert. - internal virtual void PackFromZyxwBytes(BufferSpan sourceBytes, BufferSpan destColors, int count) + internal virtual void PackFromZyxwBytes(Span sourceBytes, Span destColors, int count) { ref byte sourceRef = ref sourceBytes.DangerousGetPinnableReference(); ref TPixel destRef = ref destColors.DangerousGetPinnableReference(); @@ -208,10 +214,11 @@ namespace ImageSharp.PixelFormats /// /// Bulk version of . /// - /// The to the source colors. - /// The to the destination bytes. + /// The to the source colors. + /// The to the destination bytes. + /// The starting index of the . /// The number of pixels to convert. - internal virtual void ToZyxwBytes(BufferSpan sourceColors, BufferSpan destBytes, int count) + internal virtual void ToZyxwBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) { ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); byte[] dest = destBytes.Array; @@ -219,7 +226,7 @@ namespace ImageSharp.PixelFormats for (int i = 0; i < count; i++) { ref TPixel sp = ref Unsafe.Add(ref sourceRef, i); - sp.ToZyxwBytes(dest, destBytes.Start + (i * 4)); + sp.ToZyxwBytes(dest, startIndex + (i * 4)); } } } diff --git a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs index fd94a3592..c9dca89a3 100644 --- a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs +++ b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs @@ -10,6 +10,7 @@ namespace ImageSharp using System.Runtime.CompilerServices; using System.Runtime.InteropServices; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -26,8 +27,8 @@ namespace ImageSharp /// SIMD optimized bulk implementation of /// that works only with `count` divisible by . /// - /// The to the source colors. - /// The to the dstination vectors. + /// The to the source colors. + /// The to the dstination vectors. /// The number of pixels to convert. /// /// Implementation adapted from: @@ -39,7 +40,7 @@ namespace ImageSharp /// https://github.com/dotnet/corefx/issues/15957 /// /// - internal static void ToVector4SimdAligned(BufferSpan sourceColors, BufferSpan destVectors, int count) + internal static void ToVector4SimdAligned(Span sourceColors, Span destVectors, int count) { if (!Vector.IsHardwareAccelerated) { @@ -90,7 +91,7 @@ namespace ImageSharp } /// - internal override void ToVector4(BufferSpan sourceColors, BufferSpan destVectors, int count) + internal override void ToVector4(Span sourceColors, Span destVectors, int count) { if (count < 256 || !Vector.IsHardwareAccelerated) { @@ -117,7 +118,7 @@ namespace ImageSharp } /// - internal override void PackFromXyzBytes(BufferSpan sourceBytes, BufferSpan destColors, int count) + internal override void PackFromXyzBytes(Span sourceBytes, Span destColors, int count) { ref RGB24 sourceRef = ref Unsafe.As(ref sourceBytes.DangerousGetPinnableReference()); ref Rgba32 destRef = ref destColors.DangerousGetPinnableReference(); @@ -133,10 +134,10 @@ namespace ImageSharp } /// - internal override void ToXyzBytes(BufferSpan sourceColors, BufferSpan destBytes, int count) + internal override void ToXyzBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) { ref Rgba32 sourceRef = ref sourceColors.DangerousGetPinnableReference(); - ref RGB24 destRef = ref Unsafe.As(ref destBytes.DangerousGetPinnableReference()); + ref RGB24 destRef = ref Unsafe.As(ref new Span(destBytes.Array, startIndex).DangerousGetPinnableReference()); for (int i = 0; i < count; i++) { @@ -148,19 +149,19 @@ namespace ImageSharp } /// - internal override unsafe void PackFromXyzwBytes(BufferSpan sourceBytes, BufferSpan destColors, int count) + internal override unsafe void PackFromXyzwBytes(Span sourceBytes, Span destColors, int count) { - BufferSpan.Copy(sourceBytes, destColors.AsBytes(), count * sizeof(Rgba32)); + SpanHelper.Copy(sourceBytes, destColors.AsBytes(), count * sizeof(Rgba32)); } /// - internal override unsafe void ToXyzwBytes(BufferSpan sourceColors, BufferSpan destBytes, int count) + internal override unsafe void ToXyzwBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) { - BufferSpan.Copy(sourceColors.AsBytes(), destBytes, count * sizeof(Rgba32)); + SpanHelper.Copy(sourceColors.AsBytes(), new Span(destBytes.Array, startIndex), count * sizeof(Rgba32)); } /// - internal override void PackFromZyxBytes(BufferSpan sourceBytes, BufferSpan destColors, int count) + internal override void PackFromZyxBytes(Span sourceBytes, Span destColors, int count) { ref RGB24 sourceRef = ref Unsafe.As(ref sourceBytes.DangerousGetPinnableReference()); ref Rgba32 destRef = ref destColors.DangerousGetPinnableReference(); @@ -176,10 +177,10 @@ namespace ImageSharp } /// - internal override void ToZyxBytes(BufferSpan sourceColors, BufferSpan destBytes, int count) + internal override void ToZyxBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) { ref Rgba32 sourceRef = ref sourceColors.DangerousGetPinnableReference(); - ref RGB24 destRef = ref Unsafe.As(ref destBytes.DangerousGetPinnableReference()); + ref RGB24 destRef = ref Unsafe.As(ref new Span(destBytes.Array, startIndex).DangerousGetPinnableReference()); for (int i = 0; i < count; i++) { @@ -191,7 +192,7 @@ namespace ImageSharp } /// - internal override void PackFromZyxwBytes(BufferSpan sourceBytes, BufferSpan destColors, int count) + internal override void PackFromZyxwBytes(Span sourceBytes, Span destColors, int count) { ref RGBA32 sourceRef = ref Unsafe.As(ref sourceBytes.DangerousGetPinnableReference()); ref Rgba32 destRef = ref destColors.DangerousGetPinnableReference(); @@ -206,10 +207,10 @@ namespace ImageSharp } /// - internal override void ToZyxwBytes(BufferSpan sourceColors, BufferSpan destBytes, int count) + internal override void ToZyxwBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) { ref Rgba32 sourceRef = ref sourceColors.DangerousGetPinnableReference(); - ref RGBA32 destRef = ref Unsafe.As(ref destBytes.DangerousGetPinnableReference()); + ref RGBA32 destRef = ref Unsafe.As(ref new Span(destBytes.Array, startIndex).DangerousGetPinnableReference()); for (int i = 0; i < count; i++) { diff --git a/src/ImageSharp/PixelFormats/RgbaVector.PixelOperations.cs b/src/ImageSharp/PixelFormats/RgbaVector.PixelOperations.cs index da0900c11..5c7ee17ca 100644 --- a/src/ImageSharp/PixelFormats/RgbaVector.PixelOperations.cs +++ b/src/ImageSharp/PixelFormats/RgbaVector.PixelOperations.cs @@ -5,8 +5,11 @@ namespace ImageSharp.PixelFormats { + using System; using System.Numerics; + using ImageSharp.Memory; + /// /// Provides optimized overrides for bulk operations. /// @@ -18,9 +21,9 @@ namespace ImageSharp.PixelFormats internal class PixelOperations : PixelOperations { /// - internal override unsafe void ToVector4(BufferSpan sourceColors, BufferSpan destVectors, int count) + internal override unsafe void ToVector4(Span sourceColors, Span destVectors, int count) { - BufferSpan.Copy(sourceColors.AsBytes(), destVectors.AsBytes(), count * sizeof(Vector4)); + SpanHelper.Copy(sourceColors.AsBytes(), destVectors.AsBytes(), count * sizeof(Vector4)); } } } diff --git a/src/ImageSharp/Processing/Processors/Convolution/BoxBlurProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/BoxBlurProcessor.cs index 652442388..b97e07079 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/BoxBlurProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/BoxBlurProcessor.cs @@ -7,6 +7,7 @@ namespace ImageSharp.Processing.Processors { using System; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/Convolution2DProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/Convolution2DProcessor.cs index e6a42cc0c..29086b53f 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/Convolution2DProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/Convolution2DProcessor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System.Numerics; using System.Threading.Tasks; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor.cs index 965a725a1..e39104793 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/Convolution2PassProcessor.cs @@ -9,6 +9,7 @@ namespace ImageSharp.Processing.Processors using System.Numerics; using System.Threading.Tasks; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor.cs index 475f14ccf..17d5e3243 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/ConvolutionProcessor.cs @@ -9,6 +9,7 @@ namespace ImageSharp.Processing.Processors using System.Numerics; using System.Threading.Tasks; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetector2DProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetector2DProcessor.cs index 457854a31..6f1057e00 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetector2DProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetector2DProcessor.cs @@ -7,6 +7,7 @@ namespace ImageSharp.Processing.Processors { using System; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetectorCompassProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetectorCompassProcessor.cs index a4d1d5409..a03d12677 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetectorCompassProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetectorCompassProcessor.cs @@ -9,6 +9,7 @@ namespace ImageSharp.Processing.Processors using System.Numerics; using System.Threading.Tasks; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetectorProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetectorProcessor.cs index e7670dd1d..415b574b8 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetectorProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/EdgeDetectorProcessor.cs @@ -7,6 +7,7 @@ namespace ImageSharp.Processing.Processors { using System; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/KayyaliProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/KayyaliProcessor.cs index d72816a76..b1361b514 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/KayyaliProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/KayyaliProcessor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/KirschProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/KirschProcessor.cs index d882bdb16..af4700bb9 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/KirschProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/KirschProcessor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/Laplacian3X3Processor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/Laplacian3X3Processor.cs index 39f64fb5a..5f58d56f8 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/Laplacian3X3Processor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/Laplacian3X3Processor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/Laplacian5X5Processor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/Laplacian5X5Processor.cs index c65cb5bd7..2e365374c 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/Laplacian5X5Processor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/Laplacian5X5Processor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/LaplacianOfGaussianProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/LaplacianOfGaussianProcessor.cs index 57ab4dce6..de2594653 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/LaplacianOfGaussianProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/LaplacianOfGaussianProcessor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/PrewittProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/PrewittProcessor.cs index d1515dee8..1b2d5f50e 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/PrewittProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/PrewittProcessor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/RobertsCrossProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/RobertsCrossProcessor.cs index bab9ff622..d1b9614b6 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/RobertsCrossProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/RobertsCrossProcessor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/RobinsonProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/RobinsonProcessor.cs index 4afca0f01..bc687f674 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/RobinsonProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/RobinsonProcessor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/ScharrProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/ScharrProcessor.cs index a583d3c0d..339b9741f 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/ScharrProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/ScharrProcessor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/SobelProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/SobelProcessor.cs index 1c2a6a18f..b9060ecbc 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/SobelProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/EdgeDetection/SobelProcessor.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Processing.Processors using System; using System.Diagnostics.CodeAnalysis; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/GaussianBlurProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/GaussianBlurProcessor.cs index 87de922a1..4cd49e149 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/GaussianBlurProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/GaussianBlurProcessor.cs @@ -7,6 +7,7 @@ namespace ImageSharp.Processing.Processors { using System; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Convolution/GaussianSharpenProcessor.cs b/src/ImageSharp/Processing/Processors/Convolution/GaussianSharpenProcessor.cs index 34d099033..5bb29a67e 100644 --- a/src/ImageSharp/Processing/Processors/Convolution/GaussianSharpenProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Convolution/GaussianSharpenProcessor.cs @@ -7,6 +7,7 @@ namespace ImageSharp.Processing.Processors { using System; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// diff --git a/src/ImageSharp/Processing/Processors/Effects/BackgroundColorProcessor.cs b/src/ImageSharp/Processing/Processors/Effects/BackgroundColorProcessor.cs index 511a810b2..cc95f15fc 100644 --- a/src/ImageSharp/Processing/Processors/Effects/BackgroundColorProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Effects/BackgroundColorProcessor.cs @@ -9,6 +9,7 @@ namespace ImageSharp.Processing.Processors using System.Numerics; using System.Threading.Tasks; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -82,7 +83,7 @@ namespace ImageSharp.Processing.Processors { int offsetY = y - startY; - BufferSpan destination = sourcePixels.GetRowSpan(offsetY).Slice(minX - startX, width); + Span destination = sourcePixels.GetRowSpan(offsetY).Slice(minX - startX, width); // this switched color & destination in the 2nd and 3rd places because we are applying the target colour under the current one blender.Blend(destination, colors, destination, amount); diff --git a/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs b/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs index 5b5d64a9c..9de91c47b 100644 --- a/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Overlays/GlowProcessor.cs @@ -9,6 +9,7 @@ namespace ImageSharp.Processing.Processors using System.Numerics; using System.Threading.Tasks; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -96,7 +97,7 @@ namespace ImageSharp.Processing.Processors amounts[i] = (this.options.BlendPercentage * (1 - (.95F * (distance / maxDistance)))).Clamp(0, 1); } - BufferSpan destination = sourcePixels.GetRowSpan(offsetY).Slice(offsetX, width); + Span destination = sourcePixels.GetRowSpan(offsetY).Slice(offsetX, width); this.blender.Blend(destination, destination, rowColors, amounts); } diff --git a/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs b/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs index d698b543c..be431b07d 100644 --- a/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Overlays/VignetteProcessor.cs @@ -9,6 +9,7 @@ namespace ImageSharp.Processing.Processors using System.Numerics; using System.Threading.Tasks; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -104,7 +105,7 @@ namespace ImageSharp.Processing.Processors amounts[i] = (this.options.BlendPercentage * (.9F * (distance / maxDistance))).Clamp(0, 1); } - BufferSpan destination = sourcePixels.GetRowSpan(offsetY).Slice(offsetX, width); + Span destination = sourcePixels.GetRowSpan(offsetY).Slice(offsetX, width); this.blender.Blend(destination, destination, rowColors, amounts); } diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs index f9c78c12f..d49f37803 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResamplingWeightedProcessor.Weights.cs @@ -4,6 +4,8 @@ namespace ImageSharp.Processing.Processors using System.Numerics; using System.Runtime.CompilerServices; + using ImageSharp.Memory; + /// /// Conains the definition of and . /// @@ -12,7 +14,7 @@ namespace ImageSharp.Processing.Processors /// /// Points to a collection of of weights allocated in . /// - internal unsafe struct WeightsWindow + internal struct WeightsWindow { /// /// The local left index position @@ -22,9 +24,7 @@ namespace ImageSharp.Processing.Processors /// /// The span of weights pointing to . /// - // TODO: In the case of switching to official System.Memory and System.Buffers.Primitives this should be System.Buffers.Buffer (formerly Memory), because Span is stack-only! - // see: https://github.com/dotnet/corefxlab/blob/873d35ebed7264e2f9adb556f3b61bebc12395d6/docs/specs/memory.md - public BufferSpan Span; + public Span Span; /// /// Initializes a new instance of the struct. @@ -32,7 +32,7 @@ namespace ImageSharp.Processing.Processors /// The local left index /// The span [MethodImpl(MethodImplOptions.AggressiveInlining)] - internal WeightsWindow(int left, BufferSpan span) + internal WeightsWindow(int left, Span span) { this.Left = left; this.Span = span; @@ -55,7 +55,7 @@ namespace ImageSharp.Processing.Processors /// The source row position. /// The weighted sum [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Vector4 ComputeWeightedRowSum(BufferSpan rowSpan, int sourceX) + public Vector4 ComputeWeightedRowSum(Span rowSpan, int sourceX) { ref float horizontalValues = ref this.Ptr; int left = this.Left; @@ -82,7 +82,7 @@ namespace ImageSharp.Processing.Processors /// The source row position. /// The weighted sum [MethodImpl(MethodImplOptions.AggressiveInlining)] - public Vector4 ComputeExpandedWeightedRowSum(BufferSpan rowSpan, int sourceX) + public Vector4 ComputeExpandedWeightedRowSum(Span rowSpan, int sourceX) { ref float horizontalValues = ref this.Ptr; int left = this.Left; @@ -169,7 +169,7 @@ namespace ImageSharp.Processing.Processors /// The weights public WeightsWindow GetWeightsWindow(int destIdx, int leftIdx, int rightIdx) { - BufferSpan span = this.dataBuffer.GetRowSpan(destIdx).Slice(leftIdx, rightIdx - leftIdx + 1); + Span span = this.dataBuffer.GetRowSpan(destIdx).Slice(leftIdx, rightIdx - leftIdx + 1); return new WeightsWindow(leftIdx, span); } } diff --git a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs index dde79a7e4..61a64f60f 100644 --- a/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs +++ b/src/ImageSharp/Processing/Processors/Transforms/ResizeProcessor.cs @@ -9,6 +9,7 @@ namespace ImageSharp.Processing.Processors using System.Numerics; using System.Threading.Tasks; + using ImageSharp.Memory; using ImageSharp.PixelFormats; /// @@ -121,7 +122,7 @@ namespace ImageSharp.Processing.Processors // TODO: Without Parallel.For() this buffer object could be reused: using (Buffer tempRowBuffer = new Buffer(sourcePixels.Width)) { - BufferSpan sourceRow = sourcePixels.GetRowSpan(y); + Span sourceRow = sourcePixels.GetRowSpan(y); PixelOperations.Instance.ToVector4( sourceRow, diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromVector4ReferenceVsPointer.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromVector4ReferenceVsPointer.cs index 65c4a235d..30d93e3d9 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromVector4ReferenceVsPointer.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromVector4ReferenceVsPointer.cs @@ -6,7 +6,7 @@ using BenchmarkDotNet.Attributes; using ImageSharp; - using ImageSharp.PixelFormats; + using ImageSharp.Memory; /// /// Compares two implementation candidates for general BulkPixelOperations.ToVector4(): diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs index d363769d0..501ae7949 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/PackFromXyzw.cs @@ -3,6 +3,7 @@ namespace ImageSharp.Benchmarks.Color.Bulk { using BenchmarkDotNet.Attributes; + using ImageSharp.Memory; using ImageSharp.PixelFormats; public abstract class PackFromXyzw diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs index cd1797558..65a2988b9 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToVector4.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Benchmarks.Color.Bulk using BenchmarkDotNet.Attributes; + using ImageSharp.Memory; using ImageSharp.PixelFormats; public abstract class ToVector4 diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs index 663f85fb7..af76434a5 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs @@ -3,6 +3,7 @@ namespace ImageSharp.Benchmarks.Color.Bulk { using BenchmarkDotNet.Attributes; + using ImageSharp.Memory; using ImageSharp.PixelFormats; public abstract class ToXyz @@ -45,13 +46,13 @@ namespace ImageSharp.Benchmarks.Color.Bulk [Benchmark] public void CommonBulk() { - new PixelOperations().ToXyzBytes(this.source, this.destination, this.Count); + new PixelOperations().ToXyzBytes(this.source, this.destination, 0, this.Count); } [Benchmark] public void OptimizedBulk() { - PixelOperations.Instance.ToXyzBytes(this.source, this.destination, this.Count); + PixelOperations.Instance.ToXyzBytes(this.source, this.destination, 0, this.Count); } } diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs index 7ac621113..01e7ef371 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs @@ -7,6 +7,8 @@ using System.Threading.Tasks; namespace ImageSharp.Benchmarks.Color.Bulk { using BenchmarkDotNet.Attributes; + + using ImageSharp.Memory; using ImageSharp.PixelFormats; public abstract class ToXyzw @@ -49,16 +51,16 @@ namespace ImageSharp.Benchmarks.Color.Bulk [Benchmark] public void CommonBulk() { - new PixelOperations().ToXyzwBytes(this.source, this.destination, this.Count); + new PixelOperations().ToXyzwBytes(this.source, this.destination, 0, this.Count); } [Benchmark] public void OptimizedBulk() { - PixelOperations.Instance.ToXyzwBytes(this.source, this.destination, this.Count); + PixelOperations.Instance.ToXyzwBytes(this.source, this.destination, 0, this.Count); } } - + public class ToXyzw_Rgba32 : ToXyzw { } diff --git a/tests/ImageSharp.Benchmarks/General/Array2D.cs b/tests/ImageSharp.Benchmarks/General/Array2D.cs index fce92e9be..9d44fc93d 100644 --- a/tests/ImageSharp.Benchmarks/General/Array2D.cs +++ b/tests/ImageSharp.Benchmarks/General/Array2D.cs @@ -9,6 +9,8 @@ namespace ImageSharp.Benchmarks.General using BenchmarkDotNet.Attributes; + using ImageSharp.Memory; + public class Array2D { private float[] flatArray; diff --git a/tests/ImageSharp.Benchmarks/General/ClearBuffer.cs b/tests/ImageSharp.Benchmarks/General/ClearBuffer.cs index 97deb72c5..fc1b46a91 100644 --- a/tests/ImageSharp.Benchmarks/General/ClearBuffer.cs +++ b/tests/ImageSharp.Benchmarks/General/ClearBuffer.cs @@ -3,11 +3,10 @@ namespace ImageSharp.Benchmarks.General { using System; using System.Runtime.CompilerServices; - using System.Runtime.InteropServices; using BenchmarkDotNet.Attributes; - using ImageSharp.PixelFormats; + using ImageSharp.Memory; public unsafe class ClearBuffer { diff --git a/tests/ImageSharp.Benchmarks/General/IterateArray.cs b/tests/ImageSharp.Benchmarks/General/IterateArray.cs index eeab6506a..b9e2f7acd 100644 --- a/tests/ImageSharp.Benchmarks/General/IterateArray.cs +++ b/tests/ImageSharp.Benchmarks/General/IterateArray.cs @@ -5,6 +5,8 @@ namespace ImageSharp.Benchmarks.General using BenchmarkDotNet.Attributes; + using ImageSharp.Memory; + public class IterateArray { // Usual pinned stuff diff --git a/tests/ImageSharp.Benchmarks/General/PixelIndexing.cs b/tests/ImageSharp.Benchmarks/General/PixelIndexing.cs index d9237b801..18c99a744 100644 --- a/tests/ImageSharp.Benchmarks/General/PixelIndexing.cs +++ b/tests/ImageSharp.Benchmarks/General/PixelIndexing.cs @@ -5,6 +5,8 @@ using BenchmarkDotNet.Attributes; + using ImageSharp.Memory; + // Pixel indexing benchmarks compare different methods for getting/setting all pixel values in a subsegment of a single pixel row. public abstract unsafe class PixelIndexing { diff --git a/tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs b/tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs index e6d1a2fd0..018f11334 100644 --- a/tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs +++ b/tests/ImageSharp.Benchmarks/General/Vectorization/VectorFetching.cs @@ -1,9 +1,11 @@ namespace ImageSharp.Benchmarks.General.Vectorization { + using System; using System.Numerics; using System.Runtime.CompilerServices; using BenchmarkDotNet.Attributes; + using ImageSharp.Memory; /// /// This benchmark compares different methods for fetching memory data into @@ -88,11 +90,11 @@ namespace ImageSharp.Benchmarks.General.Vectorization } [Benchmark] - public void FetchWithBufferSpanUtility() + public void FetchWithSpanUtility() { Vector v = new Vector(this.testValue); - BufferSpan span = new BufferSpan(this.data); + Span span = new Span(this.data); ref Vector start = ref span.FetchVector(); diff --git a/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs b/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs index 474788b35..1da69f1a8 100644 --- a/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs +++ b/tests/ImageSharp.Benchmarks/PixelBlenders/PorterDuffBulkVsPixel.cs @@ -5,16 +5,19 @@ namespace ImageSharp.Benchmarks { + using System; using BenchmarkDotNet.Attributes; using ImageSharp.PixelFormats; using CoreSize = ImageSharp.Size; using System.Numerics; + + using ImageSharp.Memory; using ImageSharp.PixelFormats.PixelBlenders; public class PorterDuffBulkVsPixel : BenchmarkBase { - private void BulkVectorConvert(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + private void BulkVectorConvert(Span destination, Span background, Span source, Span amount) where TPixel : struct, IPixel { Guard.MustBeGreaterThanOrEqualTo(background.Length, destination.Length, nameof(background.Length)); @@ -23,9 +26,9 @@ namespace ImageSharp.Benchmarks using (Buffer buffer = new Buffer(destination.Length * 3)) { - BufferSpan destinationSpan = buffer.Slice(0, destination.Length); - BufferSpan backgroundSpan = buffer.Slice(destination.Length, destination.Length); - BufferSpan sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); + Span destinationSpan = buffer.Slice(0, destination.Length); + Span backgroundSpan = buffer.Slice(destination.Length, destination.Length); + Span sourceSpan = buffer.Slice(destination.Length * 2, destination.Length); PixelOperations.Instance.ToVector4(background, backgroundSpan, destination.Length); PixelOperations.Instance.ToVector4(source, sourceSpan, destination.Length); @@ -38,7 +41,7 @@ namespace ImageSharp.Benchmarks PixelOperations.Instance.PackFromVector4(destinationSpan, destination, destination.Length); } } - private void BulkPixelConvert(BufferSpan destination, BufferSpan background, BufferSpan source, BufferSpan amount) + private void BulkPixelConvert(Span destination, Span background, Span source, Span amount) where TPixel : struct, IPixel { Guard.MustBeGreaterThanOrEqualTo(destination.Length, background.Length, nameof(destination)); @@ -66,7 +69,7 @@ namespace ImageSharp.Benchmarks { for (int y = 0; y < image.Height; y++) { - BufferSpan span = pixels.GetRowSpan(y); + Span span = pixels.GetRowSpan(y); BulkVectorConvert(span, span, span, amounts); } } @@ -89,7 +92,7 @@ namespace ImageSharp.Benchmarks { for (int y = 0; y < image.Height; y++) { - BufferSpan span = pixels.GetRowSpan(y); + Span span = pixels.GetRowSpan(y); BulkPixelConvert(span, span, span, amounts); } } diff --git a/tests/ImageSharp.Benchmarks/Samplers/Glow.cs b/tests/ImageSharp.Benchmarks/Samplers/Glow.cs index 76a0bc23b..7608d3065 100644 --- a/tests/ImageSharp.Benchmarks/Samplers/Glow.cs +++ b/tests/ImageSharp.Benchmarks/Samplers/Glow.cs @@ -15,6 +15,8 @@ namespace ImageSharp.Benchmarks using System; using System.Threading.Tasks; + using ImageSharp.Memory; + public class Glow : BenchmarkBase { private GlowProcessor bulk; diff --git a/tests/ImageSharp.Benchmarks/Samplers/Resize.cs b/tests/ImageSharp.Benchmarks/Samplers/Resize.cs index 60c0d31d2..d96be70f7 100644 --- a/tests/ImageSharp.Benchmarks/Samplers/Resize.cs +++ b/tests/ImageSharp.Benchmarks/Samplers/Resize.cs @@ -46,34 +46,34 @@ namespace ImageSharp.Benchmarks } } - [Benchmark(Description = "ImageSharp Vector Resize")] - public CoreSize ResizeCoreVector() - { - using (Image image = new Image(2000, 2000)) - { - image.Resize(400, 400); - return new CoreSize(image.Width, image.Height); - } - } + //[Benchmark(Description = "ImageSharp Vector Resize")] + //public CoreSize ResizeCoreVector() + //{ + // using (Image image = new Image(2000, 2000)) + // { + // image.Resize(400, 400); + // return new CoreSize(image.Width, image.Height); + // } + //} - [Benchmark(Description = "ImageSharp Compand Resize")] - public CoreSize ResizeCoreCompand() - { - using (Image image = new Image(2000, 2000)) - { - image.Resize(400, 400, true); - return new CoreSize(image.Width, image.Height); - } - } + //[Benchmark(Description = "ImageSharp Compand Resize")] + //public CoreSize ResizeCoreCompand() + //{ + // using (Image image = new Image(2000, 2000)) + // { + // image.Resize(400, 400, true); + // return new CoreSize(image.Width, image.Height); + // } + //} - [Benchmark(Description = "ImageSharp Vector Compand Resize")] - public CoreSize ResizeCoreVectorCompand() - { - using (Image image = new Image(2000, 2000)) - { - image.Resize(400, 400, true); - return new CoreSize(image.Width, image.Height); - } - } + //[Benchmark(Description = "ImageSharp Vector Compand Resize")] + //public CoreSize ResizeCoreVectorCompand() + //{ + // using (Image image = new Image(2000, 2000)) + // { + // image.Resize(400, 400, true); + // return new CoreSize(image.Width, image.Height); + // } + //} } } diff --git a/tests/ImageSharp.Tests/Colors/PixelOperationsTests.cs b/tests/ImageSharp.Tests/Colors/PixelOperationsTests.cs index 4deeb40e8..e6d23dfc5 100644 --- a/tests/ImageSharp.Tests/Colors/PixelOperationsTests.cs +++ b/tests/ImageSharp.Tests/Colors/PixelOperationsTests.cs @@ -5,6 +5,7 @@ namespace ImageSharp.Tests.Colors using System; using System.Numerics; + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Xunit; @@ -180,7 +181,7 @@ namespace ImageSharp.Tests.Colors TestOperation( source, expected, - (s, d) => Operations.ToXyzBytes(s, d, count) + (s, d) => Operations.ToXyzBytes(s, d, 0, count) ); } @@ -221,7 +222,7 @@ namespace ImageSharp.Tests.Colors TestOperation( source, expected, - (s, d) => Operations.ToXyzwBytes(s, d, count) + (s, d) => Operations.ToXyzwBytes(s, d, 0, count) ); } @@ -262,7 +263,7 @@ namespace ImageSharp.Tests.Colors TestOperation( source, expected, - (s, d) => Operations.ToZyxBytes(s, d, count) + (s, d) => Operations.ToZyxBytes(s, d, 0, count) ); } @@ -303,7 +304,7 @@ namespace ImageSharp.Tests.Colors TestOperation( source, expected, - (s, d) => Operations.ToZyxwBytes(s, d, count) + (s, d) => Operations.ToZyxwBytes(s, d, 0, count) ); } @@ -316,8 +317,8 @@ namespace ImageSharp.Tests.Colors public Buffer ActualDestBuffer { get; } public Buffer ExpectedDestBuffer { get; } - public BufferSpan Source => this.SourceBuffer; - public BufferSpan ActualDest => this.ActualDestBuffer; + public Span Source => this.SourceBuffer; + public Span ActualDest => this.ActualDestBuffer; public TestBuffers(TSource[] source, TDest[] expectedDest) { @@ -366,13 +367,13 @@ namespace ImageSharp.Tests.Colors internal static void TestOperation( TSource[] source, TDest[] expected, - Action, BufferSpan> action) + Action, Buffer> action) where TSource : struct where TDest : struct { using (TestBuffers buffers = new TestBuffers(source, expected)) { - action(buffers.Source, buffers.ActualDest); + action(buffers.Source, buffers.ActualDestBuffer); buffers.Verify(); } } diff --git a/tests/ImageSharp.Tests/Common/Buffer2DTests.cs b/tests/ImageSharp.Tests/Common/Buffer2DTests.cs index ac92ab87b..5f44a132d 100644 --- a/tests/ImageSharp.Tests/Common/Buffer2DTests.cs +++ b/tests/ImageSharp.Tests/Common/Buffer2DTests.cs @@ -4,6 +4,8 @@ namespace ImageSharp.Tests.Common using System; using System.Runtime.CompilerServices; + using ImageSharp.Memory; + using Xunit; using static TestStructs; @@ -13,7 +15,7 @@ namespace ImageSharp.Tests.Common // ReSharper disable once ClassNeverInstantiated.Local private class Assert : Xunit.Assert { - public static void SpanPointsTo(BufferSpan span, Buffer buffer, int bufferOffset = 0) + public static void SpanPointsTo(Span span, Buffer buffer, int bufferOffset = 0) where T : struct { ref T actual = ref span.DangerousGetPinnableReference(); @@ -75,9 +77,9 @@ namespace ImageSharp.Tests.Common { using (Buffer2D buffer = new Buffer2D(width, height)) { - BufferSpan span = buffer.GetRowSpan(y); + Span span = buffer.GetRowSpan(y); - Assert.Equal(width * y, span.Start); + // Assert.Equal(width * y, span.Start); Assert.Equal(width, span.Length); Assert.SpanPointsTo(span, buffer, width * y); } @@ -91,9 +93,9 @@ namespace ImageSharp.Tests.Common { using (Buffer2D buffer = new Buffer2D(width, height)) { - BufferSpan span = buffer.GetRowSpan(x, y); + Span span = buffer.GetRowSpan(x, y); - Assert.Equal(width * y + x, span.Start); + // Assert.Equal(width * y + x, span.Start); Assert.Equal(width - x, span.Length); Assert.SpanPointsTo(span, buffer, width * y + x); } diff --git a/tests/ImageSharp.Tests/Common/BufferSpanTests.cs b/tests/ImageSharp.Tests/Common/BufferSpanTests.cs index e5fcbf9ff..af33a981b 100644 --- a/tests/ImageSharp.Tests/Common/BufferSpanTests.cs +++ b/tests/ImageSharp.Tests/Common/BufferSpanTests.cs @@ -7,13 +7,14 @@ namespace ImageSharp.Tests.Common using System.Numerics; using System.Runtime.CompilerServices; + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Xunit; using static TestStructs; - public unsafe class BufferSpanTests + public unsafe class SpanTests { // ReSharper disable once ClassNeverInstantiated.Local private class Assert : Xunit.Assert @@ -31,7 +32,7 @@ namespace ImageSharp.Tests.Common { float[] stuff = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }; - BufferSpan span = new BufferSpan(stuff); + Span span = new Span(stuff); ref Vector v = ref span.FetchVector(); @@ -48,10 +49,10 @@ namespace ImageSharp.Tests.Common using (Buffer colorBuf = new Buffer(fooz)) { - BufferSpan orig = colorBuf.Slice(1); - BufferSpan asBytes = orig.AsBytes(); + Span orig = colorBuf.Slice(1); + Span asBytes = orig.AsBytes(); - Assert.Equal(asBytes.Start, sizeof(Foo)); + // Assert.Equal(asBytes.Start, sizeof(Foo)); Assert.Equal(orig.Length * Unsafe.SizeOf(), asBytes.Length); Assert.SameRefs(ref orig.DangerousGetPinnableReference(), ref asBytes.DangerousGetPinnableReference()); } @@ -65,10 +66,10 @@ namespace ImageSharp.Tests.Common Foo[] array = Foo.CreateArray(3); // Act: - BufferSpan span = new BufferSpan(array); + Span span = new Span(array); // Assert: - Assert.Equal(array, span.Array); + Assert.Equal(array, span.ToArray()); Assert.Equal(3, span.Length); Assert.SameRefs(ref array[0], ref span.DangerousGetPinnableReference()); } @@ -80,11 +81,9 @@ namespace ImageSharp.Tests.Common int start = 2; // Act: - BufferSpan span = new BufferSpan(array, start); + Span span = new Span(array, start); // Assert: - Assert.Equal(array, span.Array); - Assert.Equal(start, span.Start); Assert.SameRefs(ref array[start], ref span.DangerousGetPinnableReference()); Assert.Equal(array.Length - start, span.Length); } @@ -96,11 +95,9 @@ namespace ImageSharp.Tests.Common int start = 2; int length = 3; // Act: - BufferSpan span = new BufferSpan(array, start, length); + Span span = new Span(array, start, length); // Assert: - Assert.Equal(array, span.Array); - Assert.Equal(start, span.Start); Assert.SameRefs(ref array[start], ref span.DangerousGetPinnableReference()); Assert.Equal(length, span.Length); } @@ -116,14 +113,12 @@ namespace ImageSharp.Tests.Common int start1 = 2; int totalOffset = start0 + start1; - BufferSpan span = new BufferSpan(array, start0); + Span span = new Span(array, start0); // Act: span = span.Slice(start1); // Assert: - Assert.Equal(array, span.Array); - Assert.Equal(totalOffset, span.Start); Assert.SameRefs(ref array[totalOffset], ref span.DangerousGetPinnableReference()); Assert.Equal(array.Length - totalOffset, span.Length); } @@ -137,37 +132,35 @@ namespace ImageSharp.Tests.Common int totalOffset = start0 + start1; int sliceLength = 3; - BufferSpan span = new BufferSpan(array, start0); + Span span = new Span(array, start0); // Act: span = span.Slice(start1, sliceLength); // Assert: - Assert.Equal(array, span.Array); - Assert.Equal(totalOffset, span.Start); Assert.SameRefs(ref array[totalOffset], ref span.DangerousGetPinnableReference()); Assert.Equal(sliceLength, span.Length); } } - [Theory] - [InlineData(4)] - [InlineData(1500)] - public void Clear(int count) - { - Foo[] array = Foo.CreateArray(count + 42); + //[Theory] + //[InlineData(4)] + //[InlineData(1500)] + //public void Clear(int count) + //{ + // Foo[] array = Foo.CreateArray(count + 42); - int offset = 2; - BufferSpan ap = new BufferSpan(array, offset); + // int offset = 2; + // Span ap = new Span(array, offset); - // Act: - ap.Clear(count); + // // Act: + // ap.Clear(count); - Assert.NotEqual(default(Foo), array[offset - 1]); - Assert.Equal(default(Foo), array[offset]); - Assert.Equal(default(Foo), array[offset + count - 1]); - Assert.NotEqual(default(Foo), array[offset + count]); - } + // Assert.NotEqual(default(Foo), array[offset - 1]); + // Assert.Equal(default(Foo), array[offset]); + // Assert.Equal(default(Foo), array[offset + count - 1]); + // Assert.NotEqual(default(Foo), array[offset + count]); + //} public class Indexer { @@ -187,7 +180,7 @@ namespace ImageSharp.Tests.Common public void Read(int length, int start, int index) { Foo[] a = Foo.CreateArray(length); - BufferSpan span = new BufferSpan(a, start); + Span span = new Span(a, start); Foo element = span[index]; @@ -199,7 +192,7 @@ namespace ImageSharp.Tests.Common public void Write(int length, int start, int index) { Foo[] a = Foo.CreateArray(length); - BufferSpan span = new BufferSpan(a, start); + Span span = new Span(a, start); span[index] = new Foo(666, 666); @@ -214,9 +207,9 @@ namespace ImageSharp.Tests.Common public void AsBytes_Read(int length, int start, int index, int byteOffset) { Foo[] a = Foo.CreateArray(length); - BufferSpan span = new BufferSpan(a, start); + Span span = new Span(a, start); - BufferSpan bytes = span.AsBytes(); + Span bytes = span.AsBytes(); byte actual = bytes[index * Unsafe.SizeOf() + byteOffset]; @@ -234,7 +227,7 @@ namespace ImageSharp.Tests.Common public void DangerousGetPinnableReference(int start, int length) { Foo[] a = Foo.CreateArray(length); - BufferSpan span = new BufferSpan(a, start); + Span span = new Span(a, start); ref Foo r = ref span.DangerousGetPinnableReference(); Assert.True(Unsafe.AreSame(ref a[start], ref r)); @@ -276,10 +269,10 @@ namespace ImageSharp.Tests.Common Foo[] source = Foo.CreateArray(count + 2); Foo[] dest = new Foo[count + 5]; - BufferSpan apSource = new BufferSpan(source, 1); - BufferSpan apDest = new BufferSpan(dest, 1); + Span apSource = new Span(source, 1); + Span apDest = new Span(dest, 1); - BufferSpan.Copy(apSource, apDest, count - 1); + SpanHelper.Copy(apSource, apDest, count - 1); AssertNotDefault(source, 1); AssertNotDefault(dest, 1); @@ -299,10 +292,10 @@ namespace ImageSharp.Tests.Common AlignedFoo[] source = AlignedFoo.CreateArray(count + 2); AlignedFoo[] dest = new AlignedFoo[count + 5]; - BufferSpan apSource = new BufferSpan(source, 1); - BufferSpan apDest = new BufferSpan(dest, 1); + Span apSource = new Span(source, 1); + Span apDest = new Span(dest, 1); - BufferSpan.Copy(apSource, apDest, count - 1); + SpanHelper.Copy(apSource, apDest, count - 1); AssertNotDefault(source, 1); AssertNotDefault(dest, 1); @@ -322,10 +315,10 @@ namespace ImageSharp.Tests.Common int[] source = CreateTestInts(count + 2); int[] dest = new int[count + 5]; - BufferSpan apSource = new BufferSpan(source, 1); - BufferSpan apDest = new BufferSpan(dest, 1); + Span apSource = new Span(source, 1); + Span apDest = new Span(dest, 1); - BufferSpan.Copy(apSource, apDest, count - 1); + SpanHelper.Copy(apSource, apDest, count - 1); AssertNotDefault(source, 1); AssertNotDefault(dest, 1); @@ -346,10 +339,10 @@ namespace ImageSharp.Tests.Common Foo[] source = Foo.CreateArray(count + 2); byte[] dest = new byte[destCount + sizeof(Foo) * 2]; - BufferSpan apSource = new BufferSpan(source, 1); - BufferSpan apDest = new BufferSpan(dest, sizeof(Foo)); + Span apSource = new Span(source, 1); + Span apDest = new Span(dest, sizeof(Foo)); - BufferSpan.Copy(apSource.AsBytes(), apDest, (count - 1) * sizeof(Foo)); + SpanHelper.Copy(apSource.AsBytes(), apDest, (count - 1) * sizeof(Foo)); AssertNotDefault(source, 1); @@ -369,10 +362,10 @@ namespace ImageSharp.Tests.Common AlignedFoo[] source = AlignedFoo.CreateArray(count + 2); byte[] dest = new byte[destCount + sizeof(AlignedFoo) * 2]; - BufferSpan apSource = new BufferSpan(source, 1); - BufferSpan apDest = new BufferSpan(dest, sizeof(AlignedFoo)); + Span apSource = new Span(source, 1); + Span apDest = new Span(dest, sizeof(AlignedFoo)); - BufferSpan.Copy(apSource.AsBytes(), apDest, (count - 1) * sizeof(AlignedFoo)); + SpanHelper.Copy(apSource.AsBytes(), apDest, (count - 1) * sizeof(AlignedFoo)); AssertNotDefault(source, 1); @@ -392,10 +385,10 @@ namespace ImageSharp.Tests.Common int[] source = CreateTestInts(count + 2); byte[] dest = new byte[destCount + sizeof(int) + 1]; - BufferSpan apSource = new BufferSpan(source); - BufferSpan apDest = new BufferSpan(dest); + Span apSource = new Span(source); + Span apDest = new Span(dest); - BufferSpan.Copy(apSource.AsBytes(), apDest, count * sizeof(int)); + SpanHelper.Copy(apSource.AsBytes(), apDest, count * sizeof(int)); AssertNotDefault(source, 1); @@ -413,10 +406,10 @@ namespace ImageSharp.Tests.Common byte[] source = CreateTestBytes(srcCount); Foo[] dest = new Foo[count + 2]; - BufferSpan apSource = new BufferSpan(source); - BufferSpan apDest = new BufferSpan(dest); + Span apSource = new Span(source); + Span apDest = new Span(dest); - BufferSpan.Copy(apSource, apDest.AsBytes(), count * sizeof(Foo)); + SpanHelper.Copy(apSource, apDest.AsBytes(), count * sizeof(Foo)); AssertNotDefault(source, sizeof(Foo) + 1); AssertNotDefault(dest, 1); @@ -435,7 +428,7 @@ namespace ImageSharp.Tests.Common using (Buffer colorBuf = new Buffer(colors)) using (Buffer byteBuf = new Buffer(colors.Length * 4)) { - BufferSpan.Copy(colorBuf.Span.AsBytes(), byteBuf, colorBuf.Length * sizeof(Rgba32)); + SpanHelper.Copy(colorBuf.Span.AsBytes(), byteBuf, colorBuf.Length * sizeof(Rgba32)); byte[] a = byteBuf.Array; diff --git a/tests/ImageSharp.Tests/Common/BufferTests.cs b/tests/ImageSharp.Tests/Common/BufferTests.cs index 23205d012..010bf40b3 100644 --- a/tests/ImageSharp.Tests/Common/BufferTests.cs +++ b/tests/ImageSharp.Tests/Common/BufferTests.cs @@ -6,6 +6,8 @@ namespace ImageSharp.Tests.Common using System.Runtime.InteropServices; using System.Threading.Tasks; + using ImageSharp.Memory; + using Xunit; using static TestStructs; @@ -15,7 +17,7 @@ namespace ImageSharp.Tests.Common // ReSharper disable once ClassNeverInstantiated.Local private class Assert : Xunit.Assert { - public static void SpanPointsTo(BufferSpan span, Buffer buffer, int bufferOffset = 0) + public static void SpanPointsTo(Span span, Buffer buffer, int bufferOffset = 0) where T : struct { ref T actual = ref span.DangerousGetPinnableReference(); @@ -58,10 +60,8 @@ namespace ImageSharp.Tests.Common } } - [Theory] - [InlineData(42)] - [InlineData(1111)] - public void Clear(int count) + [Fact] + public void Clear() { Foo[] a = { new Foo() { A = 1, B = 2 }, new Foo() { A = 3, B = 4 } }; using (Buffer buffer = new Buffer(a)) @@ -144,10 +144,10 @@ namespace ImageSharp.Tests.Common { using (Buffer buffer = new Buffer(bufferLength)) { - BufferSpan span = buffer; + Span span = buffer; - Assert.Equal(buffer.Array, span.Array); - Assert.Equal(0, span.Start); + //Assert.Equal(buffer.Array, span.ToArray()); + //Assert.Equal(0, span.Start); Assert.SpanPointsTo(span, buffer); Assert.Equal(span.Length, bufferLength); } @@ -158,10 +158,10 @@ namespace ImageSharp.Tests.Common { using (Buffer buffer = new Buffer(42)) { - BufferSpan span = buffer.Span; + Span span = buffer.Span; - Assert.Equal(buffer.Array, span.Array); - Assert.Equal(0, span.Start); + // Assert.Equal(buffer.Array, span.ToArray()); + // Assert.Equal(0, span.Start); Assert.SpanPointsTo(span, buffer); Assert.Equal(span.Length, 42); } @@ -177,10 +177,8 @@ namespace ImageSharp.Tests.Common { using (Buffer buffer = new Buffer(bufferLength)) { - BufferSpan span = buffer.Slice(start); + Span span = buffer.Slice(start); - Assert.Equal(buffer.Array, span.Array); - Assert.Equal(start, span.Start); Assert.SpanPointsTo(span, buffer, start); Assert.Equal(span.Length, bufferLength - start); } @@ -193,10 +191,8 @@ namespace ImageSharp.Tests.Common { using (Buffer buffer = new Buffer(bufferLength)) { - BufferSpan span = buffer.Slice(start, spanLength); + Span span = buffer.Slice(start, spanLength); - Assert.Equal(buffer.Array, span.Array); - Assert.Equal(start, span.Start); Assert.SpanPointsTo(span, buffer, start); Assert.Equal(span.Length, spanLength); } diff --git a/tests/ImageSharp.Tests/Common/Fast2DArrayTests.cs b/tests/ImageSharp.Tests/Common/Fast2DArrayTests.cs index 7db7a4820..efdcaa848 100644 --- a/tests/ImageSharp.Tests/Common/Fast2DArrayTests.cs +++ b/tests/ImageSharp.Tests/Common/Fast2DArrayTests.cs @@ -7,6 +7,8 @@ namespace ImageSharp.Tests.Common { using System; + using ImageSharp.Memory; + using Xunit; public class Fast2DArrayTests diff --git a/tests/ImageSharp.Tests/Common/PixelDataPoolTests.cs b/tests/ImageSharp.Tests/Common/PixelDataPoolTests.cs index e673b28f1..1291160b2 100644 --- a/tests/ImageSharp.Tests/Common/PixelDataPoolTests.cs +++ b/tests/ImageSharp.Tests/Common/PixelDataPoolTests.cs @@ -8,6 +8,7 @@ namespace ImageSharp.Tests { using System.Linq; + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Xunit; diff --git a/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs b/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs index 254c54ba1..7bacebe42 100644 --- a/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs +++ b/tests/ImageSharp.Tests/Drawing/FillPatternTests.cs @@ -10,6 +10,7 @@ namespace ImageSharp.Tests.Drawing using ImageSharp.Drawing; using ImageSharp.Drawing.Brushes; + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Xunit; diff --git a/tests/ImageSharp.Tests/ImageComparer.cs b/tests/ImageSharp.Tests/ImageComparer.cs index 9b8a51fde..d339dc83d 100644 --- a/tests/ImageSharp.Tests/ImageComparer.cs +++ b/tests/ImageSharp.Tests/ImageComparer.cs @@ -2,6 +2,7 @@ { using System; using ImageSharp; + using ImageSharp.Memory; using ImageSharp.PixelFormats; using Xunit; diff --git a/tests/ImageSharp.Tests/PixelFormats/PixelBlenders/PorterDuffFunctionsTests_TPixel.cs b/tests/ImageSharp.Tests/PixelFormats/PixelBlenders/PorterDuffFunctionsTests_TPixel.cs index 5fa1fccbc..8932f1ffe 100644 --- a/tests/ImageSharp.Tests/PixelFormats/PixelBlenders/PorterDuffFunctionsTests_TPixel.cs +++ b/tests/ImageSharp.Tests/PixelFormats/PixelBlenders/PorterDuffFunctionsTests_TPixel.cs @@ -16,10 +16,10 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public class PorterDuffFunctionsTests_TPixel { - private static BufferSpan AsSpan(T value) + private static Span AsSpan(T value) where T : struct { - return new BufferSpan(new[] { value }); + return new Span(new[] { value }); } public static TheoryData NormalBlendFunctionData = new TheoryData() { @@ -50,7 +50,7 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void NormalBlendFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - BufferSpan dest = new BufferSpan(new TPixel[1]); + Span dest = new Span(new TPixel[1]); new DefaultNormalPixelBlender().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); VectorAssert.Equal(expected, dest[0], 2); } @@ -89,7 +89,7 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void MultiplyFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - BufferSpan dest = new BufferSpan(new TPixel[1]); + Span dest = new Span(new TPixel[1]); new DefaultMultiplyPixelBlender().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); VectorAssert.Equal(expected, dest[0], 2); } @@ -128,7 +128,7 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void AddFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - BufferSpan dest = new BufferSpan(new TPixel[1]); + Span dest = new Span(new TPixel[1]); new DefaultAddPixelBlender().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); VectorAssert.Equal(expected, dest[0], 2); } @@ -167,7 +167,7 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void SubstractFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - BufferSpan dest = new BufferSpan(new TPixel[1]); + Span dest = new Span(new TPixel[1]); new DefaultSubstractPixelBlender().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); VectorAssert.Equal(expected, dest[0], 2); } @@ -206,7 +206,7 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void ScreenFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - BufferSpan dest = new BufferSpan(new TPixel[1]); + Span dest = new Span(new TPixel[1]); new DefaultScreenPixelBlender().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); VectorAssert.Equal(expected, dest[0], 2); } @@ -245,7 +245,7 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void DarkenFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - BufferSpan dest = new BufferSpan(new TPixel[1]); + Span dest = new Span(new TPixel[1]); new DefaultDarkenPixelBlender().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); VectorAssert.Equal(expected, dest[0], 2); } @@ -284,7 +284,7 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void LightenFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - BufferSpan dest = new BufferSpan(new TPixel[1]); + Span dest = new Span(new TPixel[1]); new DefaultLightenPixelBlender().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); VectorAssert.Equal(expected, dest[0], 2); } @@ -323,7 +323,7 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void OverlayFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - BufferSpan dest = new BufferSpan(new TPixel[1]); + Span dest = new Span(new TPixel[1]); new DefaultOverlayPixelBlender().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); VectorAssert.Equal(expected, dest[0], 2); } @@ -362,7 +362,7 @@ namespace ImageSharp.Tests.PixelFormats.PixelBlenders public void HardLightFunction_Blender_Bulk(TestPixel back, TestPixel source, float amount, TestPixel expected) where TPixel : struct, IPixel { - BufferSpan dest = new BufferSpan(new TPixel[1]); + Span dest = new Span(new TPixel[1]); new DefaultHardLightPixelBlender().Blend(dest, back.AsSpan(), source.AsSpan(), AsSpan(amount)); VectorAssert.Equal(expected, dest[0], 2); } diff --git a/tests/ImageSharp.Tests/TestUtilities/TestPixel.cs b/tests/ImageSharp.Tests/TestUtilities/TestPixel.cs index 7e3d318c0..852bc587d 100644 --- a/tests/ImageSharp.Tests/TestUtilities/TestPixel.cs +++ b/tests/ImageSharp.Tests/TestUtilities/TestPixel.cs @@ -38,9 +38,9 @@ namespace ImageSharp.Tests.TestUtilities return pix; } - internal BufferSpan AsSpan() + internal Span AsSpan() { - return new BufferSpan(new[] { AsPixel() }); + return new Span(new[] { AsPixel() }); } public void Deserialize(IXunitSerializationInfo info) From 649aab1ded98795d82effa6ab27d16256ac6cd92 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 13 May 2017 02:52:01 +0200 Subject: [PATCH 31/34] Span on IPixel API surface. --- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 4 +- src/ImageSharp/Image/PixelAccessor{TPixel}.cs | 24 ++++------- src/ImageSharp/PixelFormats/Alpha8.cs | 8 ++-- src/ImageSharp/PixelFormats/Argb32.cs | 9 ++-- src/ImageSharp/PixelFormats/Bgr565.cs | 8 ++-- src/ImageSharp/PixelFormats/Bgra4444.cs | 8 ++-- src/ImageSharp/PixelFormats/Bgra5551.cs | 8 ++-- src/ImageSharp/PixelFormats/Byte4.cs | 8 ++-- src/ImageSharp/PixelFormats/HalfSingle.cs | 9 ++-- src/ImageSharp/PixelFormats/HalfVector2.cs | 9 ++-- src/ImageSharp/PixelFormats/HalfVector4.cs | 9 ++-- src/ImageSharp/PixelFormats/IPixel.cs | 8 ++-- .../PixelFormats/NormalizedByte2.cs | 8 ++-- .../PixelFormats/NormalizedByte4.cs | 8 ++-- .../PixelFormats/NormalizedShort2.cs | 9 ++-- .../PixelFormats/NormalizedShort4.cs | 9 ++-- .../PixelFormats/PixelOperations{TPixel}.cs | 42 +++++++------------ src/ImageSharp/PixelFormats/Rg32.cs | 8 ++-- src/ImageSharp/PixelFormats/Rgba1010102.cs | 8 ++-- .../PixelFormats/Rgba32.PixelOperations.cs | 16 +++---- src/ImageSharp/PixelFormats/Rgba32.cs | 9 ++-- src/ImageSharp/PixelFormats/Rgba64.cs | 8 ++-- src/ImageSharp/PixelFormats/RgbaVector.cs | 9 ++-- src/ImageSharp/PixelFormats/Short2.cs | 8 ++-- src/ImageSharp/PixelFormats/Short4.cs | 8 ++-- .../ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs | 4 +- .../Color/Bulk/ToXyzw.cs | 4 +- .../Colors/PixelOperationsTests.cs | 8 ++-- 28 files changed, 134 insertions(+), 144 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 49c18db34..0482b2691 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -346,11 +346,11 @@ namespace ImageSharp.Formats if (this.bytesPerPixel == 4) { - PixelOperations.Instance.ToXyzwBytes(rowSpan, this.rawScanline, 0, this.width); + PixelOperations.Instance.ToXyzwBytes(rowSpan, this.rawScanline, this.width); } else { - PixelOperations.Instance.ToXyzBytes(rowSpan, this.rawScanline, 0, this.width); + PixelOperations.Instance.ToXyzBytes(rowSpan, this.rawScanline, this.width); } } diff --git a/src/ImageSharp/Image/PixelAccessor{TPixel}.cs b/src/ImageSharp/Image/PixelAccessor{TPixel}.cs index f4c7c7642..a54c03b63 100644 --- a/src/ImageSharp/Image/PixelAccessor{TPixel}.cs +++ b/src/ImageSharp/Image/PixelAccessor{TPixel}.cs @@ -348,10 +348,8 @@ namespace ImageSharp for (int y = 0; y < height; y++) { Span source = this.GetRowSpan(sourceX, sourceY + y); - using (Buffer destination = new Buffer(area.Bytes)) - { - Operations.ToZyxBytes(source, destination, y * area.RowStride, width); - } + Span destination = area.GetRowSpan(y); + Operations.ToZyxBytes(source, destination, width); } } @@ -369,10 +367,8 @@ namespace ImageSharp for (int y = 0; y < height; y++) { Span source = this.GetRowSpan(sourceX, sourceY + y); - using (Buffer destination = new Buffer(area.Bytes)) - { - Operations.ToZyxwBytes(source, destination, y * area.RowStride, width); - } + Span destination = area.GetRowSpan(y); + Operations.ToZyxwBytes(source, destination, width); } } @@ -390,10 +386,8 @@ namespace ImageSharp for (int y = 0; y < height; y++) { Span source = this.GetRowSpan(sourceX, sourceY + y); - using (Buffer destination = new Buffer(area.Bytes)) - { - Operations.ToXyzBytes(source, destination, y * area.RowStride, width); - } + Span destination = area.GetRowSpan(y); + Operations.ToXyzBytes(source, destination, width); } } @@ -411,10 +405,8 @@ namespace ImageSharp for (int y = 0; y < height; y++) { Span source = this.GetRowSpan(sourceX, sourceY + y); - using (Buffer destination = new Buffer(area.Bytes)) - { - Operations.ToXyzwBytes(source, destination, y * area.RowStride, width); - } + Span destination = area.GetRowSpan(y); + Operations.ToXyzwBytes(source, destination, width); } } diff --git a/src/ImageSharp/PixelFormats/Alpha8.cs b/src/ImageSharp/PixelFormats/Alpha8.cs index ac2627701..c184ed9cf 100644 --- a/src/ImageSharp/PixelFormats/Alpha8.cs +++ b/src/ImageSharp/PixelFormats/Alpha8.cs @@ -87,7 +87,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { bytes[startIndex] = 0; bytes[startIndex + 1] = 0; @@ -96,7 +96,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { bytes[startIndex] = 0; bytes[startIndex + 1] = 0; @@ -106,7 +106,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { bytes[startIndex] = 0; bytes[startIndex + 1] = 0; @@ -115,7 +115,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { bytes[startIndex] = 0; bytes[startIndex + 1] = 0; diff --git a/src/ImageSharp/PixelFormats/Argb32.cs b/src/ImageSharp/PixelFormats/Argb32.cs index 61e860aee..bd47f72f9 100644 --- a/src/ImageSharp/PixelFormats/Argb32.cs +++ b/src/ImageSharp/PixelFormats/Argb32.cs @@ -5,6 +5,7 @@ namespace ImageSharp.PixelFormats { + using System; using System.Numerics; using System.Runtime.CompilerServices; @@ -241,7 +242,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { bytes[startIndex] = this.R; bytes[startIndex + 1] = this.G; @@ -250,7 +251,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { bytes[startIndex] = this.R; bytes[startIndex + 1] = this.G; @@ -260,7 +261,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { bytes[startIndex] = this.B; bytes[startIndex + 1] = this.G; @@ -269,7 +270,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { bytes[startIndex] = this.B; bytes[startIndex + 1] = this.G; diff --git a/src/ImageSharp/PixelFormats/Bgr565.cs b/src/ImageSharp/PixelFormats/Bgr565.cs index 813b6fe85..92bbac14c 100644 --- a/src/ImageSharp/PixelFormats/Bgr565.cs +++ b/src/ImageSharp/PixelFormats/Bgr565.cs @@ -110,7 +110,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; bytes[startIndex] = (byte)MathF.Round(vector.X); @@ -120,7 +120,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; bytes[startIndex] = (byte)MathF.Round(vector.X); @@ -131,7 +131,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; bytes[startIndex] = (byte)MathF.Round(vector.Z); @@ -141,7 +141,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; bytes[startIndex] = (byte)MathF.Round(vector.Z); diff --git a/src/ImageSharp/PixelFormats/Bgra4444.cs b/src/ImageSharp/PixelFormats/Bgra4444.cs index 8fb2d0c26..0bac00dfe 100644 --- a/src/ImageSharp/PixelFormats/Bgra4444.cs +++ b/src/ImageSharp/PixelFormats/Bgra4444.cs @@ -101,7 +101,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; bytes[startIndex] = (byte)vector.X; @@ -111,7 +111,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; bytes[startIndex] = (byte)vector.X; @@ -122,7 +122,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; bytes[startIndex] = (byte)vector.Z; @@ -132,7 +132,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; bytes[startIndex] = (byte)vector.Z; diff --git a/src/ImageSharp/PixelFormats/Bgra5551.cs b/src/ImageSharp/PixelFormats/Bgra5551.cs index 26cfa6b8c..f151db644 100644 --- a/src/ImageSharp/PixelFormats/Bgra5551.cs +++ b/src/ImageSharp/PixelFormats/Bgra5551.cs @@ -101,7 +101,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; bytes[startIndex] = (byte)vector.X; @@ -111,7 +111,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; bytes[startIndex] = (byte)vector.X; @@ -122,7 +122,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; bytes[startIndex] = (byte)vector.Z; @@ -132,7 +132,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; bytes[startIndex] = (byte)vector.Z; diff --git a/src/ImageSharp/PixelFormats/Byte4.cs b/src/ImageSharp/PixelFormats/Byte4.cs index 951b7c3cb..264bc7497 100644 --- a/src/ImageSharp/PixelFormats/Byte4.cs +++ b/src/ImageSharp/PixelFormats/Byte4.cs @@ -102,7 +102,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); bytes[startIndex] = (byte)vector.X; @@ -112,7 +112,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); bytes[startIndex] = (byte)vector.X; @@ -123,7 +123,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); bytes[startIndex] = (byte)vector.Z; @@ -133,7 +133,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); bytes[startIndex] = (byte)vector.Z; diff --git a/src/ImageSharp/PixelFormats/HalfSingle.cs b/src/ImageSharp/PixelFormats/HalfSingle.cs index e9f02d34e..4cc9acc22 100644 --- a/src/ImageSharp/PixelFormats/HalfSingle.cs +++ b/src/ImageSharp/PixelFormats/HalfSingle.cs @@ -5,6 +5,7 @@ namespace ImageSharp.PixelFormats { + using System; using System.Numerics; using System.Runtime.CompilerServices; @@ -110,7 +111,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= MaxBytes; @@ -124,7 +125,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= MaxBytes; @@ -139,7 +140,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= MaxBytes; @@ -153,7 +154,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= MaxBytes; diff --git a/src/ImageSharp/PixelFormats/HalfVector2.cs b/src/ImageSharp/PixelFormats/HalfVector2.cs index 8813fd455..f490f7169 100644 --- a/src/ImageSharp/PixelFormats/HalfVector2.cs +++ b/src/ImageSharp/PixelFormats/HalfVector2.cs @@ -5,6 +5,7 @@ namespace ImageSharp.PixelFormats { + using System; using System.Numerics; using System.Runtime.CompilerServices; @@ -124,7 +125,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= MaxBytes; @@ -138,7 +139,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= MaxBytes; @@ -153,7 +154,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= MaxBytes; @@ -167,7 +168,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= MaxBytes; diff --git a/src/ImageSharp/PixelFormats/HalfVector4.cs b/src/ImageSharp/PixelFormats/HalfVector4.cs index e8c78047e..7c496c161 100644 --- a/src/ImageSharp/PixelFormats/HalfVector4.cs +++ b/src/ImageSharp/PixelFormats/HalfVector4.cs @@ -5,6 +5,7 @@ namespace ImageSharp.PixelFormats { + using System; using System.Numerics; using System.Runtime.CompilerServices; @@ -117,7 +118,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= MaxBytes; @@ -131,7 +132,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= MaxBytes; @@ -146,7 +147,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= MaxBytes; @@ -160,7 +161,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= MaxBytes; diff --git a/src/ImageSharp/PixelFormats/IPixel.cs b/src/ImageSharp/PixelFormats/IPixel.cs index 9a8d9730a..030cb93f4 100644 --- a/src/ImageSharp/PixelFormats/IPixel.cs +++ b/src/ImageSharp/PixelFormats/IPixel.cs @@ -56,7 +56,7 @@ namespace ImageSharp.PixelFormats /// /// The bytes to set the color in. /// The starting index of the . - void ToXyzBytes(byte[] bytes, int startIndex); + void ToXyzBytes(Span bytes, int startIndex); /// /// Expands the packed representation into a given byte array. @@ -64,7 +64,7 @@ namespace ImageSharp.PixelFormats /// /// The bytes to set the color in. /// The starting index of the . - void ToXyzwBytes(byte[] bytes, int startIndex); + void ToXyzwBytes(Span bytes, int startIndex); /// /// Expands the packed representation into a given byte array. @@ -72,7 +72,7 @@ namespace ImageSharp.PixelFormats /// /// The bytes to set the color in. /// The starting index of the . - void ToZyxBytes(byte[] bytes, int startIndex); + void ToZyxBytes(Span bytes, int startIndex); /// /// Expands the packed representation into a given byte array. @@ -80,6 +80,6 @@ namespace ImageSharp.PixelFormats /// /// The bytes to set the color in. /// The starting index of the . - void ToZyxwBytes(byte[] bytes, int startIndex); + void ToZyxwBytes(Span bytes, int startIndex); } } \ No newline at end of file diff --git a/src/ImageSharp/PixelFormats/NormalizedByte2.cs b/src/ImageSharp/PixelFormats/NormalizedByte2.cs index 93226342e..47a4f3005 100644 --- a/src/ImageSharp/PixelFormats/NormalizedByte2.cs +++ b/src/ImageSharp/PixelFormats/NormalizedByte2.cs @@ -134,7 +134,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= Half; @@ -150,7 +150,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= Half; @@ -167,7 +167,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= Half; @@ -183,7 +183,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= Half; diff --git a/src/ImageSharp/PixelFormats/NormalizedByte4.cs b/src/ImageSharp/PixelFormats/NormalizedByte4.cs index 66a79fefc..4559bd082 100644 --- a/src/ImageSharp/PixelFormats/NormalizedByte4.cs +++ b/src/ImageSharp/PixelFormats/NormalizedByte4.cs @@ -127,7 +127,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= Half; @@ -143,7 +143,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= Half; @@ -160,7 +160,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= Half; @@ -176,7 +176,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= Half; diff --git a/src/ImageSharp/PixelFormats/NormalizedShort2.cs b/src/ImageSharp/PixelFormats/NormalizedShort2.cs index 99e5a98c0..648b68905 100644 --- a/src/ImageSharp/PixelFormats/NormalizedShort2.cs +++ b/src/ImageSharp/PixelFormats/NormalizedShort2.cs @@ -5,6 +5,7 @@ namespace ImageSharp.PixelFormats { + using System; using System.Numerics; using System.Runtime.CompilerServices; @@ -120,7 +121,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= Half; @@ -136,7 +137,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= Half; @@ -153,7 +154,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= Half; @@ -169,7 +170,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= Half; diff --git a/src/ImageSharp/PixelFormats/NormalizedShort4.cs b/src/ImageSharp/PixelFormats/NormalizedShort4.cs index 932ab97cf..7b520aace 100644 --- a/src/ImageSharp/PixelFormats/NormalizedShort4.cs +++ b/src/ImageSharp/PixelFormats/NormalizedShort4.cs @@ -5,6 +5,7 @@ namespace ImageSharp.PixelFormats { + using System; using System.Numerics; using System.Runtime.CompilerServices; @@ -128,7 +129,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= Half; @@ -144,7 +145,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= Half; @@ -161,7 +162,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= Half; @@ -177,7 +178,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector *= Half; diff --git a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs index fc1817a89..b92b86b41 100644 --- a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs @@ -9,8 +9,6 @@ namespace ImageSharp.PixelFormats using System.Numerics; using System.Runtime.CompilerServices; - using ImageSharp.Memory; - /// /// A stateless class implementing Strategy Pattern for batched pixel-data conversion operations /// for pixel buffers of type . @@ -86,21 +84,19 @@ namespace ImageSharp.PixelFormats } /// - /// Bulk version of . + /// Bulk version of . /// /// The to the source colors. - /// The to the destination bytes. - /// The starting index of the . + /// The to the destination bytes. /// The number of pixels to convert. - internal virtual void ToXyzBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) + internal virtual void ToXyzBytes(Span sourceColors, Span destBytes, int count) { ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); - byte[] dest = destBytes.Array; for (int i = 0; i < count; i++) { ref TPixel sp = ref Unsafe.Add(ref sourceRef, i); - sp.ToXyzBytes(dest, startIndex + (i * 3)); + sp.ToXyzBytes(destBytes, i * 3); } } @@ -128,21 +124,19 @@ namespace ImageSharp.PixelFormats } /// - /// Bulk version of . + /// Bulk version of /// /// The to the source colors. - /// The to the destination bytes. - /// The starting index of the . + /// The to the destination bytes. /// The number of pixels to convert. - internal virtual void ToXyzwBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) + internal virtual void ToXyzwBytes(Span sourceColors, Span destBytes, int count) { ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); - byte[] dest = destBytes.Array; for (int i = 0; i < count; i++) { ref TPixel sp = ref Unsafe.Add(ref sourceRef, i); - sp.ToXyzwBytes(dest, startIndex + (i * 4)); + sp.ToXyzwBytes(destBytes, i * 4); } } @@ -170,21 +164,19 @@ namespace ImageSharp.PixelFormats } /// - /// Bulk version of . + /// Bulk version of . /// /// The to the source colors. - /// The to the destination bytes. - /// The starting index of the . + /// The to the destination bytes. /// The number of pixels to convert. - internal virtual void ToZyxBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) + internal virtual void ToZyxBytes(Span sourceColors, Span destBytes, int count) { ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); - byte[] dest = destBytes.Array; for (int i = 0; i < count; i++) { ref TPixel sp = ref Unsafe.Add(ref sourceRef, i); - sp.ToZyxBytes(dest, startIndex + (i * 3)); + sp.ToZyxBytes(destBytes, i * 3); } } @@ -212,21 +204,19 @@ namespace ImageSharp.PixelFormats } /// - /// Bulk version of . + /// Bulk version of . /// /// The to the source colors. - /// The to the destination bytes. - /// The starting index of the . + /// The to the destination bytes. /// The number of pixels to convert. - internal virtual void ToZyxwBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) + internal virtual void ToZyxwBytes(Span sourceColors, Span destBytes, int count) { ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); - byte[] dest = destBytes.Array; for (int i = 0; i < count; i++) { ref TPixel sp = ref Unsafe.Add(ref sourceRef, i); - sp.ToZyxwBytes(dest, startIndex + (i * 4)); + sp.ToZyxwBytes(destBytes, i * 4); } } } diff --git a/src/ImageSharp/PixelFormats/Rg32.cs b/src/ImageSharp/PixelFormats/Rg32.cs index 3e23777f6..ea7d8729b 100644 --- a/src/ImageSharp/PixelFormats/Rg32.cs +++ b/src/ImageSharp/PixelFormats/Rg32.cs @@ -114,7 +114,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; @@ -125,7 +125,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; @@ -137,7 +137,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; @@ -148,7 +148,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; diff --git a/src/ImageSharp/PixelFormats/Rgba1010102.cs b/src/ImageSharp/PixelFormats/Rgba1010102.cs index 747112fd8..ca7b74fbb 100644 --- a/src/ImageSharp/PixelFormats/Rgba1010102.cs +++ b/src/ImageSharp/PixelFormats/Rgba1010102.cs @@ -108,7 +108,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; @@ -119,7 +119,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; @@ -131,7 +131,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; @@ -142,7 +142,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; diff --git a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs index c9dca89a3..168787ba7 100644 --- a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs +++ b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs @@ -134,10 +134,10 @@ namespace ImageSharp } /// - internal override void ToXyzBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) + internal override void ToXyzBytes(Span sourceColors, Span destBytes, int count) { ref Rgba32 sourceRef = ref sourceColors.DangerousGetPinnableReference(); - ref RGB24 destRef = ref Unsafe.As(ref new Span(destBytes.Array, startIndex).DangerousGetPinnableReference()); + ref RGB24 destRef = ref Unsafe.As(ref destBytes.DangerousGetPinnableReference()); for (int i = 0; i < count; i++) { @@ -155,9 +155,9 @@ namespace ImageSharp } /// - internal override unsafe void ToXyzwBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) + internal override unsafe void ToXyzwBytes(Span sourceColors, Span destBytes, int count) { - SpanHelper.Copy(sourceColors.AsBytes(), new Span(destBytes.Array, startIndex), count * sizeof(Rgba32)); + SpanHelper.Copy(sourceColors.AsBytes(), destBytes, count * sizeof(Rgba32)); } /// @@ -177,10 +177,10 @@ namespace ImageSharp } /// - internal override void ToZyxBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) + internal override void ToZyxBytes(Span sourceColors, Span destBytes, int count) { ref Rgba32 sourceRef = ref sourceColors.DangerousGetPinnableReference(); - ref RGB24 destRef = ref Unsafe.As(ref new Span(destBytes.Array, startIndex).DangerousGetPinnableReference()); + ref RGB24 destRef = ref Unsafe.As(ref destBytes.DangerousGetPinnableReference()); for (int i = 0; i < count; i++) { @@ -207,10 +207,10 @@ namespace ImageSharp } /// - internal override void ToZyxwBytes(Span sourceColors, Buffer destBytes, int startIndex, int count) + internal override void ToZyxwBytes(Span sourceColors, Span destBytes, int count) { ref Rgba32 sourceRef = ref sourceColors.DangerousGetPinnableReference(); - ref RGBA32 destRef = ref Unsafe.As(ref new Span(destBytes.Array, startIndex).DangerousGetPinnableReference()); + ref RGBA32 destRef = ref Unsafe.As(ref destBytes.DangerousGetPinnableReference()); for (int i = 0; i < count; i++) { diff --git a/src/ImageSharp/PixelFormats/Rgba32.cs b/src/ImageSharp/PixelFormats/Rgba32.cs index 15a9ed0fd..9b82a3701 100644 --- a/src/ImageSharp/PixelFormats/Rgba32.cs +++ b/src/ImageSharp/PixelFormats/Rgba32.cs @@ -5,6 +5,7 @@ namespace ImageSharp { + using System; using System.Numerics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -230,7 +231,7 @@ namespace ImageSharp /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { bytes[startIndex] = this.R; bytes[startIndex + 1] = this.G; @@ -239,7 +240,7 @@ namespace ImageSharp /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { bytes[startIndex] = this.R; bytes[startIndex + 1] = this.G; @@ -249,7 +250,7 @@ namespace ImageSharp /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { bytes[startIndex] = this.B; bytes[startIndex + 1] = this.G; @@ -258,7 +259,7 @@ namespace ImageSharp /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { bytes[startIndex] = this.B; bytes[startIndex + 1] = this.G; diff --git a/src/ImageSharp/PixelFormats/Rgba64.cs b/src/ImageSharp/PixelFormats/Rgba64.cs index 296c17a4e..417828368 100644 --- a/src/ImageSharp/PixelFormats/Rgba64.cs +++ b/src/ImageSharp/PixelFormats/Rgba64.cs @@ -107,7 +107,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; @@ -118,7 +118,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; @@ -130,7 +130,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; @@ -141,7 +141,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4() * 255F; diff --git a/src/ImageSharp/PixelFormats/RgbaVector.cs b/src/ImageSharp/PixelFormats/RgbaVector.cs index a92b794ef..5332f4a8e 100644 --- a/src/ImageSharp/PixelFormats/RgbaVector.cs +++ b/src/ImageSharp/PixelFormats/RgbaVector.cs @@ -5,6 +5,7 @@ namespace ImageSharp.PixelFormats { + using System; using System.Numerics; using System.Runtime.CompilerServices; @@ -234,7 +235,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { Vector4 vector = Vector4.Clamp(this.backingVector, Vector4.Zero, Vector4.One) * MaxBytes; vector += Half; @@ -245,7 +246,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { Vector4 vector = Vector4.Clamp(this.backingVector, Vector4.Zero, Vector4.One) * MaxBytes; vector += Half; @@ -257,7 +258,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { Vector4 vector = Vector4.Clamp(this.backingVector, Vector4.Zero, Vector4.One) * MaxBytes; vector += Half; @@ -268,7 +269,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { Vector4 vector = Vector4.Clamp(this.backingVector, Vector4.Zero, Vector4.One) * MaxBytes; vector += Half; diff --git a/src/ImageSharp/PixelFormats/Short2.cs b/src/ImageSharp/PixelFormats/Short2.cs index 6c871ecb6..b848b5505 100644 --- a/src/ImageSharp/PixelFormats/Short2.cs +++ b/src/ImageSharp/PixelFormats/Short2.cs @@ -119,7 +119,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { Vector2 vector = this.ToVector2(); vector /= 65534; @@ -135,7 +135,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { Vector2 vector = this.ToVector2(); vector /= 65534; @@ -152,7 +152,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { Vector2 vector = this.ToVector2(); vector /= 65534; @@ -168,7 +168,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { Vector2 vector = this.ToVector2(); vector /= 65534; diff --git a/src/ImageSharp/PixelFormats/Short4.cs b/src/ImageSharp/PixelFormats/Short4.cs index de110c7d3..763de19bc 100644 --- a/src/ImageSharp/PixelFormats/Short4.cs +++ b/src/ImageSharp/PixelFormats/Short4.cs @@ -125,7 +125,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzBytes(byte[] bytes, int startIndex) + public void ToXyzBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector /= 65534; @@ -141,7 +141,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToXyzwBytes(byte[] bytes, int startIndex) + public void ToXyzwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector /= 65534; @@ -158,7 +158,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxBytes(byte[] bytes, int startIndex) + public void ToZyxBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector /= 65534; @@ -174,7 +174,7 @@ namespace ImageSharp.PixelFormats /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - public void ToZyxwBytes(byte[] bytes, int startIndex) + public void ToZyxwBytes(Span bytes, int startIndex) { Vector4 vector = this.ToVector4(); vector /= 65534; diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs index af76434a5..3c75fc2d1 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyz.cs @@ -46,13 +46,13 @@ namespace ImageSharp.Benchmarks.Color.Bulk [Benchmark] public void CommonBulk() { - new PixelOperations().ToXyzBytes(this.source, this.destination, 0, this.Count); + new PixelOperations().ToXyzBytes(this.source, this.destination, this.Count); } [Benchmark] public void OptimizedBulk() { - PixelOperations.Instance.ToXyzBytes(this.source, this.destination, 0, this.Count); + PixelOperations.Instance.ToXyzBytes(this.source, this.destination, this.Count); } } diff --git a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs index 01e7ef371..f64bf561b 100644 --- a/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs +++ b/tests/ImageSharp.Benchmarks/Color/Bulk/ToXyzw.cs @@ -51,13 +51,13 @@ namespace ImageSharp.Benchmarks.Color.Bulk [Benchmark] public void CommonBulk() { - new PixelOperations().ToXyzwBytes(this.source, this.destination, 0, this.Count); + new PixelOperations().ToXyzwBytes(this.source, this.destination, this.Count); } [Benchmark] public void OptimizedBulk() { - PixelOperations.Instance.ToXyzwBytes(this.source, this.destination, 0, this.Count); + PixelOperations.Instance.ToXyzwBytes(this.source, this.destination, this.Count); } } diff --git a/tests/ImageSharp.Tests/Colors/PixelOperationsTests.cs b/tests/ImageSharp.Tests/Colors/PixelOperationsTests.cs index e6d23dfc5..c91218ccc 100644 --- a/tests/ImageSharp.Tests/Colors/PixelOperationsTests.cs +++ b/tests/ImageSharp.Tests/Colors/PixelOperationsTests.cs @@ -181,7 +181,7 @@ namespace ImageSharp.Tests.Colors TestOperation( source, expected, - (s, d) => Operations.ToXyzBytes(s, d, 0, count) + (s, d) => Operations.ToXyzBytes(s, d, count) ); } @@ -222,7 +222,7 @@ namespace ImageSharp.Tests.Colors TestOperation( source, expected, - (s, d) => Operations.ToXyzwBytes(s, d, 0, count) + (s, d) => Operations.ToXyzwBytes(s, d, count) ); } @@ -263,7 +263,7 @@ namespace ImageSharp.Tests.Colors TestOperation( source, expected, - (s, d) => Operations.ToZyxBytes(s, d, 0, count) + (s, d) => Operations.ToZyxBytes(s, d, count) ); } @@ -304,7 +304,7 @@ namespace ImageSharp.Tests.Colors TestOperation( source, expected, - (s, d) => Operations.ToZyxwBytes(s, d, 0, count) + (s, d) => Operations.ToZyxwBytes(s, d, count) ); } From 5de5b9124259b5a34702c7d3092546b0223e2232 Mon Sep 17 00:00:00 2001 From: Anton Firszov Date: Sat, 13 May 2017 03:44:50 +0200 Subject: [PATCH 32/34] added guards to unsafe PixelOperations methods --- src/ImageSharp/Common/Helpers/DebugGuard.cs | 2 ++ src/ImageSharp/Common/Helpers/Guard.cs | 19 ++++++++++++ .../PixelFormats/PixelOperations{TPixel}.cs | 30 +++++++++++++++++++ .../PixelFormats/Rgba32.PixelOperations.cs | 27 +++++++++++++++++ .../RgbaVector.PixelOperations.cs | 3 ++ 5 files changed, 81 insertions(+) diff --git a/src/ImageSharp/Common/Helpers/DebugGuard.cs b/src/ImageSharp/Common/Helpers/DebugGuard.cs index 096f96f04..f6941fc6f 100644 --- a/src/ImageSharp/Common/Helpers/DebugGuard.cs +++ b/src/ImageSharp/Common/Helpers/DebugGuard.cs @@ -170,6 +170,7 @@ namespace ImageSharp /// /// is true /// + [Conditional("DEBUG")] public static void MustBeSameSized(Span target, Span other, string parameterName) where T : struct { @@ -189,6 +190,7 @@ namespace ImageSharp /// /// is true /// + [Conditional("DEBUG")] public static void MustBeSizedAtLeast(Span target, Span minSpan, string parameterName) where T : struct { diff --git a/src/ImageSharp/Common/Helpers/Guard.cs b/src/ImageSharp/Common/Helpers/Guard.cs index cf307e936..a4b392fcf 100644 --- a/src/ImageSharp/Common/Helpers/Guard.cs +++ b/src/ImageSharp/Common/Helpers/Guard.cs @@ -230,5 +230,24 @@ namespace ImageSharp throw new ArgumentException(message, parameterName); } } + + /// + /// Verifies, that the `target` span has the length of 'minSpan', or longer. + /// + /// The element type of the spans + /// The target span. + /// The minimum length. + /// The name of the parameter that is to be checked. + /// + /// is true + /// + public static void MustBeSizedAtLeast(Span target, int minLength, string parameterName) + where T : struct + { + if (target.Length < minLength) + { + throw new ArgumentException($"Span-s must be at least of length {minLength}!", parameterName); + } + } } } diff --git a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs b/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs index b92b86b41..993a11232 100644 --- a/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs +++ b/src/ImageSharp/PixelFormats/PixelOperations{TPixel}.cs @@ -30,6 +30,9 @@ namespace ImageSharp.PixelFormats /// The number of pixels to convert. internal virtual void PackFromVector4(Span sourceVectors, Span destColors, int count) { + Guard.MustBeSizedAtLeast(sourceVectors, count, nameof(sourceVectors)); + Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors)); + ref Vector4 sourceRef = ref sourceVectors.DangerousGetPinnableReference(); ref TPixel destRef = ref destColors.DangerousGetPinnableReference(); @@ -49,6 +52,9 @@ namespace ImageSharp.PixelFormats /// The number of pixels to convert. internal virtual void ToVector4(Span sourceColors, Span destVectors, int count) { + Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors)); + Guard.MustBeSizedAtLeast(destVectors, count, nameof(destVectors)); + ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); ref Vector4 destRef = ref destVectors.DangerousGetPinnableReference(); @@ -68,6 +74,9 @@ namespace ImageSharp.PixelFormats /// The number of pixels to convert. internal virtual void PackFromXyzBytes(Span sourceBytes, Span destColors, int count) { + Guard.MustBeSizedAtLeast(sourceBytes, count * 3, nameof(sourceBytes)); + Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors)); + ref byte sourceRef = ref sourceBytes.DangerousGetPinnableReference(); ref TPixel destRef = ref destColors.DangerousGetPinnableReference(); @@ -91,6 +100,9 @@ namespace ImageSharp.PixelFormats /// The number of pixels to convert. internal virtual void ToXyzBytes(Span sourceColors, Span destBytes, int count) { + Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors)); + Guard.MustBeSizedAtLeast(destBytes, count * 3, nameof(destBytes)); + ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); for (int i = 0; i < count; i++) @@ -108,6 +120,9 @@ namespace ImageSharp.PixelFormats /// The number of pixels to convert. internal virtual void PackFromXyzwBytes(Span sourceBytes, Span destColors, int count) { + Guard.MustBeSizedAtLeast(sourceBytes, count * 4, nameof(sourceBytes)); + Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors)); + ref byte sourceRef = ref sourceBytes.DangerousGetPinnableReference(); ref TPixel destRef = ref destColors.DangerousGetPinnableReference(); @@ -131,6 +146,9 @@ namespace ImageSharp.PixelFormats /// The number of pixels to convert. internal virtual void ToXyzwBytes(Span sourceColors, Span destBytes, int count) { + Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors)); + Guard.MustBeSizedAtLeast(destBytes, count * 4, nameof(destBytes)); + ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); for (int i = 0; i < count; i++) @@ -148,6 +166,9 @@ namespace ImageSharp.PixelFormats /// The number of pixels to convert. internal virtual void PackFromZyxBytes(Span sourceBytes, Span destColors, int count) { + Guard.MustBeSizedAtLeast(sourceBytes, count * 3, nameof(sourceBytes)); + Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors)); + ref byte sourceRef = ref sourceBytes.DangerousGetPinnableReference(); ref TPixel destRef = ref destColors.DangerousGetPinnableReference(); @@ -171,6 +192,9 @@ namespace ImageSharp.PixelFormats /// The number of pixels to convert. internal virtual void ToZyxBytes(Span sourceColors, Span destBytes, int count) { + Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors)); + Guard.MustBeSizedAtLeast(destBytes, count * 3, nameof(destBytes)); + ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); for (int i = 0; i < count; i++) @@ -188,6 +212,9 @@ namespace ImageSharp.PixelFormats /// The number of pixels to convert. internal virtual void PackFromZyxwBytes(Span sourceBytes, Span destColors, int count) { + Guard.MustBeSizedAtLeast(sourceBytes, count * 4, nameof(sourceBytes)); + Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors)); + ref byte sourceRef = ref sourceBytes.DangerousGetPinnableReference(); ref TPixel destRef = ref destColors.DangerousGetPinnableReference(); @@ -211,6 +238,9 @@ namespace ImageSharp.PixelFormats /// The number of pixels to convert. internal virtual void ToZyxwBytes(Span sourceColors, Span destBytes, int count) { + Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors)); + Guard.MustBeSizedAtLeast(destBytes, count * 4, nameof(destBytes)); + ref TPixel sourceRef = ref sourceColors.DangerousGetPinnableReference(); for (int i = 0; i < count; i++) diff --git a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs index 168787ba7..2ba663603 100644 --- a/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs +++ b/src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs @@ -93,6 +93,9 @@ namespace ImageSharp /// internal override void ToVector4(Span sourceColors, Span destVectors, int count) { + Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors)); + Guard.MustBeSizedAtLeast(destVectors, count, nameof(destVectors)); + if (count < 256 || !Vector.IsHardwareAccelerated) { // Doesn't worth to bother with SIMD: @@ -120,6 +123,9 @@ namespace ImageSharp /// internal override void PackFromXyzBytes(Span sourceBytes, Span destColors, int count) { + Guard.MustBeSizedAtLeast(sourceBytes, count * 3, nameof(sourceBytes)); + Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors)); + ref RGB24 sourceRef = ref Unsafe.As(ref sourceBytes.DangerousGetPinnableReference()); ref Rgba32 destRef = ref destColors.DangerousGetPinnableReference(); @@ -136,6 +142,9 @@ namespace ImageSharp /// internal override void ToXyzBytes(Span sourceColors, Span destBytes, int count) { + Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors)); + Guard.MustBeSizedAtLeast(destBytes, count * 3, nameof(destBytes)); + ref Rgba32 sourceRef = ref sourceColors.DangerousGetPinnableReference(); ref RGB24 destRef = ref Unsafe.As(ref destBytes.DangerousGetPinnableReference()); @@ -151,18 +160,27 @@ namespace ImageSharp /// internal override unsafe void PackFromXyzwBytes(Span sourceBytes, Span destColors, int count) { + Guard.MustBeSizedAtLeast(sourceBytes, count * 4, nameof(sourceBytes)); + Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors)); + SpanHelper.Copy(sourceBytes, destColors.AsBytes(), count * sizeof(Rgba32)); } /// internal override unsafe void ToXyzwBytes(Span sourceColors, Span destBytes, int count) { + Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors)); + Guard.MustBeSizedAtLeast(destBytes, count * 4, nameof(destBytes)); + SpanHelper.Copy(sourceColors.AsBytes(), destBytes, count * sizeof(Rgba32)); } /// internal override void PackFromZyxBytes(Span sourceBytes, Span destColors, int count) { + Guard.MustBeSizedAtLeast(sourceBytes, count * 3, nameof(sourceBytes)); + Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors)); + ref RGB24 sourceRef = ref Unsafe.As(ref sourceBytes.DangerousGetPinnableReference()); ref Rgba32 destRef = ref destColors.DangerousGetPinnableReference(); @@ -179,6 +197,9 @@ namespace ImageSharp /// internal override void ToZyxBytes(Span sourceColors, Span destBytes, int count) { + Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors)); + Guard.MustBeSizedAtLeast(destBytes, count * 3, nameof(destBytes)); + ref Rgba32 sourceRef = ref sourceColors.DangerousGetPinnableReference(); ref RGB24 destRef = ref Unsafe.As(ref destBytes.DangerousGetPinnableReference()); @@ -194,6 +215,9 @@ namespace ImageSharp /// internal override void PackFromZyxwBytes(Span sourceBytes, Span destColors, int count) { + Guard.MustBeSizedAtLeast(sourceBytes, count * 4, nameof(sourceBytes)); + Guard.MustBeSizedAtLeast(destColors, count, nameof(destColors)); + ref RGBA32 sourceRef = ref Unsafe.As(ref sourceBytes.DangerousGetPinnableReference()); ref Rgba32 destRef = ref destColors.DangerousGetPinnableReference(); @@ -209,6 +233,9 @@ namespace ImageSharp /// internal override void ToZyxwBytes(Span sourceColors, Span destBytes, int count) { + Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors)); + Guard.MustBeSizedAtLeast(destBytes, count * 4, nameof(destBytes)); + ref Rgba32 sourceRef = ref sourceColors.DangerousGetPinnableReference(); ref RGBA32 destRef = ref Unsafe.As(ref destBytes.DangerousGetPinnableReference()); diff --git a/src/ImageSharp/PixelFormats/RgbaVector.PixelOperations.cs b/src/ImageSharp/PixelFormats/RgbaVector.PixelOperations.cs index 5c7ee17ca..ac25b7f14 100644 --- a/src/ImageSharp/PixelFormats/RgbaVector.PixelOperations.cs +++ b/src/ImageSharp/PixelFormats/RgbaVector.PixelOperations.cs @@ -23,6 +23,9 @@ namespace ImageSharp.PixelFormats /// internal override unsafe void ToVector4(Span sourceColors, Span destVectors, int count) { + Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors)); + Guard.MustBeSizedAtLeast(destVectors, count, nameof(destVectors)); + SpanHelper.Copy(sourceColors.AsBytes(), destVectors.AsBytes(), count * sizeof(Vector4)); } } From ce396ccef851fdc2023068706ce97d5536aed42a Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Sun, 14 May 2017 10:34:50 +0200 Subject: [PATCH 33/34] Remove resolution value from profile before setting it if it does not have the correct data type. (Fixes #215) --- .../MetaData/Profiles/Exif/ExifProfile.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs index b270caf5d..1217fc0a9 100644 --- a/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs +++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs @@ -238,11 +238,18 @@ namespace ImageSharp private void SyncResolution(ExifTag tag, double resolution) { ExifValue value = this.GetValue(tag); - if (value != null) + if (value == null) { - Rational newResolution = new Rational(resolution, false); - this.SetValue(tag, newResolution); + return; } + + if (value.IsArray || value.DataType != ExifDataType.Rational) + { + this.RemoveValue(value.Tag); + } + + Rational newResolution = new Rational(resolution, false); + this.SetValue(tag, newResolution); } private void InitializeValues() From b33d633df4e6b13fbb913f81e08fa07aad6abffd Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Sun, 14 May 2017 10:38:35 +0200 Subject: [PATCH 34/34] Fixed var warnings. --- .../MetaData/Profiles/Exif/ExifProfile.cs | 10 ++++---- .../MetaData/Profiles/Exif/ExifReader.cs | 6 ++--- .../MetaData/Profiles/Exif/ExifValue.cs | 4 +-- tests/ImageSharp.Tests/ConfigurationTests.cs | 25 ++++++++++--------- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs index 1217fc0a9..a7fd8fd6a 100644 --- a/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs +++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifProfile.cs @@ -136,7 +136,7 @@ namespace ImageSharp return null; } - using (MemoryStream memStream = new MemoryStream(this.data, this.thumbnailOffset, this.thumbnailLength)) + using (var memStream = new MemoryStream(this.data, this.thumbnailOffset, this.thumbnailLength)) { return Image.Load(memStream); } @@ -201,7 +201,7 @@ namespace ImageSharp } } - ExifValue newExifValue = ExifValue.Create(tag, value); + var newExifValue = ExifValue.Create(tag, value); this.values.Add(newExifValue); } @@ -221,7 +221,7 @@ namespace ImageSharp return null; } - ExifWriter writer = new ExifWriter(this.values, this.Parts); + var writer = new ExifWriter(this.values, this.Parts); return writer.GetData(); } @@ -248,7 +248,7 @@ namespace ImageSharp this.RemoveValue(value.Tag); } - Rational newResolution = new Rational(resolution, false); + var newResolution = new Rational(resolution, false); this.SetValue(tag, newResolution); } @@ -265,7 +265,7 @@ namespace ImageSharp return; } - ExifReader reader = new ExifReader(); + var reader = new ExifReader(); this.values = reader.Read(this.data); this.invalidTags = new List(reader.InvalidTags); this.thumbnailOffset = (int)reader.ThumbnailOffset; diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs index 6164bd228..53123bfc2 100644 --- a/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs +++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifReader.cs @@ -75,7 +75,7 @@ namespace ImageSharp { DebugGuard.NotNull(data, nameof(data)); - Collection result = new Collection(); + var result = new Collection(); this.exifData = data; @@ -357,7 +357,7 @@ namespace ImageSharp private TEnum ToEnum(int value, TEnum defaultValue) where TEnum : struct { - TEnum enumValue = (TEnum)(object)value; + var enumValue = (TEnum)(object)value; if (Enum.GetValues(typeof(TEnum)).Cast().Any(v => v.Equals(enumValue))) { return enumValue; @@ -403,7 +403,7 @@ namespace ImageSharp private void GetThumbnail(uint offset) { - Collection values = new Collection(); + var values = new Collection(); this.AddValues(values, offset); foreach (ExifValue value in values) diff --git a/src/ImageSharp/MetaData/Profiles/Exif/ExifValue.cs b/src/ImageSharp/MetaData/Profiles/Exif/ExifValue.cs index 8cd05b53c..a2965917b 100644 --- a/src/ImageSharp/MetaData/Profiles/Exif/ExifValue.cs +++ b/src/ImageSharp/MetaData/Profiles/Exif/ExifValue.cs @@ -39,7 +39,7 @@ namespace ImageSharp } else { - Array array = (Array)other.exifValue; + var array = (Array)other.exifValue; this.exifValue = array.Clone(); } } @@ -264,7 +264,7 @@ namespace ImageSharp return this.ToString(this.exifValue); } - StringBuilder sb = new StringBuilder(); + var sb = new StringBuilder(); foreach (object value in (Array)this.exifValue) { sb.Append(this.ToString(value)); diff --git a/tests/ImageSharp.Tests/ConfigurationTests.cs b/tests/ImageSharp.Tests/ConfigurationTests.cs index 3c0b7e702..aa3c4edfc 100644 --- a/tests/ImageSharp.Tests/ConfigurationTests.cs +++ b/tests/ImageSharp.Tests/ConfigurationTests.cs @@ -7,6 +7,7 @@ namespace ImageSharp.Tests { using System; using System.Collections.Generic; + using System.IO; using System.Linq; using ImageSharp.Formats; @@ -23,7 +24,7 @@ namespace ImageSharp.Tests [Fact] public void DefaultsToLocalFileSystem() { - Configuration configuration = Configuration.CreateDefaultInstance(); + var configuration = Configuration.CreateDefaultInstance(); ImageSharp.IO.IFileSystem fs = configuration.FileSystem; @@ -33,7 +34,7 @@ namespace ImageSharp.Tests [Fact] public void IfAutoloadWellknwonFormatesIsTrueAllFormateAreLoaded() { - Configuration configuration = Configuration.CreateDefaultInstance(); + var configuration = Configuration.CreateDefaultInstance(); Assert.Equal(4, configuration.ImageFormats.Count); } @@ -95,7 +96,7 @@ namespace ImageSharp.Tests [Fact] public void TestAddImageFormatThrowsWithNullEncoder() { - TestFormat format = new TestFormat { Encoder = null }; + var format = new TestFormat { Encoder = null }; Assert.Throws(() => { @@ -110,7 +111,7 @@ namespace ImageSharp.Tests [Fact] public void TestAddImageFormatThrowsWithNullDecoder() { - TestFormat format = new TestFormat { Decoder = null }; + var format = new TestFormat { Decoder = null }; Assert.Throws(() => { @@ -125,7 +126,7 @@ namespace ImageSharp.Tests [Fact] public void TestAddImageFormatThrowsWithNullOrEmptyMimeType() { - TestFormat format = new TestFormat { MimeType = null }; + var format = new TestFormat { MimeType = null }; Assert.Throws(() => { @@ -147,7 +148,7 @@ namespace ImageSharp.Tests [Fact] public void TestAddImageFormatThrowsWithNullOrEmptyExtension() { - TestFormat format = new TestFormat { Extension = null }; + var format = new TestFormat { Extension = null }; Assert.Throws(() => { @@ -169,7 +170,7 @@ namespace ImageSharp.Tests [Fact] public void TestAddImageFormatThrowsWenSupportedExtensionsIsNullOrEmpty() { - TestFormat format = new TestFormat { SupportedExtensions = null }; + var format = new TestFormat { SupportedExtensions = null }; Assert.Throws(() => { @@ -191,7 +192,7 @@ namespace ImageSharp.Tests [Fact] public void TestAddImageFormatThrowsWithoutDefaultExtension() { - TestFormat format = new TestFormat { Extension = "test" }; + var format = new TestFormat { Extension = "test" }; Assert.Throws(() => { @@ -206,7 +207,7 @@ namespace ImageSharp.Tests [Fact] public void TestAddImageFormatThrowsWithEmptySupportedExtension() { - TestFormat format = new TestFormat + var format = new TestFormat { Extension = "test", SupportedExtensions = new[] { "test", string.Empty } @@ -238,7 +239,7 @@ namespace ImageSharp.Tests { Configuration.Default.AddImageFormat(new PngFormat()); - Image image = new Image(1, 1); + var image = new Image(1, 1); Assert.Equal(image.Configuration.ParallelOptions, Configuration.Default.ParallelOptions); Assert.Equal(image.Configuration.ImageFormats, Configuration.Default.ImageFormats); } @@ -251,8 +252,8 @@ namespace ImageSharp.Tests { Configuration.Default.AddImageFormat(new PngFormat()); - Image image = new Image(1, 1); - Image image2 = new Image(image); + var image = new Image(1, 1); + var image2 = new Image(image); Assert.Equal(image2.Configuration.ParallelOptions, image.Configuration.ParallelOptions); Assert.True(image2.Configuration.ImageFormats.SequenceEqual(image.Configuration.ImageFormats)); }