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

BufferedImagegradientMask(BufferedImage img, float start, float stop)
Draws linear alpha mask on source image.
BufferedImage result = new BufferedImage(img.getWidth(), img.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics2D g2 = result.createGraphics();
g2.drawImage(img, 0, 0, null);
GradientPaint paint = new GradientPaint(0, img.getHeight(), new Color(1.0f, 1.0f, 1.0f, start), 0, 0,
        new Color(1.0f, 1.0f, 1.0f, stop), false); 
g2.setComposite(AlphaComposite.DstIn);
g2.setPaint(paint);
g2.fillRect(0, 0, img.getWidth(), img.getHeight());
...
BufferedImagegreyScale(BufferedImage image)
turns a Buffered Image greyscale
BufferedImage gs = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
Graphics2D g2 = (Graphics2D) gs.getGraphics();
g2.drawImage(image, 0, 0, null);
g2.dispose();
return gs;
voidgrid(BufferedImage image, int between)
grid
Graphics g = image.getGraphics();
g.setColor(Color.gray.brighter());
for (int x = 0; x < image.getWidth(); x += between) {
    g.drawRect(x, 0, 0, image.getHeight());
for (int y = 0; y < image.getHeight(); y += between) {
    g.drawRect(0, y, image.getWidth(), 0);
doubleheight(BufferedImage image, Double measurementLatitude, Double measurementLongitude)
This function converts a measurementLatitude, measurementLongitude into a pixel co-ordinate and reads the associated pixel and returns the red value of the pixel - representing height - 0 is low, 255 is high
int w = image.getWidth();
int h = image.getHeight();
int zeroLat = h / 2;
int zeroLong = w / 2;
Double degreeXPerPixel = (double) w / 360;
Double degreeYPerPixel = (double) h / 180;
int y = zeroLat + ((int) Math.round(measurementLatitude * degreeYPerPixel * -1));
int x = zeroLong + ((int) Math.round(measurementLongitude * degreeXPerPixel));
...
BufferedImagehighlight(BufferedImage img, Color source, Color dest)
Turn the BufferedImage instance color components from source to destination for highlighting.
img = deepCopy(img);
short[] r = new short[256];
short[] g = new short[256];
short[] b = new short[256];
short[] a = new short[256];
for (int i = 0; i < 256; i++) {
    r[i] = (short) i;
    g[i] = (short) i;
...
booleanhitTest(BufferedImage image, int x, int y)
Returns true if the supplied image contains a non-transparent pixel at the specified coordinates, false otherwise.
int argb = image.getRGB(x, y);
return (argb >> 24) != 0;
BufferedImagehueShift(BufferedImage image, int hue)
changes the hue of image.
BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
float ihue = hue / 360.0f;
for (int y = 0; y < image.getHeight(); y++) {
    for (int x = 0; x < image.getWidth(); x++) {
        int RGB = image.getRGB(x, y);
        int R = (RGB >> 16) & 0xff;
        int G = (RGB >> 8) & 0xff;
        int B = (RGB) & 0xff;
...
floatinColormap(float[][] in, float min, float max, float[][] colormap, BufferedImage b)
in Colormap
int width = in[0].length;
int height = in.length;
int cvals = colormap.length;
float[] bData = ((DataBufferFloat) b.getRaster().getDataBuffer()).getData();
for (int yy = 0; yy < height; yy++) {
    for (int xx = 0; xx < width; xx++) {
        int cmapIdx = (int) (cvals * (in[yy][xx] - min) / max);
        bData[yy * width + xx] = colormap[cmapIdx][0];
...
BufferedImageindexToDirectColorModel(BufferedImage image)
index To Direct Color Model
BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(),
        BufferedImage.TYPE_BYTE_BINARY);
Graphics2D g2 = result.createGraphics();
g2.drawImage(image, null, null);
return result;
voidintBuffer2BufferedImage(IntBuffer ib, BufferedImage img)
Convert IntBuffer to BufferedImage.
int[] rgbArray = new int[ib.capacity()];
ib.rewind();
ib.get(rgbArray);
img.setRGB(0, 0, img.getWidth(), img.getHeight(), rgbArray, 0, img.getWidth());