Browse Source

remove PixelAccessor<T> usages from common code

af/merge-core
Anton Firszov 8 years ago
parent
commit
026631027f
  1. 71
      src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs
  2. 22
      src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
  3. 35
      src/ImageSharp/Processing/Convolution/Processors/EdgeDetectorCompassProcessor.cs

71
src/ImageSharp/Formats/Bmp/BmpDecoderCore.cs

@ -103,36 +103,42 @@ namespace SixLabors.ImageSharp.Formats.Bmp
this.ReadImageHeaders(stream, out bool inverted, out byte[] palette); this.ReadImageHeaders(stream, out bool inverted, out byte[] palette);
var image = new Image<TPixel>(this.configuration, this.infoHeader.Width, this.infoHeader.Height); var image = new Image<TPixel>(this.configuration, this.infoHeader.Width, this.infoHeader.Height);
using (PixelAccessor<TPixel> pixels = image.Lock())
Buffer2D<TPixel> pixels = image.Frames.RootFrame.PixelBuffer;
switch (this.infoHeader.Compression)
{ {
switch (this.infoHeader.Compression) case BmpCompression.RGB:
{ if (this.infoHeader.BitsPerPixel == 32)
case BmpCompression.RGB: {
if (this.infoHeader.BitsPerPixel == 32) this.ReadRgb32(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted);
{ }
this.ReadRgb32(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); else if (this.infoHeader.BitsPerPixel == 24)
} {
else if (this.infoHeader.BitsPerPixel == 24) this.ReadRgb24(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted);
{ }
this.ReadRgb24(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); else if (this.infoHeader.BitsPerPixel == 16)
} {
else if (this.infoHeader.BitsPerPixel == 16) this.ReadRgb16(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted);
{ }
this.ReadRgb16(pixels, this.infoHeader.Width, this.infoHeader.Height, inverted); else if (this.infoHeader.BitsPerPixel <= 8)
} {
else if (this.infoHeader.BitsPerPixel <= 8) this.ReadRgbPalette(
{ pixels,
this.ReadRgbPalette(pixels, palette, this.infoHeader.Width, this.infoHeader.Height, this.infoHeader.BitsPerPixel, inverted); palette,
} this.infoHeader.Width,
this.infoHeader.Height,
this.infoHeader.BitsPerPixel,
inverted);
}
break; break;
case BmpCompression.RLE8: case BmpCompression.RLE8:
this.ReadRle8(pixels, palette, this.infoHeader.Width, this.infoHeader.Height, inverted); this.ReadRle8(pixels, palette, this.infoHeader.Width, this.infoHeader.Height, inverted);
break; break;
default: default:
throw new NotSupportedException("Does not support this kind of bitmap files."); throw new NotSupportedException("Does not support this kind of bitmap files.");
}
} }
return image; return image;
@ -207,7 +213,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
/// <param name="width">The width of the bitmap.</param> /// <param name="width">The width of the bitmap.</param>
/// <param name="height">The height of the bitmap.</param> /// <param name="height">The height of the bitmap.</param>
/// <param name="inverted">Whether the bitmap is inverted.</param> /// <param name="inverted">Whether the bitmap is inverted.</param>
private void ReadRle8<TPixel>(PixelAccessor<TPixel> pixels, byte[] colors, int width, int height, bool inverted) private void ReadRle8<TPixel>(Buffer2D<TPixel> pixels, byte[] colors, int width, int height, bool inverted)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
var color = default(TPixel); var color = default(TPixel);
@ -319,7 +325,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
/// <param name="height">The height of the bitmap.</param> /// <param name="height">The height of the bitmap.</param>
/// <param name="bits">The number of bits per pixel.</param> /// <param name="bits">The number of bits per pixel.</param>
/// <param name="inverted">Whether the bitmap is inverted.</param> /// <param name="inverted">Whether the bitmap is inverted.</param>
private void ReadRgbPalette<TPixel>(PixelAccessor<TPixel> pixels, byte[] colors, int width, int height, int bits, bool inverted) private void ReadRgbPalette<TPixel>(Buffer2D<TPixel> pixels, byte[] colors, int width, int height, int bits, bool inverted)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
// Pixels per byte (bits per pixel) // Pixels per byte (bits per pixel)
@ -381,7 +387,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
/// <param name="width">The width of the bitmap.</param> /// <param name="width">The width of the bitmap.</param>
/// <param name="height">The height of the bitmap.</param> /// <param name="height">The height of the bitmap.</param>
/// <param name="inverted">Whether the bitmap is inverted.</param> /// <param name="inverted">Whether the bitmap is inverted.</param>
private void ReadRgb16<TPixel>(PixelAccessor<TPixel> pixels, int width, int height, bool inverted) private void ReadRgb16<TPixel>(Buffer2D<TPixel> pixels, int width, int height, bool inverted)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
int padding = CalculatePadding(width, 2); int padding = CalculatePadding(width, 2);
@ -422,7 +428,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
/// <param name="width">The width of the bitmap.</param> /// <param name="width">The width of the bitmap.</param>
/// <param name="height">The height of the bitmap.</param> /// <param name="height">The height of the bitmap.</param>
/// <param name="inverted">Whether the bitmap is inverted.</param> /// <param name="inverted">Whether the bitmap is inverted.</param>
private void ReadRgb24<TPixel>(PixelAccessor<TPixel> pixels, int width, int height, bool inverted) private void ReadRgb24<TPixel>(Buffer2D<TPixel> pixels, int width, int height, bool inverted)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
int padding = CalculatePadding(width, 3); int padding = CalculatePadding(width, 3);
@ -447,7 +453,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
/// <param name="width">The width of the bitmap.</param> /// <param name="width">The width of the bitmap.</param>
/// <param name="height">The height of the bitmap.</param> /// <param name="height">The height of the bitmap.</param>
/// <param name="inverted">Whether the bitmap is inverted.</param> /// <param name="inverted">Whether the bitmap is inverted.</param>
private void ReadRgb32<TPixel>(PixelAccessor<TPixel> pixels, int width, int height, bool inverted) private void ReadRgb32<TPixel>(Buffer2D<TPixel> pixels, int width, int height, bool inverted)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
int padding = CalculatePadding(width, 4); int padding = CalculatePadding(width, 4);
@ -574,6 +580,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
this.stream.Read(palette, 0, colorMapSize); this.stream.Read(palette, 0, colorMapSize);
} }
// TODO: ReSharper tells this expression is always true, looks like he's pretty right about it:
if (this.infoHeader.Width > int.MaxValue || this.infoHeader.Height > int.MaxValue) if (this.infoHeader.Width > int.MaxValue || this.infoHeader.Height > int.MaxValue)
{ {
throw new ArgumentOutOfRangeException( throw new ArgumentOutOfRangeException(

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

@ -92,18 +92,16 @@ namespace SixLabors.ImageSharp.Formats.Bmp
private void WriteImage<TPixel>(Stream stream, ImageFrame<TPixel> image) private void WriteImage<TPixel>(Stream stream, ImageFrame<TPixel> image)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (PixelAccessor<TPixel> pixels = image.Lock()) Buffer2D<TPixel> pixels = image.PixelBuffer;
switch (this.bitsPerPixel)
{ {
switch (this.bitsPerPixel) case BmpBitsPerPixel.Pixel32:
{ this.Write32Bit(stream, pixels);
case BmpBitsPerPixel.Pixel32: break;
this.Write32Bit(stream, pixels);
break;
case BmpBitsPerPixel.Pixel24: case BmpBitsPerPixel.Pixel24:
this.Write24Bit(stream, pixels); this.Write24Bit(stream, pixels);
break; break;
}
} }
} }
@ -118,7 +116,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
/// <typeparam name="TPixel">The pixel format.</typeparam> /// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="stream">The <see cref="Stream"/> to write to.</param> /// <param name="stream">The <see cref="Stream"/> to write to.</param>
/// <param name="pixels">The <see cref="PixelAccessor{TPixel}"/> containing pixel data.</param> /// <param name="pixels">The <see cref="PixelAccessor{TPixel}"/> containing pixel data.</param>
private void Write32Bit<TPixel>(Stream stream, PixelAccessor<TPixel> pixels) private void Write32Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (IManagedByteBuffer row = this.AllocateRow(pixels.Width, 4)) using (IManagedByteBuffer row = this.AllocateRow(pixels.Width, 4))
@ -138,7 +136,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
/// <typeparam name="TPixel">The pixel format.</typeparam> /// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="stream">The <see cref="Stream"/> to write to.</param> /// <param name="stream">The <see cref="Stream"/> to write to.</param>
/// <param name="pixels">The <see cref="PixelAccessor{TPixel}"/> containing pixel data.</param> /// <param name="pixels">The <see cref="PixelAccessor{TPixel}"/> containing pixel data.</param>
private void Write24Bit<TPixel>(Stream stream, PixelAccessor<TPixel> pixels) private void Write24Bit<TPixel>(Stream stream, Buffer2D<TPixel> pixels)
where TPixel : struct, IPixel<TPixel> where TPixel : struct, IPixel<TPixel>
{ {
using (IManagedByteBuffer row = this.AllocateRow(pixels.Width, 3)) using (IManagedByteBuffer row = this.AllocateRow(pixels.Width, 3))

35
src/ImageSharp/Processing/Convolution/Processors/EdgeDetectorCompassProcessor.cs

@ -3,11 +3,14 @@
using System; using System;
using System.Numerics; using System.Numerics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks; using System.Threading.Tasks;
using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Primitives; using SixLabors.ImageSharp.Primitives;
using SixLabors.ImageSharp.Processing.Filters.Processors; using SixLabors.ImageSharp.Processing.Filters.Processors;
using SixLabors.ImageSharp.Processing.Processors; using SixLabors.ImageSharp.Processing.Processors;
using SixLabors.Memory;
using SixLabors.Primitives; using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing.Convolution.Processors namespace SixLabors.ImageSharp.Processing.Convolution.Processors
@ -128,27 +131,35 @@ namespace SixLabors.ImageSharp.Processing.Convolution.Processors
{ {
new ConvolutionProcessor<TPixel>(kernels[i]).Apply(pass, sourceRectangle, configuration); new ConvolutionProcessor<TPixel>(kernels[i]).Apply(pass, sourceRectangle, configuration);
using (PixelAccessor<TPixel> passPixels = pass.Lock()) Buffer2D<TPixel> passPixels = pass.PixelBuffer;
using (PixelAccessor<TPixel> targetPixels = source.Lock()) Buffer2D<TPixel> targetPixels = source.PixelBuffer;
{
Parallel.For( Parallel.For(
minY, minY,
maxY, maxY,
configuration.ParallelOptions, configuration.ParallelOptions,
y => y =>
{ {
int offsetY = y - shiftY; int offsetY = y - shiftY;
ref TPixel passPixelsBase = ref MemoryMarshal.GetReference(passPixels.GetRowSpan(offsetY));
ref TPixel targetPixelsBase = ref MemoryMarshal.GetReference(targetPixels.GetRowSpan(offsetY));
for (int x = minX; x < maxX; x++) for (int x = minX; x < maxX; x++)
{ {
int offsetX = x - shiftX; int offsetX = x - shiftX;
// Grab the max components of the two pixels // Grab the max components of the two pixels
TPixel packed = default(TPixel); ref TPixel currentPassPixel = ref Unsafe.Add(ref passPixelsBase, offsetX);
packed.PackFromVector4(Vector4.Max(passPixels[offsetX, offsetY].ToVector4(), targetPixels[offsetX, offsetY].ToVector4())); ref TPixel currentTargetPixel = ref Unsafe.Add(ref targetPixelsBase, offsetX);
targetPixels[offsetX, offsetY] = packed;
var pixelValue = Vector4.Max(
currentPassPixel.ToVector4(),
currentTargetPixel.ToVector4());
currentTargetPixel.PackFromVector4(pixelValue);
} }
}); });
}
} }
} }
} }

Loading…
Cancel
Save