示例#1
0
        public void DecompressImageData()
        {
            if (dataDecompressed)
            {
                return;
            }

            var rect        = Rect;
            var bytesPerRow = Util.BytesPerRow(rect, Layer.PsdFile.BitDepth);
            var bytesTotal  = rect.Height * bytesPerRow;

            if (this.ImageCompression != PhotoshopFile.ImageCompression.Raw)
            {
                imageData = new byte[bytesTotal];

                var stream = new MemoryStream(Data);
                switch (this.ImageCompression)
                {
                case ImageCompression.Rle:
                    var rleReader = new RleReader(stream);
                    for (int i = 0; i < rect.Height; i++)
                    {
                        int rowIndex = i * bytesPerRow;
                        rleReader.Read(imageData, rowIndex, bytesPerRow);
                    }
                    break;

                case ImageCompression.Zip:
                case ImageCompression.ZipPrediction:
                    // .NET implements Deflate (RFC 1951) but not zlib (RFC 1950),
                    // so we have to skip the first two bytes.
                    stream.ReadByte();
                    stream.ReadByte();

                    var deflateStream     = new DeflateStream(stream, CompressionMode.Decompress);
                    var bytesDecompressed = deflateStream.Read(imageData, 0, bytesTotal);
                    Debug.Assert(bytesDecompressed == bytesTotal, "ZIP deflation output is different length than expected.");
                    break;
                }
            }

            // Reverse multi-byte pixels to little-endian.  This cannot be done
            // on 32-bit depth images with ZipPrediction because the bytes have
            // been packed together.
            bool fReverseEndianness = (Layer.PsdFile.BitDepth == 16) ||
                                      (Layer.PsdFile.BitDepth == 32) && (ImageCompression != PhotoshopFile.ImageCompression.ZipPrediction);

            if (fReverseEndianness)
            {
                ReverseEndianness(imageData, rect);
            }

            if (this.ImageCompression == PhotoshopFile.ImageCompression.ZipPrediction)
            {
                UnpredictImageData(rect);
            }

            dataDecompressed = true;
        }
示例#2
0
        private void DecompressImageData()
        {
            using (var stream = new MemoryStream(ImageDataRaw))
            {
                var bytesPerRow = Util.BytesPerRow(Rect, Layer.PsdFile.BitDepth);
                var bytesTotal  = (int)Rect.height * bytesPerRow;
                ImageData = new byte[bytesTotal];

                switch (this.ImageCompression)
                {
                case ImageCompression.Rle:
                    var rleReader = new RleReader(stream);
                    for (int i = 0; i < Rect.height; i++)
                    {
                        int rowIndex = i * bytesPerRow;
                        rleReader.Read(ImageData, rowIndex, bytesPerRow);
                    }
                    break;

                case ImageCompression.Zip:
                case ImageCompression.ZipPrediction:

                    // .NET implements Deflate (RFC 1951) but not zlib (RFC 1950),
                    // so we have to skip the first two bytes.
                    stream.ReadByte();
                    stream.ReadByte();

                    var deflateStream = new DeflateStream(stream, CompressionMode.Decompress);
                    // bytesDecompressed
                    deflateStream.Read(ImageData, 0, bytesTotal);
                    break;

                default:
                    throw new PsdInvalidException("Unknown image compression method.");
                }
            }
        }