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); }