public LengthWriter(BinaryReverseWriter writer)
        {
            _writer = writer;

            // we will write the correct length later, so remember
            // the position
            _lengthPosition = _writer.BaseStream.Position;
            _writer.Write((uint)0xFEEDFEED);

            // remember the start  position for calculation Image
            // resources length
            _startPosition = _writer.BaseStream.Position;
        }
            ///////////////////////////////////////////////////////////////////////////
            public void Save(BinaryReverseWriter writer)
            {
                Debug.WriteLine("Mask Save started at " + writer.BaseStream.Position.ToString());

                if (m_rect.IsEmpty)
                {
                    writer.Write((uint)0);
                    return;
                }

                using (new LengthWriter(writer))
                {
                    writer.Write(m_rect.Top);
                    writer.Write(m_rect.Left);
                    writer.Write(m_rect.Bottom);
                    writer.Write(m_rect.Right);

                    writer.Write(m_defaultColor);

                    writer.Write((byte)m_flags.Data);

                    // padding 2 bytes so that size is 20
                    writer.Write((int)0);
                }
            }
            internal void SavePixelData(BinaryReverseWriter writer)
            {
                Debug.WriteLine("Channel SavePixelData started at " + writer.BaseStream.Position);

                writer.Write((short)ImageCompression);
                writer.Write(ImageData);
            }
            private void CompressImageData()
            {
                if (ImageCompression == ImageCompression.Rle)
                {
                    MemoryStream dataStream = new MemoryStream();
                    BinaryReverseWriter writer = new BinaryReverseWriter(dataStream);

                    // we will write the correct lengths later, so remember
                    // the position
                    long lengthPosition = writer.BaseStream.Position;

                    int[] rleRowLenghs = new int[_layer.Rect.Height];

                    if (ImageCompression == ImageCompression.Rle)
                    {
                        for (int i = 0; i < rleRowLenghs.Length; i++)
                        {
                            writer.Write((short)0x1234);
                        }
                    }

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

                    int 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;
                    }

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

                    for (int row = 0; row < _layer.Rect.Height; row++)
                    {
                        int rowIndex = row * _layer.Rect.Width;
                        rleRowLenghs[row] = RleHelper.EncodedRow(writer.BaseStream, ImageData, rowIndex, bytesPerRow);
                    }

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

                    long endPosition = writer.BaseStream.Position;

                    writer.BaseStream.Position = lengthPosition;

                    for (int i = 0; i < rleRowLenghs.Length; i++)
                    {
                        writer.Write((short)rleRowLenghs[i]);
                    }

                    writer.BaseStream.Position = endPosition;

                    dataStream.Close();

                    Data = dataStream.ToArray();

                    dataStream.Dispose();

                }
                else
                {
                    Data = (byte[])ImageData.Clone();
                }
            }
            ///////////////////////////////////////////////////////////////////////////
            public void Save(BinaryReverseWriter writer)
            {
                Debug.WriteLine("BlendingRanges Save started at " + writer.BaseStream.Position.ToString());

                writer.Write((uint)m_data.Length);
                writer.Write(m_data);
            }
            internal void Save(BinaryReverseWriter writer)
            {
                Debug.WriteLine("Channel Save started at " + writer.BaseStream.Position.ToString());

                writer.Write(Id);

                CompressImageData();

                writer.Write(Data.Length + 2); // 2 bytes for the image compression
            }
        ///////////////////////////////////////////////////////////////////////////
        public void Save(BinaryReverseWriter writer)
        {
            Debug.WriteLine("Layer Save started at " + writer.BaseStream.Position.ToString());

            writer.Write(Rect.Top);
            writer.Write(Rect.Left);
            writer.Write(Rect.Bottom);
            writer.Write(Rect.Right);

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

            writer.Write((short)_channels.Count);
            foreach (Channel ch in _channels)
                ch.Save(writer);

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

            string signature = "8BIM";
            writer.Write(signature.ToCharArray());
            writer.Write(_blendModeKey.ToCharArray());
            writer.Write(Opacity);
            writer.Write((byte)(Clipping ? 1 : 0));

            writer.Write((byte)_flags.Data);

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

            writer.Write((byte)0);

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

            using (new LengthWriter(writer))
            {
                m_maskData.Save(writer);
                m_blendingRangesData.Save(writer);

                long namePosition = writer.BaseStream.Position;

                writer.WritePascalString(Name);

                int paddingBytes = (int)((writer.BaseStream.Position - namePosition) % 4);
                Debug.Print("Layer {0} write padding bytes after name", paddingBytes);

                for (int i = 0; i < paddingBytes; i++)
                    writer.Write((byte)0);

                foreach (AdjusmentLayerInfo info in m_adjustmentInfo)
                {
                    info.Save(writer);
                }
            }
        }
            public void Save(BinaryReverseWriter writer)
            {
                Debug.WriteLine("AdjusmentLayerInfo Save started at " + writer.BaseStream.Position.ToString());

                string signature = "8BIM";

                writer.Write(signature.ToCharArray());
                writer.Write(m_key.ToCharArray());
                writer.Write((uint)m_data.Length);
                writer.Write(m_data);
            }
        ///////////////////////////////////////////////////////////////////////////
        private void SaveLayers(BinaryReverseWriter writer)
        {
            Debug.WriteLine("SaveLayers started at " + writer.BaseStream.Position.ToString());

              using (new LengthWriter(writer))
              {
            short numberOfLayers = (short)m_layers.Count;
            if (AbsoluteAlpha)
              numberOfLayers = (short)-numberOfLayers;

            writer.Write(numberOfLayers);

            foreach (Layer layer in m_layers)
            {
              layer.Save(writer);
            }

            foreach (Layer layer in m_layers)
            {
              foreach (Layer.Channel channel in layer.Channels)
              {
            channel.SavePixelData(writer);
              }
            }

            if (writer.BaseStream.Position % 2 == 1)
              writer.Write((byte)0);
              }
        }
        //////////////////////////////////////////////////////////////////
        public void Save(BinaryReverseWriter writer)
        {
            StoreData();

            if (m_osType == String.Empty)
                m_osType = "8BIM";

            writer.Write(m_osType.ToCharArray());
            writer.Write(m_id);

            writer.WritePascalString(m_name);

            writer.Write((int)m_data.Length);
            writer.Write(m_data);

            if (writer.BaseStream.Position % 2 == 1)
                writer.Write((byte)0);
        }
        ///////////////////////////////////////////////////////////////////////////
        private void SaveImageRLE(BinaryReverseWriter writer)
        {
            // we will write the correct lengths later, so remember
              // the position
              long lengthPosition = writer.BaseStream.Position;

              int[] rleRowLenghs = new int[m_rows * m_channels];

              if (m_imageCompression == ImageCompression.Rle)
              {
            for (int i = 0; i < rleRowLenghs.Length; i++)
            {
              writer.Write((short)0x1234);
            }
              }

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

              for (int ch = 0; ch < m_channels; ch++)
              {
            int startIdx = ch * m_rows;

            for (int row = 0; row < m_rows; row++)
              rleRowLenghs[row + startIdx] = RleHelper.EncodedRow(writer.BaseStream, m_imageData[ch], row * m_columns, m_columns);
              }

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

              long endPosition = writer.BaseStream.Position;

              writer.BaseStream.Position = lengthPosition;

              for (int i = 0; i < rleRowLenghs.Length; i++)
              {
            writer.Write((short)rleRowLenghs[i]);
              }

              writer.BaseStream.Position = endPosition;

              //---------------------------------------------------------------
        }
        ///////////////////////////////////////////////////////////////////////////
        private void SaveImage(BinaryReverseWriter writer)
        {
            Debug.WriteLine("SaveImage started at " + writer.BaseStream.Position.ToString());

              writer.Write((short)m_imageCompression);

              if (m_imageCompression == ImageCompression.Rle)
              {
            SaveImageRLE(writer);
              }
              else
              {
            for (int ch = 0; ch < m_channels; ch++)
            {
              writer.Write(m_imageData[ch]);
            }
              }
        }
        ///////////////////////////////////////////////////////////////////////////
        private void SaveHeader(BinaryReverseWriter writer)
        {
            Debug.WriteLine("SaveHeader started at " + writer.BaseStream.Position.ToString());

              string signature = "8BPS";
              writer.Write(signature.ToCharArray());
              writer.Write(Version);
              writer.Write(new byte[] { 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, });
              writer.Write(m_channels);
              writer.Write(m_rows);
              writer.Write(m_columns);
              writer.Write((short)m_depth);
              writer.Write((short)m_colorMode);
        }
        ///////////////////////////////////////////////////////////////////////////
        private void SaveGlobalLayerMask(BinaryReverseWriter writer)
        {
            Debug.WriteLine("SaveGlobalLayerMask started at " + writer.BaseStream.Position.ToString());

              writer.Write((uint)GlobalLayerMaskData.Length);
              writer.Write(GlobalLayerMaskData);
        }
        private void SaveColorModeData(BinaryReverseWriter writer)
        {
            Debug.WriteLine("SaveColorModeData started at " + writer.BaseStream.Position.ToString());

              writer.Write((uint)ColorModeData.Length);
              writer.Write(ColorModeData);
        }