/** * Create a data buffer of a particular type. * * @param dataType the desired data type of the buffer. * @param size the size of the data buffer bank * @param numBanks the number of banks the buffer should have */ public static DataBuffer createBuffer(int dataType, int size, int numBanks) { switch (dataType) { case DataBuffer.TYPE_BYTE: return new DataBufferByte(size, numBanks); case DataBuffer.TYPE_SHORT: return new DataBufferShort(size, numBanks); case DataBuffer.TYPE_USHORT: return new DataBufferUShort(size, numBanks); case DataBuffer.TYPE_INT: return new DataBufferInt(size, numBanks); case DataBuffer.TYPE_FLOAT: return new DataBufferFloat(size, numBanks); case DataBuffer.TYPE_DOUBLE: return new DataBufferDouble(size, numBanks); default: throw new UnsupportedOperationException(); } }
/** * Create a data buffer of a particular type. * * @param dataType the desired data type of the buffer * @param data an array containing the data * @param size the size of the data buffer bank */ public static DataBuffer createBufferFromData(int dataType, Object data, int size) { switch (dataType) { case DataBuffer.TYPE_BYTE: return new DataBufferByte((byte[]) data, size); case DataBuffer.TYPE_SHORT: return new DataBufferShort((short[]) data, size); case DataBuffer.TYPE_USHORT: return new DataBufferUShort((short[]) data, size); case DataBuffer.TYPE_INT: return new DataBufferInt((int[]) data, size); case DataBuffer.TYPE_FLOAT: return new DataBufferFloat((float[]) data, size); case DataBuffer.TYPE_DOUBLE: return new DataBufferDouble((double[]) data, size); default: throw new UnsupportedOperationException(); } }
BufferedImage createFloatBufferedImage(int w, int h, int bands) { // Define dimensions and layout of the image //int bands = 4; // 4 bands for ARGB, 3 for RGB etc int[] bandOffsets = {0, 1, 2, 3}; // length == bands, 0 == R, 1 == G, 2 == B and 3 == A // Create a TYPE_FLOAT sample model (specifying how the pixels are stored) SampleModel sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_FLOAT, w, h, bands, w * bands, bandOffsets); // ...and data buffer (where the pixels are stored) DataBuffer buffer = new DataBufferFloat(w * h * bands); // Wrap it in a writable raster WritableRaster raster = Raster.createWritableRaster(sampleModel, buffer, null); // Create a color model compatible with this sample model/raster (TYPE_FLOAT) // Note that the number of bands must equal the number of color components in the // color space (3 for RGB) + 1 extra band if the color model contains alpha ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB); ColorModel colorModel = new ComponentColorModel(colorSpace, true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_FLOAT); // And finally create an image with this raster return new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null); }
public static PlanarImage createFloatPlanarImage(float[] src, int width, int height){ int[] bandOffsets = {0}; SampleModel sampleModel = new PixelInterleavedSampleModel(TYPE_FLOAT, width, height, 1, width, bandOffsets); ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY); ColorModel colorModel = new ComponentColorModel(colorSpace, false, false, Transparency.TRANSLUCENT, TYPE_FLOAT); PlanarImage opImage; DataBuffer buffer = new DataBufferFloat(width * height); // Wrap it in a writable raster WritableRaster raster = Raster.createWritableRaster(sampleModel, buffer, null); raster.setPixels(0, 0, width, height, src); // Create an image with this raster BufferedImage image = new BufferedImage(colorModel, raster, colorModel.isAlphaPremultiplied(), null); opImage = PlanarImage.wrapRenderedImage(image); return opImage; }
public static Object getData(final DataBuffer db) { if (db instanceof DataBufferByte) { return ((DataBufferByte) db).getData(); } else if (db instanceof DataBufferUShort) { return ((DataBufferUShort) db).getData(); } else if (db instanceof DataBufferShort) { return ((DataBufferShort) db).getData(); } else if (db instanceof DataBufferInt) { return ((DataBufferInt) db).getData(); } else if (db instanceof DataBufferFloat) { return ((DataBufferFloat) db).getData(); } else if (db instanceof DataBufferDouble) { return ((DataBufferDouble) db).getData(); } else { throw new RuntimeException("Not found DataBuffer class !"); } }
@Override public Object getData(DataBuffer db) { if (db instanceof DataBufferByte){ return ((DataBufferByte)db).getData(); } else if (db instanceof DataBufferUShort){ return ((DataBufferUShort)db).getData(); } else if (db instanceof DataBufferShort){ return ((DataBufferShort)db).getData(); } else if (db instanceof DataBufferInt){ return ((DataBufferInt)db).getData(); } else if (db instanceof DataBufferFloat){ return ((DataBufferFloat)db).getData(); } else if (db instanceof DataBufferDouble){ return ((DataBufferDouble)db).getData(); } else { // awt.235=Wrong Data Buffer type : {0} throw new IllegalArgumentException(Messages.getString("awt.235", //$NON-NLS-1$ db.getClass())); } }
@Override protected void setUp() throws Exception { super.setUp(); float dataArrays[][] = new float[numBanks][]; for(int i = 0; i < numBanks; i++){ dataArrays[i] = new float[arraySize]; } float dataArray[] = new float[arraySize]; int offsets[] = new int[numBanks]; for(int i = 0; i < numBanks; i++){ offsets[i] = i; } db1 = new DataBufferFloat(dataArrays, size); db2 = new DataBufferFloat(dataArrays, size, offsets); db3 = new DataBufferFloat(dataArray, size); db4 = new DataBufferFloat(dataArray, size, numBanks); db5 = new DataBufferFloat(size); db6 = new DataBufferFloat(size, numBanks); }
public final void test_from_BuffImg_to_FloatDataBuffer(){ src = createImage(BufferedImage.TYPE_INT_RGB); DataBufferFloat dbf = new DataBufferFloat(w * h * 3); int offsets[] = new int[]{0,1,2}; ComponentSampleModel csm = new ComponentSampleModel(DataBuffer.TYPE_FLOAT, w, h, 3, 3 * w, offsets); WritableRaster wr = new OrdinaryWritableRaster(csm, dbf, new Point(0, 0)); ColorModel cm = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false, Transparency.OPAQUE, DataBuffer.TYPE_FLOAT); BufferedImage dst = new BufferedImage(cm, wr, cm.isAlphaPremultiplied(), null); Graphics2D g2d = dst.createGraphics(); g2d.drawImage(src, 0, 0, null); for(int y = 0; y < h; y++){ for(int x = 0; x < w; x++){ assertEquals(src.getRGB(x, y), dst.getRGB(x, y)); } } }
/** Extracts pixel data as arrays of floats, one per channel. */ public static float[][] getFloats(final WritableRaster r, final int x, final int y, final int w, final int h) { if (canUseBankDataDirectly(r, DataBuffer.TYPE_FLOAT, DataBufferFloat.class) && x == 0 && y == 0 && w == r.getWidth() && h == r.getHeight()) { return ((DataBufferFloat) r.getDataBuffer()).getBankData(); } // NB: an order of magnitude faster than the naive makeType solution final int c = r.getNumBands(); final float[][] samples = new float[c][w * h]; for (int i = 0; i < c; i++) r.getSamples(x, y, w, h, i, samples[i]); return samples; }
@Override public BufferedImage getBufferedImage(final ByteSource byteSource, final Map<String, Object> params) throws ImageReadException, IOException { try (RgbeInfo info = new RgbeInfo(byteSource)) { // It is necessary to create our own BufferedImage here as the // org.apache.commons.imaging.common.IBufferedImageFactory interface does // not expose this complexity final DataBuffer buffer = new DataBufferFloat(info.getPixelData(), info.getWidth() * info.getHeight()); final BufferedImage ret = new BufferedImage(new ComponentColorModel( ColorSpace.getInstance(ColorSpace.CS_sRGB), false, false, Transparency.OPAQUE, buffer.getDataType()), Raster.createWritableRaster( new BandedSampleModel(buffer.getDataType(), info.getWidth(), info.getHeight(), 3), buffer, new Point()), false, null); return ret; } }
public static DataBuffer clone(DataBuffer dataBuffer) { if (dataBuffer instanceof DataBufferByte) { return clone((DataBufferByte) dataBuffer); } else if (dataBuffer instanceof DataBufferDouble) { return clone((DataBufferDouble) dataBuffer); } else if (dataBuffer instanceof DataBufferFloat) { return clone((DataBufferFloat) dataBuffer); } else if (dataBuffer instanceof DataBufferInt) { return clone((DataBufferInt) dataBuffer); } else if (dataBuffer instanceof DataBufferShort) { return clone((DataBufferShort) dataBuffer); } else if (dataBuffer instanceof DataBufferUShort) { return clone((DataBufferUShort) dataBuffer); } else { throw new UnsupportedOperationException("Don't know how to clone " + dataBuffer.getClass().getName()); } }
@Override public float[] getDataFloat(DataBuffer db) { if (db instanceof DataBufferFloat){ return ((DataBufferFloat)db).getData(); } return null; }
private void setupLazyCustomConversion(final BufferedImage image) { imageForLazyCustomConversion = image; final boolean hasAlpha = image.getColorModel().hasAlpha(); int pixelFormat = pixelAttributes.format; int pixelType = pixelAttributes.type; if (pixelFormat == 0) { pixelFormat = hasAlpha ? GL.GL_RGBA : GL.GL_RGB; } alignment = 1; // FIXME: do we need better? rowLength = width; // FIXME: correct in all cases? // Allow previously-selected pixelType (if any) to override that // we can infer from the DataBuffer final DataBuffer data = image.getRaster().getDataBuffer(); if (data instanceof DataBufferByte || isPackedInt(image)) { // Don't use GL_UNSIGNED_INT for BufferedImage packed int images if (pixelType == 0) pixelType = GL.GL_UNSIGNED_BYTE; } else if (data instanceof DataBufferDouble) { throw new RuntimeException("DataBufferDouble rasters not supported by OpenGL"); } else if (data instanceof DataBufferFloat) { if (pixelType == 0) pixelType = GL.GL_FLOAT; } else if (data instanceof DataBufferInt) { // FIXME: should we support signed ints? if (pixelType == 0) pixelType = GL.GL_UNSIGNED_INT; } else if (data instanceof DataBufferShort) { if (pixelType == 0) pixelType = GL.GL_SHORT; } else if (data instanceof DataBufferUShort) { if (pixelType == 0) pixelType = GL.GL_UNSIGNED_SHORT; } else { throw new RuntimeException("Unexpected DataBuffer type?"); } pixelAttributes = new GLPixelAttributes(pixelFormat, pixelType); }
private Buffer wrapImageDataBuffer(final BufferedImage image) { // // Note: Grabbing the DataBuffer will defeat Java2D's image // management mechanism (as of JDK 5/6, at least). This shouldn't // be a problem for most JOGL apps, but those that try to upload // the image into an OpenGL texture and then use the same image in // Java2D rendering might find the 2D rendering is not as fast as // it could be. // final DataBuffer data = image.getRaster().getDataBuffer(); if (data instanceof DataBufferByte) { return ByteBuffer.wrap(((DataBufferByte) data).getData()); } else if (data instanceof DataBufferDouble) { throw new RuntimeException("DataBufferDouble rasters not supported by OpenGL"); } else if (data instanceof DataBufferFloat) { return FloatBuffer.wrap(((DataBufferFloat) data).getData()); } else if (data instanceof DataBufferInt) { return IntBuffer.wrap(((DataBufferInt) data).getData()); } else if (data instanceof DataBufferShort) { return ShortBuffer.wrap(((DataBufferShort) data).getData()); } else if (data instanceof DataBufferUShort) { return ShortBuffer.wrap(((DataBufferUShort) data).getData()); } else { throw new RuntimeException("Unexpected DataBuffer type?"); } }
/** * read a raster Image * * @param image * @param symbolizer */ public void readImage(GridCoverage2D image, RasterSymbolizer symbolizer) { System.out.println("READING THE RASTER"); // To avoid multiple read if ((isRead == false) || (symbolizer.getChannelSelection() != null)) // &&(imageFilename // != // null)) { // Declaration and initialization // Set width, height, nbBands and compute size width = image.getRenderedImage().getWidth(); height = image.getRenderedImage().getHeight(); nbBands = image.getNumSampleDimensions(); // size of the input image !! size = width * height * nbBands; // Determine data type of image imageDataType = image.getSampleDimension(0).getSampleDimensionType().name(); // Channel Selection // TODO : VALIDATION of channel selection from symbolizer selectBands(image, symbolizer); // Determine the format format = generateFormatFromNbBandSelected(); // Determine the data type GLinternalDataType = generateGLinternalDataTypeFromImageDataType(); // Automatically detect the best GLinternalFormat GLinternalFormat = generateOptimalGLinternalFormat(); // Buffer allocation and acquisition // INFO : bufferImage.order(ByteOrder.nativeOrder()); // to adjust // the ByteBuffer instance's endianness to match the current // platform. if ((GLinternalDataType == GL11.GL_BYTE) || (GLinternalDataType == GL11.GL_UNSIGNED_BYTE)) { byte[] byteData = ((DataBufferByte) image.getRenderedImage().getData().getDataBuffer()).getData(); bufferImage = ByteBuffer.allocateDirect(byteData.length); bufferImage.order(ByteOrder.nativeOrder()); bufferImage.put(ByteBuffer.wrap(byteData)); } else if ((GLinternalDataType == GL11.GL_SHORT) || (GLinternalDataType == GL11.GL_UNSIGNED_SHORT)) { short[] shortData = ((DataBufferShort) image.getRenderedImage().getData().getDataBuffer()).getData(); bufferImage = ByteBuffer.allocateDirect(shortData.length * 2); bufferImage.order(ByteOrder.nativeOrder()); bufferImage.asShortBuffer().put(ShortBuffer.wrap(shortData)); } else if ((GLinternalDataType == GL11.GL_INT) || (GLinternalDataType == GL11.GL_UNSIGNED_INT)) { int[] intData = ((DataBufferInt) image.getRenderedImage().getData().getDataBuffer()).getData(); bufferImage = ByteBuffer.allocateDirect(intData.length * 4); bufferImage.order(ByteOrder.nativeOrder()); bufferImage.asIntBuffer().put(IntBuffer.wrap(intData)); } else if (GLinternalDataType == GL11.GL_FLOAT) { float[] floatData = ((DataBufferFloat) image.getRenderedImage().getData().getDataBuffer()).getData(); bufferImage = ByteBuffer.allocateDirect(floatData.length * 4); bufferImage.order(ByteOrder.nativeOrder()); bufferImage.asFloatBuffer().put(FloatBuffer.wrap(floatData)); } else if (GLinternalDataType == GL11.GL_DOUBLE) { double[] doubleData = ((DataBufferDouble) image.getRenderedImage().getData().getDataBuffer()).getData(); bufferImage = ByteBuffer.allocateDirect(doubleData.length * 8); bufferImage.order(ByteOrder.nativeOrder()); bufferImage.asDoubleBuffer().put(DoubleBuffer.wrap(doubleData)); } else { System.err.println("This type of data : " + GLinternalDataType + "is not recognized."); } // Rewind the buffer, not very with our way-to-put but why not bufferImage.rewind(); // Now, bufferImage is ok, reading is complete isRead = true; // TEMP ?? animation animate = symbolizer.getAnimate(); } }
public static float[] getDataFloat(final DataBuffer db) { return ((DataBufferFloat) db).getData(); }
@Override public void initialize() throws OperatorException { writeDatasetDescriptor(); installAuxiliaryData(AUXDATA_DIR.toPath()); minSampleFlh = 0.0; maxSampleFlh = 0.0025; minSampleMci = -0.004; maxSampleMci = 0.0; minSampleChl = 0.0; maxSampleChl = 0.75; Product coastDistProduct; try { coastDistProduct = ProductIO.readProduct(new File(AUXDATA_DIR, FEX_COAST_DIST_PRODUCT_FILE)); } catch (IOException e) { throw new OperatorException(e); } // todo - regenerate a better costDist dataset. The current one has a cutoff at ~800 nm final Band coastDistance = coastDistProduct.addBand("coast_dist_nm_cleaned", "coast_dist_nm > 300.0 ? 300.0 : coast_dist_nm"); coastDistWidth = coastDistProduct.getSceneRasterWidth(); coastDistHeight = coastDistProduct.getSceneRasterHeight(); coastDistData = ((DataBufferFloat) coastDistance.getSourceImage().getData().getDataBuffer()).getData(); coastDistProduct.dispose(); patchWriterConfig = new HashMap<>(); patchWriterConfig.put("html.labelValues", new String[][]{ /*0*/ {"ab_none", "* Not a Bloom *"}, /*1*/ {"ab_cyano", "Cyanobacteria"}, /*2*/ {"ab_coco", "Cocolithophores"}, /*3*/ {"ab_float", "Floating Bloom"}, /*4*/ {"ab_case_1", "Case 1 Bloom"}, /*5*/ {"ab_coastal", "Coastal Bloom"}, /*6*/ {"ab_susp_mat", "Suspended Matter"}, }); super.initialize(); result = new FeatureWriterResult(getSourceProduct().getName()); }
public static DataBufferFloat clone(DataBufferFloat dataBuffer) { return new DataBufferFloat(clone(dataBuffer.getBankData()), dataBuffer.getSize(), dataBuffer.getOffsets()); }
/** * Creates an image from the given float data. * * @param data Array containing image data. * @param w Width of image plane. * @param h Height of image plane. * @param c Number of channels. * @param interleaved If set, the channels are assumed to be interleaved; * otherwise they are assumed to be sequential. For example, for RGB * data, the pattern "RGBRGBRGB..." is interleaved, while * "RRR...GGG...BBB..." is sequential. */ public static BufferedImage makeImage(final float[] data, final int w, final int h, final int c, final boolean interleaved) { if (c == 1) return makeImage(data, w, h); final int dataType = DataBuffer.TYPE_FLOAT; final DataBuffer buffer = new DataBufferFloat(data, c * w * h); return constructImage(c, dataType, w, h, interleaved, false, buffer); }
/** * Creates an image from the given single-precision floating point data. * * @param data Array containing image data. It is assumed that each channel * corresponds to one element of the array. For example, for RGB * data, data[0] is R, data[1] is G, and data[2] is B. * @param w Width of image plane. * @param h Height of image plane. */ public static BufferedImage makeImage(final float[][] data, final int w, final int h) { final int dataType = DataBuffer.TYPE_FLOAT; final DataBuffer buffer = new DataBufferFloat(data, data[0].length); return constructImage(data.length, dataType, w, h, false, true, buffer); }