Example usage for java.awt.image Raster getSampleModel

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

Introduction

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

Prototype

public SampleModel getSampleModel() 

Source Link

Document

Returns the SampleModel that describes the layout of the image data.

Usage

From source file:GraphicsUtil.java

/**
 * Coerces <tt>ras</tt> to be writable.  The returned Raster continues to
 * reference the DataBuffer from ras, so modifications to the returned
 * WritableRaster will be seen in ras.<p>
 *
 * You can specify a new location for the returned WritableRaster, this
 * is especially useful for constructing BufferedImages which require
 * the Raster to be at (0,0)./* w  w w . j av a2  s.  c  om*/
 *
 * This method should only be used if you need a WritableRaster due to
 * an interface (such as to construct a BufferedImage), but have no
 * intention of modifying the contents of the returned Raster.  If
 * you have any doubt about other users of the data in <tt>ras</tt>,
 * use copyRaster (above).
 *
 * @param ras The raster to make writable.
 *
 * @param minX The x location for the upper left corner of the
 *             returned WritableRaster.
 *
 * @param minY The y location for the upper left corner of the
 *             returned WritableRaster.
 *
 * @return A Writable version of <tT>ras</tt> with it's upper left
 *         hand coordinate set to minX, minY (shares it's DataBuffer
 *         with <tt>ras</tt>).
 */
public static WritableRaster makeRasterWritable(Raster ras, int minX, int minY) {
    WritableRaster ret = Raster.createWritableRaster(ras.getSampleModel(), ras.getDataBuffer(),
            new Point(0, 0));
    ret = ret.createWritableChild(ras.getMinX() - ras.getSampleModelTranslateX(),
            ras.getMinY() - ras.getSampleModelTranslateY(), ras.getWidth(), ras.getHeight(), minX, minY, null);
    return ret;
}

From source file:GraphicsUtil.java

/**
 * Creates a new raster that has a <b>copy</b> of the data in
 * <tt>ras</tt>.  This is highly optimized for speed.  There is
 * no provision for changing any aspect of the SampleModel.
 * However you can specify a new location for the returned raster.
 *
 * This method should be used when you need to change the contents
 * of a Raster that you do not "own" (ie the result of a
 * <tt>getData</tt> call).// w  w  w . j a v a 2  s.c  o  m
 *
 * @param ras The Raster to copy.
 *
 * @param minX The x location for the upper left corner of the
 *             returned WritableRaster.
 *
 * @param minY The y location for the upper left corner of the
 *             returned WritableRaster.
 *
 * @return    A writable copy of <tt>ras</tt>
 */
public static WritableRaster copyRaster(Raster ras, int minX, int minY) {
    WritableRaster ret = Raster.createWritableRaster(ras.getSampleModel(), new Point(0, 0));
    ret = ret.createWritableChild(ras.getMinX() - ras.getSampleModelTranslateX(),
            ras.getMinY() - ras.getSampleModelTranslateY(), ras.getWidth(), ras.getHeight(), minX, minY, null);

    // Use System.arraycopy to copy the data between the two...
    DataBuffer srcDB = ras.getDataBuffer();
    DataBuffer retDB = ret.getDataBuffer();
    if (srcDB.getDataType() != retDB.getDataType()) {
        throw new IllegalArgumentException("New DataBuffer doesn't match original");
    }
    int len = srcDB.getSize();
    int banks = srcDB.getNumBanks();
    int[] offsets = srcDB.getOffsets();
    for (int b = 0; b < banks; b++) {
        switch (srcDB.getDataType()) {
        case DataBuffer.TYPE_BYTE: {
            DataBufferByte srcDBT = (DataBufferByte) srcDB;
            DataBufferByte retDBT = (DataBufferByte) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
        }
        case DataBuffer.TYPE_INT: {
            DataBufferInt srcDBT = (DataBufferInt) srcDB;
            DataBufferInt retDBT = (DataBufferInt) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
        }
        case DataBuffer.TYPE_SHORT: {
            DataBufferShort srcDBT = (DataBufferShort) srcDB;
            DataBufferShort retDBT = (DataBufferShort) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
        }
        case DataBuffer.TYPE_USHORT: {
            DataBufferUShort srcDBT = (DataBufferUShort) srcDB;
            DataBufferUShort retDBT = (DataBufferUShort) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
        }
        }
    }

    return ret;
}

From source file:GraphicsUtil.java

/**
 * Creates a new raster that has a <b>copy</b> of the data in
 * <tt>ras</tt>.  This is highly optimized for speed.  There is
 * no provision for changing any aspect of the SampleModel.
 * However you can specify a new location for the returned raster.
 *
 * This method should be used when you need to change the contents
 * of a Raster that you do not "own" (ie the result of a
 * <tt>getData</tt> call).//from  w  w  w.  j  a va 2  s. c o  m
 *
 * @param ras The Raster to copy.
 *
 * @param minX The x location for the upper left corner of the
 *             returned WritableRaster.
 *
 * @param minY The y location for the upper left corner of the
 *             returned WritableRaster.
 *
 * @return    A writable copy of <tt>ras</tt>
 */
public static WritableRaster copyRaster(Raster ras, int minX, int minY) {
    WritableRaster ret = Raster.createWritableRaster(ras.getSampleModel(), new Point(0, 0));
    ret = ret.createWritableChild(ras.getMinX() - ras.getSampleModelTranslateX(),
            ras.getMinY() - ras.getSampleModelTranslateY(), ras.getWidth(), ras.getHeight(), minX, minY, null);

    // Use System.arraycopy to copy the data between the two...
    DataBuffer srcDB = ras.getDataBuffer();
    DataBuffer retDB = ret.getDataBuffer();
    if (srcDB.getDataType() != retDB.getDataType()) {
        throw new IllegalArgumentException("New DataBuffer doesn't match original");
    }
    int len = srcDB.getSize();
    int banks = srcDB.getNumBanks();
    int[] offsets = srcDB.getOffsets();
    for (int b = 0; b < banks; b++) {
        switch (srcDB.getDataType()) {
        case DataBuffer.TYPE_BYTE: {
            DataBufferByte srcDBT = (DataBufferByte) srcDB;
            DataBufferByte retDBT = (DataBufferByte) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
            break;
        }
        case DataBuffer.TYPE_INT: {
            DataBufferInt srcDBT = (DataBufferInt) srcDB;
            DataBufferInt retDBT = (DataBufferInt) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
            break;
        }
        case DataBuffer.TYPE_SHORT: {
            DataBufferShort srcDBT = (DataBufferShort) srcDB;
            DataBufferShort retDBT = (DataBufferShort) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
            break;
        }
        case DataBuffer.TYPE_USHORT: {
            DataBufferUShort srcDBT = (DataBufferUShort) srcDB;
            DataBufferUShort retDBT = (DataBufferUShort) retDB;
            System.arraycopy(srcDBT.getData(b), offsets[b], retDBT.getData(b), offsets[b], len);
            break;
        }
        }
    }

    return ret;
}

From source file:GraphicsUtil.java

/**
 * Copies data from one raster to another. Only the region of
 * overlap between src and dst is copied.  <tt>Src</tt> and
 * <tt>Dst</tt> must have compatible SampleModels.
 *
 * @param src The source of the data/*from   w  w  w . j a v  a 2 s .co  m*/
 * @param dst The destination for the data.
 */
public static void copyData(Raster src, WritableRaster dst) {
    if (is_INT_PACK_Data(src.getSampleModel(), false) && is_INT_PACK_Data(dst.getSampleModel(), false)) {
        copyData_INT_PACK(src, dst);
        return;
    }

    copyData_FALLBACK(src, dst);
}

From source file:main.MapKit.java

@Override
public CompositeContext createContext(ColorModel srcColorModel, ColorModel dstColorModel,
        RenderingHints hints) {/*from  w ww.ja va2  s  .c om*/
    return new CompositeContext() {
        @Override
        public void compose(Raster src, Raster dstIn, WritableRaster dstOut) {
            if (src.getSampleModel().getDataType() != DataBuffer.TYPE_INT
                    || dstIn.getSampleModel().getDataType() != DataBuffer.TYPE_INT
                    || dstOut.getSampleModel().getDataType() != DataBuffer.TYPE_INT) {
                throw new IllegalStateException("Source and destination must store pixels as INT.");
            }

            int width = Math.min(src.getWidth(), dstIn.getWidth());
            int height = Math.min(src.getHeight(), dstIn.getHeight());

            int[] srcPixel = new int[4];
            int[] dstPixel = new int[4];
            int[] srcPixels = new int[width];
            int[] dstPixels = new int[width];

            for (int y = 0; y < height; y++) {
                src.getDataElements(0, y, width, 1, srcPixels);
                dstIn.getDataElements(0, y, width, 1, dstPixels);

                for (int x = 0; x < width; x++) {
                    // pixels are stored as INT_ARGB
                    // our arrays are [R, G, B, A]
                    int pixel = srcPixels[x];
                    srcPixel[0] = (pixel >> 16) & 0xFF;
                    srcPixel[1] = (pixel >> 8) & 0xFF;
                    srcPixel[2] = (pixel >> 0) & 0xFF;
                    srcPixel[3] = (pixel >> 24) & 0xFF;

                    pixel = dstPixels[x];
                    dstPixel[0] = (pixel >> 16) & 0xFF;
                    dstPixel[1] = (pixel >> 8) & 0xFF;
                    dstPixel[2] = (pixel >> 0) & 0xFF;
                    dstPixel[3] = (pixel >> 24) & 0xFF;

                    int[] result = new int[] { (srcPixel[0] * dstPixel[0]) >> 8,
                            (srcPixel[1] * dstPixel[1]) >> 8, (srcPixel[2] * dstPixel[2]) >> 8,
                            (srcPixel[3] * dstPixel[3]) >> 8 };

                    // mixes the result with the opacity
                    dstPixels[x] = (result[3]) << 24 | (result[0]) << 16 | (result[1]) << 8 | (result[2]);
                }
                dstOut.setDataElements(0, y, width, 1, dstPixels);
            }
        }

        @Override
        public void dispose() {
            // empty
        }
    };
}

From source file:GraphicsUtil.java

/**
 * An internal optimized version of copyData designed to work on
 * Integer packed data with a SinglePixelPackedSampleModel.  Only
 * the region of overlap between src and dst is copied.
 *
 * Calls to this should be preflighted with is_INT_PACK_Data
 * on both src and dest (requireAlpha can be false).
 *
 * @param src The source of the data/*from  w w  w.ja  v a2s.  com*/
 * @param dst The destination for the data.
 */
public static void copyData_INT_PACK(Raster src, WritableRaster dst) {
    // System.out.println("Fast copyData");
    int x0 = dst.getMinX();
    if (x0 < src.getMinX())
        x0 = src.getMinX();

    int y0 = dst.getMinY();
    if (y0 < src.getMinY())
        y0 = src.getMinY();

    int x1 = dst.getMinX() + dst.getWidth() - 1;
    if (x1 > src.getMinX() + src.getWidth() - 1)
        x1 = src.getMinX() + src.getWidth() - 1;

    int y1 = dst.getMinY() + dst.getHeight() - 1;
    if (y1 > src.getMinY() + src.getHeight() - 1)
        y1 = src.getMinY() + src.getHeight() - 1;

    int width = x1 - x0 + 1;
    int height = y1 - y0 + 1;

    SinglePixelPackedSampleModel srcSPPSM;
    srcSPPSM = (SinglePixelPackedSampleModel) src.getSampleModel();

    final int srcScanStride = srcSPPSM.getScanlineStride();
    DataBufferInt srcDB = (DataBufferInt) src.getDataBuffer();
    final int[] srcPixels = srcDB.getBankData()[0];
    final int srcBase = (srcDB.getOffset()
            + srcSPPSM.getOffset(x0 - src.getSampleModelTranslateX(), y0 - src.getSampleModelTranslateY()));

    SinglePixelPackedSampleModel dstSPPSM;
    dstSPPSM = (SinglePixelPackedSampleModel) dst.getSampleModel();

    final int dstScanStride = dstSPPSM.getScanlineStride();
    DataBufferInt dstDB = (DataBufferInt) dst.getDataBuffer();
    final int[] dstPixels = dstDB.getBankData()[0];
    final int dstBase = (dstDB.getOffset()
            + dstSPPSM.getOffset(x0 - dst.getSampleModelTranslateX(), y0 - dst.getSampleModelTranslateY()));

    if ((srcScanStride == dstScanStride) && (srcScanStride == width)) {
        // System.out.println("VERY Fast copyData");

        System.arraycopy(srcPixels, srcBase, dstPixels, dstBase, width * height);
    } else if (width > 128) {
        int srcSP = srcBase;
        int dstSP = dstBase;
        for (int y = 0; y < height; y++) {
            System.arraycopy(srcPixels, srcSP, dstPixels, dstSP, width);
            srcSP += srcScanStride;
            dstSP += dstScanStride;
        }
    } else {
        for (int y = 0; y < height; y++) {
            int srcSP = srcBase + y * srcScanStride;
            int dstSP = dstBase + y * dstScanStride;
            for (int x = 0; x < width; x++)
                dstPixels[dstSP++] = srcPixels[srcSP++];
        }
    }
}

From source file:com.bc.ceres.jai.opimage.ReinterpretOpImage.java

private void reformat(Raster sourceRaster, WritableRaster targetRaster, Rectangle targetRectangle) {
    final int sourceDataType = sourceRaster.getSampleModel().getDataType();
    final int targetDataType = targetRaster.getSampleModel().getDataType();
    final PixelAccessor sourceAcc = new PixelAccessor(getSourceImage(0));
    final PixelAccessor targetAcc = new PixelAccessor(this);
    final UnpackedImageData sourcePixels;
    final UnpackedImageData targetPixels;

    sourcePixels = sourceAcc.getPixels(sourceRaster, targetRectangle, sourceDataType, false);

    switch (sourceDataType) {
    case DataBuffer.TYPE_BYTE:
        if (interpretationType == ReinterpretDescriptor.INTERPRET_BYTE_SIGNED) {
            targetPixels = targetAcc.getPixels(targetRaster, targetRectangle, targetDataType, true);
            reformatSByte(sourcePixels, targetPixels, targetRectangle);
            break;
        }/*from  ww w.j av  a2  s.c o  m*/
    case DataBuffer.TYPE_INT:
        if (interpretationType == ReinterpretDescriptor.INTERPRET_INT_UNSIGNED) {
            targetPixels = targetAcc.getPixels(targetRaster, targetRectangle, targetDataType, true);
            reformatUInt(sourcePixels, targetPixels, targetRectangle);
            break;
        }
    default:
        targetPixels = sourcePixels;
    }

    targetAcc.setPixels(targetPixels);
}

From source file:com.bc.ceres.jai.opimage.ReinterpretOpImage.java

private void rescale(Raster sourceRaster, WritableRaster targetRaster, Rectangle targetRectangle) {
    final int sourceDataType = sourceRaster.getSampleModel().getDataType();
    final int targetDataType = targetRaster.getSampleModel().getDataType();
    final PixelAccessor sourceAcc = new PixelAccessor(getSourceImage(0));
    final PixelAccessor targetAcc = new PixelAccessor(this);
    final UnpackedImageData sourcePixels;
    final UnpackedImageData targetPixels;

    sourcePixels = sourceAcc.getPixels(sourceRaster, targetRectangle, sourceDataType, false);
    targetPixels = targetAcc.getPixels(targetRaster, targetRectangle, targetDataType, true);

    switch (sourceDataType) {
    case DataBuffer.TYPE_BYTE:
        if (interpretationType == ReinterpretDescriptor.INTERPRET_BYTE_SIGNED) {
            rescaleSByte(sourcePixels, targetPixels, targetRectangle);
        } else {/*from   ww w.  ja va 2s . c o  m*/
            rescaleByte(sourcePixels, targetPixels, targetRectangle);
        }
        break;
    case DataBuffer.TYPE_USHORT:
        rescaleUShort(sourcePixels, targetPixels, targetRectangle);
        break;
    case DataBuffer.TYPE_SHORT:
        rescaleShort(sourcePixels, targetPixels, targetRectangle);
        break;
    case DataBuffer.TYPE_INT:
        if (interpretationType == ReinterpretDescriptor.INTERPRET_INT_UNSIGNED) {
            rescaleUInt(sourcePixels, targetPixels, targetRectangle);
        } else {
            rescaleInt(sourcePixels, targetPixels, targetRectangle);
        }
        break;
    case DataBuffer.TYPE_FLOAT:
        rescaleFloat(sourcePixels, targetPixels, targetRectangle);
        break;
    case DataBuffer.TYPE_DOUBLE:
        rescaleDouble(sourcePixels, targetPixels, targetRectangle);
        break;
    }

    targetAcc.setPixels(targetPixels);
}

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

private static void writeHeader(final Raster raster, final OutputStream out) throws IOException {
    final DataOutputStream dos = new DataOutputStream(out);

    int headersize = HEADERSIZE;
    // this is in integers!
    // MAKE SURE TO KEEP THIS CORRECT IF YOU ADD PARAMETERS TO THE HEADER!!!

    final SampleModel model = raster.getSampleModel();
    final SampleModelType modeltype = toSampleModelType(model);

    int[] bandOffsets = null;
    switch (modeltype) {
    case BANDED:/* www .j a  v a  2s .  c  om*/
        break;
    case PIXELINTERLEAVED:
    case COMPONENT:
        bandOffsets = ((ComponentSampleModel) model).getBandOffsets();

        // add pixel-stride, scanline-stride, band offset count, & band offsets to
        // the header count
        headersize += 3 + bandOffsets.length;
        break;
    case MULTIPIXELPACKED:
        break;
    case SINGLEPIXELPACKED:
        break;
    default:
    }

    dos.writeInt(headersize);
    dos.writeInt(raster.getHeight());
    dos.writeInt(raster.getWidth());
    dos.writeInt(raster.getNumBands());
    dos.writeInt(raster.getTransferType());

    dos.writeInt(modeltype.ordinal());

    switch (modeltype) {
    case BANDED:
        break;
    case COMPONENT:
    case PIXELINTERLEAVED: {
        final ComponentSampleModel pism = (ComponentSampleModel) model;
        dos.writeInt(pism.getPixelStride());
        dos.writeInt(pism.getScanlineStride());

        dos.writeInt(bandOffsets.length);
        for (final int bandOffset : bandOffsets) {
            dos.writeInt(bandOffset);
        }
    }
        break;
    case MULTIPIXELPACKED:
        break;
    case SINGLEPIXELPACKED:
        break;
    default:
    }

}

From source file:org.mrgeo.opimage.MrsPyramidOpImage.java

public static MrsPyramidOpImage create(final MrsImageDataProvider dp, final Long level,
        final TileClusterInfo tileClusterInfo)
        throws JsonGenerationException, JsonMappingException, IOException {
    // since this is a SourcelessOpImage, we'll need to calculate the layout
    // ourselves...
    final MrsImagePyramidMetadata metadata = dp.getMetadataReader().read();

    final int tileSize = metadata.getTilesize();
    final int minX = (int) tileClusterInfo.getOffsetX() * tileSize;
    final int minY = (int) tileClusterInfo.getOffsetY() * tileSize;
    final int zoomlevel = level.intValue();
    final int width = tileClusterInfo.getWidth() * tileSize;
    final int height = tileClusterInfo.getHeight() * tileSize;

    MrsTileReader<Raster> tileReader = dp.getMrsTileReader(zoomlevel);
    final Iterator<Raster> it = tileReader.get();
    Raster raster = it.next();
    final SampleModel sampleModel = raster.getSampleModel();
    final ImageLayout layout = calculateLayout(dp, zoomlevel);
    return new MrsPyramidOpImage(dp, zoomlevel, layout, null, sampleModel, minX, minY, width, height, tileSize,
            tileClusterInfo);// w ww . j a  va2  s .c  o  m
}