Search in sources :

Example 41 with BufferedImage

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;
}
Also used : BufferedImage(java.awt.image.BufferedImage)

Example 42 with BufferedImage

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();
    }
}
Also used : WritableRaster(java.awt.image.WritableRaster) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage)

Example 43 with BufferedImage

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);
}
Also used : ImageIcon(javax.swing.ImageIcon) BufferedImage(java.awt.image.BufferedImage)

Example 44 with BufferedImage

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"));
}
Also used : Random(java.util.Random) Color(java.awt.Color) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 45 with BufferedImage

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;
}
Also used : BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D)

Aggregations

BufferedImage (java.awt.image.BufferedImage)1702 Graphics2D (java.awt.Graphics2D)376 IOException (java.io.IOException)220 File (java.io.File)195 FunctionException (lucee.runtime.exp.FunctionException)122 Graphics (java.awt.Graphics)104 ByteArrayOutputStream (java.io.ByteArrayOutputStream)89 Color (java.awt.Color)88 ByteArrayInputStream (java.io.ByteArrayInputStream)86 Test (org.junit.Test)81 WritableRaster (java.awt.image.WritableRaster)75 Point (java.awt.Point)71 Rectangle (java.awt.Rectangle)68 AffineTransform (java.awt.geom.AffineTransform)57 Image (java.awt.Image)56 InputStream (java.io.InputStream)51 Dimension (java.awt.Dimension)50 ImageIcon (javax.swing.ImageIcon)50 LayoutlibDelegate (com.android.tools.layoutlib.annotations.LayoutlibDelegate)46 ImageWriter (javax.imageio.ImageWriter)45