/// <summary>
    /// Converts animated GIF image to WebP format
    /// </summary>
    private static void ConvertAnimatedGifImageToWebP()
    {
        using (var gifReader = new GifReader("../../../../_Input/Sheep.gif"))
            using (var writer = new WebPWriter("../../../../_Output/ConvertAnimatedGifImageToWebP.webp"))
            {
                for (int i = 0; i < gifReader.Frames.Count; ++i)
                {
                    writer.FrameOptions.Delay = gifReader.Frames[i].Delay * 10;

                    var disposalMethod = gifReader.Frames[i].DisposalMethod;
                    if (disposalMethod == DisposalMethod.None || disposalMethod == DisposalMethod.Background)
                    {
                        writer.FrameOptions.DisposalMethod = gifReader.Frames[i].DisposalMethod;
                    }

                    writer.FrameOptions.Left = gifReader.Frames[i].Left;
                    writer.FrameOptions.Top  = gifReader.Frames[i].Top;

                    var bitmap = gifReader.Frames[i].GetBitmap();
                    bitmap.ColorManagement.Convert(PixelFormat.Format24bppRgb);

                    Pipeline.Run(bitmap + writer);
                }
            }
    }
        internal static GifApplicationExtensionBlock Create(GifReader reader)
        {
            GifApplicationExtensionBlock gifApplicationExtensionBlock = new GifApplicationExtensionBlock(reader);

            if (gifApplicationExtensionBlock.ApplicationIdentifier.Equals("NETSCAPE") && gifApplicationExtensionBlock.ApplicationAuthenticationCode.Equals("2.0"))
            {
                if ((null != gifApplicationExtensionBlock.Bytes))
                {
                    if ((3 == gifApplicationExtensionBlock.Bytes.Length) && (1 == gifApplicationExtensionBlock.Bytes[0]))
                    {
                        gifApplicationExtensionBlock = new GifNetscapeLoopingApplicationExtensionBlock(gifApplicationExtensionBlock);
                    }
                    else if ((5 == gifApplicationExtensionBlock.Bytes.Length) && (2 == gifApplicationExtensionBlock.Bytes[0]))
                    {
                        gifApplicationExtensionBlock = new GifNetscapeBufferingApplicationExtensionBlock(gifApplicationExtensionBlock);
                    }

                }
            }
            else if (gifApplicationExtensionBlock.ApplicationIdentifier.Equals("ANIMEXTS") && gifApplicationExtensionBlock.ApplicationAuthenticationCode.Equals("1.0"))
            {
                if ((null != gifApplicationExtensionBlock.Bytes) && (3 == gifApplicationExtensionBlock.Bytes.Length) && (1 == gifApplicationExtensionBlock.Bytes[0]))
                {
                    gifApplicationExtensionBlock = new GifAnimextsLoopingApplicationExtensionBlock(gifApplicationExtensionBlock);
                }
            }

            return gifApplicationExtensionBlock;
        }
Пример #3
0
    /// <summary>
    /// Resizes animated image in GIF format
    /// </summary>
    private static void ResizeAnimatedGif()
    {
        const int width  = 200;
        const int height = 50;

        using (var reader = new GifReader("../../../../_Output/WriteAnimatedGif.gif"))
        {
            var kX = (float)width / (float)reader.Width;
            var kY = (float)height / (float)reader.Height;

            using (var writer = new GifWriter("../../../../_Output/ResizeAnimatedGif.gif"))
            {
                // Copy general properties of the source file
                writer.BackgroundIndex = reader.BackgroundEntryIndex;
                writer.Palette         = reader.Palette;
                writer.PlaybackCount   = reader.PlaybackCount;

                for (int i = 0; i < reader.Frames.Count; i++)
                {
                    // Read a frame
                    using (var frame = (GifFrame)reader.Frames[i])
                        using (var bitmap = frame.GetBitmap())
                        {
                            // Preserve the original palette
                            ColorPalette palette = bitmap.Palette;
                            // Preserve the original pixel format
                            PixelFormat pixelFormat = bitmap.PixelFormat;

                            // Convert the bitmap to a non-indexed format
                            bitmap.ColorManagement.Convert(Aurigma.GraphicsMill.ColorSpace.Rgb, true, false);

                            // Resize the bitmap in a low quality mode to prevent noise
                            var newWidth  = Math.Max(1, (int)((float)bitmap.Width * kX));
                            var newHeight = Math.Max(1, (int)((float)bitmap.Height * kY));
                            bitmap.Transforms.Resize(newWidth, newHeight, ResizeInterpolationMode.Low);

                            // Return to the indexed format
                            bitmap.ColorManagement.Palette = palette;
                            bitmap.ColorManagement.Convert(pixelFormat);

                            // Copy frame settings
                            writer.FrameOptions.Left           = (ushort)((float)frame.Left * kX);
                            writer.FrameOptions.Top            = (ushort)((float)frame.Top * kY);
                            writer.FrameOptions.Delay          = frame.Delay;
                            writer.FrameOptions.DisposalMethod = frame.DisposalMethod;

                            // Add the frame
                            Pipeline.Run(bitmap + writer);
                        }
                }
            }
        }

        // BUGBUG

        /*
         * Should we move it to the A.08 GIF Format project?
         */
    }
Пример #4
0
 /// <summary>
 /// Reads frames of animated image in GIF format
 /// </summary>
 private static void ReadAnimatedGif()
 {
     using (var reader = new GifReader("../../../../_Output/WriteAnimatedGif.gif"))
     {
         for (int i = 0; i < reader.Frames.Count; i++)
         {
             using (var bitmap = reader.Frames[i].GetBitmap())
             {
                 bitmap.Save("../../../../_Output/ReadAnimatedGif_" + i + ".gif");
             }
         }
     }
 }
Пример #5
0
        internal void Read(GifReader reader)
        {
            String signature = reader.ReadString(3);
            if (signature != "GIF")
            {
                throw new Exception("No GIF signature");
            }

            version = reader.ReadString(3);
            if ((version != "87a") && (version != "89a"))
            {
                throw new Exception("Unsupported GIF version: " + version);
            }
        }
        internal void Read(GifReader reader, GifLogicalScreenDescriptorBlock gifLogicalScreenDescriptorBlock)
        {
            bytes = reader.ReadBytes(gifLogicalScreenDescriptorBlock.GlobalColorTableSize * 3);

            //byte[] globalColorTableBuffer = reader.ReadBytes(globalColorTableSize * 3);

            //globalColorTable = new Color[globalColorTableSize];

            //int baseIndex = 0;
            //for (UInt16 colorIndex = 0; colorIndex < globalColorTableSize; colorIndex++)
            //{
            //    globalColorTable[colorIndex] = Color.FromArgb(globalColorTableBuffer[baseIndex], globalColorTableBuffer[baseIndex + 1], globalColorTableBuffer[baseIndex + 2]);
            //    baseIndex += 3;
            //}
        }
Пример #7
0
    /// <summary>
    /// Resizes animated image in GIF format
    /// </summary>
    private static void ResizesAnimatedGif()
    {
        using (var reader = new GifReader("../../../../_Output/WriteAnimatedGif.gif"))
            using (var writer = new GifWriter("../../../../_Output/ResizesAnimatedGif.gif"))
            {
                // Copy general properties of the source file
                writer.BackgroundIndex = reader.BackgroundEntryIndex;
                writer.Palette         = reader.Palette;
                writer.PlaybackCount   = reader.PlaybackCount;

                for (int i = 0; i < reader.Frames.Count; i++)
                {
                    // Read a frame
                    using (var frame = (GifFrame)reader.Frames[i])
                        using (var bitmap = frame.GetBitmap())
                        {
                            // Preserve the original palette
                            ColorPalette palette = bitmap.Palette;
                            // Preserve the original pixel format
                            PixelFormat pixelFormat = bitmap.PixelFormat;

                            // Convert the bitmap to a non-indexed format
                            bitmap.ColorManagement.Convert(Aurigma.GraphicsMill.ColorSpace.Rgb, true, false);

                            // Resize the bitmap in a high quality mode
                            bitmap.Transforms.Resize(frame.Width / 2, frame.Height / 2, ResizeInterpolationMode.High);

                            // Return to the indexed format
                            bitmap.Palette = palette;
                            bitmap.ColorManagement.Convert(pixelFormat);

                            // Copy frame delay
                            writer.FrameOptions.Delay = frame.Delay;

                            // Add the frame
                            Pipeline.Run(bitmap + writer);
                        }
                }
            }

        // BUGBUG

        /*
         * Может тоже в GIF Format перенести?
         */
    }
Пример #8
0
        public void GifReaderObject_Success()
        {
            DecodedImage decodedImage;
            IImageReader reader = null;

            string image = "anil.gif";

            string format = image.Substring(image.IndexOf('.') + 1);

            if (format.Equals("gif"))
            {
                reader = new GifReader(image);
            }

            Assert.NotNull(reader);
            Assert.IsType <GifReader>(reader);
        }
Пример #9
0
        /// <summary>
        /// 设置UITexture.
        /// </summary>
        /// <param name="url"></param>
        /// <param name="UITexture"></param>
        /// <returns></returns>
        public static IEnumerator SetTextureToUrl(string url, UITexture varTexture)
        {
            using (WWW www = new WWW(url))
            {
                //挂起程序段,等资源下载完成后,继续执行下去;
                yield return(www);

                if (string.IsNullOrEmpty(www.error))
                {
                    if (varTexture != null && www.texture != null)
                    {
                        GifReader.GetImage(varTexture, www.bytes, string.Empty);
                    }
                }

                www.Dispose();
            }
        }
Пример #10
0
 internal GifExtensionBlock(Byte label, GifReader reader)
     : base(GifBlockType.Extension)
 {
     this.label = label;
     Read(reader);
 }
 internal GifPlainTextExtensionBlock(GifReader reader)
     : base(GifBlockType.PlainTextExtension)
 {
     Read(reader);
 }
        internal void Read(GifReader reader)
        {
            Byte size = reader.ReadByte();
            if (size != 12)
            {
                System.Diagnostics.Debug.WriteLine("Wrong size of " + ToString());
            }

            left = reader.ReadUInt16();
            top = reader.ReadUInt16();
            width = reader.ReadUInt16();
            height = reader.ReadUInt16();

            cellWidth = reader.ReadByte();
            cellHeight = reader.ReadByte();

            foregroundColor = reader.ReadByte();
            backgroundColor = reader.ReadByte();

            text = reader.ReadTextSubBlocks();
        }
 internal GifGraphicControExtensionBlock(GifReader reader)
     : base(GifBlockType.GraphicControlExtension)
 {
     Read(reader);
 }
 internal GifApplicationExtensionBlock(GifReader reader)
     : base(GifBlockType.ApplicationExtension)
 {
     Read(reader);
 }
 internal GifLogicalScreenDescriptorBlock(GifReader reader)
     : base(GifBlockType.LogicalScreenDescriptor)
 {
     Read(reader);
 }
 internal GifGlobalColorTableBlock(GifReader reader, GifLogicalScreenDescriptorBlock gifLogicalScreenDescriptorBlock)
     : base(GifBlockType.GlobalColorTable)
 {
     Read(reader, gifLogicalScreenDescriptorBlock);
 }
 internal void Read(GifReader reader, GifImageDescriptorBlock gifImageDescriptorBlock)
 {
     bytes = reader.ReadBytes(gifImageDescriptorBlock.LocalColorTableSize * 3);
 }
 internal GifLocalColorTableBlock(GifReader reader, GifImageDescriptorBlock gifImageDescriptorBlock)
     : base(GifBlockType.LocalColorTable)
 {
     Read(reader, gifImageDescriptorBlock);
 }
        internal void Read(GifReader reader)
        {
            lzwMinimumCodeSize = reader.ReadByte();

            imageData = reader.ReadBinarySubBlocks();
        }
 internal GifTableBasedImageDataBlock(GifReader reader)
     : base(GifBlockType.TableBasedImageData)
 {
     Read(reader);
 }
Пример #21
0
 internal void Read(GifReader reader)
 {
     bytes = reader.ReadBinarySubBlocks();
 }
 internal GifImageDescriptorBlock(GifReader reader)
     : base(GifBlockType.ImageDescriptor)
 {
     Read(reader);
 }
        internal void Read(GifReader reader)
        {
            width = reader.ReadUInt16();
            height = reader.ReadUInt16();

            Byte flags = reader.ReadByte();
            globalColorTableFlag = 0x80 == (flags & 0x80);
            colorResolution = (Byte)(((flags & 0x70) >> 4) + 1);
            globalColorTableSorted = 0x08 == (flags & 0x08);
            globalColorTableSize = (UInt16)(2 << (flags & 0x07));

            backgroundColorIndex = reader.ReadByte();
            pixelAspectRatio = reader.ReadByte();
        }
Пример #24
0
        public void Read(Stream stream)
        {
            blocks.Clear();

            // reader

            GifReader reader = new GifReader(stream);

            // Header

            AddBlock(new GifHeaderBlock(reader));

            // Logical Screen Descriptor

            GifLogicalScreenDescriptorBlock gifLogicalScreenDescriptorBlock = new GifLogicalScreenDescriptorBlock(reader);
            AddBlock(gifLogicalScreenDescriptorBlock);

            // Global Color Table

            if (gifLogicalScreenDescriptorBlock.GlobalColorTableFlag)
            {
                AddBlock(new GifGlobalColorTableBlock(reader, gifLogicalScreenDescriptorBlock));
            }

            // Segments

            while (true)
            {
                Byte identifier = reader.ReadByte();
                switch (identifier)
                {
                    // Frames

                    case 0x2C:
                        GifImageDescriptorBlock gifImageDescriptorBlock = new GifImageDescriptorBlock(reader);
                        AddBlock(gifImageDescriptorBlock);

                        if (gifImageDescriptorBlock.LocalColorTableFlag)
                        {
                            AddBlock(new GifLocalColorTableBlock(reader, gifImageDescriptorBlock));
                        }

                        AddBlock(new GifTableBasedImageDataBlock(reader));
                        break;

                    // Extensions

                    case 0x21:
                        Byte label = reader.ReadByte();

                        switch (label)
                        {
                            case 0xF9:
                                AddBlock(new GifGraphicControExtensionBlock(reader));
                                break;

                            case 0xFE:
                                AddBlock(new GifCommentExtensionBlock(reader));
                                break;

                            case 0x01:
                                AddBlock(new GifPlainTextExtensionBlock(reader));
                                break;

                            case 0xFF:
                                AddBlock(GifApplicationExtensionBlock.Create(reader));
                                break;

                            default:
                                AddBlock(new GifExtensionBlock(label, reader));
                                break;
                        }
                        break;

                    // Trailer

                    case 0x3B:
                        AddBlock(new GifTrailerBlock());
                        return;

                    default:
                        throw new Exception("Unknown segment identifier: 0x" + identifier.ToString("X2"));
                }
            }
        }
        internal void Read(GifReader reader)
        {
            Byte size = reader.ReadByte();
            if (size != 11)
            {
                System.Diagnostics.Debug.WriteLine("Wrong size of " + ToString());
            }

            applicationIdentifier = reader.ReadString(8);
            applicationAuthenticationCode = reader.ReadString(3);

            bytes = reader.ReadBinarySubBlocks();
        }
        internal void Read(GifReader reader)
        {
            Byte size = reader.ReadByte();
            if (size != 4)
            {
                System.Diagnostics.Debug.WriteLine("Wrong size of " + ToString());
            }

            Byte flags = reader.ReadByte();
            disposalMethod = (GifDisposalMethod)((flags & 0x1C) >> 2);
            userInputExpected = 0x02 == (flags & 0x02);
            transparency = 0x01 == (flags & 0x01);

            delay = reader.ReadUInt16();
            transparencyIndex = reader.ReadByte();

            Byte terminator = reader.ReadByte();
            if (terminator != 0x00)
            {
                throw new Exception("Unknown extension terminator: 0x" + terminator.ToString("X2"));
            }
        }
 internal GifCommentExtensionBlock(GifReader reader)
     : base(GifBlockType.CommentExtension)
 {
     Read(reader);
 }
Пример #28
0
 internal GifHeaderBlock(GifReader reader)
     : base(GifBlockType.Header)
 {
     Read(reader);
 }
 internal void Read(GifReader reader)
 {
     comment = reader.ReadTextSubBlocks();
 }
        internal void Read(GifReader reader)
        {
            left = reader.ReadUInt16();
            top = reader.ReadUInt16();

            width = reader.ReadUInt16();
            height = reader.ReadUInt16();

            Byte flags = reader.ReadByte();
            localColorTableFlag = 0x80 == (flags & 0x80);
            interlaced = 0x40 == (flags & 0x40);
            localColorTableSorted = 0x20 == (flags & 0x20);
            localColorTableSize = (UInt16)(2 << (flags & 0x07));
        }