Java BufferedImage Operation optimizeForGraphicsHardwareIfRequired(BufferedImage image)

Here you can find the source of optimizeForGraphicsHardwareIfRequired(BufferedImage image)

Description

Converts an image to an optimized version for the default screen.
The optimized image should draw faster.

License

Minecraft Mod Public

Parameter

Parameter Description
image The image to optimize.

Return

Returns the optimized image.

Declaration

public static BufferedImage optimizeForGraphicsHardwareIfRequired(BufferedImage image) 

Method Source Code


//package com.java2s;
import java.awt.*;

public class Main {
    /**//from w w  w .  j ava  2s  .c om
     * Converts an image to an optimized version for the default screen.<br>
     * The optimized image should draw faster. Only creates a new image if
     * the passed image is not already optimized.
     * @param image The image to optimize.
     * @return Returns the optimized image.
     */
    public static BufferedImage optimizeForGraphicsHardwareIfRequired(BufferedImage image) {
        try {
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
            GraphicsDevice gd = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gd.getDefaultConfiguration();

            // return passed image if it already is optimized
            if (image.getColorModel().equals(gc.getColorModel())) {
                return image;
            }
            // create an empty optimized BufferedImage
            int w = image.getWidth();
            int h = image.getHeight();

            // strange things happen on Linux and Windows on some systems when
            // monitors are set to 16 bit.
            boolean bit16 = gc.getColorModel().getPixelSize() < 24;
            final int transp;
            if (bit16) {
                transp = Transparency.TRANSLUCENT;
            } else {
                transp = image.getColorModel().getTransparency();
            }
            BufferedImage optimized = gc.createCompatibleImage(w, h, transp);

            // draw the passed image into the optimized image
            optimized.getGraphics().drawImage(image, 0, 0, null);
            return optimized;
        } catch (Exception e) {
            // return the original image if an exception occured.
            return image;
        }
    }
}

Related

  1. offset(final BufferedImage image, final float[] scales, final float[] offsets)
  2. open(BufferedImage image)
  3. openAsBufferedImage(String path)
  4. operatedImage(BufferedImage source, BufferedImageOp op)
  5. optimizeForGraphicsHardware(BufferedImage image)
  6. optimizeImage(BufferedImage image)
  7. otsuTreshold(BufferedImage original)
  8. output(BufferedImage image, OutputStream out)
  9. outPutImage(BufferedImage bufferedimage, String targetPath)