use of java.awt.image.BufferedImage in project libgdx by libgdx.
the class DistanceFieldGenerator method generateDistanceField.
/**
* Process the image into a distance field.
*
* The input image should be binary (black/white), but if not, see {@link #isInside(int)}.
*
* The returned image is a factor of {@code upscale} smaller than {@code inImage}.
* Opaque pixels more than {@link #spread} away in the output image from white remain opaque;
* transparent pixels more than {@link #spread} away in the output image from black remain transparent.
* In between, we get a smooth transition from opaque to transparent, with an alpha value of 0.5
* when we are exactly on the edge.
*
* @param inImage the image to process.
* @return the distance field image
*/
public BufferedImage generateDistanceField(BufferedImage inImage) {
final int inWidth = inImage.getWidth();
final int inHeight = inImage.getHeight();
final int outWidth = inWidth / downscale;
final int outHeight = inHeight / downscale;
final BufferedImage outImage = new BufferedImage(outWidth, outHeight, BufferedImage.TYPE_4BYTE_ABGR);
// Note: coordinates reversed to mimic storage of BufferedImage, for memory locality
final boolean[][] bitmap = new boolean[inHeight][inWidth];
for (int y = 0; y < inHeight; ++y) {
for (int x = 0; x < inWidth; ++x) {
bitmap[y][x] = isInside(inImage.getRGB(x, y));
}
}
for (int y = 0; y < outHeight; ++y) {
for (int x = 0; x < outWidth; ++x) {
int centerX = (x * downscale) + (downscale / 2);
int centerY = (y * downscale) + (downscale / 2);
float signedDistance = findSignedDistance(centerX, centerY, bitmap);
outImage.setRGB(x, y, distanceToRGB(signedDistance));
}
}
return outImage;
}
use of java.awt.image.BufferedImage in project libgdx by libgdx.
the class PreAlpha method generatePremultiplyAlpha.
private void generatePremultiplyAlpha(File out) {
try {
BufferedImage outImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
float[] color = new float[4];
WritableRaster raster = image.getRaster();
WritableRaster outRaster = outImage.getRaster();
for (int x = 0, w = image.getWidth(); x < w; ++x) for (int y = 0, h = image.getHeight(); y < h; ++y) {
raster.getPixel(x, y, color);
float alpha = color[3] / 255f;
for (int i = 0; i < 3; ++i) color[i] *= alpha;
outRaster.setPixel(x, y, color);
}
ImageIO.write(outImage, "png", out);
} catch (IOException e) {
e.printStackTrace();
}
}
use of java.awt.image.BufferedImage in project libgdx by libgdx.
the class Hiero method getColorIcon.
static Icon getColorIcon(java.awt.Color color) {
BufferedImage image = new BufferedImage(32, 16, BufferedImage.TYPE_INT_RGB);
java.awt.Graphics g = image.getGraphics();
g.setColor(color);
g.fillRect(1, 1, 30, 14);
g.setColor(java.awt.Color.black);
g.drawRect(0, 0, 31, 15);
return new ImageIcon(image);
}
use of java.awt.image.BufferedImage in project libgdx by libgdx.
the class ImagePacker method main.
public static void main(String[] argv) throws IOException {
Random rand = new Random(0);
ImagePacker packer = new ImagePacker(512, 512, 1, true);
BufferedImage[] images = new BufferedImage[100];
for (int i = 0; i < images.length; i++) {
Color color = new Color((float) Math.random(), (float) Math.random(), (float) Math.random(), 1);
images[i] = createImage(rand.nextInt(50) + 10, rand.nextInt(50) + 10, color);
}
// BufferedImage[] images = { ImageIO.read( new File( "test.png" ) ) };
Arrays.sort(images, new Comparator<BufferedImage>() {
@Override
public int compare(BufferedImage o1, BufferedImage o2) {
return o2.getWidth() * o2.getHeight() - o1.getWidth() * o1.getHeight();
}
});
for (int i = 0; i < images.length; i++) packer.insertImage("" + i, images[i]);
ImageIO.write(packer.getImage(), "png", new File("packed.png"));
}
use of java.awt.image.BufferedImage in project libgdx by libgdx.
the class ImagePacker method createImage.
private static BufferedImage createImage(int width, int height, Color color) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
Graphics2D g = image.createGraphics();
g.setColor(color);
g.fillRect(0, 0, width, height);
g.dispose();
return image;
}
Aggregations