public void AddPaletteColors(Buffer2DRegion <TPixel> pixelRegion)
        {
            Rectangle         bounds = pixelRegion.Rectangle;
            Buffer2D <TPixel> source = pixelRegion.Buffer;

            using IMemoryOwner <Rgba32> buffer = this.Configuration.MemoryAllocator.Allocate <Rgba32>(bounds.Width);
            Span <Rgba32> bufferSpan           = buffer.GetSpan();

            // Loop through each row
            for (int y = bounds.Top; y < bounds.Bottom; y++)
            {
                Span <TPixel> row = source.GetRowSpan(y).Slice(bounds.Left, bounds.Width);
                PixelOperations <TPixel> .Instance.ToRgba32(this.Configuration, row, bufferSpan);

                for (int x = 0; x < bufferSpan.Length; x++)
                {
                    Rgba32 rgba = bufferSpan[x];

                    // Add the color to the Octree
                    this.octree.AddColor(rgba);
                }
            }

            Span <TPixel> paletteSpan  = this.paletteOwner.GetSpan();
            int           paletteIndex = 0;

            this.octree.Palletize(paletteSpan, this.maxColors, ref paletteIndex);

            // Length of reduced palette + transparency.
            ReadOnlyMemory <TPixel> result = this.paletteOwner.Memory.Slice(0, Math.Min(paletteIndex + 2, this.maxColors));

            this.pixelMap = new EuclideanPixelMap <TPixel>(this.Configuration, result);

            this.palette = result;
        }
Example #2
0
            public void CopyTo(int horizontalFactor, int verticalFactor)
            {
                Block8x8F block = CreateRandomFloatBlock(0, 100);

                var start = new Point(50, 50);

                using (Buffer2D <float> buffer = Configuration.Default.MemoryAllocator.Allocate2D <float>(100, 100, AllocationOptions.Clean))
                {
                    Buffer2DRegion <float> region = buffer.GetRegion(start.X, start.Y, 8 * horizontalFactor, 8 * verticalFactor);
                    block.ScaledCopyTo(region, horizontalFactor, verticalFactor);

                    for (int y = 0; y < 8 * verticalFactor; y++)
                    {
                        for (int x = 0; x < 8 * horizontalFactor; x++)
                        {
                            int yy = y / verticalFactor;
                            int xx = x / horizontalFactor;

                            float expected = block[xx, yy];
                            float actual   = region[x, y];

                            Assert.Equal(expected, actual);
                        }
                    }

                    VerifyAllZeroOutsideSubArea(buffer, start.X, start.Y, horizontalFactor, verticalFactor);
                }
            }
        /// <inheritdoc/>
        public void AddPaletteColors(Buffer2DRegion <TPixel> pixelRegion)
        {
            Rectangle         bounds = pixelRegion.Rectangle;
            Buffer2D <TPixel> source = pixelRegion.Buffer;

            this.Build3DHistogram(source, bounds);
            this.Get3DMoments(this.memoryAllocator);
            this.BuildCube();

            // Slice again since maxColors has been updated since the buffer was created.
            Span <TPixel>         paletteSpan = this.paletteOwner.GetSpan().Slice(0, this.maxColors);
            ReadOnlySpan <Moment> momentsSpan = this.momentsOwner.GetSpan();

            for (int k = 0; k < paletteSpan.Length; k++)
            {
                this.Mark(ref this.colorCube[k], (byte)k);

                Moment moment = Volume(ref this.colorCube[k], momentsSpan);

                if (moment.Weight > 0)
                {
                    ref TPixel color = ref paletteSpan[k];
                    color.FromScaledVector4(moment.Normalize());
                }
            }
Example #4
0
        public void GetReferenceToOrigin(int bufferCapacity)
        {
            this.memoryAllocator.BufferCapacityInBytes = sizeof(int) * bufferCapacity;

            using Buffer2D <int> buffer = this.CreateTestBuffer(20, 30);
            Buffer2DRegion <int> area0  = buffer.GetRegion(6, 8, 10, 10);

            ref int r = ref area0.GetReferenceToOrigin();
Example #5
0
        public void Construct()
        {
            using Buffer2D <int> buffer = this.memoryAllocator.Allocate2D <int>(10, 20);
            var rectangle = new Rectangle(3, 2, 5, 6);
            var area      = new Buffer2DRegion <int>(buffer, rectangle);

            Assert.Equal(buffer, area.Buffer);
            Assert.Equal(rectangle, area.Rectangle);
        }
        private static void PaintWhite(Buffer2DRegion <L8> region)
        {
            var white = new L8(255);

            for (int y = 0; y < region.Height; y++)
            {
                region.GetRowSpan(y).Fill(white);
            }
        }
        /// <inheritdoc/>
        public void AddPaletteColors(Buffer2DRegion <TPixel> pixelRegion)
        {
            Rectangle         bounds = pixelRegion.Rectangle;
            Buffer2D <TPixel> source = pixelRegion.Buffer;

            using (IMemoryOwner <Rgba32> buffer = this.Configuration.MemoryAllocator.Allocate <Rgba32>(bounds.Width))
            {
                Span <Rgba32> bufferSpan = buffer.GetSpan();

                // Loop through each row
                for (int y = bounds.Top; y < bounds.Bottom; y++)
                {
                    Span <TPixel> row = source.DangerousGetRowSpan(y).Slice(bounds.Left, bounds.Width);
                    PixelOperations <TPixel> .Instance.ToRgba32(this.Configuration, row, bufferSpan);

                    for (int x = 0; x < bufferSpan.Length; x++)
                    {
                        Rgba32 rgba = bufferSpan[x];

                        // Add the color to the Octree
                        this.octree.AddColor(rgba);
                    }
                }
            }

            int           paletteIndex = 0;
            Span <TPixel> paletteSpan  = this.paletteOwner.GetSpan();

            // On very rare occasions, (blur.png), the quantizer does not preserve a
            // transparent entry when palletizing the captured colors.
            // To workaround this we ensure the palette ends with the default color
            // for higher bit depths. Lower bit depths will correctly reduce the palette.
            // TODO: Investigate more evenly reduced palette reduction.
            int max = this.maxColors;

            if (this.bitDepth == 8)
            {
                max--;
            }

            this.octree.Palletize(paletteSpan, max, ref paletteIndex);
            ReadOnlyMemory <TPixel> result = this.paletteOwner.Memory.Slice(0, paletteSpan.Length);

            // When called multiple times by QuantizerUtilities.BuildPalette
            // this prevents memory churn caused by reallocation.
            if (this.pixelMap is null)
            {
                this.pixelMap = new EuclideanPixelMap <TPixel>(this.Configuration, result);
            }
            else
            {
                this.pixelMap.Clear(result);
            }

            this.palette = result;
        }
Example #8
0
        public void Indexer(int bufferCapacity, int rx, int ry, int x, int y)
        {
            this.memoryAllocator.BufferCapacityInBytes = sizeof(int) * bufferCapacity;
            using Buffer2D <int> buffer = this.CreateTestBuffer(20, 30);
            var r = new Rectangle(rx, ry, 5, 6);

            Buffer2DRegion <int> region = buffer.GetRegion(r);

            int value    = region[x, y];
            int expected = ((ry + y) * 100) + rx + x;

            Assert.Equal(expected, value);
        }
Example #9
0
        public void GetSubArea()
        {
            using Buffer2D <int> buffer = this.CreateTestBuffer(20, 30);
            Buffer2DRegion <int> area0  = buffer.GetRegion(6, 8, 10, 10);

            Buffer2DRegion <int> area1 = area0.GetSubRegion(4, 4, 5, 5);

            var expectedRect = new Rectangle(10, 12, 5, 5);

            Assert.Equal(buffer, area1.Buffer);
            Assert.Equal(expectedRect, area1.Rectangle);

            int value00 = (12 * 100) + 10;

            Assert.Equal(value00, area1[0, 0]);
        }
Example #10
0
        public ResizeWorker(
            Configuration configuration,
            Buffer2DRegion <TPixel> source,
            PixelConversionModifiers conversionModifiers,
            ResizeKernelMap horizontalKernelMap,
            ResizeKernelMap verticalKernelMap,
            int destWidth,
            Rectangle targetWorkingRect,
            Point targetOrigin)
        {
            this.configuration       = configuration;
            this.source              = source;
            this.sourceRectangle     = source.Rectangle;
            this.conversionModifiers = conversionModifiers;
            this.horizontalKernelMap = horizontalKernelMap;
            this.verticalKernelMap   = verticalKernelMap;
            this.destWidth           = destWidth;
            this.targetWorkingRect   = targetWorkingRect;
            this.targetOrigin        = targetOrigin;

            this.windowBandHeight = verticalKernelMap.MaxDiameter;

            // We need to make sure the working buffer is contiguous:
            int workingBufferLimitHintInBytes = Math.Min(
                configuration.WorkingBufferSizeHintInBytes,
                configuration.MemoryAllocator.GetBufferCapacityInBytes());

            int numberOfWindowBands = ResizeHelper.CalculateResizeWorkerHeightInWindowBands(
                this.windowBandHeight,
                destWidth,
                workingBufferLimitHintInBytes);

            this.workerHeight = Math.Min(this.sourceRectangle.Height, numberOfWindowBands * this.windowBandHeight);

            this.transposedFirstPassBuffer = configuration.MemoryAllocator.Allocate2D <Vector4>(
                this.workerHeight,
                destWidth,
                preferContiguosImageBuffers: true,
                options: AllocationOptions.Clean);

            this.tempRowBuffer    = configuration.MemoryAllocator.Allocate <Vector4>(this.sourceRectangle.Width);
            this.tempColumnBuffer = configuration.MemoryAllocator.Allocate <Vector4>(destWidth);

            this.currentWindow = new RowInterval(0, this.workerHeight);
        }
Example #11
0
            public void Copy1x1Scale()
            {
                Block8x8F block = CreateRandomFloatBlock(0, 100);

                using (Buffer2D <float> buffer = Configuration.Default.MemoryAllocator.Allocate2D <float>(20, 20, AllocationOptions.Clean))
                {
                    Buffer2DRegion <float> region = buffer.GetRegion(5, 10, 8, 8);
                    block.Copy1x1Scale(ref region.GetReferenceToOrigin(), region.Stride);

                    Assert.Equal(block[0, 0], buffer[5, 10]);
                    Assert.Equal(block[1, 0], buffer[6, 10]);
                    Assert.Equal(block[0, 1], buffer[5, 11]);
                    Assert.Equal(block[0, 7], buffer[5, 17]);
                    Assert.Equal(block[63], buffer[12, 17]);

                    VerifyAllZeroOutsideSubArea(buffer, 5, 10);
                }
            }
Example #12
0
        public void GetRowSpan(int bufferCapacity, int rx, int ry, int y, int w, int h)
        {
            this.memoryAllocator.BufferCapacityInBytes = sizeof(int) * bufferCapacity;

            using Buffer2D <int> buffer = this.CreateTestBuffer(20, 30);
            var r = new Rectangle(rx, ry, w, h);

            Buffer2DRegion <int> region = buffer.GetRegion(r);

            Span <int> span = region.GetRowSpan(y);

            Assert.Equal(w, span.Length);

            for (int i = 0; i < w; i++)
            {
                int expected = ((ry + y) * 100) + rx + i;
                int value    = span[i];

                Assert.Equal(expected, value);
            }
        }
 public void Setup()
 {
     this.buffer     = Configuration.Default.MemoryAllocator.Allocate2D <float>(1000, 500);
     this.destRegion = this.buffer.GetRegion(200, 100, 128, 128);
 }
 public void AddPaletteColors(Buffer2DRegion <TPixel> pixelRegion)
 {
 }