Java Utililty Methods BufferedImage Operation

List of utility methods to do BufferedImage Operation

Description

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

Method

voidshowColors(BufferedImage image, boolean alpha, boolean red, boolean green, boolean blue)
show Colors
int a, r, g, b, rgb;
for (int y = 0; y < image.getHeight(); ++y) {
    for (int x = 0; x < image.getWidth(); ++x) {
        rgb = image.getRGB(x, y);
        if (alpha) {
            a = ((rgb >> 24) & 0xff);
        } else {
            a = 255;
...
BufferedImageshrink(BufferedImage source, double factor)
shrink
int w = (int) (source.getWidth() * factor);
int h = (int) (source.getHeight() * factor);
Image image = source.getScaledInstance(w, h, Image.SCALE_AREA_AVERAGING);
BufferedImage result = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
Graphics2D g = result.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return result;
...
BufferedImagesmaller(final BufferedImage src, final int radius, final int opaqueLimit)
smaller
BufferedImage ret = new BufferedImage(src.getWidth(), src.getHeight(), BufferedImage.TYPE_INT_ARGB);
for (int y = 0; y < src.getHeight(); y++) {
    for (int x = 0; x < src.getWidth(); x++) {
        if (((src.getRGB(x, y) >> 24) & 0xff) > opaqueLimit
                && checkRadius(src, x, y, opaqueLimit, radius)) {
            ret.setRGB(x, y, src.getRGB(x, y));
return ret;
BufferedImagestitchImages(BufferedImage[] images, int[] relX)
stitch Images
if (images == null || images.length < 2) {
    return null;
int[] xMax = max(relX);
int x0 = relX[0];
int xmax = xMax[0];
int xmaxIndex = xMax[1];
int W = xmax + images[xmaxIndex].getWidth();
...
BufferedImageswitchAxes(BufferedImage img)
switch Axes
if (img == null) {
    return null;
return rotateImage(flipImageHorizontal(img), -90, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
BufferedImagesymmetrifyY(BufferedImage image, boolean useFirstHalfImage, boolean flipVertical)
symmetrify Y
int halfWidth = image.getHeight() / 2;
int startReadPosition = 0;
int startWritePosition = 0;
int endWritePosition = image.getHeight() - 1;
if (!useFirstHalfImage)
    startReadPosition = halfWidth;
if (!useFirstHalfImage ^ flipVertical)
    startWritePosition = halfWidth;
    endWritePosition = halfWidth;
BufferedImage returned = new BufferedImage(image.getWidth(), image.getHeight(), image.getType());
for (int i = 0; i < image.getWidth(); i++) {
    for (int j = 0; j < image.getHeight() / 2; j++) {
        int color = image.getRGB(i, startReadPosition + j);
        returned.setRGB(i, startWritePosition + j, color);
        returned.setRGB(i, endWritePosition - j, color);
return returned;
booleantestNeighbours(BufferedImage source, int x, int y, boolean alpha)
test Neighbours
boolean up = testNeighbour(source, x, y - 1, alpha);
boolean down = testNeighbour(source, x, y + 1, alpha);
boolean left = testNeighbour(source, x - 1, y, alpha);
boolean right = testNeighbour(source, x + 1, y, alpha);
return up || down || left || right;
float[]texture2D(BufferedImage normalMap, int x, int y)
Acts like GLSL function of same name
int argb = normalMap.getRGB(x, y);
float[] vec4 = new float[4];
vec4[0] = ((argb >> 16) & 0xFF) / 255f;
vec4[1] = ((argb >> 8) & 0xFF) / 255f;
vec4[2] = ((argb) & 0xFF) / 255f;
vec4[3] = ((argb >> 24) & 0xFF) / 255f;
return vec4;
BufferedImagethresholdImage(BufferedImage image, int threshold)
Converts an image (RGB, RGBA, ...
BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
result.getGraphics().drawImage(image, 0, 0, null);
WritableRaster raster = result.getRaster();
int[] pixels = new int[image.getWidth()];
for (int y = 0; y < image.getHeight(); y++) {
    raster.getPixels(0, y, image.getWidth(), 1, pixels);
    for (int i = 0; i < pixels.length; i++) {
        if (pixels[i] < threshold)
...
BufferedImagetile(BufferedImage source, Rectangle r, GraphicsConfiguration conf)
Cut out a rectangular section of the target image and create a GraphicsConfiguration -compatible (but not volatile) copy.
final BufferedImage sub = source.getSubimage(r.x, r.y, r.width, r.height);
final BufferedImage tile = conf.createCompatibleImage(r.width, r.height, Transparency.BITMASK);
final Graphics2D g = (Graphics2D) tile.getGraphics();
g.drawImage(sub, null, 0, 0);
g.dispose();
return tile;