Search in sources :

Example 86 with SampleModel

use of java.awt.image.SampleModel in project imageio-ext by geosolutions-it.

the class HDF4Test method rasterBandsProperties.

/**
 * This test method retrieves properties from each band of the sample
 * dataset
 */
@Test
public void rasterBandsProperties() throws FileNotFoundException, IOException {
    if (!isLinux && isDriverAvailable)
        return;
    try {
        ImageReader reader = new HDF4ImageReaderSpi().createReaderInstance();
        String fileName = "TOVS_DAILY_AM_870330_NG.HDF";
        File file = TestData.file(this, fileName);
        reader.setInput(file);
        final int numImages = 3;
        final int startIndex = 2;
        int bands;
        Iterator<ImageTypeSpecifier> it;
        SampleModel sm;
        for (int i = startIndex; i < startIndex + numImages; i++) {
            it = reader.getImageTypes(i);
            ImageTypeSpecifier its;
            if (it.hasNext()) {
                its = (ImageTypeSpecifier) it.next();
                sm = its.getSampleModel();
                bands = sm.getNumBands();
                double d;
                StringBuffer sb = new StringBuffer("RasterBands properties retrieval").append(" Image: ").append(i);
                HDF4ImageReader mReader = (HDF4ImageReader) reader;
                for (int j = 0; j < bands; j++) {
                    sb.append(" \n\t Band: ").append(j).append(" --- ");
                    try {
                        d = mReader.getNoDataValue(i, j);
                        sb.append("NoDataV=").append(d);
                    } catch (IllegalArgumentException iae) {
                        sb.append(" NoDataV=NotAvailable");
                    }
                    try {
                        d = mReader.getOffset(i, j);
                        sb.append(" Off=").append(d);
                    } catch (IllegalArgumentException iae) {
                        sb.append(" Off=NotAvailable");
                    }
                    try {
                        d = mReader.getScale(i, j);
                        sb.append(" Scale=").append(d);
                    } catch (IllegalArgumentException iae) {
                        sb.append(" Scale=NotAvailable");
                    }
                    try {
                        d = mReader.getMinimum(i, j);
                        sb.append(" Min=").append(d);
                    } catch (IllegalArgumentException iae) {
                        sb.append(" Min=NotAvailable");
                    }
                    try {
                        d = mReader.getMaximum(i, j);
                        sb.append(" MAX=").append(d);
                    } catch (IllegalArgumentException iae) {
                        sb.append(" MAX=NotAvailable");
                    }
                }
                LOGGER.info(sb.toString());
            }
        }
        reader.dispose();
    } catch (FileNotFoundException fnfe) {
        warningMessage();
    }
}
Also used : FileNotFoundException(java.io.FileNotFoundException) ImageTypeSpecifier(javax.imageio.ImageTypeSpecifier) SampleModel(java.awt.image.SampleModel) ImageReader(javax.imageio.ImageReader) File(java.io.File) Test(org.junit.Test) AbstractGDALTest(it.geosolutions.imageio.gdalframework.AbstractGDALTest)

Example 87 with SampleModel

use of java.awt.image.SampleModel in project HeavenMS by ronancpl.

the class PNGMapleCanvas method getImage.

@Override
public BufferedImage getImage() {
    int sizeUncompressed = 0;
    int size8888 = 0;
    int maxWriteBuf = 2;
    int maxHeight = 3;
    byte[] writeBuf = new byte[maxWriteBuf];
    @SuppressWarnings("unused") byte[] rowPointers = new byte[maxHeight];
    switch(getFormat()) {
        case 1:
        case 513:
            sizeUncompressed = getHeight() * getWidth() * 4;
            break;
        case 2:
            sizeUncompressed = getHeight() * getWidth() * 8;
            break;
        case 517:
            sizeUncompressed = getHeight() * getWidth() / 128;
            break;
    }
    size8888 = getHeight() * getWidth() * 8;
    if (size8888 > maxWriteBuf) {
        maxWriteBuf = size8888;
        writeBuf = new byte[maxWriteBuf];
    }
    if (getHeight() > maxHeight) {
        maxHeight = getHeight();
        rowPointers = new byte[maxHeight];
    }
    Inflater dec = new Inflater();
    dec.setInput(getData(), 0, dataLength);
    int declen = 0;
    byte[] uc = new byte[sizeUncompressed];
    try {
        declen = dec.inflate(uc);
    } catch (DataFormatException ex) {
        throw new RuntimeException("zlib fucks", ex);
    }
    dec.end();
    if (getFormat() == 1) {
        for (int i = 0; i < sizeUncompressed; i++) {
            byte low = (byte) (uc[i] & 0x0F);
            byte high = (byte) (uc[i] & 0xF0);
            writeBuf[(i << 1)] = (byte) (((low << 4) | low) & 0xFF);
            writeBuf[(i << 1) + 1] = (byte) (high | ((high >>> 4) & 0xF));
        }
    } else if (getFormat() == 2) {
        writeBuf = uc;
    } else if (getFormat() == 513) {
        for (int i = 0; i < declen; i += 2) {
            byte bBits = (byte) ((uc[i] & 0x1F) << 3);
            byte gBits = (byte) (((uc[i + 1] & 0x07) << 5) | ((uc[i] & 0xE0) >> 3));
            byte rBits = (byte) (uc[i + 1] & 0xF8);
            writeBuf[(i << 1)] = (byte) (bBits | (bBits >> 5));
            writeBuf[(i << 1) + 1] = (byte) (gBits | (gBits >> 6));
            writeBuf[(i << 1) + 2] = (byte) (rBits | (rBits >> 5));
            writeBuf[(i << 1) + 3] = (byte) 0xFF;
        }
    } else if (getFormat() == 517) {
        byte b = 0x00;
        int pixelIndex = 0;
        for (int i = 0; i < declen; i++) {
            for (int j = 0; j < 8; j++) {
                b = (byte) (((uc[i] & (0x01 << (7 - j))) >> (7 - j)) * 255);
                for (int k = 0; k < 16; k++) {
                    pixelIndex = (i << 9) + (j << 6) + k * 2;
                    writeBuf[pixelIndex] = b;
                    writeBuf[pixelIndex + 1] = b;
                    writeBuf[pixelIndex + 2] = b;
                    writeBuf[pixelIndex + 3] = (byte) 0xFF;
                }
            }
        }
    }
    DataBufferByte imgData = new DataBufferByte(writeBuf, sizeUncompressed);
    SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, getWidth(), getHeight(), 4, getWidth() * 4, ZAHLEN);
    WritableRaster imgRaster = Raster.createWritableRaster(sm, imgData, new Point(0, 0));
    BufferedImage aa = new BufferedImage(getWidth(), getHeight(), BufferedImage.TYPE_INT_ARGB);
    aa.setData(imgRaster);
    return aa;
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) Point(java.awt.Point) DataBufferByte(java.awt.image.DataBufferByte) Point(java.awt.Point) BufferedImage(java.awt.image.BufferedImage) DataFormatException(java.util.zip.DataFormatException) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) SampleModel(java.awt.image.SampleModel) WritableRaster(java.awt.image.WritableRaster) Inflater(java.util.zip.Inflater)

Example 88 with SampleModel

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

the class GriddedElevationModelImage method redraw.

public void redraw() {
    synchronized (this) {
        if (this.imageBuffer == null) {
            final int width = this.elevationModel.getGridWidth();
            final int height = this.elevationModel.getGridHeight();
            final ColorModel colorModel = ColorModel.getRGBdefault();
            final DataBuffer imageBuffer;
            if (this.cached) {
                imageBuffer = new TempFileMappedIntDataBuffer(width, height);
            } else {
                imageBuffer = new GriddedElevationModelRasterizerDataBuffer(this.rasterizer);
            }
            final SampleModel sampleModel = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, width, height, new int[] { // Red
            0x00ff0000, // Green
            0x0000ff00, // Blue
            0x000000ff, // Alpha
            0xff000000 });
            final WritableRaster raster = new IntegerRaster(sampleModel, imageBuffer);
            final BufferedImage image = new BufferedImage(colorModel, raster, false, null);
            if (this.cached) {
                this.rasterizer.rasterize(imageBuffer);
            }
            setRenderedImage(image);
            this.imageBuffer = imageBuffer;
        } else {
            if (this.cached) {
                this.rasterizer.rasterize(this.imageBuffer);
            }
        }
    }
}
Also used : SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) SampleModel(java.awt.image.SampleModel) ColorModel(java.awt.image.ColorModel) WritableRaster(java.awt.image.WritableRaster) GriddedElevationModelRasterizerDataBuffer(com.revolsys.elevation.gridded.rasterizer.GriddedElevationModelRasterizerDataBuffer) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) BufferedImage(java.awt.image.BufferedImage) GriddedElevationModelRasterizerDataBuffer(com.revolsys.elevation.gridded.rasterizer.GriddedElevationModelRasterizerDataBuffer) DataBuffer(java.awt.image.DataBuffer)

Example 89 with SampleModel

use of java.awt.image.SampleModel 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)

Example 90 with SampleModel

use of java.awt.image.SampleModel in project bioformats by openmicroscopy.

the class AWTImageTools method constructImage.

/**
 * Creates an image with the given DataBuffer.
 */
public static BufferedImage constructImage(int c, int type, int w, int h, boolean interleaved, boolean banded, DataBuffer buffer, ColorModel colorModel) {
    if (c > 4) {
        throw new IllegalArgumentException("Cannot construct image with " + c + " channels");
    }
    if (colorModel == null || colorModel instanceof DirectColorModel) {
        colorModel = makeColorModel(c, type);
        if (colorModel == null)
            return null;
        if (buffer instanceof UnsignedIntBuffer) {
            try {
                colorModel = new UnsignedIntColorModel(32, type, c);
            } catch (IOException e) {
                return null;
            }
        }
    }
    SampleModel model;
    if (c > 2 && type == DataBuffer.TYPE_INT && buffer.getNumBanks() == 1 && !(buffer instanceof UnsignedIntBuffer)) {
        int[] bitMasks = new int[c];
        for (int i = 0; i < c; i++) {
            bitMasks[i] = 0xff << ((c - i - 1) * 8);
        }
        model = new SinglePixelPackedSampleModel(DataBuffer.TYPE_INT, w, h, bitMasks);
    } else if (banded)
        model = new BandedSampleModel(type, w, h, c);
    else if (interleaved) {
        int[] bandOffsets = new int[c];
        for (int i = 0; i < c; i++) bandOffsets[i] = i;
        model = new PixelInterleavedSampleModel(type, w, h, c, c * w, bandOffsets);
    } else {
        int[] bandOffsets = new int[c];
        for (int i = 0; i < c; i++) bandOffsets[i] = i * w * h;
        model = new ComponentSampleModel(type, w, h, 1, w, bandOffsets);
    }
    WritableRaster raster = Raster.createWritableRaster(model, buffer, null);
    BufferedImage b = null;
    if (c == 1 && type == DataBuffer.TYPE_BYTE && !(buffer instanceof SignedByteBuffer)) {
        if (colorModel instanceof IndexColorModel) {
            b = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_INDEXED);
        } else {
            b = new BufferedImage(w, h, BufferedImage.TYPE_BYTE_GRAY);
        }
        b.setData(raster);
    } else if (c == 1 && type == DataBuffer.TYPE_USHORT) {
        if (!(colorModel instanceof IndexColorModel)) {
            b = new BufferedImage(w, h, BufferedImage.TYPE_USHORT_GRAY);
            b.setData(raster);
        }
    } else if (c > 2 && type == DataBuffer.TYPE_INT && buffer.getNumBanks() == 1 && !(buffer instanceof UnsignedIntBuffer)) {
        if (c == 3) {
            b = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
        } else if (c == 4) {
            b = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
        }
        if (b != null)
            b.setData(raster);
    }
    if (b == null)
        b = new BufferedImage(colorModel, raster, false, null);
    return b;
}
Also used : PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) IOException(java.io.IOException) ComponentSampleModel(java.awt.image.ComponentSampleModel) BufferedImage(java.awt.image.BufferedImage) ComponentSampleModel(java.awt.image.ComponentSampleModel) SampleModel(java.awt.image.SampleModel) BandedSampleModel(java.awt.image.BandedSampleModel) SinglePixelPackedSampleModel(java.awt.image.SinglePixelPackedSampleModel) PixelInterleavedSampleModel(java.awt.image.PixelInterleavedSampleModel) BandedSampleModel(java.awt.image.BandedSampleModel) WritableRaster(java.awt.image.WritableRaster) DirectColorModel(java.awt.image.DirectColorModel) IndexColorModel(java.awt.image.IndexColorModel)

Aggregations

SampleModel (java.awt.image.SampleModel)97 ColorModel (java.awt.image.ColorModel)43 ComponentSampleModel (java.awt.image.ComponentSampleModel)38 Point (java.awt.Point)33 SinglePixelPackedSampleModel (java.awt.image.SinglePixelPackedSampleModel)33 DataBuffer (java.awt.image.DataBuffer)32 Rectangle (java.awt.Rectangle)30 IndexColorModel (java.awt.image.IndexColorModel)29 BufferedImage (java.awt.image.BufferedImage)27 MultiPixelPackedSampleModel (java.awt.image.MultiPixelPackedSampleModel)26 WritableRaster (java.awt.image.WritableRaster)26 PixelInterleavedSampleModel (java.awt.image.PixelInterleavedSampleModel)23 DataBufferByte (java.awt.image.DataBufferByte)20 ImageTypeSpecifier (javax.imageio.ImageTypeSpecifier)18 ComponentColorModel (java.awt.image.ComponentColorModel)17 DataBufferInt (java.awt.image.DataBufferInt)17 ColorSpace (java.awt.color.ColorSpace)15 Raster (java.awt.image.Raster)15 DataBufferUShort (java.awt.image.DataBufferUShort)13 BandedSampleModel (java.awt.image.BandedSampleModel)12