Example usage for java.awt Transparency OPAQUE

List of usage examples for java.awt Transparency OPAQUE

Introduction

In this page you can find the example usage for java.awt Transparency OPAQUE.

Prototype

int OPAQUE

To view the source code for java.awt Transparency OPAQUE.

Click Source Link

Document

Represents image data that is guaranteed to be completely opaque, meaning that all pixels have an alpha value of 1.0.

Usage

From source file:imageLines.ImageHelpers.java

public static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean higherQuality) {
    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = (BufferedImage) img;
    int w, h;//  www .ja  va2  s  . c o m
    if (higherQuality) {
        // Use multi-step technique: start with original size, then
        // scale down in multiple passes with drawImage()
        // until the target size is reached
        w = img.getWidth();
        h = img.getHeight();
    } else {
        // Use one-step technique: scale directly from original
        // size to target size with a single drawImage() call
        w = targetWidth;
        h = targetHeight;
    }

    do {
        if (higherQuality && w > targetWidth) {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
        }

        if (higherQuality && h > targetHeight) {
            h /= 2;
            if (h < targetHeight) {
                h = targetHeight;
            }
        }

        BufferedImage tmp = new BufferedImage(w, h, type);
        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();

        ret = tmp;
    } while (w != targetWidth || h != targetHeight);

    return ret;
}

From source file:ConvertUtil.java

/**
 * Converts the source image to 4-bit colour using the given colour map. No
 * transparency./*from ww  w .  j ava 2 s. co  m*/
 * 
 * @param src
 *            the source image to convert
 * @param cmap
 *            the colour map, which should contain no more than 16 entries
 *            The entries are in the form RRGGBB (hex).
 * @return a copy of the source image with a 4-bit colour depth, with the
 *         custom colour pallette
 */
public static BufferedImage convert4(BufferedImage src, int[] cmap) {
    IndexColorModel icm = new IndexColorModel(4, cmap.length, cmap, 0, false, Transparency.OPAQUE,
            DataBuffer.TYPE_BYTE);
    BufferedImage dest = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_BYTE_BINARY,
            icm);
    ColorConvertOp cco = new ColorConvertOp(src.getColorModel().getColorSpace(),
            dest.getColorModel().getColorSpace(), null);
    cco.filter(src, dest);

    return dest;
}

From source file:com.fun.util.TesseractUtil.java

/**
 * //from ww  w.java2s  .  c  o  m
 *
 * @param imageFile
 * @param times
 * @param targetFile
 * @throws IOException
 */
private static void scaled(File imageFile, int times, File targetFile) throws IOException {
    BufferedImage image = ImageIO.read(imageFile);
    int targetWidth = image.getWidth() * times;
    int targetHeight = image.getHeight() * times;
    int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage tmp = new BufferedImage(targetWidth, targetHeight, type);
    Graphics2D g2 = tmp.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC);
    g2.drawImage(image, 0, 0, targetWidth, targetHeight, null);
    g2.dispose();
    ImageIO.write(tmp, "png", targetFile);
}

From source file:PictureScaler.java

/**
 * Convenience method that returns a scaled instance of the
 * provided BufferedImage.//from   w  w  w.  j  ava 2s  .c o  m
 * 
 * 
 * @param img the original image to be scaled
 * @param targetWidth the desired width of the scaled instance,
 *    in pixels
 * @param targetHeight the desired height of the scaled instance,
 *    in pixels
 * @param hint one of the rendering hints that corresponds to
 *    RenderingHints.KEY_INTERPOLATION (e.g.
 *    RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR,
 *    RenderingHints.VALUE_INTERPOLATION_BILINEAR,
 *    RenderingHints.VALUE_INTERPOLATION_BICUBIC)
 * @param progressiveBilinear if true, this method will use a multi-step
 *    scaling technique that provides higher quality than the usual
 *    one-step technique (only useful in down-scaling cases, where
 *    targetWidth or targetHeight is
 *    smaller than the original dimensions)
 * @return a scaled version of the original BufferedImage
 */
public BufferedImage getFasterScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean progressiveBilinear) {
    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = img;
    BufferedImage scratchImage = null;
    Graphics2D g2 = null;
    int w, h;
    int prevW = ret.getWidth();
    int prevH = ret.getHeight();
    boolean isTranslucent = img.getTransparency() != Transparency.OPAQUE;

    if (progressiveBilinear) {
        // Use multi-step technique: start with original size, then
        // scale down in multiple passes with drawImage()
        // until the target size is reached
        w = img.getWidth();
        h = img.getHeight();
    } else {
        // Use one-step technique: scale directly from original
        // size to target size with a single drawImage() call
        w = targetWidth;
        h = targetHeight;
    }

    do {
        if (progressiveBilinear && w > targetWidth) {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
        }

        if (progressiveBilinear && h > targetHeight) {
            h /= 2;
            if (h < targetHeight) {
                h = targetHeight;
            }
        }

        if (scratchImage == null || isTranslucent) {
            // Use a single scratch buffer for all iterations
            // and then copy to the final, correctly-sized image
            // before returning
            scratchImage = new BufferedImage(w, h, type);
            g2 = scratchImage.createGraphics();
        }
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(ret, 0, 0, w, h, 0, 0, prevW, prevH, null);
        prevW = w;
        prevH = h;

        ret = scratchImage;
    } while (w != targetWidth || h != targetHeight);

    if (g2 != null) {
        g2.dispose();
    }

    // If we used a scratch buffer that is larger than our target size,
    // create an image of the right size and copy the results into it
    if (targetWidth != ret.getWidth() || targetHeight != ret.getHeight()) {
        scratchImage = new BufferedImage(targetWidth, targetHeight, type);
        g2 = scratchImage.createGraphics();
        g2.drawImage(ret, 0, 0, null);
        g2.dispose();
        ret = scratchImage;
    }

    return ret;
}

From source file:net.sf.jasperreports.engine.RenderableUtil.java

/**
 *
 *///from   w  w  w. j  a  va  2s  .  c om
public Renderable getRenderable(Image img, OnErrorTypeEnum onErrorType) throws JRException {
    ImageTypeEnum type = ImageTypeEnum.JPEG;
    if (img instanceof RenderedImage) {
        ColorModel colorModel = ((RenderedImage) img).getColorModel();
        //if the image has transparency, encode as PNG
        if (colorModel.hasAlpha() && colorModel.getTransparency() != Transparency.OPAQUE) {
            type = ImageTypeEnum.PNG;
        }
    }

    return getRenderable(img, type, onErrorType);
}

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

public final static BufferedImage reduceImage(BufferedImage image, int width, int height) {
    int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = (BufferedImage) image;
    int w = image.getWidth();
    int h = image.getHeight();

    while (w != width || h != height) {
        if (w > width) {
            w /= 2;//w w  w.j a v  a  2 s.  co m
            if (w < width)
                w = width;
        }

        if (h > height) {
            h /= 2;
            if (h < height)
                h = height;
        }

        BufferedImage tmp = new BufferedImage(w, h, type);

        Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();
        ret = tmp;
    }
    return ret;
}

From source file:com.t3.image.ImageUtil.java

/**
 * Look at the image and determine which Transparency is most appropriate.
 * If it finds any translucent pixels it returns Transparency.TRANSLUCENT, if 
 * it finds at least one purely transparent pixel and no translucent pixels
 * it will return Transparency.BITMASK, in all other cases it returns 
 * Transparency.OPAQUE, including errors
 * // w ww . j  a v a  2 s .  c  om
 * @param image
 * @return one of Transparency constants
 */
public static int pickBestTransparency(Image image) {

    // Take a shortcut if possible
    if (image instanceof BufferedImage) {
        return pickBestTransparency((BufferedImage) image);
    }

    // Legacy method
    // NOTE: This is a horrible memory hog
    int width = image.getWidth(null);
    int height = image.getHeight(null);
    int[] pixelArray = new int[width * height];
    PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, pixelArray, 0, width);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        System.err.println("interrupted waiting for pixels!");
        return Transparency.OPAQUE;
    }

    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
        System.err.println("image fetch aborted or errored");
        return Transparency.OPAQUE;
    }

    // Look for specific pixels
    boolean foundTransparent = false;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            // Get the next pixel
            int pixel = pixelArray[y * width + x];
            int alpha = (pixel >> 24) & 0xff;

            // Is there translucency or just pure transparency ?
            if (alpha > 0 && alpha < 255) {
                return Transparency.TRANSLUCENT;
            }

            if (alpha == 0 && !foundTransparent) {
                foundTransparent = true;
            }
        }
    }

    return foundTransparent ? Transparency.BITMASK : Transparency.OPAQUE;
}

From source file:net.rptools.lib.image.ImageUtil.java

/**
 * Look at the image and determine which Transparency is most appropriate. If it finds any translucent pixels it
 * returns Transparency.TRANSLUCENT, if it finds at least one purely transparent pixel and no translucent pixels it
 * will return Transparency.BITMASK, in all other cases it returns Transparency.OPAQUE, including errors
 * /*from ww  w .ja  v  a  2s .  c o m*/
 * @param image
 * @return one of Transparency constants
 */
public static int pickBestTransparency(Image image) {
    // Take a shortcut if possible
    if (image instanceof BufferedImage) {
        return pickBestTransparency((BufferedImage) image);
    }

    // Legacy method
    // NOTE: This is a horrible memory hog
    int width = image.getWidth(null);
    int height = image.getHeight(null);
    int[] pixelArray = new int[width * height];
    PixelGrabber pg = new PixelGrabber(image, 0, 0, width, height, pixelArray, 0, width);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        System.err.println("interrupted waiting for pixels!");
        return Transparency.OPAQUE;
    }
    if ((pg.getStatus() & ImageObserver.ABORT) != 0) {
        System.err.println("image fetch aborted or errored");
        return Transparency.OPAQUE;
    }
    // Look for specific pixels
    boolean foundTransparent = false;
    for (int y = 0; y < height; y++) {
        for (int x = 0; x < width; x++) {
            // Get the next pixel
            int pixel = pixelArray[y * width + x];
            int alpha = (pixel >> 24) & 0xff;

            // Is there translucency or just pure transparency ?
            if (alpha > 0 && alpha < 255) {
                return Transparency.TRANSLUCENT;
            }
            if (alpha == 0 && !foundTransparent) {
                foundTransparent = true;
            }
        }
    }
    return foundTransparent ? Transparency.BITMASK : Transparency.OPAQUE;
}

From source file:com.alvermont.terraj.planet.io.ImageBuilder.java

/**
 * Create and return a <code>BufferedImage</code> object from the
 * result of the projection. This image can then be further processed
 * or written out to a file using <code>ImageIO</code>.
 *
 * @param proj The projection that will provide the image
 * @return A <code>BufferedImage</code> object containing the results of
 * the projection./*w  w w . j a va2s.  c o m*/
 */
public BufferedImage getImage(Projector proj) {
    // get the pixel data and store it into a data buffer
    final byte[] pixels = getPixels(proj);

    final DataBuffer db = new DataBufferByte(pixels, pixels.length);

    // set up offsets for the R,G,B elements
    final int[] offsets = new int[3];

    offsets[0] = 0;
    offsets[1] = 1;
    offsets[2] = 2;

    // create the raster from the pixel data
    final int height = proj.getParameters().getProjectionParameters().getHeight();
    final int width = proj.getParameters().getProjectionParameters().getWidth();

    final WritableRaster raster = Raster.createInterleavedRaster(db, width, height, width * 3, 3, offsets,
            null);

    // create a colour model that matches the raster we have
    final ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false,
            Transparency.OPAQUE, DataBuffer.TYPE_BYTE);

    // combine raster and colour model into a buffered image
    final BufferedImage img = new BufferedImage(cm, raster, false, null);

    return img;
}

From source file:FileUtils.java

/**
 * Convenience method that returns a scaled instance of the
 * provided {@code BufferedImage}./*from  w w w. ja  v a  2  s .c  o  m*/
 *
 * @param img           the original image to be scaled
 * @param targetWidth   the desired width of the scaled instance,
 *                      in pixels
 * @param targetHeight  the desired height of the scaled instance,
 *                      in pixels
 * @param hint          one of the rendering hints that corresponds to
 *                      {@code RenderingHints.KEY_INTERPOLATION} (e.g.
 *                      {@code RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR},
 *                      {@code RenderingHints.VALUE_INTERPOLATION_BILINEAR},
 *                      {@code RenderingHints.VALUE_INTERPOLATION_BICUBIC})
 * @param higherQuality if true, this method will use a multi-step
 *                      scaling technique that provides higher quality than the usual
 *                      one-step technique (only useful in downscaling cases, where
 *                      {@code targetWidth} or {@code targetHeight} is
 *                      smaller than the original dimensions, and generally only when
 *                      the {@code BILINEAR} hint is specified)
 * @return a scaled version of the original {@code BufferedImage}
 */
public static BufferedImage getScaledInstance(BufferedImage img, int targetWidth, int targetHeight, Object hint,
        boolean higherQuality) {
    final int type = img.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB
            : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = (BufferedImage) img;
    int w;
    int h;
    if (higherQuality) {
        // Use multi-step technique: start with original size, then
        // scale down in multiple passes with drawImage()
        // until the target size is reached
        w = img.getWidth();
        h = img.getHeight();
    } else {
        // Use one-step technique: scale directly from original
        // size to target size with a single drawImage() call
        w = targetWidth;
        h = targetHeight;
    }

    do {
        if (higherQuality && w > targetWidth) {
            w /= 2;
            if (w < targetWidth) {
                w = targetWidth;
            }
        }

        if (higherQuality && h > targetHeight) {
            h /= 2;
            if (h < targetHeight) {
                h = targetHeight;
            }
        }

        final BufferedImage tmp = new BufferedImage(w, h, type);
        final Graphics2D g2 = tmp.createGraphics();
        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
        g2.drawImage(ret, 0, 0, w, h, null);
        g2.dispose();

        ret = tmp;
    } while (w != targetWidth || h != targetHeight);

    return ret;
}