Browse Source

exlusivly use frames to access pixel spans

pull/326/head
Scott Williams 9 years ago
parent
commit
ee60eb3e83
  1. 24
      src/ImageSharp/Advanced/IPixelSource.cs
  2. 33
      src/ImageSharp/Advanced/ImageExtensions.cs
  3. 4
      src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
  4. 4
      src/ImageSharp/Formats/Gif/GifDecoderCore.cs
  5. 2
      src/ImageSharp/Formats/Gif/GifEncoderCore.cs
  6. 6
      src/ImageSharp/Formats/Jpeg/Common/Decoder/JpegImagePostProcessor.cs
  7. 2
      src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs
  8. 14
      src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs
  9. 10
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  10. 6
      src/ImageSharp/Formats/Png/PngEncoderCore.cs
  11. 13
      src/ImageSharp/Image/IImageFrame.cs
  12. 2
      src/ImageSharp/Image/Image.LoadPixelData.cs
  13. 11
      src/ImageSharp/Image/ImageExtensions.cs
  14. 4
      src/ImageSharp/Image/ImageFrame{TPixel}.cs
  15. 21
      src/ImageSharp/Image/Image{TPixel}.cs
  16. 20
      src/ImageSharp/Image/PixelAccessorExtensions.cs
  17. 8
      src/ImageSharp/Image/PixelAccessor{TPixel}.cs
  18. 4
      tests/ImageSharp.Benchmarks/Image/CopyPixels.cs
  19. 6
      tests/ImageSharp.Tests/Formats/Jpg/JpegImagePostProcessorTests.cs
  20. 2
      tests/ImageSharp.Tests/Image/ImageLoadTests.cs
  21. 5
      tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/GrayscaleTest.cs
  22. 6
      tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingBridge.cs
  23. 2
      tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

24
src/ImageSharp/Advanced/IPixelSource.cs

@ -0,0 +1,24 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.MetaData;
using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp.Advanced
{
/// <summary>
/// Encapsulates the basic properties and methods required to manipulate images.
/// </summary>
/// <typeparam name="TPixel">The type of the pixel.</typeparam>
internal interface IPixelSource<TPixel>
where TPixel : struct, IPixel<TPixel>
{
/// <summary>
/// Gets the pixel buffer.
/// </summary>
Buffer2D<TPixel> PixelBuffer { get; }
}
}

33
src/ImageSharp/Advanced/ImageExtensions.cs

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.IO;
using System.Text;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;
@ -26,16 +27,6 @@ namespace SixLabors.ImageSharp.Advanced
where TPixel : struct, IPixel<TPixel>
=> GetSpan(source);
/// <summary>
/// Gets the representation of the pixels as an area of contiguous memory in the given pixel format.
/// </summary>
/// <typeparam name="TPixel">The type of the pixel.</typeparam>
/// <param name="source">The source.</param>
/// <returns>The <see cref="Span{TPixel}"/></returns>
public static Span<TPixel> GetPixelSpan<TPixel>(this Image<TPixel> source)
where TPixel : struct, IPixel<TPixel>
=> GetSpan(source);
/// <summary>
/// Gets a <see cref="Span{TPixal}"/> representing the row 'y' beginning from the the first pixel on that row.
/// </summary>
@ -48,25 +39,27 @@ namespace SixLabors.ImageSharp.Advanced
=> GetSpan(source, row);
/// <summary>
/// Gets a <see cref="Span{TPixal}"/> representing the row 'y' beginning from the the first pixel on that row.
/// Gets the span.
/// </summary>
/// <typeparam name="TPixel">The type of the pixel.</typeparam>
/// <param name="source">The source.</param>
/// <param name="row">The row.</param>
/// <returns>The <see cref="Span{TPixel}"/></returns>
public static Span<TPixel> GetPixelRowSpan<TPixel>(this Image<TPixel> source, int row)
/// <returns>The span retuned from Pixel source</returns>
private static Span<TPixel> GetSpan<TPixel>(IPixelSource<TPixel> source)
where TPixel : struct, IPixel<TPixel>
=> GetSpan(source, row);
=> source.PixelBuffer.Span;
/// <summary>
/// Gets the span.
/// </summary>
/// <typeparam name="TPixel">The type of the pixel.</typeparam>
/// <param name="source">The source.</param>
/// <returns>The span retuned from Pixel source</returns>
private static Span<TPixel> GetSpan<TPixel>(IImageFrame<TPixel> source)
/// <param name="row">The row.</param>
/// <returns>
/// The span retuned from Pixel source
/// </returns>
private static Span<TPixel> GetSpan<TPixel>(IPixelSource<TPixel> source, int row)
where TPixel : struct, IPixel<TPixel>
=> source.PixelBuffer.Span;
=> GetSpan(source.PixelBuffer, row);
/// <summary>
/// Gets the span.
@ -77,9 +70,9 @@ namespace SixLabors.ImageSharp.Advanced
/// <returns>
/// The span retuned from Pixel source
/// </returns>
private static Span<TPixel> GetSpan<TPixel>(IImageFrame<TPixel> source, int row)
private static Span<TPixel> GetSpan<TPixel>(Buffer2D<TPixel> source, int row)
where TPixel : struct, IPixel<TPixel>
=> source.PixelBuffer.Span.Slice(row * source.Width, source.Width);
=> source.Span.Slice(row * source.Width, source.Width);
/// <summary>
/// Gets the bounds of the image.

4
src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs

@ -73,7 +73,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
WriteHeader(writer, fileHeader);
this.WriteInfo(writer, infoHeader);
this.WriteImage(writer, image);
this.WriteImage(writer, image.Frames.RootFrame);
writer.Flush();
}
@ -127,7 +127,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
/// <param name="image">
/// The <see cref="ImageFrame{TPixel}"/> containing pixel data.
/// </param>
private void WriteImage<TPixel>(EndianBinaryWriter writer, IImageFrame<TPixel> image)
private void WriteImage<TPixel>(EndianBinaryWriter writer, ImageFrame<TPixel> image)
where TPixel : struct, IPixel<TPixel>
{
using (PixelAccessor<TPixel> pixels = image.Lock())

4
src/ImageSharp/Formats/Gif/GifDecoderCore.cs

@ -378,7 +378,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
this.SetFrameMetaData(this.metaData);
image = (ImageFrame<TPixel>)this.image;
image = this.image.Frames.RootFrame;
}
else
{
@ -471,7 +471,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
return;
}
this.previousFrame = currentFrame == null ? this.image : currentFrame;
this.previousFrame = currentFrame == null ? this.image.Frames.RootFrame : currentFrame;
if (this.graphicsControlExtension != null &&
this.graphicsControlExtension.DisposalMethod == DisposalMethod.RestoreToBackground)

2
src/ImageSharp/Formats/Gif/GifEncoderCore.cs

@ -103,7 +103,7 @@ namespace SixLabors.ImageSharp.Formats.Gif
ditheredQuantizer.Dither = !this.hasFrames;
// Quantize the image returning a palette.
QuantizedImage<TPixel> quantized = ditheredQuantizer.Quantize(image, paletteSize);
QuantizedImage<TPixel> quantized = ditheredQuantizer.Quantize(image.Frames.RootFrame, paletteSize);
int index = this.GetTransparentIndex(quantized);

6
src/ImageSharp/Formats/Jpeg/Common/Decoder/JpegImagePostProcessor.cs

@ -98,7 +98,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Common.Decoder
/// </summary>
/// <typeparam name="TPixel">The pixel type</typeparam>
/// <param name="destination">The destination image</param>
public void PostProcess<TPixel>(Image<TPixel> destination)
public void PostProcess<TPixel>(ImageFrame<TPixel> destination)
where TPixel : struct, IPixel<TPixel>
{
this.PixelRowCounter = 0;
@ -119,7 +119,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Common.Decoder
/// </summary>
/// <typeparam name="TPixel">The pixel type</typeparam>
/// <param name="destination">The destination image.</param>
public void DoPostProcessorStep<TPixel>(Image<TPixel> destination)
public void DoPostProcessorStep<TPixel>(ImageFrame<TPixel> destination)
where TPixel : struct, IPixel<TPixel>
{
foreach (JpegComponentPostProcessor cpp in this.ComponentProcessors)
@ -137,7 +137,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.Common.Decoder
/// </summary>
/// <typeparam name="TPixel">The pixel type</typeparam>
/// <param name="destination">The destination image</param>
private void ConvertColorsInto<TPixel>(Image<TPixel> destination)
private void ConvertColorsInto<TPixel>(ImageFrame<TPixel> destination)
where TPixel : struct, IPixel<TPixel>
{
int maxY = Math.Min(destination.Height, this.PixelRowCounter + PixelRowsPerStep);

2
src/ImageSharp/Formats/Jpeg/GolangPort/OrigJpegDecoderCore.cs

@ -815,7 +815,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.GolangPort
using (var postProcessor = new JpegImagePostProcessor(this))
{
var image = new Image<TPixel>(this.configuration, this.ImageWidth, this.ImageHeight, this.MetaData);
postProcessor.PostProcess(image);
postProcessor.PostProcess(image.Frames.RootFrame);
return image;
}
}

14
src/ImageSharp/Formats/Jpeg/PdfJsPort/PdfJsJpegDecoderCore.cs

@ -159,7 +159,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort
this.QuantizeAndInverseAllComponents();
var image = new Image<TPixel>(this.configuration, this.ImageWidth, this.ImageHeight, metadata);
this.FillPixelData(image);
this.FillPixelData(image.Frames.RootFrame);
this.AssignResolution(image);
return image;
}
@ -326,7 +326,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="image">The image</param>
private void FillPixelData<TPixel>(Image<TPixel> image)
private void FillPixelData<TPixel>(ImageFrame<TPixel> image)
where TPixel : struct, IPixel<TPixel>
{
if (this.NumberOfComponents > 4)
@ -856,7 +856,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void FillGrayScaleImage<TPixel>(Image<TPixel> image)
private void FillGrayScaleImage<TPixel>(ImageFrame<TPixel> image)
where TPixel : struct, IPixel<TPixel>
{
for (int y = 0; y < image.Height; y++)
@ -875,7 +875,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void FillYCbCrImage<TPixel>(Image<TPixel> image)
private void FillYCbCrImage<TPixel>(ImageFrame<TPixel> image)
where TPixel : struct, IPixel<TPixel>
{
for (int y = 0; y < image.Height; y++)
@ -894,7 +894,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void FillYcckImage<TPixel>(Image<TPixel> image)
private void FillYcckImage<TPixel>(ImageFrame<TPixel> image)
where TPixel : struct, IPixel<TPixel>
{
for (int y = 0; y < image.Height; y++)
@ -915,7 +915,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void FillCmykImage<TPixel>(Image<TPixel> image)
private void FillCmykImage<TPixel>(ImageFrame<TPixel> image)
where TPixel : struct, IPixel<TPixel>
{
for (int y = 0; y < image.Height; y++)
@ -941,7 +941,7 @@ namespace SixLabors.ImageSharp.Formats.Jpeg.PdfJsPort
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void FillRgbImage<TPixel>(Image<TPixel> image)
private void FillRgbImage<TPixel>(ImageFrame<TPixel> image)
where TPixel : struct, IPixel<TPixel>
{
for (int y = 0; y < image.Height; y++)

10
src/ImageSharp/Formats/Png/PngDecoderCore.cs

@ -236,7 +236,7 @@ namespace SixLabors.ImageSharp.Formats.Png
}
deframeStream.AllocateNewBytes(currentChunk.Length);
this.ReadScanlines(deframeStream.CompressedStream, image);
this.ReadScanlines(deframeStream.CompressedStream, image.Frames.RootFrame);
stream.Read(this.crcBuffer, 0, 4);
break;
case PngChunkTypes.Palette:
@ -442,7 +442,7 @@ namespace SixLabors.ImageSharp.Formats.Png
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="dataStream">The <see cref="MemoryStream"/> containing data.</param>
/// <param name="image"> The pixel data.</param>
private void ReadScanlines<TPixel>(Stream dataStream, Image<TPixel> image)
private void ReadScanlines<TPixel>(Stream dataStream, ImageFrame<TPixel> image)
where TPixel : struct, IPixel<TPixel>
{
if (this.header.InterlaceMethod == PngInterlaceMode.Adam7)
@ -461,7 +461,7 @@ namespace SixLabors.ImageSharp.Formats.Png
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="compressedStream">The compressed pixel data stream.</param>
/// <param name="image">The image to decode to.</param>
private void DecodePixelData<TPixel>(Stream compressedStream, Image<TPixel> image)
private void DecodePixelData<TPixel>(Stream compressedStream, ImageFrame<TPixel> image)
where TPixel : struct, IPixel<TPixel>
{
while (this.currentRow < this.header.Height)
@ -519,7 +519,7 @@ namespace SixLabors.ImageSharp.Formats.Png
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="compressedStream">The compressed pixel data stream.</param>
/// <param name="image">The current image.</param>
private void DecodeInterlacedPixelData<TPixel>(Stream compressedStream, Image<TPixel> image)
private void DecodeInterlacedPixelData<TPixel>(Stream compressedStream, ImageFrame<TPixel> image)
where TPixel : struct, IPixel<TPixel>
{
while (true)
@ -609,7 +609,7 @@ namespace SixLabors.ImageSharp.Formats.Png
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="defilteredScanline">The de-filtered scanline</param>
/// <param name="pixels">The image</param>
private void ProcessDefilteredScanline<TPixel>(byte[] defilteredScanline, Image<TPixel> pixels)
private void ProcessDefilteredScanline<TPixel>(byte[] defilteredScanline, ImageFrame<TPixel> pixels)
where TPixel : struct, IPixel<TPixel>
{
var color = default(TPixel);

6
src/ImageSharp/Formats/Png/PngEncoderCore.cs

@ -233,12 +233,12 @@ namespace SixLabors.ImageSharp.Formats.Png
// Collect the indexed pixel data
if (this.pngColorType == PngColorType.Palette)
{
this.CollectIndexedBytes<TPixel>(image, stream, header);
this.CollectIndexedBytes<TPixel>(image.Frames.RootFrame, stream, header);
}
this.WritePhysicalChunk(stream, image);
this.WriteGammaChunk(stream);
this.WriteDataChunks(image, stream);
this.WriteDataChunks(image.Frames.RootFrame, stream);
this.WriteEndChunk(stream);
stream.Flush();
}
@ -649,7 +649,7 @@ namespace SixLabors.ImageSharp.Formats.Png
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="pixels">The image.</param>
/// <param name="stream">The stream.</param>
private void WriteDataChunks<TPixel>(Image<TPixel> pixels, Stream stream)
private void WriteDataChunks<TPixel>(ImageFrame<TPixel> pixels, Stream stream)
where TPixel : struct, IPixel<TPixel>
{
this.bytesPerScanline = this.width * this.bytesPerPixel;

13
src/ImageSharp/Image/IImageFrame.cs

@ -9,19 +9,6 @@ using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp
{
/// <summary>
/// Encapsulates the basic properties and methods required to manipulate images.
/// </summary>
/// <typeparam name="TPixel">The type of the pixel.</typeparam>
internal interface IImageFrame<TPixel> : IImageFrame
where TPixel : struct, IPixel<TPixel>
{
/// <summary>
/// Gets the pixel buffer.
/// </summary>
Buffer2D<TPixel> PixelBuffer { get; }
}
/// <summary>
/// Encapsulates the basic properties and methods required to manipulate images.
/// </summary>

2
src/ImageSharp/Image/Image.LoadPixelData.cs

@ -70,7 +70,7 @@ namespace SixLabors.ImageSharp
Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data));
var image = new Image<TPixel>(config, width, height);
SpanHelper.Copy(data, image.GetPixelSpan(), count);
SpanHelper.Copy(data, image.Frames.RootFrame.GetPixelSpan(), count);
return image;
}

11
src/ImageSharp/Image/ImageExtensions.cs

@ -158,7 +158,7 @@ namespace SixLabors.ImageSharp
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static byte[] SavePixelData<TPixel>(this Image<TPixel> source)
where TPixel : struct, IPixel<TPixel>
=> source.GetPixelSpan().AsBytes().ToArray();
=> source.Frames.RootFrame.SavePixelData();
/// <summary>
/// Saves the raw image to the given bytes.
@ -169,7 +169,7 @@ namespace SixLabors.ImageSharp
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SavePixelData<TPixel>(this Image<TPixel> source, byte[] buffer)
where TPixel : struct, IPixel<TPixel>
=> SavePixelData(source, new Span<byte>(buffer));
=> source.Frames.RootFrame.SavePixelData(buffer);
/// <summary>
/// Saves the raw image to the given bytes.
@ -180,12 +180,7 @@ namespace SixLabors.ImageSharp
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
public static void SavePixelData<TPixel>(this Image<TPixel> source, Span<byte> buffer)
where TPixel : struct, IPixel<TPixel>
{
Span<byte> byteBuffer = source.GetPixelSpan().AsBytes();
Guard.MustBeGreaterThanOrEqualTo(buffer.Length, byteBuffer.Length, nameof(buffer));
byteBuffer.CopyTo(buffer);
}
=> source.Frames.RootFrame.SavePixelData(buffer);
/// <summary>
/// Returns a Base64 encoded string from the given image.

4
src/ImageSharp/Image/ImageFrame{TPixel}.cs

@ -17,7 +17,7 @@ namespace SixLabors.ImageSharp
/// Represents a single frame in a animation.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
public sealed class ImageFrame<TPixel> : IImageFrame<TPixel>
public sealed class ImageFrame<TPixel> : IImageFrame, IPixelSource<TPixel>
where TPixel : struct, IPixel<TPixel>
{
/// <summary>
@ -65,7 +65,7 @@ namespace SixLabors.ImageSharp
}
/// <inheritdoc/>
Buffer2D<TPixel> IImageFrame<TPixel>.PixelBuffer => this.pixelBuffer;
Buffer2D<TPixel> IPixelSource<TPixel>.PixelBuffer => this.pixelBuffer;
/// <inheritdoc/>
public int Width => this.pixelBuffer.Width;

21
src/ImageSharp/Image/Image{TPixel}.cs

@ -1,10 +1,11 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.MetaData;
@ -16,7 +17,7 @@ namespace SixLabors.ImageSharp
/// Encapsulates an image, which consists of the pixel data for a graphics image and its attributes.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
public sealed partial class Image<TPixel> : IImageFrame<TPixel>
public sealed partial class Image<TPixel> : IDisposable
where TPixel : struct, IPixel<TPixel>
{
/// <summary>
@ -100,12 +101,12 @@ namespace SixLabors.ImageSharp
/// <summary>
/// Gets the width.
/// </summary>
public int Width => this.RootFrame?.Width ?? 0;
public int Width => this.Frames.RootFrame.Width;
/// <summary>
/// Gets the height.
/// </summary>
public int Height => this.RootFrame?.Height ?? 0;
public int Height => this.Frames.RootFrame.Height;
/// <summary>
/// Gets the meta data of the image.
@ -120,13 +121,7 @@ namespace SixLabors.ImageSharp
/// <summary>
/// Gets the root frame.
/// </summary>
private IImageFrame<TPixel> RootFrame => this.Frames?.RootFrame;
/// <inheritdoc/>
Buffer2D<TPixel> IImageFrame<TPixel>.PixelBuffer => this.RootFrame.PixelBuffer;
/// <inheritdoc/>
ImageFrameMetaData IImageFrame.MetaData => this.RootFrame.MetaData;
private IPixelSource<TPixel> PixelSource => this.Frames?.RootFrame;
/// <summary>
/// Gets or sets the pixel at the specified position.
@ -136,9 +131,9 @@ namespace SixLabors.ImageSharp
/// <returns>The <see typeparam="TPixel"/> at the specified position.</returns>
public TPixel this[int x, int y]
{
get => this.RootFrame.PixelBuffer[x, y];
get => this.PixelSource.PixelBuffer[x, y];
set => this.RootFrame.PixelBuffer[x, y] = value;
set => this.PixelSource.PixelBuffer[x, y] = value;
}
/// <summary>

20
src/ImageSharp/Image/PixelAccessorExtensions.cs

@ -5,6 +5,7 @@ using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using Unsafe = System.Runtime.CompilerServices.Unsafe;
@ -27,10 +28,27 @@ namespace SixLabors.ImageSharp
/// <returns>
/// The <see cref="PixelAccessor{TPixel}" />
/// </returns>
internal static PixelAccessor<TPixel> Lock<TPixel>(this IImageFrame<TPixel> frame)
internal static PixelAccessor<TPixel> Lock<TPixel>(this IPixelSource<TPixel> frame)
where TPixel : struct, IPixel<TPixel>
{
return new PixelAccessor<TPixel>(frame);
}
/// <summary>
/// Locks the image providing access to the pixels.
/// <remarks>
/// It is imperative that the accessor is correctly disposed off after use.
/// </remarks>
/// </summary>
/// <typeparam name="TPixel">The type of the pixel.</typeparam>
/// <param name="image">The image.</param>
/// <returns>
/// The <see cref="PixelAccessor{TPixel}" />
/// </returns>
internal static PixelAccessor<TPixel> Lock<TPixel>(this Image<TPixel> image)
where TPixel : struct, IPixel<TPixel>
{
return image.Frames.RootFrame.Lock();
}
}
}

8
src/ImageSharp/Image/PixelAccessor{TPixel}.cs

@ -4,7 +4,7 @@
using System;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using Unsafe = System.Runtime.CompilerServices.Unsafe;
@ -41,11 +41,11 @@ namespace SixLabors.ImageSharp
/// Initializes a new instance of the <see cref="PixelAccessor{TPixel}"/> class.
/// </summary>
/// <param name="image">The image to provide pixel access for.</param>
public PixelAccessor(IImageFrame<TPixel> image)
public PixelAccessor(IPixelSource<TPixel> image)
{
Guard.NotNull(image, nameof(image));
Guard.MustBeGreaterThan(image.Width, 0, "image width");
Guard.MustBeGreaterThan(image.Height, 0, "image height");
Guard.MustBeGreaterThan(image.PixelBuffer.Width, 0, "image width");
Guard.MustBeGreaterThan(image.PixelBuffer.Height, 0, "image height");
this.SetPixelBufferUnsafe(image.PixelBuffer, false);
}

4
tests/ImageSharp.Benchmarks/Image/CopyPixels.cs

@ -103,8 +103,8 @@ namespace SixLabors.ImageSharp.Benchmarks.Image
Configuration.Default.ParallelOptions,
y =>
{
Span<Rgba32> sourceRow = source.GetPixelRowSpan(y);
Span<Rgba32> targetRow = target.GetPixelRowSpan(y);
Span<Rgba32> sourceRow = source.Frames.RootFrame.GetPixelRowSpan(y);
Span<Rgba32> targetRow = target.Frames.RootFrame.GetPixelRowSpan(y);
for (int x = 0; x < source.Width; x++)
{

6
tests/ImageSharp.Tests/Formats/Jpg/JpegImagePostProcessorTests.cs

@ -56,9 +56,9 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
string imageFile = provider.SourceFileOrDescription;
using (OrigJpegDecoderCore decoder = JpegFixture.ParseStream(imageFile))
using (var pp = new JpegImagePostProcessor(decoder))
using (var image = new Image<Rgba32>(decoder.ImageWidth, decoder.ImageHeight))
using (var imageFrame = new ImageFrame<Rgba32>(decoder.ImageWidth, decoder.ImageHeight))
{
pp.DoPostProcessorStep(image);
pp.DoPostProcessorStep(imageFrame);
JpegComponentPostProcessor[] cp = pp.ComponentProcessors;
@ -80,7 +80,7 @@ namespace SixLabors.ImageSharp.Tests.Formats.Jpg
using (var pp = new JpegImagePostProcessor(decoder))
using (var image = new Image<Rgba32>(decoder.ImageWidth, decoder.ImageHeight))
{
pp.PostProcess(image);
pp.PostProcess(image.Frames.RootFrame);
image.DebugSave(provider);

2
tests/ImageSharp.Tests/Image/ImageLoadTests.cs

@ -326,7 +326,7 @@ namespace SixLabors.ImageSharp.Tests
using (Image<Rgba32> img = image1Provider.GetImage())
{
Assert.Equal(166036, img.GetPixelSpan().Length);
Assert.Equal(166036, img.Frames.RootFrame.GetPixelSpan().Length);
}
}

5
tests/ImageSharp.Tests/Processing/Processors/ColorMatrix/GrayscaleTest.cs

@ -32,9 +32,10 @@ namespace SixLabors.ImageSharp.Tests.Processing.Processors.ColorMatrix
{
image.Mutate(x => x.Grayscale(value));
byte[] data = new byte[3];
for (int i = 0; i < image.GetPixelSpan().Length; i++)
System.Span<TPixel> span = image.Frames.RootFrame.GetPixelSpan();
for (int i = 0; i < span.Length; i++)
{
image.GetPixelSpan()[i].ToXyzBytes(data, 0);
span[i].ToXyzBytes(data, 0);
Assert.Equal(data[0], data[1]);
Assert.Equal(data[1], data[2]);
}

6
tests/ImageSharp.Tests/TestUtilities/ReferenceCodecs/SystemDrawingBridge.cs

@ -101,7 +101,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
var destPtr = (Argb32*)workBuffer.Pin();
for (int y = 0; y < h; y++)
{
Span<TPixel> row = image.GetPixelRowSpan(y);
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);
byte* sourcePtr = sourcePtrBase + data.Stride * y;
@ -143,7 +143,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
var destPtr = (Rgb24*)workBuffer.Pin();
for (int y = 0; y < h; y++)
{
Span<TPixel> row = image.GetPixelRowSpan(y);
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);
byte* sourcePtr = sourcePtrBase + data.Stride * y;
@ -176,7 +176,7 @@ namespace SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs
for (int y = 0; y < h; y++)
{
Span<TPixel> row = image.GetPixelRowSpan(y);
Span<TPixel> row = image.Frames.RootFrame.GetPixelRowSpan(y);
ToArgb32(row, workBuffer);
byte* destPtr = destPtrBase + data.Stride * y;

2
tests/ImageSharp.Tests/TestUtilities/TestImageExtensions.cs

@ -196,7 +196,7 @@ namespace SixLabors.ImageSharp.Tests
{
var image = new Image<Rgba32>(buffer.Width, buffer.Height);
Span<Rgba32> pixels = image.GetPixelSpan();
Span<Rgba32> pixels = image.Frames.RootFrame.GetPixelSpan();
for (int i = 0; i < buffer.Length; i++)
{

Loading…
Cancel
Save