public static void SplitImage(Document psd, int layerIndex, string outputFilePrefix) { ImageResources.GridGuidesInfo guidesInfo = (ImageResources.GridGuidesInfo)psd.GetResource(typeof(ImageResources.GridGuidesInfo)); List<int> vertical = new List<int>(); List<ImageResources.GridGuidesInfo.GridGuide> guides = guidesInfo.GetGuidesByAlignment(false); foreach (ImageResources.GridGuidesInfo.GridGuide gg in guides) vertical.Add((int)gg.LocationInPixels); vertical.Add((int)psd.Header.Rows); List<int> horizontal = new List<int>(); guides = guidesInfo.GetGuidesByAlignment(true); foreach (ImageResources.GridGuidesInfo.GridGuide gg in guides) horizontal.Add((int)gg.LocationInPixels); horizontal.Add((int)psd.Header.Columns); Bitmap bmp = psd.Layers[layerIndex].Bitmap; int lastX = 0; int cnt = 0; foreach (int x in horizontal) { int lastY = 0; foreach (int y in vertical) { ERectangle rct = ERectangle.FromLTRB(lastX, lastY, x, y); Bitmap bmpNew = new Bitmap(rct.Width, rct.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); Graphics g = Graphics.FromImage(bmpNew); g.DrawImage(bmp, new Rectangle(0, 0, rct.Width, rct.Height), rct.ToRectangle(), GraphicsUnit.Pixel); g.Dispose(); //TODO: examine bitmap and see if it can be reduced (e.g. middle parts are probably the same) Endogine.BitmapHelpers.BitmapHelper.Save(bmpNew, outputFilePrefix + "_slice" + cnt + ".png"); cnt++; lastY = y; } lastX = x; } }
public GlobalImage(Document document) { this._document = document; }
//public void AddChannel(Channel ch) //{ // this._channels.Add(ch.Usage, ch); //} public Layer(BinaryPSDReader reader, Document document) { this._document = document; this._rect = reader.ReadPSDRectangle(); ushort numChannels = reader.ReadUInt16(); this._channels = new Dictionary<int, Channel>(); for (int channelNum = 0; channelNum < numChannels; channelNum++) { Channel ch = new Channel(reader, this); if (this._channels.ContainsKey(ch.Usage)) continue; //TODO: !! this._channels.Add(ch.Usage, ch); } string sHeader = new string(reader.ReadPSDChars(4)); if (sHeader != "8BIM") throw(new Exception("Layer Channelheader error!")); //'levl'=Levels 'curv'=Curves 'brit'=Brightness/contrast 'blnc'=Color balance 'hue '=Old Hue/saturation, Photoshop 4.0 'hue2'=New Hue/saturation, Photoshop 5.0 'selc'=Selective color 'thrs'=Threshold 'nvrt'=Invert 'post'=Posterize this.BlendKey = new string(reader.ReadPSDChars(4)); int nBlend = -1; try { nBlend = (int)Enum.Parse(typeof(_blendKeysPsd), this.BlendKey); } catch { throw new Exception("Unknown blend key: " + this.BlendKey); } if (nBlend >= 0) { BlendKeys key = (BlendKeys)nBlend; this.BlendKey = Enum.GetName(typeof(BlendKeys), key); } this.Opacity = reader.ReadByte(); this.Clipping = reader.ReadByte(); this.Flags = reader.ReadByte(); reader.ReadByte(); //padding uint extraDataSize = reader.ReadUInt32(); long nChannelEndPos = reader.BaseStream.Position + (long)extraDataSize; if (extraDataSize > 0) { uint nLength; this._mask = new Mask(reader, this); if (this._mask.Rectangle == null) this._mask = null; //blending ranges this._blendRanges = new List<System.Drawing.Color>(); nLength = reader.ReadUInt32(); //First come Composite gray blend source / destination; Contains 2 black values followed by 2 white values. Present but irrelevant for Lab & Grayscale. //Then 4+4 for each channel (source + destination colors) for (uint i = 0; i < nLength/8; i++) { this._blendRanges.Add(System.Drawing.Color.FromArgb((int)reader.ReadUInt32())); this._blendRanges.Add(System.Drawing.Color.FromArgb((int)reader.ReadUInt32())); } //Name //nLength = (uint)reader.ReadByte(); //reader.BaseStream.Position -= 1; //TODO: wtf did I do here? this.Name = reader.ReadPascalString(); //TODO: sometimes there's a 2-byte padding here, but it's not 4-aligned... What is it? long posBefore = reader.BaseStream.Position; sHeader = new string(reader.ReadPSDChars(4)); if (sHeader != "8BIM") { reader.BaseStream.Position-=2; sHeader = new string(reader.ReadPSDChars(4)); } if (sHeader != "8BIM") reader.BaseStream.Position = posBefore; else { reader.BaseStream.Position -= 4; this._resources = LayerResource.ReadLayerResources(reader, null); } if (reader.BaseStream.Position != nChannelEndPos) reader.BaseStream.Position = nChannelEndPos; } }
public Layer(Document document) { this._document = document; }