How to convert a byte array into 16-bit image
Hi.. everybody can excuse me for my poor english, i'm italian :P
I hope i'm writing in the right section of the forum. I need some helps to solve the conversion suggested by the title of the thread.
I state that I am working with NetBeans IDE 7.0.1 and the last jdk version, i suppose.
Problem: i'm working with an array filled of consecutive bytes that describe the pixels of an image. I know that these bytes are encoded in pairs of two and i want to get the file (png, bmp, tif .. it doesn't matter) in any way, not necessarily in the most efficient. Ideas?
I followed this guideline: i have created an array of short and mapped into it the bytes (2 by 2). With him i have built a DataBufferUShort. After that, i have created a new BufferedImage, a MultiPixelPackedSampleModel (suitable for images with multiple bytes per pixel), a WritableRaster through the method createPackedRaster of the class Raster (also suitable for the purpose, since i can specify the bits of the pixels) and finally set the raster into the BufferedImage. Here the pith of my code:
Code:
short[] shortArray;
DataBufferUShort buffer;
MultiPixelPackedSampleModel model;
WritableRaster raster;
shortArray = new short[totBytes / 2];
for (int i = 0; i < shortArray.length; i++)
shortArray[i] = (short)(((pixels[2*i+1]&0xFF)<<8) | (pixels[2*i]&0xFF));
buffer = new DataBufferUShort(shortArray, totBytes/2);
BufferedImage bimage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
model = new MultiPixelPackedSampleModel(DataBuffer.TYPE_USHORT, width, height, 16);
raster = Raster.createPackedRaster(buffer, width, height, 16, null);
bimage.setData(raster);
try {
File outputfile = new File("temp.png");
ImageIO.write(bimage, "png", outputfile);
} catch (IOException e) {}
// totBytes, width e height are parameters passed to the function.
It's not probably the best solution for purpose, however a java.lang.ArrayIndexOutOfBoundsException is raised when performing bimage.setData(raster). Some good soul has a 30 seconds to help me? Perhaps suggesting an alternative route that passes through other classes. Thanks in advance to all (expecially for my english).
Re: How to convert a byte array into 16-bit image
Quote:
java.lang.ArrayIndexOutOfBoundsException is raised when performing bimage.setData(raster).
Are the width and height values with the bounds of the image?
Re: How to convert a byte array into 16-bit image
I think so, if i have understand what you mean. The same values of width and height are passed to all constructors: BufferdImage, MultiPixelPackedSampleModel and createPackedRaster. In my test the width and height values are respectively 3072 and 2252 and the exact exception raised is java.lang.ArrayIndexOutOfBoundsException: 3072.
Re: How to convert a byte array into 16-bit image
Remember arrays are 0 based. The largest valid index is the size - 1
Re: How to convert a byte array into 16-bit image
yes, i know it.. but i'm not understanding the reason of this exception.. i'm making a conceptual error or i'm using that methods in a wrong way. Width and hieght are the same for all functions in that piece of code. I'm freaking out :P
Re: How to convert a byte array into 16-bit image
What is the size of your array where the exception occurs?
Say it is: anArray[4]
then the indexes will go from 0 to 3. An index of 4 with be out of bounds.
Check your code to be sure the index does get too big.
If you can not see the problem add printlns to print the value of the indexes and the size of the array you are indexing.
Re: How to convert a byte array into 16-bit image
a little new: the exception was caused by the third parameter passed to BufferedImage Constructor.. i must use TYPE_USHORT_GRAY instead of TYPE_INT_RGB.. now an image comes out but it's not correct ^^
Re: How to convert a byte array into 16-bit image
Sorry, I'm not familiar with what you are doing with images.
Re: How to convert a byte array into 16-bit image
yeah i can understand.. ^^ however, thank you so much for your help.