Browse Source

Add bounds checks. Fix #29

pull/33/head
James Jackson-South 9 years ago
parent
commit
0f2e213ae9
  1. 34
      src/ImageSharp/Image/PixelAccessor.cs
  2. 5
      src/ImageSharp/Samplers/Processors/CropProcessor.cs

34
src/ImageSharp/Image/PixelAccessor.cs

@ -104,8 +104,38 @@ namespace ImageSharp
/// <returns>The <see typeparam="TColor"/> at the specified position.</returns>
public TColor this[int x, int y]
{
get { return Unsafe.Read<TColor>(this.pixelsBase + (((y * this.Width) + x) * Unsafe.SizeOf<TColor>())); }
set { Unsafe.Write(this.pixelsBase + (((y * this.Width) + x) * Unsafe.SizeOf<TColor>()), value); }
get
{
#if DEBUG
if (x < 0 || x >= this.Width)
{
throw new ArgumentOutOfRangeException(nameof(x), x, $"{x} is outwith the image bounds.");
}
if (y < 0 || y >= this.Height)
{
throw new ArgumentOutOfRangeException(nameof(y), y, $"{y} is outwith the image bounds.");
}
#endif
return Unsafe.Read<TColor>(this.pixelsBase + (((y * this.Width) + x) * Unsafe.SizeOf<TColor>()));
}
set
{
#if DEBUG
if (x < 0 || x >= this.Width)
{
throw new ArgumentOutOfRangeException(nameof(x), x, $"{x} is outwith the image bounds.");
}
if (y < 0 || y >= this.Height)
{
throw new ArgumentOutOfRangeException(nameof(y), y, $"{y} is outwith the image bounds.");
}
#endif
Unsafe.Write(this.pixelsBase + (((y * this.Width) + x) * Unsafe.SizeOf<TColor>()), value);
}
}
/// <summary>

5
src/ImageSharp/Samplers/Processors/CropProcessor.cs

@ -24,6 +24,11 @@ namespace ImageSharp.Processors
int sourceX = sourceRectangle.X;
int sourceY = sourceRectangle.Y;
Guard.MustBeGreaterThanOrEqualTo(startX, sourceX, nameof(targetRectangle));
Guard.MustBeGreaterThanOrEqualTo(startY, sourceY, nameof(targetRectangle));
Guard.MustBeLessThanOrEqualTo(endX, sourceRectangle.Right, nameof(targetRectangle));
Guard.MustBeLessThanOrEqualTo(endY, sourceRectangle.Bottom, nameof(targetRectangle));
using (PixelAccessor<TColor, TPacked> sourcePixels = source.Lock())
using (PixelAccessor<TColor, TPacked> targetPixels = target.Lock())
{

Loading…
Cancel
Save