public void TestCrop() { ITiffPixelBuffer <TiffGray8> pixelBuffer = InitializePixelBuffer(out TiffGray8[] pixels); TiffPixelBuffer <TiffGray8> structBuffer = pixelBuffer.AsPixelBuffer(); Assert.Equal(3, structBuffer.Width); Assert.Equal(2, structBuffer.Height); Assert.False(structBuffer.IsEmpty); Assert.Throws <ArgumentOutOfRangeException>("offset", () => structBuffer.Crop(new TiffPoint(4, 0))); Assert.Throws <ArgumentOutOfRangeException>("offset", () => structBuffer.Crop(new TiffPoint(0, 3))); Assert.Throws <ArgumentOutOfRangeException>("offset", () => structBuffer.Crop(new TiffPoint(4, 3))); Assert.Throws <ArgumentOutOfRangeException>("size", () => structBuffer.Crop(new TiffPoint(0, 0), new TiffSize(4, 1))); Assert.Throws <ArgumentOutOfRangeException>("size", () => structBuffer.Crop(new TiffPoint(0, 0), new TiffSize(1, 3))); Assert.Throws <ArgumentOutOfRangeException>("size", () => structBuffer.Crop(new TiffPoint(0, 0), new TiffSize(4, 3))); structBuffer = pixelBuffer.Crop(new TiffPoint(1, 1), new TiffSize(1, 1)); Assert.Equal(1, structBuffer.Width); Assert.Equal(1, structBuffer.Height); structBuffer = pixelBuffer.AsPixelBuffer().Crop(new TiffPoint(1, 1), new TiffSize(1, 1)); Assert.Equal(1, structBuffer.Width); Assert.Equal(1, structBuffer.Height); structBuffer = pixelBuffer.Crop(new TiffPoint(1, 1)); Assert.Equal(2, structBuffer.Width); Assert.Equal(1, structBuffer.Height); structBuffer = pixelBuffer.AsPixelBuffer().Crop(new TiffPoint(1, 1)); Assert.Equal(2, structBuffer.Width); Assert.Equal(1, structBuffer.Height); ITiffPixelBuffer <TiffGray8> pixelBuffer2 = TiffPixelBufferUnsafeMarshal.GetBuffer(structBuffer, out TiffPoint offset, out TiffSize size); Assert.True(ReferenceEquals(pixelBuffer, pixelBuffer2)); Assert.Equal(1, offset.X); Assert.Equal(1, offset.Y); Assert.Equal(2, size.Width); Assert.Equal(1, size.Height); }
/// <summary> /// Crop a sub region from <paramref name="buffer"/>. /// </summary> /// <typeparam name="TPixel">The pixel type.</typeparam> /// <param name="buffer">The original pixel buffer.</param> /// <param name="offset">The number of columns and rows to skip.</param> /// <param name="size">The number of columns and rows to take.</param> /// <returns>A <see cref="TiffPixelBuffer{TPixel}"/> representing the cropped region.</returns> public static TiffPixelBuffer <TPixel> Crop <TPixel>(this ITiffPixelBuffer <TPixel> buffer, TiffPoint offset, TiffSize size) where TPixel : unmanaged { return(buffer.AsPixelBuffer().Crop(offset, size)); }