Пример #1
0
        public List <AdditionalLayerInformationBlock> Read(Stream stream, AdditionalLayerInformation additionalLayerInformation, FileVersion fileVersion)
        {
            var reader = new BigEndianBinaryReader(stream);

            stream.Position = additionalLayerInformation.Offset;
            var list = new List <AdditionalLayerInformationBlock>();

            while (stream.Position < additionalLayerInformation.Offset + additionalLayerInformation.Length)
            {
                var block = new AdditionalLayerInformationBlock();
                block.Signature = new string(reader.ReadChars(4));
                block.Key       = new string(reader.ReadChars(4));

                if (fileVersion == FileVersion.Psb && LongDataKeys.Contains(block.Key))
                {
                    block.Length = reader.ReadInt64();
                }
                else
                {
                    block.Length = reader.ReadInt32();
                }

                if ((block.Length % 2) != 0)
                {
                    block.Length += 3; // should be 1, but 3 works there for unknown reason
                }

                block.Offset     = stream.Position;
                stream.Position += block.Length;
                list.Add(block);
            }

            return(list);
        }
Пример #2
0
        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);
        }
Пример #3
0
        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);
        }