/** * Converts the {@code BufferedImage} to a {@code BufferedImage} of the * given type. The new image will have the same {@code ColorModel}, * {@code Raster} and properties as the original image, if possible. * <p/> * If the image is already a {@code BufferedImage} of the given type, it * is simply returned and no conversion takes place. * <p/> * This method simply invokes * {@link #toBuffered(RenderedImage,int) toBuffered((RenderedImage) pOriginal, pType)}. * * @param pOriginal the image to convert. * @param pType the type of buffered image * * @return a {@code BufferedImage} * * @throws IllegalArgumentException if {@code pOriginal == null} * or if {@code pType} is not a valid type for {@code BufferedImage} * * @see java.awt.image.BufferedImage#getType() */ public static BufferedImage toBuffered(BufferedImage pOriginal, int pType) { return toBuffered((RenderedImage) pOriginal, pType); }
/** * Converts the {@code Image} to a {@code BufferedImage} of the given type. * The new image will have the same {@code ColorModel}, {@code Raster} and * properties as the original image, if possible. * <p/> * If the image is already a {@code BufferedImage} of the given type, it * is simply returned and no conversion takes place. * * @param pOriginal the image to convert. * @param pType the type of buffered image * * @return a {@code BufferedImage} * * @throws IllegalArgumentException if {@code pOriginal == null} * or if {@code pType} is not a valid type for {@code BufferedImage} * * @see java.awt.image.BufferedImage#getType() */ public static BufferedImage toBuffered(Image pOriginal, int pType) { return toBuffered(pOriginal, pType, null); }
protected RenderedImage doFilter(BufferedImage pImage, ServletRequest pRequest, ImageServletResponse pResponse) { // Filter Image img = ImageUtil.filter(pImage, imageFilter); // Create BufferedImage & return return ImageUtil.toBuffered(img, BufferedImage.TYPE_INT_RGB); // TODO: This is ok for JPEG only... } }
/** * Rotates the image to the given angle. Areas not covered with pixels from * the source image will be left transparent, if possible. * * @param pImage the source image * @param pAngle the angle of rotation, in radians * * @return a new {@code BufferedImage}, unless {@code pAngle == 0.0} */ public static BufferedImage createRotated(final Image pImage, final double pAngle) { return createRotated0(toBuffered(pImage), pAngle); }
/** * Creates an indexed version of the given image (a {@code BufferedImage} * with an {@code IndexColorModel}. * The resulting image will have a maximum of 256 different colors. * Transparent parts of the original will be replaced with solid black. * Default (possibly HW accelerated) dither will be used. * * @param pImage the image to convert * * @return an indexed version of the given image */ public static BufferedImage createIndexed(Image pImage) { return IndexImage.getIndexedImage(toBuffered(pImage), 256, Color.black, IndexImage.DITHER_DEFAULT); }
/** * Creates a scaled instance of the given {@code Image}, and converts it to * a {@code BufferedImage} if needed. * * @param pImage the {@code Image} to scale * @param pWidth width in pixels * @param pHeight height in pixels * @param pHints scaling mHints * * @return a {@code BufferedImage} * * @throws NullPointerException if {@code pImage} is {@code null}. * * @see Image#SCALE_AREA_AVERAGING * @see Image#SCALE_DEFAULT * @see Image#SCALE_FAST * @see Image#SCALE_REPLICATE * @see Image#SCALE_SMOOTH * @see ResampleOp */ public static BufferedImage createResampled(Image pImage, int pWidth, int pHeight, int pHints) { // NOTE: TYPE_4BYTE_ABGR or TYPE_3BYTE_BGR is more efficient when accelerated... BufferedImage image = pImage instanceof BufferedImage ? (BufferedImage) pImage : toBuffered(pImage, BufferedImage.TYPE_4BYTE_ABGR); return createResampled(image, pWidth, pHeight, pHints); }
/** * Creates an indexed version of the given image (a {@code BufferedImage} * with an {@code IndexColorModel}. * * @param pImage the image to convert * @param pColors number of colors in the resulting image * @param pMatte color to replace transparent parts of the original. * @param pHints hints controlling dither, transparency and color selection * * @return an indexed version of the given image * * @see #COLOR_SELECTION_DEFAULT * @see #COLOR_SELECTION_FAST * @see #COLOR_SELECTION_QUALITY * @see #DITHER_NONE * @see #DITHER_DEFAULT * @see #DITHER_DIFFUSION * @see #DITHER_DIFFUSION_ALTSCANS * @see #TRANSPARENCY_DEFAULT * @see #TRANSPARENCY_OPAQUE * @see #TRANSPARENCY_BITMASK * @see #TRANSPARENCY_TRANSLUCENT */ public static BufferedImage createIndexed(Image pImage, int pColors, Color pMatte, int pHints) { return IndexImage.getIndexedImage(toBuffered(pImage), pColors, pMatte, pHints); }
/** * Creates an indexed version of the given image (a {@code BufferedImage} * with an {@code IndexColorModel}. * * @param pImage the image to convert * @param pColors the {@code IndexColorModel} to be used in the resulting * image. * @param pMatte color to replace transparent parts of the original. * @param pHints hints controlling dither, transparency and color selection * * @return an indexed version of the given image * * @see #COLOR_SELECTION_DEFAULT * @see #COLOR_SELECTION_FAST * @see #COLOR_SELECTION_QUALITY * @see #DITHER_NONE * @see #DITHER_DEFAULT * @see #DITHER_DIFFUSION * @see #DITHER_DIFFUSION_ALTSCANS * @see #TRANSPARENCY_DEFAULT * @see #TRANSPARENCY_OPAQUE * @see #TRANSPARENCY_BITMASK * @see #TRANSPARENCY_TRANSLUCENT */ public static BufferedImage createIndexed(Image pImage, IndexColorModel pColors, Color pMatte, int pHints) { return IndexImage.getIndexedImage(toBuffered(pImage), pColors, pMatte, pHints); }
/** * Creates a flipped version of the given image. * * @param pImage the image to flip * @param pAxis the axis to flip around * @return a new {@code BufferedImage} */ public static BufferedImage createFlipped(final Image pImage, final int pAxis) { switch (pAxis) { case FLIP_HORIZONTAL: case FLIP_VERTICAL: // TODO case FLIP_BOTH:?? same as rotate 180? break; default: throw new IllegalArgumentException("Illegal direction: " + pAxis); } BufferedImage source = toBuffered(pImage); AffineTransform transform; if (pAxis == FLIP_HORIZONTAL) { transform = AffineTransform.getTranslateInstance(0, source.getHeight()); transform.scale(1, -1); } else { transform = AffineTransform.getTranslateInstance(source.getWidth(), 0); transform.scale(-1, 1); } AffineTransformOp transformOp = new AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR); return transformOp.filter(source, null); }
private static MagickImage rgbToMagic(BufferedImage pImage, boolean pAlpha) throws MagickException { MagickImage image = new MagickImage(); BufferedImage buffered = ImageUtil.toBuffered(pImage, pAlpha ? BufferedImage.TYPE_4BYTE_ABGR : BufferedImage.TYPE_3BYTE_BGR); // Need to get data of sub raster, not the full data array, this is // just a convenient way Raster raster; if (buffered.getRaster().getParent() != null) { raster = buffered.getData(new Rectangle(buffered.getWidth(), buffered.getHeight())); } else { raster = buffered.getRaster(); } image.constituteImage(buffered.getWidth(), buffered.getHeight(), pAlpha ? "ABGR" : "BGR", ((DataBufferByte) raster.getDataBuffer()).getData()); return image; }
private static BufferedImage flatten(final BufferedImage pImage, final Color pBackgroundColor) { BufferedImage image = ImageUtil.toBuffered(pImage, BufferedImage.TYPE_INT_ARGB); Graphics2D g = image.createGraphics(); try { g.setComposite(AlphaComposite.DstOver); g.setColor(pBackgroundColor); g.fillRect(0, 0, pImage.getWidth(), pImage.getHeight()); } finally { g.dispose(); } return image; }
BufferedImage buffered = ImageUtil.toBuffered(pImage, pAlpha ? BufferedImage.TYPE_4BYTE_ABGR : BufferedImage.TYPE_BYTE_GRAY);
@Override public boolean importData(JComponent comp, Transferable t) { if (canImport(comp, t.getTransferDataFlavors())) { try { Image transferData = (Image) t.getTransferData(DataFlavor.imageFlavor); image = ImageUtil.toBuffered(transferData); setIcon(new BufferedImageIcon(image)); return true; } catch (UnsupportedFlavorException | IOException ignore) { } } return false; }
/** * Creates an indexed version of the given image (a {@code BufferedImage} * with an {@code IndexColorModel}. * * @param pImage the image to convert * @param pColors an {@code Image} used to get colors from. If the image is * has an {@code IndexColorModel}, it will be uesd, otherwise an * {@code IndexColorModel} is created from the image. * @param pMatte color to replace transparent parts of the original. * @param pHints hints controlling dither, transparency and color selection * * @return an indexed version of the given image * * @see #COLOR_SELECTION_DEFAULT * @see #COLOR_SELECTION_FAST * @see #COLOR_SELECTION_QUALITY * @see #DITHER_NONE * @see #DITHER_DEFAULT * @see #DITHER_DIFFUSION * @see #DITHER_DIFFUSION_ALTSCANS * @see #TRANSPARENCY_DEFAULT * @see #TRANSPARENCY_OPAQUE * @see #TRANSPARENCY_BITMASK * @see #TRANSPARENCY_TRANSLUCENT */ public static BufferedImage createIndexed(Image pImage, Image pColors, Color pMatte, int pHints) { return IndexImage.getIndexedImage(toBuffered(pImage), IndexImage.getIndexColorModel(pColors, 255, pHints), pMatte, pHints); }
@Test public void testToBufferedImage() { BufferedImage sameAsImage = ImageUtil.toBuffered((RenderedImage) image); BufferedImage bufferedScaled = ImageUtil.toBuffered(scaled); // Should be no need to convert assertSame(image, sameAsImage); // Should have same dimensions assertEquals(scaled.getWidth(null), bufferedScaled.getWidth()); assertEquals(scaled.getHeight(null), bufferedScaled.getHeight()); }
/** * Creates a scaled instance of the given {@code RenderedImage}, and * converts it to a {@code BufferedImage} if needed. * * @param pImage the {@code RenderedImage} to scale * @param pWidth width in pixels * @param pHeight height in pixels * @param pHints scaling mHints * * @return a {@code BufferedImage} * * @throws NullPointerException if {@code pImage} is {@code null}. * * @see Image#SCALE_AREA_AVERAGING * @see Image#SCALE_DEFAULT * @see Image#SCALE_FAST * @see Image#SCALE_REPLICATE * @see Image#SCALE_SMOOTH * @see ResampleOp */ public static BufferedImage createResampled(RenderedImage pImage, int pWidth, int pHeight, int pHints) { // NOTE: TYPE_4BYTE_ABGR or TYPE_3BYTE_BGR is more efficient when accelerated... BufferedImage image = pImage instanceof BufferedImage ? (BufferedImage) pImage : toBuffered(pImage, pImage.getColorModel().hasAlpha() ? BufferedImage.TYPE_4BYTE_ABGR : BufferedImage.TYPE_3BYTE_BGR); return createResampled(image, pWidth, pHeight, pHints); }
@Test public void testToBufferedImageType() { // Assumes image is TYPE_INT_ARGB BufferedImage converted = ImageUtil.toBuffered(image, BufferedImage.TYPE_BYTE_INDEXED); BufferedImage convertedToo = ImageUtil.toBuffered(image, BufferedImage.TYPE_BYTE_BINARY); // Should not be the same assertNotSame(image, converted); assertNotSame(image, convertedToo); // Correct type assertTrue(converted.getType() == BufferedImage.TYPE_BYTE_INDEXED); assertTrue(convertedToo.getType() == BufferedImage.TYPE_BYTE_BINARY); // Should have same dimensions assertEquals(image.getWidth(), converted.getWidth()); assertEquals(image.getHeight(), converted.getHeight()); assertEquals(image.getWidth(), convertedToo.getWidth()); assertEquals(image.getHeight(), convertedToo.getHeight()); }
@Test public void testToBufferedImageTypeNull() { BufferedImage img = null; boolean threwRuntimeException = false; try { img = ImageUtil.toBuffered(null, BufferedImage.TYPE_INT_ARGB); } catch (RuntimeException ne) { threwRuntimeException = true; } // No input should return null assertNull(img); // Should have thrown an exception assertTrue(threwRuntimeException); }
@Test public void testToBufferedImageNull() { BufferedImage img = null; boolean threwRuntimeException = false; try { img = ImageUtil.toBuffered((Image) null); } catch (RuntimeException ne) { threwRuntimeException = true; } // No input should return null assertNull(img); // Should have thrown an exception assertTrue(threwRuntimeException); }
image = ImageUtil.toBuffered(fakeSubsampling(image, pParam));