Пример #1
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.Rle)
            {
                RleRowLengths = new RleRowLengths(Rect.Height);
            }

            var compressor = ImageDataFactory.Create(this, null);

            compressor.Write(ImageData);
            ImageDataRaw = compressor.ReadCompressed();

            Length = 2 + ImageDataRaw.Length;
            if (ImageCompression == ImageCompression.Rle)
            {
                var rowLengthSize = Layer.PsdFile.IsLargeDocument ? 4 : 2;
                Length += rowLengthSize * Rect.Height;
            }
        }
Пример #2
0
        //////////////////////////////////////////////////////////////////

        internal void LoadPixelData(PsdBinaryReader reader)
        {
            var endPosition = reader.BaseStream.Position + this.Length;

            ImageCompression = (ImageCompression)reader.ReadInt16();
            var dataLength = this.Length - 2;

            switch (ImageCompression)
            {
            case ImageCompression.Raw:
                ImageDataRaw = reader.ReadBytes(dataLength);
                break;

            case ImageCompression.Rle:
                // RLE row lengths
                RleRowLengths = new RleRowLengths(reader, (int)Rect.height);
                var rleDataLength = (int)(endPosition - reader.BaseStream.Position);

                // The PSD specification states that rows are padded to even sizes.
                // However, Photoshop doesn't actually do this.  RLE rows can have
                // odd lengths in the header, and there is no padding between rows.
                ImageDataRaw = reader.ReadBytes(rleDataLength);
                break;

            case ImageCompression.Zip:
            case ImageCompression.ZipPrediction:
                ImageDataRaw = reader.ReadBytes(dataLength);
                break;
            }
        }
Пример #3
0
        //internal void Save(PsdBinaryWriter writer)
        //{
        //  Util.DebugMessage(writer.BaseStream, "Save, Begin, Channel");

        //  writer.Write(ID);
        //  if (Layer.PsdFile.IsLargeDocument)
        //  {
        //    writer.Write(Length);
        //  }
        //  else
        //  {
        //    writer.Write((Int32)Length);
        //  }

        //  Util.DebugMessage(writer.BaseStream, "Save, End, Channel, {0}", ID);
        //}

        //////////////////////////////////////////////////////////////////

        internal void LoadPixelData(PsdBinaryReader reader)
        {
            Util.DebugMessage(reader.BaseStream, "Load, Begin, Channel image");

            if (Length == 0)
            {
                ImageCompression = ImageCompression.Raw;
                ImageDataRaw     = new byte[0];
                return;
            }

            var endPosition = reader.BaseStream.Position + this.Length;

            ImageCompression = (ImageCompression)reader.ReadInt16();
            var longDataLength = this.Length - 2;

            Util.CheckByteArrayLength(longDataLength);
            var dataLength = (int)longDataLength;

            switch (ImageCompression)
            {
            case ImageCompression.Raw:
                ImageDataRaw = reader.ReadBytes(dataLength);
                break;

            case ImageCompression.Rle:
                // RLE row lengths
                RleRowLengths = new RleRowLengths(reader, Rect.Height,
                                                  Layer.PsdFile.IsLargeDocument);
                var rleDataLength = (int)(endPosition - reader.BaseStream.Position);
                Debug.Assert(rleDataLength == RleRowLengths.Total,
                             "RLE row lengths do not sum to length of channel image data.");

                // The PSD specification states that rows are padded to even sizes.
                // However, Photoshop doesn't actually do this.  RLE rows can have
                // odd lengths in the header, and there is no padding between rows.
                ImageDataRaw = reader.ReadBytes(rleDataLength);
                break;

            case ImageCompression.Zip:
            case ImageCompression.ZipPrediction:
                ImageDataRaw = reader.ReadBytes(dataLength);
                break;
            }

            Util.DebugMessage(reader.BaseStream, "Load, End, Channel image, {0}",
                              ID, Layer.Name);
            Debug.Assert(reader.BaseStream.Position == endPosition,
                         "Pixel data was not fully read in.");
        }
Пример #4
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.");
            }
        }