Java Code Examples for java.awt.image.BufferedImage#isAlphaPremultiplied()

The following examples show how to use java.awt.image.BufferedImage#isAlphaPremultiplied() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ImageManufacture.java    From MyBox with Apache License 2.0 6 votes vote down vote up
public static BufferedImage premultipliedAlpha2(BufferedImage source,
        boolean removeAlpha) {
    try {
        if (source == null || !hasAlpha(source)
                || (source.isAlphaPremultiplied() && !removeAlpha)) {
            return source;
        }
        BufferedImage target = clone(source);
        target.coerceData(true);
        if (removeAlpha) {
            target = removeAlpha(target);
        }
        return target;
    } catch (Exception e) {
        logger.error(e.toString());
        return source;
    }
}
 
Example 2
Source File: PNGImageResizer.java    From entando-components with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Crea e restituisce un'immagine in base all'immagine master ed alle dimensioni massime consentite.
 * L'immagine risultante sarĂ  un'immagine rispettante le proporzioni dell'immagine sorgente.
 * @param imageIcon L'immagine sorgente.
 * @param dimensioneX la dimensione orizzontale massima.
 * @param dimensioneY La dimensione verticale massima.
 * @return L'immagine risultante.
 * @throws ApsSystemException In caso di errore.
 */
@Override
protected BufferedImage getResizedImage(ImageIcon imageIcon, int dimensioneX, int dimensioneY) throws ApsSystemException {
   	Image image = imageIcon.getImage();
   	BufferedImage bi = this.toBufferedImage(image);
   	double scale = this.computeScale(image.getWidth(null), image.getHeight(null), dimensioneX, dimensioneY);
	int scaledW = (int) (scale * image.getWidth(null));
	int scaledH = (int) (scale * image.getHeight(null));
	BufferedImage biRes = new BufferedImage(bi.getColorModel(), 
       		bi.getColorModel().createCompatibleWritableRaster(scaledW, scaledH), 
       		bi.isAlphaPremultiplied() , null);
       AffineTransform tx = new AffineTransform();
	tx.scale(scale, scale);
       Graphics2D bufImageGraphics = biRes.createGraphics();
       bufImageGraphics.drawImage(image, tx, null);
       return biRes;
}
 
Example 3
Source File: ImageUtils.java    From ocr-neuralnet with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a copy of the image.
 * @param image the image to copy.
 * @return the copy image.
 */
public static BufferedImage deepCopy(BufferedImage image) {
    return new BufferedImage(
            image.getColorModel(),
            image.copyData(null),
            image.isAlphaPremultiplied(),
            null);
}
 
Example 4
Source File: MultiplexImageReader.java    From orbit-image-analysis with GNU General Public License v3.0 4 votes vote down vote up
public BufferedImage[] openUnmixedImages(int no, int x, int y, int w, int h) throws FormatException, IOException {
    int[] zct = getZCTCoords(no);
    int z = zct[0];
    int chan = zct[1];
    int t = zct[2];
    // load all channels for current rect
    BufferedImage[] channels = new BufferedImage[sizeC];
    for (int c=0; c<sizeC; c++) {
        int no2 = getIndex(z,c,t);
        channels[c] = super.openImage(no2,x,y,w,h);
    }

    BufferedImage ori = channels[0];
    BufferedImage[] bi = new BufferedImage[sizeC];
    WritableRaster[] raster = new WritableRaster[sizeC];
    for (int c=0; c<sizeC; c++) {
        if (!channelIndependent[c]) {
            bi[c] = new BufferedImage(ori.getColorModel(), ori.getRaster().createCompatibleWritableRaster(0, 0, w, h), ori.isAlphaPremultiplied(), null);
            raster[c] = bi[c].getRaster();
        } else {
            bi[c] = channels[c];   // original image, independent channel
            raster[c] = bi[c].getRaster();
        }
    }
    double[] measurements = new double[sizeDependend];
    double[] out = new double[sizeDependend];
    for (int ix=ori.getMinX(); ix<ori.getMinX()+ori.getWidth(); ix++)
        for (int iy=ori.getMinY(); iy<ori.getMinY()+ori.getHeight(); iy++) {
            for (int c=0; c<sizeDependend; c++) {
                measurements[c] = channels[channelMap[c]].getRaster().getSampleDouble(ix,iy,0); // only 1 banded rasters allowed here
            }
            fastMultiply(invMatrix,measurements,out);   // here the real unmixing takes place
            for (int c=0; c<sizeDependend; c++) {
                if (out[c]>255) out[c] = 255d;        // TODO: adjust for 16bit!!!
                if (out[c]<0) out[c] = 0d;
                raster[channelMap[c]].setSample(ix, iy, 0, out[c]);
            }
        }
    unmixedChannels = bi;
    currentRect = new RectZT(x,y,w,h,z,t);

    return bi;
}
 
Example 5
Source File: Invert.java    From Pixelitor with GNU General Public License v3.0 4 votes vote down vote up
/**
     * The two arguments can point to the same image to invert an image in-place
     */
    public static void invertImage(BufferedImage src, BufferedImage dest) {
        int[] srcData = ImageUtils.getPixelsAsArray(src);
        int[] destData = ImageUtils.getPixelsAsArray(dest);

        boolean simple = !src.isAlphaPremultiplied();

        for (int i = 0; i < destData.length; i++) {
            int srcPixel = srcData[i];
//            int alpha = srcPixel & 0xFF000000;
            int a = (srcPixel >>> 24) & 0xFF;

            if (a == 255 || simple) {
                destData[i] = srcPixel ^ 0x00FFFFFF;  // invert the r, g, b values
            } else if (a == 0) {
                destData[i] = 0;
            } else {
                int r = (srcPixel >>> 16) & 0xFF;
                int g = (srcPixel >>> 8) & 0xFF;
                int b = srcPixel & 0xFF;

                // unpremultiply
                float f = 255.0f / a;
                int ur = (int) (r * f);
                int ug = (int) (g * f);
                int ub = (int) (b * f);

                if (ur > 255) {
                    ur = 255;
                }
                if (ug > 255) {
                    ug = 255;
                }
                if (ub > 255) {
                    ub = 255;
                }

                // invert
                ur = 255 - ur;
                ug = 255 - ug;
                ub = 255 - ub;

                // premultiply
                float f2 = a * (1.0f / 255.0f);
                r = (int) (ur * f2);
                g = (int) (ug * f2);
                b = (int) (ub * f2);

                r = PixelUtils.clamp(r);
                g = PixelUtils.clamp(g);
                b = PixelUtils.clamp(b);

                destData[i] = a << 24 | r << 16 | g << 8 | b;
            }
        }
    }