Here you can find the source of applyGrayscaleMaskToAlpha(BufferedImage image, BufferedImage mask)
public static BufferedImage applyGrayscaleMaskToAlpha(BufferedImage image, BufferedImage mask)
//package com.java2s; //License from project: Apache License import java.awt.image.BufferedImage; public class Main { public static BufferedImage applyGrayscaleMaskToAlpha(BufferedImage image, BufferedImage mask) { BufferedImage result = new BufferedImage(image.getWidth(), image.getHeight(), image.getType()); int width = image.getWidth(); int height = image.getHeight(); int[] imagePixels = image.getRGB(0, 0, width, height, null, 0, width); int[] maskPixels = mask.getRGB(0, 0, width, height, null, 0, width); for (int i = 0; i < imagePixels.length; i++) { int color = imagePixels[i] & 0x00ffffff; // Mask preexisting alpha int alpha = maskPixels[i] << 24; // Shift blue to alpha imagePixels[i] = color | alpha; }/*from w ww . ja v a 2 s . c o m*/ result.setRGB(0, 0, width, height, imagePixels, 0, width); return result; } }