I want to store a large number of equally sized large 3D binary images in Java. My current thought process is to store the information in each bit of a byte[]. Therefore, in the space of one image I could effectively store 8 images (one image per bit). I have the following loops to get/set the image data.
Set
final byte[] storedArray = organData.get(imageNumber);
final short[] binaryArray = getArray(binaryOrgan);
int i=0;
for (short binaryDat开发者_开发问答a : binaryArray) {
byte storedData = (byte)storedArray[i];
if (binaryData == 0)
storedData &= ~(1 << bitNumber);
else
storedData |= (1 << bitNumber);
storedArray[i] = storedData;
++i;
}
Get
final byte[] storedArray = organData.get(imageNumber);
final short[] binaryArray = getArray(binaryImage);
int i=0;
for (short storedData : storedArray) {
binaryArray[i] = (short)((storedData >> bitNumber) & 1);
++i;
}
Currently these are working fine however they are much too slow. Is there a way to speed this up? For instance just get the nth bit of every element in a byte[] without iterating over the byte[]? Ideally I'd like to get this as fast as a lookup in a HashMap (which is what I'm currently using).
PS If there is another way to efficiently store and quickly get/set a set of binary images, I'm all ears.
have smth like this:
private static final int MASKS[] = new int[] {
1, 2, 4, 8, 16, 32, 64, 128};
then
int imageIndex;
boolean colorSet = (yourByte & MASKS[imageIndex]) != 0;
精彩评论