Image processing problems with bufferedImage

I have a problem with trying to normalize an image in Java.
My image is 16-bit, so it is stored in a short array.
By normalize I mean that I am trying to set the pixel with lowest intensity to Short.MIN_VALUE and the pixel with the highest intensity to Short.MAX_VALUE and all the other pixels in between somewhere in the middle.

I construct the buffered image like this...
bi = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_GRAY);
Then after I do my normalization code on the short array, I set the buffered image to use the short array like this...
bi.getRaster().setDataElements(0, 0, width, height, shortbuf);

The problem is that BufferedImage is set to be TYPE_USHORT_GRAY. Java doesn't have unsigned shorts, and there is no type defining a grayscale image using signed shorts. So the maximum value I can store in my shorts is half of what I could store in an unsigned short, resulting in a crappy normalization.

Can anyone tell me another way to do this?

Comments

  • Use a larger primitive type, like int.

    : I have a problem with trying to normalize an image in Java.
    : My image is 16-bit, so it is stored in a short array.
    : By normalize I mean that I am trying to set the pixel with lowest intensity to Short.MIN_VALUE and the pixel with the highest intensity to Short.MAX_VALUE and all the other pixels in between somewhere in the middle.
    :
    : I construct the buffered image like this...
    : bi = new BufferedImage(width, height, BufferedImage.TYPE_USHORT_GRAY);
    : Then after I do my normalization code on the short array, I set the buffered image to use the short array like this...
    : bi.getRaster().setDataElements(0, 0, width, height, shortbuf);
    :
    : The problem is that BufferedImage is set to be TYPE_USHORT_GRAY. Java doesn't have unsigned shorts, and there is no type defining a grayscale image using signed shorts. So the maximum value I can store in my shorts is half of what I could store in an unsigned short, resulting in a crappy normalization.
    :
    : Can anyone tell me another way to do this?
    :

    [italic][blue]Just my 2 bits[/blue][/italic]

  • Thanks for replying.
    I've tried this, unfortunately the specification of TYPE_USHORT_GRAY causes the program to crash because it is expecting the short data type. Also there is no TYPE_INT_GRAY in the specification. All of the INT types are for color pictures, no grayscale.
    Ack! Why would Java not provide unsigned types and then make 16-bit grayscale buffered images only work properly with unsigned data?!?!?
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories