Example #1
0
        public static GifHeaderDirectory ProcessBytes(string file)
        {
            Com.Drew.Metadata.Metadata metadata = new Com.Drew.Metadata.Metadata();
            InputStream stream = new FileInputStream(file);

            new GifReader().Extract(new Com.Drew.Lang.StreamReader(stream), metadata);
            stream.Close();
            GifHeaderDirectory directory = metadata.GetDirectory <GifHeaderDirectory>();

            NUnit.Framework.Assert.IsNotNull(directory);
            return(directory);
        }
Example #2
0
        public virtual void TestPhotoshopGif()
        {
            GifHeaderDirectory directory = ProcessBytes("Tests/Data/photoshop-8x12-32colors-alpha.gif");

            Sharpen.Tests.IsFalse(directory.HasErrors());
            Sharpen.Tests.AreEqual("89a", directory.GetString(GifHeaderDirectory.TagGifFormatVersion));
            Sharpen.Tests.AreEqual(8, directory.GetInt(GifHeaderDirectory.TagImageWidth));
            Sharpen.Tests.AreEqual(12, directory.GetInt(GifHeaderDirectory.TagImageHeight));
            Sharpen.Tests.AreEqual(32, directory.GetInt(GifHeaderDirectory.TagColorTableSize));
            Sharpen.Tests.IsFalse(directory.GetBoolean(GifHeaderDirectory.TagIsColorTableSorted));
            Sharpen.Tests.AreEqual(5, directory.GetInt(GifHeaderDirectory.TagBitsPerPixel));
            Sharpen.Tests.IsTrue(directory.GetBoolean(GifHeaderDirectory.TagHasGlobalColorTable));
            Sharpen.Tests.AreEqual(8, directory.GetInt(GifHeaderDirectory.TagTransparentColorIndex));
        }
Example #3
0
        public virtual void TestMsPaintGif()
        {
            GifHeaderDirectory directory = ProcessBytes("Tests/Data/mspaint-10x10.gif");

            Sharpen.Tests.IsFalse(directory.HasErrors());
            Sharpen.Tests.AreEqual("89a", directory.GetString(GifHeaderDirectory.TagGifFormatVersion));
            Sharpen.Tests.AreEqual(10, directory.GetInt(GifHeaderDirectory.TagImageWidth));
            Sharpen.Tests.AreEqual(10, directory.GetInt(GifHeaderDirectory.TagImageHeight));
            Sharpen.Tests.AreEqual(256, directory.GetInt(GifHeaderDirectory.TagColorTableSize));
            Sharpen.Tests.IsFalse(directory.GetBoolean(GifHeaderDirectory.TagIsColorTableSorted));
            Sharpen.Tests.AreEqual(8, directory.GetInt(GifHeaderDirectory.TagBitsPerPixel));
            Sharpen.Tests.IsTrue(directory.GetBoolean(GifHeaderDirectory.TagHasGlobalColorTable));
            Sharpen.Tests.AreEqual(0, directory.GetInt(GifHeaderDirectory.TagTransparentColorIndex));
        }
Example #4
0
        public virtual void Extract([NotNull] SequentialReader reader, [NotNull] Com.Drew.Metadata.Metadata metadata)
        {
            GifHeaderDirectory directory = new GifHeaderDirectory();

            metadata.AddDirectory(directory);
            // FILE HEADER
            //
            // 3 - signature: "GIF"
            // 3 - version: either "87a" or "89a"
            //
            // LOGICAL SCREEN DESCRIPTOR
            //
            // 2 - pixel width
            // 2 - pixel height
            // 1 - screen and color map information flags (0 is LSB)
            //       0-2  Size of the global color table
            //       3    Color table sort flag (89a only)
            //       4-6  Color resolution
            //       7    Global color table flag
            // 1 - background color index
            // 1 - pixel aspect ratio
            reader.SetMotorolaByteOrder(false);
            try
            {
                string signature = reader.GetString(3);
                if (!signature.Equals("GIF"))
                {
                    directory.AddError("Invalid GIF file signature");
                    return;
                }
                string version = reader.GetString(3);
                if (!version.Equals(Gif87aVersionIdentifier) && !version.Equals(Gif89aVersionIdentifier))
                {
                    directory.AddError("Unexpected GIF version");
                    return;
                }
                directory.SetString(GifHeaderDirectory.TagGifFormatVersion, version);
                directory.SetInt(GifHeaderDirectory.TagImageWidth, reader.GetUInt16());
                directory.SetInt(GifHeaderDirectory.TagImageHeight, reader.GetUInt16());
                short flags = reader.GetUInt8();
                // First three bits = (BPP - 1)
                int colorTableSize = 1 << ((flags & 7) + 1);
                directory.SetInt(GifHeaderDirectory.TagColorTableSize, colorTableSize);
                if (version.Equals(Gif89aVersionIdentifier))
                {
                    bool isColorTableSorted = (flags & 8) != 0;
                    directory.SetBoolean(GifHeaderDirectory.TagIsColorTableSorted, isColorTableSorted);
                }
                int bitsPerPixel = ((flags & unchecked ((int)(0x70))) >> 4) + 1;
                directory.SetInt(GifHeaderDirectory.TagBitsPerPixel, bitsPerPixel);
                bool hasGlobalColorTable = (flags & unchecked ((int)(0xf))) != 0;
                directory.SetBoolean(GifHeaderDirectory.TagHasGlobalColorTable, hasGlobalColorTable);
                directory.SetInt(GifHeaderDirectory.TagTransparentColorIndex, reader.GetUInt8());
                int aspectRatioByte = reader.GetUInt8();
                if (aspectRatioByte != 0)
                {
                    float pixelAspectRatio = (float)((aspectRatioByte + 15d) / 64d);
                    directory.SetFloat(GifHeaderDirectory.TagPixelAspectRatio, pixelAspectRatio);
                }
            }
            catch (IOException)
            {
                directory.AddError("Unable to read BMP header");
            }
        }
Example #5
0
 public virtual void Extract([NotNull] SequentialReader reader, [NotNull] Com.Drew.Metadata.Metadata metadata)
 {
     GifHeaderDirectory directory = new GifHeaderDirectory();
     metadata.AddDirectory(directory);
     // FILE HEADER
     //
     // 3 - signature: "GIF"
     // 3 - version: either "87a" or "89a"
     //
     // LOGICAL SCREEN DESCRIPTOR
     //
     // 2 - pixel width
     // 2 - pixel height
     // 1 - screen and color map information flags (0 is LSB)
     //       0-2  Size of the global color table
     //       3    Color table sort flag (89a only)
     //       4-6  Color resolution
     //       7    Global color table flag
     // 1 - background color index
     // 1 - pixel aspect ratio
     reader.SetMotorolaByteOrder(false);
     try
     {
         string signature = reader.GetString(3);
         if (!signature.Equals("GIF"))
         {
             directory.AddError("Invalid GIF file signature");
             return;
         }
         string version = reader.GetString(3);
         if (!version.Equals(Gif87aVersionIdentifier) && !version.Equals(Gif89aVersionIdentifier))
         {
             directory.AddError("Unexpected GIF version");
             return;
         }
         directory.SetString(GifHeaderDirectory.TagGifFormatVersion, version);
         directory.SetInt(GifHeaderDirectory.TagImageWidth, reader.GetUInt16());
         directory.SetInt(GifHeaderDirectory.TagImageHeight, reader.GetUInt16());
         short flags = reader.GetUInt8();
         // First three bits = (BPP - 1)
         int colorTableSize = 1 << ((flags & 7) + 1);
         directory.SetInt(GifHeaderDirectory.TagColorTableSize, colorTableSize);
         if (version.Equals(Gif89aVersionIdentifier))
         {
             bool isColorTableSorted = (flags & 8) != 0;
             directory.SetBoolean(GifHeaderDirectory.TagIsColorTableSorted, isColorTableSorted);
         }
         int bitsPerPixel = ((flags & unchecked((int)(0x70))) >> 4) + 1;
         directory.SetInt(GifHeaderDirectory.TagBitsPerPixel, bitsPerPixel);
         bool hasGlobalColorTable = (flags & unchecked((int)(0xf))) != 0;
         directory.SetBoolean(GifHeaderDirectory.TagHasGlobalColorTable, hasGlobalColorTable);
         directory.SetInt(GifHeaderDirectory.TagTransparentColorIndex, reader.GetUInt8());
         int aspectRatioByte = reader.GetUInt8();
         if (aspectRatioByte != 0)
         {
             float pixelAspectRatio = (float)((aspectRatioByte + 15d) / 64d);
             directory.SetFloat(GifHeaderDirectory.TagPixelAspectRatio, pixelAspectRatio);
         }
     }
     catch (IOException)
     {
         directory.AddError("Unable to read BMP header");
     }
 }