Пример #1
0
        public void Equals_WhenFalse(byte r1, byte g1, byte b1, byte r2, byte g2, byte b2)
        {
            var a = new Rgb24(r1, g1, b1);
            var b = new Rgb24(r2, g2, b2);

            Assert.False(a.Equals(b));
            Assert.False(a.Equals((object)b));
        }
Пример #2
0
        public void Equals_WhenTrue(byte r, byte g, byte b)
        {
            var x = new Rgb24(r, g, b);
            var y = new Rgb24(r, g, b);

            Assert.True(x.Equals(y));
            Assert.True(x.Equals((object)y));
            Assert.Equal(x.GetHashCode(), y.GetHashCode());
        }
Пример #3
0
        /// <summary>
        /// Processes the de-filtered scanline filling the image pixel data
        /// </summary>
        /// <typeparam name="TPixel">The pixel format.</typeparam>
        /// <param name="defilteredScanline">The de-filtered scanline</param>
        /// <param name="pixels">The image</param>
        private void ProcessDefilteredScanline <TPixel>(byte[] defilteredScanline, ImageFrame <TPixel> pixels)
            where TPixel : struct, IPixel <TPixel>
        {
            var           color   = default(TPixel);
            Span <TPixel> rowSpan = pixels.GetPixelRowSpan(this.currentRow);

            // Trim the first marker byte from the buffer
            var scanlineBuffer = new Span <byte>(defilteredScanline, 1);

            switch (this.pngColorType)
            {
            case PngColorType.Grayscale:
                int         factor       = 255 / ((int)Math.Pow(2, this.header.BitDepth) - 1);
                Span <byte> newScanline1 = ToArrayByBitsLength(scanlineBuffer, this.bytesPerScanline, this.header.BitDepth);

                for (int x = 0; x < this.header.Width; x++)
                {
                    byte intensity = (byte)(newScanline1[x] * factor);
                    if (this.hasTrans && intensity == this.intensityTrans)
                    {
                        color.PackFromRgba32(new Rgba32(intensity, intensity, intensity, 0));
                    }
                    else
                    {
                        color.PackFromRgba32(new Rgba32(intensity, intensity, intensity));
                    }

                    rowSpan[x] = color;
                }

                break;

            case PngColorType.GrayscaleWithAlpha:

                for (int x = 0; x < this.header.Width; x++)
                {
                    int offset = x * this.bytesPerPixel;

                    byte intensity = scanlineBuffer[offset];
                    byte alpha     = scanlineBuffer[offset + this.bytesPerSample];

                    color.PackFromRgba32(new Rgba32(intensity, intensity, intensity, alpha));
                    rowSpan[x] = color;
                }

                break;

            case PngColorType.Palette:

                this.ProcessScanlineFromPalette(scanlineBuffer, rowSpan);

                break;

            case PngColorType.Rgb:

                if (!this.hasTrans)
                {
                    if (this.header.BitDepth == 16)
                    {
                        int length = this.header.Width * 3;
                        using (var compressed = new Buffer <byte>(length))
                        {
                            // TODO: Should we use pack from vector here instead?
                            this.From16BitTo8Bit(scanlineBuffer, compressed, length);
                            PixelOperations <TPixel> .Instance.PackFromRgb24Bytes(compressed, rowSpan, this.header.Width);
                        }
                    }
                    else
                    {
                        PixelOperations <TPixel> .Instance.PackFromRgb24Bytes(scanlineBuffer, rowSpan, this.header.Width);
                    }
                }
                else
                {
                    if (this.header.BitDepth == 16)
                    {
                        int length = this.header.Width * 3;
                        using (var compressed = new Buffer <byte>(length))
                        {
                            // TODO: Should we use pack from vector here instead?
                            this.From16BitTo8Bit(scanlineBuffer, compressed, length);

                            Span <Rgb24> rgb24Span = compressed.Span.NonPortableCast <byte, Rgb24>();
                            for (int x = 0; x < this.header.Width; x++)
                            {
                                ref Rgb24 rgb24  = ref rgb24Span[x];
                                var       rgba32 = default(Rgba32);
                                rgba32.Rgb = rgb24;
                                rgba32.A   = (byte)(rgb24.Equals(this.rgb24Trans) ? 0 : 255);

                                color.PackFromRgba32(rgba32);
                                rowSpan[x] = color;
                            }
                        }
                    }
                    else
                    {
                        Span <Rgb24> rgb24Span = scanlineBuffer.NonPortableCast <byte, Rgb24>();
                        for (int x = 0; x < this.header.Width; x++)
                        {
                            ref Rgb24 rgb24  = ref rgb24Span[x];
                            var       rgba32 = default(Rgba32);
                            rgba32.Rgb = rgb24;
                            rgba32.A   = (byte)(rgb24.Equals(this.rgb24Trans) ? 0 : 255);

                            color.PackFromRgba32(rgba32);
                            rowSpan[x] = color;
                        }
                    }