Tiff to PNG

Hello,
I’m trying to convert a tiff to a PNG. And it’s not working.
I’m using the simple exemple on your website.

   public static void tranformImageTiffToPng(Resource tiffImg, WritableResource outResource) throws Exception {
        Document doc = new Document();
        DocumentBuilder builder = new DocumentBuilder(doc);

        Shape shape = builder.insertImage(tiffImg.getInputStream());
        shape.getImageData().save(outResource.getOutputStream());
    }

But it’s not working.

Here is the tiff I give as input, and the output I recieve. (it’s like the entire picture is on one line.
I’ve tried to add the JAI dependency, with maven, but it didn’t change anything.

result.png (3.8 KB)

fig2_tif.zip (7.8 KB)

What would be really great is that you tell me how to add JAI or another advanced picture manager for java with maven. I’m not under any GPL licence since I’m not using my code on production.

@rodrigue.leopold The provided code does not actually convert TIFF to PNG. ImageData.save method save the image data as is into the specified file, i.e. in your case the inserted TIFF image is saved with PNG extension. To actually convert TIFF to PNG you should use ShapeRenderer. Please see the following code:

private static void convertTIFToPNG(String inputFilePath, String outputFilePath) throws Exception
{
    Document doc = new Document();
    DocumentBuilder builder = new DocumentBuilder(doc);
    Shape shape = builder.insertImage(inputFilePath);
    shape.getShapeRenderer().save(outputFilePath, new ImageSaveOptions(SaveFormat.PNG));
}

Here is the output produced by this code on my side: out.png (15.8 KB)

Also, please note, TIF format is not directly supported by Aspose.Words. To process TIF images you should add additional dependencies:
https://docs.aspose.com/words/java/system-requirements/#optional-dependencies

You can try adding the following to the POM file:

<dependency>
    <groupId>javax.media.jai</groupId>
    <artifactId>com.springsource.javax.media.jai.core</artifactId>
    <version>1.1.3</version>
</dependency>

I’ve managed to add the dependency to the pom. I can see the jar in the classpath : javax\media\jai\com.springsource.javax.media.jai.core\1.1.3\com.springsource.javax.media.jai.core-1.1.3.jar
but it changes nothing. I still have the same result as yesterday. I changed my code to do what you told me.
Do I have to do something more ?

To give you one more hint, the following lines work.

final BufferedImage tif = ImageIO.read(tiffImg.getInputStream());
ImageIO.write(tif, "png", outResource.getOutputStream());

@rodrigue.leopold Which version of Aspose.Words for Java do you use? I cannot reproduce the problem with the latest 24.2 version of Aspose.Words.

I’m using

<aspose.version>24.2</aspose.version>
<aspose.classifier>jdk17</aspose.classifier>

To make sur I’m not introducing or missing a dependency. Here is my classpath (in the right order)
Sorry, it’s a little long, but you can see that there is JAI and JOGL and aspose 24.2

C:\Program Files\JetBrains\IntelliJ IDEA 2022.3.3\lib\idea_rt.jar
C:\Program Files\JetBrains\IntelliJ IDEA 2022.3.3\plugins\junit\lib\junit5-rt.jar
C:\Program Files\JetBrains\IntelliJ IDEA 2022.3.3\plugins\junit\lib\junit-rt.jar
C:\work\Sources\type2type\html2word\target\test-classes
C:\work\Sources\type2type\html2word\target\classes
C:\Users\leopold\.m2\repository\fr\cstb\doef\helper\2.3\helper-2.3.jar
C:\Users\leopold\.m2\repository\javax\activation\activation\1.1.1\activation-1.1.1.jar
C:\Users\leopold\.m2\repository\fr\cstb\doef\expressionLanguage\2.3\expressionLanguage-2.3.jar
C:\work\Sources\type2type\word2html\target\classes
C:\work\Sources\type2type\html2text\target\classes
C:\work\Sources\type2type\xml2html\target\classes
C:\Users\leopold\.m2\repository\javax\media\jai\com.springsource.javax.media.jai.core\1.1.3\com.springsource.javax.media.jai.core-1.1.3.jar
C:\work\Sources\type2type\commons\target\classes
C:\Users\leopold\.m2\repository\org\apache\commons\commons-jexl3\3.1\commons-jexl3-3.1.jar
C:\Users\leopold\.m2\repository\com\aspose\aspose-words\24.2\aspose-words-24.2-jdk17.jar
C:\Users\leopold\.m2\repository\com\aspose\aspose-words\24.2\aspose-words-24.2-javadoc.jar
C:\Users\leopold\.m2\repository\com\aspose\aspose-words\24.2\aspose-words-24.2-shaping-harfbuzz-plugin.jar
C:\Users\leopold\.m2\repository\org\jogamp\jogl\jogl-all-main\2.3.2\jogl-all-main-2.3.2.jar
C:\Users\leopold\.m2\repository\org\jogamp\jogl\jogl-all\2.3.2\jogl-all-2.3.2.jar
C:\Users\leopold\.m2\repository\org\jogamp\jogl\jogl-all\2.3.2\jogl-all-2.3.2-natives-android-aarch64.jar
C:\Users\leopold\.m2\repository\org\jogamp\jogl\jogl-all\2.3.2\jogl-all-2.3.2-natives-android-armv6.jar
C:\Users\leopold\.m2\repository\org\jogamp\jogl\jogl-all\2.3.2\jogl-all-2.3.2-natives-linux-amd64.jar
C:\Users\leopold\.m2\repository\org\jogamp\jogl\jogl-all\2.3.2\jogl-all-2.3.2-natives-linux-armv6.jar
C:\Users\leopold\.m2\repository\org\jogamp\jogl\jogl-all\2.3.2\jogl-all-2.3.2-natives-linux-armv6hf.jar
C:\Users\leopold\.m2\repository\org\jogamp\jogl\jogl-all\2.3.2\jogl-all-2.3.2-natives-linux-i586.jar
C:\Users\leopold\.m2\repository\org\jogamp\jogl\jogl-all\2.3.2\jogl-all-2.3.2-natives-macosx-universal.jar
C:\Users\leopold\.m2\repository\org\jogamp\jogl\jogl-all\2.3.2\jogl-all-2.3.2-natives-solaris-amd64.jar
C:\Users\leopold\.m2\repository\org\jogamp\jogl\jogl-all\2.3.2\jogl-all-2.3.2-natives-solaris-i586.jar
C:\Users\leopold\.m2\repository\org\jogamp\jogl\jogl-all\2.3.2\jogl-all-2.3.2-natives-windows-amd64.jar
C:\Users\leopold\.m2\repository\org\jogamp\jogl\jogl-all\2.3.2\jogl-all-2.3.2-natives-windows-i586.jar
C:\Users\leopold\.m2\repository\org\jogamp\gluegen\gluegen-rt-main\2.3.2\gluegen-rt-main-2.3.2.jar
C:\Users\leopold\.m2\repository\org\jogamp\gluegen\gluegen-rt\2.3.2\gluegen-rt-2.3.2.jar
C:\Users\leopold\.m2\repository\org\jogamp\gluegen\gluegen-rt\2.3.2\gluegen-rt-2.3.2-natives-android-aarch64.jar
C:\Users\leopold\.m2\repository\org\jogamp\gluegen\gluegen-rt\2.3.2\gluegen-rt-2.3.2-natives-android-armv6.jar
C:\Users\leopold\.m2\repository\org\jogamp\gluegen\gluegen-rt\2.3.2\gluegen-rt-2.3.2-natives-linux-amd64.jar
C:\Users\leopold\.m2\repository\org\jogamp\gluegen\gluegen-rt\2.3.2\gluegen-rt-2.3.2-natives-linux-armv6.jar
C:\Users\leopold\.m2\repository\org\jogamp\gluegen\gluegen-rt\2.3.2\gluegen-rt-2.3.2-natives-linux-armv6hf.jar
C:\Users\leopold\.m2\repository\org\jogamp\gluegen\gluegen-rt\2.3.2\gluegen-rt-2.3.2-natives-linux-i586.jar
C:\Users\leopold\.m2\repository\org\jogamp\gluegen\gluegen-rt\2.3.2\gluegen-rt-2.3.2-natives-macosx-universal.jar
C:\Users\leopold\.m2\repository\org\jogamp\gluegen\gluegen-rt\2.3.2\gluegen-rt-2.3.2-natives-solaris-amd64.jar
C:\Users\leopold\.m2\repository\org\jogamp\gluegen\gluegen-rt\2.3.2\gluegen-rt-2.3.2-natives-solaris-i586.jar
C:\Users\leopold\.m2\repository\org\jogamp\gluegen\gluegen-rt\2.3.2\gluegen-rt-2.3.2-natives-windows-amd64.jar
C:\Users\leopold\.m2\repository\org\jogamp\gluegen\gluegen-rt\2.3.2\gluegen-rt-2.3.2-natives-windows-i586.jar
C:\Users\leopold\.m2\repository\org\jsoup\jsoup\1.12.1\jsoup-1.12.1.jar
C:\Users\leopold\.m2\repository\com\sun\jersey\jersey-client\1.19.4\jersey-client-1.19.4.jar
C:\Users\leopold\.m2\repository\com\sun\jersey\jersey-core\1.19.4\jersey-core-1.19.4.jar
C:\Users\leopold\.m2\repository\javax\ws\rs\jsr311-api\1.1.1\jsr311-api-1.1.1.jar
C:\Users\leopold\.m2\repository\org\json\json\20180813\json-20180813.jar
C:\Users\leopold\.m2\repository\org\apache\commons\commons-lang3\3.9\commons-lang3-3.9.jar
C:\Users\leopold\.m2\repository\org\apache\commons\commons-text\1.6\commons-text-1.6.jar
C:\Users\leopold\.m2\repository\commons-logging\commons-logging\1.2\commons-logging-1.2.jar
C:\Users\leopold\.m2\repository\commons-io\commons-io\2.6\commons-io-2.6.jar
C:\Users\leopold\.m2\repository\org\slf4j\slf4j-api\1.7.26\slf4j-api-1.7.26.jar
C:\Users\leopold\.m2\repository\org\slf4j\slf4j-log4j12\1.7.26\slf4j-log4j12-1.7.26.jar
C:\Users\leopold\.m2\repository\log4j\log4j\1.2.17\log4j-1.2.17.jar
C:\Users\leopold\.m2\repository\junit\junit\4.12\junit-4.12.jar
C:\Users\leopold\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar
C:\Users\leopold\.m2\repository\org\springframework\spring-core\5.1.8.RELEASE\spring-core-5.1.8.RELEASE.jar
C:\Users\leopold\.m2\repository\org\springframework\spring-jcl\5.1.8.RELEASE\spring-jcl-5.1.8.RELEASE.jar
C:\Users\leopold\.m2\repository\org\springframework\spring-test\5.1.8.RELEASE\spring-test-5.1.8.RELEASE.jar
C:\Users\leopold\.m2\repository\org\springframework\spring-context\5.1.8.RELEASE\spring-context-5.1.8.RELEASE.jar
C:\Users\leopold\.m2\repository\org\springframework\spring-aop\5.1.8.RELEASE\spring-aop-5.1.8.RELEASE.jar
C:\Users\leopold\.m2\repository\org\springframework\spring-expression\5.1.8.RELEASE\spring-expression-5.1.8.RELEASE.jar
C:\Users\leopold\.m2\repository\org\springframework\spring-beans\5.1.8.RELEASE\spring-beans-5.1.8.RELEASE.jar
C:\Users\leopold\.m2\repository\javax\xml\bind\jaxb-api\2.3.1\jaxb-api-2.3.1.jar
C:\Users\leopold\.m2\repository\javax\activation\javax.activation-api\1.2.0\javax.activation-api-1.2.0.jar
C:\Users\leopold\.m2\repository\com\sun\xml\bind\jaxb-core\2.3.0.1\jaxb-core-2.3.0.1.jar
C:\Users\leopold\.m2\repository\com\sun\xml\bind\jaxb-impl\2.3.0.1\jaxb-impl-2.3.0.1.jar

@rodrigue.leopold Thank you for additional information. we will further investigate the issue on our side and get back to you soon.

@rodrigue.leopold We have tested the scenario on two different machines and the problem is still not reproducible.

Have you tried installing JAI in your system? As described here:
https://docs.aspose.com/words/java/system-requirements/#optional-dependencies

Unfortunately, there are no other ideas why this does not work on your side.

If it’s not a big industrial secret, can you tell me how you detect the JAR presence ?
The idea is to understand why the class is not detected . Does it have to be loaded in the main classloader ?
It’s complicated to install the JAR directly in my JVM as you propose, because machines can be used by several teams for different projects.
Did you try with oracle’s java or with temurin or another ?

@rodrigue.leopold I will consult with our Java developments and get back to you soon.

@rodrigue.leopold,

could you check if JAI classes are available using the following method?

private static boolean checkJAI()
{
    try
    {
        Class.forName("javax.media.jai.JAI");
        return true;
    }
    catch (Throwable cnf)
    {
        return false;
    }
}

In addition, please tell us which version of Java and which operating system you are using.

Hello.
I found why it’s not working. The dependency

javax\media\jai\com.springsource.javax.media.jai.codec\1.1.3\com.springsource.javax.media.jai.codec-1.1.3.jar

wasn’t in the classpath.
I don’t know exactly why, but there was a maven mixup. I deleted everything, recompiled, and rebuilt everything, and suddently this JAR appeared in the classpath, and now it’s working well.

I’ve got nice well compressed, images.
Thanks for your help.

@rodrigue.leopold It is perfect that you managed to resolve the problem.