use of java.nio.ByteBuffer in project platform_frameworks_base by android.
the class CameraTestUtils method getDataFromImage.
/**
* <p>Read data from all planes of an Image into a contiguous unpadded, unpacked
* 1-D linear byte array, such that it can be write into disk, or accessed by
* software conveniently. It supports YUV_420_888/NV21/YV12 and JPEG input
* Image format.</p>
*
* <p>For YUV_420_888/NV21/YV12/Y8/Y16, it returns a byte array that contains
* the Y plane data first, followed by U(Cb), V(Cr) planes if there is any
* (xstride = width, ystride = height for chroma and luma components).</p>
*
* <p>For JPEG, it returns a 1-D byte array contains a complete JPEG image.</p>
*/
public static byte[] getDataFromImage(Image image) {
assertNotNull("Invalid image:", image);
int format = image.getFormat();
int width = image.getWidth();
int height = image.getHeight();
int rowStride, pixelStride;
byte[] data = null;
// Read image data
Plane[] planes = image.getPlanes();
assertTrue("Fail to get image planes", planes != null && planes.length > 0);
// Check image validity
checkAndroidImageFormat(image);
ByteBuffer buffer = null;
// Same goes for DEPTH_POINT_CLOUD
if (format == ImageFormat.JPEG || format == ImageFormat.DEPTH_POINT_CLOUD || format == ImageFormat.RAW_PRIVATE) {
buffer = planes[0].getBuffer();
assertNotNull("Fail to get jpeg or depth ByteBuffer", buffer);
data = new byte[buffer.remaining()];
buffer.get(data);
buffer.rewind();
return data;
}
int offset = 0;
data = new byte[width * height * ImageFormat.getBitsPerPixel(format) / 8];
int maxRowSize = planes[0].getRowStride();
for (int i = 0; i < planes.length; i++) {
if (maxRowSize < planes[i].getRowStride()) {
maxRowSize = planes[i].getRowStride();
}
}
byte[] rowData = new byte[maxRowSize];
if (VERBOSE)
Log.v(TAG, "get data from " + planes.length + " planes");
for (int i = 0; i < planes.length; i++) {
buffer = planes[i].getBuffer();
assertNotNull("Fail to get bytebuffer from plane", buffer);
rowStride = planes[i].getRowStride();
pixelStride = planes[i].getPixelStride();
assertTrue("pixel stride " + pixelStride + " is invalid", pixelStride > 0);
if (VERBOSE) {
Log.v(TAG, "pixelStride " + pixelStride);
Log.v(TAG, "rowStride " + rowStride);
Log.v(TAG, "width " + width);
Log.v(TAG, "height " + height);
}
// For multi-planar yuv images, assuming yuv420 with 2x2 chroma subsampling.
int w = (i == 0) ? width : width / 2;
int h = (i == 0) ? height : height / 2;
assertTrue("rowStride " + rowStride + " should be >= width " + w, rowStride >= w);
for (int row = 0; row < h; row++) {
int bytesPerPixel = ImageFormat.getBitsPerPixel(format) / 8;
int length;
if (pixelStride == bytesPerPixel) {
// Special case: optimized read of the entire row
length = w * bytesPerPixel;
buffer.get(data, offset, length);
offset += length;
} else {
// Generic case: should work for any pixelStride but slower.
// Use intermediate buffer to avoid read byte-by-byte from
// DirectByteBuffer, which is very bad for performance
length = (w - 1) * pixelStride + bytesPerPixel;
buffer.get(rowData, 0, length);
for (int col = 0; col < w; col++) {
data[offset++] = rowData[col * pixelStride];
}
}
// Advance buffer the remainder of the row stride
if (row < h - 1) {
buffer.position(buffer.position() + rowStride - length);
}
}
if (VERBOSE)
Log.v(TAG, "Finished reading data from plane " + i);
buffer.rewind();
}
return data;
}
use of java.nio.ByteBuffer in project platform_frameworks_base by android.
the class CameraTestUtils method isImageStronglyEqual.
/**
* <p>
* Checks whether the two images are strongly equal.
* </p>
* <p>
* Two images are strongly equal if and only if the data, formats, sizes,
* and timestamps are same. For {@link ImageFormat#PRIVATE PRIVATE} format
* images, the image data is not not accessible thus the data comparison is
* effectively skipped as the number of planes is zero.
* </p>
* <p>
* Note that this method compares the pixel data even outside of the crop
* region, which may not be necessary for general use case.
* </p>
*
* @param lhsImg First image to be compared with.
* @param rhsImg Second image to be compared with.
* @return true if the two images are equal, false otherwise.
* @throws IllegalArgumentException If either of image is null.
*/
public static boolean isImageStronglyEqual(Image lhsImg, Image rhsImg) {
if (lhsImg == null || rhsImg == null) {
throw new IllegalArgumentException("Images should be non-null");
}
if (lhsImg.getFormat() != rhsImg.getFormat()) {
Log.i(TAG, "lhsImg format " + lhsImg.getFormat() + " is different with rhsImg format " + rhsImg.getFormat());
return false;
}
if (lhsImg.getWidth() != rhsImg.getWidth()) {
Log.i(TAG, "lhsImg width " + lhsImg.getWidth() + " is different with rhsImg width " + rhsImg.getWidth());
return false;
}
if (lhsImg.getHeight() != rhsImg.getHeight()) {
Log.i(TAG, "lhsImg height " + lhsImg.getHeight() + " is different with rhsImg height " + rhsImg.getHeight());
return false;
}
if (lhsImg.getTimestamp() != rhsImg.getTimestamp()) {
Log.i(TAG, "lhsImg timestamp " + lhsImg.getTimestamp() + " is different with rhsImg timestamp " + rhsImg.getTimestamp());
return false;
}
if (!lhsImg.getCropRect().equals(rhsImg.getCropRect())) {
Log.i(TAG, "lhsImg crop rect " + lhsImg.getCropRect() + " is different with rhsImg crop rect " + rhsImg.getCropRect());
return false;
}
// Compare data inside of the image.
Plane[] lhsPlanes = lhsImg.getPlanes();
Plane[] rhsPlanes = rhsImg.getPlanes();
ByteBuffer lhsBuffer = null;
ByteBuffer rhsBuffer = null;
for (int i = 0; i < lhsPlanes.length; i++) {
lhsBuffer = lhsPlanes[i].getBuffer();
rhsBuffer = rhsPlanes[i].getBuffer();
if (!lhsBuffer.equals(rhsBuffer)) {
Log.i(TAG, "byte buffers for plane " + i + " don't matach.");
return false;
}
}
return true;
}
use of java.nio.ByteBuffer in project honeycomb by altamiracorp.
the class ITUtils method assertRowCount.
/**
* Check that the table open on the proxy has the expected number of data rows
* and index rows on each index (checks both ascending and descending
* directions). Note: this could be very slow for big tables.
* @param proxy HandlerProxy with table already open
* @param schema TableSchema of open table
* @param expectedRowCount Expected number of rows
*/
public static void assertRowCount(final HandlerProxy proxy, final TableSchema schema, final long expectedRowCount) {
checkState(proxy.getTableName() != null, "Proxy must have an open table.");
checkNotNull(schema);
verifyRowCount(expectedRowCount);
proxy.startTableScan();
assertResultCount(proxy, expectedRowCount);
proxy.endScan();
QueryKey queryKey;
for (IndexSchema indexSchema : schema.getIndices()) {
queryKey = new QueryKey(indexSchema.getIndexName(), QueryType.INDEX_FIRST, ImmutableMap.<String, ByteBuffer>of());
proxy.startIndexScan(queryKey.serialize());
assertResultCount(proxy, expectedRowCount);
proxy.endScan();
queryKey = new QueryKey(indexSchema.getIndexName(), QueryType.INDEX_LAST, ImmutableMap.<String, ByteBuffer>of());
proxy.startIndexScan(queryKey.serialize());
assertResultCount(proxy, expectedRowCount);
proxy.endScan();
}
}
use of java.nio.ByteBuffer in project honeycomb by altamiracorp.
the class MySqlBugIT method testInsertBuildsIndexCorrectlyOnNullColumns.
/*
DROP TABLE if exists t1;
CREATE TABLE t1(c1 BIGINT SIGNED NULL,c2 BIGINT SIGNED NULL , INDEX(c1, c2));
INSERT INTO t1 VALUES
(4611686018427387903, NULL),
(4611686018427387903, NULL);
SELECT * FROM t1 WHERE c1 = 4611686018427387903 AND c2 is null ORDER BY c1 ASC;
Expected two rows
Actual was zero rows
*/
@Test
public void testInsertBuildsIndexCorrectlyOnNullColumns() {
final Map<String, ByteBuffer> map = Maps.newHashMap();
map.put(TestConstants.COLUMN1, ITUtils.encodeValue(INDEX_COL_VALUE));
final Row row = new Row(map, UUID.randomUUID());
proxy.insertRow(row.serialize());
proxy.flush();
HashMap<String, ByteBuffer> keys = Maps.newHashMap();
keys.put(TestConstants.COLUMN1, ITUtils.encodeValue(INDEX_COL_VALUE));
keys.put(TestConstants.COLUMN2, null);
final QueryKey key = new QueryKey(TestConstants.INDEX2, QueryType.EXACT_KEY, keys);
ITUtils.assertReceivingDifferentRows(proxy, key, ROW_COUNT);
}
use of java.nio.ByteBuffer in project honeycomb by altamiracorp.
the class MySqlBugIT method testUpdateNotChangingIndicesWhenUpdatedColumnNotInIndex.
/*
DROP TABLE IF EXISTS t1;
CREATE TABLE t1(c1 INT, c2 INT, INDEX(c1));
INSERT INTO t1 VALUES(NULL, 1);
UPDATE t1 SET c2=2 WHERE c1 IS NULL;
Index scan: Null, 1
Table scan: Null, 2
*/
@Test
public void testUpdateNotChangingIndicesWhenUpdatedColumnNotInIndex() {
Map<String, ByteBuffer> values = Maps.newHashMap();
values.put(TestConstants.COLUMN2, ITUtils.encodeValue(1));
Row row = new Row(values, UUID.randomUUID());
proxy.insertRow(row.serialize());
proxy.flush();
proxy.startTableScan();
byte[] nextRow = proxy.getNextRow();
row = Row.deserialize(nextRow);
proxy.endScan();
// update t1 set c2=2 where c1 is null
row.getRecords().put(TestConstants.COLUMN2, ITUtils.encodeValue(2));
proxy.updateRow(nextRow, row.serialize());
Map<String, ByteBuffer> searchMap = Maps.newHashMap();
searchMap.put(TestConstants.COLUMN1, null);
QueryKey key = new QueryKey(TestConstants.INDEX1, QueryType.EXACT_KEY, searchMap);
proxy.startIndexScan(key.serialize());
Row result = Row.deserialize(proxy.getNextRow());
assertEquals(result.getRecords().get(TestConstants.COLUMN2).getLong(), ITUtils.encodeValue(2).getLong());
proxy.endScan();
}
Aggregations