下面列出了怎么用java.awt.image.DataBufferFloat的API类实例代码及写法,或者点击链接到github查看源代码。
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 !");
}
}
/** 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());
}
}
public static float[] getDataFloat(final DataBuffer db) {
return ((DataBufferFloat) db).getData();
}
public static byte[] getDataBufferBinary(final DataBuffer dataBuffer) {
final DataBufferProtos.DataBuffer.Builder bldr = DataBufferProtos.DataBuffer.newBuilder();
bldr.setType(dataBuffer.getDataType());
bldr.addAllOffsets(Ints.asList(dataBuffer.getOffsets()));
bldr.setSize(dataBuffer.getSize());
switch (dataBuffer.getDataType()) {
case DataBuffer.TYPE_BYTE:
final ByteDataBuffer.Builder byteBldr = ByteDataBuffer.newBuilder();
final byte[][] byteBank = ((DataBufferByte) dataBuffer).getBankData();
final Iterable<ByteString> byteIt = () -> new Iterator<ByteString>() {
private int index = 0;
@Override
public boolean hasNext() {
return byteBank.length > index;
}
@Override
public ByteString next() {
if (!hasNext()) {
throw new NoSuchElementException();
}
return ByteString.copyFrom(byteBank[index++]);
}
};
byteBldr.addAllBanks(byteIt);
bldr.setByteDb(byteBldr.build());
break;
case DataBuffer.TYPE_SHORT:
setBuilder(shortToInt(((DataBufferShort) dataBuffer).getBankData()), bldr);
break;
case DataBuffer.TYPE_USHORT:
setBuilder(shortToInt(((DataBufferUShort) dataBuffer).getBankData()), bldr);
break;
case DataBuffer.TYPE_INT:
setBuilder(((DataBufferInt) dataBuffer).getBankData(), bldr);
break;
case DataBuffer.TYPE_FLOAT:
final FloatDataBuffer.Builder fltBldr = FloatDataBuffer.newBuilder();
final float[][] fltBank = ((DataBufferFloat) dataBuffer).getBankData();
final Iterable<FloatArray> floatIt = () -> new Iterator<FloatArray>() {
private int index = 0;
@Override
public boolean hasNext() {
return fltBank.length > index;
}
@Override
public FloatArray next() {
return FloatArray.newBuilder().addAllSamples(Floats.asList(fltBank[index++])).build();
}
};
fltBldr.addAllBanks(floatIt);
bldr.setFlt(fltBldr);
break;
case DataBuffer.TYPE_DOUBLE:
final DoubleDataBuffer.Builder dblBldr = DoubleDataBuffer.newBuilder();
final double[][] dblBank = ((DataBufferDouble) dataBuffer).getBankData();
final Iterable<DoubleArray> dblIt = () -> new Iterator<DoubleArray>() {
private int index = 0;
@Override
public boolean hasNext() {
return dblBank.length > index;
}
@Override
public DoubleArray next() {
return DoubleArray.newBuilder().addAllSamples(Doubles.asList(dblBank[index++])).build();
}
};
dblBldr.addAllBanks(dblIt);
bldr.setDbl(dblBldr);
break;
default:
throw new RuntimeException(
"Unsupported DataBuffer type for serialization " + dataBuffer.getDataType());
}
return bldr.build().toByteArray();
}
public static DataBuffer getDataBuffer(final byte[] binary)
throws IOException, ClassNotFoundException {
// // Read serialized form from the stream.
final DataBufferProtos.DataBuffer buffer = DataBufferProtos.DataBuffer.parseFrom(binary);
final int[] offsets = ArrayUtils.toPrimitive(buffer.getOffsetsList().toArray(new Integer[] {}));
// Restore the transient DataBuffer.
switch (buffer.getType()) {
case DataBuffer.TYPE_BYTE:
return new DataBufferByte(
listToByte(buffer.getByteDb().getBanksList()),
buffer.getSize(),
offsets);
case DataBuffer.TYPE_SHORT:
return new DataBufferShort(
intToShort(listToInt(buffer.getSint().getBanksList())),
buffer.getSize(),
offsets);
case DataBuffer.TYPE_USHORT:
return new DataBufferUShort(
intToShort(listToInt(buffer.getSint().getBanksList())),
buffer.getSize(),
offsets);
case DataBuffer.TYPE_INT:
return new DataBufferInt(
listToInt(buffer.getSint().getBanksList()),
buffer.getSize(),
offsets);
case DataBuffer.TYPE_FLOAT:
return new DataBufferFloat(
listToFloat(buffer.getFlt().getBanksList()),
buffer.getSize(),
offsets);
case DataBuffer.TYPE_DOUBLE:
return new DataBufferDouble(
listToDouble(buffer.getDbl().getBanksList()),
buffer.getSize(),
offsets);
default:
throw new RuntimeException(
"Unsupported data buffer type for deserialization" + buffer.getType());
}
}
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);
}