public BlendingRanges(BinaryReverseReader reader, Layer layer) { Data = new Byte[0]; Debug.WriteLine("BlendingRanges started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); Layer = layer; Int32 dataLength = reader.ReadInt32(); if (dataLength <= 0) return; Data = reader.ReadBytes(dataLength); }
internal void LoadPixelData(BinaryReverseReader reverseReader) { Debug.WriteLine("Channel.LoadPixelData started at " + reverseReader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); Data = reverseReader.ReadBytes(Length); using (BinaryReverseReader imageReader = DataReader) { ImageCompression = (ImageCompression)imageReader.ReadInt16(); Int32 bytesPerRow = 0; switch (Layer.PsdFile.Depth) { case 1: bytesPerRow = Layer.Rect.Width;//NOT sure break; case 8: bytesPerRow = Layer.Rect.Width; break; case 16: bytesPerRow = Layer.Rect.Width * 2; break; } ImageData = new Byte[Layer.Rect.Height * bytesPerRow]; switch (ImageCompression) { case ImageCompression.Raw: imageReader.Read(ImageData, 0, ImageData.Length); break; case ImageCompression.Rle: { Int32[] rowLengthList = new Int32[Layer.Rect.Height]; for (Int32 i = 0; i < rowLengthList.Length; i++) rowLengthList[i] = imageReader.ReadInt16(); for (Int32 i = 0; i < Layer.Rect.Height; i++) { Int32 rowIndex = i * Layer.Rect.Width; RleHelper.DecodedRow(imageReader.BaseStream, ImageData, rowIndex, bytesPerRow); //if (rowLenghtList[i] % 2 == 1) // readerImg.ReadByte(); } } break; } } }
public AdjusmentLayerInfo(BinaryReverseReader reader, Layer layer) { Debug.WriteLine("AdjusmentLayerInfo started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); Layer = layer; String signature = new String(reader.ReadChars(4)); if (signature != "8BIM") { throw new IOException("Could not read an image resource"); } Key = new String(reader.ReadChars(4)); UInt32 dataLength = reader.ReadUInt32(); Data = reader.ReadBytes((Int32)dataLength); }
public ImageResource(BinaryReverseReader reverseReader) { Name = String.Empty; OSType = new String(reverseReader.ReadChars(4)); if (OSType != "8BIM" && OSType != "MeSa") { throw new InvalidOperationException("Could not read an image resource"); } ID = reverseReader.ReadInt16(); Name = reverseReader.ReadPascalString(); UInt32 settingLength = reverseReader.ReadUInt32(); Data = reverseReader.ReadBytes((Int32)settingLength); if (reverseReader.BaseStream.Position % 2 == 1) reverseReader.ReadByte(); }
/// <summary> /// Load up the masking information of the supplied PSD /// </summary> private void LoadGlobalLayerMask(BinaryReverseReader reader) { Debug.WriteLine("LoadGlobalLayerMask started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); UInt32 maskLength = reader.ReadUInt32(); if (maskLength <= 0) return; _globalLayerMaskData = reader.ReadBytes((Int32)maskLength); }
public PsdFile Load(String filename) { using (FileStream stream = new FileStream(filename, FileMode.Open)) { //binary reverse reader reads data types in big-endian format. BinaryReverseReader reader = new BinaryReverseReader(stream); #region "Headers" //The headers area is used to check for a valid PSD file Debug.WriteLine("LoadHeader started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); String signature = new String(reader.ReadChars(4)); if (signature != "8BPS") throw new IOException("Bad or invalid file stream supplied"); //get the version number, should be 1 always if ((Version = reader.ReadInt16()) != 1) throw new IOException("Invalid version number supplied"); //get rid of the 6 bytes reserverd in PSD format reader.BaseStream.Position += 6; //get the rest of the information from the PSD file. //Everytime ReadInt16() is called, it reads 2 bytes. //Everytime ReadInt32() is called, it reads 4 bytes. _channels = reader.ReadInt16(); _rows = reader.ReadInt32(); _columns = reader.ReadInt32(); _depth = reader.ReadInt16(); ColorMode = (ColorModes)reader.ReadInt16(); //by end of headers, the reader has read 26 bytes into the file. #endregion //End Headers #region "ColorModeData" Debug.WriteLine("LoadColorModeData started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); UInt32 paletteLength = reader.ReadUInt32(); //readUint32() advances the reader 4 bytes. if (paletteLength > 0) { ColorModeData = reader.ReadBytes((Int32)paletteLength); } #endregion //End ColorModeData #region "Loading Image Resources" //This part takes extensive use of classes that I didn't write therefore //I can't document much on what they do. Debug.WriteLine("LoadingImageResources started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); _imageResources.Clear(); UInt32 imgResLength = reader.ReadUInt32(); if (imgResLength <= 0) return null; Int64 startPosition = reader.BaseStream.Position; while ((reader.BaseStream.Position - startPosition) < imgResLength) { ImageResource imgRes = new ImageResource(reader); ResourceIDs resID = (ResourceIDs)imgRes.ID; switch (resID) { case ResourceIDs.ResolutionInfo: imgRes = new ResolutionInfo(imgRes); break; case ResourceIDs.Thumbnail1: case ResourceIDs.Thumbnail2: imgRes = new Thumbnail(imgRes); break; case ResourceIDs.AlphaChannelNames: imgRes = new AlphaChannels(imgRes); break; } _imageResources.Add(imgRes); } // make sure we are not on a wrong offset, so set the stream position // manually reader.BaseStream.Position = startPosition + imgResLength; #endregion //End LoadingImageResources #region "Layer and Mask Info" //We are gonna load up all the layers and masking of the PSD now. Debug.WriteLine("LoadLayerAndMaskInfo - Part1 started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); UInt32 layersAndMaskLength = reader.ReadUInt32(); if (layersAndMaskLength <= 0) return null; //new start position startPosition = reader.BaseStream.Position; //Lets start by loading up all the layers LoadLayers(reader); //we are done the layers, load up the masks LoadGlobalLayerMask(reader); // make sure we are not on a wrong offset, so set the stream position // manually reader.BaseStream.Position = startPosition + layersAndMaskLength; #endregion //End Layer and Mask info #region "Loading Final Image" //we have loaded up all the information from the PSD file //into variables we can use later on. //lets finish loading the raw data that defines the image //in the picture. Debug.WriteLine("LoadImage started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); ImageCompression = (ImageCompression)reader.ReadInt16(); ImageData = new Byte[_channels][]; //--------------------------------------------------------------- if (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 += _rows * _channels * 2; } //--------------------------------------------------------------- Int32 bytesPerRow = 0; switch (_depth) { case 1: bytesPerRow = _columns;//NOT Shure break; case 8: bytesPerRow = _columns; break; case 16: bytesPerRow = _columns * 2; break; } //--------------------------------------------------------------- for (Int32 ch = 0; ch < _channels; ch++) { ImageData[ch] = new Byte[_rows * bytesPerRow]; switch (ImageCompression) { case ImageCompression.Raw: reader.Read(ImageData[ch], 0, ImageData[ch].Length); break; case ImageCompression.Rle: { for (Int32 i = 0; i < _rows; i++) { Int32 rowIndex = i * _columns; RleHelper.DecodedRow(reader.BaseStream, ImageData[ch], rowIndex, bytesPerRow); } } break; } } #endregion //End LoadingFinalImage } return this; } //end Load()
public Layer(BinaryReverseReader reverseReader, PsdFile psdFile) { AdjustmentInfo = new List<AdjusmentLayerInfo>(); SortedChannels = new SortedList<Int16, Channel>(); Channels = new List<Channel>(); Debug.WriteLine("Layer started at " + reverseReader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); PsdFile = psdFile; Rectangle localRectangle = new Rectangle { Y = reverseReader.ReadInt32(), X = reverseReader.ReadInt32() }; localRectangle.Height = reverseReader.ReadInt32() - localRectangle.Y; localRectangle.Width = reverseReader.ReadInt32() - localRectangle.X; Rect = localRectangle; Int32 numberOfChannels = reverseReader.ReadUInt16(); Channels.Clear(); for (Int32 channel = 0; channel < numberOfChannels; channel++) { Channel ch = new Channel(reverseReader, this); Channels.Add(ch); SortedChannels.Add(ch.ID, ch); } String signature = new String(reverseReader.ReadChars(4)); if (signature != "8BIM") throw (new IOException("Layer Channelheader error")); _blendModeKeyStr = new String(reverseReader.ReadChars(4)); Opacity = reverseReader.ReadByte(); Clipping = reverseReader.ReadByte() > 0; Byte flags = reverseReader.ReadByte(); _flags = new BitVector32(flags); reverseReader.ReadByte(); //padding Debug.WriteLine("Layer extraDataSize started at " + reverseReader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); // this is the total size of the MaskData, the BlendingRangesData, the // Name and the AdjustmenLayerInfo UInt32 extraDataSize = reverseReader.ReadUInt32(); // remember the start position for calculation of the // AdjustmenLayerInfo size Int64 extraDataStartPosition = reverseReader.BaseStream.Position; MaskData = new Mask(reverseReader, this); BlendingRangesData = new BlendingRanges(reverseReader, this); Int64 namePosition = reverseReader.BaseStream.Position; Name = reverseReader.ReadPascalString(); Int32 paddingBytes = (Int32)((reverseReader.BaseStream.Position - namePosition) % 4); Debug.Print("Layer {0} padding bytes after name", paddingBytes); reverseReader.ReadBytes(paddingBytes); AdjustmentInfo.Clear(); Int64 adjustmenLayerEndPos = extraDataStartPosition + extraDataSize; while (reverseReader.BaseStream.Position < adjustmenLayerEndPos) { try { AdjustmentInfo.Add(new AdjusmentLayerInfo(reverseReader, this)); } catch { reverseReader.BaseStream.Position = adjustmenLayerEndPos; } } // make shure we are not on a wrong offset, so set the stream position // manually reverseReader.BaseStream.Position = adjustmenLayerEndPos; }