Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / June 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Armin Gajda - 13 Jun 2006 14:03 GMT
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

Signature

Bye Armin

Andrey Kuznetsov - 13 Jun 2006 17:08 GMT
> 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

Signature

http://uio.imagero.com Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.