BufferedImage and bit depth

U

Uli Kunkel

I'm trying to convert 24bit depth jpg to 16 bit.
I'm using BufferedImage.TYPE_USHORT_565_RGB option to do so.
There are 2 thing that bother me:


1. Anyway when I write the file it always says it is 24 bit in the header.
The photo changes but the bit depth info stays the same.

2. The photo is smaller with 24 bit depth (BufferedImage.TYPE_INT_RGB)
than with 16 bit depth (BufferedImage.TYPE_USHORT_565_RGB) ?!?

Is it even recommendable to have 16bit jpegs?

Thanks in advance for any suggestions.
 
M

Mayeul

Uli said:
I'm trying to convert 24bit depth jpg to 16 bit.
I'm using BufferedImage.TYPE_USHORT_565_RGB option to do so.
There are 2 thing that bother me:


1. Anyway when I write the file it always says it is 24 bit in the header.
The photo changes but the bit depth info stays the same.

I think I see the problem. Your BufferedImage probably contains 16 bit
data all right.

But whatever API you use to export the image to file seems to ignore the
bit depth of your BufferedImage, and just converts it to 24 bit, then
exports to file.

There might exist some way to explicitly state you want to export as a
16 bit depth image (and then you probably won't even have to do that
conversion yourself, depending on how good the exporter's conversion
is.) You may want to look for it.
I am not too used with fine-tuning ImageIO exports myself.
2. The photo is smaller with 24 bit depth (BufferedImage.TYPE_INT_RGB)
than with 16 bit depth (BufferedImage.TYPE_USHORT_565_RGB) ?!?

The cause probably is that the exporter you used applies a lesser
compression by default. Since bit depth is still 24 bit, it may very
well compress worse.

Here again, I suppose it can be fine-tuned when using ImageIO.
 
J

Joshua Cranmer

Uli said:
1. Anyway when I write the file it always says it is 24 bit in the header.
The photo changes but the bit depth info stays the same.

You're changing the in-memory type of the image, which does not
necessarily correspond to the file representation.

If you play around with ImageIO, you can get much more fine-tuned
control over the file output of the image, including color depth.
2. The photo is smaller with 24 bit depth (BufferedImage.TYPE_INT_RGB)
than with 16 bit depth (BufferedImage.TYPE_USHORT_565_RGB) ?!?

It could be that the 16-bit file used less lossy parameters (i.e., less
able to be compressed) than the 24-bit file. Or maybe the 24-bit file
had more redundancy that compressed more easily.
 
K

Knute Johnson

Uli said:
I'm trying to convert 24bit depth jpg to 16 bit.
I'm using BufferedImage.TYPE_USHORT_565_RGB option to do so.
There are 2 thing that bother me:


1. Anyway when I write the file it always says it is 24 bit in the header.
The photo changes but the bit depth info stays the same.

2. The photo is smaller with 24 bit depth (BufferedImage.TYPE_INT_RGB)
than with 16 bit depth (BufferedImage.TYPE_USHORT_565_RGB) ?!?

Is it even recommendable to have 16bit jpegs?

Thanks in advance for any suggestions.

I don't think that is possible. I'm not an expert but there are two
JPEG file formats, JFIF and Exif. Both of them store the data in a
format according to their color space. The BufferedImage type is not
relevant to the file format other than it determines the color space.

Adjusting the compression of a JPEG written using ImageIO is fairly easy
while not obvious. Below is code I use to write a JPEG image and
control the compression.

You should look at JPEGImageWriteParam class and the docs, "JPEG
Metadata Format Specification and Usage Notes" which you will find near
the top of the JPEGImageWriteParam doc page.

public static void writeJPEG(RenderedImage image, float quality,
File file)
throws IOException {
if (quality < 0.0f || quality > 1.0f)
throw new IllegalArgumentException("0.0 < Quality < 1.0");
ImageWriter writer = null;
Iterator iter = ImageIO.getImageWritersByFormatName("JPEG");
if (!iter.hasNext())
throw new IOException("No Writers Available");
writer = (ImageWriter)iter.next();
if (file.exists())
file.delete();
ImageOutputStream ios = ImageIO.createImageOutputStream(file);
writer.setOutput(ios);
JPEGImageWriteParam iwp = new JPEGImageWriteParam(null);
iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
iwp.setCompressionQuality(quality);
writer.write(null,new IIOImage(image,null,null),iwp);
ios.flush();
writer.dispose();
ios.close();
}
 
R

Roedy Green

I'm trying to convert 24bit depth jpg to 16 bit.

Do you mean to have a palette map with 16-bit lookup?
--
Roedy Green Canadian Mind Products
http://mindprod.com

If everyone lived the way people do in Vancouver, we would need three more entire planets to support us.
~ Guy Dauncey
 
F

floffy

Joshua Cranmer said:
You're changing the in-memory type of the image, which does not
necessarily correspond to the file representation.

If you play around with ImageIO, you can get much more fine-tuned
control over the file output of the image, including color depth.


It could be that the 16-bit file used less lossy parameters (i.e., less
 
C

charlesbos73

....

Do you mean to have a palette map with 16-bit lookup?

The OP wrote:

OP: I'm using BufferedImage.TYPE_USHORT_*565_RGB* option to do so.
OP: ...
OP: Is it even recommendable to have 16bit *jpegs*?

So obviously he's not talking about using a palette map...
 
U

Uli Kunkel

Uli said:
I'm trying to convert 24bit depth jpg to 16 bit.
I'm using BufferedImage.TYPE_USHORT_565_RGB option to do so.
There are 2 thing that bother me:


1. Anyway when I write the file it always says it is 24 bit in the header.
The photo changes but the bit depth info stays the same.

2. The photo is smaller with 24 bit depth (BufferedImage.TYPE_INT_RGB)
than with 16 bit depth (BufferedImage.TYPE_USHORT_565_RGB) ?!?

Is it even recommendable to have 16bit jpegs?

Thanks in advance for any suggestions.


Thank you all for your answers.
Based on them I decided not to wory :) and do go with 24 bit depth.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top