Exemple #1
0
        /// <inheritdoc/>
        protected override void OnApply(ImageFrame <TPixel> source, Rectangle sourceRectangle, Configuration configuration)
        {
            float threshold   = this.Threshold * 255F;
            var   rgba        = default(Rgba32);
            bool  isAlphaOnly = typeof(TPixel) == typeof(Alpha8);

            var interest = Rectangle.Intersect(sourceRectangle, source.Bounds());
            int startY   = interest.Y;
            int endY     = interest.Bottom;
            int startX   = interest.X;
            int endX     = interest.Right;

            // Collect the values before looping so we can reduce our calculation count for identical sibling pixels
            TPixel             sourcePixel   = source[startX, startY];
            TPixel             previousPixel = sourcePixel;
            PixelPair <TPixel> pair          = this.GetClosestPixelPair(ref sourcePixel, this.Palette);

            sourcePixel.ToRgba32(ref rgba);

            // Convert to grayscale using ITU-R Recommendation BT.709 if required
            byte luminance = (byte)(isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B)).Clamp(0, 255);

            for (int y = startY; y < endY; y++)
            {
                Span <TPixel> row = source.GetPixelRowSpan(y);

                for (int x = startX; x < endX; x++)
                {
                    sourcePixel = row[x];

                    // Check if this is the same as the last pixel. If so use that value
                    // rather than calculating it again. This is an inexpensive optimization.
                    if (!previousPixel.Equals(sourcePixel))
                    {
                        pair = this.GetClosestPixelPair(ref sourcePixel, this.Palette);
                        sourcePixel.ToRgba32(ref rgba);
                        luminance = (byte)(isAlphaOnly ? rgba.A : (.2126F * rgba.R) + (.7152F * rgba.G) + (.0722F * rgba.B)).Clamp(0, 255);

                        // Setup the previous pointer
                        previousPixel = sourcePixel;
                    }

                    TPixel transformedPixel = luminance >= threshold ? pair.First : pair.Second;
                    this.Diffuser.Dither(source, sourcePixel, transformedPixel, x, y, startX, startY, endX, endY);
                }
            }
        }
        protected PixelPair <TPixel> GetClosestPixelPair(ref TPixel pixel, TPixel[] colorPalette)
        {
            // Check if the color is in the lookup table
            if (this.cache.ContainsKey(pixel))
            {
                return(this.cache[pixel]);
            }

            // Not found - loop through the palette and find the nearest match.
            float leastDistance       = int.MaxValue;
            float secondLeastDistance = int.MaxValue;
            var   vector = pixel.ToVector4();

            var closest       = default(TPixel);
            var secondClosest = default(TPixel);

            for (int index = 0; index < colorPalette.Length; index++)
            {
                TPixel temp       = colorPalette[index];
                var    tempVector = temp.ToVector4();
                float  distance   = Vector4.Distance(vector, tempVector);

                if (distance < leastDistance)
                {
                    leastDistance = distance;
                    secondClosest = closest;
                    closest       = temp;
                }
                else if (distance < secondLeastDistance)
                {
                    secondLeastDistance = distance;
                    secondClosest       = temp;
                }
            }

            // Pop it into the cache for next time
            var pair = new PixelPair <TPixel>(closest, secondClosest);

            this.cache.Add(pixel, pair);

            return(pair);
        }
Exemple #3
0
 /// <inheritdoc/>
 public bool Equals(PixelPair<TPixel> other)
     => this.First.Equals(other.First) && this.Second.Equals(other.Second);