Creating 16 or 32 bit image from byte array
Given the width, height, depth and byte array of an image, I need to create an SWT Image object. Currently, I have no trouble when given the data for 8 bit images.
int imageWidth;
int imageHeight;
int depth;
byte[] imageBytes;
//calculate the values
imageData = new ImageData( imageWidth, imageHeight, depth, new PaletteData(255,255,255), 1, imageBytes);
new Image( display, imageData );
The problem I have is that for 16 bit images the color is a bit off. What should be black pixels are actually gray.
For 32 bit images, only a few black pixels show up and the rest of the image is white.
Any ideas?
Thank you.
Edit: The imageBytes array is read from a proprietary graphics file that I have been unable to get the specification for(and so I am not entirely sure of the format).
I was able to make some progress on the 32-bit image. It looks like the 32 bit image was in one of the RGBAX formats. I converted it into a 24-bit image and it now has the same problem as the 16-bit image(what is gray should be black).
The size of the imageBytes arr开发者_运维百科ay is (width * height * (depth/8) ) where depth is in bits.
I tried changing the byte ordering of each pixel however it didn't solve the problem. In areas that should be solid black, it would either be black, a mixture of white and black or be all white.
From the symptoms you describe, it's possible that you're experiencing endian issues. It looks like Java's byte
datatype is 8-bits only, so when you're constructing your 16-bit images, a total of two Java bytes
makes up a single pixel in the constructed image. Obviously, For 32-bit images, the number of Java bytes
per pixel will be 4.
For example, in a big-endian 32-bit world, [0 0 0 255]
would be a pretty dark gray (essentially black). On the other hand, in a little-endian world it would be pretty close to white.
Some questions:
- How are you populating your
imageBytes
array? - What are its dimensions? One would assume it would be
width*height*depth/8
ifdepth
is in bits.
Some things you could try:
- Read the SWT documentation to find out the endianness that the
ImageData
constructor expects - Shuffle the
bytes
within a single pixel in yourimageBytes
array. For example, in the 32-bit case,[0 0 0 255]
becomes[255 0 0 0 ]
.
Given that (a) you are working with an unknown graphics format and (b) you are having gray issues, I suspect that the high bit is not for color but for some other feature (perhaps transparency / alpha). This could easily make an expected all black image 50% gray.
Try a simple bit op on each image byte (or 16 bits or ...) and set the high bit to zero.
For a single byte, ie:
finalByte = originalByte & 0x7f;
It's also possible that this bit is a marker for run length encoding (RLE), or other varint encoding, but if you're getting the correct image sizes / dimensions, this is probably not the case.
精彩评论