Example usage for java.awt.image DataBuffer TYPE_FLOAT

List of usage examples for java.awt.image DataBuffer TYPE_FLOAT

Introduction

In this page you can find the example usage for java.awt.image DataBuffer TYPE_FLOAT.

Prototype

int TYPE_FLOAT

To view the source code for java.awt.image DataBuffer TYPE_FLOAT.

Click Source Link

Document

Tag for float data.

Usage

From source file:org.mrgeo.cmd.mrsimageinfo.MrsImageInfo.java

private static void printNodata(final MrsPyramidMetadata metadata) {
    System.out.print("NoData: ");
    for (int band = 0; band < metadata.getBands(); band++) {
        if (band > 0) {
            System.out.print(", ");
        }/*w  ww.  ja  va2 s.co m*/
        switch (metadata.getTileType()) {
        case DataBuffer.TYPE_BYTE:
            System.out.print(metadata.getDefaultValueByte(band));
            break;
        case DataBuffer.TYPE_FLOAT:
            System.out.print(metadata.getDefaultValueFloat(band));
            break;
        case DataBuffer.TYPE_DOUBLE:
            System.out.print(metadata.getDefaultValueDouble(band));
            break;
        case DataBuffer.TYPE_INT:
            System.out.print(metadata.getDefaultValueInt(band));
            break;
        case DataBuffer.TYPE_SHORT:
        case DataBuffer.TYPE_USHORT:
            System.out.print(metadata.getDefaultValueShort(band));
            break;
        default:
            break;
        }
    }
    System.out.println("");
}

From source file:org.mrgeo.cmd.mrsimageinfo.MrsImageInfo.java

private static void printTileType(final MrsPyramidMetadata metadata) {
    System.out.print("Type: ");
    switch (metadata.getTileType()) {
    case DataBuffer.TYPE_BYTE:
        System.out.println("byte");
        break;// www .  j a v  a 2  s.  c  o  m
    case DataBuffer.TYPE_FLOAT:
        System.out.println("float");
        break;
    case DataBuffer.TYPE_DOUBLE:
        System.out.println("double");
        break;
    case DataBuffer.TYPE_INT:
        System.out.println("int");
        break;
    case DataBuffer.TYPE_SHORT:
        System.out.println("short");
        break;
    case DataBuffer.TYPE_USHORT:
        System.out.println("unsigned short");
        break;
    default:
        break;
    }
}

From source file:org.mrgeo.data.raster.RasterWritable.java

private static byte[] rasterToBytes(final Raster raster) {
    final int datatype = raster.getTransferType();

    byte[] pixels;

    final Object elements = raster.getDataElements(raster.getMinX(), raster.getMinY(), raster.getWidth(),
            raster.getHeight(), null);//from  w  w  w . j a va  2s  . com

    switch (datatype) {
    case DataBuffer.TYPE_BYTE: {
        pixels = (byte[]) elements;
        break;
    }
    case DataBuffer.TYPE_FLOAT: {
        final float[] floatElements = (float[]) elements;

        pixels = new byte[floatElements.length * RasterUtils.FLOAT_BYTES];

        final ByteBuffer bytebuff = ByteBuffer.wrap(pixels);
        final FloatBuffer floatbuff = bytebuff.asFloatBuffer();
        floatbuff.put(floatElements);

        break;
    }
    case DataBuffer.TYPE_DOUBLE: {
        final double[] doubleElements = (double[]) elements;

        pixels = new byte[doubleElements.length * RasterUtils.DOUBLE_BYTES];

        final ByteBuffer bytebuff = ByteBuffer.wrap(pixels);
        final DoubleBuffer doubleBuff = bytebuff.asDoubleBuffer();
        doubleBuff.put(doubleElements);

        break;
    }
    case DataBuffer.TYPE_INT: {
        final int[] intElements = (int[]) elements;

        pixels = new byte[intElements.length * RasterUtils.INT_BYTES];

        final ByteBuffer bytebuff = ByteBuffer.wrap(pixels);
        final IntBuffer intBuff = bytebuff.asIntBuffer();
        intBuff.put(intElements);

        break;
    }
    case DataBuffer.TYPE_SHORT:
    case DataBuffer.TYPE_USHORT: {
        final short[] shortElements = (short[]) elements;

        pixels = new byte[shortElements.length * RasterUtils.SHORT_BYTES];

        final ByteBuffer bytebuff = ByteBuffer.wrap(pixels);
        final ShortBuffer shortbuff = bytebuff.asShortBuffer();
        shortbuff.put(shortElements);

        break;
    }
    default:
        throw new RasterWritableException("Error trying to append raster.  Bad raster data type");
    }

    return pixels;
}

From source file:org.mrgeo.data.raster.RasterWritable.java

private static Raster read(final byte[] rasterBytes, Writable payload) throws IOException {
    WritableRaster raster;//from   w ww .j  av  a 2 s.  c om

    final ByteBuffer rasterBuffer = ByteBuffer.wrap(rasterBytes);

    @SuppressWarnings("unused")
    final int headersize = rasterBuffer.getInt(); // this isn't really used anymore...
    final int height = rasterBuffer.getInt();
    final int width = rasterBuffer.getInt();
    final int bands = rasterBuffer.getInt();
    final int datatype = rasterBuffer.getInt();
    final SampleModelType sampleModelType = SampleModelType.values()[rasterBuffer.getInt()];

    SampleModel model;
    switch (sampleModelType) {
    case BANDED:
        model = new BandedSampleModel(datatype, width, height, bands);
        break;
    case MULTIPIXELPACKED:
        throw new NotImplementedException("MultiPixelPackedSampleModel not implemented yet");
        // model = new MultiPixelPackedSampleModel(dataType, w, h, numberOfBits)
    case PIXELINTERLEAVED: {
        final int pixelStride = rasterBuffer.getInt();
        final int scanlineStride = rasterBuffer.getInt();
        final int bandcnt = rasterBuffer.getInt();
        final int[] bandOffsets = new int[bandcnt];
        for (int i = 0; i < bandcnt; i++) {
            bandOffsets[i] = rasterBuffer.getInt();
        }
        model = new PixelInterleavedSampleModel(datatype, width, height, pixelStride, scanlineStride,
                bandOffsets);
        break;
    }
    case SINGLEPIXELPACKED:
        throw new NotImplementedException("SinglePixelPackedSampleModel not implemented yet");
        // model = new SinglePixelPackedSampleModel(dataType, w, h, bitMasks);
    case COMPONENT: {
        final int pixelStride = rasterBuffer.getInt();
        final int scanlineStride = rasterBuffer.getInt();
        final int bandcnt = rasterBuffer.getInt();
        final int[] bandOffsets = new int[bandcnt];
        for (int i = 0; i < bandcnt; i++) {
            bandOffsets[i] = rasterBuffer.getInt();
        }
        model = new ComponentSampleModel(datatype, width, height, pixelStride, scanlineStride, bandOffsets);
        break;
    }
    default:
        throw new RasterWritableException("Unknown RasterSampleModel type");
    }

    // include the header size param in the count
    int startdata = rasterBuffer.position();

    // calculate the data size
    int[] samplesize = model.getSampleSize();
    int samplebytes = 0;
    for (int ss : samplesize) {
        // bits to bytes
        samplebytes += (ss / 8);
    }
    int databytes = model.getHeight() * model.getWidth() * samplebytes;

    // final ByteBuffer rasterBuffer = ByteBuffer.wrap(rasterBytes, headerbytes, databytes);
    // the corner of the raster is always 0,0
    raster = Raster.createWritableRaster(model, null);

    switch (datatype) {
    case DataBuffer.TYPE_BYTE: {
        // we can't use the byte buffer explicitly because the header info is
        // still in it...
        final byte[] bytedata = new byte[databytes];
        rasterBuffer.get(bytedata);

        raster.setDataElements(0, 0, width, height, bytedata);
        break;
    }
    case DataBuffer.TYPE_FLOAT: {
        final FloatBuffer floatbuff = rasterBuffer.asFloatBuffer();
        final float[] floatdata = new float[databytes / RasterUtils.FLOAT_BYTES];

        floatbuff.get(floatdata);

        raster.setDataElements(0, 0, width, height, floatdata);
        break;
    }
    case DataBuffer.TYPE_DOUBLE: {
        final DoubleBuffer doublebuff = rasterBuffer.asDoubleBuffer();
        final double[] doubledata = new double[databytes / RasterUtils.DOUBLE_BYTES];

        doublebuff.get(doubledata);

        raster.setDataElements(0, 0, width, height, doubledata);

        break;
    }
    case DataBuffer.TYPE_INT: {
        final IntBuffer intbuff = rasterBuffer.asIntBuffer();
        final int[] intdata = new int[databytes / RasterUtils.INT_BYTES];

        intbuff.get(intdata);

        raster.setDataElements(0, 0, width, height, intdata);

        break;
    }
    case DataBuffer.TYPE_SHORT:
    case DataBuffer.TYPE_USHORT: {
        final ShortBuffer shortbuff = rasterBuffer.asShortBuffer();
        final short[] shortdata = new short[databytes / RasterUtils.SHORT_BYTES];
        shortbuff.get(shortdata);
        raster.setDataElements(0, 0, width, height, shortdata);
        break;
    }
    default:
        throw new RasterWritableException("Error trying to read raster.  Bad raster data type");
    }

    // should we even try to extract the payload?
    if (payload != null) {
        // test to see if this is a raster with a possible payload
        final int payloadStart = startdata + databytes;
        if (rasterBytes.length > payloadStart) {
            // extract the payload
            final ByteArrayInputStream bais = new ByteArrayInputStream(rasterBytes, payloadStart,
                    rasterBytes.length - payloadStart);
            final DataInputStream dis = new DataInputStream(bais);
            payload.readFields(dis);
        }
    }
    return raster;
}

From source file:org.mrgeo.image.ImageStats.java

/**
 * Computes pixel value statistics: min, max, sum, count, & mean for a Raster and returns an array
 * of ImageStats objects, one for each band in the image.
 * // w  ww  .  ja  v  a  2s  .com
 * @param raster
 *          the raster to compute stats for
 * @param nodata
 *          the value to ignore
 * @return an array of ImageStats objects
 */
static public void computeAndUpdateStats(final ImageStats[] tileStats, final Raster raster,
        final double[] nodata) throws RasterWritableException {
    final int type = raster.getTransferType();
    Number sample;
    for (int y = 0; y < raster.getHeight(); y++) {
        for (int x = 0; x < raster.getWidth(); x++) {
            for (int b = 0; b < raster.getNumBands(); b++) {
                switch (type) {
                case DataBuffer.TYPE_BYTE:
                case DataBuffer.TYPE_INT:
                case DataBuffer.TYPE_SHORT:
                case DataBuffer.TYPE_USHORT:
                    sample = raster.getSample(x, y, b);
                    break;
                case DataBuffer.TYPE_FLOAT:
                    sample = raster.getSampleFloat(x, y, b);
                    break;
                case DataBuffer.TYPE_DOUBLE:
                    sample = raster.getSampleDouble(x, y, b);
                    break;
                default:
                    throw new RasterWritableException(
                            "Error computing tile statistics. Unsupported raster data type");
                }
                updateStats(tileStats[b], sample, nodata[b]);
            }
        }
    }

}

From source file:org.mrgeo.image.MrsImagePyramidMetadata.java

public static int toBytes(final int tiletype) {
    switch (tiletype) {
    case DataBuffer.TYPE_BYTE: {
        return 1;
    }//  www . j  a  v a  2  s.  c  o m
    case DataBuffer.TYPE_FLOAT: {
        return RasterUtils.FLOAT_BYTES;
    }
    case DataBuffer.TYPE_DOUBLE: {
        return RasterUtils.DOUBLE_BYTES;
    }
    case DataBuffer.TYPE_INT: {
        return RasterUtils.INT_BYTES;
    }
    case DataBuffer.TYPE_SHORT: {
        return RasterUtils.SHORT_BYTES;
    }
    case DataBuffer.TYPE_USHORT: {
        return RasterUtils.USHORT_BYTES;
    }
    }

    return 0;
}

From source file:org.mrgeo.image.MrsImagePyramidMetadata.java

public static int toTileType(final String tiletype) {
    if (tiletype == "Byte") {
        return DataBuffer.TYPE_BYTE;
    }/*from w  ww . j a  va2  s . co m*/
    if (tiletype == "Float") {
        return DataBuffer.TYPE_FLOAT;
    }
    if (tiletype == "Double") {
        return DataBuffer.TYPE_DOUBLE;
    }
    if (tiletype == "Int") {
        return DataBuffer.TYPE_INT;
    }
    if (tiletype == "Short") {
        return DataBuffer.TYPE_SHORT;
    }
    if (tiletype == "UShort") {
        return DataBuffer.TYPE_USHORT;
    }

    return DataBuffer.TYPE_UNDEFINED;
}

From source file:org.mrgeo.image.MrsImagePyramidMetadata.java

public static String toTileTypeText(final int tiletype) {
    switch (tiletype) {
    case DataBuffer.TYPE_BYTE: {
        return "Byte";
    }//  ww  w.  j  ava  2  s. c o  m
    case DataBuffer.TYPE_FLOAT: {
        return "Float";
    }
    case DataBuffer.TYPE_DOUBLE: {
        return "Double";
    }
    case DataBuffer.TYPE_INT: {
        return "Int";
    }
    case DataBuffer.TYPE_SHORT: {
        return "Short";
    }
    case DataBuffer.TYPE_USHORT: {
        return "UShort";
    }
    }

    return "Undefined";
}

From source file:org.mrgeo.image.MrsPyramidMetadata.java

public static int toTileType(final String tiletype) {
    if (tiletype.equals("Byte")) {
        return DataBuffer.TYPE_BYTE;
    }/*from w  w  w.  j  a v  a 2 s .c  o m*/
    if (tiletype.equals("Float")) {
        return DataBuffer.TYPE_FLOAT;
    }
    if (tiletype.equals("Double")) {
        return DataBuffer.TYPE_DOUBLE;
    }
    if (tiletype.equals("Int")) {
        return DataBuffer.TYPE_INT;
    }
    if (tiletype.equals("Short")) {
        return DataBuffer.TYPE_SHORT;
    }
    if (tiletype.equals("UShort")) {
        return DataBuffer.TYPE_USHORT;
    }

    return DataBuffer.TYPE_UNDEFINED;
}

From source file:org.mrgeo.rasterops.GeoTiffExporter.java

public static void export(final RenderedImage image, final Bounds bounds, final OutputStream os,
        final boolean replaceNan, final String xmp, final Number nodata) throws IOException {
    OpImageRegistrar.registerMrGeoOps();

    final TIFFEncodeParam param = new TIFFEncodeParam();
    // The version of GDAL that Legion is using requires a tile size > 1
    param.setTileSize(image.getTileWidth(), image.getTileHeight());
    param.setWriteTiled(true);//  w  w w .j a  va2  s .  c  o  m

    // if the image only has 1 pixel, the value of this pixel changes after compressing (especially
    // if this pixel is no data value. e.g -9999 changes to -8192 when read the image back).
    // So don't do compress if the image has only 1 pixel.
    if (image.getWidth() > 1 && image.getHeight() > 1) {
        // Deflate lossless compression (also known as "Zip-in-TIFF")
        param.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);
        param.setDeflateLevel(Deflater.BEST_COMPRESSION);
    }

    final GeoTIFFDirectory dir = new GeoTIFFDirectory();

    // GTModelTypeGeoKey : using geographic coordinate system.
    dir.addGeoKey(new XTIFFField(1024, XTIFFField.TIFF_SHORT, 1, new char[] { 2 }));
    // GTRasterTypeGeoKey : pixel is point
    dir.addGeoKey(new XTIFFField(1025, XTIFFField.TIFF_SHORT, 1, new char[] { 1 }));
    // GeographicTypeGeoKey : 4326 WGS84
    dir.addGeoKey(new XTIFFField(2048, XTIFFField.TIFF_SHORT, 1, new char[] { 4326 }));
    dir.addGeoKey(new XTIFFField(2049, XTIFFField.TIFF_ASCII, 7, new String[] { "WGS 84" }));
    // GeogAngularUnitsGeoKey : Angular Degree
    dir.addGeoKey(new XTIFFField(2054, XTIFFField.TIFF_SHORT, 1, new char[] { 9102 }));
    if (xmp != null) {
        final byte[] b = xmp.getBytes("UTF8");
        dir.addField(new XTIFFField(700, XTIFFField.TIFF_BYTE, b.length, b));
    }
    dir.getFields();

    final double[] tiePoints = new double[6];
    tiePoints[0] = 0.0;
    tiePoints[1] = 0.0;
    tiePoints[2] = 0.0;
    tiePoints[3] = bounds.getMinX();
    tiePoints[4] = bounds.getMaxY();
    tiePoints[5] = 0.0;
    dir.setTiepoints(tiePoints);
    final double[] pixelScale = new double[3];
    pixelScale[0] = bounds.getWidth() / image.getWidth();
    pixelScale[1] = bounds.getHeight() / image.getHeight();
    pixelScale[2] = 0;
    dir.setPixelScale(pixelScale);

    final Vector<TIFFField> fields = toTiffField(dir.getFields());

    RenderedImage output = image;

    final String[] nullValues = new String[1];
    switch (image.getSampleModel().getDataType()) {
    case DataBuffer.TYPE_DOUBLE:
        nullValues[0] = Double.toString(nodata.doubleValue());
        if (replaceNan) {
            output = ReplaceNanDescriptor.create(image, nodata.doubleValue());
        }
        // Tiff exporter doesn't handle doubles. Yuck!
        output = ConvertToFloatDescriptor.create(output);

        // Double.NaN (our default nodata on ingest) should not be written out as nodata on export
        // (i.e. GeoTiffs imported without NODATA metadata field should be exported as such)
        if (!Double.isNaN(nodata.doubleValue())) {
            fields.add(new TIFFField(NULL_TAG, XTIFFField.TIFF_ASCII, 1, nullValues));
        }
        break;
    case DataBuffer.TYPE_FLOAT:
        nullValues[0] = Double.toString(nodata.floatValue());
        if (replaceNan) {
            output = ReplaceNanDescriptor.create(image, nodata.floatValue());
        }
        // Float.NaN (our default nodata on ingest) should not be written out as nodata on export
        // (i.e. GeoTiffs imported without NODATA metadata field should be exported as such)
        if (!Float.isNaN(nodata.floatValue())) {
            fields.add(new TIFFField(NULL_TAG, XTIFFField.TIFF_ASCII, 1, nullValues));
        }
        break;
    case DataBuffer.TYPE_INT:
    case DataBuffer.TYPE_USHORT:
    case DataBuffer.TYPE_SHORT:
    case DataBuffer.TYPE_BYTE:
        nullValues[0] = Integer.toString(nodata.intValue());
        fields.add(new TIFFField(NULL_TAG, XTIFFField.TIFF_ASCII, 1, nullValues));
        break;
    }

    param.setExtraFields(fields.toArray(new TIFFField[0]));

    EncodeDescriptor.create(output, os, "TIFF", param, null);
}