Beispiel #1
0
        public static Layer MakeImageLayer(this PsdFile psd, Bitmap bmp, string name, int x = 0, int y = 0)
        {
            Layer psdLayer = new Layer(psd);

            // Set layer metadata
            psdLayer.Name               = name;
            psdLayer.Rect               = new Rectangle(new Point(x, y), bmp.Size);
            psdLayer.BlendModeKey       = PsdBlendMode.Normal;
            psdLayer.Opacity            = 255;
            psdLayer.Visible            = true;
            psdLayer.Masks              = new MaskInfo();
            psdLayer.BlendingRangesData = new BlendingRanges(psdLayer);
            psdLayer.SetBitmap(bmp, ImageReplaceOption.KeepCenter, psd.ImageCompression);
            return(psdLayer);
        }
Beispiel #2
0
        public InfoLayers(PsdFile psdFile, string key)
        {
            PsdFile = psdFile;

            switch (key)
            {
            // The key does not have to match the bit depth, but it does have to
            // be one of the known values.
            case "Layr":
            case "Lr16":
            case "Lr32":
                this.key = key;
                break;

            default:
                throw new PsdInvalidException(
                          $"{nameof(InfoLayers)} key must be Layr, Lr16, or Lr32.");
            }
        }
Beispiel #3
0
        public InfoLayers(PsdBinaryReader reader, PsdFile psdFile,
                          string key, long dataLength)
            : this(psdFile, key)
        {
            if (psdFile.Layers.Count > 0)
            {
                throw new PsdInvalidException(
                          "Cannot have both regular layers and Additional Info layers");
            }

            var endPosition = reader.BaseStream.Position + dataLength;

            psdFile.LoadLayers(reader, false);

            if (reader.BaseStream.Position != endPosition)
            {
                throw new PsdInvalidException(
                          $"Incorrect length for {nameof(InfoLayers)}.");
            }
        }
Beispiel #4
0
        public static Layer MakeSectionLayers(this PsdFile psd, string name, out Layer dividerLayer, bool isOpen = false)
        {
            Layer headLayer = new Layer(psd)
            {
                Name         = name,
                Rect         = new Rectangle(),
                BlendModeKey = PsdBlendMode.Normal,
                Opacity      = 255,
                Visible      = true,
                Masks        = new MaskInfo()
            };

            // Set layer metadata
            headLayer.BlendingRangesData = new BlendingRanges(headLayer);
            headLayer.AdditionalInfo.Add(new LayerSectionInfo()
            {
                SectionType = isOpen ? LayerSectionType.OpenFolder : LayerSectionType.ClosedFolder
            });

            dividerLayer = new Layer(psd)
            {
                Name               = LayerGroupDivider,
                Rect               = new Rectangle(),
                BlendModeKey       = PsdBlendMode.Normal,
                Opacity            = 255,
                Visible            = true,
                Masks              = new MaskInfo(),
                BlendingRangesData = new BlendingRanges(headLayer)
            };
            // Set layer metadata
            dividerLayer.AdditionalInfo.Add(new LayerSectionInfo()
            {
                SectionType = LayerSectionType.SectionDivider
            });

            return(headLayer);
        }
Beispiel #5
0
 public virtual void OnLoadLayersHeader(PsdFile psdFile)
 {
 }
Beispiel #6
0
        /// <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)
        {
            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(
                          $"{nameof(LayerInfo)} signature invalid, must be 8BIM or 8B64.");
            }

            var key           = reader.ReadAsciiChars(4);
            var hasLongLength = LayerInfoUtil.HasLongLength(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);
        }