As a simple question: Is there any way to copy every other byte quickly from one array to another in Java?
Disclaimer: My code is already fast enough, but I found myself wondering if there was any native method faster than a simple for loop.
Problem Detail (for those interested): I am splitting a YUV byte array into three separate byte arrays (Y, U, and V). If you haven't seen YUV arrays before (in the Android standard NV21 format) the structure is height*width bytes of Y followed by (height*width)/2 bytes of UV data (our eyes are less sensitive to chrominance resolution) interleaved. It is possible, using arraycopy
to optimize the transfer of Y bytes into a dedicated array using:
System.arraycopy(yuvs, 0, yArray, 0, imgSize.width*imgSize.height);
However I am copying the UV portions into dedicated arrays as follows:
for (bt_y = 544; bt_y < previewSize.height*(3.0/2.0); bt_y++) {
for (bt_x = 0; bt_x < 960;) {
uArray[UVIndex] = yuvs[bt_y*960+bt_x++];
vArray[UVIndex++] = yuvs[bt_y*960+bt_x++];
}
}
Is there a method that can somehow take in a "stride" variable or something that could allow for selecting elements at a known interval?
No. System.arraycopy is fast because it can map directly to memcpy which in turn is just doing a block copy of some memory from one place to another with zero processing.
Your case requires processing and that has to happen somewhere. You could do it in C (JNI) but in my experience it's not worth it, the JIT compiler will do a good job of optimising such a simple case anyway.