Example #1
0
        public void IterateRectangularBuffer(
            int maxDegreeOfParallelism,
            int bufferWidth,
            int bufferHeight,
            int rectX,
            int rectY,
            int rectWidth,
            int rectHeight)
        {
            MemoryAllocator memoryAllocator = Configuration.Default.MemoryAllocator;

            using (Buffer2D <Point> expected = memoryAllocator.Allocate2D <Point>(bufferWidth, bufferHeight, AllocationOptions.Clean))
                using (Buffer2D <Point> actual = memoryAllocator.Allocate2D <Point>(bufferWidth, bufferHeight, AllocationOptions.Clean))
                {
                    var rect = new Rectangle(rectX, rectY, rectWidth, rectHeight);

                    void FillRow(int y, Buffer2D <Point> buffer)
                    {
                        for (int x = rect.Left; x < rect.Right; x++)
                        {
                            buffer[x, y] = new Point(x, y);
                        }
                    }

                    // Fill Expected data:
                    for (int y = rectY; y < rect.Bottom; y++)
                    {
                        FillRow(y, expected);
                    }

                    // Fill actual data using IterateRows:
                    var settings = new ParallelExecutionSettings(maxDegreeOfParallelism, memoryAllocator);

                    void RowAction(RowInterval rows)
                    {
                        this.output.WriteLine(rows.ToString());
                        for (int y = rows.Min; y < rows.Max; y++)
                        {
                            FillRow(y, actual);
                        }
                    }

                    var operation = new TestRowIntervalOperation(RowAction);

                    ParallelRowIterator.IterateRowIntervals(
                        rect,
                        settings,
                        in operation);

                    // Assert:
                    TestImageExtensions.CompareBuffers(expected.DangerousGetSingleSpan(), actual.DangerousGetSingleSpan());
                }
        }
Example #2
0
        internal static Image <Rgba32> ToGrayscaleImage(this Buffer2D <float> buffer, float scale)
        {
            var image = new Image <Rgba32>(buffer.Width, buffer.Height);

            Assert.True(image.Frames.RootFrame.TryGetSinglePixelSpan(out Span <Rgba32> pixels));
            Span <float> bufferSpan = buffer.DangerousGetSingleSpan();

            for (int i = 0; i < bufferSpan.Length; i++)
            {
                float value = bufferSpan[i] * scale;
                var   v     = new Vector4(value, value, value, 1f);
                pixels[i].FromVector4(v);
            }

            return(image);
        }