示例#1
0
 public static void Encode(Span <byte> scanline, Span <byte> result)
 {
     // Insert a byte before the data.
     result[0] = 0;
     result    = result.Slice(1);
     SpanHelper.Copy(scanline, result);
 }
示例#2
0
        /// <inheritdoc/>
        protected override void OnApply(ImageBase <TPixel> source, Rectangle sourceRectangle)
        {
            if (this.CropRectangle == sourceRectangle)
            {
                return;
            }

            int minY = Math.Max(this.CropRectangle.Y, sourceRectangle.Y);
            int maxY = Math.Min(this.CropRectangle.Bottom, sourceRectangle.Bottom);
            int minX = Math.Max(this.CropRectangle.X, sourceRectangle.X);
            int maxX = Math.Min(this.CropRectangle.Right, sourceRectangle.Right);

            using (var targetPixels = new PixelAccessor <TPixel>(this.CropRectangle.Width, this.CropRectangle.Height))
            {
                Parallel.For(
                    minY,
                    maxY,
                    this.ParallelOptions,
                    y =>
                {
                    Span <TPixel> sourceRow = source.GetRowSpan(minX, y);
                    Span <TPixel> targetRow = targetPixels.GetRowSpan(y - minY);
                    SpanHelper.Copy(sourceRow, targetRow, maxX - minX);
                });

                source.SwapPixelsBuffers(targetPixels);
            }
        }
示例#3
0
        /// <inheritdoc/>
        protected override void OnFrameApply(ImageFrame <TPixel> source, ImageFrame <TPixel> destination, Rectangle sourceRectangle, Configuration configuration)
        {
            // Handle resize dimensions identical to the original
            if (source.Width == destination.Width && source.Height == destination.Height && sourceRectangle == this.CropRectangle)
            {
                // the cloned will be blank here copy all the pixel data over
                source.GetPixelSpan().CopyTo(destination.GetPixelSpan());
                return;
            }

            int minY = Math.Max(this.CropRectangle.Y, sourceRectangle.Y);
            int maxY = Math.Min(this.CropRectangle.Bottom, sourceRectangle.Bottom);
            int minX = Math.Max(this.CropRectangle.X, sourceRectangle.X);
            int maxX = Math.Min(this.CropRectangle.Right, sourceRectangle.Right);

            Parallel.For(
                minY,
                maxY,
                configuration.ParallelOptions,
                y =>
            {
                Span <TPixel> sourceRow = source.GetPixelRowSpan(y).Slice(minX);
                Span <TPixel> targetRow = destination.GetPixelRowSpan(y - minY);
                SpanHelper.Copy(sourceRow, targetRow, maxX - minX);
            });
        }
            /// <inheritdoc />
            internal override unsafe void ToVector4(Span <RgbaVector> sourceColors, Span <Vector4> destVectors, int count)
            {
                Guard.MustBeSizedAtLeast(sourceColors, count, nameof(sourceColors));
                Guard.MustBeSizedAtLeast(destVectors, count, nameof(destVectors));

                SpanHelper.Copy(sourceColors.AsBytes(), destVectors.AsBytes(), count * sizeof(Vector4));
            }
示例#5
0
        /// <inheritdoc/>
        protected override void OnApply(ImageFrame <TPixel> source, Rectangle sourceRectangle, Configuration configuration)
        {
            if (this.CropRectangle == sourceRectangle)
            {
                return;
            }

            int minY = Math.Max(this.CropRectangle.Y, sourceRectangle.Y);
            int maxY = Math.Min(this.CropRectangle.Bottom, sourceRectangle.Bottom);
            int minX = Math.Max(this.CropRectangle.X, sourceRectangle.X);
            int maxX = Math.Min(this.CropRectangle.Right, sourceRectangle.Right);

            using (Buffer2D <TPixel> targetPixels = configuration.MemoryManager.Allocate2D <TPixel>(this.CropRectangle.Size))
            {
                Parallel.For(
                    minY,
                    maxY,
                    configuration.ParallelOptions,
                    y =>
                {
                    Span <TPixel> sourceRow = source.GetPixelRowSpan(y).Slice(minX);
                    Span <TPixel> targetRow = targetPixels.GetRowSpan(y - minY);
                    SpanHelper.Copy(sourceRow, targetRow, maxX - minX);
                });

                Buffer2D <TPixel> .SwapContents(source.PixelBuffer, targetPixels);
            }
        }
示例#6
0
        /// <summary>
        /// Copies the pixels to a <see cref="Buffer2D{TPixel}"/> of the same size.
        /// </summary>
        /// <param name="target">The target pixel buffer accessor.</param>
        internal void CopyTo(Buffer2D <TPixel> target)
        {
            if (this.Size() != target.Size())
            {
                throw new ArgumentException("ImageFrame<TPixel>.CopyTo(): target must be of the same size!", nameof(target));
            }

            SpanHelper.Copy(this.GetPixelSpan(), target.Span);
        }
示例#7
0
        /// <summary>
        /// Create a new instance of the <see cref="Image{TPixel}"/> class from the raw <typeparamref name="TPixel"/> data.
        /// </summary>
        /// <param name="data">The Span containing the image Pixel data.</param>
        /// <param name="width">The width of the final image.</param>
        /// <param name="height">The height of the final image.</param>
        /// <typeparam name="TPixel">The pixel format.</typeparam>
        /// <returns>A new <see cref="Image{TPixel}"/>.</returns>
        public static ImageFrame <TPixel> LoadPixelData <TPixel>(Span <TPixel> data, int width, int height)
            where TPixel : struct, IPixel <TPixel>
        {
            int count = width * height;

            Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data));

            var image = new ImageFrame <TPixel>(width, height);

            SpanHelper.Copy(data, image.GetPixelSpan(), count);

            return(image);
        }
示例#8
0
        /// <summary>
        /// Create a new instance of the <see cref="Image{TPixel}"/> class from the raw <typeparamref name="TPixel"/> data.
        /// </summary>
        /// <param name="config">The config for the decoder.</param>
        /// <param name="data">The Span containing the image Pixel data.</param>
        /// <param name="width">The width of the final image.</param>
        /// <param name="height">The height of the final image.</param>
        /// <typeparam name="TPixel">The pixel format.</typeparam>
        /// <returns>A new <see cref="Image{TPixel}"/>.</returns>
        private static Image <TPixel> LoadPixelData <TPixel>(Configuration config, Span <TPixel> data, int width, int height)
            where TPixel : struct, IPixel <TPixel>
        {
            int count = width * height;

            Guard.MustBeGreaterThanOrEqualTo(data.Length, count, nameof(data));

            var image = new Image <TPixel>(config, width, height);

            SpanHelper.Copy(data, image.Frames.RootFrame.GetPixelSpan(), count);

            return(image);
        }
示例#9
0
            public void IntToBytes(int count)
            {
                int destCount = count * sizeof(int);

                int[]  source = CreateTestInts(count + 2);
                byte[] dest   = new byte[destCount + sizeof(int) + 1];

                var apSource = new Span <int>(source);
                var apDest   = new Span <byte>(dest);

                SpanHelper.Copy(apSource.AsBytes(), apDest, count * sizeof(int));

                AssertNotDefault(source, 1);

                Assert.True((bool)ElementsAreEqual(source, dest, 0));
                Assert.True((bool)ElementsAreEqual(source, dest, count - 1));
                Assert.False((bool)ElementsAreEqual(source, dest, count));
            }
示例#10
0
            public void GenericToOwnType_Aligned(int count)
            {
                TestStructs.AlignedFoo[] source = TestStructs.AlignedFoo.CreateArray(count + 2);
                TestStructs.AlignedFoo[] dest   = new TestStructs.AlignedFoo[count + 5];

                var apSource = new Span <TestStructs.AlignedFoo>(source, 1, source.Length - 1);
                var apDest   = new Span <TestStructs.AlignedFoo>(dest, 1, dest.Length - 1);

                SpanHelper.Copy(apSource, apDest, count - 1);

                AssertNotDefault(source, 1);
                AssertNotDefault(dest, 1);

                Assert.NotEqual(source[0], dest[0]);
                Assert.Equal(source[1], dest[1]);
                Assert.Equal(source[2], dest[2]);
                Assert.Equal(source[count - 1], dest[count - 1]);
                Assert.NotEqual(source[count], dest[count]);
            }
示例#11
0
            public void IntToInt(int count)
            {
                int[] source = CreateTestInts(count + 2);
                int[] dest   = new int[count + 5];

                var apSource = new Span <int>(source, 1, source.Length - 1);
                var apDest   = new Span <int>(dest, 1, dest.Length - 1);

                SpanHelper.Copy(apSource, apDest, count - 1);

                AssertNotDefault(source, 1);
                AssertNotDefault(dest, 1);

                Assert.NotEqual(source[0], dest[0]);
                Assert.Equal(source[1], dest[1]);
                Assert.Equal(source[2], dest[2]);
                Assert.Equal(source[count - 1], dest[count - 1]);
                Assert.NotEqual(source[count], dest[count]);
            }
示例#12
0
            public void BytesToGeneric(int count)
            {
                int srcCount = count * sizeof(TestStructs.Foo);

                byte[]            source = CreateTestBytes(srcCount);
                TestStructs.Foo[] dest   = new TestStructs.Foo[count + 2];

                var apSource = new Span <byte>(source);
                var apDest   = new Span <TestStructs.Foo>(dest);

                SpanHelper.Copy(apSource, apDest.AsBytes(), count * sizeof(TestStructs.Foo));

                AssertNotDefault(source, sizeof(TestStructs.Foo) + 1);
                AssertNotDefault(dest, 1);

                Assert.True((bool)ElementsAreEqual(dest, source, 0));
                Assert.True((bool)ElementsAreEqual(dest, source, 1));
                Assert.True((bool)ElementsAreEqual(dest, source, count - 1));
                Assert.False((bool)ElementsAreEqual(dest, source, count));
            }
示例#13
0
            public void GenericToBytes_Aligned(int count)
            {
                int destCount = count * sizeof(TestStructs.Foo);

                TestStructs.AlignedFoo[] source = TestStructs.AlignedFoo.CreateArray(count + 2);
                byte[] dest = new byte[destCount + sizeof(TestStructs.AlignedFoo) * 2];

                var apSource = new Span <TestStructs.AlignedFoo>(source, 1, source.Length - 1);
                var apDest   = new Span <byte>(dest, sizeof(TestStructs.AlignedFoo), dest.Length - sizeof(TestStructs.AlignedFoo));

                SpanHelper.Copy(apSource.AsBytes(), apDest, (count - 1) * sizeof(TestStructs.AlignedFoo));

                AssertNotDefault(source, 1);

                Assert.False((bool)ElementsAreEqual(source, dest, 0));
                Assert.True((bool)ElementsAreEqual(source, dest, 1));
                Assert.True((bool)ElementsAreEqual(source, dest, 2));
                Assert.True((bool)ElementsAreEqual(source, dest, count - 1));
                Assert.False((bool)ElementsAreEqual(source, dest, count));
            }
示例#14
0
 /// <summary>
 /// Copies the pixels to another <see cref="PixelAccessor{TPixel}"/> of the same size.
 /// </summary>
 /// <param name="target">The target pixel buffer accessor.</param>
 internal void CopyTo(PixelAccessor <TPixel> target)
 {
     SpanHelper.Copy(this.Pixels, target.PixelBuffer.Span);
 }
示例#15
0
            /// <inheritdoc />
            internal override unsafe void ToVector4(Span <RgbaVector> sourceColors, Span <Vector4> destVectors, int count)
            {
                GuardSpans(sourceColors, nameof(sourceColors), destVectors, nameof(destVectors), count);

                SpanHelper.Copy(sourceColors.NonPortableCast <RgbaVector, Vector4>(), destVectors, count);
            }