Search in sources :

Example 16 with DataBufferShort

use of java.awt.image.DataBufferShort in project javacv by bytedeco.

the class Java2DFrameConverter method copy.

public static void copy(BufferedImage image, Frame frame, double gamma, boolean flipChannels, Rectangle roi) {
    Buffer out = frame.image[0].position(roi == null ? 0 : roi.y * frame.imageStride + roi.x * frame.imageChannels);
    SampleModel sm = image.getSampleModel();
    Raster r = image.getRaster();
    DataBuffer in = r.getDataBuffer();
    int x = -r.getSampleModelTranslateX();
    int y = -r.getSampleModelTranslateY();
    int step = sm.getWidth() * sm.getNumBands();
    int channels = sm.getNumBands();
    if (sm instanceof ComponentSampleModel) {
        step = ((ComponentSampleModel) sm).getScanlineStride();
        channels = ((ComponentSampleModel) sm).getPixelStride();
    } else if (sm instanceof SinglePixelPackedSampleModel) {
        step = ((SinglePixelPackedSampleModel) sm).getScanlineStride();
        channels = 1;
    } else if (sm instanceof MultiPixelPackedSampleModel) {
        step = ((MultiPixelPackedSampleModel) sm).getScanlineStride();
        // ??
        channels = ((MultiPixelPackedSampleModel) sm).getPixelBitStride() / 8;
    }
    int start = y * step + x * channels;
    if (in instanceof DataBufferByte) {
        byte[] a = ((DataBufferByte) in).getData();
        flipCopyWithGamma(ByteBuffer.wrap(a, start, a.length - start), step, (ByteBuffer) out, frame.imageStride, false, gamma, false, flipChannels ? channels : 0);
    } else if (in instanceof DataBufferDouble) {
        double[] a = ((DataBufferDouble) in).getData();
        flipCopyWithGamma(DoubleBuffer.wrap(a, start, a.length - start), step, (DoubleBuffer) out, frame.imageStride, gamma, false, flipChannels ? channels : 0);
    } else if (in instanceof DataBufferFloat) {
        float[] a = ((DataBufferFloat) in).getData();
        flipCopyWithGamma(FloatBuffer.wrap(a, start, a.length - start), step, (FloatBuffer) out, frame.imageStride, gamma, false, flipChannels ? channels : 0);
    } else if (in instanceof DataBufferInt) {
        int[] a = ((DataBufferInt) in).getData();
        int stride = frame.imageStride;
        if (out instanceof ByteBuffer) {
            out = ((ByteBuffer) out).order(flipChannels ? ByteOrder.LITTLE_ENDIAN : ByteOrder.BIG_ENDIAN).asIntBuffer();
            stride /= 4;
        }
        flipCopyWithGamma(IntBuffer.wrap(a, start, a.length - start), step, (IntBuffer) out, stride, gamma, false, flipChannels ? channels : 0);
    } else if (in instanceof DataBufferShort) {
        short[] a = ((DataBufferShort) in).getData();
        flipCopyWithGamma(ShortBuffer.wrap(a, start, a.length - start), step, (ShortBuffer) out, frame.imageStride, true, gamma, false, flipChannels ? channels : 0);
    } else if (in instanceof DataBufferUShort) {
        short[] a = ((DataBufferUShort) in).getData();
        flipCopyWithGamma(ShortBuffer.wrap(a, start, a.length - start), step, (ShortBuffer) out, frame.imageStride, false, gamma, false, flipChannels ? channels : 0);
    } else {
        assert false;
    }
}
Also used : FloatBuffer(java.nio.FloatBuffer) ShortBuffer(java.nio.ShortBuffer) ByteBuffer(java.nio.ByteBuffer) IntBuffer(java.nio.IntBuffer) Buffer(java.nio.Buffer) DoubleBuffer(java.nio.DoubleBuffer) DataBuffer(java.awt.image.DataBuffer) DoubleBuffer(java.nio.DoubleBuffer) DataBufferDouble(java.awt.image.DataBufferDouble) Raster(java.awt.image.Raster) WritableRaster(java.awt.image.WritableRaster) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) ComponentSampleModel(java.awt.image.ComponentSampleModel) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) ByteBuffer(java.nio.ByteBuffer) DataBufferShort(java.awt.image.DataBufferShort) ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) MultiPixelPackedSampleModel(java.awt.image.MultiPixelPackedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) IntBuffer(java.nio.IntBuffer) DataBufferFloat(java.awt.image.DataBufferFloat) DataBufferUShort(java.awt.image.DataBufferUShort) ShortBuffer(java.nio.ShortBuffer) DataBuffer(java.awt.image.DataBuffer)

Example 17 with DataBufferShort

use of java.awt.image.DataBufferShort in project com.revolsys.open by revolsys.

the class Gdal method getBufferedImage.

/**
 * <p>
 * Convert the overview raster from {@link Dataset} to a
 * {@link BufferedImage} . The raster will be clipped to the
 * sourceOffsetX,sourceOffsetY -> sourceWidth, sourceHeight rectangle. The
 * clip rectangle will be adjusted to fit inside the bounds of the source
 * image. The result image will scaled to the dimensions of targetWidth,
 * targetHeight.
 * </p>
 *
 * @param dataset
 *            The image dataset.
 * @param overviewIndex
 *            The index of the overview raster data. Use -1 for the whole
 *            image.
 * @param sourceOffsetX
 *            The x location of the clip rectangle.
 * @param sourceOffsetY
 *            The y location of the clip rectangle.
 * @param sourceWidth
 *            The width of the clip rectangle. Use -1 to auto calculate.
 * @param sourceHeight
 *            The height of the clip rectangle. Use -1 to auto calculate.
 * @param targetWidth
 *            The width of the result image. Use -1 to auto calculate.
 * @param targetHeight
 *            The height of the result image. Use -1 to auto calculate.
 * @return The buffered image.
 */
public static BufferedImage getBufferedImage(final Dataset dataset, final int overviewIndex, int sourceOffsetX, int sourceOffsetY, int sourceWidth, int sourceHeight, int targetWidth, int targetHeight) {
    synchronized (dataset) {
        final int bandCount = dataset.getRasterCount();
        final ByteBuffer[] bandData = new ByteBuffer[bandCount];
        final int[] banks = new int[bandCount];
        final int[] offsets = new int[bandCount];
        int pixels = 0;
        int bandDataType = 0;
        int rasterColorInterpretation = -1;
        ColorTable rasterColorTable = null;
        for (int bandIndex = 0; bandIndex < bandCount; bandIndex++) {
            final Band band = dataset.GetRasterBand(bandIndex + 1);
            try {
                Band overviewBand;
                if (overviewIndex == -1) {
                    overviewBand = band;
                } else {
                    overviewBand = band.GetOverview(overviewIndex);
                }
                try {
                    if (rasterColorTable == null) {
                        rasterColorTable = band.GetRasterColorTable();
                        rasterColorInterpretation = band.GetRasterColorInterpretation();
                        bandDataType = band.getDataType();
                        final int overviewWidth = overviewBand.getXSize();
                        final int overviewHeight = overviewBand.getYSize();
                        if (sourceOffsetX < 0) {
                            sourceOffsetX = 0;
                        }
                        if (sourceOffsetY < 0) {
                            sourceOffsetY = 0;
                        }
                        if (sourceOffsetX >= overviewWidth || sourceOffsetY >= overviewHeight) {
                            return new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
                        }
                        if (sourceWidth < 0) {
                            sourceWidth = overviewWidth;
                        }
                        if (sourceOffsetX + sourceWidth > overviewWidth) {
                            sourceWidth = overviewWidth - sourceOffsetX;
                        }
                        if (targetWidth < 0) {
                            targetWidth = sourceWidth;
                        }
                        if (sourceHeight < 0) {
                            sourceHeight = overviewHeight;
                        }
                        if (sourceOffsetY + sourceHeight > overviewHeight) {
                            sourceHeight = overviewHeight - sourceOffsetY;
                        }
                        if (targetHeight < 0) {
                            targetHeight = sourceHeight;
                        }
                        pixels = targetWidth * targetHeight;
                    }
                    if (pixels > 0 && sourceHeight > 0 && sourceWidth > 0) {
                        final int bufferSize = pixels * gdal.GetDataTypeSize(bandDataType) / 8;
                        final ByteBuffer data = ByteBuffer.allocateDirect(bufferSize);
                        data.order(ByteOrder.nativeOrder());
                        final int result = overviewBand.ReadRaster_Direct(sourceOffsetX, sourceOffsetY, sourceWidth, sourceHeight, targetWidth, targetHeight, bandDataType, data);
                        if (result == gdalconstConstants.CE_None) {
                            bandData[bandIndex] = data;
                        } else {
                            throw new RuntimeException("Error converting image");
                        }
                    } else {
                        return new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
                    }
                    banks[bandIndex] = bandIndex;
                    offsets[bandIndex] = 0;
                } finally {
                    overviewBand.delete();
                }
            } finally {
                band.delete();
            }
        }
        DataBuffer imageBuffer = null;
        SampleModel sampleModel = null;
        int dataType = 0;
        int dataBufferType = 0;
        if (bandDataType == gdalconstConstants.GDT_Byte) {
            final byte[][] bytes = new byte[bandCount][];
            for (int bandIndex = 0; bandIndex < bandCount; bandIndex++) {
                bytes[bandIndex] = new byte[pixels];
                bandData[bandIndex].get(bytes[bandIndex]);
            }
            imageBuffer = new DataBufferByte(bytes, pixels);
            dataBufferType = DataBuffer.TYPE_BYTE;
            sampleModel = new BandedSampleModel(dataBufferType, targetWidth, targetHeight, targetWidth, banks, offsets);
            dataType = rasterColorInterpretation == gdalconstConstants.GCI_PaletteIndex ? BufferedImage.TYPE_BYTE_INDEXED : BufferedImage.TYPE_BYTE_GRAY;
        } else if (bandDataType == gdalconstConstants.GDT_Int16) {
            final short[][] shorts = new short[bandCount][];
            for (int bandIndex = 0; bandIndex < bandCount; bandIndex++) {
                shorts[bandIndex] = new short[pixels];
                bandData[bandIndex].asShortBuffer().get(shorts[bandIndex]);
            }
            imageBuffer = new DataBufferShort(shorts, pixels);
            dataBufferType = DataBuffer.TYPE_USHORT;
            sampleModel = new BandedSampleModel(dataBufferType, targetWidth, targetHeight, targetWidth, banks, offsets);
            dataType = BufferedImage.TYPE_USHORT_GRAY;
        } else if (bandDataType == gdalconstConstants.GDT_Int32) {
            final int[][] ints = new int[bandCount][];
            for (int bandIndex = 0; bandIndex < bandCount; bandIndex++) {
                ints[bandIndex] = new int[pixels];
                bandData[bandIndex].asIntBuffer().get(ints[bandIndex]);
            }
            imageBuffer = new DataBufferInt(ints, pixels);
            dataBufferType = DataBuffer.TYPE_INT;
            sampleModel = new BandedSampleModel(dataBufferType, targetWidth, targetHeight, targetWidth, banks, offsets);
            dataType = BufferedImage.TYPE_CUSTOM;
        }
        final WritableRaster raster = Raster.createWritableRaster(sampleModel, imageBuffer, null);
        BufferedImage image = null;
        ColorModel colorModel = null;
        if (rasterColorInterpretation == gdalconstConstants.GCI_PaletteIndex) {
            dataType = BufferedImage.TYPE_BYTE_INDEXED;
            colorModel = rasterColorTable.getIndexColorModel(gdal.GetDataTypeSize(bandDataType));
            image = new BufferedImage(colorModel, raster, false, null);
        } else {
            ColorSpace colorSpace = null;
            if (bandCount > 2) {
                colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
                colorModel = new ComponentColorModel(colorSpace, false, false, Transparency.OPAQUE, dataBufferType);
                image = new BufferedImage(colorModel, raster, true, null);
            } else {
                image = new BufferedImage(targetWidth, targetHeight, dataType);
                image.setData(raster);
            }
        }
        return image;
    }
}
Also used : ColorTable(org.gdal.gdal.ColorTable) ColorSpace(java.awt.color.ColorSpace) ComponentColorModel(java.awt.image.ComponentColorModel) Band(org.gdal.gdal.Band) DataBufferInt(java.awt.image.DataBufferInt) DataBufferByte(java.awt.image.DataBufferByte) ByteBuffer(java.nio.ByteBuffer) BufferedImage(java.awt.image.BufferedImage) DataBufferShort(java.awt.image.DataBufferShort) SampleModel(java.awt.image.SampleModel) BandedSampleModel(java.awt.image.BandedSampleModel) BandedSampleModel(java.awt.image.BandedSampleModel) WritableRaster(java.awt.image.WritableRaster) ComponentColorModel(java.awt.image.ComponentColorModel) ColorModel(java.awt.image.ColorModel) DataBuffer(java.awt.image.DataBuffer)

Aggregations

DataBufferShort (java.awt.image.DataBufferShort)17 DataBuffer (java.awt.image.DataBuffer)16 DataBufferByte (java.awt.image.DataBufferByte)16 DataBufferInt (java.awt.image.DataBufferInt)15 DataBufferUShort (java.awt.image.DataBufferUShort)10 SampleModel (java.awt.image.SampleModel)10 ComponentSampleModel (java.awt.image.ComponentSampleModel)8 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)7 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)7 BufferedImage (java.awt.image.BufferedImage)6 Point (java.awt.Point)5 DataBufferDouble (java.awt.image.DataBufferDouble)5 DataBufferFloat (java.awt.image.DataBufferFloat)5 BandedSampleModel (java.awt.image.BandedSampleModel)3 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)3 Raster (java.awt.image.Raster)3 WritableRaster (java.awt.image.WritableRaster)3 ByteBuffer (java.nio.ByteBuffer)3 Graphics2D (java.awt.Graphics2D)2 Rectangle (java.awt.Rectangle)2