示例#1
0
        /// <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());
                }
            }
        /// <inheritdoc/>
        protected override ReadOnlyMemory <TPixel> GetPalette()
        {
            if (this.palette is null)
            {
                this.palette = new TPixel[this.colors];
                ReadOnlySpan <Moment> momentsSpan = this.moments.GetSpan();

                for (int k = 0; k < this.colors; 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 this.palette[k];
                        color.FromScaledVector4(moment.Normalize());
                    }
                }
            }
        /// <inheritdoc/>
        public void BuildPalette(ImageFrame <TPixel> source, Rectangle bounds)
        {
            this.Build3DHistogram(source, bounds);
            this.Get3DMoments(this.memoryAllocator);
            this.BuildCube();

            ReadOnlySpan <Moment> momentsSpan = this.momentsOwner.GetSpan();
            Span <TPixel>         paletteSpan = this.paletteOwner.GetSpan();

            for (int k = 0; k < this.maxColors; 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());
                }
            }