Browse Source

Don't go through SpanHelper to Copy

pull/495/head
Jason Nelson 8 years ago
parent
commit
092282ed13
  1. 19
      src/ImageSharp/Image.LoadPixelData.cs
  2. 7
      src/ImageSharp/ImageFrame.LoadPixelData.cs
  3. 2
      src/ImageSharp/ImageFrame{TPixel}.cs
  4. 2
      src/ImageSharp/PixelAccessor{TPixel}.cs
  5. 4
      src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs
  6. 2
      src/ImageSharp/PixelFormats/RgbaVector.PixelOperations.cs
  7. 2
      src/ImageSharp/Processing/Transforms/Processors/CropProcessor.cs

19
src/ImageSharp/Image.LoadPixelData.cs

@ -2,12 +2,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
namespace SixLabors.ImageSharp namespace SixLabors.ImageSharp
@ -102,14 +97,11 @@ namespace SixLabors.ImageSharp
/// <returns>A new <see cref="Image{TPixel}"/>.</returns> /// <returns>A new <see cref="Image{TPixel}"/>.</returns>
public static Image<TPixel> LoadPixelData<TPixel>(Configuration config, TPixel[] data, int width, int height) public static Image<TPixel> LoadPixelData<TPixel>(Configuration config, TPixel[] data, int width, int height)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
int count = width * height; // There's an implict cast to Span from Array
Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data)); // Should we remove this overload and expose Span<TPixel> ?
var image = new Image<TPixel>(config, width, height); return LoadPixelData(config, new Span<TPixel>(data), width, height);
SpanHelper.Copy(data, image.GetPixelSpan(), count);
return image;
} }
/// <summary> /// <summary>
@ -128,7 +120,8 @@ namespace SixLabors.ImageSharp
Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data)); Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data));
var image = new Image<TPixel>(config, width, height); var image = new Image<TPixel>(config, width, height);
SpanHelper.Copy(data, image.Frames.RootFrame.GetPixelSpan(), count);
data.Slice(0, count).CopyTo(image.Frames.RootFrame.GetPixelSpan());
return image; return image;
} }

7
src/ImageSharp/ImageFrame.LoadPixelData.cs

@ -2,11 +2,7 @@
// Licensed under the Apache License, Version 2.0. // Licensed under the Apache License, Version 2.0.
using System; using System;
using System.IO;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using SixLabors.ImageSharp.Advanced; using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Formats;
using SixLabors.ImageSharp.Memory; using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
@ -46,7 +42,8 @@ namespace SixLabors.ImageSharp
Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data)); Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data));
var image = new ImageFrame<TPixel>(memoryManager, width, height); var image = new ImageFrame<TPixel>(memoryManager, width, height);
SpanHelper.Copy(data, image.GetPixelSpan(), count);
data.Slice(0, count).CopyTo(image.GetPixelSpan());
return image; return image;
} }

2
src/ImageSharp/ImageFrame{TPixel}.cs

@ -181,7 +181,7 @@ namespace SixLabors.ImageSharp
throw new ArgumentException("ImageFrame<TPixel>.CopyTo(): target must be of the same size!", nameof(target)); throw new ArgumentException("ImageFrame<TPixel>.CopyTo(): target must be of the same size!", nameof(target));
} }
SpanHelper.Copy(this.GetPixelSpan(), target.Span); this.GetPixelSpan().CopyTo(target.Span);
} }
/// <summary> /// <summary>

2
src/ImageSharp/PixelAccessor{TPixel}.cs

@ -112,7 +112,7 @@ namespace SixLabors.ImageSharp
/// <param name="target">The target pixel buffer accessor.</param> /// <param name="target">The target pixel buffer accessor.</param>
internal void CopyTo(PixelAccessor<TPixel> target) internal void CopyTo(PixelAccessor<TPixel> target)
{ {
SpanHelper.Copy(this.PixelBuffer.Span, target.PixelBuffer.Span); this.PixelBuffer.Span.CopyTo(target.PixelBuffer.Span);
} }
/// <summary> /// <summary>

4
src/ImageSharp/PixelFormats/Rgba32.PixelOperations.cs

@ -149,7 +149,7 @@ namespace SixLabors.ImageSharp.PixelFormats
{ {
GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count); GuardSpans(source, nameof(source), destPixels, nameof(destPixels), count);
SpanHelper.Copy(source, destPixels, count); source.Slice(0, count).CopyTo(destPixels);
} }
/// <inheritdoc /> /// <inheritdoc />
@ -157,7 +157,7 @@ namespace SixLabors.ImageSharp.PixelFormats
{ {
GuardSpans(sourcePixels, nameof(sourcePixels), dest, nameof(dest), count); GuardSpans(sourcePixels, nameof(sourcePixels), dest, nameof(dest), count);
SpanHelper.Copy(sourcePixels, dest, count); sourcePixels.Slice(0, count).CopyTo(dest);
} }
/// <summary> /// <summary>

2
src/ImageSharp/PixelFormats/RgbaVector.PixelOperations.cs

@ -22,7 +22,7 @@ namespace SixLabors.ImageSharp.PixelFormats
{ {
GuardSpans(sourceColors, nameof(sourceColors), destVectors, nameof(destVectors), count); GuardSpans(sourceColors, nameof(sourceColors), destVectors, nameof(destVectors), count);
SpanHelper.Copy(sourceColors.NonPortableCast<RgbaVector, Vector4>(), destVectors, count); sourceColors.NonPortableCast<RgbaVector, Vector4>().Slice(0, count).CopyTo(destVectors);
} }
} }
} }

2
src/ImageSharp/Processing/Transforms/Processors/CropProcessor.cs

@ -67,7 +67,7 @@ namespace SixLabors.ImageSharp.Processing.Transforms.Processors
{ {
Span<TPixel> sourceRow = source.GetPixelRowSpan(y).Slice(minX); Span<TPixel> sourceRow = source.GetPixelRowSpan(y).Slice(minX);
Span<TPixel> targetRow = destination.GetPixelRowSpan(y - minY); Span<TPixel> targetRow = destination.GetPixelRowSpan(y - minY);
SpanHelper.Copy(sourceRow, targetRow, maxX - minX); sourceRow.Slice(0, maxX - minX).CopyTo(targetRow);
}); });
} }
} }

Loading…
Cancel
Save