Ejemplo n.º 1
0
        private static void CopyFromZYXW <TColor, TPacked>(Image <TColor, TPacked> image)
            where TColor : struct, IPackedPixel <TPacked>
            where TPacked : struct
        {
            using (PixelAccessor <TColor, TPacked> pixels = image.Lock())
            {
                byte red   = 1;
                byte green = 2;
                byte blue  = 3;
                byte alpha = 4;

                using (PixelRow <TColor, TPacked> row = new PixelRow <TColor, TPacked>(1, ComponentOrder.ZYXW))
                {
                    row.Bytes[0] = blue;
                    row.Bytes[1] = green;
                    row.Bytes[2] = red;
                    row.Bytes[3] = alpha;

                    pixels.CopyFrom(row, 0);

                    Color color = (Color)(object)pixels[0, 0];
                    Assert.Equal(red, color.R);
                    Assert.Equal(green, color.G);
                    Assert.Equal(blue, color.B);
                    Assert.Equal(alpha, color.A);
                }
            }
        }
Ejemplo n.º 2
0
        private void RestoreToBackground(ImageBase <TColor, TPacked> frame)
        {
            if (this.restoreArea == null)
            {
                return;
            }

            // Optimization for when the size of the frame is the same as the image size.
            if (this.restoreArea.Value.Width == this.decodedImage.Width &&
                this.restoreArea.Value.Height == this.decodedImage.Height)
            {
                using (PixelAccessor <TColor, TPacked> pixelAccessor = frame.Lock())
                {
                    pixelAccessor.Reset();
                }
            }
            else
            {
                using (PixelRow <TColor, TPacked> emptyRow = new PixelRow <TColor, TPacked>(this.restoreArea.Value.Width, ComponentOrder.XYZW))
                {
                    using (PixelAccessor <TColor, TPacked> pixelAccessor = frame.Lock())
                    {
                        for (int y = this.restoreArea.Value.Top; y < this.restoreArea.Value.Top + this.restoreArea.Value.Height; y++)
                        {
                            pixelAccessor.CopyFrom(emptyRow, y, this.restoreArea.Value.Left);
                        }
                    }
                }
            }

            this.restoreArea = null;
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Reads the 16 bit color palette from the stream
        /// </summary>
        /// <typeparam name="TColor">The pixel format.</typeparam>
        /// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
        /// <param name="pixels">The <see cref="PixelAccessor{TColor, TPacked}"/> to assign the palette to.</param>
        /// <param name="width">The width of the bitmap.</param>
        /// <param name="height">The height of the bitmap.</param>
        /// <param name="inverted">Whether the bitmap is inverted.</param>
        private void ReadRgb16 <TColor, TPacked>(PixelAccessor <TColor, TPacked> pixels, int width, int height, bool inverted)
            where TColor : struct, IPackedPixel <TPacked>
            where TPacked : struct
        {
            // We divide here as we will store the colors in our floating point format.
            const int ScaleR         = 8; // 256/32
            const int ScaleG         = 4; // 256/64
            const int ComponentCount = 2;

            TColor color = default(TColor);

            using (PixelRow <TColor, TPacked> row = new PixelRow <TColor, TPacked>(width, ComponentOrder.XYZ))
            {
                for (int y = 0; y < height; y++)
                {
                    row.Read(this.currentStream);

                    int newY = Invert(y, height, inverted);

                    int offset = 0;
                    for (int x = 0; x < width; x++)
                    {
                        short temp = BitConverter.ToInt16(row.Bytes, offset);

                        byte r = (byte)(((temp & Rgb16RMask) >> 11) * ScaleR);
                        byte g = (byte)(((temp & Rgb16GMask) >> 5) * ScaleG);
                        byte b = (byte)((temp & Rgb16BMask) * ScaleR);

                        color.PackFromBytes(r, g, b, 255);
                        pixels[x, newY] = color;
                        offset         += ComponentCount;
                    }
                }
            }
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Collects a row of true color pixel data.
 /// </summary>
 /// <typeparam name="TColor">The pixel format.</typeparam>
 /// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
 /// <param name="pixels">The image pixel accessor.</param>
 /// <param name="row">The row index.</param>
 /// <param name="rawScanline">The raw scanline.</param>
 private void CollectColorBytes <TColor, TPacked>(PixelAccessor <TColor, TPacked> pixels, int row, byte[] rawScanline)
     where TColor : struct, IPackedPixel <TPacked>
     where TPacked : struct
 {
     // We can use the optimized PixelAccessor here and copy the bytes in unmanaged memory.
     using (PixelRow <TColor, TPacked> pixelRow = new PixelRow <TColor, TPacked>(this.width, rawScanline, this.bytesPerPixel == 4 ? ComponentOrder.XYZW : ComponentOrder.XYZ))
     {
         pixels.CopyTo(pixelRow, row);
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 /// Writes the 24bit color palette to the stream.
 /// </summary>
 /// <typeparam name="TColor">The pixel format.</typeparam>
 /// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
 /// <param name="writer">The <see cref="EndianBinaryWriter"/> containing the stream to write to.</param>
 /// <param name="pixels">The <see cref="PixelAccessor{TColor,TPacked}"/> containing pixel data.</param>
 private void Write24Bit <TColor, TPacked>(EndianBinaryWriter writer, PixelAccessor <TColor, TPacked> pixels)
     where TColor : struct, IPackedPixel <TPacked>
     where TPacked : struct
 {
     using (PixelRow <TColor, TPacked> row = new PixelRow <TColor, TPacked>(pixels.Width, ComponentOrder.ZYX, this.padding))
     {
         for (int y = pixels.Height - 1; y >= 0; y--)
         {
             pixels.CopyTo(row, y);
             writer.Write(row.Bytes);
         }
     }
 }
Ejemplo n.º 6
0
        /// <summary>
        /// Reads the 32 bit color palette from the stream
        /// </summary>
        /// <typeparam name="TColor">The pixel format.</typeparam>
        /// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
        /// <param name="pixels">The <see cref="PixelAccessor{TColor, TPacked}"/> to assign the palette to.</param>
        /// <param name="width">The width of the bitmap.</param>
        /// <param name="height">The height of the bitmap.</param>
        /// <param name="inverted">Whether the bitmap is inverted.</param>
        private void ReadRgb32 <TColor, TPacked>(PixelAccessor <TColor, TPacked> pixels, int width, int height, bool inverted)
            where TColor : struct, IPackedPixel <TPacked>
            where TPacked : struct
        {
            int padding = CalculatePadding(width, 4);

            using (PixelRow <TColor, TPacked> row = new PixelRow <TColor, TPacked>(width, ComponentOrder.ZYXW, padding))
            {
                for (int y = 0; y < height; y++)
                {
                    row.Read(this.currentStream);

                    int newY = Invert(y, height, inverted);
                    pixels.CopyFrom(row, newY);
                }
            }
        }
Ejemplo n.º 7
0
        private static void CopyToZYX <TColor, TPacked>(Image <TColor, TPacked> image)
            where TColor : struct, IPackedPixel <TPacked>
            where TPacked : struct
        {
            using (PixelAccessor <TColor, TPacked> pixels = image.Lock())
            {
                byte red   = 1;
                byte green = 2;
                byte blue  = 3;

                using (PixelRow <TColor, TPacked> row = new PixelRow <TColor, TPacked>(1, ComponentOrder.ZYX))
                {
                    pixels[0, 0] = (TColor)(object)new Color(red, green, blue);

                    pixels.CopyTo(row, 0);

                    Assert.Equal(blue, row.Bytes[0]);
                    Assert.Equal(green, row.Bytes[1]);
                    Assert.Equal(red, row.Bytes[2]);
                }
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Reads the frames colors, mapping indices to colors.
        /// </summary>
        /// <param name="indices">The indexed pixels.</param>
        /// <param name="colorTable">The color table containing the available colors.</param>
        /// <param name="descriptor">The <see cref="GifImageDescriptor"/></param>
        private unsafe void ReadFrameColors(byte[] indices, byte[] colorTable, GifImageDescriptor descriptor)
        {
            int imageWidth  = this.logicalScreenDescriptor.Width;
            int imageHeight = this.logicalScreenDescriptor.Height;

            ImageFrame <TColor, TPacked> previousFrame = null;

            ImageFrame <TColor, TPacked> currentFrame = null;

            ImageBase <TColor, TPacked> image;

            if (this.nextFrame == null)
            {
                image = this.decodedImage;

                image.Quality = colorTable.Length / 3;

                // This initializes the image to become fully transparent because the alpha channel is zero.
                image.InitPixels(imageWidth, imageHeight);
            }
            else
            {
                if (this.graphicsControlExtension != null &&
                    this.graphicsControlExtension.DisposalMethod == DisposalMethod.RestoreToPrevious)
                {
                    previousFrame = this.nextFrame;
                }

                currentFrame = this.nextFrame.Clone();

                image = currentFrame;

                this.RestoreToBackground(image);

                this.decodedImage.Frames.Add(currentFrame);
            }

            if (this.graphicsControlExtension != null && this.graphicsControlExtension.DelayTime > 0)
            {
                image.FrameDelay = this.graphicsControlExtension.DelayTime;
            }

            int i                  = 0;
            int interlacePass      = 0; // The interlace pass
            int interlaceIncrement = 8; // The interlacing line increment
            int interlaceY         = 0; // The current interlaced line

            using (PixelAccessor <TColor, TPacked> pixelAccessor = image.Lock())
            {
                using (PixelRow <TColor, TPacked> pixelRow = new PixelRow <TColor, TPacked>(imageWidth, ComponentOrder.XYZW))
                {
                    for (int y = descriptor.Top; y < descriptor.Top + descriptor.Height; y++)
                    {
                        // Check if this image is interlaced.
                        int writeY; // the target y offset to write to
                        if (descriptor.InterlaceFlag)
                        {
                            // If so then we read lines at predetermined offsets.
                            // When an entire image height worth of offset lines has been read we consider this a pass.
                            // With each pass the number of offset lines changes and the starting line changes.
                            if (interlaceY >= descriptor.Height)
                            {
                                interlacePass++;
                                switch (interlacePass)
                                {
                                case 1:
                                    interlaceY = 4;
                                    break;

                                case 2:
                                    interlaceY         = 2;
                                    interlaceIncrement = 4;
                                    break;

                                case 3:
                                    interlaceY         = 1;
                                    interlaceIncrement = 2;
                                    break;
                                }
                            }

                            writeY = interlaceY + descriptor.Top;

                            interlaceY += interlaceIncrement;
                        }
                        else
                        {
                            writeY = y;
                        }

                        pixelRow.Reset();

                        byte *pixelBase = pixelRow.PixelBase;
                        for (int x = 0; x < descriptor.Width; x++)
                        {
                            int index = indices[i];

                            if (this.graphicsControlExtension == null ||
                                this.graphicsControlExtension.TransparencyFlag == false ||
                                this.graphicsControlExtension.TransparencyIndex != index)
                            {
                                int indexOffset = index * 3;
                                *(pixelBase + 0) = colorTable[indexOffset];
                                *(pixelBase + 1) = colorTable[indexOffset + 1];
                                *(pixelBase + 2) = colorTable[indexOffset + 2];
                                *(pixelBase + 3) = 255;
                            }

                            i++;
                            pixelBase += 4;
                        }

                        pixelAccessor.CopyFrom(pixelRow, writeY, descriptor.Left);
                    }
                }
            }

            if (previousFrame != null)
            {
                this.nextFrame = previousFrame;
                return;
            }

            if (currentFrame == null)
            {
                this.nextFrame = this.decodedImage.ToFrame();
            }
            else
            {
                this.nextFrame = currentFrame.Clone();
            }

            if (this.graphicsControlExtension != null &&
                this.graphicsControlExtension.DisposalMethod == DisposalMethod.RestoreToBackground)
            {
                this.restoreArea = new Rectangle(descriptor.Left, descriptor.Top, descriptor.Width, descriptor.Height);
            }
        }