Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2005

Tip: Looking for answers? Try searching our database.

setPixels on BufferedImage.TYPE_USHORT_565_RGB

Thread view: 
Sean McGinnis - 09 Dec 2005 02:16 GMT
Does anyone know the format of pixels for an image of
TYPE_USHORT_565_RGB format? I have an application that receives a byte[]
of pixel information in 565 format so that two bytes contains an
individual pixel.

I originally went through and cast all bytes to int's so I could pass
the int[] to the image.getRaster().setPixels() method but I get an
ArrayIndexOutOfBounds. After a little more investigation I found that
the image getPixels() length is 1.5 times bigger.

Anyone know how I can convert my byte[] into a format that setPixels
will like?

Thanks for your help,
Sean
Roedy Green - 09 Dec 2005 03:23 GMT
>TYPE_USHORT_565_RGB format

reading that literally, each pixel is an unsigned 16 bit short.

The top 5 bits are red, the middle 6 bits are green, and the low order
5 bits are blue.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Sean McGinnis - 09 Dec 2005 11:24 GMT
>>TYPE_USHORT_565_RGB format
>
> reading that literally, each pixel is an unsigned 16 bit short.
>
> The top 5 bits are red, the middle 6 bits are green, and the low order
> 5 bits are blue.

Thanks Roedy. I actually figured it out. The red, green, and blue are
still kept in separate int's.

I thought too from the description that they would just take up two
int's, one to holder the upper bounds of the ushort and one to hold the
lower bounds.

It ends up that each separate bit value needs to be saved off in its own
int. That's why the size was throwing me off. It ended up working like:

int[] pixelData = new int[(int)(rawData * 1.5)]; // 2 bytes into three ints
int pixel = 0;

for (int i = 0; i < pixelData.length; i += 3)
{
    pixel = [two bytes into an int];

    pixelData[i] = [red masked portion from pixel];
    pixelData[i+1] = [green masked portion from pixel];
    pixelData[i+2] = [blue portion from pixel];
}

It ended up having much poorer performance than using an ARGB type. I
think because on rendering it had to do the conversion to full RGB. I
ended up going back to storing my data in ARGB format and doing the
calculations before calling setPixels to convert the 5 or 6 bit values
to its correct 0-255 range. It seems like more work up front but then
better performance by the rendering engine later on.
Roedy Green - 09 Dec 2005 17:02 GMT
>Thanks Roedy. I actually figured it out. The red, green, and blue are
>still kept in separate int's.
>
>I thought too from the description that they would just take up two
>int's, one to holder the upper bounds of the ushort and one to hold the
>lower bounds.
Internally the pixels are likely stored in an array of shorts. It is
just when you access a pixel at a time does it split them out into r g
b for you.  I would hazard a guess there is some means to
import/export a large number of pixels as a ushort array.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Boudewijn Dijkstra - 09 Dec 2005 20:26 GMT
>>>TYPE_USHORT_565_RGB format
>>
[quoted text clipped - 26 lines]
> It ended up having much poorer performance than using an ARGB type. I think
> because on rendering it had to do the conversion to full RGB.

No wonder, because now you're performing two conversions.  I think you'll be
better off directly converting the 565 bytes into ARGB int pixels, which can
be directly used to build a BufferedImage.  This should save a lot of memory
operations.  Assuming big-endian:

int raw, pixel, j = 0;
for (int i = 0; i < pixelData.length; i++)
{
 raw = rawData[j++];
 pixel  = ((raw & 0xf8) << 24) | ((raw & 0x07) << 13);
 raw = rawData[j++];
 pixel |= ((raw & 0xe0) <<  5) | ((raw & 0x1f) <<  3);
 pixelData[i] = pixel;
}

Alternatively, you could convert your byte array into a short array and let
Java do the rest of the conversion work for you.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.