Skip to main content

TIFF endian issues

3 replies [Last post]
tuj
Offline
Joined: 2006-09-29

I am having an issue writing a TIFF file. My source file starts with 49h 49h (indicating its little-endian), but when I try to write the exact same file back out, I get the first two bytes as 4dh 4dh (which indicates big-endian).

Maybe I am doing something wrong? My goal is to be able to edit the metadata and save the file back. Here is my code:

Iterator readers = ImageIO.getImageReadersByFormatName("tif");
ImageReader reader = (ImageReader)readers.next();
File source = new File(fileName);
ImageInputStream iis = ImageIO.createImageInputStream(source);
reader.setInput(iis, true);
BufferedImage bi = reader.read(0);
TIFFImageMetadata timd = (TIFFImageMetadata)reader.getImageMetadata(0);
Iterator writers = ImageIO.getImageWritersByFormatName("tif");
ImageWriter writer = null;
while(writers.hasNext()){
writer = (ImageWriter)writers.next();
}
File out = new File("C:\\dev\\java\\testStubs\\tiffStrip\\output.tiff");
if(out.exists()) out.delete();
out.createNewFile();
ImageOutputStream ios = ImageIO.createImageOutputStream(out);
writer.setOutput(ios);
IIOImage img = new IIOImage(bi, null, timd);
writer.write(null, img, null);

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Brian Burkhalter

The output byte order is set on the basis of the stream metadata object

http://download.java.net/media/jai-imageio/javadoc/1.1-beta/com/sun/medi...

provided to the writer. If that object is null it is derived from the
ByteOrder of the output stream.

Brian

On Mon, 2 Oct 2006, jai-imageio@javadesktop.org wrote:

> I am having an issue writing a TIFF file. My source file starts with 49h 49h (indicating its little-endian), but when I try to write the exact same file back out, I get the first two bytes as 4dh 4dh (which indicates big-endian).
>
> Maybe I am doing something wrong? My goal is to be able to edit the metadata and save the file back. Here is my code:
>
> Iterator readers = ImageIO.getImageReadersByFormatName("tif");
> ImageReader reader = (ImageReader)readers.next();
> File source = new File(fileName);
> ImageInputStream iis = ImageIO.createImageInputStream(source);
> reader.setInput(iis, true);
> BufferedImage bi = reader.read(0);
> TIFFImageMetadata timd = (TIFFImageMetadata)reader.getImageMetadata(0);
> Iterator writers = ImageIO.getImageWritersByFormatName("tif");
> ImageWriter writer = null;
> while(writers.hasNext()){
> writer = (ImageWriter)writers.next();
> }
> File out = new File("C:\\dev\\java\\testStubs\\tiffStrip\\output.tiff");
> if(out.exists()) out.delete();
> out.createNewFile();
> ImageOutputStream ios = ImageIO.createImageOutputStream(out);
> writer.setOutput(ios);
> IIOImage img = new IIOImage(bi, null, timd);
> writer.write(null, img, null);
> [Message sent by forum member 'tuj' (tuj)]
>
> http://forums.java.net/jive/thread.jspa?messageID=158826
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
> For additional commands, e-mail: interest-help@jai-imageio.dev.java.net
>
>

----------------
Brian Burkhalter
Java Media, Imaging, and Graphics
Sun Microsystems, Inc.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This email message is for the sole use of the intended recipient(s)
and may contain confidential and privileged information. Any
unauthorized review, use, disclosure or distribution is prohibited.
If you are not the intended recipient, please contact the sender by
reply email and destroy all copies of the original message.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---------------------------------------------------------------------
To unsubscribe, e-mail: interest-unsubscribe@jai-imageio.dev.java.net
For additional commands, e-mail: interest-help@jai-imageio.dev.java.net

tuj
Offline
Joined: 2006-09-29

Ok, so I checked the stream metadata and it only has one attribute which is byte order with a value of LITTLE_ENDIAN. I also specified the byte order both in my input and output streams.

Still no luck. The TIFF still outputs in big-endian format.

Just to be clear, here are the lines that I changed:

ImageInputStream iis = ImageIO.createImageInputStream(source);
iis.setByteOrder(ByteOrder.LITTLE_ENDIAN);
reader.setInput(iis, true);

and for the output:

ImageOutputStream ios = ImageIO.createImageOutputStream(out);
ios.setByteOrder(ByteOrder.LITTLE_ENDIAN);
writer.setOutput(ios);

Changing that did not have any impact on the results (output was still BIG-endian). However, I did get the stream metadata object and passed that to the imageWriter and that fixed the problem.

tuj
Offline
Joined: 2006-09-29

Passing the stream metadata object to the imageWriter solved my problem. Thanks!