private BufferedImage toCompatibleImage(BufferedImage image) { // obtain the current system graphical settings GraphicsConfiguration gfx_config = GraphicsEnvironment. getLocalGraphicsEnvironment().getDefaultScreenDevice(). getDefaultConfiguration(); /* * if image is already compatible and optimized for current system * settings, simply return it */ if (image.getColorModel().equals(gfx_config.getColorModel())) return image; // image is not optimized, so create a new image that is BufferedImage new_image = gfx_config.createCompatibleImage( image.getWidth(), image.getHeight(), image.getTransparency()); // get the graphics context of the new image to draw the old image on Graphics2D g2d = (Graphics2D) new_image.getGraphics(); // actually draw the image and dispose of context no longer needed g2d.drawImage(image, 0, 0, null); g2d.dispose(); // return the new optimized image return new_image; }
width, height, (src.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB));
int type = (src.getTransparency() == Transparency.OPAQUE ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB); BufferedImage result = new BufferedImage(src.getWidth(),
boolean higherQuality) int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage ret = (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;
final int targetWidth = targetDim.width; final int targetHeight = targetDim.height; final int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage ret = (BufferedImage) img;
if(image.getTransparency() == Transparency.OPAQUE) { imageType = BufferedImage.TYPE_INT_RGB;
boolean imageHasAlpha = (src.getTransparency() != BufferedImage.OPAQUE);
if (img.getTransparency() == Transparency.OPAQUE){ ByteBuffer data = BufferUtils.createByteBuffer(img.getWidth()*img.getHeight()*3);
private static int getTransparency(Image pImage) { if (pImage instanceof BufferedImage) { BufferedImage bi = (BufferedImage) pImage; return bi.getTransparency(); } return Transparency.OPAQUE; }
BufferedImage image = ImageIO.read ( url ); BufferedImage convertedImage = null; GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment (); GraphicsDevice gd = ge.getDefaultScreenDevice (); GraphicsConfiguration gc = gd.getDefaultConfiguration (); convertedImage = gc.createCompatibleImage (image.getWidth (), image.getHeight (), image.getTransparency () ); Graphics2D g2d = convertedImage.createGraphics (); g2d.drawImage ( image, 0, 0, image.getWidth (), image.getHeight (), null ); g2d.dispose()
/** * Convenience method that returns a scaled instance of the provided * {@code BufferedImage}. * * @param image 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 * @return a scaled version of the original {@code BufferedImage} */ public static BufferedImage getScaledInstance(BufferedImage image, int targetWidth, int targetHeight) { 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(); return tmp; }
/** * A replacement for the standard <code>BufferedImage.getSubimage</code> * method. * * @param image * @param x the X coordinate of the upper-left corner of the specified * rectangular region * @param y the Y coordinate of the upper-left corner of the specified * rectangular region * @param width the width of the specified rectangular region * @param height the height of the specified rectangular region * @return a BufferedImage that is the subimage of <code>image</code>. */ public static BufferedImage getSubImage(BufferedImage image, int x, int y, int width, int height) { int type = (image.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage tmp = new BufferedImage(width, height, type); Graphics2D g2 = tmp.createGraphics(); g2.drawImage(image.getSubimage(x, y, width, height), 0, 0, null); g2.dispose(); return tmp; }
private static BufferedImage getAlphaImage(BufferedImage image) throws IOException { if (!image.getColorModel().hasAlpha()) { return null; } if (image.getTransparency() == Transparency.BITMASK) { throw new UnsupportedOperationException("BITMASK Transparency JPEG compression is not" + " useful, use LosslessImageFactory instead"); } WritableRaster alphaRaster = image.getAlphaRaster(); if (alphaRaster == null) { // happens sometimes (PDFBOX-2654) despite colormodel claiming to have alpha return null; } BufferedImage alphaImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY); alphaImage.setData(alphaRaster); return alphaImage; }
int alphaByteIdx = 0; int alphaBitPos = 7; int transparency = image.getTransparency(); int apbc = transparency == Transparency.BITMASK ? 1 : 8; byte[] alphaImageData;
BufferedImage buffered = (BufferedImage) pImage; if (buffered.getType() != BufferedImage.TYPE_CUSTOM && equals(buffered.getColorModel(), pConfiguration.getColorModel(buffered.getTransparency()))) { return buffered;
@Test public void testGetBufferedImageGIF() { URL resource = getClass().getResource("/tux.gif"); assertNotNull(resource); Image source = Toolkit.getDefaultToolkit().createImage(resource); assertNotNull(source); BufferedImageFactory factory = new BufferedImageFactory(source); BufferedImage image = factory.getBufferedImage(); assertEquals(250, image.getWidth()); assertEquals(250, image.getHeight()); assertEquals(Transparency.BITMASK, image.getTransparency()); // All corners of image should be fully transparent assertEquals(0, image.getRGB(0, 0) >>> 24); assertEquals(0, image.getRGB(249, 0) >>> 24); assertEquals(0, image.getRGB(0, 249) >>> 24); assertEquals(0, image.getRGB(249, 249) >>> 24); }
@Test public void testMultiChannelNoTransparencyPSB() throws IOException { PSDImageReader imageReader = createReader(); // The following PSB is RGB, has 4 channels (1 alpha/auxillary channel), but should be treated as opaque try (ImageInputStream stream = ImageIO.createImageInputStream(getClassLoaderResource("/psb/rgb-multichannel-no-transparency.psb"))) { imageReader.setInput(stream); BufferedImage image = imageReader.read(0); assertEquals(Transparency.OPAQUE, image.getTransparency()); } }
@Test public void testMultiChannelNoTransparencyPSD() throws IOException { PSDImageReader imageReader = createReader(); // The following PSD is RGB, has 4 channels (1 alpha/auxillary channel), but should be treated as opaque try (ImageInputStream stream = ImageIO.createImageInputStream(getClassLoaderResource("/psd/rgb-multichannel-no-transparency.psd"))) { imageReader.setInput(stream); BufferedImage image = imageReader.read(0); assertEquals(Transparency.OPAQUE, image.getTransparency()); } }
imageXObject.getCOSObject().setItem(COSName.DECODE_PARMS, decodeParms); if (image.getTransparency() != Transparency.OPAQUE)