Skip to content

Commit

Permalink
Added UnsafePixelCollection that does not boundary checking (closes #37)
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemstra committed Sep 11, 2017
1 parent 33d6e21 commit 9e309be
Show file tree
Hide file tree
Showing 6 changed files with 1,333 additions and 4 deletions.
9 changes: 9 additions & 0 deletions Source/Magick.NET/Shared/IMagickImage.cs
Expand Up @@ -1781,6 +1781,15 @@ public partial interface IMagickImage : IEquatable<IMagickImage>, IComparable<IM
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Should be a method.")]
IPixelCollection GetPixels();

/// <summary>
/// Returns a pixel collection that can be used to read or modify the pixels of this image. This instance
/// will not do any bounds checking and directly call ImageMagick.
/// </summary>
/// <returns>A pixel collection that can be used to read or modify the pixels of this image.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
[SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate", Justification = "Should be a method.")]
IPixelCollection GetPixelsUnsafe();

/// <summary>
/// Retrieve a named profile from the image.
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions Source/Magick.NET/Shared/MagickImage.cs
Expand Up @@ -3265,6 +3265,20 @@ public IPixelCollection GetPixels()
return new SafePixelCollection(this);
}

/// <summary>
/// Returns a pixel collection that can be used to read or modify the pixels of this image. This instance
/// will not do any bounds checking and directly call ImageMagick.
/// </summary>
/// <returns>A pixel collection that can be used to read or modify the pixels of this image.</returns>
/// <exception cref="MagickException">Thrown when an error is raised by ImageMagick.</exception>
public IPixelCollection GetPixelsUnsafe()
{
if (Settings.Ping)
throw new InvalidOperationException("Image contains no pixel data.");

return new UnsafePixelCollection(this);
}

/// <summary>
/// Retrieve a named profile from the image.
/// </summary>
Expand Down
3 changes: 2 additions & 1 deletion Source/Magick.NET/Shared/Pixels/PixelCollection.cs
Expand Up @@ -105,7 +105,8 @@ public QuantumType[] GetValues()

public virtual void SetPixel(Pixel pixel)
{
SetPixelUnchecked(pixel.X, pixel.Y, pixel.Value);
if (pixel != null)
SetPixelUnchecked(pixel.X, pixel.Y, pixel.Value);
}

public void SetPixel(IEnumerable<Pixel> pixels)
Expand Down
42 changes: 42 additions & 0 deletions Source/Magick.NET/Shared/Pixels/UnsafePixelCollection.cs
@@ -0,0 +1,42 @@
// Copyright 2013-2017 Dirk Lemstra <https://github.com/dlemstra/Magick.NET/>
//
// Licensed under the ImageMagick License (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
//
// https://www.imagemagick.org/script/license.php
//
// Unless required by applicable law or agreed to in writing, software distributed under the
// License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
// either express or implied. See the License for the specific language governing permissions
// and limitations under the License.

using System;
using System.Collections;
using System.Collections.Generic;

#if Q8
using QuantumType = System.Byte;
#elif Q16
using QuantumType = System.UInt16;
#elif Q16HDRI
using QuantumType = System.Single;
#else
#error Not implemented!
#endif

namespace ImageMagick
{
internal sealed class UnsafePixelCollection : PixelCollection
{
public UnsafePixelCollection(MagickImage image)
: base(image)
{
}

public override void SetPixel(int x, int y, QuantumType[] value)
{
if (value != null)
base.SetPixel(x, y, value);
}
}
}
Expand Up @@ -71,9 +71,8 @@ public void Indexer_InsideImage_ReturnsPixel()
using (IPixelCollection pixels = image.GetPixels())
{
Pixel pixel = pixels[300, 100];
MagickColor color = pixel.ToColor();

ColorAssert.AreEqual(MagickColors.Red, color);
ColorAssert.AreEqual(MagickColors.Red, pixel.ToColor());
}
}
}
Expand Down Expand Up @@ -274,7 +273,7 @@ public void GetPixel_OutsideImage_ThrowsException()
{
ExceptionAssert.ThrowsArgumentException<ArgumentOutOfRangeException>("x", () =>
{
Pixel pixel = pixels.GetPixel(image.Width + 1, 0);
pixels.GetPixel(image.Width + 1, 0);
});
}
}
Expand Down

0 comments on commit 9e309be

Please sign in to comment.