/////////////////////////////////////////////////////////////////////////// private void LoadImage(BinaryReverseReader reader) { Debug.WriteLine("LoadImage started at " + reader.BaseStream.Position); m_imageCompression = (ImageCompression)reader.ReadInt16(); m_imageData = new byte[m_channels][]; //--------------------------------------------------------------- if (m_imageCompression == ImageCompression.Rle) { // The RLE-compressed data is proceeded by a 2-byte data count for each row in the data, // which we're going to just skip. reader.BaseStream.Position += m_rows * m_channels * 2; } //--------------------------------------------------------------- int bytesPerRow = 0; switch (m_depth) { case 1: bytesPerRow = m_columns; //NOT Shure break; case 8: bytesPerRow = m_columns; break; case 16: bytesPerRow = m_columns * 2; break; } //--------------------------------------------------------------- for (int ch = 0; ch < m_channels; ch++) { m_imageData[ch] = new byte[m_rows * bytesPerRow]; switch (m_imageCompression) { case ImageCompression.Raw: reader.Read(m_imageData[ch], 0, m_imageData[ch].Length); break; case ImageCompression.Rle: { for (int i = 0; i < m_rows; i++) { int rowIndex = i * m_columns; RleHelper.DecodedRow(reader.BaseStream, m_imageData[ch], rowIndex, bytesPerRow); } } break; } } }
////////////////////////////////////////////////////////////////// internal void LoadPixelData(BinaryReverseReader reader) { Debug.WriteLine("Channel.LoadPixelData started at " + reader.BaseStream.Position); m_data = reader.ReadBytes(Length); using (BinaryReverseReader readerImg = DataReader) { m_imageCompression = (ImageCompression)readerImg.ReadInt16(); int bytesPerRow = 0; switch (m_layer.PsdFile.Depth) { case 1: bytesPerRow = m_layer.m_rect.Width; //NOT Shure break; case 8: bytesPerRow = m_layer.m_rect.Width; break; case 16: bytesPerRow = m_layer.m_rect.Width * 2; break; } m_imageData = new byte[m_layer.m_rect.Height * bytesPerRow]; switch (m_imageCompression) { case ImageCompression.Raw: readerImg.Read(m_imageData, 0, m_imageData.Length); break; case ImageCompression.Rle: { var rowLenghtList = new int[m_layer.m_rect.Height]; for (int i = 0; i < rowLenghtList.Length; i++) { rowLenghtList[i] = readerImg.ReadInt16(); } for (int i = 0; i < m_layer.m_rect.Height; i++) { var rowIndex = i * m_layer.m_rect.Width; RleHelper.DecodedRow(readerImg.BaseStream, m_imageData, rowIndex, bytesPerRow); //if (rowLenghtList[i] % 2 == 1) // readerImg.ReadByte(); } } break; } } }
public void DecompressImageData() { MemoryStream stream = new MemoryStream(m_data); for (int i = 0; i < m_layer.m_rect.Height; i++) { int rowIndex = i * m_layer.m_rect.Width; RleHelper.DecodedRow(stream, m_imageData, rowIndex, m_bytesPerRow); } }
internal void LoadPixelData(BinaryReverseReader reader) { Data = reader.ReadBytes(Length); using (BinaryReverseReader dataReader = DataReader) { //从文档 五 - 5 读取信息 ImageCompression = (ImageCompression)dataReader.ReadInt16(); int columns = 0; switch (Layer.PsdFile.Depth) { case 1: columns = (int)Layer.Rect.width; break; case 8: columns = (int)Layer.Rect.width; break; case 16: columns = (int)Layer.Rect.width * 2; break; } ImageData = new byte[(int)Layer.Rect.height * columns]; switch (ImageCompression) { case ImageCompression.Raw: dataReader.Read(ImageData, 0, ImageData.Length); break; case ImageCompression.Rle: int[] nums = new int[(int)Layer.Rect.height]; for (int i = 0; i < Layer.Rect.height; i++) { nums[i] = dataReader.ReadInt16(); } for (int index = 0; index < Layer.Rect.height; ++index) { int startIdx = index * (int)Layer.Rect.width; RleHelper.DecodedRow(dataReader.BaseStream, ImageData, startIdx, columns); } break; } } }
private void LoadImage(BinaryReverseReader reader) { _imageCompression = (ImageCompression)reader.ReadInt16(); _imageData = new byte[_channels][]; if (_imageCompression == ImageCompression.Rle) { reader.BaseStream.Position += _height * _channels * 2; } int columns = 0; switch (_depth) { case 1: columns = _width; break; case 8: columns = _width; break; case 16: columns = _width * 2; break; } for (int index1 = 0; index1 < (int)_channels; ++index1) { _imageData[index1] = new byte[_height * columns]; switch (_imageCompression) { case ImageCompression.Raw: reader.Read(_imageData[index1], 0, _imageData[index1].Length); break; case ImageCompression.Rle: for (int index2 = 0; index2 < _height; ++index2) { int startIdx = index2 * _width; RleHelper.DecodedRow(reader.BaseStream, _imageData[index1], startIdx, columns); } break; } } }
/////////////////////////////////////////////////////////////////////////// private void SaveImageRLE(BinaryReverseWriter writer) { // we will write the correct lengths later, so remember // the position var lengthPosition = writer.BaseStream.Position; var 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++) { var 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 (var i = 0; i < rleRowLenghs.Length; i++) { writer.Write((short)rleRowLenghs[i]); } writer.BaseStream.Position = endPosition; //--------------------------------------------------------------- }
internal void LoadPixelData(BinaryReverseReader reader) { Debug.WriteLine("Mask.LoadPixelData started at " + reader.BaseStream.Position.ToString()); if (m_rect.IsEmpty || m_layer.SortedChannels.ContainsKey(-2) == false) { return; } Channel maskChannel = m_layer.SortedChannels[-2]; maskChannel.Data = reader.ReadBytes((int)maskChannel.Length); using (BinaryReverseReader readerImg = maskChannel.DataReader) { maskChannel.ImageCompression = (ImageCompression)readerImg.ReadInt16(); int bytesPerRow = 0; switch (m_layer.PsdFile.Depth) { case 1: bytesPerRow = ImageDecoder.BytesFromBits(m_layer.m_rect.Width); break; case 8: bytesPerRow = m_rect.Width; break; case 16: bytesPerRow = m_rect.Width * 2; break; } maskChannel.ImageData = new byte[m_rect.Height * bytesPerRow]; // Fill Array for (int i = 0; i < maskChannel.ImageData.Length; i++) { maskChannel.ImageData[i] = 0xAB; } m_imageData = (byte[])maskChannel.ImageData.Clone(); switch (maskChannel.ImageCompression) { case ImageCompression.Raw: readerImg.Read(maskChannel.ImageData, 0, maskChannel.ImageData.Length); break; case ImageCompression.Rle: { uint[] rowLengthList = new uint[m_rect.Height]; for (int i = 0; i < rowLengthList.Length; i++) { rowLengthList[i] = readerImg.ReadUInt16(); } for (int i = 0; i < m_rect.Height; i++) { int rowIndex = i * m_rect.Width; RleHelper.DecodedRow(readerImg.BaseStream, maskChannel.ImageData, rowIndex, bytesPerRow); } } break; default: break; } m_imageData = (byte[])maskChannel.ImageData.Clone(); } }
public void CompressImageData() { if (m_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[] rleRowLengths = new int[m_layer.m_rect.Height]; for (int i = 0; i < rleRowLengths.Length; i++) { writer.Write((short)0x1234); } //--------------------------------------------------------------- int bytesPerRow = 0; switch (m_layer.PsdFile.Depth) { case 1: bytesPerRow = ImageDecoder.BytesFromBits(m_layer.m_rect.Width); break; case 8: bytesPerRow = m_layer.m_rect.Width; break; case 16: bytesPerRow = m_layer.m_rect.Width * 2; break; } //--------------------------------------------------------------- for (int row = 0; row < m_layer.m_rect.Height; row++) { int rowIndex = row * m_layer.m_rect.Width; rleRowLengths[row] = RleHelper.EncodedRow(writer.BaseStream, m_imageData, rowIndex, bytesPerRow); } //--------------------------------------------------------------- long endPosition = writer.BaseStream.Position; writer.BaseStream.Position = lengthPosition; for (int i = 0; i < rleRowLengths.Length; i++) { writer.Write((short)rleRowLengths[i]); } writer.BaseStream.Position = endPosition; dataStream.Close(); m_data = dataStream.ToArray(); dataStream.Dispose(); } else { m_data = (byte[])m_imageData.Clone(); } }
/// <summary> /// Reads the pixel data from a reader. /// </summary> /// <param name="reader">The reader to use to read the pixel data.</param> internal void LoadPixelData(BinaryReverseReader reader) { if (rect.width <= 0 || !Layer.SortedChannels.ContainsKey(-2)) { return; } Channel channel = Layer.SortedChannels[-2]; channel.Data = reader.ReadBytes(channel.Length); using (BinaryReverseReader dataReader = channel.DataReader) { channel.ImageCompression = (ImageCompression)dataReader.ReadInt16(); int columns = 0; switch (Layer.PsdFile.Depth) { case 1: columns = (int)rect.width; break; case 8: columns = (int)rect.width; break; case 16: columns = (int)rect.width * 2; break; } channel.ImageData = new byte[(int)rect.height * columns]; for (int index = 0; index < channel.ImageData.Length; ++index) { channel.ImageData[index] = 171; } ImageData = (byte[])channel.ImageData.Clone(); switch (channel.ImageCompression) { case ImageCompression.Raw: dataReader.Read(channel.ImageData, 0, channel.ImageData.Length); break; case ImageCompression.Rle: int[] nums = new int[(int)rect.height]; for (int i = 0; i < (int)rect.height; i++) { nums[i] = dataReader.ReadInt16(); } for (int index = 0; index < (int)rect.height; ++index) { int startIdx = index * (int)rect.width; RleHelper.DecodedRow(dataReader.BaseStream, channel.ImageData, startIdx, columns); } break; } ImageData = (byte[])channel.ImageData.Clone(); } }