Example usage for java.awt.image BufferedImage getSampleModel

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

Introduction

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

Prototype

public SampleModel getSampleModel() 

Source Link

Document

Returns the SampleModel associated with this BufferedImage .

Usage

From source file:it.geosolutions.imageio.plugins.nitronitf.ImageIOUtils.java

public static boolean canDisplay(BufferedImage image) {
    log.info("Data Type: " + image.getSampleModel().getDataType());
    log.info("Pixel Size: " + image.getColorModel().getPixelSize());

    if (image.getSampleModel().getDataType() == DataBuffer.TYPE_FLOAT
            && image.getColorModel().getPixelSize() == 64)
        return false;
    return !GraphicsEnvironment.isHeadless();
}

From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * Rescales using predefined min,max intensities and converts to 8bit. Works for gray- and rgb color images.
 *
 * @param bi16//from w w w . ja  v  a  2  s . com
 * @return
 */
public static BufferedImage convertTo8bit(BufferedImage bi16, int min, int max) {
    BufferedImage bi = null;
    if (bi16.getSampleModel().getNumBands() == 1) {
        bi16 = ImageUtils.scaleIntensities(bi16, new int[] { min }, new int[] { max });
        bi = new BufferedImage(bi16.getWidth(), bi16.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    } else if (bi16.getSampleModel().getNumBands() == 3) {
        throw new IllegalArgumentException("only grayscale images supported");
    } else {
        throw new IllegalArgumentException(
                "Only images with 1 band (gray-color) or 3 bands (rgb) supported. This image has "
                        + bi16.getSampleModel().getNumBands() + " bands.");
    }
    Graphics2D g2d = bi.createGraphics();
    g2d.drawImage(bi16, 0, 0, null);
    g2d.dispose();
    return bi;
}

From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * Rescales using arbitrary intensities and converts to 8bit. Works for gray- and rgb color images.
 *
 * @param bi16//from  w  w w.  java2 s .  c om
 * @return
 */
public static BufferedImage convertTo8bit(BufferedImage bi16, double intScalingMin, double intScalignMax) {
    // TODO: use min,max from plate meta data, not image specific
    int[][] minMax = ImageUtils.getMinMaxIntensitiesOfBI(bi16);
    BufferedImage bi = null;
    if (bi16.getSampleModel().getNumBands() == 1) {
        bi16 = ImageUtils.scaleIntensities(bi16, getPercentileIntensity(bi16, intScalingMin),
                getPercentileIntensity(bi16, intScalignMax));
        bi = new BufferedImage(bi16.getWidth(), bi16.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    } else if (bi16.getSampleModel().getNumBands() == 3) {
        bi16 = ImageUtils.scaleIntensities(bi16, getPercentileIntensity(bi16, intScalingMin),
                getPercentileIntensity(bi16, intScalignMax));
        bi = new BufferedImage(bi16.getWidth(), bi16.getHeight(), BufferedImage.TYPE_INT_RGB);
    } else {
        throw new IllegalArgumentException(
                "Only images with 1 band (gray-color) or 3 bands (rgb) supported. This image has "
                        + bi16.getSampleModel().getNumBands() + " bands.");
    }
    Graphics2D g2d = bi.createGraphics();
    g2d.drawImage(bi16, 0, 0, null);
    g2d.dispose();
    return bi;
}

From source file:edu.stanford.epad.epadws.handlers.dicom.DSOUtil.java

public static String getPixelValues(SourceImage sImg, int frameNum) {
    int signMask = 0;
    int signBit = 0;

    BufferedImage src = sImg.getBufferedImage(frameNum);
    if (sImg.isSigned()) {
        // the source image will already have been sign extended to the data type size
        // so we don't need to worry about other than exactly 8 and 16 bits
        if (src.getSampleModel().getDataType() == DataBuffer.TYPE_BYTE) {
            signBit = 0x0080;/*from ww  w. j  av  a  2s .  co  m*/
            signMask = 0xffffff80;
        } else { // assume short or ushort
            signBit = 0x8000;
            signMask = 0xffff8000;
        }
    }
    double[] storedPixelValueArray;
    if (src.getRaster().getDataBuffer() instanceof DataBufferFloat) {
        float[] storedPixelValues = src.getSampleModel().getPixels(0, 0, src.getWidth(), src.getHeight(),
                (float[]) null, src.getRaster().getDataBuffer());
        //copy to double array
        storedPixelValueArray = new double[storedPixelValues.length];
        for (int i = 0; i < storedPixelValues.length; i++) {
            storedPixelValueArray[i] = storedPixelValues[i];
        }
    } else if (src.getRaster().getDataBuffer() instanceof DataBufferDouble) {
        double[] storedPixelValues = src.getSampleModel().getPixels(0, 0, src.getWidth(), src.getHeight(),
                (double[]) null, src.getRaster().getDataBuffer());
        storedPixelValueArray = storedPixelValues;
    } else {
        int[] storedPixelValues = src.getSampleModel().getPixels(0, 0, src.getWidth(), src.getHeight(),
                (int[]) null, src.getRaster().getDataBuffer());
        int storedPixelValueInt = 0;
        //copy to double array
        storedPixelValueArray = new double[storedPixelValues.length];
        for (int i = 0; i < storedPixelValues.length; i++) {
            storedPixelValueInt = storedPixelValues[i];

            if (sImg.isSigned() && (storedPixelValueInt & signBit) != 0) {
                storedPixelValueInt |= signMask; // sign extend
            }
            storedPixelValueArray[i] = storedPixelValueInt;
        }

    }
    return JSON.toString(storedPixelValueArray);

}

From source file:com.actelion.research.orbit.imageAnalysis.utils.ImageUtils.java

/**
 * Rescales using plate intensities and converts to 8bit. Works for gray- and rgb color images.
 *
 * @param bi16/*w w w  .  ja va2s.c o  m*/
 * @return
 */
public static BufferedImage convertTo8bit(int rdfId, BufferedImage bi16, PlateScalingMin plateScalingMin,
        PlateScalingMax plateScalingMax) throws Exception {
    RawDataFile rdf = DALConfig.getImageProvider().LoadRawDataFile(rdfId);
    /*
    if (plateScalingMin==defaultPlateScalingMin && plateScalingMax==defaultPlateScalingMax) {
        // /orbitvol1/2014-11/4309324.1002.jpg
        String url = RawUtils.STR_SERVER+"/rawFile?rawFile="+rdf.getDataPath()+"/"+rdfId+"."+RawUtils.LEVEL_8BITPREVIEW+".jpg";
        System.out.println("url: " + url);
        return ImageIO.read(new URL(url));
    }
    */

    RawData rd = DALConfig.getImageProvider().LoadRawData(rdf.getRawDataId());
    List<RawMeta> rmList = DALConfig.getImageProvider().LoadRawMetasByRawDataFile(rdfId);
    List<RawMeta> rmDataList = DALConfig.getImageProvider().LoadRawMetasByRawData(rd.getRawDataId());
    rmList.addAll(rmDataList);
    HashMap<String, RawMeta> rmHash = RawUtilsCommon.getHashFromMetaList(rmList);

    if (!rmHash.containsKey(RawUtilsCommon.STR_META_CHANNEL))
        throw new Exception("Error: Meta data 'Channel' not available for RawDataFile " + rdfId);
    //int channel = Integer.parseInt(rmHash.get(RawUtils.STR_META_CHANNEL).getValue()) - 1;
    int channel = Integer.parseInt(rmHash.get(RawUtilsCommon.STR_META_CHANNEL).getValue());
    String metaKey = "Percentiles_wvlength_" + channel + "_channel_0";
    if (!rmHash.containsKey(metaKey))
        throw new Exception("Error: Meta data '" + metaKey + "' not available for RawDataFile " + rdfId);
    String val = rmHash.get(metaKey).getValue();
    int[] minmax = parseMinMax(val, plateScalingMin, plateScalingMax);

    BufferedImage bi = null;
    if (bi16.getSampleModel().getNumBands() == 1) {
        bi16 = ImageUtils.scaleIntensities(bi16, new int[] { minmax[0] }, new int[] { minmax[1] });
        bi = new BufferedImage(bi16.getWidth(), bi16.getHeight(), BufferedImage.TYPE_BYTE_GRAY);
    } else {
        throw new IllegalArgumentException("Only images with 1 band (gray-color) supported. This image has "
                + bi16.getSampleModel().getNumBands() + " bands.");
    }
    Graphics2D g2d = bi.createGraphics();
    g2d.drawImage(bi16, 0, 0, null);
    g2d.dispose();
    return bi;
}

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

private BufferedImage jpgImage(BufferedImage src) {
    int w = src.getWidth();
    int h = src.getHeight();
    SampleModel srcSM = src.getSampleModel();
    WritableRaster srcWR = src.getRaster();
    java.awt.image.DataBuffer srcDB = srcWR.getDataBuffer();

    ColorModel rgb = new DirectColorModel(32, 0xff0000, 65280, 255);
    int[] bitMasks = new int[] { 0xff0000, 65280, 255 };

    SampleModel csm = new SinglePixelPackedSampleModel(3, w, h, bitMasks);
    int data[] = new int[w * h];
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            int pix[] = null;
            int sample[] = srcSM.getPixel(j, i, pix, srcDB);
            if (sample[3] == 0 && sample[2] == 0 && sample[1] == 0 && sample[0] == 0)
                data[i * w + j] = 0xffffff;
            else//from w w  w  .j av a  2s . c  om
                data[i * w + j] = sample[0] << 16 | sample[1] << 8 | sample[2];
        }

    }

    java.awt.image.DataBuffer db = new DataBufferInt(data, w * h * 3);
    WritableRaster wr = Raster.createWritableRaster(csm, db, new Point(0, 0));
    return new BufferedImage(rgb, wr, false, null);
}

From source file:qupath.lib.gui.panels.classify.RandomTrainingRegionSelector.java

public static Map<Integer, List<PathObject>> objectClusterer(final Collection<PathObject> pathObjects,
        final BufferedImage imgThumbnail, final double thumbScaleX, final double thumbScaleY,
        final int nClusters) {

    Map<Integer, List<PathObject>> map = new HashMap<>();
    if (pathObjects.isEmpty())
        return map;

    if (nClusters <= 1 || pathObjects.size() == 1) {
        map.put(Integer.valueOf(0), new ArrayList<>(pathObjects));
        return map;
    }/* w  ww  .  j  av  a  2 s.c  o m*/

    //      int maxIterations = 100;

    KMeansPlusPlusClusterer<ClusterableObject> km = new KMeansPlusPlusClusterer<>(nClusters);
    List<ClusterableObject> clusterableObjects = new ArrayList<>();
    WritableRaster raster = imgThumbnail.getRaster();
    int nChannels = raster.getNumBands();
    double[] valueBuffer = new double[nChannels];
    int w = imgThumbnail.getWidth();
    int h = imgThumbnail.getHeight();
    boolean isRGB = imgThumbnail.getSampleModel().getNumBands() == 3
            && imgThumbnail.getSampleModel().getSampleSize(0) == 8;

    for (PathObject pathObject : pathObjects) {
        // Get pixel values for the ROI centroid
        // CIE LAB is used rather than RGB where possible, due to better suitability for Euclidean distances
        ROI roi = pathObject.getROI();
        if (roi == null)
            continue;
        int x = (int) (roi.getCentroidX() * thumbScaleX + 0.5);
        int y = (int) (roi.getCentroidY() * thumbScaleY + 0.5);
        if (x < 0 || x >= w || y < 0 || y >= h)
            continue;

        if (isRGB)
            valueBuffer = makeCIELAB(imgThumbnail.getRGB(x, y), valueBuffer);
        else {
            for (int c = 0; c < nChannels; c++)
                valueBuffer[c] = raster.getSampleDouble(x, y, c);
        }

        clusterableObjects.add(new ClusterableObject(pathObject, valueBuffer));
    }
    List<CentroidCluster<ClusterableObject>> results = km.cluster(clusterableObjects);

    int i = 0;
    for (CentroidCluster<ClusterableObject> centroidCluster : results) {
        Integer label = Integer.valueOf(i);
        List<PathObject> objects = new ArrayList<>();
        for (ClusterableObject co : centroidCluster.getPoints())
            objects.add(co.getPathObject());
        map.put(label, objects);
        i++;
    }

    return map;
}