/** * 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); }
BufferedImage buffered = (BufferedImage) pImage; if (buffered.getType() != BufferedImage.TYPE_CUSTOM && equals(buffered.getColorModel(), pConfiguration.getColorModel(buffered.getTransparency()))) { return buffered; int w = ImageUtil.getWidth(pImage); int h = ImageUtil.getHeight(pImage); BufferedImage temp = createClear(w, h, BI_TYPE_ANY, getTransparency(pImage), pBackgroundColor, pConfiguration); drawOnto(temp, pImage);
/** * Tries to use H/W-accelerated code for an image for display purposes. * Note that transparent parts of the image might be replaced by solid * color. Additional image information not used by the current diplay * hardware may be discarded, like extra bith depth etc. * * @param pImage any {@code Image} * @return a {@code BufferedImage} */ public static BufferedImage accelerate(Image pImage) { return accelerate(pImage, null, DEFAULT_CONFIGURATION); }
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 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); }
&& (pICM == null || equals(((BufferedImage) pOriginal).getColorModel(), pICM))) { return (BufferedImage) pOriginal; image = createBuffered(getWidth(pOriginal), getHeight(pOriginal), pType, Transparency.TRANSLUCENT);//new BufferedImage(getWidth(pOriginal), getHeight(pOriginal), pType); image = new BufferedImage(getWidth(pOriginal), getHeight(pOriginal), pType, pICM); drawOnto(image, pOriginal);
final BufferedImage notContrasted = ImageUtil.toBuffered(ImageUtil.contrast(original, 0f)); final BufferedImage contrasted = ImageUtil.toBuffered(ImageUtil.contrast(original)); final BufferedImage contrastedDefault = ImageUtil.toBuffered(ImageUtil.contrast(original, 0.5f)); for (int y = 0; y < original.getHeight(); y++) { for (int x = 0; x < original.getWidth(); x++) { final BufferedImage contrastedMax = ImageUtil.toBuffered(ImageUtil.contrast(original, 1f)); for (int y = 0; y < contrastedMax.getHeight(); y++) { for (int x = 0; x < contrastedMax.getWidth(); x++) { final BufferedImage contrastedNegative = ImageUtil.toBuffered(ImageUtil.contrast(original, -0.5f)); for (int y = 0; y < original.getHeight(); y++) { for (int x = 0; x < original.getWidth(); x++) { final BufferedImage contrastedMoreNegative = ImageUtil.toBuffered(ImageUtil.contrast(original, -1.0f)); for (int y = 0; y < contrastedMoreNegative.getHeight(); y++) { for (int x = 0; x < contrastedMoreNegative.getWidth(); x++) {
BufferedImage scaled = createResampled(pImage, pWidth, pHeight, pHints); if (type != scaled.getType() && type != BI_TYPE_ANY || !equals(scaled.getColorModel(), cm)) { raster = createCompatibleWritableRaster((BufferedImage) pImage, cm, pWidth, pHeight); drawOnto(temp, scaled);
image = ImageUtil.toBuffered(image, BufferedImage.TYPE_INT_RGB); image = ImageUtil.createIndexed( ImageUtil.toBuffered(image), 256, null, (image.getColorModel().getTransparency() == Transparency.OPAQUE ? ImageUtil.TRANSPARENCY_OPAQUE : ImageUtil.TRANSPARENCY_BITMASK) | ImageUtil.DITHER_DIFFUSION_ALTSCANS );
if (filterType != FILTER_POINT && filterType != FILTER_BOX && (cm = input.getColorModel()) instanceof IndexColorModel) { temp = ImageUtil.toBuffered(input, cm.hasAlpha() ? BufferedImage.TYPE_4BYTE_ABGR : BufferedImage.TYPE_3BYTE_BGR); result = output != null && temp.getType() != BufferedImage.TYPE_CUSTOM ? /*output*/ ImageUtil.toBuffered(output, temp.getType()) : createCompatibleDestImage(temp, null); ImageUtil.drawOnto(output, result); result = output;
image = ImageUtil.toBuffered(ImageUtil.grayscale(image)); indexed = getIndexedImage(image, colors = getIndexColorModel(image, numColors, hints), background, hints); indexed = getIndexedImage(ImageUtil.toBuffered(image, BufferedImage.TYPE_INT_ARGB), colors = getIndexColorModel(paletteImg, numColors, hints), background, hints); image = ImageUtil.toBuffered(image, BufferedImage.TYPE_INT_ARGB); indexed = getIndexedImage(image, colors = getIndexColorModel(image, numColors, hints), background, hints);
assertNotNull(original); final BufferedImage notBrightened = ImageUtil.toBuffered(ImageUtil.brightness(original, 0f)); final BufferedImage brightened = ImageUtil.toBuffered(ImageUtil.brightness(original, 0.4f)); final BufferedImage brightenedMore = ImageUtil.toBuffered(ImageUtil.brightness(original, 0.9f)); for (int y = 0; y < original.getHeight(); y++) { for (int x = 0; x < original.getWidth(); x++) { final BufferedImage brightenedMax = ImageUtil.toBuffered(ImageUtil.brightness(original, 2f)); for (int y = 0; y < brightenedMax.getHeight(); y++) { for (int x = 0; x < brightenedMax.getWidth(); x++) { final BufferedImage brightenedNegative = ImageUtil.toBuffered(ImageUtil.brightness(original, -0.4f)); final BufferedImage brightenedNegativeMore = ImageUtil.toBuffered(ImageUtil.brightness(original, -0.9f)); for (int y = 0; y < original.getHeight(); y++) { for (int x = 0; x < original.getWidth(); x++) { final BufferedImage brightenedMaxNegative = ImageUtil.toBuffered(ImageUtil.brightness(original, -2f)); for (int y = 0; y < brightenedMaxNegative.getHeight(); y++) { for (int x = 0; x < brightenedMaxNegative.getWidth(); x++) {
/** * Creates a blurred version of the given image. * * @param pOriginal the original image * * @return a new {@code BufferedImage} with a blurred version of the given image */ public static BufferedImage blur(BufferedImage pOriginal) { return blur(pOriginal, 1.5f); }
/** * Creates a transparent image. * * @param pWidth the requested width of the image * @param pHeight the requested height of the image * * @throws IllegalArgumentException if {@code pType} is not a valid type * for {@code BufferedImage} * * @return the new image */ public static BufferedImage createTransparent(int pWidth, int pHeight) { return createTransparent(pWidth, pHeight, BI_TYPE_ANY); }
image = ImageUtil.createIndexed(original, 256, null, ImageUtil.COLOR_SELECTION_FAST | ImageUtil.DITHER_DIFFUSION_ALTSCANS);
private BufferedImage box(final BufferedImage pImage, final int pMaxDimension) { // TODO: ImageUtil.toRGB method? ColorConvertOp MUCH faster than ImageUtil.toBuffered(img, type) BufferedImage image = pImage; if (image.getType() == 0) { try { ColorConvertOp op = new ColorConvertOp(ColorSpace.getInstance(ColorSpace.CS_sRGB), null); image = op.filter(image, new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_4BYTE_ABGR_PRE)); } catch (Exception e) { e.printStackTrace(); image = ImageUtil.accelerate(image); } } if (image.getWidth() > pMaxDimension || image.getHeight() > pMaxDimension) { int w, h; if (image.getWidth() > image.getHeight()) { w = pMaxDimension; h = (int) Math.round(w / (image.getWidth() / (double) image.getHeight())); } else { h = pMaxDimension; w = (int) Math.round(h * (image.getWidth() / (double) image.getHeight())); } return ImageUtil.createResampled(image, w, h, Image.SCALE_DEFAULT); } return image; } }
/** * Adds contrast * * @param pOriginal the BufferedImage to add contrast to * * @return an {@code Image}, containing the contrasted image. */ public static Image contrast(Image pOriginal) { return contrast(pOriginal, 0.3f); }
private BufferedImage resampleImage(final BufferedImage image, final Dimension size) { if (image != null && size != null && (image.getWidth() != size.width || image.getHeight() != size.height)) { int resampleAlgorithm = getResampleAlgorithmFromRequest(); // TODO: One possibility is to NOT handle index color here, and only handle it later, IF NEEDED (read: GIF, // possibly also for PNG) when we know the output format (flush method). // This will make the filter faster (and better quality, possibly at the expense of more bytes being sent // over the wire) in the general case. Who uses GIF nowadays anyway? // Also, this means we could either keep the original IndexColorModel in the filter, or go through the // expensive operation of re-calculating the optimal palette for the new image (the latter might improve quality). // NOTE: Only use createScaled if IndexColorModel, as it's more expensive due to color conversion /* if (image.getColorModel() instanceof IndexColorModel) { // return ImageUtil.createScaled(image, size.width, size.height, resampleAlgorithm); BufferedImage resampled = ImageUtil.createResampled(image, size.width, size.height, resampleAlgorithm); return ImageUtil.createIndexed(resampled, (IndexColorModel) image.getColorModel(), null, ImageUtil.DITHER_NONE | ImageUtil.TRANSPARENCY_BITMASK); // return ImageUtil.createIndexed(resampled, 256, null, ImageUtil.COLOR_SELECTION_QUALITY | ImageUtil.DITHER_NONE | ImageUtil.TRANSPARENCY_BITMASK); } else { */ return ImageUtil.createResampled(image, size.width, size.height, resampleAlgorithm); // } } return image; }