/** Extract a ninepatch from a texture atlas, according to the android specification. * @see <a href="http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch">ninepatch specification</a> * @param page The image file related to the page the region is in * @param region The region to extract */ private BufferedImage extractNinePatch (BufferedImage page, Region region, File outputDirFile) { BufferedImage splitImage = extractImage(page, region, outputDirFile, NINEPATCH_PADDING); Graphics2D g2 = splitImage.createGraphics(); g2.setColor(Color.BLACK); // Draw the four lines to save the ninepatch's padding and splits int startX = region.splits[0] + NINEPATCH_PADDING; int endX = region.width - region.splits[1] + NINEPATCH_PADDING - 1; int startY = region.splits[2] + NINEPATCH_PADDING; int endY = region.height - region.splits[3] + NINEPATCH_PADDING - 1; if (endX >= startX) g2.drawLine(startX, 0, endX, 0); if (endY >= startY) g2.drawLine(0, startY, 0, endY); if (region.pads != null) { int padStartX = region.pads[0] + NINEPATCH_PADDING; int padEndX = region.width - region.pads[1] + NINEPATCH_PADDING - 1; int padStartY = region.pads[2] + NINEPATCH_PADDING; int padEndY = region.height - region.pads[3] + NINEPATCH_PADDING - 1; g2.drawLine(padStartX, splitImage.getHeight() - 1, padEndX, splitImage.getHeight() - 1); g2.drawLine(splitImage.getWidth() - 1, padStartY, splitImage.getWidth() - 1, padEndY); } g2.dispose(); return splitImage; }
/** Extract an image from a texture atlas. * @param page The image file related to the page the region is in * @param region The region to extract * @param outputDirFile The output directory * @param padding padding (in pixels) to apply to the image * @return The extracted image */ private BufferedImage extractImage (BufferedImage page, Region region, File outputDirFile, int padding) { BufferedImage splitImage = null; // get the needed part of the page and rotate if needed if (region.rotate) { BufferedImage srcImage = page.getSubimage(region.left, region.top, region.height, region.width); splitImage = new BufferedImage(region.width, region.height, page.getType()); AffineTransform transform = new AffineTransform(); transform.rotate(Math.toRadians(90.0)); transform.translate(0, -region.width); AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR); op.filter(srcImage, splitImage); } else { splitImage = page.getSubimage(region.left, region.top, region.width, region.height); } // draw the image to a bigger one if padding is needed if (padding > 0) { BufferedImage paddedImage = new BufferedImage(splitImage.getWidth() + padding * 2, splitImage.getHeight() + padding * 2, page.getType()); Graphics2D g2 = paddedImage.createGraphics(); g2.drawImage(splitImage, padding, padding, null); g2.dispose(); return paddedImage; } else { return splitImage; } }
private void load (TextureAtlasData data) { ObjectMap<Page, Texture> pageToTexture = new ObjectMap<Page, Texture>(); for (Page page : data.pages) { Texture texture = null; if (page.texture == null) { texture = new Texture(page.textureFile, page.format, page.useMipMaps); texture.setFilter(page.minFilter, page.magFilter); texture.setWrap(page.uWrap, page.vWrap); } else { texture = page.texture; texture.setFilter(page.minFilter, page.magFilter); texture.setWrap(page.uWrap, page.vWrap); } textures.add(texture); pageToTexture.put(page, texture); } for (Region region : data.regions) { int width = region.width; int height = region.height; AtlasRegion atlasRegion = new AtlasRegion(pageToTexture.get(region.page), region.left, region.top, region.rotate ? height : width, region.rotate ? width : height); atlasRegion.index = region.index; atlasRegion.name = region.name; atlasRegion.offsetX = region.offsetX; atlasRegion.offsetY = region.offsetY; atlasRegion.originalHeight = region.originalHeight; atlasRegion.originalWidth = region.originalWidth; atlasRegion.rotate = region.rotate; atlasRegion.splits = region.splits; atlasRegion.pads = region.pads; if (region.flip) atlasRegion.flip(false, true); regions.add(atlasRegion); } }
public int compare (Region region1, Region region2) { int i1 = region1.index; if (i1 == -1) i1 = Integer.MAX_VALUE; int i2 = region2.index; if (i2 == -1) i2 = Integer.MAX_VALUE; return i1 - i2; }
/** Extract an image from a texture atlas. * @param page The image file related to the page the region is in * @param region The region to extract * @param outputDirFile The output directory * @param padding padding (in pixels) to apply to the image * @return The extracted image */ private BufferedImage extractImage (BufferedImage page, Region region, File outputDirFile, int padding) { BufferedImage splitImage = null; // get the needed part of the page and rotate if needed if (region.rotate) { BufferedImage srcImage = page.getSubimage(region.left, region.top, region.height, region.width); splitImage = new BufferedImage(region.height, region.width, page.getType()); AffineTransform transform = new AffineTransform(); transform.rotate(Math.toRadians(90.0)); transform.translate(0, -region.width); AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR); op.filter(srcImage, splitImage); } else { splitImage = page.getSubimage(region.left, region.top, region.width, region.height); } // draw the image to a bigger one if padding is needed if (padding > 0) { BufferedImage paddedImage = new BufferedImage(splitImage.getWidth() + padding * 2, splitImage.getHeight() + padding * 2, page.getType()); Graphics2D g2 = paddedImage.createGraphics(); g2.drawImage(splitImage, padding, padding, null); g2.dispose(); return paddedImage; } else { return splitImage; } }
/** * Extract a ninepatch from a texture atlas, according to the android * specification. * * @see <a * href="http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch">ninepatch * specification</a> * @param page * The image file related to the page the region is in * @param region * The region to extract */ private BufferedImage extractNinePatch(BufferedImage page, Region region, File outputDirFile) { BufferedImage splitImage = extractImage(page, region, outputDirFile, NINEPATCH_PADDING); Graphics2D g2 = splitImage.createGraphics(); g2.setColor(Color.BLACK); // Draw the four lines to save the ninepatch's padding and splits int startX = region.splits[0] + NINEPATCH_PADDING; int endX = region.width - region.splits[1] + NINEPATCH_PADDING - 1; int startY = region.splits[2] + NINEPATCH_PADDING; int endY = region.height - region.splits[3] + NINEPATCH_PADDING - 1; if (endX >= startX) g2.drawLine(startX, 0, endX, 0); if (endY >= startY) g2.drawLine(0, startY, 0, endY); if (region.pads != null) { int padStartX = region.pads[0] + NINEPATCH_PADDING; int padEndX = region.width - region.pads[1] + NINEPATCH_PADDING - 1; int padStartY = region.pads[2] + NINEPATCH_PADDING; int padEndY = region.height - region.pads[3] + NINEPATCH_PADDING - 1; g2.drawLine(padStartX, splitImage.getHeight() - 1, padEndX, splitImage.getHeight() - 1); g2.drawLine(splitImage.getWidth() - 1, padStartY, splitImage.getWidth() - 1, padEndY); } g2.dispose(); return splitImage; }
public Array<Region> getRegions () { return regions; }
public static void crack(String srcAtlas,String dstDir) throws Exception{ FileHandle fh = Gdx.files.absolute(srcAtlas); TextureAtlasData data = new TextureAtlasData(fh, fh.parent(),false); File dir = new File(dstDir); if(!dir.exists()){ dir.mkdirs(); System.out.println("mkdirs:"+dstDir); } for(Region region:data.getRegions()){ File file = region.page.textureFile.file(); BufferedImage root = ImageIO.read(file); String fileName = region.name ; int sizeWidth = region.originalWidth; int sizeHeight= region.originalHeight; int width = region.width; int height= region.height; int x = region.left ; int y = region.top ; int offsetX = (int)region.offsetX; int offsetY = (int)region.offsetY; BufferedImage canvas = null; if(region.rotate){ canvas = new BufferedImage(height, width, BufferedImage.TYPE_INT_ARGB); canvas.getGraphics().drawImage(root, 0, 0, height, width, x, y, x+height, y+width, null); }else{ canvas = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); canvas.getGraphics().drawImage(root, 0, 0, width, height, x, y, x+width, y+height, null); } if(offsetX!=0 || offsetY!=0){ BufferedImage canvas2 = canvas; canvas = new BufferedImage(sizeWidth, sizeHeight, BufferedImage.TYPE_INT_ARGB); canvas.getGraphics().drawImage(canvas2, offsetX, offsetY, width, height, 0, 0, width, height, null); } if(region.rotate){ canvas = rotate(canvas, Math.toRadians(90)); } ImageIO.write(canvas, "png", new File(dstDir+fileName+".png")); System.out.println("Proccess to "+dstDir+fileName +".png" + " offsetX:"+region.offsetX+",offsetY:"+region.offsetY +" rotate:"+region.rotate); } }