views:

900

answers:

3

Under Java what is the best way to go about converting an TIF file to a PNG?

Simplicity is preferable, but if the simplest way is to use a third party library then I would consider that solution.

+2  A: 

Java advanced imaging APi is a good library for image manipulations

http://java.sun.com/products/java-media/jai/iio.html

MLefrancois
Yeah I was looking at that, I honestly have no idea how to install it. They have a download ZIP that contains a JAR file with an EXE file inside of it. I've never seen that before.
James McMahon
+3  A: 

First, install JAI. Then install JAI/ImageIO. Then do

public static void main(final String[] args) throws Exception
{
    final BufferedImage tif = ImageIO.read(new File("test.tif"));
    ImageIO.write(tif, "png", new File("test.png"));
}
Jonathan Feinberg
Can you explain what is going on with the non-standard install? Usually Java libraries are just JAR files that you place inside your CLASSPATH, but JAI installs native DLLs as well. How does this work if you deploy to a webserver?
James McMahon
You can also get pure-java JAI, but there's no reason to do so, if your platform is supported by the native code.If you deploy your app to a web server, then the server's JRE must also have the JAI and JAI/ImageIO extensions installed as well.
Jonathan Feinberg
@Jonathan Feinberg, Do you need to include the JARs on the classpath or does installing the extensions make it part of the JRE?
James McMahon
The installers do everything required to make it "just work" with your JRE.
Jonathan Feinberg
@Jonathan Feinberg, I am having no luck with this approach; the ImageIO.read() call is returning null. Any suggestions?
James McMahon
I should also note that I am using the approach laid out at http://www.exampledepot.com/egs/javax.imageio/DetermineFormat.html to check to see if TIF files are supported for read operations, according to canReadFormat(), they are not.
James McMahon
Right. First you have to install JAI and JAI/ImageIO.
Jonathan Feinberg
@Jonathan Feinberg, yeah I installed both of those, I've ever tried copying the DLLs manually into various JDK and JREs I have installed on my system. I am having no luck.
James McMahon
Once I include the JAI and JAI ImageIO JARs in my path it seems to be working. I think this may bypass the DLLs and make native Java calls, which may be less preformant.
James McMahon
This site has some good tips to see if JAI is installed properly programmatically http://forums.java.net/jive/message.jspa?messageID=64311.
James McMahon
+3  A: 

Use imageMagic java libraries like im4java, their performance and quality is much better then JAI

for example:

import org.im4java.core.ConvertCmd;
import org.im4java.core.IMOperation;

public static void convertTifToPng(File inputImage, File outputImage){
  IMOperation op = new IMOperation();
  op.addImage(); //place holder for input file
  op.addImage(); //place holder for output file

  ConvertCmd convert = new ConvertCmd();
  convert.run(op, new Object[]{inputImage.getAbsolutePath(), outputImage.getAbsolutePath()});
}

maven dependency for im4java is

<dependency>
  <groupId>im4java</groupId>
  <artifactId>im4java</artifactId>
  <version>0.98.0</version>
</dependency>
I agree that IM's quality is excellent, but I would be very nervous about using it in-process like that (because, in my opinion, the engineering quality is not good, and there can be fatal crashes). I'd instead shell-out to `convert` using ProcessBuilder, if I were going to use IM.
Jonathan Feinberg
@giladbu: ImageMagick is great and I do it the way Jonathan is suggesting but I just want to comment on the "quality" part: I very much doubt that JAI would produce an image of worse quality than ImageMagick when *reading* a lossy TIFF (should the TIFF of the OP be lossy). As for PNG, it's lossless and JAI cannot go wrong there. So in the TIFF-to-PNG case I don't agree that ImageMagick's quality would be better than JAI. For TIFF-to-JPG now we'd be talking about something entirely different and I'd use ImageMagick without thinking twice about it.
Webinator
Hmm I didn't realize that PNGs were lossless.
James McMahon
So what does the ImageMagic approach require? An installation of the ImageMagic command line tools and then plugging the im4java jars into my project?
James McMahon