Search in sources :

Example 1 with Rect

use of com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect in project libgdx by libgdx.

the class GridPacker method packPage.

private Page packPage(Array<Rect> inputRects, int cellWidth, int cellHeight) {
    Page page = new Page();
    page.outputRects = new Array();
    int maxWidth = settings.maxWidth, maxHeight = settings.maxHeight;
    if (settings.edgePadding) {
        maxWidth -= settings.paddingX;
        maxHeight -= settings.paddingY;
    }
    int x = 0, y = 0;
    for (int i = inputRects.size - 1; i >= 0; i--) {
        if (x + cellWidth > maxWidth) {
            y += cellHeight;
            if (y > maxHeight - cellHeight)
                break;
            x = 0;
        }
        Rect rect = inputRects.removeIndex(i);
        rect.x = x;
        rect.y = y;
        rect.width += settings.paddingX;
        rect.height += settings.paddingY;
        page.outputRects.add(rect);
        x += cellWidth;
        page.width = Math.max(page.width, x);
        page.height = Math.max(page.height, y + cellHeight);
    }
    // Flip so rows start at top.
    for (int i = page.outputRects.size - 1; i >= 0; i--) {
        Rect rect = page.outputRects.get(i);
        rect.y = page.height - rect.y - rect.height;
    }
    return page;
}
Also used : Array(com.badlogic.gdx.utils.Array) Rect(com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect) Page(com.badlogic.gdx.tools.texturepacker.TexturePacker.Page)

Example 2 with Rect

use of com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect in project libgdx by libgdx.

the class ImageProcessor method addImage.

/** The image won't be kept in-memory during packing if {@link Settings#limitMemory} is true. */
public void addImage(File file) {
    BufferedImage image;
    try {
        image = ImageIO.read(file);
    } catch (IOException ex) {
        throw new RuntimeException("Error reading image: " + file, ex);
    }
    if (image == null)
        throw new RuntimeException("Unable to read image: " + file);
    String name = file.getAbsolutePath().replace('\\', '/');
    // Strip root dir off front of image path.
    if (rootPath != null) {
        if (!name.startsWith(rootPath))
            throw new RuntimeException("Path '" + name + "' does not start with root: " + rootPath);
        name = name.substring(rootPath.length());
    }
    // Strip extension.
    int dotIndex = name.lastIndexOf('.');
    if (dotIndex != -1)
        name = name.substring(0, dotIndex);
    Rect rect = addImage(image, name);
    if (rect != null && settings.limitMemory)
        rect.unloadImage(file);
}
Also used : Rect(com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Example 3 with Rect

use of com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect in project libgdx by libgdx.

the class MaxRectsPacker method packPage.

private Page packPage(Array<Rect> inputRects) {
    int paddingX = settings.paddingX, paddingY = settings.paddingY;
    float maxWidth = settings.maxWidth, maxHeight = settings.maxHeight;
    int edgePaddingX = 0, edgePaddingY = 0;
    if (settings.edgePadding) {
        if (settings.duplicatePadding) {
            // If duplicatePadding, edges get only half padding.
            maxWidth -= paddingX;
            maxHeight -= paddingY;
        } else {
            maxWidth -= paddingX * 2;
            maxHeight -= paddingY * 2;
            edgePaddingX = paddingX;
            edgePaddingY = paddingY;
        }
    }
    // Find min size.
    int minWidth = Integer.MAX_VALUE, minHeight = Integer.MAX_VALUE;
    for (int i = 0, nn = inputRects.size; i < nn; i++) {
        Rect rect = inputRects.get(i);
        minWidth = Math.min(minWidth, rect.width);
        minHeight = Math.min(minHeight, rect.height);
        float width = rect.width - paddingX, height = rect.height - paddingY;
        if (settings.rotation) {
            if ((width > maxWidth || height > maxHeight) && (width > maxHeight || height > maxWidth)) {
                String paddingMessage = (edgePaddingX > 0 || edgePaddingY > 0) ? (" and edge padding " + paddingX + "," + paddingY) : "";
                throw new RuntimeException("Image does not fit with max page size " + settings.maxWidth + "x" + settings.maxHeight + paddingMessage + ": " + rect.name + "[" + width + "," + height + "]");
            }
        } else {
            if (width > maxWidth) {
                String paddingMessage = edgePaddingX > 0 ? (" and X edge padding " + paddingX) : "";
                throw new RuntimeException("Image does not fit with max page width " + settings.maxWidth + paddingMessage + ": " + rect.name + "[" + width + "," + height + "]");
            }
            if (height > maxHeight && (!settings.rotation || width > maxHeight)) {
                String paddingMessage = edgePaddingY > 0 ? (" and Y edge padding " + paddingY) : "";
                throw new RuntimeException("Image does not fit in max page height " + settings.maxHeight + paddingMessage + ": " + rect.name + "[" + width + "," + height + "]");
            }
        }
    }
    minWidth = Math.max(minWidth, settings.minWidth);
    minHeight = Math.max(minHeight, settings.minHeight);
    if (!settings.silent)
        System.out.print("Packing");
    // Find the minimal page size that fits all rects.
    Page bestResult = null;
    if (settings.square) {
        int minSize = Math.max(minWidth, minHeight);
        int maxSize = Math.min(settings.maxWidth, settings.maxHeight);
        BinarySearch sizeSearch = new BinarySearch(minSize, maxSize, settings.fast ? 25 : 15, settings.pot);
        int size = sizeSearch.reset(), i = 0;
        while (size != -1) {
            Page result = packAtSize(true, size - edgePaddingX, size - edgePaddingY, inputRects);
            if (!settings.silent) {
                if (++i % 70 == 0)
                    System.out.println();
                System.out.print(".");
            }
            bestResult = getBest(bestResult, result);
            size = sizeSearch.next(result == null);
        }
        if (!settings.silent)
            System.out.println();
        // Rects don't fit on one page. Fill a whole page and return.
        if (bestResult == null)
            bestResult = packAtSize(false, maxSize - edgePaddingX, maxSize - edgePaddingY, inputRects);
        sort.sort(bestResult.outputRects, rectComparator);
        bestResult.width = Math.max(bestResult.width, bestResult.height);
        bestResult.height = Math.max(bestResult.width, bestResult.height);
        return bestResult;
    } else {
        BinarySearch widthSearch = new BinarySearch(minWidth, settings.maxWidth, settings.fast ? 25 : 15, settings.pot);
        BinarySearch heightSearch = new BinarySearch(minHeight, settings.maxHeight, settings.fast ? 25 : 15, settings.pot);
        int width = widthSearch.reset(), i = 0;
        int height = settings.square ? width : heightSearch.reset();
        while (true) {
            Page bestWidthResult = null;
            while (width != -1) {
                Page result = packAtSize(true, width - edgePaddingX, height - edgePaddingY, inputRects);
                if (!settings.silent) {
                    if (++i % 70 == 0)
                        System.out.println();
                    System.out.print(".");
                }
                bestWidthResult = getBest(bestWidthResult, result);
                width = widthSearch.next(result == null);
                if (settings.square)
                    height = width;
            }
            bestResult = getBest(bestResult, bestWidthResult);
            if (settings.square)
                break;
            height = heightSearch.next(bestWidthResult == null);
            if (height == -1)
                break;
            width = widthSearch.reset();
        }
        if (!settings.silent)
            System.out.println();
        // Rects don't fit on one page. Fill a whole page and return.
        if (bestResult == null)
            bestResult = packAtSize(false, settings.maxWidth - edgePaddingX, settings.maxHeight - edgePaddingY, inputRects);
        sort.sort(bestResult.outputRects, rectComparator);
        return bestResult;
    }
}
Also used : Rect(com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect) Page(com.badlogic.gdx.tools.texturepacker.TexturePacker.Page)

Example 4 with Rect

use of com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect in project libgdx by libgdx.

the class TexturePackerTest method render.

public void render() {
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    Settings settings = new Settings();
    settings.fast = false;
    settings.pot = false;
    settings.maxWidth = 1024;
    settings.maxHeight = 1024;
    settings.rotation = false;
    settings.paddingX = 0;
    if (pages == null) {
        Random random = new Random(1243);
        Array<Rect> inputRects = new Array();
        for (int i = 0; i < 240; i++) {
            Rect rect = new Rect();
            rect.name = "rect" + i;
            rect.height = 16 + random.nextInt(120);
            rect.width = 16 + random.nextInt(240);
            inputRects.add(rect);
        }
        for (int i = 0; i < 10; i++) {
            Rect rect = new Rect();
            rect.name = "rect" + (40 + i);
            rect.height = 400 + random.nextInt(340);
            rect.width = 1 + random.nextInt(10);
            inputRects.add(rect);
        }
        long s = System.nanoTime();
        pages = new MaxRectsPacker(settings).pack(inputRects);
        long e = System.nanoTime();
        System.out.println("fast: " + settings.fast);
        System.out.println((e - s) / 1e6f + " ms");
        System.out.println();
    }
    int x = 20, y = 20;
    for (Page page : pages) {
        renderer.setColor(Color.GRAY);
        renderer.begin(ShapeType.Filled);
        for (int i = 0; i < page.outputRects.size; i++) {
            Rect rect = page.outputRects.get(i);
            renderer.rect(x + rect.x + settings.paddingX, y + rect.y + settings.paddingY, rect.width - settings.paddingX, rect.height - settings.paddingY);
        }
        renderer.end();
        renderer.setColor(Color.RED);
        renderer.begin(ShapeType.Line);
        for (int i = 0; i < page.outputRects.size; i++) {
            Rect rect = page.outputRects.get(i);
            renderer.rect(x + rect.x + settings.paddingX, y + rect.y + settings.paddingY, rect.width - settings.paddingX, rect.height - settings.paddingY);
        }
        renderer.setColor(Color.GREEN);
        renderer.rect(x, y, page.width + settings.paddingX * 2, page.height + settings.paddingY * 2);
        renderer.end();
        x += page.width + 20;
    }
}
Also used : Array(com.badlogic.gdx.utils.Array) Rect(com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect) Random(java.util.Random) Page(com.badlogic.gdx.tools.texturepacker.TexturePacker.Page) Settings(com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings)

Example 5 with Rect

use of com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect in project libgdx by libgdx.

the class GridPacker method pack.

public Array<Page> pack(Array<Rect> inputRects) {
    if (!settings.silent)
        System.out.print("Packing");
    int cellWidth = 0, cellHeight = 0;
    for (int i = 0, nn = inputRects.size; i < nn; i++) {
        Rect rect = inputRects.get(i);
        cellWidth = Math.max(cellWidth, rect.width);
        cellHeight = Math.max(cellHeight, rect.height);
    }
    cellWidth += settings.paddingX;
    cellHeight += settings.paddingY;
    inputRects.reverse();
    Array<Page> pages = new Array();
    while (inputRects.size > 0) {
        Page result = packPage(inputRects, cellWidth, cellHeight);
        pages.add(result);
    }
    return pages;
}
Also used : Array(com.badlogic.gdx.utils.Array) Rect(com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect) Page(com.badlogic.gdx.tools.texturepacker.TexturePacker.Page)

Aggregations

Rect (com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect)10 Page (com.badlogic.gdx.tools.texturepacker.TexturePacker.Page)6 Array (com.badlogic.gdx.utils.Array)5 BufferedImage (java.awt.image.BufferedImage)2 Alias (com.badlogic.gdx.tools.texturepacker.TexturePacker.Alias)1 Settings (com.badlogic.gdx.tools.texturepacker.TexturePacker.Settings)1 Graphics2D (java.awt.Graphics2D)1 WritableRaster (java.awt.image.WritableRaster)1 IOException (java.io.IOException)1 Random (java.util.Random)1 Matcher (java.util.regex.Matcher)1