/////////////////////////////////////////////////////////////////////////// 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 Channel(BinaryReverseReader reader, Layer layer) { //从文档 五 - 4- 6) 开始读 ID = reader.ReadInt16(); Length = reader.ReadInt32(); Layer = layer; }
/// <summary> /// Initializes a new instance of the <see cref="ImageResource"/> class using a reader. /// </summary> /// <param name="reader">The reader to use to create the instance.</param> public ImageResource(BinaryReverseReader reader) { // read the OS type string osType = new string(reader.ReadChars(4)); if (osType != "8BIM" && osType != "MeSa") { throw new InvalidOperationException("Could not read an image resource"); } // read the ID ID = reader.ReadInt16(); // read the name Name = string.Empty; Name = reader.ReadPascalString(); // read the length of the data in bytes uint length = reader.ReadUInt32(); // read the actual data Data = reader.ReadBytes((int)length); if (reader.BaseStream.Position % 2L != 1L) { return; } reader.ReadByte(); }
public PsdText(BinaryReverseReader stream) { int textSize = stream.ReadInt32() * 2; StringBuilder buffer = new StringBuilder(textSize); bool stopReading = false; for (int ti = 0; ti < textSize; ti++) { char b = (char)stream.ReadByte(); if (b == 0) { stopReading = true; } else { stopReading = false; } if (!stopReading) { if (b == 13 || b == 10) { buffer.Append('\n'); } else { buffer.Append(b); } } } value = buffer.ToString(); }
/** * Instantiates a new psd descriptor. * * @param stream the stream * @throws IOException Signals that an I/O exception has occurred. */ public PsdDescriptor(BinaryReverseReader stream) { int unicstrlength = stream.ReadInt32() * 2; string unicstr = Encoding.Unicode.GetString(stream.ReadBytes(unicstrlength)); int classlength = stream.ReadInt32(); if (classlength != 0) { classId = Encoding.Default.GetString(stream.ReadBytes(classlength)); } else { classId = Encoding.Default.GetString(stream.ReadBytes(4)); } int itemsCount = stream.ReadInt32(); for (int i = 0; i < itemsCount; i++) { int size = stream.ReadInt32(); if (size == 0) { size = 4; } String key = Encoding.Default.GetString(stream.ReadBytes(size)); objects.Add(key, PsdObjectFactory.loadPsdObject(stream)); } }
/////////////////////////////////////////////////////////////////////////// private void LoadLayerAndMaskInfo(BinaryReverseReader reader) { Debug.WriteLine("LoadLayerAndMaskInfo started at " + reader.BaseStream.Position.ToString()); uint layersAndMaskLength = reader.ReadUInt32(); if (layersAndMaskLength <= 0) { return; } long startPosition = reader.BaseStream.Position; LoadLayers(reader); LoadGlobalLayerMask(reader); //----------------------------------------------------------------------- //Debug.Assert(reader.BaseStream.Position == startPosition + layersAndMaskLength, "LoadLayerAndMaskInfo"); //----------------------------------------------------------------------- // make sure we are not on a wrong offset, so set the stream position // manually reader.BaseStream.Position = startPosition + layersAndMaskLength; }
/////////////////////////////////////////////////////////////////////////// private void LoadHeader(BinaryReverseReader reader) { Debug.WriteLine("LoadHeader started at " + reader.BaseStream.Position.ToString()); string signature = new string(reader.ReadChars(4)); if (signature != "8BPS") { throw new IOException("The given stream is not a valid PSD file"); } m_version = reader.ReadInt16(); if (m_version != 1) { throw new IOException("The PSD file has an unknown version"); } //6 bytes reserved reader.BaseStream.Position += 6; m_channels = reader.ReadInt16(); m_rows = reader.ReadInt32(); m_columns = reader.ReadInt32(); m_depth = reader.ReadInt16(); m_colorMode = (ColorModes)reader.ReadInt16(); }
/// <summary> /// Initializes a new instance of the <see cref="ImageResource"/> class using a reader. /// </summary> /// <param name="reader">The reader to use to create the instance.</param> public ImageResource(BinaryReverseReader reader) { // read the OS type string osType = new string(reader.ReadChars(4)); if (osType != "8BIM" && osType != "MeSa") { throw new InvalidOperationException("Could not read an image resource"); } // read the ID ID = reader.ReadInt16(); // read the name Name = string.Empty; Name = reader.ReadPascalString(); // read the length of the data in bytes uint length = reader.ReadUInt32(); // read the actual data Data = reader.ReadBytes((int)length); if (reader.BaseStream.Position % 2L != 1L) { return; } reader.ReadByte(); }
/// <summary> /// Initializes a new instance of the <see cref="Mask"/> class. /// </summary> /// <param name="reader">The reader to use to initialize the instance.</param> /// <param name="layer">The layer this mask belongs to.</param> internal Mask(BinaryReverseReader reader, Layer layer) { Layer = layer; uint num1 = reader.ReadUInt32(); if (num1 <= 0U) { return; } long position = reader.BaseStream.Position; rect = new Rect(); rect.y = reader.ReadInt32(); rect.x = reader.ReadInt32(); rect.height = reader.ReadInt32() - rect.y; rect.width = reader.ReadInt32() - rect.x; DefaultColor = reader.ReadByte(); flags = new BitVector32(reader.ReadByte()); if ((int)num1 == 36) { reader.ReadByte(); // bit vector reader.ReadByte(); // ??? reader.ReadInt32(); // rect Y reader.ReadInt32(); // rect X reader.ReadInt32(); // rect total height (actual height = this - Y) reader.ReadInt32(); // rect total width (actual width = this - Y) } reader.BaseStream.Position = position + num1; }
/// <summary> /// Initializes a new instance of the <see cref="Mask"/> class. /// </summary> /// <param name="reader">The reader to use to initialize the instance.</param> /// <param name="layer">The layer this mask belongs to.</param> internal Mask(BinaryReverseReader reader, Layer layer) { Layer = layer; uint num1 = reader.ReadUInt32(); if (num1 <= 0U) { return; } long position = reader.BaseStream.Position; rect = new Rect(); rect.y = reader.ReadInt32(); rect.x = reader.ReadInt32(); rect.height = reader.ReadInt32() - rect.y; rect.width = reader.ReadInt32() - rect.x; DefaultColor = reader.ReadByte(); flags = new BitVector32(reader.ReadByte()); if ((int)num1 == 36) { reader.ReadByte(); // bit vector reader.ReadByte(); // ??? reader.ReadInt32(); // rect Y reader.ReadInt32(); // rect X reader.ReadInt32(); // rect total height (actual height = this - Y) reader.ReadInt32(); // rect total width (actual width = this - Y) } reader.BaseStream.Position = position + num1; }
private byte lookForwardByte(BinaryReverseReader stream) { byte b = readByte(stream); putBack(); return(b); }
private void LoadHeader(BinaryReverseReader reader) { string strHead = reader.ReadStringNew(4); if (strHead != "8BPS") { UnityEngine.Debug.LogError("The given stream is not a valid PSD file"); throw new IOException("The given stream is not a valid PSD file"); } _version = reader.ReadInt16(); if (_version != 1) { UnityEngine.Debug.LogError("The PSD file has an invalid version"); throw new IOException("The PSD file has an invalid version"); } reader.BaseStream.Position += 6L; _channels = reader.ReadInt16(); _height = reader.ReadInt32(); _width = reader.ReadInt32(); _depth = reader.ReadInt16(); ColorMode = (ColorModes)reader.ReadInt16(); }
private void skipString(BinaryReverseReader stream, string str) { for (int i = 0; i < str.Length; i++) { char streamCh = (char)readByte(stream); Debug.Assert(streamCh == str[i], "char " + streamCh + " mustBe " + str[i]); } }
/** * Instantiates a new psd list. * * @param stream the stream * @throws IOException Signals that an I/O exception has occurred. */ public PsdList(BinaryReverseReader stream) { int itemsCount = stream.ReadInt32(); for (int i = 0; i < itemsCount; i++) { objects.Add(PsdObjectFactory.loadPsdObject(stream)); } }
public Matrix2D(BinaryReverseReader r) { this.M11 = r.ReadDouble(); this.M12 = r.ReadDouble(); this.M13 = r.ReadDouble(); this.M21 = r.ReadDouble(); this.M22 = r.ReadDouble(); this.M23 = r.ReadDouble(); }
internal Channel(BinaryReverseReader reader, Layer layer) { Debug.WriteLine("Channel started at " + reader.BaseStream.Position.ToString()); m_id = reader.ReadInt16(); Length = reader.ReadInt32(); m_layer = layer; }
////////////////////////////////////////////////////////////////// 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; } } }
private void LoadGlobalLayerMask(BinaryReverseReader reader) { uint num = reader.ReadUInt32(); if (num <= 0U) { return; } reader.ReadBytes((int)num); }
public int getSize(BinaryReverseReader str) { int size = str.ReadInt32(); if (size == 0) { size = 4; } return(size); }
/// <summary> /// A peculiar type of ascii string frequently used throughout the Descriptor data structure. /// First 4 bytes are length (in bytes), followed by string. If length is 0, length is assumed to be 4. No idea why they did this... RLE compression? /// </summary> /// <param name="r"></param> /// <returns></returns> public static string ReadSpecialString(BinaryReverseReader r) { uint length = r.ReadUInt32(); if (length == 0) { length = 4; } return(new string(r.ReadPSDChars((int)length))); }
private void LoadColorModeData(BinaryReverseReader reader) { uint num = reader.ReadUInt32(); if (num <= 0U) { return; } ColorModeData = reader.ReadBytes((int)num); }
private void skipWhitespaces(BinaryReverseReader stream) { byte b; do { b = readByte(stream); } while (b == ' ' || b == 10 || b == 9); putBack(); }
public void Load(Stream stream) { BinaryReverseReader reader = new BinaryReverseReader(stream); LoadHeader(reader); LoadColorModeData(reader); LoadImageResources(reader); LoadLayerAndMaskInfo(reader); LoadImage(reader); }
private void LoadColorModeData(BinaryReverseReader reader) { Debug.WriteLine("LoadColorModeData started at " + reader.BaseStream.Position.ToString()); uint paletteLength = reader.ReadUInt32(); if (paletteLength > 0) { ColorModeData = reader.ReadBytes((int)paletteLength); } }
public static DynVal ReadDescriptor(BinaryReverseReader r) { DynVal v = new DynVal(); v.UnicodeName = r.ReadPSDUnicodeString(); v.Type = OSType.Descriptor; v.classID = ReadSpecialString(r); v.Name = GetMeaningOfFourCC(v.classID); v.Children = DynVal.ReadValues(r, false); return(v); }
public void Load(Stream stream) { var reader = new BinaryReverseReader(stream); LoadHeader(reader); LoadColorModeData(reader); LoadImageResources(reader); LoadLayerAndMaskInfo(reader); LoadImage(reader); }
/// <summary> /// Reads the color mode data from the reader. /// </summary> /// <param name="reader">The reader to use to read the color mode data.</param> private void LoadColorModeData(BinaryReverseReader reader) { uint num = reader.ReadUInt32(); // Debug.Log(Time.time + "LoadColorModeData number=" + num); if (num <= 0U) { return; } ColorModeData = reader.ReadBytes((int)num); }
/// <summary> /// Reads all of the layers from the reader. /// </summary> /// <param name="reader">The reader to use to read the layers.</param> private void LoadLayers(BinaryReverseReader reader) { int num1 = reader.ReadInt32(); //.ReadUInt32(); ;// reader.ReadUInt32(); // Debug.Log(Time.time + "LoadLayers num1=" + num1 + ",return ?" + (num1 <= 0U)); if (num1 <= 0U) { return; } long position = reader.BaseStream.Position; short num2 = reader.ReadInt16(); if (num2 < 0) { AbsoluteAlpha = true; num2 = Math.Abs(num2); } Layers.Clear(); //Debug.Log(Time.time + "LoadLayers num2=" + num2 + ",return ?" + (num2 == 0)); if (num2 == 0) { return; } for (int index = 0; index < (int)num2; ++index) { Layers.Add(new Layer(reader, this)); } foreach (Layer layer in Layers) { foreach (Channel channel in layer.Channels) { if (channel.ID != -2) { channel.LoadPixelData(reader); } } layer.MaskData.LoadPixelData(reader); } if (reader.BaseStream.Position % 2L == 1L) { reader.ReadByte(); } reader.BaseStream.Position = position + num1; }
public BlendingRanges(BinaryReverseReader reader) { //读文档 五 - 4 - 15) 到 五 - 4 - 20) int count = reader.ReadInt32(); if (count <= 0) { return; } byte[] data = reader.ReadBytes(count); }
public PsdTextData(BinaryReverseReader stream) { int size = stream.ReadInt32(); byte[] array = new byte[size]; offset = stream.BaseStream.Position; array = stream.ReadBytes(size); Stream str = new MemoryStream(array); BinaryReverseReader br = new BinaryReverseReader(str); properties = readMap(br); }
/// <summary> /// Parses either a list format or a dictionary format. Specify skipKeys=true to parse the list format /// </summary> /// <param name="r"></param> /// <param name="skipKeys"></param> /// <returns></returns> public static List <DynVal> ReadValues(BinaryReverseReader r, bool skipKeys) { int numValues = (int)r.ReadUInt32(); List <DynVal> values = new List <DynVal>(numValues); for (int i = 0; i < numValues; i++) { values.Add(ReadValue(r, skipKeys)); } return(values); }
private void LoadImageResources(BinaryReverseReader reader) { _imageResources.Clear(); uint num = reader.ReadUInt32(); if (num <= 0U) { return; } long position = reader.BaseStream.Position; while (reader.BaseStream.Position - position < num) { ImageResource imgRes = new ImageResource(reader); switch ((ResourceIDs)imgRes.ID) { case ResourceIDs.XMLInfo: _metaData = XDocument.Load(XmlReader.Create(new MemoryStream(imgRes.Data))); IEnumerable <XElement> source = _metaData.Descendants(XName.Get("Category", "http://ns.adobe.com/photoshop/1.0/")); if (source.Any()) { _category = source.First().Value; } break; case ResourceIDs.ResolutionInfo: imgRes = new ResolutionInfo(imgRes); break; case ResourceIDs.AlphaChannelNames: imgRes = new AlphaChannels(imgRes); break; case ResourceIDs.PsCCOrignPathInfo: imgRes = new AlphaChannels(imgRes); break; case ResourceIDs.PsCCPathSelectionState: imgRes = new AlphaChannels(imgRes); break; case ResourceIDs.TransparencyIndex: Debug.Log("have transparent "); break; } _imageResources.Add(imgRes); } reader.BaseStream.Position = position + num; }
private void LoadGlobalLayerMask(BinaryReverseReader reader) { Debug.WriteLine("LoadGlobalLayerMask started at " + reader.BaseStream.Position.ToString()); uint maskLength = reader.ReadUInt32(); if (maskLength <= 0) { return; } GlobalLayerMaskData = reader.ReadBytes((int)maskLength); }
/// <summary> /// Initializes a new instance of the <see cref="Mask"/> class. /// </summary> /// <param name="reader">The reader to use to initialize the instance.</param> /// <param name="layer">The layer this mask belongs to.</param> internal Mask(BinaryReverseReader reader, Layer layer) { Layer = layer; uint num1 = reader.ReadUInt32(); Debug.Log("mask num1=" + num1); if (num1 <= 0U) { return; } long position = reader.BaseStream.Position; rect = new Rect(); rect.y = reader.ReadInt32(); rect.x = reader.ReadInt32(); rect.height = reader.ReadInt32() - rect.y; rect.width = reader.ReadInt32() - rect.x; DefaultColor = reader.ReadByte(); flags = new BitVector32(reader.ReadByte()); int tempNum1 = -1; int tempNum2 = -1; int tempNum3 = -1; int tempNum4 = -1; int tempNum5 = -1; int tempNum6 = -1; if ((int)num1 == 36) { tempNum1 = reader.ReadByte(); // bit vector tempNum2 = reader.ReadByte(); // ??? tempNum3 = reader.ReadInt32(); // rect Y tempNum4 = reader.ReadInt32(); // rect X tempNum5 = reader.ReadInt32(); // rect total height (actual height = this - Y) tempNum6 = reader.ReadInt32(); // rect total width (actual width = this - Y) } Debug.Log("燕茹 Mask data" + ",num1=" + num1 + ",DefaultColor=" + DefaultColor + ",flags=" + flags + ",tempNum1=" + tempNum1 + ",tempNum2=" + tempNum2 + ",tempNum3=" + tempNum3 + ",tempNum4=" + tempNum4 + ",tempNum5=" + tempNum5 + ",tempNum6=" + tempNum6 ); reader.BaseStream.Position = position + num1; }
/// <summary> /// Initializes a new instance of the <see cref="AdjustmentLayerInfo"/> class. /// </summary> /// <param name="reader">The reader containing the PSD file data</param> /// <param name="layer">The layer that this adjustment info belongs to</param> public AdjustmentLayerInfo(BinaryReverseReader reader, Layer layer) { if (new string(reader.ReadChars(4)) != "8BIM") { throw new IOException("Could not read an image resource"); } Key = new string(reader.ReadChars(4)); if (Key == "lfx2" || Key == "lrFX") { layer.HasEffects = true; } uint length = reader.ReadUInt32(); Data = reader.ReadBytes((int)length); }
/// <summary> /// Initializes a new instance of the <see cref="PsdFile"/> class. /// </summary> /// <param name="fileName">The filepath of the PSD file to open.</param> public PsdFile(string fileName) { Category = string.Empty; Version = 1; Layers = new List<Layer>(); ImageResources = new List<ImageResource>(); using (FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { BinaryReverseReader reader = new BinaryReverseReader(fileStream); LoadHeader(reader); LoadColorModeData(reader); LoadImageResources(reader); LoadLayerAndMaskInfo(reader); LoadImage(reader); } }
/////////////////////////////////////////////////////////////////////////// private void LoadHeader(BinaryReverseReader reader) { Debug.WriteLine("LoadHeader started at " + reader.BaseStream.Position); var signature = new string(reader.ReadChars(4)); if (signature != "8BPS") throw new IOException("The given stream is not a valid PSD file"); m_version = reader.ReadInt16(); if (m_version != 1) throw new IOException("The PSD file has an unkown version"); //6 bytes reserved reader.BaseStream.Position += 6; m_channels = reader.ReadInt16(); m_rows = reader.ReadInt32(); m_columns = reader.ReadInt32(); m_depth = reader.ReadInt16(); m_colorMode = (ColorModes) reader.ReadInt16(); }
internal void LoadPixelData(BinaryReverseReader reader) { Debug.WriteLine("Mask.LoadPixelData started at " + reader.BaseStream.Position); if (m_rect.IsEmpty || m_layer.SortedChannels.ContainsKey(-2) == false) return; Channel maskChannel = m_layer.SortedChannels[-2]; maskChannel.Data = reader.ReadBytes(maskChannel.Length); using (BinaryReverseReader readerImg = maskChannel.DataReader) { maskChannel.ImageCompression = (ImageCompression) readerImg.ReadInt16(); int bytesPerRow = 0; switch (m_layer.PsdFile.Depth) { case 1: bytesPerRow = m_rect.Width; //NOT Shure 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: { var rowLenghtList = new int[m_rect.Height]; for (int i = 0; i < rowLenghtList.Length; i++) rowLenghtList[i] = readerImg.ReadInt16(); for (int i = 0; i < m_rect.Height; i++) { int rowIndex = i*m_rect.Width; RleHelper.DecodedRow(readerImg.BaseStream, maskChannel.ImageData, rowIndex, bytesPerRow); } } break; } m_imageData = (byte[]) maskChannel.ImageData.Clone(); } }
/////////////////////////////////////////////////////////////////////////// internal Mask(BinaryReverseReader reader, Layer layer) { Debug.WriteLine("Mask started at " + reader.BaseStream.Position); m_layer = layer; uint maskLength = reader.ReadUInt32(); if (maskLength <= 0) return; long startPosition = reader.BaseStream.Position; //----------------------------------------------------------------------- m_rect = new Rectangle { Y = reader.ReadInt32(), X = reader.ReadInt32() }; m_rect.Height = reader.ReadInt32() - m_rect.Y; m_rect.Width = reader.ReadInt32() - m_rect.X; m_defaultColor = reader.ReadByte(); //----------------------------------------------------------------------- byte flags = reader.ReadByte(); m_flags = new BitVector32(flags); //----------------------------------------------------------------------- if (maskLength == 36) { //var realFlags = new BitVector32(reader.ReadByte()); //var realUserMaskBackground = reader.ReadByte(); #if false var y = reader.ReadInt32(); var x = reader.ReadInt32(); var rect = new Rectangle { Y = y, X = x, Height = reader.ReadInt32() - y, Width = reader.ReadInt32() - x }; #else // this is just reading unused bytes... reader.ReadInt32(); reader.ReadInt32(); reader.ReadInt32(); reader.ReadInt32(); #endif } // there is other stuff following, but we will ignore this. reader.BaseStream.Position = startPosition + maskLength; }
/////////////////////////////////////////////////////////////////////////// private void LoadLayerAndMaskInfo(BinaryReverseReader reader) { Debug.WriteLine("LoadLayerAndMaskInfo started at " + reader.BaseStream.Position); uint layersAndMaskLength = reader.ReadUInt32(); if (layersAndMaskLength <= 0) return; long startPosition = reader.BaseStream.Position; LoadLayers(reader); LoadGlobalLayerMask(reader); //----------------------------------------------------------------------- //Debug.Assert(reader.BaseStream.Position == startPosition + layersAndMaskLength, "LoadLayerAndMaskInfo"); //----------------------------------------------------------------------- // make shure we are not on a wrong offset, so set the stream position // manually reader.BaseStream.Position = startPosition + layersAndMaskLength; }
private void LoadGlobalLayerMask(BinaryReverseReader reader) { Debug.WriteLine("LoadGlobalLayerMask started at " + reader.BaseStream.Position); uint maskLength = reader.ReadUInt32(); if (maskLength <= 0) return; GlobalLayerMaskData = reader.ReadBytes((int) maskLength); }
////////////////////////////////////////////////////////////////// public ImageResource(BinaryReverseReader reader) { m_osType = new string(reader.ReadChars(4)); if (m_osType != "8BIM" && m_osType != "MeSa") { throw new InvalidOperationException("Could not read an image resource"); } m_id = reader.ReadInt16(); m_name = reader.ReadPascalString(); uint settingLength = reader.ReadUInt32(); m_data = reader.ReadBytes((int) settingLength); if (reader.BaseStream.Position%2 == 1) reader.ReadByte(); }
private void LoadColorModeData(BinaryReverseReader reader) { Debug.WriteLine("LoadColorModeData started at " + reader.BaseStream.Position); uint paletteLength = reader.ReadUInt32(); if (paletteLength > 0) { ColorModeData = reader.ReadBytes((int) paletteLength); } }
/////////////////////////////////////////////////////////////////////////// private void LoadLayers(BinaryReverseReader reader) { Debug.WriteLine("LoadLayers started at " + reader.BaseStream.Position.ToString()); uint layersInfoSectionLength = reader.ReadUInt32(); if (layersInfoSectionLength <= 0) return; long startPosition = reader.BaseStream.Position; short numberOfLayers = reader.ReadInt16(); // If <0, then number of layers is absolute value, // and the first alpha channel contains the transparency data for // the merged result. if (numberOfLayers < 0) { AbsoluteAlpha = true; numberOfLayers = Math.Abs(numberOfLayers); } m_layers.Clear(); if (numberOfLayers == 0) return; for (int i = 0; i < numberOfLayers; i++) { m_layers.Add(new Layer(reader, this)); } PrivateThreadPool threadPool = new PrivateThreadPool(); foreach (Layer layer in m_layers) { foreach (Layer.Channel channel in layer.Channels) { if (channel.ID != -2) { channel.LoadPixelData(reader); DecompressChannelContext dcc = new DecompressChannelContext(channel); WaitCallback waitCallback = new WaitCallback(dcc.DecompressChannel); threadPool.QueueUserWorkItem(waitCallback); } } layer.MaskData.LoadPixelData(reader); } threadPool.Drain(); //----------------------------------------------------------------------- if (reader.BaseStream.Position % 2 == 1) reader.ReadByte(); //----------------------------------------------------------------------- // make sure we are not on a wrong offset, so set the stream position // manually reader.BaseStream.Position = startPosition + layersInfoSectionLength; }
/// <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(); } }
public Layer(BinaryReverseReader reader, PsdFile psdFile) { Debug.WriteLine("Layer started at " + reader.BaseStream.Position); m_psdFile = psdFile; m_rect = new Rectangle { Y = reader.ReadInt32(), X = reader.ReadInt32() }; m_rect.Height = reader.ReadInt32() - m_rect.Y; m_rect.Width = reader.ReadInt32() - m_rect.X; //----------------------------------------------------------------------- int numberOfChannels = reader.ReadUInt16(); m_channels.Clear(); for (int channel = 0; channel < numberOfChannels; channel++) { var ch = new Channel(reader, this); m_channels.Add(ch); m_sortedChannels.Add(ch.ID, ch); } //----------------------------------------------------------------------- var signature = new string(reader.ReadChars(4)); if (signature != LayerConstants.EightBimSignature) throw (new IOException("Layer Channelheader error!")); m_blendModeKey = new string(reader.ReadChars(4)); m_opacity = reader.ReadByte(); m_clipping = reader.ReadByte() > 0; //----------------------------------------------------------------------- byte flags = reader.ReadByte(); m_flags = new BitVector32(flags); //----------------------------------------------------------------------- reader.ReadByte(); //padding //----------------------------------------------------------------------- Debug.WriteLine("Layer extraDataSize started at " + reader.BaseStream.Position); // this is the total size of the MaskData, the BlendingRangesData, the // Name and the AdjustmenLayerInfo uint extraDataSize = reader.ReadUInt32(); // remember the start position for calculation of the // AdjustmenLayerInfo size long extraDataStartPosition = reader.BaseStream.Position; m_maskData = new Mask(reader, this); m_blendingRangesData = new BlendingRanges(reader, this); //----------------------------------------------------------------------- var namePosition = reader.BaseStream.Position; m_name = reader.ReadPascalString(); var paddingBytes = (int)((reader.BaseStream.Position - namePosition) % 4); Debug.Print("Layer {0} padding bytes after name", paddingBytes); reader.ReadBytes(paddingBytes); //----------------------------------------------------------------------- m_adjustmentInfo.Clear(); long adjustmenLayerEndPos = extraDataStartPosition + extraDataSize; while (reader.BaseStream.Position < adjustmenLayerEndPos) { try { m_adjustmentInfo.Add(new AdjusmentLayerInfo(reader, this)); } catch { reader.BaseStream.Position = adjustmenLayerEndPos; } } //----------------------------------------------------------------------- // make shure we are not on a wrong offset, so set the stream position // manually reader.BaseStream.Position = adjustmenLayerEndPos; }
public AdjusmentLayerInfo(BinaryReverseReader reader, Layer layer) { Debug.WriteLine("AdjusmentLayerInfo started at " + reader.BaseStream.Position); m_layer = layer; var signature = new string(reader.ReadChars(4)); if (signature != LayerConstants.EightBimSignature) { throw new IOException("Could not read an image resource"); } m_key = new string(reader.ReadChars(4)); uint dataLength = reader.ReadUInt32(); m_data = reader.ReadBytes((int) dataLength); }
/////////////////////////////////////////////////////////////////////////// public BlendingRanges(BinaryReverseReader reader, Layer layer) { Debug.WriteLine("BlendingRanges started at " + reader.BaseStream.Position); m_layer = layer; int dataLength = reader.ReadInt32(); if (dataLength <= 0) return; m_data = reader.ReadBytes(dataLength); }
/////////////////////////////////////////////////////////////////////////// private void LoadImageResources(BinaryReverseReader reader) { Debug.WriteLine("LoadImageResources started at " + reader.BaseStream.Position); m_imageResources.Clear(); uint imgResLength = reader.ReadUInt32(); if (imgResLength <= 0) return; long startPosition = reader.BaseStream.Position; while ((reader.BaseStream.Position - startPosition) < imgResLength) { var imgRes = new ImageResource(reader); var 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; } m_imageResources.Add(imgRes); } //----------------------------------------------------------------------- // make shure we are not on a wrong offset, so set the stream position // manually reader.BaseStream.Position = startPosition + imgResLength; }
/// <summary> /// Parses either a list format or a dictionary format. Specify skipKeys=true to parse the list format /// </summary> /// <param name="r"></param> /// <param name="skipKeys"></param> /// <returns></returns> public static List<DynVal> ReadValues(BinaryReverseReader r, bool skipKeys) { int numValues = (int)r.ReadUInt32(); List<DynVal> values = new List<DynVal>(numValues); for (int i = 0; i < numValues; i++) values.Add(ReadValue(r, skipKeys)); return values; }
/////////////////////////////////////////////////////////////////////////// private void LoadLayers(BinaryReverseReader reader) { Debug.WriteLine("LoadLayers started at " + reader.BaseStream.Position); uint layersInfoSectionLength = reader.ReadUInt32(); if (layersInfoSectionLength <= 0) return; long startPosition = reader.BaseStream.Position; short numberOfLayers = reader.ReadInt16(); // If <0, then number of layers is absolute value, // and the first alpha channel contains the transparency data for // the merged result. if (numberOfLayers < 0) { AbsoluteAlpha = true; numberOfLayers = Math.Abs(numberOfLayers); } m_layers.Clear(); if (numberOfLayers == 0) return; for (int i = 0; i < numberOfLayers; i++) { m_layers.Add(new Layer(reader, this)); } foreach (Layer layer in m_layers) { foreach (Layer.Channel channel in layer.Channels) { if (channel.ID != -2) channel.LoadPixelData(reader); } layer.MaskData.LoadPixelData(reader); } //----------------------------------------------------------------------- if (reader.BaseStream.Position%2 == 1) reader.ReadByte(); //----------------------------------------------------------------------- // make shure we are not on a wrong offset, so set the stream position // manually reader.BaseStream.Position = startPosition + layersInfoSectionLength; }
public static DynVal ReadValue(BinaryReverseReader r, bool skipKey) { DynVal vt = new DynVal(); if (!skipKey) vt.Name = GetMeaningOfFourCC(ReadSpecialString(r)); //TODO: should be assigned a sequential number? vt.Type = parseTypeString(new string(r.ReadPSDChars(4))); switch (vt.Type) { case OSType.tdta: uint unknown = r.ReadUInt32(); TdTaParser p = new TdTaParser(r); object o = p.ParseOneTree(); vt.Value = o; break; case OSType.Descriptor: vt = DynVal.ReadDescriptor(r); break; case OSType.List: vt.Children = ReadValues(r,true); break; case OSType.Double: vt.Value = r.ReadDouble(); break; case OSType.UnitFloat: //Unif float //TODO: need a specific type for this, with a double and a type (percent/pixel)? string tst = GetMeaningOfFourCC(new string(r.ReadPSDChars(4))); //#Prc #Pxl #Ang = percent / pixels / angle? double d = r.ReadDouble(); tst += ": " + d; vt.Value = tst; break; case OSType.Enumerated: string namesp = ReadSpecialString(r); string item = ReadSpecialString(r); //vt.Value = namesp + "." + item; //TODO: cast to real enum vt.Value = GetMeaningOfFourCC(namesp) + "." + GetMeaningOfFourCC(item); break; case OSType.Integer: vt.Value = r.ReadInt32(); //4 byte integer break; case OSType.Boolean: vt.Value = r.ReadBoolean(); break; case OSType.String: vt.Value = r.ReadPSDUnicodeString(); break; default: throw new Exception("Unhandled type: " + vt.Type); } return vt; }
/////////////////////////////////////////////////////////////////////////// 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; } } }
/// <summary> /// Reads the text information for the layer. /// </summary> /// <param name="dataReader">The reader to use to read the text data.</param> private void ReadTextLayer(BinaryReverseReader dataReader) { IsTextLayer = true; // read the text layer's text string dataReader.Seek("/Text"); dataReader.ReadBytes(4); Text = dataReader.ReadString(); // read the text justification dataReader.Seek("/Justification "); int justification = dataReader.ReadByte() - 48; Justification = TextJustification.Left; if (justification == 1) { Justification = TextJustification.Right; } else if (justification == 2) { Justification = TextJustification.Center; } // read the font size dataReader.Seek("/FontSize "); FontSize = dataReader.ReadFloat(); // read the font fill color dataReader.Seek("/FillColor"); dataReader.Seek("/Values [ "); float alpha = dataReader.ReadFloat(); dataReader.ReadByte(); float red = dataReader.ReadFloat(); dataReader.ReadByte(); float green = dataReader.ReadFloat(); dataReader.ReadByte(); float blue = dataReader.ReadFloat(); FillColor = new Color(red * byte.MaxValue, green * byte.MaxValue, blue * byte.MaxValue, alpha * byte.MaxValue); // read the font name dataReader.Seek("/FontSet "); dataReader.Seek("/Name"); dataReader.ReadBytes(4); FontName = dataReader.ReadString(); // read the warp style dataReader.Seek("warpStyle"); dataReader.Seek("warpStyle"); dataReader.ReadBytes(3); int num13 = dataReader.ReadByte(); WarpStyle = string.Empty; for (; num13 > 0; --num13) { string str = WarpStyle + dataReader.ReadChar(); WarpStyle = str; } }
public FontInfo(BinaryReverseReader r) { this.Mark = r.ReadUInt16(); this.FontType = r.ReadUInt32(); this.FontName = r.ReadPascalString(); this.FontFamilyName = r.ReadPascalString(); this.FontStyleName = r.ReadPascalString(); this.Script = r.ReadUInt16(); ushort NumDesignAxesVectors = r.ReadUInt16(); this.DesignVectors = new List<uint>(); for (int vectorNum = 0; vectorNum < NumDesignAxesVectors; vectorNum++) this.DesignVectors.Add(r.ReadUInt32()); }
//public BitVector32 Flags //{ // get { return flags; } //} /// <summary> /// Initializes a new instance of the <see cref="Layer"/> class using the provided reader containing the PSD file data. /// </summary> /// <param name="reader">The reader containing the PSD file data.</param> /// <param name="psdFile">The PSD file to set as the parent.</param> public Layer(BinaryReverseReader reader, PsdFile psdFile) { Children = new List<Layer>(); PsdFile = psdFile; // read the rect Rect rect = new Rect(); rect.y = reader.ReadInt32(); rect.x = reader.ReadInt32(); rect.height = reader.ReadInt32() - rect.y; rect.width = reader.ReadInt32() - rect.x; Rect = rect; // read the channels int channelCount = reader.ReadUInt16(); Channels = new List<Channel>(); SortedChannels = new SortedList<short, Channel>(); for (int index = 0; index < channelCount; ++index) { Channel channel = new Channel(reader, this); Channels.Add(channel); SortedChannels.Add(channel.ID, channel); } // read the header and verify it if (new string(reader.ReadChars(4)) != "8BIM") { throw new IOException("Layer Channelheader error!"); } // read the blend mode key (unused) (defaults to "norm") reader.ReadChars(4); // read the opacity Opacity = reader.ReadByte(); // read the clipping (unused) (< 0 = base, > 0 = non base) reader.ReadByte(); // read all of the flags (protectTrans, visible, obsolete, ver5orLater, pixelDataIrrelevant) flags = new BitVector32(reader.ReadByte()); // skip a padding byte reader.ReadByte(); uint num3 = reader.ReadUInt32(); long position1 = reader.BaseStream.Position; MaskData = new Mask(reader, this); BlendingRangesData = new BlendingRanges(reader); long position2 = reader.BaseStream.Position; // read the name Name = reader.ReadPascalString(); // read the adjustment info int count = (int)((reader.BaseStream.Position - position2) % 4L); reader.ReadBytes(count); AdjustmentInfo = new List<AdjustmentLayerInfo>(); long num4 = position1 + num3; while (reader.BaseStream.Position < num4) { try { AdjustmentInfo.Add(new AdjustmentLayerInfo(reader, this)); } catch { reader.BaseStream.Position = num4; } } foreach (AdjustmentLayerInfo adjustmentLayerInfo in AdjustmentInfo) { if (adjustmentLayerInfo.Key == "TySh") { ReadTextLayer(adjustmentLayerInfo.DataReader); } else if (adjustmentLayerInfo.Key == "luni") { // read the unicode name BinaryReverseReader dataReader = adjustmentLayerInfo.DataReader; dataReader.ReadBytes(3); dataReader.ReadByte(); Name = dataReader.ReadString().TrimEnd(new char[1]); } } reader.BaseStream.Position = num4; }
/// <summary> /// A peculiar type of ascii string frequently used throughout the Descriptor data structure. /// First 4 bytes are length (in bytes), followed by string. If length is 0, length is assumed to be 4. No idea why they did this... RLE compression? /// </summary> /// <param name="r"></param> /// <returns></returns> public static string ReadSpecialString(BinaryReverseReader r) { uint length = r.ReadUInt32(); if (length == 0) length = 4; return new string(r.ReadPSDChars((int)length)); }
public static DynVal ReadDescriptor(BinaryReverseReader r) { DynVal v = new DynVal(); v.UnicodeName = r.ReadPSDUnicodeString(); v.Type = OSType.Descriptor; v.classID = ReadSpecialString(r); v.Name = GetMeaningOfFourCC(v.classID); v.Children = DynVal.ReadValues(r, false); return v; }
////////////////////////////////////////////////////////////////// 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; } } }
internal Channel(BinaryReverseReader reader, Layer layer) { Debug.WriteLine("Channel started at " + reader.BaseStream.Position); m_id = reader.ReadInt16(); Length = reader.ReadInt32(); m_layer = layer; }