Java BufferedImage Operation applyGrayscaleMaskToAlpha(BufferedImage image, BufferedImage mask)

Here you can find the source of applyGrayscaleMaskToAlpha(BufferedImage image, BufferedImage mask)

Description

apply Grayscale Mask To Alpha

License

Apache License

Declaration

public static BufferedImage applyGrayscaleMaskToAlpha(BufferedImage image, BufferedImage mask) 

Method Source Code

//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;
    }
}

Related

  1. appendImages(BufferedImage leftImage, BufferedImage rightImage)
  2. applyAlphaMask(BufferedImage buffer, BufferedImage alphaMask, int imgHeight)
  3. applyExplicitSMask(BufferedImage baseImage, BufferedImage sMaskImage)
  4. applyGrayDecode(BufferedImage rgbImage, int bitsPerComponent, float[] decode)
  5. applyMask(BufferedImage img, Color keyColor)
  6. applyMaskImage(BufferedImage src, int x, int y, int w, int h)
  7. applyShadow(BufferedImage image)
  8. areBufferedImagesEqual(BufferedImage img1, BufferedImage img2)