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.

LVL 32
Daniel WilsonAsked:
Who is Participating?
 
Daniel WilsonConnect With a Mentor Author 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
 
Thomas4019Connect With a Mentor Commented:
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
 
CEHJConnect With a Mentor Commented:
>>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
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
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
 
CEHJConnect With a Mentor Commented:
OK. And it has to end as one int per pixel?
0
 
CEHJCommented:
I thought it sounded a little strange ;-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.