Example usage for java.awt.image ColorModel isAlphaPremultiplied

List of usage examples for java.awt.image ColorModel isAlphaPremultiplied

Introduction

In this page you can find the example usage for java.awt.image ColorModel isAlphaPremultiplied.

Prototype

boolean isAlphaPremultiplied

To view the source code for java.awt.image ColorModel isAlphaPremultiplied.

Click Source Link

Usage

From source file:ImageUtil.java

/**
 * create new image from source image// w  ww  . j a  v  a  2 s . c  o m
 * @param srcImg source image
 * @param targetWidth target image width
 * @param targetHeight target image height
 * @return new image with specify width and height
 */
public static BufferedImage createNewImage(BufferedImage srcImg, int targetWidth, int targetHeight) {
    BufferedImage targetImg = null;
    int type = srcImg.getType();
    if (type == BufferedImage.TYPE_CUSTOM) {
        ColorModel cm = srcImg.getColorModel();
        WritableRaster raster = cm.createCompatibleWritableRaster(targetWidth, targetHeight);
        boolean alphaPremultiplied = cm.isAlphaPremultiplied();
        targetImg = new BufferedImage(cm, raster, alphaPremultiplied, null);
    } else {
        targetImg = new BufferedImage(targetWidth, targetHeight, type);
    }
    return targetImg;
}

From source file:com.fengduo.bee.service.impl.file.FileServiceImpl.java

/**
 * ?/*from www . ja  v a2  s .c  om*/
 * 
 * @param source
 * @param targetW
 * @param targetH
 * @param ifScaling ?
 * @return
 */
public static BufferedImage resize(BufferedImage source, int targetW, int targetH, boolean ifScaling) {
    // targetWtargetH
    int type = source.getType();
    BufferedImage target = null;
    double sx = (double) targetW / source.getWidth();
    double sy = (double) targetH / source.getHeight();
    // targetWtargetH??,?if else???
    if (ifScaling) {
        if (sx > sy) {
            sx = sy;
            targetW = (int) (sx * source.getWidth());
        } else {
            sy = sx;
            targetH = (int) (sy * source.getHeight());
        }
    }
    if (type == BufferedImage.TYPE_CUSTOM) { // handmade
        ColorModel cm = source.getColorModel();
        WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
        boolean alphaPremultiplied = cm.isAlphaPremultiplied();
        target = new BufferedImage(cm, raster, alphaPremultiplied, null);
    } else
        target = new BufferedImage(targetW, targetH, type);
    Graphics2D g = target.createGraphics();
    // smoother than exlax:
    g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
    g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
    g.dispose();
    return target;
}

From source file:oct.util.Util.java

public static BufferedImage deepCopyBufferedImage(BufferedImage bi) {
    ColorModel cm = bi.getColorModel();
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    WritableRaster raster = bi.copyData(null);
    return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}

From source file:GraphicsUtil.java

/**
 * Create a new ColorModel with it's alpha premultiplied state matching
 * newAlphaPreMult.//from ww  w .j ava 2  s . co  m
 * @param cm The ColorModel to change the alpha premult state of.
 * @param newAlphaPreMult The new state of alpha premult.
 * @return   A new colorModel that has isAlphaPremultiplied()
 *           equal to newAlphaPreMult.
 */
public static ColorModel coerceColorModel(ColorModel cm, boolean newAlphaPreMult) {
    if (cm.isAlphaPremultiplied() == newAlphaPreMult)
        return cm;

    // Easiest way to build proper colormodel for new Alpha state...
    // Eventually this should switch on known ColorModel types and
    // only fall back on this hack when the CM type is unknown.
    WritableRaster wr = cm.createCompatibleWritableRaster(1, 1);
    return cm.coerceData(wr, newAlphaPreMult);
}

From source file:lucee.runtime.img.ImageUtil.java

public static BufferedImage createBufferedImage(BufferedImage image, int columns, int rows) {
    ColorModel colormodel = image.getColorModel();
    BufferedImage newImage;//from  w w w  . ja v a 2 s  .  c om
    if (colormodel instanceof IndexColorModel) {
        if (colormodel.getTransparency() != 1)
            newImage = new BufferedImage(columns, rows, 2);
        else
            newImage = new BufferedImage(columns, rows, 1);
    } else {
        newImage = new BufferedImage(colormodel,
                image.getRaster().createCompatibleWritableRaster(columns, rows),
                colormodel.isAlphaPremultiplied(), null);
    }
    return newImage;
}

From source file:GraphicsUtil.java

/**
 * Coerces data within a bufferedImage to match newAlphaPreMult,
 * Note that this can not change the colormodel of bi so you
 *
 * @param wr The raster to change the state of.
 * @param cm The colormodel currently associated with data in wr.
 * @param newAlphaPreMult The desired state of alpha Premult for raster.
 * @return A new colormodel that matches newAlphaPreMult.
 *///from ww  w .j  av  a  2  s. co  m
public static ColorModel coerceData(WritableRaster wr, ColorModel cm, boolean newAlphaPreMult) {

    // System.out.println("CoerceData: " + cm.isAlphaPremultiplied() +
    //                    " Out: " + newAlphaPreMult);
    if (cm.hasAlpha() == false)
        // Nothing to do no alpha channel
        return cm;

    if (cm.isAlphaPremultiplied() == newAlphaPreMult)
        // nothing to do alpha state matches...
        return cm;

    // System.out.println("CoerceData: " + wr.getSampleModel());

    if (newAlphaPreMult) {
        multiplyAlpha(wr);
    } else {
        divideAlpha(wr);
    }

    return coerceColorModel(cm, newAlphaPreMult);
}

From source file:GraphicsUtil.java

/**
 * Coerces data within a bufferedImage to match newAlphaPreMult,
 * Note that this can not change the colormodel of bi so you
 *
 * @param wr The raster to change the state of.
 * @param cm The colormodel currently associated with data in wr.
 * @param newAlphaPreMult The desired state of alpha Premult for raster.
 * @return A new colormodel that matches newAlphaPreMult.
 *///from www  .j  a v a2s .  c  o  m
public static ColorModel coerceData(WritableRaster wr, ColorModel cm, boolean newAlphaPreMult) {

    // System.out.println("CoerceData: " + cm.isAlphaPremultiplied() +
    //                    " Out: " + newAlphaPreMult);
    if (!cm.hasAlpha())
        // Nothing to do no alpha channel
        return cm;

    if (cm.isAlphaPremultiplied() == newAlphaPreMult)
        // nothing to do alpha state matches...
        return cm;

    // System.out.println("CoerceData: " + wr.getSampleModel());

    if (newAlphaPreMult) {
        multiplyAlpha(wr);
    } else {
        divideAlpha(wr);
    }

    return coerceColorModel(cm, newAlphaPreMult);
}

From source file:ImageOpByRomain.java

/**
 * <p>/* w ww .ja  v a2  s.  c om*/
 * Returns a new <code>BufferedImage</code> using the same color model as
 * the image passed as a parameter. The returned image is only compatible with
 * the image passed as a parameter. This does not mean the returned image is
 * compatible with the hardware.
 * </p>
 * 
 * @param image
 *            the reference image from which the color model of the new image
 *            is obtained
 * @return a new <code>BufferedImage</code>, compatible with the color
 *         model of <code>image</code>
 */
public static BufferedImage createColorModelCompatibleImage(BufferedImage image) {
    ColorModel cm = image.getColorModel();
    return new BufferedImage(cm, cm.createCompatibleWritableRaster(image.getWidth(), image.getHeight()),
            cm.isAlphaPremultiplied(), null);
}

From source file:app.springapp.ImageConverter.java

BufferedImage deepCopy(BufferedImage bi) {
    ColorModel cm = bi.getColorModel();
    boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
    WritableRaster raster = bi.copyData(null);
    return new BufferedImage(cm, raster, isAlphaPremultiplied, null);
}

From source file:com.dianping.imcaptcha.strategy.WaveFilter.java

public BufferedImage filter(BufferedImage src, BufferedImage dst) {
    int width = src.getWidth();
    int height = src.getHeight();
    int type = src.getType();
    WritableRaster srcRaster = src.getRaster();

    originalSpace = new Rectangle(0, 0, width, height);
    transformedSpace = new Rectangle(0, 0, width, height);
    transformSpace(transformedSpace);/*from  w  ww  .  java 2  s  .  c  o m*/

    if (dst == null) {
        ColorModel dstCM = src.getColorModel();
        dst = new BufferedImage(dstCM,
                dstCM.createCompatibleWritableRaster(transformedSpace.width, transformedSpace.height),
                dstCM.isAlphaPremultiplied(), null);
    }
    WritableRaster dstRaster = dst.getRaster();

    int[] inPixels = getRGB(src, 0, 0, width, height, null);

    if (interpolation == NEAREST_NEIGHBOUR)
        return filterPixelsNN(dst, width, height, inPixels, transformedSpace);

    int srcWidth = width;
    int srcHeight = height;
    int srcWidth1 = width - 1;
    int srcHeight1 = height - 1;
    int outWidth = transformedSpace.width;
    int outHeight = transformedSpace.height;
    int outX, outY;
    int index = 0;
    int[] outPixels = new int[outWidth];
    float radius = srcHeight * 1.0f / 2 / (float) Math.PI;
    outX = transformedSpace.x;
    outY = transformedSpace.y;
    float[] out = new float[2];

    for (int y = 0; y < outHeight; y++) {
        for (int x = 0; x < outWidth; x++) {
            transformInverse(outX + x, outY + y, out, radius);
            int srcX = (int) Math.floor(out[0]);
            int srcY = (int) Math.floor(out[1]);
            float xWeight = out[0] - srcX;
            float yWeight = out[1] - srcY;
            int nw, ne, sw, se;

            if (srcX >= 0 && srcX < srcWidth1 && srcY >= 0 && srcY < srcHeight1) {
                // Easy case, all corners are in the image
                int i = srcWidth * srcY + srcX;
                nw = inPixels[i];
                ne = inPixels[i + 1];
                sw = inPixels[i + srcWidth];
                se = inPixels[i + srcWidth + 1];
            } else {
                // Some of the corners are off the image
                nw = getPixel(inPixels, srcX, srcY, srcWidth, srcHeight);
                ne = getPixel(inPixels, srcX + 1, srcY, srcWidth, srcHeight);
                sw = getPixel(inPixels, srcX, srcY + 1, srcWidth, srcHeight);
                se = getPixel(inPixels, srcX + 1, srcY + 1, srcWidth, srcHeight);
            }
            outPixels[x] = ImageMath.bilinearInterpolate(xWeight, yWeight, nw, ne, sw, se);
        }
        setRGB(dst, 0, y, transformedSpace.width, 1, outPixels);
    }
    return dst;
}