private void NextSize(Loon.Core.Geom.Point.Point2i size) { if (size.x > size.y) { size.y <<= 1; } else { size.x <<= 1; } }
public static int GetBoundingShape(int[] xPoints, int[] yPoints, int startAngle, int arcAngle, int centerX, int centerY, int boundingX, int boundingY, int boundingWidth, int boundingHeight) { xPoints[0] = centerX; yPoints[0] = centerY; Loon.Core.Geom.Point.Point2i startPoint = GetBoundingPointAtAngle(boundingX, boundingY, boundingWidth, boundingHeight, startAngle); xPoints[1] = startPoint.x; yPoints[1] = startPoint.y; int i = 2; for (int angle = 0; angle < arcAngle; i++, angle += 90) { if (angle + 90 > arcAngle && ((startAngle + angle - 45) % 360) / 90 == ((startAngle + arcAngle + 45) % 360) / 90) { break; } int modAngle = (startAngle + angle) % 360; if (modAngle > 315 || modAngle <= 45) { xPoints[i] = boundingX + boundingWidth; yPoints[i] = boundingY; } else if (modAngle > 135 && modAngle <= 225) { xPoints[i] = boundingX; yPoints[i] = boundingY + boundingHeight; } else if (modAngle > 45 && modAngle <= 135) { xPoints[i] = boundingX; yPoints[i] = boundingY; } else { xPoints[i] = boundingX + boundingWidth; yPoints[i] = boundingY + boundingHeight; } } Loon.Core.Geom.Point.Point2i endPoint = GetBoundingPointAtAngle(boundingX, boundingY, boundingWidth, boundingHeight, (startAngle + arcAngle) % 360); if (xPoints[i - 1] != endPoint.x || yPoints[i - 1] != endPoint.y) { xPoints[i] = endPoint.x; yPoints[i++] = endPoint.y; } return(i); }
private LImage PackImage() { CheckPacked(); if (packing) { if (temps.IsEmpty()) { throw new InvalidOperationException("Nothing to Pack !"); } int maxWidth = 0; int maxHeight = 0; int totalArea = 0; for (int i = 0; i < temps.Size(); i++) { PackEntry entry = (PackEntry) temps.Get(i); int width = entry.image.GetWidth(); int height = entry.image.GetHeight(); if (width > maxWidth) { maxWidth = width; } if (height > maxHeight) { maxHeight = height; } totalArea += width * height; } Loon.Core.Geom.Point.Point2i size = new Loon.Core.Geom.Point.Point2i(CloseTwoPower(maxWidth), CloseTwoPower(maxHeight)); bool fitAll = false; loop: { while (!fitAll) { int area = size.x * size.y; if (area < totalArea) { NextSize(size); continue; } Node root = new Node(size.x, size.y); for (int i = 0; i < temps.Size(); i++) { PackEntry entry = (PackEntry) temps.Get(i); Node inserted = root.Insert(entry); if (inserted == null) { NextSize(size); goto loop; } } fitAll = true; } } int srcWidth = size.x; int srcHeight = size.y; //由于LGraphics操作较耗时,此处直接处理像素(其实也快不了几毫秒,未来将改写为C#混合C/C++) LImage image = LImage.CreateImage(srcWidth, srcHeight, useAlpha); int[] dstPixels = image.GetIntPixels(); int[] srcPixels = null; for (int i = 0; i < temps.Size(); i++) { PackEntry entry = (PackEntry)temps.Get(i); LImage img = entry.image; srcPixels = img.GetIntPixels(); int w = img.Width; int h = img.Height; int x = entry.bounds.left; int y = entry.bounds.top; if (x < 0) { w += x; x = 0; } if (y < 0) { h += y; y = 0; } if (x + w > srcWidth) { w = srcWidth - x; } if (y + h > srcHeight) { h = srcHeight - y; } if (img.hasAlpha) { int findIndex = y * srcWidth + x; int drawIndex = 0; int moveFind = srcWidth - w; for (int col = 0; col < h; col++) { for (int row = 0; row < w; ) { if (srcPixels[drawIndex] != 0) { dstPixels[findIndex] = srcPixels[drawIndex]; } row++; findIndex++; drawIndex++; } findIndex += moveFind; } } else { for (int count = 0; count < h; count++) { System.Array.Copy(srcPixels, count * w, dstPixels, (y + count) * srcWidth + x, w); } } } image.SetIntPixels(dstPixels); srcPixels = null; dstPixels = null; packing = false; return image; } return null; }
private LImage PackImage() { CheckPacked(); if (packing) { if (temps.IsEmpty()) { throw new InvalidOperationException("Nothing to Pack !"); } int maxWidth = 0; int maxHeight = 0; int totalArea = 0; for (int i = 0; i < temps.Size(); i++) { PackEntry entry = (PackEntry)temps.Get(i); int width = entry.image.GetWidth(); int height = entry.image.GetHeight(); if (width > maxWidth) { maxWidth = width; } if (height > maxHeight) { maxHeight = height; } totalArea += width * height; } Loon.Core.Geom.Point.Point2i size = new Loon.Core.Geom.Point.Point2i(CloseTwoPower(maxWidth), CloseTwoPower(maxHeight)); bool fitAll = false; loop : { while (!fitAll) { int area = size.x * size.y; if (area < totalArea) { NextSize(size); continue; } Node root = new Node(size.x, size.y); for (int i = 0; i < temps.Size(); i++) { PackEntry entry = (PackEntry)temps.Get(i); Node inserted = root.Insert(entry); if (inserted == null) { NextSize(size); goto loop; } } fitAll = true; } } int srcWidth = size.x; int srcHeight = size.y; //由于LGraphics操作较耗时,此处直接处理像素(其实也快不了几毫秒,未来将改写为C#混合C/C++) LImage image = LImage.CreateImage(srcWidth, srcHeight, useAlpha); int[] dstPixels = image.GetIntPixels(); int[] srcPixels = null; for (int i = 0; i < temps.Size(); i++) { PackEntry entry = (PackEntry)temps.Get(i); LImage img = entry.image; srcPixels = img.GetIntPixels(); int w = img.Width; int h = img.Height; int x = entry.bounds.left; int y = entry.bounds.top; if (x < 0) { w += x; x = 0; } if (y < 0) { h += y; y = 0; } if (x + w > srcWidth) { w = srcWidth - x; } if (y + h > srcHeight) { h = srcHeight - y; } if (img.hasAlpha) { int findIndex = y * srcWidth + x; int drawIndex = 0; int moveFind = srcWidth - w; for (int col = 0; col < h; col++) { for (int row = 0; row < w;) { if (srcPixels[drawIndex] != 0) { dstPixels[findIndex] = srcPixels[drawIndex]; } row++; findIndex++; drawIndex++; } findIndex += moveFind; } } else { for (int count = 0; count < h; count++) { System.Array.Copy(srcPixels, count * w, dstPixels, (y + count) * srcWidth + x, w); } } } image.SetIntPixels(dstPixels); srcPixels = null; dstPixels = null; packing = false; return(image); } return(null); }
private LPixmap PackImage() { CheckPacked(); if (packing) { if (temps.IsEmpty()) { throw new Exception("Nothing to Pack !"); } int maxWidth = 0; int maxHeight = 0; int totalArea = 0; for (int i = 0; i < temps.Size(); i++) { PackEntry entry = (PackEntry)temps.Get(i); int width = entry.image.GetWidth(); int height = entry.image.GetHeight(); if (width > maxWidth) { maxWidth = width; } if (height > maxHeight) { maxHeight = height; } totalArea += width * height; } Loon.Core.Geom.Point.Point2i size = new Loon.Core.Geom.Point.Point2i(CloseTwoPower(maxWidth), CloseTwoPower(maxHeight)); bool fitAll = false; while (!fitAll) { int area = size.x * size.y; if (area < totalArea) { NextSize(size); continue; } Node root = new Node(size.x, size.y); for (int i = 0; i < temps.Size(); i++) { PackEntry entry = (PackEntry)temps.Get(i); Node inserted = root.Insert(entry); if (inserted == null) { NextSize(size); continue; } } fitAll = true; } LPixmap image = new LPixmap(size.x, size.y, useAlpha); for (int i = 0; i < temps.Size(); i++) { PackEntry entry = (PackEntry)temps.Get(i); image.DrawPixmap(entry.image, entry.bounds.left, entry.bounds.top); } packing = false; return(image); } return(null); }
private LPixmap PackImage() { CheckPacked(); if (packing) { if (temps.IsEmpty()) { throw new Exception("Nothing to Pack !"); } int maxWidth = 0; int maxHeight = 0; int totalArea = 0; for (int i = 0; i < temps.Size(); i++) { PackEntry entry = (PackEntry)temps.Get(i); int width = entry.image.GetWidth(); int height = entry.image.GetHeight(); if (width > maxWidth) { maxWidth = width; } if (height > maxHeight) { maxHeight = height; } totalArea += width * height; } Loon.Core.Geom.Point.Point2i size = new Loon.Core.Geom.Point.Point2i(CloseTwoPower(maxWidth), CloseTwoPower(maxHeight)); bool fitAll = false; while (!fitAll) { int area = size.x * size.y; if (area < totalArea) { NextSize(size); continue; } Node root = new Node(size.x, size.y); for (int i = 0; i < temps.Size(); i++) { PackEntry entry = (PackEntry)temps.Get(i); Node inserted = root.Insert(entry); if (inserted == null) { NextSize(size); continue; } } fitAll = true; } LPixmap image = new LPixmap(size.x, size.y, useAlpha); for (int i = 0; i < temps.Size(); i++) { PackEntry entry = (PackEntry)temps.Get(i); image.DrawPixmap(entry.image, entry.bounds.left, entry.bounds.top); } packing = false; return image; } return null; }