Example usage for java.awt.image IndexColorModel getTransparentPixel

List of usage examples for java.awt.image IndexColorModel getTransparentPixel

Introduction

In this page you can find the example usage for java.awt.image IndexColorModel getTransparentPixel.

Prototype

public final int getTransparentPixel() 

Source Link

Document

Returns the index of a transparent pixel in this IndexColorModel or -1 if there is no pixel with an alpha value of 0.

Usage

From source file:GifEncoder.java

/** Constructs a new GifEncoder using an 8-bit AWT Image.
 The image is assumed to be fully loaded. */
public GifEncoder(Image img) {
    width = img.getWidth(null);//from  ww  w  .  ja va 2 s. co m
    height = img.getHeight(null);
    pixels = new byte[width * height];
    PixelGrabber pg = new PixelGrabber(img, 0, 0, width, height, false);
    try {
        pg.grabPixels();
    } catch (InterruptedException e) {
        System.err.println(e);
    }
    ColorModel cm = pg.getColorModel();
    if (cm instanceof IndexColorModel) {
        pixels = (byte[]) (pg.getPixels());
        // hpm
        IndexColorModel icm = (IndexColorModel) cm;
        setTransparentPixel(icm.getTransparentPixel());
    } else
        throw new IllegalArgumentException("IMAGE_ERROR");
    IndexColorModel m = (IndexColorModel) cm;
    int mapSize = m.getMapSize();
    r = new byte[mapSize];
    g = new byte[mapSize];
    b = new byte[mapSize];
    m.getReds(r);
    m.getGreens(g);
    m.getBlues(b);
    interlace = false;
    pixelIndex = 0;
    numPixels = width * height;
}

From source file:org.apache.fop.render.pdf.ImageRenderedAdapter.java

/** {@inheritDoc} */
@Override// www. ja va 2s.  co m
public PDFColor getTransparentColor() {
    ColorModel cm = getEffectiveColorModel();
    if (cm instanceof IndexColorModel) {
        IndexColorModel icm = (IndexColorModel) cm;
        if (cm.getTransparency() == IndexColorModel.TRANSLUCENT) {
            int transPixel = icm.getTransparentPixel();
            return new PDFColor(icm.getRed(transPixel), icm.getGreen(transPixel), icm.getBlue(transPixel));
        }
    }
    return new PDFColor(getImage().getTransparentColor());
}

From source file:org.geoserver.wms.wms_1_1_1.GetMapIntegrationTest.java

@Test
public void testPng8Opaque() throws Exception {
    MockHttpServletResponse response = getAsServletResponse("wms?bbox=" + bbox + "&styles=&layers=" + layers
            + "&Format=image/png8" + "&request=GetMap" + "&width=550" + "&height=250" + "&srs=EPSG:4326");
    assertEquals("image/png; mode=8bit", response.getContentType());
    assertEquals("inline; filename=sf-states.png", response.getHeader("Content-Disposition"));

    InputStream is = getBinaryInputStream(response);
    BufferedImage bi = ImageIO.read(is);
    IndexColorModel cm = (IndexColorModel) bi.getColorModel();
    assertEquals(Transparency.OPAQUE, cm.getTransparency());
    assertEquals(-1, cm.getTransparentPixel());
}

From source file:org.geoserver.wms.wms_1_1_1.GetMapIntegrationTest.java

@Test
public void testPng8ForceBitmask() throws Exception {
    MockHttpServletResponse response = getAsServletResponse("wms?bbox=" + bbox + "&styles=&layers=" + layers
            + "&Format=image/png8" + "&request=GetMap" + "&width=550" + "&height=250"
            + "&srs=EPSG:4326&transparent=true&format_options=quantizer:octree");
    assertEquals("image/png; mode=8bit", response.getContentType());
    assertEquals("inline; filename=sf-states.png", response.getHeader("Content-Disposition"));

    InputStream is = getBinaryInputStream(response);
    BufferedImage bi = ImageIO.read(is);
    IndexColorModel cm = (IndexColorModel) bi.getColorModel();
    assertEquals(Transparency.BITMASK, cm.getTransparency());
    assertTrue(cm.getTransparentPixel() >= 0);
}

From source file:org.geotools.utils.imagemosaic.MosaicIndexBuilder.java

/**
 * This method checks the {@link ColorModel} of the current image with the
 * one of the first image in order to check if they are compatible or not in
 * order to perform a mosaic operation./*from www. java2s . c  om*/
 * 
 * <p>
 * It is worth to point out that we also check if, in case we have two index
 * color model image, we also try to suggest whether or not we should do a
 * color expansion.
 * 
 * @param defaultCM
 * @param defaultPalette
 * @param actualCM
 * @return a boolean asking to skip this feature.
 */
private boolean checkColorModels(ColorModel defaultCM, byte[][] defaultPalette, ColorModel actualCM) {

    // /////////////////////////////////////////////////////////////////////
    //
    //
    // ComponentColorModel
    //
    //
    // /////////////////////////////////////////////////////////////////////
    if (defaultCM instanceof ComponentColorModel && actualCM instanceof ComponentColorModel) {
        final ComponentColorModel defCCM = (ComponentColorModel) defaultCM,
                actualCCM = (ComponentColorModel) actualCM;
        return !(defCCM.getNumColorComponents() == actualCCM.getNumColorComponents()
                && defCCM.hasAlpha() == actualCCM.hasAlpha()
                && defCCM.getColorSpace().equals(actualCCM.getColorSpace())
                && defCCM.getTransparency() == actualCCM.getTransparency()
                && defCCM.getTransferType() == actualCCM.getTransferType());
    }
    // /////////////////////////////////////////////////////////////////////
    //
    //
    // IndexColorModel
    //
    //
    // /////////////////////////////////////////////////////////////////////
    if (defaultCM instanceof IndexColorModel && actualCM instanceof IndexColorModel) {
        final IndexColorModel defICM = (IndexColorModel) defaultCM, actualICM = (IndexColorModel) actualCM;
        if (defICM.getNumColorComponents() != actualICM.getNumColorComponents()
                || defICM.hasAlpha() != actualICM.hasAlpha()
                || !defICM.getColorSpace().equals(actualICM.getColorSpace())
                || defICM.getTransferType() != actualICM.getTransferType())
            return true;
        // ///
        //
        // Suggesting expansion in the simplest case
        //
        // ///
        if (defICM.getMapSize() != actualICM.getMapSize()
                || defICM.getTransparency() != actualICM.getTransparency()
                || defICM.getTransferType() != actualICM.getTransferType()
                || defICM.getTransparentPixel() != actualICM.getTransparentPixel()) {
            mustConvertToRGB = true;
            return false;
        }
        // //
        //
        // Now checking palettes to see if we need to do a color convert
        //
        // //
        // get the palette for this color model
        int numBands = actualICM.getNumColorComponents();
        byte[][] actualPalette = new byte[3][actualICM.getMapSize()];
        actualICM.getReds(actualPalette[0]);
        actualICM.getGreens(actualPalette[0]);
        actualICM.getBlues(actualPalette[0]);
        if (numBands == 4)
            actualICM.getAlphas(defaultPalette[0]);
        // compare them
        for (int i = 0; i < defICM.getMapSize(); i++)
            for (int j = 0; j < numBands; j++)
                if (actualPalette[j][i] != defaultPalette[j][i]) {
                    mustConvertToRGB = true;
                    break;
                }
        return false;

    }
    // //
    //
    // if we get here this means that the two color models where completely
    // different, hence skip this feature.
    //
    // //
    return true;
}