Java Code Examples for java.awt.image.BufferedImage#copyData()

The following examples show how to use java.awt.image.BufferedImage#copyData() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
@NotNull
@Override
public BufferedImage applyFilter(@NotNull BufferedImage bufferedImage) {
    Raster src = bufferedImage.getData();
    WritableRaster dest = src.createCompatibleWritableRaster();
    bufferedImage.copyData(dest);
    for (int y = pixelateSection.y; y < pixelateSection.y + pixelateSection.getHeight(); y += PIX_SIZE) {
        for (int x = pixelateSection.x; x < pixelateSection.x + pixelateSection.getWidth(); x += PIX_SIZE) {
            double[] pixel = new double[3];
            pixel = src.getPixel(x, y, pixel);
            for (int yd = y; (yd < y + PIX_SIZE) && (yd < dest.getHeight()); yd++) {
                for (int xd = x; (xd < x + PIX_SIZE) && (xd < dest.getWidth()); xd++) {
                    dest.setPixel(xd, yd, pixel);
                }
            }
        }
    }
    bufferedImage.setData(dest);
    return bufferedImage;
}
 
Example 2
BufferedImage getSplat(int id, int damage)
{
	//Get a copy of the hitsplat to get a clean one each time
	final BufferedImage rawSplat = spriteManager.getSprite(id, 0);
	if (rawSplat == null)
	{
		return null;
	}

	final BufferedImage splat = new BufferedImage(
		rawSplat.getColorModel(),
		rawSplat.copyData(null),
		rawSplat.getColorModel().isAlphaPremultiplied(),
		null);

	final Graphics g = splat.getGraphics();
	g.setFont(FontManager.getRunescapeSmallFont());

	// Align the text in the centre of the hitsplat
	final FontMetrics metrics = g.getFontMetrics();
	final String text = String.valueOf(damage);
	final int x = (splat.getWidth() - metrics.stringWidth(text)) / 2;
	final int y = (splat.getHeight() - metrics.getHeight()) / 2 + metrics.getAscent();

	g.setColor(Color.BLACK);
	g.drawString(String.valueOf(damage), x + 1, y + 1);
	g.setColor(Color.WHITE);
	g.drawString(String.valueOf(damage), x, y);
	return splat;
}
 
Example 3
Source Project: BlueMap   File: TextureGallery.java    License: MIT License 5 votes vote down vote up
/**
 * Loads a {@link Texture} from the {@link FileAccess} and the given path and returns it.<br>
 * If there is already a {@link Texture} with this path in this Gallery it replaces the {@link Texture} with the new one 
 * and the new one will have the same id as the old one.<br>
 * Otherwise the {@link Texture} will be added to the end of this gallery with the next available id.
 * @param fileAccess The {@link FileAccess} to load the image from.
 * @param path The path of the image on the {@link FileAccess}
 * @return The loaded {@link Texture}
 * @throws FileNotFoundException If there is no image in that FileAccess on that path
 * @throws IOException If an IOException occurred while loading the file
 */
public synchronized Texture loadTexture(FileAccess fileAccess, String path) throws FileNotFoundException, IOException {
	try (InputStream input = fileAccess.readFile(path)) {
		BufferedImage image = ImageIO.read(input);
		if (image == null) throw new IOException("Failed to read image: " + path);
		
		//crop off animation frames
		if (image.getHeight() > image.getWidth()){
			BufferedImage cropped = new BufferedImage(image.getWidth(), image.getWidth(), image.getType());
			image.copyData(cropped.getRaster());
			image = cropped;
		}
		
		//check halfTransparency
		boolean halfTransparent = checkHalfTransparent(image);
		
		//calculate color
		Vector4f color = calculateColor(image);
		
		//write to Base64
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		ImageIO.write(image, "png", os);
		String base64 = "data:image/png;base64," + Base64.getEncoder().encodeToString(os.toByteArray());
		
		//replace if texture with this path already exists
		Texture texture = textureMap.get(path);
		if (texture != null) {
			texture = new Texture(texture.getId(), path, color, halfTransparent, base64);
			textureMap.put(path, texture);
			textureList.set(texture.getId(), texture);
		} else {
			texture = new Texture(textureList.size(), path, color, halfTransparent, base64);
			textureMap.put(path, texture);
			textureList.add(texture);
		}
		
		return texture;
	}
}
 
Example 4
/**
 * Deep clone a BufferedIamge
 *
 * @param bi Original image
 * @return Cloned image
 */
public static BufferedImage deepCopy(BufferedImage bi) {
    ColorModel cm = bi.getColorModel();
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    WritableRaster raster = bi.copyData(null);
    return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
 
Example 5
/**
 * Make a copy of the {@link BufferedImage} object.
 *
 * @param image the provided image.
 * @return copy of the provided image.
 */
static BufferedImage deepCopy(BufferedImage image) {
    ColorModel cm = image.getColorModel();
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    WritableRaster raster = image.copyData(image.getRaster().createCompatibleWritableRaster());
    return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
 
Example 6
/**
 * Create a copy of the image.
 * @param image the image to copy.
 * @return the copy image.
 */
public static BufferedImage deepCopy(BufferedImage image) {
    return new BufferedImage(
            image.getColorModel(),
            image.copyData(null),
            image.isAlphaPremultiplied(),
            null);
}
 
Example 7
Source Project: scipio-erp   File: ImageTransform.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * SCIPIO: Attempts to create an exact copy of the original image in a new instance.
 * WARN: TODO: currently not guaranteed to work for all images.
 * Added 2017-07-14.
 * @return the cloned image
 */
public static BufferedImage cloneBufferedImage(BufferedImage image) {
    ColorModel colorModel = image.getColorModel();
    return new BufferedImage(colorModel,
            //image.copyData(image.getRaster().createCompatibleWritableRaster()),
            image.copyData(colorModel.createCompatibleWritableRaster(image.getWidth(null), image.getHeight(null))),
            colorModel.isAlphaPremultiplied(), null);
}
 
Example 8
private BufferedImage getSplat(int id, int damage)
{
	//Get a copy of the hitsplat to get a clean one each time
	final BufferedImage rawSplat = spriteManager.getSprite(id, 0);
	if (rawSplat == null)
	{
		return null;
	}

	final BufferedImage splat = new BufferedImage(
		rawSplat.getColorModel(),
		rawSplat.copyData(null),
		rawSplat.getColorModel().isAlphaPremultiplied(),
		null);

	final Graphics g = splat.getGraphics();
	g.setFont(FontManager.getRunescapeSmallFont());

	// Align the text in the centre of the hitsplat
	final FontMetrics metrics = g.getFontMetrics();
	final String text = String.valueOf(damage);
	final int x = (splat.getWidth() - metrics.stringWidth(text)) / 2;
	final int y = (splat.getHeight() - metrics.getHeight()) / 2 + metrics.getAscent();

	g.setColor(Color.BLACK);
	g.drawString(String.valueOf(damage), x + 1, y + 1);
	g.setColor(Color.WHITE);
	g.drawString(String.valueOf(damage), x, y);
	return splat;
}
 
Example 9
Source Project: hifive-pitalium   File: ImageUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 画像をDeepCopyします。
 *
 * @param image DeepCopy元の{@link BufferedImage}
 * @return DeepCopyされた {@link BufferedImage}
 */
private static BufferedImage getDeepCopyImage(BufferedImage image) {
	ColorModel cm = image.getColorModel();
	boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
	WritableRaster raster = image.copyData(null);
	WritableRaster rasterChild = (WritableRaster) raster.createChild(0, 0, image.getWidth(), image.getHeight(),
			image.getMinX(), image.getMinY(), null);
	return new BufferedImage(cm, rasterChild, isAlphaPremultiplied, null);
}
 
Example 10
/** Copies a buffered image. */
protected static BufferedImage copyImage(BufferedImage img) {
    ColorModel cm = img.getColorModel();
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    WritableRaster raster = img.copyData(null);
    return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
 
Example 11
Source Project: yt-java-game   File: Sprite.java    License: MIT License 4 votes vote down vote up
public Sprite getNewSubimage(int x, int y, int w, int h) {
	BufferedImage temp = image.getSubimage(x, y, w, h);
	BufferedImage newImage = new BufferedImage(image.getColorModel(), image.getRaster().createCompatibleWritableRaster(w,h), image.isAlphaPremultiplied(), null);
	temp.copyData(newImage.getRaster());
       return new Sprite(newImage);
}
 
Example 12
public static BufferedImage deepCopy(BufferedImage img) {
	ColorModel cm = img.getColorModel();
	boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
	WritableRaster raster = img.copyData(null);
	return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
 
Example 13
private static BufferedImage deepCopy(BufferedImage bi) {
	 ColorModel cm = bi.getColorModel();
	 boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
	 WritableRaster raster = bi.copyData(null);
	 return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
 
Example 14
Source Project: Carcassonne   File: PaintShop.java    License: Eclipse Public License 2.0 4 votes vote down vote up
private static BufferedImage deepCopy(BufferedImage image) {
    ColorModel model = image.getColorModel();
    boolean isAlphaPremultiplied = model.isAlphaPremultiplied();
    WritableRaster raster = image.copyData(null);
    return new BufferedImage(model, raster, isAlphaPremultiplied, null);
}
 
Example 15
Source Project: pdfcompare   File: ImageTools.java    License: Apache License 2.0 4 votes vote down vote up
public static BufferedImage deepCopy(BufferedImage image) {
    return new BufferedImage(image.getColorModel(), image.copyData(null), image.getColorModel().isAlphaPremultiplied(), null);
}
 
Example 16
Source Project: cineast   File: MultiImageFactory.java    License: MIT License 4 votes vote down vote up
public static BufferedImage copyBufferedImg(BufferedImage img) {
	ColorModel cm = img.getColorModel();
	boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
	WritableRaster raster = img.copyData(null);
	return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
 
Example 17
Source Project: Genetic-Algorithm-Montage   File: Utils.java    License: Apache License 2.0 4 votes vote down vote up
public static BufferedImage deepCopy(BufferedImage bi) {
    ColorModel cm = bi.getColorModel();
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    WritableRaster raster = bi.copyData(bi.getRaster().createCompatibleWritableRaster());
    return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}
 
Example 18
Source Project: ChatGameFontificator   File: Sprite.java    License: The Unlicense 4 votes vote down vote up
private static BufferedImage copyImage(BufferedImage bi)
{
    ColorModel cm = bi.getColorModel();
    WritableRaster raster = bi.copyData(null);
    return new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);
}
 
Example 19
Source Project: gpx-animator   File: Utils.java    License: Apache License 2.0 4 votes vote down vote up
public static BufferedImage deepCopy(final BufferedImage bi) {
    final ColorModel cm = bi.getColorModel();
    final boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    final WritableRaster raster = bi.copyData(null);
    return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}