I am converting an RGBA image to RGB. Using naive array copy:
for (int j=0, i=0; i<argbBytes.length; i++){
if (i%4 < 3) {
thumbRGB888[j++] = argbBytes[i];
}
}
But, as expected, it is ultra slow (in comparison to System.arraycopy() method), especially on an Android device. Is there a trick to do it faster?
Use two indexes and System.arraycopy()
, copying 3 elements each time:
for (int i = 0, j = 0; i < argbBytes.length; i += 4, j += 3)
{
System.arraycopy(argbBytes, i, thumbRGB888, j, 3);
}
Should be significantly faster. Not only does it get rid of the modulo and comparison, System.arraycopy()
is implemented with native code, relying on memcpy
which is going to be faster than individual assignments.