Java Utililty Methods BufferedImage Transform

List of utility methods to do BufferedImage Transform

Description

The list of methods to do BufferedImage Transform are organized into topic(s).

Method

BufferedImagecopyAndTranslateSubimage(BufferedImage src, Rectangle bounds)
In contrast to BufferedImage.getSubimage, this method creates a copy of the data
assert src != null;
assert bounds != null;
Rectangle intersection = SwingUtilities.computeIntersection(0, 0, src.getWidth(), src.getHeight(), 
        bounds);
if (intersection.width <= 0 || intersection.height <= 0) {
    throw new IllegalStateException("empty intersection: bounds = " + bounds + ", src width = "
            + src.getWidth() + ", src height = " + src.getHeight() + ", intersection = " + intersection);
Raster copyRaster = src.getData(intersection); 
Raster startingFrom00 = copyRaster.createChild(intersection.x, intersection.y, intersection.width,
        intersection.height, 0, 0, null);
return new BufferedImage(src.getColorModel(), (WritableRaster) startingFrom00, src.isAlphaPremultiplied(),
        null);
BufferedImagegetTransformedImage(BufferedImage image, double scaleX, double scaleY, double shearX, double shearY)
get Transformed Image
AffineTransform transform = new AffineTransform();
if (scaleX > 0 && scaleY > 0)
    transform.scale(scaleX, scaleY);
if (shearX > 0 && shearY > 0)
    transform.shear(shearX, shearY);
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BILINEAR);
BufferedImage dest = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
op.filter(image, dest);
...
BufferedImagetransform(BufferedImage image, int numquadrants)
transform
int w0 = image.getWidth();
int h0 = image.getHeight();
int w1 = w0;
int h1 = h0;
int centerX = w0 / 2;
int centerY = h0 / 2;
if (numquadrants % 2 == 1) {
    w1 = h0;
...
BufferedImagetransform(BufferedImage image, int sx, int sy, int dx, int dy)
transform
AffineTransform at = new AffineTransform();
at.concatenate(AffineTransform.getScaleInstance(sx, sy));
at.concatenate(AffineTransform.getTranslateInstance(dx, dy));
return createTransformed(image, at);
voidtransform(String originalFile, String thumbnailFile, int thumbWidth, int thumbHeight)
transform
Image image = javax.imageio.ImageIO.read(new File(originalFile));
double thumbRatio = (double) thumbWidth / (double) thumbHeight;
int imageWidth = image.getWidth(null);
int imageHeight = image.getHeight(null);
double imageRatio = (double) imageWidth / (double) imageHeight;
if (thumbRatio < imageRatio) {
    thumbHeight = (int) (thumbWidth / imageRatio);
} else {
...
ImageTransformGrayToTransparency(BufferedImage image)
Transform Gray To Transparency
ImageFilter filter = new RGBImageFilter() {
    public final int filterRGB(int x, int y, int rgb) {
        if (rgb == 0) {
            return 0;
        return rgb;
};
...
ImagetransformGrayToTransparency(BufferedImage image)
transform Gray To Transparency
RGBImageFilter filter = new RGBImageFilter() {
    @Override
    public final int filterRGB(int x, int y, int rgb) {
        return (rgb << 8) & 0xFF000000;
};
ImageProducer ip = new FilteredImageSource(image.getSource(), filter);
return Toolkit.getDefaultToolkit().createImage(ip);
...
BufferedImagetransformImage(BufferedImage image, AffineTransform transform)
Transforms an image according to the specified affine transform.
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);
BufferedImage img = op.createCompatibleDestImage(image,
        (image.getType() == BufferedImage.TYPE_BYTE_GRAY) ? image.getColorModel() : null);
img = op.filter(image, img);
return img;
BufferedImagetransformImage(BufferedImage image, AffineTransform transform)
transform Image
AffineTransformOp op = new AffineTransformOp(transform, null);
BufferedImage destImage = op.createCompatibleDestImage(image, image.getColorModel());
Graphics2D g2d = destImage.createGraphics();
g2d.drawImage(image, transform, null);
g2d.dispose();
return destImage;
BufferedImagetransformImage(BufferedImage image, AffineTransform transform, int newWidth, int newHeight)
transform Image
AffineTransformOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_BICUBIC);
BufferedImage destinationImage = new BufferedImage(newWidth, newHeight, image.getType());
Graphics2D g = destinationImage.createGraphics();
g.setColor(Color.WHITE);
destinationImage = op.filter(image, destinationImage);
return destinationImage;