Exemplo n.º 1
0
        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;

            case "lrFX":
                result = new EffectsLayer(reader, key);
                break;

            case "shmd":
                result = new MetadataSetting(reader);
                break;

            case "lfx2":
                result = new ObjectBasedEffect(reader, length);
                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);
        }
Exemplo n.º 2
0
        public Layer(PsdBinaryReader reader, PsdFile psdFile)
            : this(psdFile)
        {
            IsText = false;
            Rect   = reader.ReadRectangle();

            //-----------------------------------------------------------------------
            // Read channel headers.  Image data comes later, after the layer header.

            int numberOfChannels = reader.ReadUInt16();
            for (int channel = 0; channel < numberOfChannels; channel++)
            {
                var ch = new Channel(reader, this);
                Channels.Add(ch);
            }

            //-----------------------------------------------------------------------
            //

            var signature = reader.ReadAsciiChars(4);
            if (signature != "8BIM")
            {
                throw (new PsdInvalidException("Invalid signature in layer header."));
            }

            BlendModeKey = reader.ReadAsciiChars(4);
            Opacity      = reader.ReadByte();
            Clipping     = reader.ReadBoolean();

            var flagsByte = reader.ReadByte();
            flags = new BitVector32(flagsByte);
            reader.ReadByte();             //padding

            //-----------------------------------------------------------------------

            // This is the total size of the MaskData, the BlendingRangesData, the
            // Name and the AdjustmentLayerInfo.
            var extraDataSize          = reader.ReadUInt32();
            var extraDataStartPosition = reader.BaseStream.Position;

            Masks = new MaskInfo(reader, this);
            BlendingRangesData = new BlendingRanges(reader, this);
            Name = reader.ReadPascalString(4);

            //-----------------------------------------------------------------------
            // Process Additional Layer Information

            long adjustmentLayerEndPos = extraDataStartPosition + extraDataSize;
            while (reader.BaseStream.Position < adjustmentLayerEndPos)
            {
                var layerInfo = LayerInfoFactory.Load(reader);
                AdditionalInfo.Add(layerInfo);
            }

            foreach (var adjustmentInfo in AdditionalInfo)
            {
                switch (adjustmentInfo.Key)
                {
                case "luni":
                    Name = ((LayerUnicodeName)adjustmentInfo).Name;
                    break;

                case "TySh":
                    IsText    = true;
                    LayerText = (LayerText)adjustmentInfo;
                    break;

                case "lrFX":
                    Effects = (EffectsLayer)adjustmentInfo;
                    break;

                case "lfx2":
                    BaseEffect = (ObjectBasedEffect)adjustmentInfo;
                    break;
                }
            }
        }