示例#1
0
        public void CompressImageData()
        {
            // Can be called implicitly by Layer.PrepareSave or explicitly by the
            // consumer of this library.  Since image data compression can take
            // some time, explicit calling makes more accurate progress available.
            if (imageDataCompressed)
            {
                return;
            }

            if (ImageCompression == ImageCompression.Rle)
            {
                var dataStream   = new MemoryStream();
                var headerStream = new MemoryStream();

                var rleWriter    = new RleWriter(dataStream);
                var headerWriter = new PsdBinaryWriter(headerStream, Encoding.ASCII);

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

                var rleRowLengths = new UInt16[Layer.Rect.Height];
                var bytesPerRow   = Util.BytesPerRow(Layer.Rect, Layer.PsdFile.BitDepth);

                for (int row = 0; row < Layer.Rect.Height; row++)
                {
                    int rowIndex = row * Layer.Rect.Width;
                    rleRowLengths[row] = (UInt16)rleWriter.Write(ImageData, rowIndex, bytesPerRow);
                }

                // Write RLE row lengths and save
                for (int i = 0; i < rleRowLengths.Length; i++)
                {
                    headerWriter.Write(rleRowLengths[i]);
                }
                headerStream.Flush();
                this.RleHeader = headerStream.ToArray();
                headerStream.Close();

                // Save compressed data
                dataStream.Flush();
                data = dataStream.ToArray();
                dataStream.Close();

                Length = 2 + RleHeader.Length + Data.Length;
            }
            else
            {
                data        = ImageData;
                this.Length = 2 + Data.Length;
            }

            imageDataCompressed = true;
        }
示例#2
0
        /// <summary>
        /// Compresses the image data.
        /// </summary>
        public void CompressImageData()
        {
            // Do not recompress if compressed data is already present.
            if (ImageDataRaw != null)
            {
                return;
            }

            if (ImageData == null)
            {
                return;
            }

            if (ImageCompression == ImageCompression.Raw)
            {
                ImageDataRaw = ImageData;
                this.Length  = 2 + ImageDataRaw.Length;
            }
            else if (ImageCompression == ImageCompression.Rle)
            {
                RleRowLengths = new RleRowLengths((int)Rect.height);

                using (var dataStream = new MemoryStream())
                {
                    var rleWriter   = new RleWriter(dataStream);
                    var bytesPerRow = Util.BytesPerRow(Rect, Layer.PsdFile.BitDepth);
                    for (int row = 0; row < Rect.height; row++)
                    {
                        int rowIndex = row * (int)Rect.width;
                        RleRowLengths[row] = rleWriter.Write(
                            ImageData, rowIndex, bytesPerRow);
                    }

                    // Save compressed data
                    dataStream.Flush();
                    ImageDataRaw = dataStream.ToArray();
                }
                Length = 2 + 2 * (int)Rect.height + ImageDataRaw.Length;
            }
            else
            {
                throw new NotImplementedException("Only raw and RLE compression have been implemented.");
            }
        }