How to convert a Fax-Tiff with JAI to a PNG

A

Armin Gajda

Hi,

we get our faxes over an fax server as TIFF. The faxes should be
converted to png, what I want to do with JAI. I have some problems and
don't know what is the best way to seolve them:

My code:

SeekableStream s = new ByteArraySeekableStream(imageData);
ImageDecoder dec = ImageCodec.createImageDecoder("tiff", s, null);
int pageCount = dec.getNumPages();

for (int i = 0; i < pageCount; i++) {
RenderedImage ri = dec.decodeAsRenderedImage(i);
PlanarImage pi = scaleImage(ri);

// write png image
ByteArrayOutputStream out = new ByteArrayOutputStream ();
PNGEncodeParam pngParam =
PNGEncodeParam.getDefaultEncodeParam(pi);
pngParam.setBitDepth(2);
PNGImageEncoder encoder = new PNGImageEncoder(out, pngParam);
encoder.encode(pi);

byte[] pngImage = out.toByteArray();
out.flush();

images.add(pngImage);

// for debug
ByteArrayInputStream in = new ByteArrayInputStream(pngImage);
File file = new File("C:/ra/", i + ".png");
IOUtil.writeToFile(in, file);
in.close();
}

private PlanarImage scaleImage(RenderedImage ri) {
ParameterBlock pb = new ParameterBlock();
pb.addSource(ri);

pb.add(0.5f);
pb.add(1.0f);
pb.add(0.0f);
pb.add(0.0f);
pb.add(Interpolation.getInstance(Interpolation.INTERP_NEAREST));

return JAI.create("scale", pb);
}

1. With an ordinary conversion the pngs become wider than tall. For that
I added the function scaleImage, that resizes the image to the correct size.

2. The bit depth after the conversion is 1 Bit. If i convert the image
with 'tiff2png' the bit depth is 2 bit and the png looks mich better.
For that I have added the PNGEncodeParam but it had no effect.

Is there an easy way to convert the Tiff to a png with out loosing any
quality (or as less as possible).

This is what TiffInfo says about the TIFF:

Page 1 of 1
IFD OFFSET = 63778
The number of tags = 12

256 Image Width LONG 1 1728 (6c0 Hex)
257 Image Length LONG 1 1146 (47a Hex)
258 Bits Per Sample SHORT 1 1
259 Compression SHORT 1 2
262 Photometric SHORT 1 0
266 Fill Order SHORT 1 2
273 Strip Offsets LONG 1 8 (8 Hex)
277 Samples Per Pixel SHORT 1 1
278 Rows Per Strip LONG 1 1146 (47a Hex)
279 Strip Byte Counts LONG 1 63769 (f919 Hex)
282 X Resolution RATIONAL 1 <63928> 200 / 1 = 200.000
283 Y Resolution RATIONAL 1 <63936> 100 / 1 = 100.000
 
A

Andrey Kuznetsov

1. With an ordinary conversion the pngs become wider than tall.

because X Resolution is 200 and Y Resolution is 100.
You can set png pixel aspect ratio with PHYS chunk
(don't ask me how to do it with JAI)

Andrey
 

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
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top