Exemplo n.º 1
0
            internal void LoadPixelData(BinaryReverseReader reader)
            {
                Debug.WriteLine("Channel.LoadPixelData started at " + reader.BaseStream.Position.ToString());

                m_data = reader.ReadBytes((int)Length);

                using (BinaryReverseReader readerImg = DataReader)
                {
                    m_imageCompression = (ImageCompression)readerImg.ReadInt16();

                    int bytesPerRow = 0;

                    switch (m_layer.PsdFile.Depth)
                    {
                    case 1:
                        bytesPerRow = m_layer.m_rect.Width;    //NOT Shure
                        break;

                    case 8:
                        bytesPerRow = m_layer.m_rect.Width;
                        break;

                    case 16:
                        bytesPerRow = m_layer.m_rect.Width * 2;
                        break;
                    }

                    m_imageData = new byte[m_layer.m_rect.Height * bytesPerRow];

                    switch (m_imageCompression)
                    {
                    case ImageCompression.Raw:
                        readerImg.Read(m_imageData, 0, m_imageData.Length);
                        break;

                    case ImageCompression.Rle:
                    {
                        int[] rowLenghtList = new int[m_layer.m_rect.Height];
                        for (int i = 0; i < rowLenghtList.Length; i++)
                        {
                            rowLenghtList[i] = readerImg.ReadInt16();
                        }

                        for (int i = 0; i < m_layer.m_rect.Height; i++)
                        {
                            int rowIndex = i * m_layer.m_rect.Width;
                            RleHelper.DecodedRow(readerImg.BaseStream, m_imageData, rowIndex, bytesPerRow);

                            //if (rowLenghtList[i] % 2 == 1)
                            //    readerImg.ReadByte();
                        }
                    }
                    break;

                    default:
                        break;
                    }
                }
            }
Exemplo n.º 2
0
        public ResolutionInfo(ImageResource imgRes)
            : base(imgRes)
        {
            BinaryReverseReader reader = imgRes.DataReader;

            this.m_hRes      = reader.ReadInt16();
            this.m_hResUnit  = (ResUnit)reader.ReadInt32();
            this.m_widthUnit = (Unit)reader.ReadInt16();

            this.m_vRes       = reader.ReadInt16();
            this.m_vResUnit   = (ResUnit)reader.ReadInt32();
            this.m_heightUnit = (Unit)reader.ReadInt16();

            reader.Close();
        }
Exemplo n.º 3
0
        } //end Load()

        /// <summary>
        /// Loads up the Layers of the supplied PSD file
        /// </summary>
        private void LoadLayers(BinaryReverseReader reader)
        {
            Debug.WriteLine("LoadLayers started at " + reader.BaseStream.Position.ToString());

            uint layersInfoSectionLength = reader.ReadUInt32();

            if (layersInfoSectionLength <= 0)
            {
                return;
            }

            long startPosition = reader.BaseStream.Position;

            short numberOfLayers = reader.ReadInt16();

            // If <0, then number of layers is absolute value,
            // and the first alpha channel contains the transparency data for
            // the merged result.
            if (numberOfLayers < 0)
            {
                AbsoluteAlpha  = true;
                numberOfLayers = Math.Abs(numberOfLayers);
            }

            m_layers.Clear();

            if (numberOfLayers == 0)
            {
                return;
            }

            for (int i = 0; i < numberOfLayers; i++)
            {
                m_layers.Add(new Layer(reader, this));
            }

            foreach (Layer layer in m_layers)
            {
                foreach (Layer.Channel channel in layer.Channels)
                {
                    if (channel.ID != -2)
                    {
                        channel.LoadPixelData(reader);
                    }
                }
                layer.MaskData.LoadPixelData(reader);
            }


            if (reader.BaseStream.Position % 2 == 1)
            {
                reader.ReadByte();
            }

            // make sure we are not on a wrong offset, so set the stream position
            // manually
            reader.BaseStream.Position = startPosition + layersInfoSectionLength;
        }
Exemplo n.º 4
0
            internal Channel(BinaryReverseReader reader, Layer layer)
            {
                Debug.WriteLine("Channel started at " + reader.BaseStream.Position.ToString());

                m_id   = reader.ReadInt16();
                Length = reader.ReadInt32();

                m_layer = layer;
            }
Exemplo n.º 5
0
        public Thumbnail(ImageResource imgRes)
            : base(imgRes)
        {
            using (BinaryReverseReader reader = DataReader)
            {
                int   format         = reader.ReadInt32();
                int   width          = reader.ReadInt32();
                int   height         = reader.ReadInt32();
                int   widthBytes     = reader.ReadInt32();
                int   size           = reader.ReadInt32();
                int   compressedSize = reader.ReadInt32();
                short bitPerPixel    = reader.ReadInt16();
                short planes         = reader.ReadInt16();

                if (format == 1)
                {
                    byte[] imgData = reader.ReadBytes((int)(reader.BaseStream.Length - reader.BaseStream.Position));

                    using (MemoryStream strm = new MemoryStream(imgData))
                    {
                        m_thumbnailImage = (Bitmap)(Bitmap.FromStream(strm).Clone());
                    }

                    if (this.ID == 1033)
                    {
                        //// BGR
                        //for(int y=0;y<m_thumbnailImage.Height;y++)
                        //  for (int x = 0; x < m_thumbnailImage.Width; x++)
                        //  {
                        //    Color c=m_thumbnailImage.GetPixel(x,y);
                        //    Color c2=Color.FromArgb(c.B, c.G, c.R);
                        //    m_thumbnailImage.SetPixel(x, y, c);
                        //  }
                    }
                }
                else
                {
                    m_thumbnailImage = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                }
            }
        }
Exemplo n.º 6
0
        //////////////////////////////////////////////////////////////////

        public ImageResource(BinaryReverseReader reader)
        {
            m_osType = new string(reader.ReadChars(4));
            if (m_osType != "8BIM" && m_osType != "MeSa")
            {
                throw new InvalidOperationException("Could not read an image resource");
            }

            m_id   = reader.ReadInt16();
            m_name = reader.ReadPascalString();

            uint settingLength = reader.ReadUInt32();

            m_data = reader.ReadBytes((int)settingLength);

            if (reader.BaseStream.Position % 2 == 1)
            {
                reader.ReadByte();
            }
        }
Exemplo n.º 7
0
        public void Load(string filename)
        {
            using (FileStream stream = new FileStream(filename, FileMode.Open)) {
                //binary reverse reader reads data types in big-endian format.
                BinaryReverseReader reader = new BinaryReverseReader(stream);

                #region "Headers"
                //The headers area is used to check for a valid PSD file
                Debug.WriteLine("LoadHeader started at " + reader.BaseStream.Position.ToString());

                string signature = new string(reader.ReadChars(4));
                if (signature != "8BPS")
                {
                    throw new IOException("Bad or invalid file stream supplied");
                }

                //get the version number, should be 1 always
                if ((m_version = reader.ReadInt16()) != 1)
                {
                    throw new IOException("Invalid version number supplied");
                }

                //get rid of the 6 bytes reserverd in PSD format
                reader.BaseStream.Position += 6;

                //get the rest of the information from the PSD file.
                //Everytime ReadInt16() is called, it reads 2 bytes.
                //Everytime ReadInt32() is called, it reads 4 bytes.
                m_channels  = reader.ReadInt16();
                m_rows      = reader.ReadInt32();
                m_columns   = reader.ReadInt32();
                m_depth     = reader.ReadInt16();
                m_colorMode = (ColorModes)reader.ReadInt16();

                //by end of headers, the reader has read 26 bytes into the file.
                #endregion //End Headers

                #region "ColorModeData"
                /// <summary>
                /// If ColorMode is ColorModes.Indexed, the following 768 bytes will contain
                /// a 256-color palette. If the ColorMode is ColorModes.Duotone, the data
                /// following presumably consists of screen parameters and other related information.
                /// Unfortunately, it is intentionally not documented by Adobe, and non-Photoshop
                /// readers are advised to treat duotone images as gray-scale images.
                /// </summary>
                Debug.WriteLine("LoadColorModeData started at " + reader.BaseStream.Position.ToString());

                uint paletteLength = reader.ReadUInt32(); //readUint32() advances the reader 4 bytes.
                if (paletteLength > 0)
                {
                    ColorModeData = reader.ReadBytes((int)paletteLength);
                }
                #endregion //End ColorModeData


                #region "Loading Image Resources"
                //This part takes extensive use of classes that I didn't write therefore
                //I can't document much on what they do.

                Debug.WriteLine("LoadingImageResources started at " + reader.BaseStream.Position.ToString());

                m_imageResources.Clear();

                uint imgResLength = reader.ReadUInt32();
                if (imgResLength <= 0)
                {
                    return;
                }

                long startPosition = reader.BaseStream.Position;

                while ((reader.BaseStream.Position - startPosition) < imgResLength)
                {
                    ImageResource imgRes = new ImageResource(reader);

                    ResourceIDs resID = (ResourceIDs)imgRes.ID;
                    switch (resID)
                    {
                    case ResourceIDs.ResolutionInfo:
                        imgRes = new ResolutionInfo(imgRes);
                        break;

                    case ResourceIDs.Thumbnail1:
                    case ResourceIDs.Thumbnail2:
                        imgRes = new Thumbnail(imgRes);
                        break;

                    case ResourceIDs.AlphaChannelNames:
                        imgRes = new AlphaChannels(imgRes);
                        break;
                    }

                    m_imageResources.Add(imgRes);
                }
                // make sure we are not on a wrong offset, so set the stream position
                // manually
                reader.BaseStream.Position = startPosition + imgResLength;

                #endregion //End LoadingImageResources


                #region "Layer and Mask Info"
                //We are gonna load up all the layers and masking of the PSD now.
                Debug.WriteLine("LoadLayerAndMaskInfo - Part1 started at " + reader.BaseStream.Position.ToString());
                uint layersAndMaskLength = reader.ReadUInt32();

                if (layersAndMaskLength <= 0)
                {
                    return;
                }

                //new start position
                startPosition = reader.BaseStream.Position;

                //Lets start by loading up all the layers
                LoadLayers(reader);
                //we are done the layers, load up the masks
                LoadGlobalLayerMask(reader);

                // make sure we are not on a wrong offset, so set the stream position
                // manually
                reader.BaseStream.Position = startPosition + layersAndMaskLength;
                #endregion //End Layer and Mask info

                #region "Loading Final Image"

                //we have loaded up all the information from the PSD file
                //into variables we can use later on.

                //lets finish loading the raw data that defines the image
                //in the picture.

                Debug.WriteLine("LoadImage started at " + reader.BaseStream.Position.ToString());

                m_imageCompression = (ImageCompression)reader.ReadInt16();

                m_imageData = new byte[m_channels][];

                //---------------------------------------------------------------

                if (m_imageCompression == ImageCompression.Rle)
                {
                    // The RLE-compressed data is proceeded by a 2-byte data count for each row in the data,
                    // which we're going to just skip.
                    reader.BaseStream.Position += m_rows * m_channels * 2;
                }

                //---------------------------------------------------------------

                int bytesPerRow = 0;

                switch (m_depth)
                {
                case 1:
                    bytesPerRow = m_columns;    //NOT Shure
                    break;

                case 8:
                    bytesPerRow = m_columns;
                    break;

                case 16:
                    bytesPerRow = m_columns * 2;
                    break;
                }

                //---------------------------------------------------------------

                for (int ch = 0; ch < m_channels; ch++)
                {
                    m_imageData[ch] = new byte[m_rows * bytesPerRow];

                    switch (m_imageCompression)
                    {
                    case ImageCompression.Raw:
                        reader.Read(m_imageData[ch], 0, m_imageData[ch].Length);
                        break;

                    case ImageCompression.Rle: {
                        for (int i = 0; i < m_rows; i++)
                        {
                            int rowIndex = i * m_columns;
                            RleHelper.DecodedRow(reader.BaseStream, m_imageData[ch], rowIndex, bytesPerRow);
                        }
                    }
                    break;

                    default:
                        break;
                    }
                }

                #endregion //End LoadingFinalImage
            }
        } //end Load()
Exemplo n.º 8
0
            internal Channel(BinaryReverseReader reader, Layer layer)
            {
                Debug.WriteLine("Channel started at " + reader.BaseStream.Position.ToString());

                m_id = reader.ReadInt16();
                Length = reader.ReadInt32();

                m_layer = layer;
            }
Exemplo n.º 9
0
            internal void LoadPixelData(BinaryReverseReader reader)
            {
                Debug.WriteLine("Mask.LoadPixelData started at " + reader.BaseStream.Position.ToString());

                if (m_rect.IsEmpty || m_layer.SortedChannels.ContainsKey(-2) == false)
                {
                    return;
                }

                Channel maskChannel = m_layer.SortedChannels[-2];

                maskChannel.Data = reader.ReadBytes((int)maskChannel.Length);

                using (BinaryReverseReader readerImg = maskChannel.DataReader)
                {
                    maskChannel.ImageCompression = (ImageCompression)readerImg.ReadInt16();

                    int bytesPerRow = 0;

                    switch (m_layer.PsdFile.Depth)
                    {
                    case 1:
                        bytesPerRow = m_rect.Width;    //NOT Shure
                        break;

                    case 8:
                        bytesPerRow = m_rect.Width;
                        break;

                    case 16:
                        bytesPerRow = m_rect.Width * 2;
                        break;
                    }

                    maskChannel.ImageData = new byte[m_rect.Height * bytesPerRow];
                    // Fill Array
                    for (int i = 0; i < maskChannel.ImageData.Length; i++)
                    {
                        maskChannel.ImageData[i] = 0xAB;
                    }

                    m_imageData = (byte[])maskChannel.ImageData.Clone();

                    switch (maskChannel.ImageCompression)
                    {
                    case ImageCompression.Raw:
                        readerImg.Read(maskChannel.ImageData, 0, maskChannel.ImageData.Length);
                        break;

                    case ImageCompression.Rle:
                    {
                        int[] rowLenghtList = new int[m_rect.Height];

                        for (int i = 0; i < rowLenghtList.Length; i++)
                        {
                            rowLenghtList[i] = readerImg.ReadInt16();
                        }

                        for (int i = 0; i < m_rect.Height; i++)
                        {
                            int rowIndex = i * m_rect.Width;
                            RleHelper.DecodedRow(readerImg.BaseStream, maskChannel.ImageData, rowIndex, bytesPerRow);
                        }
                    }
                    break;

                    default:
                        break;
                    }

                    m_imageData = (byte[])maskChannel.ImageData.Clone();
                }
            }
Exemplo n.º 10
0
        //////////////////////////////////////////////////////////////////
        public ImageResource(BinaryReverseReader reader)
        {
            m_osType = new string(reader.ReadChars(4));
            if(m_osType != "8BIM" && m_osType != "MeSa") {
                throw new InvalidOperationException("Could not read an image resource");
            }

            m_id = reader.ReadInt16();
            m_name = reader.ReadPascalString();

            uint settingLength = reader.ReadUInt32();
            m_data = reader.ReadBytes((int)settingLength);

            if(reader.BaseStream.Position % 2 == 1)
                reader.ReadByte();
        }
Exemplo n.º 11
0
        /// <summary>Loads up the Layers of the supplied PSD file.</summary>      
        private void LoadLayers(BinaryReverseReader reader)
        {
            Debug.WriteLine("LoadLayers started at " + reader.BaseStream.Position.ToString());

            uint layersInfoSectionLength = reader.ReadUInt32();

            if (layersInfoSectionLength <= 0)
                return;

            long startPosition = reader.BaseStream.Position;

            short numberOfLayers = reader.ReadInt16();

            // If <0, then number of layers is absolute value,
            // and the first alpha channel contains the transparency data for
            // the merged result.
            if (numberOfLayers < 0)
            {
                AbsoluteAlpha = true;
                numberOfLayers = Math.Abs(numberOfLayers);
            }

            m_layers.Clear();

            if (numberOfLayers == 0)
                return;

            for (int i = 0; i < numberOfLayers; i++)
            {
                m_layers.Add(new Layer(reader, this));
            }

            foreach (Layer layer in m_layers)
            {
                foreach (Layer.Channel channel in layer.Channels)
                {
                    if (channel.ID != -2)
                        channel.LoadPixelData(reader);
                }
                layer.MaskData.LoadPixelData(reader);
            }

            if (reader.BaseStream.Position % 2 == 1)
                reader.ReadByte();

            // make sure we are not on a wrong offset, so set the stream position
            // manually
            reader.BaseStream.Position = startPosition + layersInfoSectionLength;
        }
Exemplo n.º 12
0
        public void Load(Stream stream)
        {
            //binary reverse reader reads data types in big-endian format.
            BinaryReverseReader reader = new BinaryReverseReader(stream);

            //The headers area is used to check for a valid PSD file
            Debug.WriteLine("LoadHeader started at " + reader.BaseStream.Position.ToString());

            string signature = new string(reader.ReadChars(4));
            if (signature != "8BPS")
                throw new IOException("Bad or invalid file stream supplied");

            //get the version number, should be 1 always
            if ((m_version = reader.ReadInt16()) != 1)
                throw new IOException("Invalid version number supplied");

            //get rid of the 6 bytes reserverd in PSD format
            reader.BaseStream.Position += 6;

            //get the rest of the information from the PSD file.
            //Everytime ReadInt16() is called, it reads 2 bytes.
            //Everytime ReadInt32() is called, it reads 4 bytes.
            m_channels = reader.ReadInt16();
            m_rows = reader.ReadInt32();
            m_columns = reader.ReadInt32();
            m_depth = reader.ReadInt16();
            m_colorMode = (ColorModes)reader.ReadInt16();

            //by end of headers, the reader has read 26 bytes into the file.

            /// <summary>
            /// If ColorMode is ColorModes.Indexed, the following 768 bytes will contain
            /// a 256-color palette. If the ColorMode is ColorModes.Duotone, the data
            /// following presumably consists of screen parameters and other related information.
            /// Unfortunately, it is intentionally not documented by Adobe, and non-Photoshop
            /// readers are advised to treat duotone images as gray-scale images.
            /// </summary>
            Debug.WriteLine("LoadColorModeData started at " + reader.BaseStream.Position.ToString());

            uint paletteLength = reader.ReadUInt32(); //readUint32() advances the reader 4 bytes.
            if (paletteLength > 0)
            {
                ColorModeData = reader.ReadBytes((int)paletteLength);
            }

            //This part takes extensive use of classes that I didn't write therefore
            //I can't document much on what they do.

            Debug.WriteLine("LoadingImageResources started at " + reader.BaseStream.Position.ToString());

            m_imageResources.Clear();

            uint imgResLength = reader.ReadUInt32();
            if (imgResLength > 0)
            {
                long startPosition = reader.BaseStream.Position;

                while ((reader.BaseStream.Position - startPosition) < imgResLength)
                {
                    ImageResource imgRes = new ImageResource(reader);

                    ResourceIDs resID = (ResourceIDs)imgRes.ID;
                    switch (resID)
                    {
                        case ResourceIDs.ResolutionInfo:
                            imgRes = new ResolutionInfo(imgRes);
                            break;
                        case ResourceIDs.Thumbnail1:
                        case ResourceIDs.Thumbnail2:
                            imgRes = new Thumbnail(imgRes);
                            break;
                        case ResourceIDs.AlphaChannelNames:
                            imgRes = new AlphaChannels(imgRes);
                            break;
                    }

                    m_imageResources.Add(imgRes);
                }
                // make sure we are not on a wrong offset, so set the stream position
                // manually
                reader.BaseStream.Position = startPosition + imgResLength;

                //We are gonna load up all the layers and masking of the PSD now.
                Debug.WriteLine("LoadLayerAndMaskInfo - Part1 started at " + reader.BaseStream.Position.ToString());
                uint layersAndMaskLength = reader.ReadUInt32();

                if (layersAndMaskLength > 0)
                {
                    //new start position
                    startPosition = reader.BaseStream.Position;

                    //Lets start by loading up all the layers
                    LoadLayers(reader);
                    //we are done the layers, load up the masks
                    LoadGlobalLayerMask(reader);

                    // make sure we are not on a wrong offset, so set the stream position
                    // manually
                    reader.BaseStream.Position = startPosition + layersAndMaskLength;

                    //we have loaded up all the information from the PSD file
                    //into variables we can use later on.

                    //lets finish loading the raw data that defines the image
                    //in the picture.

                    Debug.WriteLine("LoadImage started at " + reader.BaseStream.Position.ToString());

                    m_imageCompression = (ImageCompression)reader.ReadInt16();

                    m_imageData = new byte[m_channels][];

                    //---------------------------------------------------------------

                    if (m_imageCompression == ImageCompression.Rle)
                    {
                        // The RLE-compressed data is proceeded by a 2-byte data count for each row in the data,
                        // which we're going to just skip.
                        reader.BaseStream.Position += m_rows * m_channels * 2;
                    }

                    //---------------------------------------------------------------

                    int bytesPerRow = 0;

                    switch (m_depth)
                    {
                        case 1:
                            bytesPerRow = m_columns;//NOT Sure
                            break;
                        case 8:
                            bytesPerRow = m_columns;
                            break;
                        case 16:
                            bytesPerRow = m_columns * 2;
                            break;
                    }

                    //---------------------------------------------------------------

                    for (int ch = 0; ch < m_channels; ch++)
                    {
                        m_imageData[ch] = new byte[m_rows * bytesPerRow];

                        switch (m_imageCompression)
                        {
                            case ImageCompression.Raw:
                                reader.Read(m_imageData[ch], 0, m_imageData[ch].Length);
                                break;
                            case ImageCompression.Rle:
                                {
                                    for (int i = 0; i < m_rows; i++)
                                    {
                                        int rowIndex = i * m_columns;
                                        RleHelper.DecodedRow(reader.BaseStream, m_imageData[ch], rowIndex, bytesPerRow);
                                    }
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
        }