public virtual void Test10x12x16bitCMYK()
        {
            PsdHeaderDirectory directory = ProcessBytes("Tests/Data/10x12x16bit-CMYK.psd");

            Sharpen.Tests.AreEqual(10, directory.GetInt(PsdHeaderDirectory.TagImageWidth));
            Sharpen.Tests.AreEqual(12, directory.GetInt(PsdHeaderDirectory.TagImageHeight));
            Sharpen.Tests.AreEqual(16, directory.GetInt(PsdHeaderDirectory.TagBitsPerChannel));
            Sharpen.Tests.AreEqual(4, directory.GetInt(PsdHeaderDirectory.TagChannelCount));
            Sharpen.Tests.AreEqual(4, directory.GetInt(PsdHeaderDirectory.TagColorMode));
        }
        public virtual void Test8x8x8bitGrayscale()
        {
            PsdHeaderDirectory directory = ProcessBytes("Tests/Data/8x4x8bit-Grayscale.psd");

            Sharpen.Tests.AreEqual(8, directory.GetInt(PsdHeaderDirectory.TagImageWidth));
            Sharpen.Tests.AreEqual(4, directory.GetInt(PsdHeaderDirectory.TagImageHeight));
            Sharpen.Tests.AreEqual(8, directory.GetInt(PsdHeaderDirectory.TagBitsPerChannel));
            Sharpen.Tests.AreEqual(1, directory.GetInt(PsdHeaderDirectory.TagChannelCount));
            Sharpen.Tests.AreEqual(1, directory.GetInt(PsdHeaderDirectory.TagColorMode));
        }
        public static PsdHeaderDirectory ProcessBytes(string file)
        {
            Com.Drew.Metadata.Metadata metadata         = new Com.Drew.Metadata.Metadata();
            RandomAccessFile           randomAccessFile = new RandomAccessFile(new FilePath(file), "r");

            new PsdReader().Extract(new RandomAccessFileReader(randomAccessFile), metadata);
            randomAccessFile.Close();
            PsdHeaderDirectory directory = metadata.GetDirectory <PsdHeaderDirectory>();

            NUnit.Framework.Assert.IsNotNull(directory);
            return(directory);
        }
Example #4
0
        public static PsdHeaderDirectory ProcessBytes([NotNull] string file)
        {
            Com.Drew.Metadata.Metadata metadata = new Com.Drew.Metadata.Metadata();
            InputStream stream = new FileInputStream(new FilePath(file));

            try
            {
                new PsdReader().Extract(new Com.Drew.Lang.StreamReader(stream), metadata);
            }
            catch (Exception e)
            {
                stream.Close();
                throw;
            }
            PsdHeaderDirectory directory = metadata.GetFirstDirectoryOfType <PsdHeaderDirectory>();

            NUnit.Framework.Assert.IsNotNull(directory);
            return(directory);
        }
        public virtual void Extract(RandomAccessReader reader, Com.Drew.Metadata.Metadata metadata)
        {
            PsdHeaderDirectory directory = metadata.GetOrCreateDirectory <PsdHeaderDirectory>();

            try
            {
                int signature = reader.GetInt32(0);
                if (signature != unchecked ((int)(0x38425053)))
                {
                    directory.AddError("Invalid PSD file signature");
                    return;
                }
                int version = reader.GetUInt16(4);
                if (version != 1 && version != 2)
                {
                    directory.AddError("Invalid PSD file version (must be 1 or 2)");
                    return;
                }
                // 6 reserved bytes are skipped here.  They should be zero.
                int channelCount = reader.GetUInt16(12);
                directory.SetInt(PsdHeaderDirectory.TagChannelCount, channelCount);
                // even though this is probably an unsigned int, the max height in practice is 300,000
                int imageHeight = reader.GetInt32(14);
                directory.SetInt(PsdHeaderDirectory.TagImageHeight, imageHeight);
                // even though this is probably an unsigned int, the max width in practice is 300,000
                int imageWidth = reader.GetInt32(18);
                directory.SetInt(PsdHeaderDirectory.TagImageWidth, imageWidth);
                int bitsPerChannel = reader.GetUInt16(22);
                directory.SetInt(PsdHeaderDirectory.TagBitsPerChannel, bitsPerChannel);
                int colorMode = reader.GetUInt16(24);
                directory.SetInt(PsdHeaderDirectory.TagColorMode, colorMode);
            }
            catch (IOException)
            {
                directory.AddError("Unable to read PSD header");
            }
        }
Example #6
0
        public virtual void Extract([NotNull] SequentialReader reader, [NotNull] Com.Drew.Metadata.Metadata metadata)
        {
            PsdHeaderDirectory directory = new PsdHeaderDirectory();

            metadata.AddDirectory(directory);
            // FILE HEADER SECTION
            try
            {
                int signature = reader.GetInt32();
                if (signature != unchecked ((int)(0x38425053)))
                {
                    // "8BPS"
                    directory.AddError("Invalid PSD file signature");
                    return;
                }
                int version = reader.GetUInt16();
                if (version != 1 && version != 2)
                {
                    directory.AddError("Invalid PSD file version (must be 1 or 2)");
                    return;
                }
                // 6 reserved bytes are skipped here.  They should be zero.
                reader.Skip(6);
                int channelCount = reader.GetUInt16();
                directory.SetInt(PsdHeaderDirectory.TagChannelCount, channelCount);
                // even though this is probably an unsigned int, the max height in practice is 300,000
                int imageHeight = reader.GetInt32();
                directory.SetInt(PsdHeaderDirectory.TagImageHeight, imageHeight);
                // even though this is probably an unsigned int, the max width in practice is 300,000
                int imageWidth = reader.GetInt32();
                directory.SetInt(PsdHeaderDirectory.TagImageWidth, imageWidth);
                int bitsPerChannel = reader.GetUInt16();
                directory.SetInt(PsdHeaderDirectory.TagBitsPerChannel, bitsPerChannel);
                int colorMode = reader.GetUInt16();
                directory.SetInt(PsdHeaderDirectory.TagColorMode, colorMode);
            }
            catch (IOException)
            {
                directory.AddError("Unable to read PSD header");
                return;
            }
            // COLOR MODE DATA SECTION
            try
            {
                long sectionLength = reader.GetUInt32();

                /*
                 * Only indexed color and duotone (see the mode field in the File header section) have color mode data.
                 * For all other modes, this section is just the 4-byte length field, which is set to zero.
                 *
                 * Indexed color images: length is 768; color data contains the color table for the image,
                 *                       in non-interleaved order.
                 * Duotone images: color data contains the duotone specification (the format of which is not documented).
                 *                 Other applications that read Photoshop files can treat a duotone image as a gray	image,
                 *                 and just preserve the contents of the duotone information when reading and writing the
                 *                 file.
                 */
                reader.Skip(sectionLength);
            }
            catch (IOException)
            {
                return;
            }
            // IMAGE RESOURCES SECTION
            try
            {
                long sectionLength = reader.GetUInt32();
                System.Diagnostics.Debug.Assert((sectionLength <= int.MaxValue));
                new PhotoshopReader().Extract(reader, (int)sectionLength, metadata);
            }
            catch (IOException)
            {
            }
        }
Example #7
0
 public virtual void Extract([NotNull] SequentialReader reader, [NotNull] Com.Drew.Metadata.Metadata metadata)
 {
     PsdHeaderDirectory directory = new PsdHeaderDirectory();
     metadata.AddDirectory(directory);
     // FILE HEADER SECTION
     try
     {
         int signature = reader.GetInt32();
         if (signature != unchecked((int)(0x38425053)))
         {
             // "8BPS"
             directory.AddError("Invalid PSD file signature");
             return;
         }
         int version = reader.GetUInt16();
         if (version != 1 && version != 2)
         {
             directory.AddError("Invalid PSD file version (must be 1 or 2)");
             return;
         }
         // 6 reserved bytes are skipped here.  They should be zero.
         reader.Skip(6);
         int channelCount = reader.GetUInt16();
         directory.SetInt(PsdHeaderDirectory.TagChannelCount, channelCount);
         // even though this is probably an unsigned int, the max height in practice is 300,000
         int imageHeight = reader.GetInt32();
         directory.SetInt(PsdHeaderDirectory.TagImageHeight, imageHeight);
         // even though this is probably an unsigned int, the max width in practice is 300,000
         int imageWidth = reader.GetInt32();
         directory.SetInt(PsdHeaderDirectory.TagImageWidth, imageWidth);
         int bitsPerChannel = reader.GetUInt16();
         directory.SetInt(PsdHeaderDirectory.TagBitsPerChannel, bitsPerChannel);
         int colorMode = reader.GetUInt16();
         directory.SetInt(PsdHeaderDirectory.TagColorMode, colorMode);
     }
     catch (IOException)
     {
         directory.AddError("Unable to read PSD header");
         return;
     }
     // COLOR MODE DATA SECTION
     try
     {
         long sectionLength = reader.GetUInt32();
     /*
      * Only indexed color and duotone (see the mode field in the File header section) have color mode data.
      * For all other modes, this section is just the 4-byte length field, which is set to zero.
      *
      * Indexed color images: length is 768; color data contains the color table for the image,
      *                       in non-interleaved order.
      * Duotone images: color data contains the duotone specification (the format of which is not documented).
      *                 Other applications that read Photoshop files can treat a duotone image as a gray	image,
      *                 and just preserve the contents of the duotone information when reading and writing the
      *                 file.
      */
         reader.Skip(sectionLength);
     }
     catch (IOException)
     {
         return;
     }
     // IMAGE RESOURCES SECTION
     try
     {
         long sectionLength = reader.GetUInt32();
         System.Diagnostics.Debug.Assert((sectionLength <= int.MaxValue));
         new PhotoshopReader().Extract(reader, (int)sectionLength, metadata);
     }
     catch (IOException)
     {
     }
 }