public LayerAndMaskInformationSection Read(Stream stream, FileVersion version) { var section = new LayerAndMaskInformationSection(); var reader = new BigEndianBinaryReader(stream); section.Length = version == FileVersion.Psd ? reader.ReadInt32() : reader.ReadInt64(); section.Offset = stream.Position; // Layers information. section.LayersInformation = new LayersInformation(); section.LayersInformation.Length = version == FileVersion.Psd ? reader.ReadInt32() : reader.ReadInt64(); section.LayersInformation.LayerCount = reader.ReadInt16(); if (section.LayersInformation.LayerCount < 0) { section.LayersInformation.LayerCount *= -1; section.LayersInformation.IsFirstAlphaChannel = true; } section.LayersInformation.Offset = stream.Position; section.LayersInformation.Length -= 2; // subtract layer count bytes. // GlobalLayerMaskInformation stream.Position = section.LayersInformation.Offset + section.LayersInformation.Length; var globalLayerMaskInformationLength = reader.ReadInt32(); if (globalLayerMaskInformationLength > 0) { section.GlobalLayerMaskInformation = new GlobalLayerMaskInformation(); section.GlobalLayerMaskInformation.Length = globalLayerMaskInformationLength; section.GlobalLayerMaskInformation.OverlayColorSpace = reader.ReadInt16(); section.GlobalLayerMaskInformation.ColorComponents = new short[4]; for (int i = 0; i < section.GlobalLayerMaskInformation.ColorComponents.Length; i++) { section.GlobalLayerMaskInformation.ColorComponents[i] = reader.ReadInt16(); } section.GlobalLayerMaskInformation.Opacity = reader.ReadInt16(); section.GlobalLayerMaskInformation.Kind = (GlobalLayerMaskInfoKind)reader.ReadByte(); section.GlobalLayerMaskInformation.Offset = stream.Position; } // AdditionalLayerInformation stream.Position += globalLayerMaskInformationLength; if (stream.Position != section.Offset + section.Length) // if we did not reach the end of section, than there is additional data. { section.AdditionalLayerInformation = new AdditionalLayerInformation(); section.AdditionalLayerInformation.Offset = stream.Position; section.AdditionalLayerInformation.Length = (section.Offset + section.Length) - section.AdditionalLayerInformation.Offset; } stream.Position = section.Offset + section.Length; return(section); }
public List <ImageResourceBlock> Read(Stream stream, ImageResourcesSection imageResourcesSection) { var list = new List <ImageResourceBlock>(); var reader = new BigEndianBinaryReader(stream); stream.Position = imageResourcesSection.Offset; while (reader.BaseStream.Position < imageResourcesSection.Offset + imageResourcesSection.Length) { var imageResource = new ImageResourceBlock(); imageResource.Signature = new string(reader.ReadChars(4)); imageResource.Id = (ImageResourceId)reader.ReadInt16(); imageResource.Name = reader.ReadPascalString(); imageResource.Length = reader.ReadInt32(); if ((imageResource.Length % 2) != 0) { imageResource.Length++; } imageResource.Offset = stream.Position; stream.Position += imageResource.Length; list.Add(imageResource); } return(list); }
public FileHeader Read(Stream stream) { var header = new FileHeader(); var reader = new BigEndianBinaryReader(stream); header.Signature = new string(reader.ReadChars(4)); header.Version = (FileVersion)reader.ReadInt16(); header.Reserved = reader.ReadBytes(6); header.ChannelCount = reader.ReadInt16(); header.PixelHeight = reader.ReadInt32(); header.PixelWidth = reader.ReadInt32(); header.BitsPerChannel = reader.ReadInt16(); header.ColorMode = (ColorMode)reader.ReadInt16(); return(header); }
public ImageDataSection Read(Stream stream) { var reader = new BigEndianBinaryReader(stream); var section = new ImageDataSection(); section.CompressionMethod = (CompressionMethod)reader.ReadInt16(); section.Offset = stream.Position; section.Length = stream.Length - section.Offset; return(section); }
public List <LayerRecord> Read(Stream stream, LayersInformation layersInformation, FileVersion fileVersion) { var list = new List <LayerRecord>(); var reader = new BigEndianBinaryReader(stream); stream.Position = layersInformation.Offset; for (int i = 0; i < layersInformation.LayerCount; i++) { var layerRecord = new LayerRecord(); layerRecord.RectangleTop = reader.ReadInt32(); layerRecord.RectangleLeft = reader.ReadInt32(); layerRecord.RectangleBottom = reader.ReadInt32(); layerRecord.RectangleRight = reader.ReadInt32(); layerRecord.ChannelCount = reader.ReadInt16(); layerRecord.Channels = new ChannelInformation[layerRecord.ChannelCount]; for (int j = 0; j < layerRecord.ChannelCount; j++) { layerRecord.Channels[j] = new ChannelInformation(); layerRecord.Channels[j].Id = reader.ReadInt16(); layerRecord.Channels[j].Length = fileVersion == FileVersion.Psd ? reader.ReadInt32() : reader.ReadInt64(); } layerRecord.BlendModeSignature = new string(reader.ReadChars(4)); layerRecord.BlendModeKey = new string(reader.ReadChars(4)); layerRecord.Opacity = reader.ReadByte(); layerRecord.Clipping = (Clipping)reader.ReadByte(); var flags = reader.ReadByte(); layerRecord.IsTransparencyProtected = flags.GetBit(0); layerRecord.IsVisible = flags.GetBit(1); layerRecord.IsObsolete = flags.GetBit(2); if (flags.GetBit(3)) { layerRecord.IsPixelIrrelevantToAppearance = flags.GetBit(4); } reader.ReadByte(); // filler layerRecord.Length = reader.ReadInt32(); layerRecord.Offset = stream.Position; // TODO: Layer mask / adjustment layer data var maskLength = reader.ReadInt32(); stream.Position += maskLength; // TODO: Layer blending ranges data var blendingLength = reader.ReadInt32(); stream.Position += blendingLength; layerRecord.Name = reader.ReadPascalString(); reader.ReadByte(); //??? layerRecord.AdditionalLayerInformation = new AdditionalLayerInformation(); layerRecord.AdditionalLayerInformation.Offset = stream.Position; layerRecord.AdditionalLayerInformation.Length = (layerRecord.Offset + layerRecord.Length) - layerRecord.AdditionalLayerInformation.Offset; stream.Position = layerRecord.Offset + layerRecord.Length; list.Add(layerRecord); } return(list); }