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();

                }
            }
        public Layer(BinaryReverseReader reader, PsdFile psdFile)
        {
            Debug.WriteLine("Layer started at " + reader.BaseStream.Position.ToString());

            PsdFile = psdFile;
            Rect = new Rectangle
                       {
                           Y = reader.ReadInt32(),
                           X = reader.ReadInt32(),
                           Height = reader.ReadInt32() - Rect.Y,
                           Width = reader.ReadInt32() - Rect.X
                       };

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

            int numberOfChannels = reader.ReadUInt16();
            _channels.Clear();
            for (var channel = 0; channel < numberOfChannels; channel++)
            {
                var ch = new Channel(reader, this);
                _channels.Add(ch);
                _sortedChannels.Add(ch.Id, ch);
            }

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

            var signature = new string(reader.ReadChars(4));
            if (signature != "8BIM")
                throw (new IOException("Layer Channelheader error!"));

            _blendModeKey = new string(reader.ReadChars(4));
            Opacity = reader.ReadByte();

            Clipping = reader.ReadByte() > 0;

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

            byte flags = reader.ReadByte();
            _flags = new BitVector32(flags);

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

            reader.ReadByte(); //padding

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

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

            // this is the total size of the MaskData, the BlendingRangesData, the
            // Name and the AdjustmenLayerInfo
            uint extraDataSize = reader.ReadUInt32();

            // remember the start position for calculation of the
            // AdjustmenLayerInfo size
            long extraDataStartPosition = reader.BaseStream.Position;

            m_maskData = new Mask(reader, this);
            m_blendingRangesData = new BlendingRanges(reader, this);

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

            long namePosition = reader.BaseStream.Position;

            Name = reader.ReadPascalString();

            int paddingBytes = (int)((reader.BaseStream.Position - namePosition) % 4);

            Debug.Print("Layer {0} padding bytes after name", paddingBytes);
            reader.ReadBytes(paddingBytes);

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

            m_adjustmentInfo.Clear();

            long adjustmenLayerEndPos = extraDataStartPosition + extraDataSize;
            while (reader.BaseStream.Position < adjustmenLayerEndPos)
            {
                try
                {
                    m_adjustmentInfo.Add(new AdjusmentLayerInfo(reader, this));
                }
                catch
                {
                    reader.BaseStream.Position = adjustmenLayerEndPos;
                }
            }

            //-----------------------------------------------------------------------
            // make shure we are not on a wrong offset, so set the stream position
            // manually
            reader.BaseStream.Position = adjustmenLayerEndPos;
        }
            ///////////////////////////////////////////////////////////////////////////
            internal Mask(BinaryReverseReader reader, Layer layer)
            {
                Debug.WriteLine("Mask started at " + reader.BaseStream.Position.ToString());

                m_layer = layer;

                uint maskLength = reader.ReadUInt32();

                if (maskLength <= 0)
                    return;

                long startPosition = reader.BaseStream.Position;

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

                m_rect = new Rectangle();
                m_rect.Y = reader.ReadInt32();
                m_rect.X = reader.ReadInt32();
                m_rect.Height = reader.ReadInt32() - m_rect.Y;
                m_rect.Width = reader.ReadInt32() - m_rect.X;

                m_defaultColor = reader.ReadByte();

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

                byte flags = reader.ReadByte();
                m_flags = new BitVector32(flags);

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

                if (maskLength == 36)
                {
                    BitVector32 realFlags = new BitVector32(reader.ReadByte());

                    byte realUserMaskBackground = reader.ReadByte();

                    Rectangle rect = new Rectangle();
                    rect.Y = reader.ReadInt32();
                    rect.X = reader.ReadInt32();
                    rect.Height = reader.ReadInt32() - m_rect.Y;
                    rect.Width = reader.ReadInt32() - m_rect.X;
                }

                // there is other stuff following, but we will ignore this.
                reader.BaseStream.Position = startPosition + maskLength;
            }
            internal Channel(BinaryReverseReader reader, Layer layer)
            {
                Debug.WriteLine("Channel started at " + reader.BaseStream.Position);

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

                _layer = layer;
            }
            //////////////////////////////////////////////////////////////////
            internal void LoadPixelData(BinaryReverseReader reader)
            {
                Debug.WriteLine("Channel.LoadPixelData started at " + reader.BaseStream.Position.ToString());

                Data = reader.ReadBytes(Length);

                using (var readerImg = DataReader)
                {
                    ImageCompression = (ImageCompression)readerImg.ReadInt16();

                    var bytesPerRow = 0;

                    switch (_layer.PsdFile.Depth)
                    {
                        case 1:
                            bytesPerRow = _layer.Rect.Width;//NOT Shure
                            break;
                        case 8:
                            bytesPerRow = _layer.Rect.Width;
                            break;
                        case 16:
                            bytesPerRow = _layer.Rect.Width * 2;
                            break;
                    }

                    ImageData = new byte[_layer.Rect.Height * bytesPerRow];

                    switch (ImageCompression)
                    {
                        case ImageCompression.Raw:
                            readerImg.Read(ImageData, 0, ImageData.Length);
                            break;
                        case ImageCompression.Rle:
                            {
                                int[] rowLenghtList = new int[_layer.Rect.Height];
                                for (int i = 0; i < rowLenghtList.Length; i++)
                                    rowLenghtList[i] = readerImg.ReadInt16();

                                for (int i = 0; i < _layer.Rect.Height; i++)
                                {
                                    int rowIndex = i * _layer.Rect.Width;
                                    RleHelper.DecodedRow(readerImg.BaseStream, ImageData, rowIndex, bytesPerRow);

                                    //if (rowLenghtList[i] % 2 == 1)
                                    //  readerImg.ReadByte();
                                }
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            public AdjusmentLayerInfo(BinaryReverseReader reader, Layer layer)
            {
                Debug.WriteLine("AdjusmentLayerInfo started at " + reader.BaseStream.Position.ToString());

                m_layer = layer;

                string signature = new string(reader.ReadChars(4));
                if (signature != "8BIM")
                {
                    throw new IOException("Could not read an image resource");
                }

                m_key = new string(reader.ReadChars(4));

                uint dataLength = reader.ReadUInt32();
                m_data = reader.ReadBytes((int)dataLength);
            }
            ///////////////////////////////////////////////////////////////////////////
            public BlendingRanges(BinaryReverseReader reader, Layer layer)
            {
                Debug.WriteLine("BlendingRanges started at " + reader.BaseStream.Position.ToString());

                m_layer = layer;
                int dataLength = reader.ReadInt32();
                if (dataLength <= 0)
                    return;

                m_data = reader.ReadBytes(dataLength);
            }
        ///////////////////////////////////////////////////////////////////////////
        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 shure we are not on a wrong offset, so set the stream position
              // manually
              reader.BaseStream.Position = startPosition + layersInfoSectionLength;
        }
        //////////////////////////////////////////////////////////////////
        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();
        }
        ///////////////////////////////////////////////////////////////////////////
        private void LoadImageResources(BinaryReverseReader reader)
        {
            Debug.WriteLine("LoadImageResources 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 shure we are not on a wrong offset, so set the stream position
              // manually
              reader.BaseStream.Position = startPosition + imgResLength;
        }
        ///////////////////////////////////////////////////////////////////////////
        private void LoadLayerAndMaskInfo(BinaryReverseReader reader)
        {
            Debug.WriteLine("LoadLayerAndMaskInfo started at " + reader.BaseStream.Position.ToString());

              uint layersAndMaskLength = reader.ReadUInt32();

              if (layersAndMaskLength <= 0)
            return;

              long startPosition = reader.BaseStream.Position;

              LoadLayers(reader);
              LoadGlobalLayerMask(reader);

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

              //Debug.Assert(reader.BaseStream.Position == startPosition + layersAndMaskLength, "LoadLayerAndMaskInfo");

              //-----------------------------------------------------------------------
              // make shure we are not on a wrong offset, so set the stream position
              // manually
              reader.BaseStream.Position = startPosition + layersAndMaskLength;
        }
        ///////////////////////////////////////////////////////////////////////////
        private void LoadImage(BinaryReverseReader reader)
        {
            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;
            }
              }
        }
        ///////////////////////////////////////////////////////////////////////////
        private void LoadHeader(BinaryReverseReader reader)
        {
            Debug.WriteLine("LoadHeader started at " + reader.BaseStream.Position.ToString());

              string signature = new string(reader.ReadChars(4));
              if (signature != "8BPS")
            throw new IOException("The given stream is not a valid PSD file");

              m_version = reader.ReadInt16();
              if (m_version != 1)
            throw new IOException("The PSD file has an unkown version");

              //6 bytes reserved
              reader.BaseStream.Position += 6;

              m_channels = reader.ReadInt16();
              m_rows = reader.ReadInt32();
              m_columns = reader.ReadInt32();
              m_depth = reader.ReadInt16();
              m_colorMode = (ColorModes)reader.ReadInt16();
        }
        private void LoadGlobalLayerMask(BinaryReverseReader reader)
        {
            Debug.WriteLine("LoadGlobalLayerMask started at " + reader.BaseStream.Position.ToString());

              uint maskLength = reader.ReadUInt32();

              if (maskLength <= 0)
            return;

              GlobalLayerMaskData = reader.ReadBytes((int)maskLength);
        }
        private void LoadColorModeData(BinaryReverseReader reader)
        {
            Debug.WriteLine("LoadColorModeData started at " + reader.BaseStream.Position.ToString());

              uint paletteLength = reader.ReadUInt32();
              if (paletteLength > 0)
              {
            ColorModeData = reader.ReadBytes((int)paletteLength);
              }
        }
        public void Load(Stream stream)
        {
            BinaryReverseReader reader = new BinaryReverseReader(stream);

              LoadHeader(reader);
              LoadColorModeData(reader);
              LoadImageResources(reader);
              LoadLayerAndMaskInfo(reader);

              LoadImage(reader);
        }