下面列出了java.awt.image.BufferedImage#copyData() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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;
}
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;
}
/**
* 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;
}
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
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;
}
/**
* 画像を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);
}
/** 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);
}
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);
}
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);
}
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);
}
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);
}
public static BufferedImage deepCopy(BufferedImage image) {
return new BufferedImage(image.getColorModel(), image.copyData(null), image.getColorModel().isAlphaPremultiplied(), null);
}
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);
}
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);
}
private static BufferedImage copyImage(BufferedImage bi)
{
ColorModel cm = bi.getColorModel();
WritableRaster raster = bi.copyData(null);
return new BufferedImage(cm, raster, cm.isAlphaPremultiplied(), null);
}
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);
}