Skip to main content

JPEG in TIFF 4x file size increase!?

7 replies [Last post]
chickiboo
Offline
Joined: 2006-06-07

First off, let me say that I'm new to this, so maybe I'm missing something obvious here... or maybe not. I've spent a week checking the archives to try and get this worked out with no success.

I'm trying to convert a group of JPEGs into a multipaged TIFF. I'm doing that just fine, except that my file size is anywhere from 3-4 times the sizes of the JPEGs combined. We have a product that is generated old style JPEG in TIFFs that are only about 50k or so bigger. (It's not batch processing them, so I'm trying to replicate the process in Java.)

The increased size is completely unacceptable, but what I cannot determine is if this in inherent in the new JPEG in TIFF compression, or if I am doing something wrong.

My code follows. I've tried to write the JPEG as one strip - hence the setting of the tiling mode and size to the image height & width, but that does not make a significant difference to the image size when that code is removed.

public void mergeImages(List imageFiles, File newTIFF) throws IOException {

//Get iterator for input images
Iterator iterator = imageFiles.iterator();

//Get tif writer and set output to file
Iterator writers = ImageIO.getImageWritersByFormatName("tif");
ImageWriter writer = (ImageWriter)writers.next();
ImageOutputStream ios = ImageIO.createImageOutputStream(newTIFF);
writer.setOutput(ios);

//Set up the writeParam to compress
TIFFImageWriteParam tiffWriteParam = new TIFFImageWriteParam(Locale.US);
tiffWriteParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
tiffWriteParam.setCompressionType("JPEG");

//Write each image out to the new file
boolean firstImage = true;
while (iterator.hasNext()) {

File imageFile = (File) iterator.next();
ImageInputStream iis = ImageIO.createImageInputStream(imageFile);

//Get a reader for the stream
Iterator readers = ImageIO.getImageReaders(iis);
ImageReader reader = (ImageReader)readers.next();
reader.setInput(iis);

//Read and write out all images from input file
//We do not know the number of images up front, so end loop on Out of Bounds
try{
for(int i=0;true;i++){

IIOMetadata iioImageMetadata = reader.getImageMetadata(i);

//set tile height to image height to write single strip
int imageHeight = reader.getHeight(i);
int imageWidth = reader.getWidth(i);
tiffWriteParam.setTilingMode(ImageWriteParam.MODE_EXPLICIT);
tiffWriteParam.setTiling(imageWidth, imageHeight, 0, 0);

BufferedImage bi = reader.read(i, null);
IIOImage image = new IIOImage(bi, null, iioImageMetadata);
if(firstImage==false)
writer.writeInsert(-1,image, tiffWriteParam);
else {
writer.write(null, image, tiffWriteParam);
firstImage = false;
}
}
} catch (IndexOutOfBoundsException e){
//End loop on index out of bounds
}

//Done writing all images for this image
reader.dispose();

}
//End writing of all files
writer.dispose();
}

Message was edited by: chickiboo

Reply viewing options

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

jai-imageio@javadesktop.org wrote:
> First off, let me say that I'm new to this, so maybe I'm missing something obvious here... or maybe not. I've spent a week checking the archives to try and get this worked out with no success.
>
> I'm trying to convert a group of JPEGs into a multipaged TIFF. I'm doing that just fine, except that my file size is anywhere from 3-4 times the size of the JPEG. We have a product that is generated old style JPEG in TIFFs that are only about 50k or so bigger. (It's not batch processing them, so I'm trying to replicate the process in Java.)
>
> The increased size is completely unacceptable, but what I cannot determine is if this in inherent in the new JPEG in TIFF compression, or if I am doing something wrong.
>
> My code follows. I've tried to write the JPEG as one strip - hence the setting of the tiling mode and size to the image height & width, but that does not make a significant difference to the image size when that code is removed.

My guess would be that you need to set the amount of compression for the
JPEG part so it compresses more... but I don't know offhand how to do
that. Hopefully some metadata item.

-Bob

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

baskaraninfo
Offline
Joined: 2007-10-24

Hi

I want to convert a JPEG image file into TIFF file with good compression ratio.

I have tried using sun's JAI package. It converts the JPEG file to TIFF file, buts the resulted TIFF files size is too large.
Ex. 30 KB JPG into 1 MB TIFF file

I am using JAI.create(operation, RenderedOp, outputFile, "TIFF", null)
[b]
In the documentation, they have specified that, due to some patent reasons, JAI currently not supports compression.
[/b]

I found, there is extended Jai package download page in source forge, which I am currently trying at this time,
[b]http://sourceforge.net/projects/xtiff-jai/[/b]

The above link may be helpful for you!

bpb
Offline
Joined: 2004-06-23

Marco's comment here

http://forums.java.net/jive/thread.jspa?threadID=33251&tstart=0

is spot on. In any case I don't know which version of the docs you are reading.

For image reading and writing we recommend JAI Image I/O Tools

https://jai-imageio.dev.java.net

as opposed to the JAI codecs.

Brian

> Hi
>
> I want to convert a JPEG image file into TIFF file
> with good compression ratio.
>
> I have tried using sun's JAI package. It converts the
> JPEG file to TIFF file, buts the resulted TIFF files
> size is too large.
> Ex. 30 KB JPG into 1 MB TIFF file
>
> I am using JAI.create(operation, RenderedOp,
> outputFile, "TIFF", null)
> [b]
> In the documentation, they have specified that, due
> to some patent reasons, JAI currently not supports
> compression.
> [/b]
>
> I found, there is extended Jai package download page
> in source forge, which I am currently trying at this
> time,
> [b]http://sourceforge.net/projects/xtiff-jai/[/b]
>
> The above link may be helpful for you!

jxc
Offline
Joined: 2005-02-24

You might like to try this:
http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/ImageWriteParam.html#setCompressionQuality(float)

-James

chickiboo
Offline
Joined: 2006-06-07

> You might like to try this:
> http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/
> ImageWriteParam.html#setCompressionQuality(float)
>
> -James

I did try checking the compression quality values and get null back. The compression quality is currently set at 1. I've tried setting it at 0.5 and that works - the image is actually smaller than the original.

However we do have a requirement not to diminish the image quality. I may be able to play around with this and find a happy medium, but am hoping that there might be a lossless suggestion too.

bpb
Offline
Joined: 2004-06-23

> > You might like to try this:
> >
> http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/
>
> > ImageWriteParam.html#setCompressionQuality(float)
> >
> > -James
>
> I did try checking the compression quality values and
> get null back. The compression quality is currently
> set at 1. I've tried setting it at 0.5 and that
> works - the image is actually smaller than the
> original.
>
> However we do have a requirement not to diminish the
> image quality. I may be able to play around with
> this and find a happy medium, but am hoping that
> there might be a lossless suggestion too.

This looks like the same old thing that arose in another thread: you can't get something for free. The JPEG-in-TIFF "format" uses baseline DCT JPEG which is inherently lossy. If you use baseline JPEG you will get some distortion. The real question is whether it is psychovisually acceptable.

If you want lossless compression in TIFF then all we support is LZW and Deflate/Zip unless you have bilevel data in which case you could also use RLE, ITU T.4, or ITU T.6.

Brian

jxc
Offline
Joined: 2005-02-24

> > I did try checking the compression quality values
> and
> > get null back. The compression quality is
> currently
> > set at 1. I've tried setting it at 0.5 and that
> > works - the image is actually smaller than the
> > original.
> >
> > However we do have a requirement not to diminish
> the
> > image quality. I may be able to play around with
> > this and find a happy medium, but am hoping that
> > there might be a lossless suggestion too.
>
> This looks like the same old thing that arose in
> another thread: you can't get something for free. The
> JPEG-in-TIFF "format" uses baseline DCT JPEG which is
> inherently lossy. If you use baseline JPEG you will
> get some distortion. The real question is whether it
> is psychovisually acceptable.
>
> If you want lossless compression in TIFF then all we
> support is LZW and Deflate/Zip unless you have
> bilevel data in which case you could also use RLE,
> ITU T.4, or ITU T.6.

I was wondering whether the TIFF writer can embed a
JPEG bitstream into a TIFF file without decompressing
and compressing. I imagine that it could be done more
easily than the "lossless JPEG rotation" issue,
https://jai-imageio-core.dev.java.net/issues/show_bug.cgi?id=116

Thanks,
-James