Search in sources :

Example 11 with AffineTransformOp

use of java.awt.image.AffineTransformOp in project litiengine by gurkenlabs.

the class ImageProcessing method scaleImage.

public static BufferedImage scaleImage(final BufferedImage image, final int width, final int height, final boolean keepRatio, final boolean fill) {
    if (width == 0 || height == 0 || image == null) {
        return null;
    }
    final int imageWidth = image.getWidth();
    final int imageHeight = image.getHeight();
    double newWidth = width;
    double newHeight = height;
    if (keepRatio) {
        final double ratioWidth = image.getWidth() / (double) image.getHeight();
        final double ratioHeight = image.getHeight() / (double) image.getWidth();
        newHeight = newWidth * ratioHeight;
        if (newHeight > height) {
            newHeight = height;
            newWidth = newHeight * ratioWidth;
        }
    }
    final double scaleX = newWidth / imageWidth;
    final double scaleY = newHeight / imageHeight;
    final AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
    final AffineTransformOp bilinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    final BufferedImage scaled = bilinearScaleOp.filter(image, getCompatibleImage((int) newWidth, (int) newHeight));
    final BufferedImage newImg = getCompatibleImage((int) newWidth, (int) newHeight);
    if (newImg == null) {
        return image;
    }
    final Graphics2D g = (Graphics2D) newImg.getGraphics();
    g.drawImage(scaled, 0, 0, null);
    g.dispose();
    if (fill && (newWidth != width || newHeight != height)) {
        final BufferedImage wrapperImage = getCompatibleImage(width, height);
        final Graphics2D g2 = (Graphics2D) wrapperImage.getGraphics();
        g2.drawImage(newImg, (int) ((width - newWidth) / 2.0), (int) ((height - newHeight) / 2.0), null);
        g2.dispose();
        return wrapperImage;
    }
    return newImg;
}
Also used : AffineTransform(java.awt.geom.AffineTransform) BufferedImage(java.awt.image.BufferedImage) AffineTransformOp(java.awt.image.AffineTransformOp) Graphics2D(java.awt.Graphics2D)

Example 12 with AffineTransformOp

use of java.awt.image.AffineTransformOp in project litiengine by gurkenlabs.

the class ImageProcessing method rotate.

public static BufferedImage rotate(final BufferedImage bufferedImage, final double radians) {
    final AffineTransform tx = new AffineTransform();
    tx.rotate(radians, bufferedImage.getWidth() / 2.0, bufferedImage.getHeight() / 2.0);
    final AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    return op.filter(bufferedImage, null);
}
Also used : AffineTransform(java.awt.geom.AffineTransform) AffineTransformOp(java.awt.image.AffineTransformOp)

Example 13 with AffineTransformOp

use of java.awt.image.AffineTransformOp in project MtgDesktopCompanion by nicho92.

the class FlipActions method actionPerformed.

@Override
public void actionPerformed(ActionEvent e) {
    try {
        MagicCard mc = MTGControler.getInstance().getEnabledProviders().searchCardByCriteria("name", card.getMagicCard().getRotatedCardName(), card.getMagicCard().getEditions().get(0), true).get(0);
        card.setMagicCard(mc);
        BufferedImage bufferedImage = new BufferedImage(card.getWidth(), card.getHeight(), BufferedImage.TYPE_INT_RGB);
        AffineTransform tx = AffineTransform.getScaleInstance(-1, -1);
        tx.translate(-bufferedImage.getWidth(null), -bufferedImage.getHeight(null));
        AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
        bufferedImage = op.filter(bufferedImage, null);
        Graphics2D g2 = bufferedImage.createGraphics();
        g2.drawImage(card.getImageIcon().getImage(), tx, null);
        g2.dispose();
        card.setImage(new ImageIcon(bufferedImage));
        card.setRotated(true);
        card.revalidate();
        card.repaint();
        card.initActions();
        GamePanelGUI.getInstance().getPlayer().logAction("Flip " + card.getMagicCard());
    } catch (Exception ex) {
        logger.error(ex);
    }
}
Also used : ImageIcon(javax.swing.ImageIcon) MagicCard(org.magic.api.beans.MagicCard) AffineTransform(java.awt.geom.AffineTransform) BufferedImage(java.awt.image.BufferedImage) AffineTransformOp(java.awt.image.AffineTransformOp) Graphics2D(java.awt.Graphics2D)

Example 14 with AffineTransformOp

use of java.awt.image.AffineTransformOp in project ultimate-java by pantinor.

the class TextureUnpacker method extractImage.

/**
 * Extract an image from a texture atlas.
 *
 * @param page The image file related to the page the region is in
 * @param region The region to extract
 * @param outputDirFile The output directory
 * @param padding padding (in pixels) to apply to the image
 * @return The extracted image
 */
private BufferedImage extractImage(BufferedImage page, Region region, File outputDirFile, int padding) {
    BufferedImage splitImage = null;
    // get the needed part of the page and rotate if needed
    if (region.rotate) {
        BufferedImage srcImage = page.getSubimage(region.left, region.top, region.height, region.width);
        splitImage = new BufferedImage(region.width, region.height, page.getType());
        AffineTransform transform = new AffineTransform();
        transform.rotate(Math.toRadians(90.0));
        transform.translate(0, -region.width);
        AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
        op.filter(srcImage, splitImage);
    } else {
        splitImage = page.getSubimage(region.left, region.top, region.width, region.height);
    }
    // draw the image to a bigger one if padding is needed
    if (padding > 0) {
        BufferedImage paddedImage = new BufferedImage(splitImage.getWidth() + padding * 2, splitImage.getHeight() + padding * 2, page.getType());
        Graphics2D g2 = paddedImage.createGraphics();
        g2.drawImage(splitImage, padding, padding, null);
        g2.dispose();
        return paddedImage;
    } else {
        return splitImage;
    }
}
Also used : AffineTransform(java.awt.geom.AffineTransform) BufferedImage(java.awt.image.BufferedImage) AffineTransformOp(java.awt.image.AffineTransformOp) Graphics2D(java.awt.Graphics2D)

Example 15 with AffineTransformOp

use of java.awt.image.AffineTransformOp in project bladecoder-adventure-engine by bladecoder.

the class CustomTextureUnpacker method extractImage.

/**
 * Extract an image from a texture atlas.
 *
 * @param page
 *            The image file related to the page the region is in
 * @param region
 *            The region to extract
 * @param outputDirFile
 *            The output directory
 * @param padding
 *            padding (in pixels) to apply to the image
 * @return The extracted image
 */
private BufferedImage extractImage(BufferedImage page, Region region, File outputDirFile, int padding) {
    BufferedImage splitImage = null;
    // get the needed part of the page and rotate if needed
    if (region.rotate) {
        BufferedImage srcImage = page.getSubimage(region.left, region.top, region.height, region.width);
        splitImage = new BufferedImage(region.width, region.height, page.getType());
        AffineTransform transform = new AffineTransform();
        transform.rotate(Math.toRadians(90.0));
        transform.translate(0, -region.width);
        AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
        op.filter(srcImage, splitImage);
    } else {
        splitImage = page.getSubimage(region.left, region.top, region.width, region.height);
    }
    // draw the image to a bigger one if padding is needed
    if (padding > 0) {
        BufferedImage paddedImage = new BufferedImage(splitImage.getWidth() + padding * 2, splitImage.getHeight() + padding * 2, page.getType());
        Graphics2D g2 = paddedImage.createGraphics();
        g2.drawImage(splitImage, padding, padding, null);
        g2.dispose();
        return paddedImage;
    } else if (region.originalWidth != region.width || region.originalHeight != region.height) {
        BufferedImage paddedImage = new BufferedImage(region.originalWidth, region.originalHeight, page.getType());
        Graphics2D g2 = paddedImage.createGraphics();
        // g2.drawImage(splitImage, (int)region.offsetX, region.originalHeight - region.height, null);
        g2.drawImage(splitImage, (int) region.offsetX, region.originalHeight - region.height - (int) region.offsetY, null);
        g2.dispose();
        return paddedImage;
    } else {
        return splitImage;
    }
}
Also used : AffineTransform(java.awt.geom.AffineTransform) BufferedImage(java.awt.image.BufferedImage) AffineTransformOp(java.awt.image.AffineTransformOp) Graphics2D(java.awt.Graphics2D)

Aggregations

AffineTransformOp (java.awt.image.AffineTransformOp)48 AffineTransform (java.awt.geom.AffineTransform)31 BufferedImage (java.awt.image.BufferedImage)29 Graphics2D (java.awt.Graphics2D)20 IOException (java.io.IOException)4 Rectangle2D (java.awt.geom.Rectangle2D)3 WritableRaster (java.awt.image.WritableRaster)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Color (java.awt.Color)2 GradientPaint (java.awt.GradientPaint)2 Image (java.awt.Image)2 RenderingHints (java.awt.RenderingHints)2 ByteLookupTable (java.awt.image.ByteLookupTable)2 ConvolveOp (java.awt.image.ConvolveOp)2 LookupOp (java.awt.image.LookupOp)2 LookupTable (java.awt.image.LookupTable)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 File (java.io.File)2 ImageException (cbit.image.ImageException)1 GeometryException (cbit.vcell.geometry.GeometryException)1