Example usage for java.awt.image DataBuffer TYPE_SHORT

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

Introduction

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

Prototype

int TYPE_SHORT

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

Click Source Link

Document

Tag for signed short data.

Usage

From source file:it.geosolutions.jaiext.range.RangeTest.java

private void checkRangeConversion(Range range) {
    checkRangeConversion(range, DataBuffer.TYPE_BYTE);
    checkRangeConversion(range, DataBuffer.TYPE_DOUBLE);
    checkRangeConversion(range, DataBuffer.TYPE_FLOAT);
    checkRangeConversion(range, DataBuffer.TYPE_INT);
    checkRangeConversion(range, DataBuffer.TYPE_SHORT);
    checkRangeConversion(range, DataBuffer.TYPE_USHORT);
}

From source file:org.geoserver.jai.ConcurrentTileFactory.java

/**
 * Builds a new tile, eventually recycling the data array backing it
 *//*w  w  w. java 2  s .  c  o  m*/
public WritableRaster createTile(SampleModel sampleModel, Point location) {
    // sanity checks
    if (sampleModel == null) {
        throw new NullPointerException("sampleModel cannot be null");
    }
    if (location == null) {
        location = new Point(0, 0);
    }

    DataBuffer db = null;

    // get the three elements making the key into the recycled array map
    int type = sampleModel.getTransferType();
    long numBanks = 0;
    long size = 0;
    if (sampleModel instanceof ComponentSampleModel) {
        ComponentSampleModel csm = (ComponentSampleModel) sampleModel;
        numBanks = getNumBanksCSM(csm);
        size = getBufferSizeCSM(csm);
    } else if (sampleModel instanceof MultiPixelPackedSampleModel) {
        MultiPixelPackedSampleModel mppsm = (MultiPixelPackedSampleModel) sampleModel;
        numBanks = 1;
        int dataTypeSize = DataBuffer.getDataTypeSize(type);
        size = mppsm.getScanlineStride() * mppsm.getHeight()
                + (mppsm.getDataBitOffset() + dataTypeSize - 1) / dataTypeSize;
    } else if (sampleModel instanceof SinglePixelPackedSampleModel) {
        SinglePixelPackedSampleModel sppsm = (SinglePixelPackedSampleModel) sampleModel;
        numBanks = 1;
        size = sppsm.getScanlineStride() * (sppsm.getHeight() - 1) + sppsm.getWidth();
    }

    if (size > 0) {
        // try to build a new data buffer starting from 
        Object array = recycledArrays.getRecycledArray(type, numBanks, size);
        if (array != null) {
            switch (type) {
            case DataBuffer.TYPE_BYTE: {
                byte[][] bankData = (byte[][]) array;
                for (int i = 0; i < numBanks; i++) {
                    Arrays.fill(bankData[i], (byte) 0);
                }
                db = new DataBufferByte(bankData, (int) size);
            }
                break;
            case DataBuffer.TYPE_USHORT: {
                short[][] bankData = (short[][]) array;
                for (int i = 0; i < numBanks; i++) {
                    Arrays.fill(bankData[i], (short) 0);
                }
                db = new DataBufferUShort(bankData, (int) size);
            }
                break;
            case DataBuffer.TYPE_SHORT: {
                short[][] bankData = (short[][]) array;
                for (int i = 0; i < numBanks; i++) {
                    Arrays.fill(bankData[i], (short) 0);
                }
                db = new DataBufferShort(bankData, (int) size);
            }
                break;
            case DataBuffer.TYPE_INT: {
                int[][] bankData = (int[][]) array;
                for (int i = 0; i < numBanks; i++) {
                    Arrays.fill(bankData[i], 0);
                }
                db = new DataBufferInt(bankData, (int) size);
            }
                break;
            case DataBuffer.TYPE_FLOAT: {
                float[][] bankData = (float[][]) array;
                for (int i = 0; i < numBanks; i++) {
                    Arrays.fill(bankData[i], 0.0F);
                }
                db = DataBufferUtils.createDataBufferFloat(bankData, (int) size);
            }
                break;
            case DataBuffer.TYPE_DOUBLE: {
                double[][] bankData = (double[][]) array;
                for (int i = 0; i < numBanks; i++) {
                    Arrays.fill(bankData[i], 0.0);
                }
                db = DataBufferUtils.createDataBufferDouble(bankData, (int) size);
            }
                break;
            default:
                throw new IllegalArgumentException("Unknown array type");
            }
        }
    }

    if (db == null) {
        db = sampleModel.createDataBuffer();
    }

    return Raster.createWritableRaster(sampleModel, db, location);
}

From source file:org.geotools.gce.imagemosaic.ImageMosaicReaderTest.java

/**
 * Simple test method accessing time and 2 custom dimensions for the sample
 * dataset//from w  w  w . j  a  v  a2  s. c o  m
 * @throws IOException
 * @throws FactoryException 
 * @throws NoSuchAuthorityCodeException 
 * @throws ParseException +
 */
@Test
@SuppressWarnings("rawtypes")
public void multipleDimensionsStackedSar() throws Exception {

    final URL sourceURL = TestData.file(this, "merge").toURI().toURL();
    final AbstractGridFormat format = TestUtils.getFormat(sourceURL);
    ImageMosaicReader reader = TestUtils.getReader(sourceURL, format);

    final String[] metadataNames = reader.getMetadataNames();
    assertNotNull(metadataNames);
    assertEquals(12, metadataNames.length);
    assertEquals("false", reader.getMetadataValue("HAS_POLARIZ_DOMAIN"));
    assertEquals("true", reader.getMetadataValue("HAS_POLARIZATION_DOMAIN"));
    assertEquals("POLARIZATION", reader.getDynamicParameters().iterator().next().getName().getCode());
    assertEquals("HH,HV,VH,VV", reader.getMetadataValue("POLARIZATION_DOMAIN"));// ten characters limitation overcome!
    assertEquals("true", reader.getMetadataValue("HAS_TIME_DOMAIN"));
    assertEquals("false", reader.getMetadataValue("HAS_ELEVATION_DOMAIN"));
    assertEquals("2012-01-01T00:00:00.000Z", reader.getMetadataValue("TIME_DOMAIN"));
    assertEquals("2012-01-01T00:00:00.000Z", reader.getMetadataValue("TIME_DOMAIN_MINIMUM"));
    assertEquals("2012-01-01T00:00:00.000Z", reader.getMetadataValue("TIME_DOMAIN_MAXIMUM"));

    // use imageio with defined tiles
    final ParameterValue<Boolean> useJai = AbstractGridFormat.USE_JAI_IMAGEREAD.createValue();
    useJai.setValue(false);
    final ParameterValue<String> tileSize = AbstractGridFormat.SUGGESTED_TILE_SIZE.createValue();
    tileSize.setValue("128,128");

    // specify time
    final ParameterValue<List> time = ImageMosaicFormat.TIME.createValue();
    final SimpleDateFormat formatD = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
    formatD.setTimeZone(TimeZone.getTimeZone("GMT"));
    final Date timeD = formatD.parse("2012-01-01T00:00:00.000Z");
    time.setValue(new ArrayList() {
        {
            add(timeD);
        }
    });

    // specify additional Dimensions
    Set<ParameterDescriptor<List>> params = reader.getDynamicParameters();
    ParameterValue<List<String>> polariz = null;
    for (ParameterDescriptor param : params) {
        if (param.getName().getCode().equalsIgnoreCase("POLARIZATION")) {
            polariz = param.createValue();
            polariz.setValue(new ArrayList<String>() {
                {
                    add("HH");
                    add("HV");
                    add("VV");
                }
            });
        }
    }

    // Stacked bands
    final ParameterValue<String> paramStacked = ImageMosaicFormat.MERGE_BEHAVIOR.createValue();
    paramStacked.setValue(MergeBehavior.STACK.toString());

    // Test the output coverage
    GeneralParameterValue[] values = new GeneralParameterValue[] { useJai, tileSize, time, polariz,
            paramStacked };
    final GridCoverage2D coverage = TestUtils.getCoverage(reader, values, false);
    assertNotNull(coverage);

    // inspect reanderedImage
    final RenderedImage image = coverage.getRenderedImage();
    assertEquals("wrong number of bands detected", 3, image.getSampleModel().getNumBands());
    assertEquals(DataBuffer.TYPE_SHORT, image.getSampleModel().getDataType());
    //        ImageIO.write(image, "TIFF", new File("C:\\test.tif"));
}

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(", ");
        }//from   ww  w .j a  v a  2 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;/*  w w w . j  a va2s  .  c om*/
    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 av a  2  s .co  m

    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  www  . j  ava  2  s  . com

    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.
 * //from   ww  w.j  a  v  a2  s  . c  o  m
 * @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;
    }//from   ww w  . j a  va2  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   ww  w  .ja va2 s .  c  o  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;
}