Example usage for java.awt.image AffineTransformOp TYPE_NEAREST_NEIGHBOR

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

Introduction

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

Prototype

int TYPE_NEAREST_NEIGHBOR

To view the source code for java.awt.image AffineTransformOp TYPE_NEAREST_NEIGHBOR.

Click Source Link

Document

Nearest-neighbor interpolation type.

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_BYTE_INDEXED);

    AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
    tx.translate(-bufferedImage.getWidth(null), 0);
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    bufferedImage = op.filter(bufferedImage, null);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_BYTE_INDEXED);

    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);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BufferedImage bufferedImage = new BufferedImage(200, 200, BufferedImage.TYPE_BYTE_INDEXED);

    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);

}

From source file:ImageFlip.java

public void paint(Graphics g) {
    Image myImage = new ImageIcon("yourImage.jpg").getImage();
    BufferedImage bufferedImage = new BufferedImage(myImage.getWidth(null), myImage.getHeight(null),
            BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = (Graphics2D) g;

    Graphics gb = bufferedImage.getGraphics();
    gb.drawImage(myImage, 0, 0, null);//from   w w w .  j  a  va  2s  .com
    gb.dispose();

    AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
    tx.translate(-myImage.getWidth(null), 0);
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    bufferedImage = op.filter(bufferedImage, null);

    g2d.drawImage(myImage, 10, 10, null);
    g2d.drawImage(bufferedImage, null, 300, 10);
}

From source file:de.mfo.jsurf.Main.java

public static BufferedImage flipV(BufferedImage bi) {
    AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
    tx.translate(0, -bi.getHeight(null));
    AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
    return op.filter(bi, null);
}

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   www  .  j  a v  a 2s  . 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: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.//from www .  j ava  2s  .c  om
 * 
 * @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:ImageOps.java

public void paint(Graphics g) {
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    int w = getSize().width;
    int h = getSize().height;

    g2.setColor(Color.black);//from w ww  . jav  a 2s  .c  o m
    float[][] data = { { 0.1f, 0.1f, 0.1f, // low-pass filter
            0.1f, 0.2f, 0.1f, 0.1f, 0.1f, 0.1f }, SHARPEN3x3_3 };

    String theDesc[] = { "Convolve LowPass", "Convolve Sharpen", "LookupOp", "RescaleOp" };
    for (int i = 0; i < bi.length; i++) {
        int iw = bi[i].getWidth(this);
        int ih = bi[i].getHeight(this);
        int x = 0, y = 0;

        AffineTransform at = new AffineTransform();
        at.scale((w - 14) / 2.0 / iw, (h - 34) / 2.0 / ih);

        BufferedImageOp biop = null;
        BufferedImage bimg = new BufferedImage(iw, ih, BufferedImage.TYPE_INT_RGB);

        switch (i) {
        case 0:
        case 1:
            x = i == 0 ? 5 : w / 2 + 3;
            y = 15;
            Kernel kernel = new Kernel(3, 3, data[i]);
            ConvolveOp cop = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
            cop.filter(bi[i], bimg);
            biop = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
            break;
        case 2:
            x = 5;
            y = h / 2 + 15;
            byte chlut[] = new byte[256];
            for (int j = 0; j < 200; j++)
                chlut[j] = (byte) (256 - j);
            ByteLookupTable blut = new ByteLookupTable(0, chlut);
            LookupOp lop = new LookupOp(blut, null);
            lop.filter(bi[i], bimg);
            biop = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
            break;
        case 3:
            x = w / 2 + 3;
            y = h / 2 + 15;
            RescaleOp rop = new RescaleOp(1.1f, 20.0f, null);
            rop.filter(bi[i], bimg);
            biop = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        }
        g2.drawImage(bimg, biop, x, y);
        TextLayout tl = new TextLayout(theDesc[i], g2.getFont(), g2.getFontRenderContext());
        tl.draw(g2, (float) x, (float) y - 4);
    }
}

From source file:com.mucommander.ui.viewer.image.ImageViewer.java

private synchronized void zoom(double factor) {
    setFrameCursor(CURSOR_WAIT);/*from  www.  j  a  v  a 2 s.  c om*/

    final int srcWidth = image.getWidth(null);
    final int srcHeight = image.getHeight(null);
    final int scaledWidth = (int) (srcWidth * factor);
    final int scaledHeight = (int) (srcHeight * factor);

    if (factor != 1.0) {
        AbstractFile file = filesInDirectory.get(indexInDirectory);
        if ("svg".equalsIgnoreCase(file.getExtension())) {
            try {
                this.scaledImage = transcodeSVGDocument(file, scaledWidth, scaledHeight);
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else {
            this.scaledImage = new BufferedImage(scaledWidth, scaledHeight, BufferedImage.TYPE_INT_ARGB);
            AffineTransform at = new AffineTransform();
            at.scale(factor, factor);
            AffineTransformOp scaleOp = new AffineTransformOp(at, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
            this.scaledImage = scaleOp.filter(this.image, this.scaledImage);
        }
    } else {
        this.scaledImage = image;
    }

    statusBar.setZoom(factor);
    checkZoom();
    setFrameCursor(CURSOR_DEFAULT);
}