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; }
/** * 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 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); }
BufferedImage scaled = createResampled(pImage, pWidth, pHeight, pHints);
BufferedImage image = ImageUtil.createResampled(read, size.width, size.height, imageResponse.getResampleAlgorithmFromRequest());
/** * 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 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); }
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; } }
/** * 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); }
/** * 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); }
BufferedImage scaled = createResampled(pImage, pWidth, pHeight, pHints);
BufferedImage scaled = createResampled(pImage, pWidth, pHeight, pHints);
BufferedImage image = ImageUtil.createResampled(read, size.width, size.height, imageResponse.getResampleAlgorithmFromRequest());