Example usage for java.awt.image DataBuffer TYPE_USHORT

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

Introduction

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

Prototype

int TYPE_USHORT

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

Click Source Link

Document

Tag for unsigned short data.

Usage

From source file:nitf.imagej.NITF_Reader.java

private static Object getPixelsFromBufferedImage(BufferedImage bufImage) {
    DataBuffer dataBuffer = bufImage.getData().getDataBuffer();
    if (dataBuffer.getDataType() == DataBuffer.TYPE_BYTE)
        return ((DataBufferByte) dataBuffer).getData();
    else if (dataBuffer.getDataType() == DataBuffer.TYPE_USHORT)
        return ((DataBufferUShort) dataBuffer).getData();
    return null;/*from   ww  w .j  a v  a2s.co m*/
}

From source file:org.apache.xmlgraphics.image.codec.png.PNGImageDecoder.java

/**
 * A convenience method to create an instance of
 * <code>ComponentColorModel</code> suitable for use with the given
 * <code>SampleModel</code>. The <code>SampleModel</code> should have a data
 * type of <code>DataBuffer.TYPE_BYTE</code>, <code>TYPE_USHORT</code>, or
 * <code>TYPE_INT</code> and between 1 and 4 bands. Depending on the number
 * of bands of the <code>SampleModel</code>, either a gray, gray+alpha, rgb,
 * or rgb+alpha <code>ColorModel</code> is returned.
 *///w ww .  j a  v  a  2s. com
public static ColorModel createComponentColorModel(final SampleModel sm) {
    final int type = sm.getDataType();
    final int bands = sm.getNumBands();
    ComponentColorModel cm = null;

    if (type == DataBuffer.TYPE_BYTE) {
        switch (bands) {
        case 1:
            cm = colorModelGray8;
            break;
        case 2:
            cm = colorModelGrayAlpha8;
            break;
        case 3:
            cm = colorModelRGB8;
            break;
        case 4:
            cm = colorModelRGBA8;
            break;
        }
    } else if (type == DataBuffer.TYPE_USHORT) {
        switch (bands) {
        case 1:
            cm = colorModelGray16;
            break;
        case 2:
            cm = colorModelGrayAlpha16;
            break;
        case 3:
            cm = colorModelRGB16;
            break;
        case 4:
            cm = colorModelRGBA16;
            break;
        }
    } else if (type == DataBuffer.TYPE_INT) {
        switch (bands) {
        case 1:
            cm = colorModelGray32;
            break;
        case 2:
            cm = colorModelGrayAlpha32;
            break;
        case 3:
            cm = colorModelRGB32;
            break;
        case 4:
            cm = colorModelRGBA32;
            break;
        }
    }

    return cm;
}

From source file:org.apache.xmlgraphics.ps.PSImageUtils.java

/**
 * Extracts a packed RGB integer array of a RenderedImage.
 * @param img the image/*from  w  w  w  .j  ava2 s.c  o  m*/
 * @param startX the starting X coordinate
 * @param startY the starting Y coordinate
 * @param w the width of the cropped image
 * @param h the height of the cropped image
 * @param rgbArray the prepared integer array to write to
 * @param offset offset in the target array
 * @param scansize width of a row in the target array
 * @return the populated integer array previously passed in as rgbArray parameter
 */
public static int[] getRGB(RenderedImage img, int startX, int startY, int w, int h, int[] rgbArray, int offset,
        int scansize) {
    Raster raster = img.getData();
    int yoff = offset;
    int off;
    Object data;
    int nbands = raster.getNumBands();
    int dataType = raster.getDataBuffer().getDataType();
    switch (dataType) {
    case DataBuffer.TYPE_BYTE:
        data = new byte[nbands];
        break;
    case DataBuffer.TYPE_USHORT:
        data = new short[nbands];
        break;
    case DataBuffer.TYPE_INT:
        data = new int[nbands];
        break;
    case DataBuffer.TYPE_FLOAT:
        data = new float[nbands];
        break;
    case DataBuffer.TYPE_DOUBLE:
        data = new double[nbands];
        break;
    default:
        throw new IllegalArgumentException("Unknown data buffer type: " + dataType);
    }

    if (rgbArray == null) {
        rgbArray = new int[offset + h * scansize];
    }

    ColorModel colorModel = img.getColorModel();
    for (int y = startY; y < startY + h; y++, yoff += scansize) {
        off = yoff;
        for (int x = startX; x < startX + w; x++) {
            rgbArray[off++] = colorModel.getRGB(raster.getDataElements(x, y, data));
        }
    }

    return rgbArray;
}

From source file:org.deegree.ogcwebservices.wms.dataaccess.ID2PInterpolation.java

private void createImage() {

    ColorModel ccm;/*from w  w w.j  av a  2s  . co m*/

    if (use32Bits) {
        image = new BufferedImage(getMap.getWidth(), getMap.getHeight(), BufferedImage.TYPE_INT_ARGB);
    } else {
        ccm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_GRAY), null, false, false,
                Transparency.OPAQUE, DataBuffer.TYPE_USHORT);
        WritableRaster wr = ccm.createCompatibleWritableRaster(getMap.getWidth(), getMap.getHeight());
        image = new BufferedImage(ccm, wr, false, new Hashtable<Object, Object>());
    }
}

From source file:org.freecine.filmscan.ScanStrip.java

/**
 Load the scan strip image from disk//w w  w .j a  va2s  .  c  o m
 */
private void loadImage() {
    ImageReader reader;
    try {
        //        PlanarImage img = JAI.create( "fileload", fname );
        ImageInputStream istrm = new FileImageInputStream(file);
        reader = ImageIO.getImageReadersByFormatName("TIFF").next();
        reader.setInput(istrm);
        ImageReadParam param = reader.getDefaultReadParam();

        /*
         Set the color mode to linear sRGB as we don't have a better profile
         for the scanner/film available
         */
        ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
        ImageLayout layout = new ImageLayout();
        ColorModel cm = new ComponentColorModel(cs, false, false, ColorModel.OPAQUE, DataBuffer.TYPE_USHORT);
        layout.setColorModel(cm);
        RenderingHints hints = new RenderingHints(JAI.KEY_IMAGE_LAYOUT, layout);
        stripImage = ImageReadDescriptor.create(istrm, 0, false, false, false, null, null, param, reader,
                hints);
        System.out.println("Color model " + stripImage.getColorModel());
        // BufferedImage inImg = reader.read( 0, param );            
    } catch (FileNotFoundException ex) {
        System.out.println(ex.getMessage());
        log.error("Strip file " + file + " not found", ex);
    } catch (IOException ex) {
        log.error("IO error reading strip " + file + ": ", ex);
    }
}

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

/**
 * Builds a new tile, eventually recycling the data array backing it
 *//*from  w  w  w. j a  va2  s  . co 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.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  .  j ava 2  s  .c o  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;//from www.ja 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  av  a2 s. c  o  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;//  w w  w .  j  a  v  a  2s  .  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;
}