public PackNode Insert(Image img) { if (!this.IsLeaf) //we're not a leaf so try inserting into a child { PackNode newNode = _children[0].Insert(img); if (newNode != null) { return(newNode); } // no room in first, insert into second return(_children[1].Insert(img)); } //if there's already a lightmap here, return if (_imgId >= 0) { return(null); } //if we're too small, return if (_rct.Width + 1 < img.Size.Width || _rct.Height + 1 < img.Size.Height) { return(null); } //if we're just right, accept if (_rct.Width + 1 == img.Size.Width && _rct.Height + 1 == img.Size.Height) { return(this); } //otherwise, gotta split this node and create some kids _children = new PackNode[2]; _children[0] = new PackNode(); _children[1] = new PackNode(); //decide which way to split int dw = this._rct.Width - img.Width; int dh = this._rct.Height - img.Height; if (dw > dh) { _children[0]._rct = ERectangle.FromLTRB(_rct.Left, _rct.Top, _rct.Left + img.Width - 1, _rct.Bottom); _children[1]._rct = ERectangle.FromLTRB(_rct.Left + img.Width, _rct.Top, _rct.Right, _rct.Bottom); } else { _children[0]._rct = ERectangle.FromLTRB(_rct.Left, _rct.Top, _rct.Right, _rct.Top + img.Height - 1); _children[1]._rct = ERectangle.FromLTRB(_rct.Left, _rct.Top + img.Height, _rct.Right, _rct.Bottom); } //insert into first child we created return(this._children[0].Insert(img)); }
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 Smoke() { this.MouseActive = true; this.TextureFilter = Sprite.TextureFilters.High; this.Scaling = new EPointF(5, 5); this.Ink = RasterOps.ROPs.AddPin; this.Create(100, 100); lwidth = this.Width / res; lheight = this.Height / res; v = new vsquare[lwidth + 1][]; for (int i = 0; i < v.Length; i++) { v[i] = new vsquare[lheight + 1]; } vbuf = new vbuffer[lwidth + 1][]; for (int i = 0; i < vbuf.Length; i++) { vbuf[i] = new vbuffer[lheight + 1]; } p = new particle[pnum]; CurrentColor = Color.FromArgb(2, 255, 0, 0); ERectangle emitter = ERectangle.FromLTRB(this.Width / 2 - 20, this.Height - 20, this.Width / 2 + 20, this.Height); for (int i = 0; i < pnum; i++) { p[i] = new particle(emitter, this); p[i].Init(); } for (int i = 0; i <= lwidth; i++) { for (int u = 0; u <= lheight; u++) { v[i][u] = new vsquare(i * res, u * res, this); vbuf[i][u] = new vbuffer(i * res, u * res, this); } } RandomGust = new Gust(); }
public static unsafe Bitmap TrimWhitespace(Bitmap bmp, out EPoint topLeftCorner) { //TODO: name should be TrimAlpha int nThreshold = 0; Canvas canvas = Canvas.Create(bmp); canvas.Locked = true; ERectangle rctBounds = ERectangle.FromLTRB(0, 0, canvas.Width, canvas.Height); for (int side = 0; side < 2; side++) { int yDir = 1; int yUse = 0; if (side == 1) { yDir = -1; yUse = canvas.Height - 2; } bool bFound = false; for (int y = 0; y < canvas.Height; y++) { for (int x = 0; x < canvas.Width; x++) { if (canvas.GetPixel(x, yUse).A > nThreshold) { if (side == 0) { rctBounds.Top = yUse; } else { rctBounds.Bottom = yUse; } bFound = true; break; } } if (bFound) { break; } yUse += yDir; } } for (int side = 0; side < 2; side++) { int xDir = 1; int xUse = 0; if (side == 1) { xDir = -1; xUse = canvas.Width - 1; } bool bFound = false; for (int x = 0; x < canvas.Width; x++) { xUse += xDir; for (int y = 0; y < canvas.Height; y++) { if (canvas.GetPixel(xUse, y).A > nThreshold) { if (side == 0) { rctBounds.Left = xUse; } else { rctBounds.Right = xUse; } bFound = true; break; } } if (bFound) { break; } } } canvas.Locked = false; if (rctBounds.Width == 0 || rctBounds.Height == 0) { topLeftCorner = new EPoint(); return(null); } Bitmap trimmedBmp = new Bitmap(rctBounds.Width, rctBounds.Height); Graphics g = Graphics.FromImage(trimmedBmp); g.DrawImage(bmp, new Rectangle(0, 0, rctBounds.Width, rctBounds.Height), rctBounds.X, rctBounds.Y, rctBounds.Width, rctBounds.Height, GraphicsUnit.Pixel); topLeftCorner = new EPoint(rctBounds.X, rctBounds.Y); return(trimmedBmp); }
public static Bitmap RenderToBitmap(int nTwipSize, ArrayList commandList, ArrayList fillStyles, ArrayList lineStyles, out EPoint ptOffset) { EPoint pntCurrentLoc = new EPoint(); ArrayList pathInfos = new ArrayList(); PathInfo pathInfoCurrent = new PathInfo(); pathInfos.Add(pathInfoCurrent); bool hasDrawnSinceLastStyleChange = false; EPointF ptScale = new EPointF(1, 1) / nTwipSize; string sDebug = ""; foreach (ShapeCommand.Base cmd in commandList) { if (cmd.MovesTurtle) { if (cmd.Draws) { ((ShapeCommand.Draw)cmd).AddToPath(pathInfoCurrent.Path, pntCurrentLoc, ptScale.X); if (Shape.Debug) { ArrayList pts = ((ShapeCommand.Draw)cmd).GeneratePoints(pntCurrentLoc); if (cmd is ShapeCommand.Curve) { sDebug += "Curve"; } else { sDebug += "Line"; } sDebug += "\r\n"; foreach (EPointF pt in pts) { sDebug += pt.ToString() + "\r\n"; } } hasDrawnSinceLastStyleChange = true; } else { pathInfoCurrent.Path.CloseAllFigures(); pathInfoCurrent.Path.StartFigure(); } pntCurrentLoc = cmd.GetNewLoc(pntCurrentLoc); } else { if (hasDrawnSinceLastStyleChange) { pathInfoCurrent = new PathInfo(); pathInfos.Add(pathInfoCurrent); } if (cmd is ShapeCommand.FillStyle) { ShapeCommand.FillStyle fs = (ShapeCommand.FillStyle)cmd; Brush brush = null; if (fs.StyleId > 0) { brush = ((Style.FillStyle)fillStyles[fs.StyleId - 1]).GetBrush(); } if (fs.Side == 0) { pathInfoCurrent.Brush0 = brush; } else { pathInfoCurrent.Brush1 = brush; } } else if (cmd is ShapeCommand.LineStyle) { ShapeCommand.LineStyle ls = (ShapeCommand.LineStyle)cmd; Pen pen = null; if (ls.StyleId > 0) { pen = ((Style.LineStyle)lineStyles[ls.StyleId - 1]).GetPen(); } pathInfoCurrent.Pen = pen; if (pen != null) { pen.Width *= ptScale.X; } } hasDrawnSinceLastStyleChange = false; } } Matrix transform = new Matrix(); transform.Scale(ptScale.X, ptScale.Y); ERectangle bounds = ERectangle.FromLTRB(99999, 99999, -99999, -99999); foreach (PathInfo pathInfo in pathInfos) { //pathInfo.Path.Transform(transform); bounds.Expand(pathInfo.GetBounds().ToERectangle()); } ptOffset = bounds.TopLeft; if (bounds.Width == 0 || bounds.Height == 0) { return(null); } //this.Bounds = bounds; if (Shape.Debug) { Endogine.Files.FileReadWrite.Write("__s.txt", sDebug); } transform = new Matrix(); transform.Translate(-bounds.X, -bounds.Y); foreach (PathInfo pathInfo in pathInfos) { pathInfo.Path.Transform(transform); } Bitmap bmp = new Bitmap(bounds.Width, bounds.Height, PixelFormat.Format32bppArgb); Endogine.BitmapHelpers.Canvas canvas = Endogine.BitmapHelpers.Canvas.Create(bmp); canvas.Locked = true; canvas.Fill(Color.FromArgb(0, 255, 255, 255)); canvas.Dispose(); Graphics g = Graphics.FromImage(bmp); g.SmoothingMode = SmoothingMode.HighQuality; foreach (PathInfo pathInfo in pathInfos) { if (pathInfo.Pen != null) { g.DrawPath(pathInfo.Pen, pathInfo.Path); } if (pathInfo.Brush0 != null) { g.FillPath(pathInfo.Brush0, pathInfo.Path); } if (pathInfo.Brush1 != null) //TODO: can GDI+ handle Flash's two different fills (0 and 1)? { g.FillPath(pathInfo.Brush1, pathInfo.Path); } } return(bmp); }
public static Bitmap PackBitmapsIntoOneLarge(ArrayList bmps, EPoint pntPreferredLayout, out Node infoRoot) { //If no specification of number of tiles on X and Y, make a guess: if (pntPreferredLayout == null) { int nNumOnX = (int)Math.Sqrt(bmps.Count); if (nNumOnX * nNumOnX < bmps.Count) { nNumOnX++; } int nNumOnY = bmps.Count / nNumOnX; if (nNumOnX * nNumOnY < bmps.Count) { nNumOnY++; } pntPreferredLayout = new EPoint(nNumOnX, nNumOnY); } bool bTrimWhiteSpace = true; //when packing the bitmap tightly, must store the offsets of bitmap frames so they don't wiggle on playback: bool bUseIndividualOffsets = true; infoRoot = new Node(); Node node = infoRoot.AppendChild("root"); node.AppendChild("NumFramesTotal").Value = bmps.Count; node.AppendChild("NumFramesOnX").Value = pntPreferredLayout.X; Node subNode; subNode = node.AppendChild("Animations"); //subNode.AppendChild("Default").Value = "0 0-4"; EPoint[] offsets = new EPoint[bmps.Count]; //this is the smallest rectangle that can encompass all frames: ERectangle rctBounds = ERectangle.FromLTRB(9999, 9999, -9999, -9999); //Trim white space from all bitmaps subNode = node.AppendChild("Frames"); for (int i = 0; i < bmps.Count; i++) { Bitmap bmp = (Bitmap)bmps[i]; if (bTrimWhiteSpace) { EPoint pntMid = new EPoint(bmp.Size.Width, bmp.Size.Height) / 2; EPoint pntTopLeftCorner; bmp = BitmapHelper.TrimWhitespace(bmp, out pntTopLeftCorner); bmps[i] = bmp; offsets[i] = pntTopLeftCorner; if (bUseIndividualOffsets) //make more compact (but offset values are needed): { rctBounds.Expand(new ERectangle(0, 0, bmp.Width, bmp.Height)); //make offset to middle of input bitmap offsets[i] = pntMid - pntTopLeftCorner; } else //Expand bounds so no offset values are needed: { rctBounds.Expand(new ERectangle(pntTopLeftCorner.X, pntTopLeftCorner.Y, bmp.Width, bmp.Height)); } } else { rctBounds.Expand(new ERectangle(0, 0, bmp.Width, bmp.Height)); } } //Create the merged bitmap: EPoint totalSize = rctBounds.Size * pntPreferredLayout; Bitmap largeBmp = new Bitmap(totalSize.X, totalSize.Y); Graphics g = Graphics.FromImage(largeBmp); for (int i = 0; i < bmps.Count; i++) { Bitmap bmp = (Bitmap)bmps[i]; EPoint pntDst = new EPoint((i % pntPreferredLayout.X) * rctBounds.Size.X, (i / pntPreferredLayout.X) * rctBounds.Size.Y); Node subsub = subNode.AppendChild("Frame"); subsub.Value = i.ToString(); if (bUseIndividualOffsets) { //subsub.AppendChild("Offset").Value = (offsets[i]+rctBounds.TopLeft).ToString(); subsub.AppendChild("Offset").Value = offsets[i].ToString(); } else { //pntDst is upper left corner of destination rectangle. //Since we don't use individual offset, we want to move it according to offset: pntDst = pntDst - rctBounds.TopLeft + offsets[i]; } g.DrawImage(bmp, new RectangleF(pntDst.ToPoint(), new Size(bmp.Width, bmp.Height))); } if (bUseIndividualOffsets) { node.AppendChild("RegPoint").Value = new EPoint().ToString(); } else { node.AppendChild("RegPoint").Value = rctBounds.TopLeft.ToString(); } return(largeBmp); }
private void CalcSourceRects() { MemberSpriteBitmap mb = this.Member; this._cuttingRectPixels = ERectangle.FromLTRB( (int)(this._cuttingRectFract.Left * mb.Size.X), (int)(this._cuttingRectFract.Top * mb.Size.Y), (int)(this._cuttingRectFract.Right * mb.Size.X), (int)(this._cuttingRectFract.Bottom * mb.Size.Y)); if (this._cuttingRectFract.Width > 0.001f && this._cuttingRectPixels.Width == 0) { this._cuttingRectPixels.Width++; } if (this._cuttingRectFract.Height > 0.001f && this._cuttingRectPixels.Height == 0) { this._cuttingRectPixels.Height++; } ERectangleF[,] rects = this.CreateRectanglesFromCuttingRect(this._cuttingRectPixels.ToERectangleF(), this.Member.Size.ToEPointF()); this._midSection = rects[1, 1].ToERectangle(); for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { ERectangleF rct = rects[x, y]; if (rct.Width == 0 || rct.Height == 0) { if (this._spriteArray[x, y] != null) { this._spriteArray[x, y].Dispose(); this._spriteArray[x, y] = null; } } else { Sprite sp = this._spriteArray[x, y]; if (sp == null) { sp = new Sprite(); sp.Parent = this; sp.Name = x.ToString() + ";" + y.ToString(); this._spriteArray[x, y] = sp; } sp.Member = this.Member; sp.SourceRect = rct.ToERectangle(); sp.RegPoint = sp.SourceRect.Location; } } } this.m_aSprites.Clear(); for (int x = 0; x < 3; x++) { for (int y = 0; y < 3; y++) { if (this._spriteArray[x, y] != null) { this.m_aSprites.Add(this._spriteArray[x, y]); } } } }