Example usage for java.awt AlphaComposite Src

List of usage examples for java.awt AlphaComposite Src

Introduction

In this page you can find the example usage for java.awt AlphaComposite Src.

Prototype

AlphaComposite Src

To view the source code for java.awt AlphaComposite Src.

Click Source Link

Document

AlphaComposite object that implements the opaque SRC rule with an alpha of 1.0f.

Usage

From source file:com.zacwolf.commons.email.Email.java

public static BufferedImage makeRoundedFooter(int width, int cornerRadius, Color bgcolor, Color border)
        throws Exception {
    int height = (cornerRadius * 2) + 10;
    BufferedImage output = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = output.createGraphics();
    g2.setComposite(AlphaComposite.Src);
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g2.setColor(bgcolor);//from  w ww  . j  a v a  2 s . co m
    g2.fillRoundRect(0, 0, width, height - 1, cornerRadius, cornerRadius);
    g2.setComposite(AlphaComposite.SrcOver);
    if (border != null) {
        g2.setColor(border);
        g2.drawRoundRect(0, 0, width - 1, height - 2, cornerRadius, cornerRadius);
    }
    g2.dispose();
    Rectangle clip = createClip(output, new Dimension(width, cornerRadius), 0, height - cornerRadius - 1);
    return output.getSubimage(clip.x, clip.y, clip.width, clip.height);
}

From source file:org.apache.pdfbox.pdmodel.graphics.xobject.PDPixelMap.java

private void createImageStream(PDDocument doc, BufferedImage bi) throws IOException {
    BufferedImage alphaImage = null;
    BufferedImage rgbImage = null;
    int width = bi.getWidth();
    int height = bi.getHeight();
    if (bi.getColorModel().hasAlpha()) {
        // extract the alpha information
        WritableRaster alphaRaster = bi.getAlphaRaster();
        ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), false, false,
                Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
        alphaImage = new BufferedImage(cm, alphaRaster, false, null);
        // create a RGB image without alpha
        rgbImage = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
        Graphics2D g = rgbImage.createGraphics();
        g.setComposite(AlphaComposite.Src);
        g.drawImage(bi, 0, 0, null);//ww  w. j a  v  a2 s  .  c o m
    } else {
        rgbImage = bi;
    }
    java.io.OutputStream os = null;
    try {
        int numberOfComponents = rgbImage.getColorModel().getNumComponents();
        if (numberOfComponents == 3) {
            setColorSpace(PDDeviceRGB.INSTANCE);
        } else {
            if (numberOfComponents == 1) {
                setColorSpace(new PDDeviceGray());
            } else {
                throw new IllegalStateException();
            }
        }
        byte[] outData = new byte[width * height * numberOfComponents];
        rgbImage.getData().getDataElements(0, 0, width, height, outData);
        // add FlateDecode compression
        getPDStream().addCompression();
        os = getCOSStream().createUnfilteredStream();
        os.write(outData);

        COSDictionary dic = getCOSStream();
        dic.setItem(COSName.FILTER, COSName.FLATE_DECODE);
        dic.setItem(COSName.SUBTYPE, COSName.IMAGE);
        dic.setItem(COSName.TYPE, COSName.XOBJECT);
        if (alphaImage != null) {
            PDPixelMap smask = new PDPixelMap(doc, alphaImage);
            dic.setItem(COSName.SMASK, smask);
        }
        setBitsPerComponent(8);
        setHeight(height);
        setWidth(width);
    } finally {
        os.close();
    }
}

From source file:org.elasticwarehouse.core.parsers.FileTools.java

private static BufferedImage resizeImageWithHint(BufferedImage originalImage, int type, int IMG_WIDTH,
        int IMG_HEIGHT) {

    BufferedImage resizedImage = new BufferedImage(IMG_WIDTH, IMG_HEIGHT, type);
    Graphics2D g = resizedImage.createGraphics();
    g.drawImage(originalImage, 0, 0, IMG_WIDTH, IMG_HEIGHT, null);
    g.dispose();/*from   w w w . j  a  v  a2  s.c o  m*/
    g.setComposite(AlphaComposite.Src);

    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    return resizedImage;
}

From source file:org.jahia.services.image.AbstractJava2DImageService.java

public boolean rotateImage(Image image, File outputFile, boolean clockwise) throws IOException {
    BufferedImage originalImage = ((BufferImage) image).getOriginalImage();

    BufferedImage dest = getDestImage(originalImage.getHeight(), originalImage.getWidth(), originalImage);
    // Paint source image into the destination, scaling as needed
    Graphics2D graphics2D = getGraphics2D(dest, OperationType.ROTATE);

    double angle = Math.toRadians(clockwise ? 90 : -90);
    double sin = Math.abs(Math.sin(angle)), cos = Math.abs(Math.cos(angle));
    int w = originalImage.getWidth(), h = originalImage.getHeight();
    int neww = (int) Math.floor(w * cos + h * sin), newh = (int) Math.floor(h * cos + w * sin);
    graphics2D.translate((neww - w) / 2, (newh - h) / 2);
    graphics2D.rotate(angle, w / (double) 2, h / (double) 2);
    graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
    if (originalImage.getColorModel() instanceof IndexColorModel) {
        graphics2D.drawImage(originalImage, 0, 0, graphics2D.getBackground(), null);
    } else {/*  ww w . j a  v a  2  s  .  co  m*/
        graphics2D.drawImage(originalImage, 0, 0, null);
    }

    // Save destination image
    saveImageToFile(dest, ((BufferImage) image).getMimeType(), outputFile);
    return true;
}

From source file:org.jas.gui.ImagePanel.java

@Override
protected void paintComponent(Graphics g) {
    if (portrait == null) {
        super.paintComponent(g);
        return;/* w  ww  .  j av a2 s . c om*/
    }
    try {
        // Create a translucent intermediate image in which we can perform the soft clipping
        GraphicsConfiguration gc = ((Graphics2D) g).getDeviceConfiguration();
        BufferedImage intermediateBufferedImage = gc.createCompatibleImage(getWidth(), getHeight(),
                Transparency.TRANSLUCENT);
        Graphics2D bufferGraphics = intermediateBufferedImage.createGraphics();

        // Clear the image so all pixels have zero alpha
        bufferGraphics.setComposite(AlphaComposite.Clear);
        bufferGraphics.fillRect(0, 0, getWidth(), getHeight());

        // Render our clip shape into the image. Shape on where to paint
        bufferGraphics.setComposite(AlphaComposite.Src);
        bufferGraphics.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        bufferGraphics.setColor(Color.WHITE);
        bufferGraphics.fillRoundRect(0, 0, getWidth(), getHeight(), (int) (getWidth() * arcWidth),
                (int) (getHeight() * arcHeight));

        // SrcAtop uses the alpha value as a coverage value for each pixel stored in the
        // destination shape. For the areas outside our clip shape, the destination
        // alpha will be zero, so nothing is rendered in those areas. For
        // the areas inside our clip shape, the destination alpha will be fully
        // opaque.
        bufferGraphics.setComposite(AlphaComposite.SrcAtop);
        bufferGraphics.drawImage(portrait, 0, 0, getWidth(), getHeight(), null);
        bufferGraphics.dispose();

        // Copy our intermediate image to the screen
        g.drawImage(intermediateBufferedImage, 0, 0, null);

    } catch (Exception e) {
        log.warn("Error: Creating Renderings", e);
    }
}

From source file:org.jas.util.ImageUtils.java

public Image resize(Image image, int width, int height) {
    BufferedImage bufferedImage = (BufferedImage) image;
    int type = bufferedImage.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : bufferedImage.getType();
    BufferedImage resizedImage = new BufferedImage(width, height, type);
    Graphics2D g = resizedImage.createGraphics();
    g.setComposite(AlphaComposite.Src);

    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);

    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    g.drawImage(image, 0, 0, width, height, null);
    g.dispose();/*from  w  w  w .  ja va 2s  .  c  om*/
    return resizedImage;
}

From source file:photodb.cdi.ImageManager.java

private BufferedImage createResizedCopy(final Image originalImage, final int scaledWidth,
        final int scaledHeight, final boolean preserveAlpha) {
    final int imageType = preserveAlpha ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
    final BufferedImage scaledBI = new BufferedImage(scaledWidth, scaledHeight, imageType);
    final Graphics2D g = scaledBI.createGraphics();
    if (preserveAlpha) {
        g.setComposite(AlphaComposite.Src);
    }//w  w  w. jav  a 2 s .  c o m
    g.drawImage(originalImage, 0, 0, scaledWidth, scaledHeight, null);
    g.dispose();
    return scaledBI;
}

From source file:ro.nextreports.engine.exporter.ResultExporter.java

protected byte[] getScaledImage(InputStream is, String name, int width, int height) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    try {/*  w  w w.  ja va2  s . c  o  m*/
        BufferedImage img = ImageIO.read(is);

        if ((img.getWidth() == width) && (img.getHeight() == height)) {
            // original width and height                
            ImageIO.write(img, "png", baos);
        } else {
            int type = img.getType() == 0 ? BufferedImage.TYPE_INT_ARGB : img.getType();
            BufferedImage scaledImg = new BufferedImage(width, height, type);
            Graphics2D gScaledImg = scaledImg.createGraphics();

            gScaledImg.setComposite(AlphaComposite.Src);
            gScaledImg.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                    RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            gScaledImg.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

            gScaledImg.drawImage(img, 0, 0, width, height, null);

            ImageIO.write(scaledImg, "png", baos);
        }
    } catch (IOException ex) {
        ex.printStackTrace();
        LOG.error(ex.getMessage(), ex);
        throw new IOException("Image '" + name + "' could not be scaled.");
    } finally {
        is.close();
    }
    return baos.toByteArray();
}

From source file:se.dibbler.backend.generics.DibblerImageUtil.java

private static Response<BufferedImage> resizeWithHint(BufferedImage originalImage, int type, int height,
        int width) {
    try {/*from w  w  w.  j  a  va2 s .c  o m*/
        BufferedImage resizedImage = new BufferedImage(width, height, type);
        Graphics2D g = resizedImage.createGraphics();
        g.drawImage(originalImage, 0, 0, width, height, null);
        g.dispose();
        g.setComposite(AlphaComposite.Src);

        g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

        return Response.success(resizedImage);
    } catch (Exception e) {
        LOG.error("[ ERROR when resizing file ] [ MESSAGE : {}]", e.getMessage());
        return Response.error(GenericError.FILE_HANDLING);
    }
}

From source file:tufts.vue.LWComponent.java

/**
 * Useful for drawing drag images into an existing graphics buffer, or drawing exportable images.
 *
 * @param alpha 0.0 (invisible) to 1.0 (no alpha -- completely opaque)
 * @param maxSize max dimensions for image. May be null.  Image may be smaller than maxSize.
 * @param fillColor -- if non-null, will be rendered as background for image.
 * @param zoomRequest -- desired zoom; ignored if maxSize is non-null
 * also set, background fill will have transparency of alpha^3 to enhance contrast.
 *//*from  w ww.j av a2  s  . c  o  m*/

public void drawImage(Graphics2D g, double alpha, Dimension maxSize, Color fillColor, double zoomRequest) {
    //if (DEBUG.IMAGE) out("drawImage; size " + maxSize);

    final boolean drawBorder = false;// this instanceof LWMap; // hack for dragged images of LWMaps

    final Rectangle2D.Float bounds = getImageBounds();
    final Rectangle clip = g.getClipBounds();
    final Size fillSize = new Size(bounds);
    final double zoom = computeZoomAndSize(bounds, maxSize, zoomRequest, fillSize);

    if (DEBUG.IMAGE)
        out(TERM_GREEN + "drawImage:" + "\n\t   mapBounds: " + fmt(bounds) + "\n\t        fill: " + fillColor
                + "\n\t     maxSize: " + maxSize + "\n\t zoomRequest: " + zoomRequest + "\n\t     fitZoom: "
                + zoom + "\n\t    fillSize: " + fillSize + "\n\t          gc: " + g + "\n\t        clip: "
                + fmt(clip) + "\n\t       alpha: " + alpha + TERM_CLEAR);

    final int width = fillSize.pixelWidth();
    final int height = fillSize.pixelHeight();

    final DrawContext dc = new DrawContext(g, this);

    dc.setInteractive(false);

    if (alpha == OPAQUE) {
        dc.setPrintQuality();
    } else {
        // if alpha, assume drag image (todo: better specified as an argument)
        dc.setDraftQuality();
    }

    dc.setBackgroundFill(getRenderFillColor(null)); // sure we want null here?
    dc.setClipOptimized(false); // always draw all children -- don't bother to check bounds
    if (DEBUG.IMAGE)
        out(TERM_GREEN + "drawImage: " + dc + TERM_CLEAR);

    if (fillColor != null) {
        //             if (false && alpha != OPAQUE) {
        //                 Color c = fillColor;
        //                 // if we have an alpha and a fill, amplify the alpha on the background fill
        //                 // by changing the fill to one that has alpha*alpha, for a total of
        //                 // alpha*alpha*alpha given our GC already has an alpha set.
        //                 fillColor = new Color(c.getRed(), c.getGreen(), c.getBlue(), (int) (alpha*alpha*255+0.5));
        //             }
        if (alpha != OPAQUE)
            dc.setAlpha(alpha, AlphaComposite.SRC); // erase any underlying in cache
        if (DEBUG.IMAGE)
            out("drawImage: fill=" + fillColor);
        g.setColor(fillColor);
        g.fillRect(0, 0, width, height);
    } else { //if (alpha != OPAQUE) {
        // we didn't have a fill, but we have an alpha: make sure any cached data is cleared
        // todo?: if fill is null, we need to clear as well -- it means we have implied alpha on any non-drawn bits
        // TODO: if this is a selection drag, we usually want to fill with the map color (or ideally, the color
        // of the common parent, e.g., a slide, if there's one common parent)
        dc.g.setComposite(AlphaComposite.Clear);
        g.fillRect(0, 0, width, height);
    }

    //if (alpha != OPAQUE)
    dc.setAlpha(alpha, AlphaComposite.SRC);

    if (DEBUG.IMAGE && DEBUG.META) {
        // Fill the entire imageable area
        g.setColor(Color.green);
        g.fillRect(0, 0, Short.MAX_VALUE, Short.MAX_VALUE);
    }

    final AffineTransform rawTransform = g.getTransform();

    if (zoom != 1.0)
        dc.g.scale(zoom, zoom);

    // translate so that the upper left corner of the map region
    // we're drawing is at 0,0 on the underlying image

    g.translate(-bounds.getX(), -bounds.getY());

    // GC *must* have a bounds set or we get NPE's in JComponent (textBox) rendering
    dc.setMasterClip(bounds);

    if (DEBUG.IMAGE && DEBUG.META) {
        // fill the clipped area so we can check our clip bounds
        dc.g.setColor(Color.red);
        dc.g.fillRect(-Short.MAX_VALUE / 2, -Short.MAX_VALUE / 2, // larger values than this can blow out internal GC code and we get nothing
                Short.MAX_VALUE, Short.MAX_VALUE);
    }

    if (this instanceof LWImage) {
        // for some reason, raw images don't seem to want to draw unless we fill first
        dc.g.setColor(Color.white);
        dc.g.fill(bounds);
    }

    // render to the image through the DrawContext/GC pointing to it
    draw(dc);

    if (drawBorder) {
        g.setTransform(rawTransform);
        //g.setColor(Color.red);
        //g.fillRect(0,0, Short.MAX_VALUE, Short.MAX_VALUE);
        if (DEBUG.IMAGE) {
            g.setColor(Color.black);
            dc.setAntiAlias(false);
        } else
            g.setColor(Color.darkGray);
        g.drawRect(0, 0, width - 1, height - 1);
    }

    if (DEBUG.IMAGE)
        out(TERM_GREEN + "drawImage: completed\n" + TERM_CLEAR);

}