Example usage for java.awt.image BufferedImage getColorModel

List of usage examples for java.awt.image BufferedImage getColorModel

Introduction

In this page you can find the example usage for java.awt.image BufferedImage getColorModel.

Prototype

public ColorModel getColorModel() 

Source Link

Document

Returns the ColorModel .

Usage

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.geoserver.wms.wms_1_1_1.GetMapIntegrationTest.java

@Test
public void testTransparentPaletteTransparentOutput() throws Exception {
    String url = "wms?LAYERS=" + getLayerId(MockData.TASMANIA_DEM) + "&styles=demTranslucent&"
            + "FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1" + "&REQUEST=GetMap&SRS=EPSG%3A4326"
            + "&BBOX=145,-43,146,-41&WIDTH=100&HEIGHT=200&transparent=true";
    BufferedImage bi = getAsImage(url, "image/png");

    ColorModel cm = bi.getColorModel();
    assertTrue(cm instanceof IndexColorModel);
    assertEquals(Transparency.TRANSLUCENT, cm.getTransparency());

    // grab a pixel in the low left corner, should be transparent
    int[] pixel = new int[1];
    bi.getRaster().getPixel(4, 196, pixel);
    int[] color = new int[4];
    cm.getComponents(pixel[0], color, 0);
    assertEquals(0, color[3]);// ww  w .j av a2  s .  c  om

    // a pixel high enough to be solid, should be solid green
    bi.getRaster().getPixel(56, 49, pixel);
    cm.getComponents(pixel[0], color, 0);
    assertEquals(0, color[0]);
    assertEquals(255, color[1]);
    assertEquals(0, color[2]);
    assertEquals(255, color[3]);
}

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

@Test
public void testTransparentPaletteOpaqueOutput() throws Exception {
    String url = "wms?LAYERS=" + getLayerId(MockData.TASMANIA_DEM) + "&styles=demTranslucent&"
            + "FORMAT=image%2Fpng&SERVICE=WMS&VERSION=1.1.1" + "&REQUEST=GetMap&SRS=EPSG%3A4326"
            + "&BBOX=145,-43,146,-41&WIDTH=100&HEIGHT=200&bgcolor=0xFF0000";
    BufferedImage bi = getAsImage(url, "image/png");

    ColorModel cm = bi.getColorModel();
    assertTrue(cm instanceof IndexColorModel);
    assertEquals(Transparency.OPAQUE, cm.getTransparency());

    // grab a pixel in the low left corner, should be red (BG color)
    int[] pixel = new int[1];
    bi.getRaster().getPixel(4, 196, pixel);
    int[] color = new int[3];
    cm.getComponents(pixel[0], color, 0);
    assertEquals(255, color[0]);//from   w w w . ja  va  2s.  c o  m
    assertEquals(0, color[1]);
    assertEquals(0, color[2]);

    // a pixel high enough to be solid, should be fully green
    bi.getRaster().getPixel(56, 49, pixel);
    cm.getComponents(pixel[0], color, 0);
    assertEquals(0, color[0]);
    assertEquals(255, color[1]);
    assertEquals(0, color[2]);
}

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

@Test
public void testTransparentPaletteTransparentOutputPng8() throws Exception {
    String url = "wms?LAYERS=" + getLayerId(MockData.TASMANIA_DEM) + "&styles=demTranslucent&"
            + "FORMAT=image%2Fpng8&SERVICE=WMS&VERSION=1.1.1" + "&REQUEST=GetMap&SRS=EPSG%3A4326"
            + "&BBOX=145,-43,146,-41&WIDTH=100&HEIGHT=200&transparent=true";
    BufferedImage bi = getAsImage(url, "image/png; mode=8bit");

    ColorModel cm = bi.getColorModel();
    assertTrue(cm instanceof IndexColorModel);
    assertEquals(Transparency.TRANSLUCENT, cm.getTransparency());

    // grab a pixel in the low left corner, should be transparent
    int[] pixel = new int[1];
    bi.getRaster().getPixel(4, 196, pixel);
    int[] color = new int[4];
    cm.getComponents(pixel[0], color, 0);
    assertEquals(0, color[3]);/*w  ww  . j  a v  a  2 s  . c  o m*/

    // a pixel high enough to be solid, should be solid green
    bi.getRaster().getPixel(56, 49, pixel);
    cm.getComponents(pixel[0], color, 0);
    assertEquals(0, color[0]);
    assertEquals(255, color[1]);
    assertEquals(0, color[2]);
    assertEquals(255, color[3]);
}

From source file:com.github.dactiv.fear.service.service.account.AccountService.java

/**
 * ??/*w ww . j a  v a  2 s  . c o  m*/
 *
 * @param sourcePath   ?
 * @param targetPath   ??
 * @param portraitSize ?
 *
 * @throws IOException
 */
private String scaleImage(String sourcePath, String targetPath, PortraitSize portraitSize) throws IOException {
    InputStream inputStream = new FileInputStream(sourcePath);

    BufferedImage source = ImageIO.read(inputStream);
    ColorModel targetColorModel = source.getColorModel();

    inputStream.close();

    int width = portraitSize.getWidth();
    int height = portraitSize.getHeight();

    BufferedImage target = new BufferedImage(targetColorModel,
            targetColorModel.createCompatibleWritableRaster(width, height),
            targetColorModel.isAlphaPremultiplied(), null);

    Image scaleImage = source.getScaledInstance(width, height, Image.SCALE_SMOOTH);

    Graphics2D g = target.createGraphics();
    g.drawImage(scaleImage, 0, 0, width, height, null);
    g.dispose();

    String result = targetPath + portraitSize.getName();
    FileOutputStream fileOutputStream = new FileOutputStream(result);
    ImageIO.write(target, PORTRAIT_PIC_TYPE, fileOutputStream);

    fileOutputStream.close();

    return result;
}

From source file:com.googlecode.fightinglayoutbugs.helpers.ImageHelper.java

private static List<RectangularRegion> findSubImageInImage(BufferedImage subImage, BufferedImage image,
        int max) {
    Map<Integer, List<Point>> rgb2offsets = new HashMap<Integer, List<Point>>();
    int sw = subImage.getWidth();
    int sh = subImage.getHeight();
    for (int x = 0; x < sw; ++x) {
        for (int y = 0; y < sh; ++y) {
            int argb = subImage.getRGB(x, y);
            int a = argb >>> 24;
            if (a == 255) {
                Integer rgb = argb & 0xFFFFFF;
                List<Point> offsets = rgb2offsets.get(rgb);
                if (offsets == null) {
                    offsets = new ArrayList<Point>();
                    rgb2offsets.put(rgb, offsets);
                }//from www . ja  va  2s  .co m
                offsets.add(new Point(x, y));
            }
        }
    }
    List<RectangularRegion> result = new ArrayList<RectangularRegion>();
    int w = image.getWidth();
    int h = image.getHeight();
    int[][] p = new int[w][h];
    Raster raster = image.getRaster();
    if (raster.getTransferType() == DataBuffer.TYPE_BYTE) {
        byte[] bytes = (byte[]) raster.getDataElements(0, 0, w, h, null);
        int bytesPerPixel = (bytes.length / (w * h));
        ColorModel colorModel = image.getColorModel();
        byte[] buf = new byte[bytesPerPixel];
        for (int x = 0; x < w; ++x) {
            for (int y = 0; y < h; ++y) {
                System.arraycopy(bytes, (x + y * w) * bytesPerPixel, buf, 0, bytesPerPixel);
                p[x][y] = colorModel.getRGB(buf) & 0xFFFFFF;
            }
        }
    } else if (raster.getTransferType() == DataBuffer.TYPE_INT) {
        for (int x = 0; x < w; ++x) {
            p[x] = (int[]) raster.getDataElements(x, 0, 1, h, null);
        }
    } else {
        throw new RuntimeException("findSubImageInImage not implemented for image transfer type "
                + raster.getTransferType() + " yet.");
    }
    for (int x = 0; x < w; ++x) {
        for (int y = 0; y < h; ++y) {
            Iterator<Map.Entry<Integer, List<Point>>> i = rgb2offsets.entrySet().iterator();
            compareWithSubImageLoop: while (i.hasNext()) {
                Map.Entry<Integer, List<Point>> mapEntry = i.next();
                int expectedRgb = mapEntry.getKey();
                for (Point offset : mapEntry.getValue()) {
                    int xx = x + offset.x;
                    int yy = y + offset.y;
                    if (xx >= w || yy >= h || expectedRgb != p[xx][yy]) {
                        break compareWithSubImageLoop;
                    }
                }
                if (!i.hasNext()) {
                    result.add(new RectangularRegion(x, y, x + (sw - 1), y + (sh - 1)));
                    if (result.size() == max) {
                        return result;
                    }
                }
            }
        }
    }
    return result;
}

From source file:pl.edu.icm.visnow.lib.utils.ImageUtilities.java

public static BufferedImage[] split2RGBA(BufferedImage img) {
    int w, h, a, r, g, b, pixel;

    if (img.getType() == BufferedImage.TYPE_INT_ARGB) {
        w = img.getWidth();/*w  w  w . ja v a  2  s. co  m*/
        h = img.getHeight();
        BufferedImage[] out = new BufferedImage[4];
        WritableRaster[] rasters = new WritableRaster[4];
        for (int i = 0; i < 4; i++) {
            out[i] = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
            rasters[i] = out[i].getRaster();
        }

        ColorModel cm = img.getColorModel();
        for (int x = 0; x < w; x++) {
            for (int y = 0; y < h; y++) {
                pixel = img.getRGB(x, y);

                a = cm.getAlpha(pixel);
                r = cm.getRed(pixel);
                g = cm.getGreen(pixel);
                b = cm.getBlue(pixel);

                rasters[3].setSample(x, y, 0, a);
                rasters[0].setSample(x, y, 0, r);
                rasters[1].setSample(x, y, 0, g);
                rasters[2].setSample(x, y, 0, b);
            }
        }
        return out;
    } else {
        return null;
    }
}

From source file:org.pentaho.di.core.gui.SwingGC.java

private void drawImage(SwingUniversalImage image, int locationX, int locationY, int imageSize) {
    if (isDrawingPixelatedImages() && image.isBitmap()) {
        BufferedImage img = image.getAsBitmapForSize(imageSize, imageSize);
        ColorModel cm = img.getColorModel();
        Raster raster = img.getRaster();

        for (int x = 0; x < img.getWidth(observer); x++) {
            for (int y = 0; y < img.getHeight(observer); y++) {
                Object pix = raster.getDataElements(x, y, null);
                gc.setColor(new Color(cm.getRed(pix), cm.getGreen(pix), cm.getBlue(pix), cm.getAlpha(pix)));
                gc.setStroke(new BasicStroke(1.0f));
                gc.drawLine(locationX + xOffset + x, locationY + yOffset + y, locationX + xOffset + x + 1,
                        locationY + yOffset + y + 1);
            }/*from  w  ww.j a  va2s .  c o m*/
        }
    } else {
        image.drawToGraphics(gc, locationX, locationY, imageSize, imageSize);
    }
}

From source file:org.pentaho.di.core.gui.SwingGC.java

private void drawImage(SwingUniversalImage image, int centerX, int centerY, double angle, int imageSize) {
    if (isDrawingPixelatedImages() && image.isBitmap()) {
        BufferedImage img = image.getAsBitmapForSize(imageSize, imageSize, angle);
        ColorModel cm = img.getColorModel();
        Raster raster = img.getRaster();

        int offx = centerX + xOffset - img.getWidth() / 2;
        int offy = centerY + yOffset - img.getHeight() / 2;
        for (int x = 0; x < img.getWidth(observer); x++) {
            for (int y = 0; y < img.getHeight(observer); y++) {
                Object pix = raster.getDataElements(x, y, null);
                gc.setColor(new Color(cm.getRed(pix), cm.getGreen(pix), cm.getBlue(pix), cm.getAlpha(pix)));
                gc.setStroke(new BasicStroke(1.0f));
                gc.drawLine(offx + x, offy + y, offx + x + 1, offy + y + 1);
            }//w w  w .  j av a2  s  .  co m
        }
    } else {
        image.drawToGraphics(gc, centerX, centerY, imageSize, imageSize, angle);
    }
}

From source file:gov.lanl.adore.djatoka.plugin.ExtractPDF.java

/**
 * Returns PDF props in ImageRecord/*from ww w . j av a  2 s .c  o m*/
 * @param r ImageRecord containing absolute file path of PDF file.
 * @return a populated ImageRecord object
 * @throws DjatokaException
 */
@Override
public final ImageRecord getMetadata(ImageRecord r) throws DjatokaException {
    if ((r.getImageFile() == null || !new File(r.getImageFile()).exists()) && r.getObject() == null)
        throw new DjatokaException("Image Does Not Exist: " + r.toString());
    logger.debug("Get metadata: " + r.toString());
    try {
        DjatokaDecodeParam params = new DjatokaDecodeParam();
        BufferedImage bi = process(r, params);

        r.setWidth(bi.getWidth());
        r.setHeight(bi.getHeight());
        r.setDWTLevels(DEFAULT_LEVELS);
        r.setLevels(DEFAULT_LEVELS);
        r.setBitDepth(bi.getColorModel().getPixelSize());
        r.setNumChannels(bi.getColorModel().getNumColorComponents());

        //r.setCompositingLayerCount(getNumberOfPages(r)); // Semantics: number of pages in the PDF file.
        HashMap<String, String> pdfProps = (HashMap<String, String>) getPDFProperties(r);
        int n = Integer.parseInt(pdfProps.remove("Pages"));
        r.setCompositingLayerCount(n);

        // Since it is not possible for the viewer to query about a specific page's width and height
        // (because in Djatoka's point of view a PDF is just one image with various compositing layers, which are the pages),
        // at this point right here we query the PDF file about the size of all pages and store this
        // information in a Map. This map can be returned by getMetadata by setting it as the instProps member of the
        // ImageRecord class, which Djatoka already implements and which is returned as JSON to the viewer JS.
        // The viewer then has to store this information and later query it instead of asking Djatoka (getMetadata) again.
        //Map<String, String> instProps = getPagesSizes(r);
        r.setInstProps(pdfProps);
        logger.debug("instProps: " + r.getInstProps());

        logger.debug("Get metadata: " + r.toString());
    } catch (Exception e) {
        throw new DjatokaException(e);
    }

    return r;
}