Results 1 to 9 of 9
  1. #1
    galele87 is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default 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:

    Java 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).

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,582
    Rep Power
    23

    Default Re: How to convert a byte array into 16-bit image

    java.lang.ArrayIndexOutOfBoundsException is raised when performing bimage.setData(raster).
    Are the width and height values with the bounds of the image?

  3. #3
    galele87 is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default 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.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,582
    Rep Power
    23

    Default Re: How to convert a byte array into 16-bit image

    Remember arrays are 0 based. The largest valid index is the size - 1

  5. #5
    galele87 is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default 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

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,582
    Rep Power
    23

    Default 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.

  7. #7
    galele87 is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default 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 ^^

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,582
    Rep Power
    23

    Default Re: How to convert a byte array into 16-bit image

    Sorry, I'm not familiar with what you are doing with images.

  9. #9
    galele87 is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default Re: How to convert a byte array into 16-bit image

    yeah i can understand.. ^^ however, thank you so much for your help.

Similar Threads

  1. convert byte array into char array
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 09-13-2011, 11:32 AM
  2. convert byte array to string.
    By newbiejava in forum New To Java
    Replies: 9
    Last Post: 07-30-2010, 08:00 PM
  3. How Convert a Byte array to a image format
    By perlWhite in forum New To Java
    Replies: 2
    Last Post: 08-23-2009, 08:11 PM
  4. How Convert a Byte array to a image format
    By perlWhite in forum Advanced Java
    Replies: 1
    Last Post: 08-22-2009, 07:05 PM
  5. convert byte[] to image
    By brijesh.baser in forum Java Servlet
    Replies: 0
    Last Post: 03-13-2009, 01:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •