Example #1
0
        public void WritePixels(BinaryPSDWriter writer)
        {
            if (this._isMerged)
            {
                PixelData     px  = new PixelData(this.Width, this.Height, this.BitsPerPixel, this._channels.Count, this._isMerged);
                List <byte[]> chs = new List <byte[]>();
                foreach (Channel ch in this._channels.Values)
                {
                    if (ch.Usage != -2)
                    {
                        chs.Add(ch.Data);
                    }
                }
                px.Write(writer, chs, PixelData.Compression.Rle);
                //System.IO.MemoryStream stream = new System.IO.MemoryStream();
                //System.IO.BinaryWriter wr = new System.IO.BinaryWriter(stream);
            }
            else
            {
                foreach (Channel ch in this._channels.Values)
                {
                    if (ch.Usage != -2)
                    {
                        ch.WritePixels(writer);
                    }
                }
            }

            if (this._mask != null)
            {
                this._mask.WritePixels(writer);
            }
        }
Example #2
0
        public long[] Write(BinaryPSDWriter writer, byte[] channelData, Compression compression)
        {
            List <byte[]> channelsData = new List <byte[]>();

            channelsData.Add(channelData);
            return(this.Write(writer, channelsData, compression));
        }
Example #3
0
        public byte[] WritePixels(BinaryPSDWriter writer, Compression compression, byte[] data)
        {
            switch (compression)
            {
            case Compression.None:
                writer.Write(data);
                return(null);

            case Compression.Rle:
                int[] rleRowLenghs = new int[this._height];

                Endogine.Serialization.RleCodec codec = new Endogine.Serialization.RleCodec();
                for (int row = 0; row < this._height; row++)
                {
                    rleRowLenghs[row] = Endogine.Serialization.RleCodec.EncodeChunk(writer.BaseStream, data, (long)row * this._width, (long)this._width);
                }

                System.IO.MemoryStream memStream = new System.IO.MemoryStream();
                BinaryPSDWriter        memWriter = new BinaryPSDWriter(memStream);
                for (int i = 0; i < rleRowLenghs.Length; i++)
                {
                    memWriter.Write((short)rleRowLenghs[i]);
                }

                return(memStream.ToArray());

            default:
                throw new Exception("Compression not implemented: " + compression.ToString());
            }
            return(null);
        }
Example #4
0
 public void PreWritePixels(BinaryPSDWriter writer, Compression compression)
 {
     if (compression == Compression.Rle)
     {
         for (int i = 0; i < this._height; i++)
         {
             writer.Write((ushort)0);
         }
     }
 }
Example #5
0
        public long[] Write(BinaryPSDWriter writer, List <byte[]> channelsData, Compression compression)
        { //returns saved length of each channel's data
            if (channelsData == null)
            {
                channelsData = this._channelData;
            }
            List <long>   headerPositions = new List <long>();
            List <byte[]> headers         = new List <byte[]>();

            if (this._isMerged)
            {
                headers = new List <byte[]>();
                writer.Write((ushort)compression);
                for (int i = 0; i < this._numChannels; i++)
                {
                    headerPositions.Add(writer.BaseStream.Position);
                    this.PreWritePixels(writer, compression);
                }
            }

            long[] lengths = new long[channelsData.Count];

            int chNum = 0;

            foreach (byte[] channelData in channelsData)
            {
                long startPos = writer.BaseStream.Position;
                if (!this._isMerged)
                {
                    writer.Write((ushort)compression);
                    headerPositions.Add(writer.BaseStream.Position);
                    this.PreWritePixels(writer, compression);
                }

                byte[] header = this.WritePixels(writer, compression, channelData);
                headers.Add(header);

                lengths[chNum] = writer.BaseStream.Position - startPos;
                chNum++;
            }

            for (int i = 0; i < headers.Count; i++)
            {
                if (headers[i] != null)
                {
                    writer.BaseStream.Position = headerPositions[i];
                    writer.Write(headers[i]);
                }
            }
            writer.BaseStream.Position = writer.BaseStream.Length - 1;

            return(lengths);
        }
Example #6
0
        //public string GetIdAsFOURCC()
        //{
        //    return "" + (char)((this.ID >> 24) & 0xff) + (char)((this.ID >> 16) & 0xff) + (char)((this.ID >> 8) & 0xff) + (char)(this.ID & 0xff);
        //}

        public void Write(BinaryPSDWriter writer)
        {
            writer.Write("8BIM");
            writer.Write(this.ID);
            writer.WritePascalString(this.Name);
            writer.StartLengthBlock(typeof(uint));
            this.SubWrite(writer);
            writer.EndLengthBlock();
            writer.PadToNextMultiple(2);
            //if (writer.GetCurrentBlockLength() % 2 == 1)
            //    writer.Write((byte)0);
        }
Example #7
0
 public void Write(BinaryPSDWriter writer)
 {
     writer.Write("8BIM");
     writer.Write(this.Tag);
     writer.StartLengthBlock(typeof(uint));
     this.SubWrite(writer);
     writer.EndLengthBlock();
     if (!(this is EffectBase))
     {
         writer.PadToNextMultiple(2);
     }
 }
Example #8
0
 public void WritePixels(BinaryPSDWriter writer)
 {
     if (this._dataForSave == null)
     {
         this.PreparePixelsForSave(writer);
     }
     else
     {
         writer.Write(this._dataForSave);
         this._dataForSave = null;
     }
 }
Example #9
0
 public void Write(BinaryPSDWriter writer)
 {
     writer.Write(this.Version);
     for (int i = 0; i < 6; i++)
     {
         writer.Write((byte)0);
     }
     writer.Write(this.Channels);
     writer.Write(this.Rows);
     writer.Write(this.Columns);
     writer.Write(this.BitsPerPixel);
     writer.Write((short)this.ColorMode);
 }
Example #10
0
        public virtual void Write(BinaryPSDWriter writer)
        {
            writer.Write((ushort)this._usage);

            //we can't write pixel data immediately (comes later in the file), but we must know the size of the pixel data.
            //thus, precalculate the save data here and save it later.
            System.IO.MemoryStream memStream = new System.IO.MemoryStream();
            BinaryPSDWriter        memWriter = new BinaryPSDWriter(memStream);

            this.PreparePixelsForSave(memWriter);
            this._dataForSave = memStream.ToArray();

            writer.Write((uint)this._dataForSave.Length);
        }
Example #11
0
        public override void Write(BinaryPSDWriter writer)
        {
            writer.StartLengthBlock(typeof(uint));
            if (this.Rectangle != null)
            {
                writer.WritePSDRectangle(this.Rectangle); //new Rectangle(this.Rectangle).Write(writer);
                writer.Write(this._color);
                writer.Write(this._flags);

                if (this._otherRectangle != null)
                {
                    writer.Write(this._flags2);
                    writer.Write(this._maskBg);
                    writer.WritePSDRectangle(this._otherRectangle); //new Rectangle(this._otherRectangle).Write(writer);
                }
                else
                {
                    writer.Write((short)0); //padding
                }
            }
            writer.EndLengthBlock();
        }
Example #12
0
 public void WritePixels(BinaryPSDWriter writer)
 {
     this.PreparePixelsForSave(writer);
 }
Example #13
0
        public override void Write(BinaryPSDWriter writer)
        {
            writer.StartLengthBlock(typeof(uint));
            if (this.Rectangle != null)
            {
                writer.WritePSDRectangle(this.Rectangle); //new Rectangle(this.Rectangle).Write(writer);
                writer.Write(this._color);
                writer.Write(this._flags);

                if (this._otherRectangle != null)
                {
                    writer.Write(this._flags2);
                    writer.Write(this._maskBg);
                    writer.WritePSDRectangle(this._otherRectangle); //new Rectangle(this._otherRectangle).Write(writer);
                }
                else
                    writer.Write((short)0); //padding
            }
            writer.EndLengthBlock();
        }
Example #14
0
 public void Save(BinaryPSDWriter writer)
 {
     //writer.StartLengthBlock(typeof(uint));
     this._fakeLayer.WritePixels(writer);
     //writer.EndLengthBlock();
 }
Example #15
0
 public void PreWritePixels(BinaryPSDWriter writer, Compression compression)
 {
     if (compression == Compression.Rle)
     {
         for (int i = 0; i < this._height; i++)
             writer.Write((ushort)0);
     }
 }
Example #16
0
 public void Save(BinaryPSDWriter writer)
 {
     //writer.StartLengthBlock(typeof(uint));
     this._fakeLayer.WritePixels(writer);
     //writer.EndLengthBlock();
 }
Example #17
0
 public long[] Write(BinaryPSDWriter writer, byte[] channelData, Compression compression)
 {
     List<byte[]> channelsData = new List<byte[]>();
     channelsData.Add(channelData);
     return this.Write(writer, channelsData, compression);
 }
Example #18
0
        public byte[] WritePixels(BinaryPSDWriter writer, Compression compression, byte[] data)
        {
            switch (compression)
            {
                case Compression.None:
                    writer.Write(data);
                    return null;

                case Compression.Rle:
                    int[] rleRowLenghs = new int[this._height];

                    Endogine.Serialization.RleCodec codec = new Endogine.Serialization.RleCodec();
                    for (int row = 0; row < this._height; row++)
                        rleRowLenghs[row] = Endogine.Serialization.RleCodec.EncodeChunk(writer.BaseStream, data, (long)row * this._width, (long)this._width);

                    System.IO.MemoryStream memStream = new System.IO.MemoryStream();
                    BinaryPSDWriter memWriter = new BinaryPSDWriter(memStream);
                    for (int i = 0; i < rleRowLenghs.Length; i++)
                        memWriter.Write((short)rleRowLenghs[i]);

                    return memStream.ToArray();

                default:
                    throw new Exception("Compression not implemented: " + compression.ToString());
            }
            return null;
        }
Example #19
0
 public void Write(BinaryPSDWriter writer)
 {
     writer.Write("8BIM");
     writer.Write(this.Tag);
     writer.StartLengthBlock(typeof(uint));
     this.SubWrite(writer);
     writer.EndLengthBlock();
     if (!(this is EffectBase))
         writer.PadToNextMultiple(2);
 }
Example #20
0
        public void Write(BinaryPSDWriter writer)
        {
            writer.WritePSDRectangle(this._rect); // new Rectangle(this._rect).Write(writer);

            writer.Write((ushort)this._channels.Count);
            foreach (Channel channel in this._channels.Values)
            {
                channel.Write(writer);
            }

            writer.Write("8BIM");

            BlendKeys bk = (BlendKeys)Enum.Parse(typeof(BlendKeys), this.BlendKey);
            _blendKeysPsd bkPsd = (_blendKeysPsd)(int)bk;
            writer.Write(bkPsd.ToString());

            writer.Write(this.Opacity);
            writer.Write(this.Clipping);
            writer.Write(this.Flags);
            writer.Write((byte)0); //padding
            writer.StartLengthBlock(typeof(uint));

            if (this._mask != null || (this._blendRanges != null && this._blendRanges.Count > 0) || !string.IsNullOrEmpty(this.Name))
            {
                if (this._mask != null)
                    this._mask.Write(writer);
                else
                    writer.Write((uint)0); //TODO: should be a static of Mask..?

                //blending ranges
                writer.Write((uint)this._blendRanges.Count * 4);
                foreach (System.Drawing.Color clr in this._blendRanges)
                    writer.Write((uint)clr.ToArgb());

                //why is there padding here? Seems unnecessary.
                long namePosition = writer.BaseStream.Position;

                writer.WritePascalString(this.Name);

                int paddingBytes = (int)((writer.BaseStream.Position - namePosition) % 4);
                for (int i = 0; i < paddingBytes; i++)
                    writer.Write((byte)0);

                foreach (LayerResource res in this._resources.Values)
                {
                    res.Write(writer);
                }
            }

            writer.EndLengthBlock();
        }
Example #21
0
 //public string GetIdAsFOURCC()
 //{
 //    return "" + (char)((this.ID >> 24) & 0xff) + (char)((this.ID >> 16) & 0xff) + (char)((this.ID >> 8) & 0xff) + (char)(this.ID & 0xff);
 //}
 public void Write(BinaryPSDWriter writer)
 {
     writer.Write("8BIM");
     writer.Write(this.ID);
     writer.WritePascalString(this.Name);
     writer.StartLengthBlock(typeof(uint));
     this.SubWrite(writer);
     writer.EndLengthBlock();
     writer.PadToNextMultiple(2);
     //if (writer.GetCurrentBlockLength() % 2 == 1)
     //    writer.Write((byte)0);
 }
Example #22
0
        public void Save(string filename)
        {
            if (File.Exists(filename))
                File.Delete(filename);

            FileStream stream = new FileStream(filename,
                FileMode.OpenOrCreate, FileAccess.Write);

            BinaryPSDWriter writer = new BinaryPSDWriter(stream);

            writer.Write("8BPS");
            this._header.Write(writer);

            writer.Write((uint)0); //No palette
            //TODO: palette

            //if (this._imageResources == null)
            //    this._imageResources = new Dictionary<ImageResource.ResourceIDs, ImageResource>();
            writer.StartLengthBlock(typeof(uint));
            foreach (ImageResource imgRes in this._imageResources)
                imgRes.Write(writer);
            writer.EndLengthBlock();

            //   Layers
            writer.StartLengthBlock(typeof(uint), 4);

            // LayerInfo:
            writer.StartLengthBlock(typeof(uint));
            writer.Write((ushort)this.Layers.Count);
            foreach (Layer layer in this.Layers)
                layer.Write(writer);
            //Layer pixel data
            foreach (Layer layer in this.Layers)
                layer.WritePixels(writer);

            writer.EndLengthBlock();

            writer.EndLengthBlock();

            writer.PadToNextMultiple(4);

            //Global Mask
            writer.StartLengthBlock(typeof(uint));
            if (this._tempGlobalLayerMask != null)
                writer.Write(this._tempGlobalLayerMask);
            writer.EndLengthBlock();

            //Global image (merged)
            //TODO: !
            if (this._globalImage != null)
                this._globalImage.Save(writer);
        }
Example #23
0
        public void Save(string filename)
        {
            if (File.Exists(filename))
            {
                File.Delete(filename);
            }

            FileStream stream = new FileStream(filename,
                                               FileMode.OpenOrCreate, FileAccess.Write);

            BinaryPSDWriter writer = new BinaryPSDWriter(stream);

            writer.Write("8BPS");
            this._header.Write(writer);

            writer.Write((uint)0); //No palette
            //TODO: palette

            //if (this._imageResources == null)
            //    this._imageResources = new Dictionary<ImageResource.ResourceIDs, ImageResource>();
            writer.StartLengthBlock(typeof(uint));
            foreach (ImageResource imgRes in this._imageResources)
            {
                imgRes.Write(writer);
            }
            writer.EndLengthBlock();


            //   Layers
            writer.StartLengthBlock(typeof(uint), 4);

            // LayerInfo:
            writer.StartLengthBlock(typeof(uint));
            writer.Write((ushort)this.Layers.Count);
            foreach (Layer layer in this.Layers)
            {
                layer.Write(writer);
            }
            //Layer pixel data
            foreach (Layer layer in this.Layers)
            {
                layer.WritePixels(writer);
            }

            writer.EndLengthBlock();

            writer.EndLengthBlock();

            writer.PadToNextMultiple(4);

            //Global Mask
            writer.StartLengthBlock(typeof(uint));
            if (this._tempGlobalLayerMask != null)
            {
                writer.Write(this._tempGlobalLayerMask);
            }
            writer.EndLengthBlock();

            //Global image (merged)
            //TODO: !
            if (this._globalImage != null)
            {
                this._globalImage.Save(writer);
            }
        }
Example #24
0
 protected virtual void SubWrite(BinaryPSDWriter writer)
 {
     writer.Write(this.Data);
 }
Example #25
0
        public void WritePixels(BinaryPSDWriter writer)
        {
            if (this._isMerged)
            {
                PixelData px = new PixelData(this.Width, this.Height, this.BitsPerPixel, this._channels.Count, this._isMerged);
                List<byte[]> chs = new List<byte[]>();
                foreach (Channel ch in this._channels.Values)
                {
                    if (ch.Usage != -2)
                        chs.Add(ch.Data);
                }
                px.Write(writer, chs, PixelData.Compression.Rle);
                //System.IO.MemoryStream stream = new System.IO.MemoryStream();
                //System.IO.BinaryWriter wr = new System.IO.BinaryWriter(stream);
            }
            else
            {
                foreach (Channel ch in this._channels.Values)
                {
                    if (ch.Usage != -2)
                        ch.WritePixels(writer);
                }
            }

            if (this._mask != null)
                this._mask.WritePixels(writer);
        }
Example #26
0
 protected virtual void SubWrite(BinaryPSDWriter writer)
 {
     writer.Write(this.Data);
 }
Example #27
0
        public long[] Write(BinaryPSDWriter writer, List<byte[]> channelsData, Compression compression)
        {
            //returns saved length of each channel's data
            if (channelsData == null)
                channelsData = this._channelData;
            List<long> headerPositions = new List<long>();
            List<byte[]> headers = new List<byte[]>();

            if (this._isMerged)
            {
                headers = new List<byte[]>();
                writer.Write((ushort)compression);
                for (int i = 0; i < this._numChannels; i++)
                {
                    headerPositions.Add(writer.BaseStream.Position);
                    this.PreWritePixels(writer, compression);
                }
            }

            long[] lengths = new long[channelsData.Count];

            int chNum = 0;
            foreach (byte[] channelData in channelsData)
            {
                long startPos = writer.BaseStream.Position;
                if (!this._isMerged)
                {
                    writer.Write((ushort)compression);
                    headerPositions.Add(writer.BaseStream.Position);
                    this.PreWritePixels(writer, compression);
                }

                byte[] header = this.WritePixels(writer, compression, channelData);
                headers.Add(header);

                lengths[chNum] = writer.BaseStream.Position - startPos;
                chNum++;
            }

            for (int i = 0; i < headers.Count; i++)
            {
                if (headers[i] != null)
                {
                    writer.BaseStream.Position = headerPositions[i];
                    writer.Write(headers[i]);
                }
            }
            writer.BaseStream.Position = writer.BaseStream.Length - 1;

            return lengths;
        }
Example #28
0
        public void Write(BinaryPSDWriter writer)
        {
            writer.WritePSDRectangle(this._rect); // new Rectangle(this._rect).Write(writer);

            writer.Write((ushort)this._channels.Count);
            foreach (Channel channel in this._channels.Values)
            {
                channel.Write(writer);
            }

            writer.Write("8BIM");

            BlendKeys     bk    = (BlendKeys)Enum.Parse(typeof(BlendKeys), this.BlendKey);
            _blendKeysPsd bkPsd = (_blendKeysPsd)(int)bk;

            writer.Write(bkPsd.ToString());

            writer.Write(this.Opacity);
            writer.Write(this.Clipping);
            writer.Write(this.Flags);
            writer.Write((byte)0); //padding
            writer.StartLengthBlock(typeof(uint));

            if (this._mask != null || (this._blendRanges != null && this._blendRanges.Count > 0) || !string.IsNullOrEmpty(this.Name))
            {
                if (this._mask != null)
                {
                    this._mask.Write(writer);
                }
                else
                {
                    writer.Write((uint)0); //TODO: should be a static of Mask..?
                }
                //blending ranges
                writer.Write((uint)this._blendRanges.Count * 4);
                foreach (System.Drawing.Color clr in this._blendRanges)
                {
                    writer.Write((uint)clr.ToArgb());
                }


                //why is there padding here? Seems unnecessary.
                long namePosition = writer.BaseStream.Position;

                writer.WritePascalString(this.Name);

                int paddingBytes = (int)((writer.BaseStream.Position - namePosition) % 4);
                for (int i = 0; i < paddingBytes; i++)
                {
                    writer.Write((byte)0);
                }


                foreach (LayerResource res in this._resources.Values)
                {
                    res.Write(writer);
                }
            }

            writer.EndLengthBlock();
        }
Example #29
0
 public void WritePixels(BinaryPSDWriter writer)
 {
     this.PreparePixelsForSave(writer);
 }
Example #30
0
        protected void PreparePixelsForSave(BinaryPSDWriter writer)
        {
            PixelData px = new PixelData(this.Layer.Width, this.Layer.Height, this.Layer.BitsPerPixel, 1, this._layer.IsMerged);

            px.Write(writer, this._data, PixelData.Compression.Rle);
        }
Example #31
0
 public void Write(BinaryPSDWriter writer)
 {
     writer.Write(this.Version);
     for (int i = 0; i < 6; i++)
         writer.Write((byte)0);
     writer.Write(this.Channels);
     writer.Write(this.Rows);
     writer.Write(this.Columns);
     writer.Write(this.BitsPerPixel);
     writer.Write((short)this.ColorMode);
 }