Java BufferedImage Operation applyExplicitSMask(BufferedImage baseImage, BufferedImage sMaskImage)

Here you can find the source of applyExplicitSMask(BufferedImage baseImage, BufferedImage sMaskImage)

Description

apply Explicit S Mask

License

Apache License

Declaration

public static BufferedImage applyExplicitSMask(BufferedImage baseImage,
        BufferedImage sMaskImage) 

Method Source Code

//package com.java2s;
/*/*from  w  w w .j  a  v  a  2s  .  c o  m*/
 * Copyright 2006-2014 ICEsoft Technologies Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS
 * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

import java.awt.*;

import java.awt.geom.AffineTransform;
import java.awt.image.*;

public class Main {
    private static boolean scaleQuality;

    public static BufferedImage applyExplicitSMask(BufferedImage baseImage,
            BufferedImage sMaskImage) {

        // check to make sure the mask and the image are the same size.
        BufferedImage[] images = scaleImagesToSameSize(baseImage,
                sMaskImage);
        baseImage = images[0];
        sMaskImage = images[1];
        // apply the mask by simply painting white to the base image where
        // the mask specified no colour.
        int baseWidth = baseImage.getWidth();
        int baseHeight = baseImage.getHeight();

        BufferedImage argbImage;
        if (hasAlpha(baseImage)) {
            argbImage = baseImage;
        } else {
            argbImage = new BufferedImage(baseWidth, baseHeight,
                    BufferedImage.TYPE_INT_ARGB);
        }
        int[] srcBand = new int[baseWidth];
        int[] sMaskBand = new int[baseWidth];
        // iterate over each band to apply the mask
        for (int i = 0; i < baseHeight; i++) {
            baseImage.getRGB(0, i, baseWidth, 1, srcBand, 0, baseWidth);
            sMaskImage.getRGB(0, i, baseWidth, 1, sMaskBand, 0, baseWidth);
            // apply the soft mask blending
            for (int j = 0; j < baseWidth; j++) {
                if (sMaskBand[j] != -1 || sMaskBand[j] != 0xffffff
                        || sMaskBand[j] != 0) {
                    sMaskBand[j] = ((sMaskBand[j] & 0xff) << 24)
                            | (srcBand[j] & ~0xff000000);
                }
            }
            argbImage.setRGB(0, i, baseWidth, 1, sMaskBand, 0, baseWidth);
        }
        baseImage.flush();
        baseImage = argbImage;

        return baseImage;
    }

    /**
     * Utility method to scale the two provided images. There are two modes based
     * on the system property "".  The d
     *
     * @param baseImage base image that mask will be applied to
     * @param maskImage mask image that will be applied to base image.
     * @return array of altered baseImage and maskImage, should be same size on
     * return.
     */
    public static BufferedImage[] scaleImagesToSameSize(
            BufferedImage baseImage, BufferedImage maskImage) {
        if (scaleQuality) {
            int width = baseImage.getWidth();
            int height = baseImage.getHeight();

            WritableRaster maskRaster = maskImage.getRaster();
            int maskWidth = maskRaster.getWidth();
            int maskHeight = maskRaster.getHeight();
            // scale the image to match the image mask.
            if (width < maskWidth || height < maskHeight) {
                // calculate scale factors.
                double scaleX = maskWidth / (double) width;
                double scaleY = maskHeight / (double) height;
                // scale the mask to match the base image.
                AffineTransform tx = new AffineTransform();
                tx.scale(scaleX, scaleY);
                AffineTransformOp op = new AffineTransformOp(tx,
                        AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
                BufferedImage bim = op.filter(baseImage, null);
                baseImage.flush();
                baseImage = bim;
            } else if (width > maskWidth || height > maskHeight) {
                // calculate scale factors.
                double scaleX = width / (double) maskWidth;
                double scaleY = height / (double) maskHeight;
                // scale the mask to match the base image.
                AffineTransform tx = new AffineTransform();
                tx.scale(scaleX, scaleY);
                AffineTransformOp op = new AffineTransformOp(tx,
                        AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
                BufferedImage bim = op.filter(maskImage, null);
                maskImage.flush();
                maskImage = bim;
            }
            return new BufferedImage[] { baseImage, maskImage };
        } else {
            int width = baseImage.getWidth();
            int height = baseImage.getHeight();

            WritableRaster maskRaster = maskImage.getRaster();
            int maskWidth = maskRaster.getWidth();
            int maskHeight = maskRaster.getHeight();
            // scale the mask to match the smaller image.
            if (width < maskWidth || height < maskHeight) {
                // calculate scale factors.
                //                BufferedImage bim = (BufferedImage)
                //                        ImageUtility.getTrilinearScaledInstance(maskImage, width, height);
                double scaleX = width / (double) maskWidth;
                double scaleY = height / (double) maskHeight;
                // scale the mask to match the base image.
                AffineTransform tx = new AffineTransform();
                tx.scale(scaleX, scaleY);
                AffineTransformOp op = new AffineTransformOp(tx,
                        AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
                BufferedImage bim = op.filter(maskImage, null);
                maskImage.flush();
                maskImage = bim;
            }
            return new BufferedImage[] { baseImage, maskImage };
        }
    }

    public static boolean hasAlpha(Image image) {
        // If buffered image, the color model is readily available
        if (image instanceof BufferedImage) {
            BufferedImage bufferedImage = (BufferedImage) image;
            return bufferedImage.getColorModel().hasAlpha();
        }
        // Use a pixel grabber to retrieve the image's color model;
        // grabbing a single pixel is usually sufficient
        PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, 1, 1,
                false);
        try {
            pixelGrabber.grabPixels();
        } catch (InterruptedException e) {
            // fail quietly
        }
        // Get the image's color model
        ColorModel cm = pixelGrabber.getColorModel();
        return cm == null || cm.hasAlpha();
    }
}

Related

  1. appendImages(BufferedImage leftImage, BufferedImage rightImage)
  2. applyAlphaMask(BufferedImage buffer, BufferedImage alphaMask, int imgHeight)
  3. applyGrayDecode(BufferedImage rgbImage, int bitsPerComponent, float[] decode)
  4. applyGrayscaleMaskToAlpha(BufferedImage image, BufferedImage mask)
  5. applyMask(BufferedImage img, Color keyColor)
  6. applyMaskImage(BufferedImage src, int x, int y, int w, int h)