Example usage for java.awt.image AffineTransformOp AffineTransformOp

List of usage examples for java.awt.image AffineTransformOp AffineTransformOp

Introduction

In this page you can find the example usage for java.awt.image AffineTransformOp AffineTransformOp.

Prototype

public AffineTransformOp(AffineTransform xform, int interpolationType) 

Source Link

Document

Constructs an AffineTransformOp given an affine transform and the interpolation type.

Usage

From source file:de.mfo.jsurf.grid.RotationGrid.java

public static BufferedImage renderAnimGrid(int xAngleMin, int xAngleMax, int xSteps, int yAngleMin,
        int yAngleMax, int ySteps) {
    BufferedImage grid = new BufferedImage(ySteps * size, xSteps * size, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = (Graphics2D) grid.getGraphics();
    for (int x = 0; x < xSteps; ++x) {
        double xAngle = xAngleMin + (xAngleMax - xAngleMin) * (xSteps == 1 ? 0.5 : (x / (double) (xSteps - 1)));
        Matrix4d matRotX = new Matrix4d();
        matRotX.setIdentity();//from w  w w. j  a va2  s  .  com
        matRotX.rotX(Math.toRadians(xAngle));
        for (int y = 0; y < ySteps; ++y) {
            double yAngle = yAngleMin
                    + (yAngleMax - yAngleMin) * (ySteps == 1 ? 0.5 : (y / (double) (ySteps - 1)));
            Matrix4d matRotY = new Matrix4d();
            matRotY.setIdentity();
            matRotY.rotY(Math.toRadians(yAngle));
            additional_rotation.mul(matRotY, matRotX);
            BufferedImage bi = createBufferedImageFromRGB(draw(size, size, aam, aap));
            g2.drawImage(bi,
                    new AffineTransformOp(new AffineTransform(), AffineTransformOp.TYPE_NEAREST_NEIGHBOR),
                    (ySteps - 1 - y) * size, x * size);
        }
    }
    return grid;
}

From source file:com.jaeksoft.searchlib.util.ImageUtils.java

public static BufferedImage rotate(BufferedImage image, float degree) throws InterruptedException {

    Rotation rot = null;//from   w ww .j av a 2 s. c o  m
    AffineTransformOp[] xform = null;

    switch ((int) degree) {
    case 90:
        rot = Rotation.CW_90;
        break;
    case 180:
        rot = Rotation.CW_180;
        break;
    case 270:
        rot = Rotation.CW_270;
        break;
    default:
        xform = new AffineTransformOp[1];
        xform[0] = new AffineTransformOp(AffineTransform.getRotateInstance(Math.toRadians(degree)),
                AffineTransformOp.TYPE_BICUBIC);
        break;
    }
    if (rot != null)
        return Scalr.rotate(image, rot, xform);
    return Scalr.apply(image, xform);
}

From source file:RotateImage45Degrees.java

public RotateImage45Degrees(String imageFile) {
    addNotify();/*from   w  ww. j  ava2 s . c o m*/
    frameInsets = getInsets();
    inputImage = Toolkit.getDefaultToolkit().getImage(imageFile);

    MediaTracker mt = new MediaTracker(this);
    mt.addImage(inputImage, 0);
    try {
        mt.waitForID(0);
    } catch (InterruptedException ie) {
    }

    sourceBI = new BufferedImage(inputImage.getWidth(null), inputImage.getHeight(null),
            BufferedImage.TYPE_INT_ARGB);

    Graphics2D g = (Graphics2D) sourceBI.getGraphics();
    g.drawImage(inputImage, 0, 0, null);

    AffineTransform at = new AffineTransform();

    // scale image
    at.scale(2.0, 2.0);

    // rotate 45 degrees around image center
    at.rotate(45.0 * Math.PI / 180.0, sourceBI.getWidth() / 2.0, sourceBI.getHeight() / 2.0);

    /*
     * translate to make sure the rotation doesn't cut off any image data
     */
    AffineTransform translationTransform;
    translationTransform = findTranslation(at, sourceBI);
    at.preConcatenate(translationTransform);

    // instantiate and apply affine transformation filter
    BufferedImageOp bio;
    bio = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);

    destinationBI = bio.filter(sourceBI, null);

    int frameInsetsHorizontal = frameInsets.right + frameInsets.left;
    int frameInsetsVertical = frameInsets.top + frameInsets.bottom;
    setSize(destinationBI.getWidth() + frameInsetsHorizontal, destinationBI.getHeight() + frameInsetsVertical);
    show();
}

From source file:Sampler.java

private void createTransformations() {
    AffineTransform at;/*from  ww w.  j a  v  a2  s .  c  o m*/
    at = AffineTransform.getRotateInstance(Math.PI / 6, 0, 285);
    mOps.put("Rotate nearest neighbor", new AffineTransformOp(at, null));

    RenderingHints rh = new RenderingHints(RenderingHints.KEY_INTERPOLATION,
            RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    mOps.put("Rotate bilinear", new AffineTransformOp(at, rh));

    at = AffineTransform.getScaleInstance(.5, .5);
    mOps.put("Scale .5, .5", new AffineTransformOp(at, null));

    at = AffineTransform.getRotateInstance(Math.PI / 6);
    mOps.put("Rotate bilinear (origin)", new AffineTransformOp(at, rh));
}

From source file:org.apache.hadoop.chukwa.hicc.ImageSlicer.java

public BufferedImage prepare(String filename) {
    try {//www  .j  a  v  a2s. c o  m
        src = ImageIO.read(new File(filename));
    } catch (IOException e) {
        log.error("Image file does not exist:" + filename + ", can not render image.");
    }
    XYData fullSize = new XYData(1, 1);
    while (fullSize.getX() < src.getWidth() || fullSize.getY() < src.getHeight()) {
        fullSize.set(fullSize.getX() * 2, fullSize.getY() * 2);
    }
    float scaleX = (float) fullSize.getX() / src.getWidth();
    float scaleY = (float) fullSize.getY() / src.getHeight();
    log.info("Image size: (" + src.getWidth() + "," + src.getHeight() + ")");
    log.info("Scale size: (" + scaleX + "," + scaleY + ")");

    AffineTransform at = AffineTransform.getScaleInstance(scaleX, scaleY);

    //       AffineTransform.getScaleInstance((fullSize.getX()-src.getWidth())/2,(fullSize.getY()-src.getHeight())/2);
    AffineTransformOp op = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
    BufferedImage dest = op.filter(src, null);
    return dest;
}

From source file:org.github.jipsg.sanselan.BaseSanselanTest.java

/**
 * Some quick and dirty image scaling - please note that for best performance
 * and quality you should use image rescaling libraries.
 *///ww w  .j  a va 2 s  .  c  om
@Override
public BufferedImage resample(BufferedImage bufferedImage, int width, int height) {

    Dimension imageDimension = new Dimension(bufferedImage.getWidth(), bufferedImage.getHeight());
    Dimension boundaryDimension = new Dimension(width, height);
    Dimension scaledDimension = BufferedImageUtils.getScaledDimension(imageDimension, boundaryDimension);

    double scaleX = scaledDimension.getWidth() / bufferedImage.getWidth();
    double scaleY = scaledDimension.getHeight() / bufferedImage.getHeight();

    AffineTransform scaleTransform = AffineTransform.getScaleInstance(scaleX, scaleY);
    AffineTransformOp biLinearScaleOp = new AffineTransformOp(scaleTransform, AffineTransformOp.TYPE_BILINEAR);

    return biLinearScaleOp.filter(bufferedImage,
            new BufferedImage(scaledDimension.width, scaledDimension.height, bufferedImage.getType()));
}

From source file:se.trixon.almond.GraphicsHelper.java

public static BufferedImage flipBufferedImageX(BufferedImage bufferedImage) {
    AffineTransform affineTransform = AffineTransform.getScaleInstance(-1, 1);
    affineTransform.translate(-bufferedImage.getWidth(null), 0);
    AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform,
            AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    return affineTransformOp.filter(bufferedImage, null);
}

From source file:de.mfo.jsurf.grid.RotationGrid.java

public static void saveToPNG(OutputStream os, BufferedImage bufferedImage) throws java.io.IOException {
    AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
    tx.translate(0, -bufferedImage.getHeight(null));
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    bufferedImage = op.filter(bufferedImage, null);
    javax.imageio.ImageIO.write(bufferedImage, "png", os);
}

From source file:com.googlecode.jchav.chart.Chart.java

/**
 * Creates a PNG graphic for the given data as a thumbnail image.
 *
 * @param out the stream to write the PNG to.  The caller is responsible
 *  for closing the stream.//w w  w.  j  ava  2 s . c o m
 * 
 * @throws IOException if there was a problem creating the chart.
 */
public void writeThumbnail(final OutputStream out) throws IOException {

    // Set up the transfomration:
    final AffineTransform xform = new AffineTransform();
    xform.scale(thumbnailScale, thumbnailScale);

    // Thanks to the almanac for this one:
    // http://javaalmanac.com/egs/java.awt.image/CreateTxImage.html?l=rel
    final AffineTransformOp op = new AffineTransformOp(xform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);

    // The thumbnail does not need so much chart chrome: you can't
    // read the axis and the title is given in the HTML, so removing
    // these elements means there's more space in the thumbnail for the data
    boolean thumbChrome = false;

    if (false == thumbChrome) {
        chart.setTitle((String) null);
        chart.clearSubtitles();
        chart.removeLegend();

        // Removing the axis completly looks just weird, so we just
        // remove the labels:

        chart.getCategoryPlot().getRangeAxis().setLabel(null);
        chart.getCategoryPlot().getRangeAxis().setTickLabelsVisible(true);
        chart.getCategoryPlot().getRangeAxis().setTickMarksVisible(true);
        chart.getCategoryPlot().getRangeAxis().setAxisLineVisible(true);

        //  To show up at a small scale, we need a good sized axis stroke:
        Stroke stroke = new BasicStroke(2f);
        chart.getCategoryPlot().getRangeAxis().setAxisLineStroke(stroke);
        chart.getCategoryPlot().getRangeAxis().setTickMarkStroke(stroke);

        chart.getCategoryPlot().getDomainAxis().setLabel(null);
        chart.getCategoryPlot().getDomainAxis().setTickLabelsVisible(false);
        chart.getCategoryPlot().getDomainAxis().setAxisLineVisible(true);
        chart.getCategoryPlot().getDomainAxis().setAxisLineStroke(stroke);
    }

    final BufferedImage fullsize = chart.createBufferedImage(width, height);

    Graphics2D g = fullsize.createGraphics();
    for (Decorator decorator : thumbnailDecorators) {
        decorator.decorate(g, this);
    }

    final BufferedImage thumbnail = op.filter(fullsize, null /*null means create the image for us*/);

    ChartUtilities.writeBufferedImageAsPNG(out, thumbnail);

}

From source file:se.trixon.almond.GraphicsHelper.java

public static BufferedImage flipBufferedImageY(BufferedImage bufferedImage) {
    AffineTransform affineTransform = AffineTransform.getScaleInstance(1, -1);
    affineTransform.translate(-bufferedImage.getWidth(null), 0);
    AffineTransformOp affineTransformOp = new AffineTransformOp(affineTransform,
            AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    return affineTransformOp.filter(bufferedImage, null);
}