use of com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect in project libgdx by libgdx.
the class ImageProcessor method addImage.
/** The image will be kept in-memory during packing.
* @see #addImage(File) */
public Rect addImage(BufferedImage image, String name) {
Rect rect = processImage(image, name);
if (rect == null) {
if (!settings.silent)
System.out.println("Ignoring blank input image: " + name);
return null;
}
if (settings.alias) {
String crc = hash(rect.getImage(this));
Rect existing = crcs.get(crc);
if (existing != null) {
if (!settings.silent)
System.out.println(rect.name + " (alias of " + existing.name + ")");
existing.aliases.add(new Alias(rect));
return null;
}
crcs.put(crc, rect);
}
rects.add(rect);
return rect;
}
use of com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect in project libgdx by libgdx.
the class ImageProcessor method stripWhitespace.
/** Strips whitespace and returns the rect, or null if the image should be ignored. */
private Rect stripWhitespace(BufferedImage source) {
WritableRaster alphaRaster = source.getAlphaRaster();
if (alphaRaster == null || (!settings.stripWhitespaceX && !settings.stripWhitespaceY))
return new Rect(source, 0, 0, source.getWidth(), source.getHeight(), false);
final byte[] a = new byte[1];
int top = 0;
int bottom = source.getHeight();
if (settings.stripWhitespaceX) {
outer: for (int y = 0; y < source.getHeight(); y++) {
for (int x = 0; x < source.getWidth(); x++) {
alphaRaster.getDataElements(x, y, a);
int alpha = a[0];
if (alpha < 0)
alpha += 256;
if (alpha > settings.alphaThreshold)
break outer;
}
top++;
}
outer: for (int y = source.getHeight(); --y >= top; ) {
for (int x = 0; x < source.getWidth(); x++) {
alphaRaster.getDataElements(x, y, a);
int alpha = a[0];
if (alpha < 0)
alpha += 256;
if (alpha > settings.alphaThreshold)
break outer;
}
bottom--;
}
// Leave 1px so nothing is copied into padding.
if (settings.duplicatePadding) {
if (top > 0)
top--;
if (bottom < source.getHeight())
bottom++;
}
}
int left = 0;
int right = source.getWidth();
if (settings.stripWhitespaceY) {
outer: for (int x = 0; x < source.getWidth(); x++) {
for (int y = top; y < bottom; y++) {
alphaRaster.getDataElements(x, y, a);
int alpha = a[0];
if (alpha < 0)
alpha += 256;
if (alpha > settings.alphaThreshold)
break outer;
}
left++;
}
outer: for (int x = source.getWidth(); --x >= left; ) {
for (int y = top; y < bottom; y++) {
alphaRaster.getDataElements(x, y, a);
int alpha = a[0];
if (alpha < 0)
alpha += 256;
if (alpha > settings.alphaThreshold)
break outer;
}
right--;
}
// Leave 1px so nothing is copied into padding.
if (settings.duplicatePadding) {
if (left > 0)
left--;
if (right < source.getWidth())
right++;
}
}
int newWidth = right - left;
int newHeight = bottom - top;
if (newWidth <= 0 || newHeight <= 0) {
if (settings.ignoreBlankImages)
return null;
else
return new Rect(emptyImage, 0, 0, 1, 1, false);
}
return new Rect(source, left, top, newWidth, newHeight, false);
}
use of com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect in project libgdx by libgdx.
the class ImageProcessor method processImage.
/** Returns a rect for the image describing the texture region to be packed, or null if the image should not be packed. */
Rect processImage(BufferedImage image, String name) {
if (scale <= 0)
throw new IllegalArgumentException("scale cannot be <= 0: " + scale);
int width = image.getWidth(), height = image.getHeight();
if (image.getType() != BufferedImage.TYPE_4BYTE_ABGR) {
BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
newImage.getGraphics().drawImage(image, 0, 0, null);
image = newImage;
}
boolean isPatch = name.endsWith(".9");
int[] splits = null, pads = null;
Rect rect = null;
if (isPatch) {
// Strip ".9" from file name, read ninepatch split pixels, and strip ninepatch split pixels.
name = name.substring(0, name.length() - 2);
splits = getSplits(image, name);
pads = getPads(image, name, splits);
// Strip split pixels.
width -= 2;
height -= 2;
BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
newImage.getGraphics().drawImage(image, 0, 0, width, height, 1, 1, width + 1, height + 1, null);
image = newImage;
}
// Scale image.
if (scale != 1) {
int originalWidth = width, originalHeight = height;
width = Math.max(1, Math.round(width * scale));
height = Math.max(1, Math.round(height * scale));
BufferedImage newImage = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
if (scale < 1) {
newImage.getGraphics().drawImage(image.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING), 0, 0, null);
} else {
Graphics2D g = (Graphics2D) newImage.getGraphics();
g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
g.drawImage(image, 0, 0, width, height, null);
}
image = newImage;
}
if (isPatch) {
// Ninepatches aren't rotated or whitespace stripped.
rect = new Rect(image, 0, 0, width, height, true);
rect.splits = splits;
rect.pads = pads;
rect.canRotate = false;
} else {
rect = stripWhitespace(image);
if (rect == null)
return null;
}
// Strip digits off end of name and use as index.
int index = -1;
if (settings.useIndexes) {
Matcher matcher = indexPattern.matcher(name);
if (matcher.matches()) {
name = matcher.group(1);
index = Integer.parseInt(matcher.group(2));
}
}
rect.name = name;
rect.index = index;
return rect;
}
use of com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect in project libgdx by libgdx.
the class MaxRectsPacker method packAtSize.
/** @param fully If true, the only results that pack all rects will be considered. If false, all results are considered, not
* all rects may be packed. */
private Page packAtSize(boolean fully, int width, int height, Array<Rect> inputRects) {
Page bestResult = null;
for (int i = 0, n = methods.length; i < n; i++) {
maxRects.init(width, height);
Page result;
if (!settings.fast) {
result = maxRects.pack(inputRects, methods[i]);
} else {
Array<Rect> remaining = new Array();
for (int ii = 0, nn = inputRects.size; ii < nn; ii++) {
Rect rect = inputRects.get(ii);
if (maxRects.insert(rect, methods[i]) == null) {
while (ii < nn) remaining.add(inputRects.get(ii++));
}
}
result = maxRects.getResult();
result.remainingRects = remaining;
}
if (fully && result.remainingRects.size > 0)
continue;
if (result.outputRects.size == 0)
continue;
bestResult = getBest(bestResult, result);
}
return bestResult;
}
use of com.badlogic.gdx.tools.texturepacker.TexturePacker.Rect in project libgdx by libgdx.
the class MaxRectsPacker method pack.
public Array<Page> pack(Array<Rect> inputRects) {
for (int i = 0, nn = inputRects.size; i < nn; i++) {
Rect rect = inputRects.get(i);
rect.width += settings.paddingX;
rect.height += settings.paddingY;
}
if (settings.fast) {
if (settings.rotation) {
// Sort by longest side if rotation is enabled.
sort.sort(inputRects, new Comparator<Rect>() {
public int compare(Rect o1, Rect o2) {
int n1 = o1.width > o1.height ? o1.width : o1.height;
int n2 = o2.width > o2.height ? o2.width : o2.height;
return n2 - n1;
}
});
} else {
// Sort only by width (largest to smallest) if rotation is disabled.
sort.sort(inputRects, new Comparator<Rect>() {
public int compare(Rect o1, Rect o2) {
return o2.width - o1.width;
}
});
}
}
Array<Page> pages = new Array();
while (inputRects.size > 0) {
Page result = packPage(inputRects);
pages.add(result);
inputRects = result.remainingRects;
}
return pages;
}
Aggregations