From f994932b8b391a8512c4ebab7bdbd846956823b2 Mon Sep 17 00:00:00 2001 From: Dirk Lemstra Date: Fri, 2 Dec 2016 13:51:29 +0100 Subject: [PATCH] Moved coordinates check to a method and used a Conditional instead of an #if. --- src/ImageSharp/Image/PixelAccessor.cs | 40 ++++++++++++--------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/ImageSharp/Image/PixelAccessor.cs b/src/ImageSharp/Image/PixelAccessor.cs index fc460870c0..712fe20230 100644 --- a/src/ImageSharp/Image/PixelAccessor.cs +++ b/src/ImageSharp/Image/PixelAccessor.cs @@ -6,6 +6,7 @@ namespace ImageSharp { using System; + using System.Diagnostics; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -106,38 +107,33 @@ namespace ImageSharp { 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 + this.CheckCoordinates(x, y); + return Unsafe.Read(this.pixelsBase + (((y * this.Width) + x) * Unsafe.SizeOf())); } 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 + this.CheckCoordinates(x, y); Unsafe.Write(this.pixelsBase + (((y * this.Width) + x) * Unsafe.SizeOf()), value); } } + [Conditional("DEBUG")] + private void CheckCoordinates(int x, int y) + { + 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."); + } + } + /// /// Copies a block of pixels at the specified position. ///