• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 409
  • Last Modified:

Java - Efficient way to copy bit array to 32-bit int array

I'm working with some imaging stuff.  I have an array of bytes, but each bit represents a pixel as this is a monochrome image.

The buffer into which I need to copy is for a CMYK image.  It expects 32 bits/pixel.

I think I need to turn each 0 bit into 0x00000000 and each 1 bit into 0x FFFFFFFF.

I could run a loop that did bitwise math.  But is there an EFFICIENT way of doing this?

thanks.

0
Daniel Wilson
Asked:
Daniel Wilson
  • 3
  • 2
4 Solutions
 
Thomas4019Commented:
The most efficient way to do this is by using bitwise math. Your code would look something like the attached snippet. My source for saying the most efficient way is with bitwise operators is that is how BitSet gets individual bits in an int. BitSet is in java.util and is designed to store booleans in integers. To extract a specific bit it uses bitwise operators. This leads me to think this is the quickest way it could be done.

However, there may be a native to a particular OS way of doing it faster. If performance is a really big concern maybe you could write the conversion in C++ then use JNI to call that method.
int packedData;
int black = 0x00000000;
int white = 0xFFFFFFFF;
 
for(int i = 0; i < 16; i++)
{
	int newColor = ((packedData & (1 < i)) != 0) ? black : white;
}

Open in new window

0
 
CEHJCommented:
>>but each bit represents a pixel as this is a monochrome image.

So you're saying that each element of your array contains info for 8 pixels?
0
 
Daniel WilsonAuthor Commented:
>>So you're saying that each element of your array contains info for 8 pixels?

Yes.  This is a stencil mask in a PDF.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
OK. And it has to end as one int per pixel?
0
 
Daniel WilsonAuthor Commented:
As it turns out, I was using the wrong color model. I shouldn't have been trying to expand each bit to a 32-bit int after all.

Thanks for your assistance, though!
0
 
CEHJCommented:
I thought it sounded a little strange ;-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now