diff --git a/src/ImageSharp/Formats/Jpg/Utils/JpegUtils.cs b/src/ImageSharp/Formats/Jpg/Utils/JpegUtils.cs index e11a8a71c..7f62058b7 100644 --- a/src/ImageSharp/Formats/Jpg/Utils/JpegUtils.cs +++ b/src/ImageSharp/Formats/Jpg/Utils/JpegUtils.cs @@ -27,41 +27,12 @@ namespace ImageSharp.Formats.Jpg int sourceX) where TColor : struct, IPackedPixel, IEquatable { - pixels.UncheckedCopyTo(dest, sourceY, sourceX); + pixels.SafeCopyTo(dest, sourceY, sourceX); int stretchFromX = pixels.Width - sourceX; int stretchFromY = pixels.Height - sourceY; StretchPixels(dest, stretchFromX, stretchFromY); } - /// - /// Copy a region of image into the image destination area. Does not throw when requesting a 0-size copy. - /// - /// The pixel type - /// The source - /// The destination area. - /// The source row index. - /// The source column index. - /// - /// Thrown when an unsupported component order value is passed. - /// - public static void UncheckedCopyTo( - this PixelAccessor sourcePixels, - PixelArea destinationArea, - int sourceY, - int sourceX) - where TColor : struct, IPackedPixel, IEquatable - { - // TODO: Code smell! This is exactly the same code PixelArea.CopyTo() starts with! - int width = Math.Min(destinationArea.Width, sourcePixels.Width - sourceX); - int height = Math.Min(destinationArea.Height, sourcePixels.Height - sourceY); - if (width < 1 || height < 1) - { - return; - } - - sourcePixels.CopyTo(destinationArea, sourceY, sourceX); - } - /// /// Copy an RGB value /// diff --git a/src/ImageSharp/Image/PixelAccessor{TColor}.cs b/src/ImageSharp/Image/PixelAccessor{TColor}.cs index bb0096ecb..9496c93ec 100644 --- a/src/ImageSharp/Image/PixelAccessor{TColor}.cs +++ b/src/ImageSharp/Image/PixelAccessor{TColor}.cs @@ -183,27 +183,9 @@ namespace ImageSharp /// public void CopyFrom(PixelArea area, int targetY, int targetX = 0) { - int width = Math.Min(area.Width, this.Width - targetX); - int height = Math.Min(area.Height, this.Height - targetY); + this.CheckCoordinates(area, targetX, targetY); - this.CheckDimensions(width, height); - switch (area.ComponentOrder) - { - case ComponentOrder.Zyx: - this.CopyFromZyx(area, targetY, targetX, width, height); - break; - case ComponentOrder.Zyxw: - this.CopyFromZyxw(area, targetY, targetX, width, height); - break; - case ComponentOrder.Xyz: - this.CopyFromXyz(area, targetY, targetX, width, height); - break; - case ComponentOrder.Xyzw: - this.CopyFromXyzw(area, targetY, targetX, width, height); - break; - default: - throw new NotSupportedException(); - } + this.CopyFrom(area, targetY, targetX, area.Width, area.Height); } /// @@ -216,28 +198,36 @@ namespace ImageSharp /// Thrown when an unsupported component order value is passed. /// public void CopyTo(PixelArea area, int sourceY, int sourceX = 0) + { + this.CheckCoordinates(area, sourceX, sourceY); + + this.CopyTo(area, sourceY, sourceX, area.Width, area.Height); + } + + /// + /// Copied an area of pixels to the image and makes sure this is within the bounds of the image. + /// + /// The area. + /// The source row index. + /// The source column index. + /// + /// Thrown when an unsupported component order value is passed. + /// + public void SafeCopyTo(PixelArea area, int sourceY, int sourceX = 0) { int width = Math.Min(area.Width, this.Width - sourceX); - int height = Math.Min(area.Height, this.Height - sourceY); + if (width < 1) + { + return; + } - this.CheckDimensions(width, height); - switch (area.ComponentOrder) + int height = Math.Min(area.Height, this.Height - sourceY); + if (height < 1) { - case ComponentOrder.Zyx: - this.CopyToZyx(area, sourceY, sourceX, width, height); - break; - case ComponentOrder.Zyxw: - this.CopyToZyxw(area, sourceY, sourceX, width, height); - break; - case ComponentOrder.Xyz: - this.CopyToXyz(area, sourceY, sourceX, width, height); - break; - case ComponentOrder.Xyzw: - this.CopyToXyzw(area, sourceY, sourceX, width, height); - break; - default: - throw new NotSupportedException(); + return; } + + this.CopyTo(area, sourceY, sourceX, width, height); } /// @@ -491,21 +481,88 @@ namespace ImageSharp } /// - /// Checks that the given dimensions are within the bounds of the image. + /// Copied a row of pixels from the image /// - /// The width. - /// The height. + /// The area. + /// The target row index. + /// The target column index. + /// The width of the area to copy. + /// The height of the area to copy. + /// + /// Thrown when an unsupported component order value is passed. + /// + private void CopyFrom(PixelArea area, int targetY, int targetX, int width, int height) + { + switch (area.ComponentOrder) + { + case ComponentOrder.Zyx: + this.CopyFromZyx(area, targetY, targetX, width, height); + break; + case ComponentOrder.Zyxw: + this.CopyFromZyxw(area, targetY, targetX, width, height); + break; + case ComponentOrder.Xyz: + this.CopyFromXyz(area, targetY, targetX, width, height); + break; + case ComponentOrder.Xyzw: + this.CopyFromXyzw(area, targetY, targetX, width, height); + break; + default: + throw new NotSupportedException(); + } + } + + /// + /// Copied an area of pixels to the image. + /// + /// The area. + /// The source row index. + /// The source column index. + /// The width of the area to copy. + /// The height of the area to copy. + /// + /// Thrown when an unsupported component order value is passed. + /// + private void CopyTo(PixelArea area, int sourceY, int sourceX, int width, int height) + { + switch (area.ComponentOrder) + { + case ComponentOrder.Zyx: + this.CopyToZyx(area, sourceY, sourceX, width, height); + break; + case ComponentOrder.Zyxw: + this.CopyToZyxw(area, sourceY, sourceX, width, height); + break; + case ComponentOrder.Xyz: + this.CopyToXyz(area, sourceY, sourceX, width, height); + break; + case ComponentOrder.Xyzw: + this.CopyToXyzw(area, sourceY, sourceX, width, height); + break; + default: + throw new NotSupportedException(); + } + } + + /// + /// Checks that the given area and offset are within the bounds of the image. + /// + /// The area. + /// The x-coordinate of the pixel. Must be greater than zero and smaller than the width of the pixel. + /// The y-coordinate of the pixel. Must be greater than zero and smaller than the width of the pixel. /// /// Thrown if the dimensions are not within the bounds of the image. /// [Conditional("DEBUG")] - private void CheckDimensions(int width, int height) + private void CheckCoordinates(PixelArea area, int x, int y) { + int width = Math.Min(area.Width, this.Width - x); if (width < 1) { throw new ArgumentOutOfRangeException(nameof(width), width, $"Invalid area size specified."); } + int height = Math.Min(area.Height, this.Height - y); if (height < 1) { throw new ArgumentOutOfRangeException(nameof(height), height, $"Invalid area size specified.");