示例#1
0
        public Mask(BinaryPSDReader reader, Layer layer)
            : base(layer)
        {
            this._layer = layer;

            int nLength = (int)reader.ReadUInt32();
            if (nLength == 0)
                return;

            long nStart = reader.BaseStream.Position;

            this.Rectangle = reader.ReadPSDRectangle(); //new Rectangle(reader).ToERectangle();

            this._color = reader.ReadByte();

            this._flags = reader.ReadByte();

            if (nLength == 20)
                reader.ReadUInt16(); //padding
            else if (nLength == 36)
            {
                this._flags2 = reader.ReadByte();//same flags as above according to docs!?!?
                this._maskBg = reader.ReadByte(); //Real user mask background. Only 0 or 255 - ie bool?!?
                this._otherRectangle = reader.ReadPSDRectangle(); //new Rectangle(reader).ToERectangle(); //same as above rectangle according to docs?!?!
            }

            reader.BaseStream.Position = nStart + nLength;
        }
示例#2
0
 public Layer CreateLayer(string name)
 {
     Layer layer = new Layer(this);
     layer.Name = name;
     //layer.LayerID = this.Layers.Count;
     this._layers.Add(layer);
     return layer;
 }
示例#3
0
 private void CreateLayer()
 {
     this._fakeLayer = new Layer(this._document);
     this._fakeLayer.IsMerged = true;
     this._fakeLayer.Width = (int)this._document.Size.X;
     this._fakeLayer.Height = (int)this._document.Size.Y;
     this._fakeLayer.Channels = new Dictionary<int, Channel>();
     for (int i = 0; i < this._document.Channels; i++)
     {
         this._fakeLayer.Channels.Add(i, new Channel(this._fakeLayer));
     }
 }
示例#4
0
        public Document(string a_sFilename)
        {
            this.Init();

            FileStream stream = new FileStream(a_sFilename,
                FileMode.Open, FileAccess.Read);
            //stream.
            BinaryPSDReader reader = new BinaryPSDReader(stream);

            string signature = new string(reader.ReadPSDChars(4));
            if (signature != "8BPS")
                return;

            this._header = new Header(reader);
            //this.Version = reader.ReadUInt16();
            //if (Version != 1)
            //    throw new Exception("Can not read .psd version " + Version);
            //byte[] buf = new byte[256];
            //reader.Read(buf, (int)reader.BaseStream.Position, 6); //6 bytes reserved
            //this.Channels = reader.ReadInt16();
            //this.Rows = reader.ReadUInt32();
            //this.Columns = reader.ReadUInt32();
            //this.BitsPerPixel = (int)reader.ReadUInt16();
            //this.ColorMode = (ColorModes)reader.ReadInt16();

            #region Palette
            uint nPaletteLength = reader.ReadUInt32();
            if (nPaletteLength > 0)
            {
                this.ColorTable = new List<Color>();
                for (int i = 0; i < nPaletteLength; i+=3)
                {
                    this.ColorTable.Add(Color.FromArgb((int)reader.ReadByte(), (int)reader.ReadByte(), (int)reader.ReadByte()));
                }
                //this.ColorTable.Add(Color.FromArgb(255, 10, 20));

                if (this.ColorMode == ColorModes.Duotone)
                {
                }
                else
                {
                }
            }
            #endregion

            uint nResLength = reader.ReadUInt32(); //? Number of bytes, or number of entries??
            if (nResLength > 0)
            {
                //read settings
                this._imageResources = ImageResource.ReadImageResources(reader);
            }

            //reader.JumpToEvenNthByte(4);
            uint nTotalLayersBytes = reader.ReadUInt32();
            long nAfterLayersDefinitions = reader.BaseStream.Position + nTotalLayersBytes;

            if (nTotalLayersBytes == 8)
            {
                stream.Position += nTotalLayersBytes;
                //this.Unknown = Endogine.Serialization.ReadableBinary.CreateHexEditorString(reader.ReadBytes((int)reader.BytesToEnd));
            }
            else
            {
                uint nSize = reader.ReadUInt32(); //What's the difference between nTotalLayersBytes and nSize really?
                long nLayersEndPos = reader.BaseStream.Position + nSize;

                short nNumLayers = reader.ReadInt16();
                bool bSkipFirstAlpha = false;

                if (nNumLayers < 0)
                {
                    bSkipFirstAlpha = true;
                    nNumLayers = (short)-nNumLayers;
                }

                List<Layer> loadOrderLayers = new List<Layer>();
                this._layers = new List<Layer>(); // new Dictionary<int, Layer>();
                for (int nLayerNum = 0; nLayerNum < nNumLayers; nLayerNum++)
                {
                    Layer layerInfo = new Layer(reader, this);
                    layerInfo.DebugLayerLoadOrdinal = nLayerNum;
                    //if (layerInfo.LayerID < 0)
                    //    layerInfo.LayerID = nLayerNum;
                    //if (this._layers.ContainsKey(layerInfo.LayerID))
                    //    throw(new Exception("Duplicate layer IDs! " + layerInfo.LayerID.ToString()));
                    //else
                    //    this._layers.Add(layerInfo.LayerID, layerInfo);
                    this._layers.Add(layerInfo);
                    loadOrderLayers.Add(layerInfo);
                }

                for (int layerNum = 0; layerNum < nNumLayers; layerNum++)
                {
                    Layer layer = (Layer)loadOrderLayers[layerNum];
                    layer.ReadPixels(reader);
                }

                reader.JumpToEvenNthByte(4);
                if (reader.BaseStream.Position != nLayersEndPos)
                    reader.BaseStream.Position = nLayersEndPos; // nAfterLayersDefinitions;

                //Global layer mask
                uint maskLength = reader.ReadUInt32();
                this._tempGlobalLayerMask = null;
                if (maskLength > 0)
                {
                    this._tempGlobalLayerMask = reader.ReadBytes((int)maskLength);

                    //TODO: the docs are obviously wrong here...
                    //ushort overlayColorSpace = reader.ReadUInt16(); //undefined in docs
                    //for (int i = 0; i < 4; i++)
                    //    reader.ReadUInt16(); //TODO: UInt16 if 16-bit color? Color components - says *both* 4*2 bytes, and 8 bytes in the docs?
                    //reader.ReadUInt16(); //opacity (0-100)
                    //reader.ReadByte(); //Kind: 0=Color selectedi.e. inverted; 1=Color protected;128=use value stored per layer.
                    //reader.ReadByte(); //padding
                }

                //hmm... another section of "global" layer resources..?
                while (true)
                {
                    long cpPos = reader.BaseStream.Position;
                    string sHeader = new string(reader.ReadPSDChars(4));
                    reader.BaseStream.Position = cpPos; //TODO: -= 4 should work, but sometimes ReadPSDChars advances 5?!?!
                    if (sHeader != "8BIM")
                    {
                        break;
                    }
                    LayerResource res = LayerResource.ReadLayerResource(reader, null);
                    this._globalLayerResources.Add(res);
                }
            }

            bool readGlobalImage = true;
            if (readGlobalImage)
            {
                this._globalImage = new GlobalImage(this);
                this._globalImage.Load(reader);
                //the big merged bitmap (which is how the photoshop document looked when it was saved)
                //Bitmap bmp = this._globalImage.Bitmap;
            }

            reader.Close();
            stream.Close();
        }