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:pl.dp.bz.poid.fouriertest.FourierProc.java

public static BufferedImage copyImage(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.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * Calculates and returns band histograms of a BufferedImage
 *
 * @param image//from  w w  w . j a v a 2  s . c  o  m
 * @return
 */
public static int[][] getChannelHistograms(BufferedImage image) {
    WritableRaster raster = image.getRaster();
    int[][] histograms = new int[raster
            .getNumBands()][(int) (Math.pow(2, image.getColorModel().getPixelSize()) - 1)];

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

    for (int row = 0; row < height; row++) {
        for (int col = 0; col < width; col++) {
            for (int band = 0; band < raster.getNumBands(); band++) {
                histograms[band][raster.getSample(col, row, band)]++;
            }
        }
    }

    return histograms;
}

From source file:com.github.pitzcarraldo.dissimilar.Dissimilar.java

/**
 * Write an image showing the heatmap of ssim values, per window
 * @param pValues sequence of SSIM values
 * @param pHeight number of SSIM windows across
 * @param pWidth number of SSIM windows down
 * @param pFilename filename to save the image to (png)
 *///  w ww. ja v  a  2s .c o  m
private static BufferedImage dumpSSIMHeatMap(final double[] pValues, final int pHeight, final int pWidth,
        final String pFilename) {
    BufferedImage heatMap = new BufferedImage(pWidth, pHeight, BufferedImage.TYPE_USHORT_GRAY);

    final int maxPixelValue = (int) Math.pow(2, heatMap.getColorModel().getPixelSize()) - 1;

    int pixel = 0;
    for (int height = 0; height < pHeight; height++) {
        for (int width = 0; width < pWidth; width++) {
            pixel = ((int) (maxPixelValue * pValues[(height * pWidth) + width]));
            if (pixel < 0) {
                pixel = 0;
            }
            heatMap.setRGB(width, height, pixel);
        }
    }

    //only write to file if filename is non-null
    if (pFilename != null) {
        try {
            ImageIO.write(heatMap, "png", new File(pFilename));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    return heatMap;
}

From source file:org.esa.snap.rcp.statistics.DensityPlotPanel.java

private static byte[] getValidData(BufferedImage image) {
    if (image != null && image.getColorModel() instanceof IndexColorModel
            && image.getData().getDataBuffer() instanceof DataBufferByte) {
        return ((DataBufferByte) image.getData().getDataBuffer()).getData();
    }//  w  ww . jav  a2 s .  c o m
    return null;
}

From source file:org.sejda.sambox.pdmodel.graphics.image.JPEGFactory.java

private static PDImageXObject createJPEG(BufferedImage image, float quality, int dpi) throws IOException {
    // extract alpha channel (if any)
    BufferedImage awtColorImage = getColorImage(image);
    BufferedImage awtAlphaImage = getAlphaImage(image);

    // create XObject
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    encodeImageToJPEGStream(awtColorImage, quality, dpi, baos);
    ByteArrayInputStream byteStream = new ByteArrayInputStream(baos.toByteArray());

    PDImageXObject pdImage = new PDImageXObject(byteStream, COSName.DCT_DECODE, awtColorImage.getWidth(),
            awtColorImage.getHeight(), awtColorImage.getColorModel().getComponentSize(0),
            getColorSpaceFromAWT(awtColorImage));

    // alpha -> soft mask
    if (awtAlphaImage != null) {
        PDImage xAlpha = JPEGFactory.createFromImage(awtAlphaImage, quality);
        pdImage.getCOSObject().setItem(COSName.SMASK, xAlpha);
    }/*  w w w . j a va 2  s .c  o  m*/

    return pdImage;
}

From source file:org.deegree.framework.util.ImageUtils.java

/**
 *
 *
 * @param out//from   www. ja va2s. com
 * @param img
 *
 * @throws IOException
 */
private static void encodePng(OutputStream out, BufferedImage img) throws IOException {
    PNGEncodeParam encodeParam = PNGEncodeParam.getDefaultEncodeParam(img);

    if (encodeParam instanceof PNGEncodeParam.Palette) {
        PNGEncodeParam.Palette p = (PNGEncodeParam.Palette) encodeParam;
        byte[] b = new byte[] { -127 };
        p.setPaletteTransparency(b);
    }

    com.sun.media.jai.codec.ImageEncoder encoder = ImageCodec.createImageEncoder("PNG", out, encodeParam);
    encoder.encode(img.getData(), img.getColorModel());
}

From source file:ImageOpByRomain.java

/**
 * <p>/*from   w ww  . ja va  2 s .com*/
 * Return a new compatible image that contains a copy of the specified image.
 * This method ensures an image is compatible with the hardware, and therefore
 * optimized for fast blitting operations.
 * </p>
 * 
 * @see #createCompatibleImage(java.awt.image.BufferedImage)
 * @see #createCompatibleImage(java.awt.image.BufferedImage, int, int)
 * @see #createCompatibleImage(int, int)
 * @see #createTranslucentCompatibleImage(int, int)
 * @see #loadCompatibleImage(java.net.URL)
 * @param image
 *            the image to copy into a new compatible image
 * @return a new compatible copy, with the same width and height and
 *         transparency and content, of <code>image</code>
 */
public static BufferedImage toCompatibleImage(BufferedImage image) {
    if (image.getColorModel().equals(CONFIGURATION.getColorModel())) {
        return image;
    }

    BufferedImage compatibleImage = CONFIGURATION.createCompatibleImage(image.getWidth(), image.getHeight(),
            image.getTransparency());
    Graphics g = compatibleImage.getGraphics();
    g.drawImage(image, 0, 0, null);
    g.dispose();

    return compatibleImage;
}

From source file:ImageOpByRomain.java

/**
 * <p>/*from   w  w  w  .j av 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:com.googlecode.fightinglayoutbugs.helpers.ImageHelper.java

public static int[][] imageToPixels(BufferedImage image) {
    if (image == null) {
        return null;
    }//w  ww .ja va 2  s.c o  m
    int w = image.getWidth();
    int h = image.getHeight();
    int[][] pixels = 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);
                pixels[x][y] = colorModel.getRGB(buf) & 0xFFFFFF;
            }
        }
        return pixels;
    } else {
        throw new RuntimeException("transfer type " + raster.getTransferType() + " not implemented yet");
    }
}

From source file:com.boundlessgeo.geoserver.api.controllers.ThumbnailController.java

/**
 * Utility method for scaling thumbnails. Scales byte[] image by a scale factor.
 * Optionally crops images to square./* w w  w .  j  a  v a  2s. c om*/
 * @param src RenderedImage containing the input image
 * @param scale Scale amount
 * @param square Boolean flag to crop to a square image
 * @return RenderedImage containing the transformed image
 * @throws IOException
 */
public static BufferedImage scaleImage(BufferedImage image, double scale, boolean square) throws IOException {
    int sx = 0, sy = 0;
    int swidth = image.getWidth();
    int sheight = image.getHeight();

    if (square) {
        if (image.getHeight() > image.getWidth()) {
            sy = (int) ((image.getHeight() - image.getWidth()) / 2.0);
            sheight = swidth;
        } else if (image.getHeight() < image.getWidth()) {
            sx = (int) ((image.getWidth() - image.getHeight()) / 2.0);
            swidth = sheight;
        }
    }
    int width = (int) (swidth * scale);
    int height = (int) (sheight * scale);

    BufferedImage scaled = new BufferedImage(image.getColorModel(),
            image.getRaster().createCompatibleWritableRaster(width, height), image.isAlphaPremultiplied(),
            null);

    Graphics g = scaled.getGraphics();
    g.drawImage(image, 0, 0, width, height, sx, sy, sx + swidth, sy + sheight, null);
    g.dispose();

    return scaled;
}