Example usage for java.awt.image BufferedImageOp filter

List of usage examples for java.awt.image BufferedImageOp filter

Introduction

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

Prototype

public BufferedImage filter(BufferedImage src, BufferedImage dest);

Source Link

Document

Performs a single-input/single-output operation on a BufferedImage .

Usage

From source file:distribuidos.MyThread.java

private void difuminarTrozo(int x, int y, int w, int h) {
    preSC();/*from   w w w  . j a v a  2s .  c  om*/
    BufferedImage original = null;
    BufferedImage nueva = null;
    try {
        original = ImageIO.read(new File(imagepath));
        nueva = ImageIO.read(new File(newimagepath));
    } catch (IOException ex) {
        Logger.getLogger(MyThread.class.getName()).log(Level.SEVERE, null, ex);
    }
    BufferedImage copia = new BufferedImage(original.getWidth(), original.getHeight(), TYPE_BYTE_INDEXED);
    float[] floats = { 0, 0.125f, 0, 0.125f, 0.5f, 0.125f, 0, 0.125f, 0, };
    BufferedImageOp op = new ConvolveOp(new Kernel(3, 3, floats));
    op.filter(original, copia);

    copia = copia.getSubimage(x, y, w, h);
    Graphics2D g2d = nueva.createGraphics();
    g2d.drawImage(copia, x, y, null);
    //g2d.drawImage(copia, x, y, w, h, null);
    File output = new File(newimagepath);
    try {
        ImageIO.write(nueva, "png", output);
    } catch (IOException ex) {
        Logger.getLogger(MyThread.class.getName()).log(Level.SEVERE, null, ex);
    }
    postSC();
}

From source file:Sampler.java

private void createUI() {
    setFont(new Font("Serif", Font.PLAIN, 12));
    setLayout(new BorderLayout());
    // Set our location to the left of the image frame.
    setSize(200, 350);/*w  ww  .ja  v  a2  s  . c o m*/
    Point pt = mImageFrame.getLocation();
    setLocation(pt.x - getSize().width, pt.y);

    final Checkbox accumulateCheckbox = new Checkbox("Accumulate", false);
    final Label statusLabel = new Label("");

    // Make a sorted list of the operators.
    Enumeration e = mOps.keys();
    Vector names = new Vector();
    while (e.hasMoreElements())
        names.addElement(e.nextElement());
    Collections.sort(names);
    final java.awt.List list = new java.awt.List();
    for (int i = 0; i < names.size(); i++)
        list.add((String) names.elementAt(i));
    add(list, BorderLayout.CENTER);

    // When an item is selected, do the corresponding transformation.
    list.addItemListener(new ItemListener() {
        public void itemStateChanged(ItemEvent ie) {
            if (ie.getStateChange() != ItemEvent.SELECTED)
                return;
            String key = list.getSelectedItem();
            BufferedImageOp op = (BufferedImageOp) mOps.get(key);
            BufferedImage source = mSplitImageComponent.getSecondImage();
            boolean accumulate = accumulateCheckbox.getState();
            if (source == null || accumulate == false)
                source = mSplitImageComponent.getImage();
            String previous = mImageFrame.getTitle() + " + ";
            if (accumulate == false)
                previous = "";
            mImageFrame.setTitle(previous + key);
            statusLabel.setText("Performing " + key + "...");
            list.setEnabled(false);
            accumulateCheckbox.setEnabled(false);
            BufferedImage destination = op.filter(source, null);
            mSplitImageComponent.setSecondImage(destination);
            mSplitImageComponent.setSize(mSplitImageComponent.getPreferredSize());
            mImageFrame.setSize(mImageFrame.getPreferredSize());
            list.setEnabled(true);
            accumulateCheckbox.setEnabled(true);
            statusLabel.setText("Performing " + key + "...done.");
        }
    });

    Button loadButton = new Button("Load...");
    loadButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent ae) {
            FileDialog fd = new FileDialog(Sampler.this);
            fd.show();
            if (fd.getFile() == null)
                return;
            String path = fd.getDirectory() + fd.getFile();
            mSplitImageComponent.setImage(path);
            mSplitImageComponent.setSecondImage(null);
            //            Utilities.sizeContainerToComponent(mImageFrame,
            //               mSplitImageComponent);
            mImageFrame.validate();
            mImageFrame.repaint();
        }
    });

    Panel bottom = new Panel(new GridLayout(2, 1));
    Panel topBottom = new Panel();
    topBottom.add(accumulateCheckbox);
    topBottom.add(loadButton);
    bottom.add(topBottom);
    bottom.add(statusLabel);
    add(bottom, BorderLayout.SOUTH);

    addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            mImageFrame.dispose();
            dispose();
            System.exit(0);
        }
    });
}

From source file:SaveImage.java

public void filterImage() {
    BufferedImageOp op = null;

    if (opIndex == lastOp) {
        return;//w  ww .java 2 s.co m
    }
    lastOp = opIndex;
    switch (opIndex) {

    case 0:
        biFiltered = bi; /* original */
        return;
    case 1: /* low pass filter */
    case 2: /* sharpen */
        float[] data = (opIndex == 1) ? BLUR3x3 : SHARPEN3x3;
        op = new ConvolveOp(new Kernel(3, 3, data), ConvolveOp.EDGE_NO_OP, null);

        break;

    case 3: /* lookup */
        byte lut[] = new byte[256];
        for (int j = 0; j < 256; j++) {
            lut[j] = (byte) (256 - j);
        }
        ByteLookupTable blut = new ByteLookupTable(0, lut);
        op = new LookupOp(blut, null);
        break;
    }

    /*
     * Rather than directly drawing the filtered image to the destination,
     * filter it into a new image first, then that filtered image is ready for
     * writing out or painting.
     */
    biFiltered = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    op.filter(bi, biFiltered);
}

From source file:com.devnexus.ting.core.service.impl.BusinessServiceImpl.java

@Cacheable("sponsors")
@Override/*from w ww .  ja  v a2 s .co m*/
public SponsorList getSponsorListForEvent(Long id, boolean large) {

    final List<Sponsor> sponsors = this.getSponsorsForEvent(id);

    final SponsorList sponsorList = new SponsorList();

    for (Sponsor sponsor : sponsors) {

        FileData imageData = this.getSponsorWithPicture(sponsor.getId()).getLogo();

        final int size;

        if (SponsorLevel.UNOBTAINIUM.equals(sponsor.getSponsorLevel())) {
            size = large ? 360 : 158;
        } else if (SponsorLevel.PLATINUM.equals(sponsor.getSponsorLevel())) {
            size = large ? 360 : 158;
        } else if (SponsorLevel.GOLD.equals(sponsor.getSponsorLevel())) {
            size = large ? 360 : 158;
        } else if (SponsorLevel.SILVER.equals(sponsor.getSponsorLevel())) {
            size = large ? 360 : 158;
        } else if (SponsorLevel.BADGE.equals(sponsor.getSponsorLevel())) {
            size = large ? 360 : 158;
        } else if (SponsorLevel.LANYARD.equals(sponsor.getSponsorLevel())) {
            size = large ? 360 : 158;
        } else if (SponsorLevel.DEV_LOUNGE.equals(sponsor.getSponsorLevel())) {
            size = large ? 360 : 158;
        } else if (SponsorLevel.LANYARD.equals(sponsor.getSponsorLevel())) {
            size = large ? 360 : 158;
        } else if (SponsorLevel.COCKTAIL_HOUR.equals(sponsor.getSponsorLevel())) {
            size = large ? 360 : 158;
        } else if (SponsorLevel.MEDIA_PARTNER.equals(sponsor.getSponsorLevel())) {
            size = large ? 920 : 460;
        } else {
            throw new IllegalStateException("Unsupported SponsorLevel " + sponsor.getSponsorLevel());
        }

        if (imageData != null) {
            ByteArrayInputStream bais = new ByteArrayInputStream(imageData.getFileData());
            BufferedImage image;
            try {
                image = ImageIO.read(bais);

                int height = size;
                int width = height * image.getWidth() / image.getHeight();

                BufferedImageOp resampler = new ResampleOp(width, height, ResampleOp.FILTER_LANCZOS);
                BufferedImage scaled = resampler.filter(image, null);

                final ByteArrayOutputStream out = new ByteArrayOutputStream();
                ImageIO.write(scaled, "PNG", out);

                byte[] bytes = out.toByteArray();

                final String base64bytes = Base64.encodeBase64String(bytes);
                final String src = "data:image/png;base64," + base64bytes;
                sponsorList.addSponsor(sponsor, src);
            } catch (IOException e) {
                LOGGER.error("Error while processing logo for sponsor " + sponsor.getName(), e);
            }
        }
    }

    return sponsorList;
}

From source file:DefaultGraphics2D.java

/**
 * Renders a <code>BufferedImage</code> that is filtered with a
 * {@link BufferedImageOp}. The rendering attributes applied include the
 * <code>Clip</code>, <code>Transform</code> and <code>Composite</code>
 * attributes. This is equivalent to://from w ww .ja  v  a2 s.  com
 * 
 * <pre>
 * img1 = op.filter(img, null);
 * drawImage(img1, new AffineTransform(1f, 0f, 0f, 1f, x, y), null);
 * </pre>
 * 
 * @param img
 *          the <code>BufferedImage</code> to be rendered
 * @param op
 *          the filter to be applied to the image before rendering
 * @param x
 *          the x coordinate in user space where the image is rendered
 * @param y
 *          the y coordinate in user space where the image is rendered
 * @see #transform
 * @see #setTransform
 * @see #setComposite
 * @see #clip
 * @see #setClip(Shape)
 */
public void drawImage(BufferedImage img, BufferedImageOp op, int x, int y) {
    img = op.filter(img, null);
    drawImage(img, x, y, null);
}

From source file:org.apache.fop.render.pcl.PCLGenerator.java

private RenderedImage getMask(RenderedImage img, Dimension targetDim) {
    ColorModel cm = img.getColorModel();
    if (cm.hasAlpha()) {
        BufferedImage alpha = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
        Raster raster = img.getData();
        GraphicsUtil.copyBand(raster, cm.getNumColorComponents(), alpha.getRaster(), 0);

        BufferedImageOp op1 = new LookupOp(new ByteLookupTable(0, THRESHOLD_TABLE), null);
        BufferedImage alphat = op1.filter(alpha, null);

        BufferedImage mask;// ww  w  . java 2s.c  o m
        if (true) {
            mask = new BufferedImage(targetDim.width, targetDim.height, BufferedImage.TYPE_BYTE_BINARY);
        } else {
            byte[] arr = { (byte) 0, (byte) 0xff };
            ColorModel colorModel = new IndexColorModel(1, 2, arr, arr, arr);
            WritableRaster wraster = Raster.createPackedRaster(DataBuffer.TYPE_BYTE, targetDim.width,
                    targetDim.height, 1, 1, null);
            mask = new BufferedImage(colorModel, wraster, false, null);
        }

        Graphics2D g2d = mask.createGraphics();
        try {
            AffineTransform at = new AffineTransform();
            double sx = targetDim.getWidth() / img.getWidth();
            double sy = targetDim.getHeight() / img.getHeight();
            at.scale(sx, sy);
            g2d.drawRenderedImage(alphat, at);
        } finally {
            g2d.dispose();
        }
        /*
        try {
        BatchDiffer.saveAsPNG(alpha, new java.io.File("D:/out-alpha.png"));
        BatchDiffer.saveAsPNG(mask, new java.io.File("D:/out-mask.png"));
        } catch (IOException e) {
        e.printStackTrace();
        }*/
        return mask;
    } else {
        return null;
    }
}

From source file:org.pentaho.reporting.engine.classic.core.modules.output.pageable.pdf.internal.PdfGraphics2D.java

/**
 * @see Graphics2D#drawImage(BufferedImage, BufferedImageOp, int, int)
 *///w  w  w  .  jav a2s . co  m
@Override
public void drawImage(final BufferedImage img, final BufferedImageOp op, final int x, final int y) {
    BufferedImage result = img;
    if (op != null) {
        result = op.createCompatibleDestImage(img, img.getColorModel());
        result = op.filter(img, result);
    }
    drawImage(result, x, y, null);
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage resizeImage(BufferedImage img, double xScale, double yScale, int type) {
    if (img == null) {
        return null;
    }/*  w  w  w .  j av a  2  s  .c o m*/

    if (xScale <= 0 || yScale <= 0) {
        return null;
    }

    int w = img.getWidth();
    int h = img.getHeight();

    int neww = (int) (((double) w) * xScale);
    int newh = (int) (((double) h) * yScale);

    BufferedImage out = new BufferedImage(neww, newh, img.getType());

    AffineTransform tr = new AffineTransform();
    tr.scale(xScale, yScale);
    BufferedImageOp op = new AffineTransformOp(tr, type);
    op.filter(img, out);
    return out;
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage rotateImage(BufferedImage img, double angle, int type, Color fillBgColor) {
    if (img == null) {
        return null;
    }//from   w w  w  .  j  ava 2 s .  c  o m

    if (angle > 360.0 || angle < -360) {
        angle = angle % 360.0;
    }

    if (angle < 0) {
        angle = 360 + angle;
    }

    if (angle == 0.0 || angle == 360.0) {
        return img;
    }

    //System.out.println("angle="+angle);

    int w = img.getWidth();
    int h = img.getHeight();

    /*
    AffineTransform tr = new AffineTransform();
    tr.rotate(theta,w/2,h/2);
    BufferedImageOp op = new AffineTransformOp(tr, type);
    BufferedImage out = op.filter(img,null);
     */
    /*
    AffineTransform tr = new AffineTransform();
    tr.rotate(theta, w/2.0, h/2.0);
    AffineTransform translationTransform = findTranslation(tr, img);
    tr.preConcatenate(translationTransform);
    BufferedImageOp op = new AffineTransformOp(tr, type);
            
    BufferedImage out = op.filter(img,null);
     */
    BufferedImage out = null;
    if (angle == 90.0 || angle == 180.0 || angle == 270.0) {
        switch ((int) angle) {
        case 90:
            out = new BufferedImage(h, w, img.getType());
            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {
                    out.setRGB(h - y - 1, x, img.getRGB(x, y));
                }
            }
            break;
        case 180:
            out = new BufferedImage(w, h, img.getType());
            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {
                    out.setRGB(w - x - 1, h - y - 1, img.getRGB(x, y));
                }
            }
            break;
        case 270:
            out = new BufferedImage(h, w, img.getType());
            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {
                    out.setRGB(y, w - x - 1, img.getRGB(x, y));
                }
            }
            break;
        }
    } else {
        double theta = angle * Math.PI / 180.0;
        int neww = w, newh = h;
        double dx = 0.0, dy = 0.0;
        double s = Math.sin(theta);
        double c = Math.cos(theta);
        if (angle > 0.0 && angle < 90.0) {
            neww = (int) Math.round(((double) w) * c + ((double) h) * s);
            newh = (int) Math.round(((double) w) * s + ((double) h) * c);
            dx = ((double) h) * s;
            dy = 0.0;
        } else if (angle > 90.0 && angle < 180.0) {
            neww = (int) Math.round(-((double) w) * c + ((double) h) * s);
            newh = (int) Math.round(((double) w) * s - ((double) h) * c);
            dx = -((double) w) * c + ((double) h) * s;
            dy = -((double) h) * c;
        } else if (angle > 180.0 && angle < 270.0) {
            neww = (int) Math.round(-((double) w) * c - ((double) h) * s);
            newh = (int) Math.round(-((double) w) * s - ((double) h) * c);
            dx = -((double) w) * c;
            dy = -((double) w) * s - ((double) h) * c;
        } else if (angle > 270.0 && angle < 360.0) {
            neww = (int) Math.round(((double) w) * c - ((double) h) * s);
            newh = (int) Math.round(-((double) w) * s + ((double) h) * c);
            dx = 0.0;
            dy = -((double) w) * s;
        }

        AffineTransform tr = new AffineTransform();
        tr.translate(dx, dy);
        tr.rotate(theta);
        BufferedImageOp op = new AffineTransformOp(tr, type);
        out = new BufferedImage(neww, newh, img.getType());
        Graphics2D g2d = (Graphics2D) out.getGraphics();
        Rectangle clear = new Rectangle(0, 0, out.getWidth(), out.getHeight());
        g2d.setPaint(fillBgColor);
        g2d.fill(clear);
        op.filter(img, out);
    }
    return out;
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage translateImage(BufferedImage img, double dx, double dy, int type,
        Color fillBgColor) {/*from  w w w  .  j a  v  a2s . c om*/
    if (img == null) {
        return null;
    }

    int w = img.getWidth();
    int h = img.getHeight();

    BufferedImage out = null;
    AffineTransform tr = new AffineTransform();
    tr.translate(dx, dy);
    BufferedImageOp op = new AffineTransformOp(tr, type);
    out = new BufferedImage(w, h, img.getType());
    Graphics2D g2d = (Graphics2D) out.getGraphics();
    Rectangle clear = new Rectangle(0, 0, out.getWidth(), out.getHeight());
    g2d.setPaint(fillBgColor);
    g2d.fill(clear);
    op.filter(img, out);
    return out;
}