/////////////////////////////////////////////////////////////////////////// private void LoadHeader(PsdBinaryReader reader) { Debug.WriteLine("LoadHeader started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); var signature = reader.ReadAsciiChars(4); if (signature != "8BPS") { throw new PsdInvalidException("The given stream is not a valid PSD file"); } Version = reader.ReadInt16(); if (Version != 1) { throw new PsdInvalidException("The PSD file has an unknown version"); } //6 bytes reserved reader.BaseStream.Position += 6; this.ChannelCount = reader.ReadInt16(); this.RowCount = reader.ReadInt32(); this.ColumnCount = reader.ReadInt32(); BitDepth = reader.ReadInt16(); ColorMode = (PsdColorMode)reader.ReadInt16(); }
/////////////////////////////////////////////////////////////////////////// private void LoadHeader(PsdBinaryReader reader) { Util.DebugMessage(reader.BaseStream, "Load, Begin, File header"); var signature = reader.ReadAsciiChars(4); if (signature != "8BPS") { throw new PsdInvalidException("The given stream is not a valid PSD file"); } Version = (PsdFileVersion)reader.ReadInt16(); Util.DebugMessage(reader.BaseStream, "Load, Info, Version {0}", (int)Version); if ((Version != PsdFileVersion.Psd) && (Version != PsdFileVersion.PsbLargeDocument)) { throw new PsdInvalidException("The PSD file has an unknown version"); } //6 bytes reserved reader.BaseStream.Position += 6; this.ChannelCount = reader.ReadInt16(); this.RowCount = reader.ReadInt32(); this.ColumnCount = reader.ReadInt32(); BitDepth = reader.ReadInt16(); ColorMode = (PsdColorMode)reader.ReadInt16(); Util.DebugMessage(reader.BaseStream, "Load, End, File header"); }
public EnumeratedDescriptor(PsdBinaryReader reader) { int length = reader.ReadInt32(); Type = reader.ReadAsciiChars(length == 0 ? 4 : length); length = reader.ReadInt32(); Enum = reader.ReadAsciiChars(length == 0 ? 4 : length); }
public OffsetStructure(PsdBinaryReader reader) { NameFromClassID = reader.ReadUnicodeString(); var length = reader.ReadInt32(); ClassID = reader.ReadAsciiChars(length == 0 ? 4 : length); Offset = reader.ReadInt32(); }
public PropertyStructure(PsdBinaryReader reader) { NameFromClassID = reader.ReadUnicodeString(); int length = reader.ReadInt32(); ClassID = reader.ReadAsciiChars(length == 0 ? 4 : length); length = reader.ReadInt32(); KeyID = reader.ReadAsciiChars(length == 0 ? 4 : length); }
public EnumeratedReference(PsdBinaryReader reader) { NameFromClassID = reader.ReadUnicodeString(); var length = reader.ReadInt32(); ClassID = reader.ReadAsciiChars(length == 0 ? 4 : length); length = reader.ReadInt32(); TypeID = reader.ReadAsciiChars(length == 0 ? 4 : length); length = reader.ReadInt32(); Enum = reader.ReadAsciiChars(length == 0 ? 4 : length); }
public static LayerInfo Load(PsdBinaryReader reader) { Debug.WriteLine("LayerInfoFactory.Load started at " + reader.BaseStream.Position); var signature = reader.ReadAsciiChars(4); if (signature != "8BIM") { throw new PsdInvalidException("Could not read LayerInfo due to signature mismatch."); } var key = reader.ReadAsciiChars(4); var length = reader.ReadInt32(); var startPosition = reader.BaseStream.Position; LayerInfo result; switch (key) { case "lsct": case "lsdk": result = new LayerSectionInfo(reader, key, length); break; case "luni": result = new LayerUnicodeName(reader); break; case "TySh": result = new LayerText(reader, length); break; case "tySH": result = new LayerTextType(reader); break; default: result = new RawLayerInfo(reader, key, length); break; } // May have additional padding applied. var endPosition = startPosition + length; if (reader.BaseStream.Position < endPosition) { reader.BaseStream.Position = endPosition; } // Documentation states that the length is even-padded. Actually: // 1. Most keys have 4-padded lengths. // 2. However, some keys (LMsk) have even-padded lengths. // 3. Other keys (Txt2, Lr16, Lr32) have unpadded lengths. // // The data is always 4-padded, regardless of the stated length. reader.ReadPadding(startPosition, 4); return(result); }
internal Channel(PsdBinaryReader reader, Layer layer) { Debug.WriteLine("Channel started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); ID = reader.ReadInt16(); Length = reader.ReadInt32(); Layer = layer; }
public ClassStructure(PsdBinaryReader reader, string key) { _key = key; NameFromClassID = reader.ReadUnicodeString(); int length = reader.ReadInt32(); ClassID = reader.ReadAsciiChars(length == 0 ? 4 : length); }
public RleRowLengths(PsdBinaryReader reader, int rowCount, bool isLargeDocument) : this(rowCount) { for (int i = 0; i < rowCount; i++) { Values[i] = isLargeDocument ? reader.ReadInt32() : reader.ReadUInt16(); } }
public DescriptorStructure(PsdBinaryReader reader, string key = null) { _key = key; ClassIDName = reader.ReadUnicodeString(); var length = reader.ReadInt32(); ClassID = reader.ReadAsciiChars(length == 0 ? 4 : length); var itemCount = reader.ReadInt32(); Descriptor = new Dictionary <string, Descriptor> (); for (int i = 0; i < itemCount; i++) { length = reader.ReadInt32(); string itemName = reader.ReadAsciiChars(length == 0 ? 4 : length); Descriptor.Add(itemName, DescriptorFactory.Load(reader)); } }
/////////////////////////////////////////////////////////////////////////// public BlendingRanges(PsdBinaryReader reader, Layer layer) { Debug.WriteLine("BlendingRanges started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); Layer = layer; var dataLength = reader.ReadInt32(); if (dataLength <= 0) return; Data = reader.ReadBytes(dataLength); }
public ListStructure(PsdBinaryReader reader) { int number = reader.ReadInt32(); List = new Descriptor[number]; for (int i = 0; i < number; ++i) { List[i] = DescriptorFactory.Load(reader); } }
public LayerSectionInfo(PsdBinaryReader reader, string key, int dataLength) { // The key for layer section info is documented to be "lsct". However, // some Photoshop files use the undocumented key "lsdk", with apparently // the same data format. this.key = key; SectionType = (LayerSectionType)reader.ReadInt32(); if (dataLength >= 12) { var signature = reader.ReadAsciiChars(4); if (signature != "8BIM") throw new PsdInvalidException("Invalid section divider signature."); BlendModeKey = reader.ReadAsciiChars(4); if (dataLength >= 16) { Subtype = (LayerSectionSubtype)reader.ReadInt32(); } } }
internal Channel(PsdBinaryReader reader, Layer layer) { Util.DebugMessage(reader.BaseStream, "Load, Begin, Channel"); ID = reader.ReadInt16(); Length = (layer.PsdFile.IsLargeDocument) ? reader.ReadInt64() : reader.ReadInt32(); Layer = layer; Util.DebugMessage(reader.BaseStream, $"Load, End, Channel, {ID}"); }
public LayerId(PsdBinaryReader reader, string key, long dataLength) : this(key) { if (dataLength == 4) { id = reader.ReadInt32(); } else { throw new PsdInvalidException("LayerId data length should be 4"); } }
public LayerSectionInfo(PsdBinaryReader reader, string key, int dataLength) { // The key for layer section info is documented to be "lsct". However, // some Photoshop files use the undocumented key "lsdk", with apparently // the same data format. this.key = key; SectionType = (LayerSectionType)reader.ReadInt32(); if (dataLength >= 12) { var signature = reader.ReadAsciiChars(4); if (signature != "8BIM") { throw new PsdInvalidException("Invalid section divider signature."); } BlendModeKey = reader.ReadAsciiChars(4); if (dataLength >= 16) { Subtype = (LayerSectionSubtype)reader.ReadInt32(); } } }
/////////////////////////////////////////////////////////////////////////// public BlendingRanges(PsdBinaryReader reader, Layer layer) { Debug.WriteLine("BlendingRanges started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); Layer = layer; var dataLength = reader.ReadInt32(); if (dataLength <= 0) { Data = new byte[0]; // modified to avoid exception at Save method (butaman) return; } Data = reader.ReadBytes(dataLength); }
/////////////////////////////////////////////////////////////////////////// public BlendingRanges(PsdBinaryReader reader, Layer layer) { Debug.WriteLine("BlendingRanges started at " + reader.BaseStream.Position.ToString(CultureInfo.InvariantCulture)); Layer = layer; var dataLength = reader.ReadInt32(); if (dataLength <= 0) { return; } Data = reader.ReadBytes(dataLength); }
/////////////////////////////////////////////////////////////////////////// public BlendingRanges(PsdBinaryReader reader, Layer layer) { Util.DebugMessage(reader.BaseStream, "Load, Begin, BlendingRanges"); Layer = layer; var dataLength = reader.ReadInt32(); if (dataLength <= 0) { return; } Data = reader.ReadBytes(dataLength); Util.DebugMessage(reader.BaseStream, "Load, End, BlendingRanges"); }
public static LayerInfo Load(PsdBinaryReader reader) { Debug.WriteLine("LayerInfoFactory.Load started at " + reader.BaseStream.Position); var signature = reader.ReadAsciiChars(4); if (signature != "8BIM") throw new PsdInvalidException("Could not read LayerInfo due to signature mismatch."); var key = reader.ReadAsciiChars(4); var length = reader.ReadInt32(); var startPosition = reader.BaseStream.Position; LayerInfo result; switch (key) { case "lsct": case "lsdk": result = new LayerSectionInfo(reader, key, length); break; case "luni": result = new LayerUnicodeName(reader); break; default: result = new RawLayerInfo(reader, key, length); break; } // May have additional padding applied. var endPosition = startPosition + length; if (reader.BaseStream.Position < endPosition) reader.BaseStream.Position = endPosition; // Documentation states that the length is even-padded. Actually: // 1. Most keys have 4-padded lengths. // 2. However, some keys (LMsk) have even-padded lengths. // 3. Other keys (Txt2, Lr16, Lr32) have unpadded lengths. // // The data is always 4-padded, regardless of the stated length. reader.ReadPadding(startPosition, 4); return result; }
public ShadowEffect(PsdBinaryReader r, string key) { m_key = key; int version = r.ReadInt32(); this.Blur = r.ReadUInt32(); this.Intensity = r.ReadUInt32(); this.Angle = r.ReadUInt32(); this.Distance = r.ReadUInt32(); Color shadowColor = r.ReadPSDColor(16, true); this.BlendModeKey = this.ReadBlendKey(r); this.Enabled = r.ReadBoolean(); this.UseGlobalAngle = r.ReadBoolean(); this.Opacity = r.ReadByte(); NativeColor = r.ReadPSDColor(16, true); this.Color = Util.FromArgb(Opacity, shadowColor); }
public ReferenceStructure(PsdBinaryReader reader) { int itemCount = reader.ReadInt32(); References = new Descriptor[itemCount]; for (int i = 0; i < itemCount; ++i) { var key = reader.ReadAsciiChars(4); Descriptor item = null; switch (key) { case Property: item = new PropertyStructure(reader); break; case Class: item = new ClassStructure(reader, key); break; case EnumeratedReferebce: item = new EnumeratedReference(reader); break; case Offset: item = new OffsetStructure(reader); break; case Identifier: item = new Integer(reader, key); break; case Index: item = new Integer(reader, key); break; case Name: item = new StringStructure(reader, key); break; } References [i] = item; } }
public LayerTypeToolInfo(PsdBinaryReader reader) { Version = reader.ReadInt16(); xx = reader.ReadDouble(); xy = reader.ReadDouble(); yx = reader.ReadDouble(); yy = reader.ReadDouble(); tx = reader.ReadDouble(); ty = reader.ReadDouble(); TextVersion = reader.ReadInt16(); TextDescriptorVestion = reader.ReadInt32(); TextData = new DescriptorStructure(reader); WarpVersion = reader.ReadInt16(); WarpDescriptorVestion = reader.ReadInt32(); WarpData = new DescriptorStructure(reader); left = reader.ReadInt32(); top = reader.ReadInt32(); right = reader.ReadInt32(); bottom = reader.ReadInt32(); }
public RowData(PsdBinaryReader reader) { int length = reader.ReadInt32(); Data = reader.ReadBytes(length); }
/////////////////////////////////////////////////////////////////////////// private void LoadHeader(PsdBinaryReader reader) { var signature = reader.ReadAsciiChars(4); if (signature != "8BPS") throw new PsdInvalidException("The given stream is not a valid PSD file"); Version = reader.ReadInt16(); if (Version != 1) throw new PsdInvalidException("The PSD file has an unknown version"); //6 bytes reserved reader.BaseStream.Position += 6; this.ChannelCount = reader.ReadInt16(); this.RowCount = reader.ReadInt32(); this.ColumnCount = reader.ReadInt32(); BitDepth = reader.ReadInt16(); ColorMode = (PsdColorMode)reader.ReadInt16(); }
public static DynVal ReadValue(PsdBinaryReader r, bool skipKey) { DynVal vt = new DynVal(); if (!skipKey) { vt.Name = GetMeaningOfFourCC(ReadSpecialString(r)); } //TODO: should be assigned a sequential number? vt.Type = parseTypeString(System.Text.Encoding.ASCII.GetString(r.ReadBytes(4))); switch (vt.Type) { case OSType.tdta: // 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(System.Text.Encoding.ASCII.GetString(r.ReadBytes(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.ReadUnicodeString(); //r.ReadPSDUnicodeString(); break; default: throw new Exception("Unhandled type: " + vt.Type); } return(vt); }
public Integer(PsdBinaryReader reader, string key) { _key = key; Value = reader.ReadInt32(); }
public static DynVal ReadValue(PsdBinaryReader r, bool skipKey) { DynVal vt = new DynVal(); if (!skipKey) { vt.Name = GetMeaningOfFourCC(ReadSpecialString(r)); } //TODO: should be assigned a sequential number? vt.Type = parseTypeString(r.ReadAsciiChars(4)); switch (vt.Type) { case OSType.tdta: // unknown r.ReadUInt32(); TdTaParser p = new TdTaParser(r); object o = p.ParseOneTree(); vt.Value = o; break; case OSType.Reference: vt.References = ReadValues(r, true); break; case OSType.Descriptor: vt.Value = 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 string tst = GetMeaningOfFourCC(r.ReadAsciiChars(4)); //#Prc #Pxl #Ang = percent / pixels / angle? double d = r.ReadDouble(); tst += ": " + d; vt.Value = tst; break; case OSType.Enumerated: string typeID = ReadSpecialString(r); string enumVal = ReadSpecialString(r); vt.Value = GetMeaningOfFourCC(typeID) + "." + GetMeaningOfFourCC(enumVal); 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.ReadUnicodeString(); //r.ReadPSDUnicodeString(); break; case OSType.LargeInteger: vt.Value = r.ReadInt64(); break; case OSType.Class: vt.Value = ReadClass(r); break; case OSType.Alias: vt.Value = ReadAlias(r); break; case OSType.PropertyRef: vt.Value = ReadProperty(r); break; case OSType.EnumeratedRef: vt.Value = ReadEnumeratedRef(r); break; case OSType.OffestRef: vt.Value = ReadOffset(r); break; case OSType.IdentifierRef: vt.Value = r.ReadAsciiChars(4); break; case OSType.IndexRef: vt.Value = r.ReadUInt16(); break; case OSType.NameRef: vt.Value = r.ReadAsciiChars(4); break; default: throw new Exception("Unhandled type: " + vt.Type); } return(vt); }
/// <summary> /// Loads the next LayerInfo record. /// </summary> /// <param name="reader">The file reader</param> /// <param name="psdFile">The PSD file.</param> /// <param name="globalLayerInfo">True if the LayerInfo record is being /// loaded from the end of the Layer and Mask Information section; /// false if it is being loaded from the end of a Layer record.</param> public static LayerInfo Load(PsdBinaryReader reader, PsdFile psdFile, bool globalLayerInfo, long fileEndPos) { Util.DebugMessage(reader.BaseStream, "Load, Begin, LayerInfo"); // Some keys use a signature of 8B64, but the identity of these keys // is undocumented. We will therefore accept either signature. var signature = reader.ReadAsciiChars(4); if ((signature != "8BIM") && (signature != "8B64")) { throw new PsdInvalidException( "LayerInfo signature invalid, must be 8BIM or 8B64."); } var key = reader.ReadAsciiChars(4); var hasLongLength = LayerInfoUtil.HasLongLength(key, psdFile.IsLargeDocument); LayerInfo result = new RawLayerInfo("dummy"); bool breakFromLoop = false; while (!breakFromLoop) { var baseStartPosition = reader.BaseStream.Position; var length = hasLongLength ? reader.ReadInt64() : reader.ReadInt32(); var startPosition = reader.BaseStream.Position; switch (key) { case "Layr": case "Lr16": case "Lr32": result = new InfoLayers(reader, psdFile, key, length); break; case "lsct": case "lsdk": result = new LayerSectionInfo(reader, key, (int)length); break; case "luni": result = new LayerUnicodeName(reader); break; case "lyid": result = new LayerId(reader, key, length); break; default: result = new RawLayerInfo(reader, signature, key, length); break; } // May have additional padding applied. var endPosition = startPosition + length; if (reader.BaseStream.Position < endPosition) { reader.BaseStream.Position = endPosition; } // Documentation states that the length is even-padded. Actually: // 1. Most keys have 4-padded lengths. // 2. However, some keys (LMsk) have even-padded lengths. // 3. Other keys (Txt2, Lr16, Lr32) have unpadded lengths. // // Photoshop writes data that is always 4-padded, even when the stated // length is not a multiple of 4. The length mismatch seems to occur // only on global layer info. We do not read extra padding in other // cases because third-party programs are likely to follow the spec. if (globalLayerInfo) { reader.ReadPadding(startPosition, 4); } //try if we can read the next signature if (reader.BaseStream.Position < fileEndPos) { var nowPosition = reader.BaseStream.Position; signature = reader.ReadAsciiChars(4); if ((signature != "8BIM") && (signature != "8B64")) { hasLongLength = true; reader.BaseStream.Position = baseStartPosition; } else { reader.BaseStream.Position = nowPosition; breakFromLoop = true; } } else { breakFromLoop = true; } } Util.DebugMessage(reader.BaseStream, "Load, End, LayerInfo, {0}, {1}", result.Signature, result.Key); return(result); }
/// <summary> /// Loads the next LayerInfo record. /// </summary> /// <param name="reader">The file reader</param> /// <param name="psdFile">The PSD file.</param> /// <param name="globalLayerInfo">True if the LayerInfo record is being /// loaded from the end of the Layer and Mask Information section; /// false if it is being loaded from the end of a Layer record.</param> /// <returns>LayerInfo object if it was successfully read, or null if /// padding was found.</returns> private static LayerInfo Load(PsdBinaryReader reader, PsdFile psdFile, bool globalLayerInfo) { Util.DebugMessage(reader.BaseStream, "Load, Begin, LayerInfo"); // Most keys have undocumented signatures, so we always accept either one. var signature = reader.ReadAsciiChars(4); if ((signature != "8BIM") && (signature != "8B64")) { throw new PsdInvalidException( $"{nameof(LayerInfo)} signature invalid, must be 8BIM or 8B64."); } var key = reader.ReadAsciiChars(4); var hasLongLength = LayerInfoUtil.HasLongLength(signature, key, psdFile.IsLargeDocument); var length = hasLongLength ? reader.ReadInt64() : reader.ReadInt32(); var startPosition = reader.BaseStream.Position; LayerInfo result; switch (key) { case "Layr": case "Lr16": case "Lr32": result = new InfoLayers(reader, psdFile, key, length); break; case "lsct": case "lsdk": result = new LayerSectionInfo(reader, key, (int)length); break; case "luni": result = new LayerUnicodeName(reader); break; default: result = new RawLayerInfo(reader, signature, key, length); break; } // May have additional padding applied. var endPosition = startPosition + length; if (reader.BaseStream.Position < endPosition) { reader.BaseStream.Position = endPosition; } // Documentation states that the length is even-padded. Actually: // 1. Most keys have 4-padded lengths. // 2. However, some keys (LMsk) have even-padded lengths. // 3. Other keys (Txt2, Lr16, Lr32) have unpadded lengths. // // Photoshop writes data that is always 4-padded, even when the stated // length is not a multiple of 4. The length mismatch seems to occur // only on global layer info. We do not read extra padding in other // cases because third-party programs are likely to follow the spec. if (globalLayerInfo) { reader.ReadPadding(startPosition, 4); } Util.DebugMessage(reader.BaseStream, $"Load, End, LayerInfo, {result.Signature}, {result.Key}"); return(result); }
internal Channel(PsdBinaryReader reader, Layer layer) { ID = reader.ReadInt16(); Length = reader.ReadInt32(); Layer = layer; }
public AliasStructure(PsdBinaryReader reader) { var length = reader.ReadInt32(); Path = reader.ReadAsciiChars(length); }