Skip to main content

[JAI] Tiff image compression

3 replies [Last post]
Anonymous

I want to compress a tiff file with the following settings:
Group 4 Fax Encoding Compression 96 X 96 DPI Resolution

What settings should I use? Here is the code that I am using.

TIFFEncodeParam param = new TIFFEncodeParam();
param.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);

I have tried to use all the available compression constants defined in TIFFEncodeParam but with no
luck. It either throws an exception or creates a file with 770kb in length. The file is a blank
white page in tiff format and the windows native application is generating this file with size
around 40kb.

TIA

http://www.ProblemSolvingSkill.net
Hone your problem-solving skills

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

Reply viewing options

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

I'm going to say the same thing here that I did on JavaRanch yesterday,
which you conveniently ignored and never responded to.

As the documentation clearly states you should be using
COMPRESSION_GROUP4 for Group 4 Fax encoding. If it's throwing
exceptions one of the first things I would check is that it's a binary
image and not a color image, that would explain the errors and the
larger file size with other encodings. I suggest you provide a complete
test case that can be compiled and run without modification and provide
the image you're using to go along with it. Furthermore, I believe the
folks at Sun would rather we use Image I/O
(https://imageio.dev.java.net/) for reading and writing, but I'm not
100% sure on that.

Bala Paranj wrote:
> I want to compress a tiff file with the following settings:
> Group 4 Fax Encoding Compression 96 X 96 DPI Resolution
>
> What settings should I use? Here is the code that I am using.
>
> TIFFEncodeParam param = new TIFFEncodeParam();
> param.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);
>
> I have tried to use all the available compression constants defined in TIFFEncodeParam but with no
> luck. It either throws an exception or creates a file with 770kb in length. The file is a blank
> white page in tiff format and the windows native application is generating this file with size
> around 40kb.
>
> TIA
>
> http://www.ProblemSolvingSkill.net
> Hone your problem-solving skills
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>
>

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

Bala Paranj

Here is the test case.

package test.image.blank;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import com.sun.media.jai.codec.ImageCodec;
import com.sun.media.jai.codec.ImageEncoder;
import com.sun.media.jai.codec.TIFFEncodeParam;

public class BlankImage {
private static final long serialVersionUID = 4359602001808932422L;

public BlankImage() {
}

public void create() {
try
{
BufferedImage bi = new BufferedImage(816, 324, 1);

Graphics2D big = bi.createGraphics();

big.setColor(Color.white);
big.fillRect(0, 0, 816, 324);

ByteArrayOutputStream out = new ByteArrayOutputStream();
TIFFEncodeParam param = new TIFFEncodeParam();
param.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);

ImageEncoder tiffEncoder = ImageCodec.createImageEncoder("TIFF",
out, param);

tiffEncoder.encode(bi);

String directory = System.getProperty("user.dir");
String separator = System.getProperty("file.separator");

String outFileName = directory + separator + "blank.tif";
System.out.println("File is created in the folder " + outFileName);
OutputStream outStr = new FileOutputStream(outFileName);

BufferedOutputStream bos = new BufferedOutputStream(outStr);

TIFFEncodeParam encParam = new TIFFEncodeParam();
ImageEncoder encImage = ImageCodec.createImageEncoder("tiff", bos, encParam);
encImage.encode(bi);
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void main(String[] arguments) {
BlankImage blank = new BlankImage();
blank.create();
}

}

A simple blank image with white background results in 776 KB file!
I have used COMPRESSION_DEFLATE because if I use COMPRESSION_GROUP4, I get the following error:

"java.lang.Error: Bilevel encodings are supported for bilevel images only."

How can I bilevel encode the image. TIA.

--- Ken Blair wrote:

> I'm going to say the same thing here that I did on JavaRanch yesterday,
> which you conveniently ignored and never responded to.
>
> As the documentation clearly states you should be using
> COMPRESSION_GROUP4 for Group 4 Fax encoding. If it's throwing
> exceptions one of the first things I would check is that it's a binary
> image and not a color image, that would explain the errors and the
> larger file size with other encodings. I suggest you provide a complete
> test case that can be compiled and run without modification and provide
> the image you're using to go along with it. Furthermore, I believe the
> folks at Sun would rather we use Image I/O
> (https://imageio.dev.java.net/) for reading and writing, but I'm not
> 100% sure on that.
>
> Bala Paranj wrote:
> > I want to compress a tiff file with the following settings:
> > Group 4 Fax Encoding Compression 96 X 96 DPI Resolution
> >
> > What settings should I use? Here is the code that I am using.
> >
> > TIFFEncodeParam param = new TIFFEncodeParam();
> > param.setCompression(TIFFEncodeParam.COMPRESSION_DEFLATE);
> >
> > I have tried to use all the available compression constants defined in TIFFEncodeParam but
> with no
> > luck. It either throws an exception or creates a file with 770kb in length. The file is a
> blank
> > white page in tiff format and the windows native application is generating this file with size
> > around 40kb.
> >
> > TIA
> >
> > http://www.ProblemSolvingSkill.net
> > Hone your problem-solving skills
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> > For additional commands, e-mail: interest-help@jai.dev.java.net
> >
> >
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: interest-unsubscribe@jai.dev.java.net
> For additional commands, e-mail: interest-help@jai.dev.java.net
>
>

http://www.ProblemSolvingSkill.net
Hone your problem-solving skills

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

Aaron Bruegl

>
> How can I bilevel encode the image.
Use this operator:

http://download.java.net/media/jai/javadoc/1.1.3-beta/javax/media/jai/op...

Then use COMPRESSION_GROUP4.

-Aaron

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