2

I need to know how feasible is it to write a function in java to read a ppm file and convert it to jpg or bmp format. Anyone has experience with this? I am able to achieve the goal using tools such as ImageMagick but I want to do it in pure Java way.

Vombat
  • 1,156
  • 1
  • 15
  • 27
  • 1
    If you want to have a pure Java solution you could use JAI (Java Advanced Imaging) and start with this post [examples for Advanced Imaging Image I/O API](http://www.oracle.com/technetwork/articles/java/advancedimage-140153.html). Even the version used in the post is a bit outdated you should get the picture how to use JAI. – SubOptimal Mar 09 '15 at 15:04
  • JPEG is a bit steep but the BMP format is delightfully simple. Barring its various oddities, of course. Find yourself a good description of the headers and you're almost done. – Jongware Mar 09 '15 at 21:54
  • An older duplicate of this question: https://stackoverflow.com/questions/3649518/how-to-convert-ppm-to-jpeg-in-java – Arto Bendiken Aug 13 '17 at 13:08

2 Answers2

4

Beside the old Sun JAI implementation there are additional ImageIO plugins TwelveMonkeys ImageIO which extend the ImageIO implementation in the JDK/JRE.

Below is a small example using those plugins. The example depends on version 3.1-SNAPSHOT (earlier release version does not provide PNM support). So you need to build the plugin project first.

./pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" 
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>sub.optimal</groupId>
    <artifactId>JAI-Demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>com.twelvemonkeys.imageio</groupId>
            <artifactId>imageio-pnm</artifactId>
            <version>3.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>com.twelvemonkeys.imageio</groupId>
            <artifactId>imageio-jpeg</artifactId>
            <version>3.1-SNAPSHOT</version>
        </dependency>
    </dependencies>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.7</maven.compiler.source>
        <maven.compiler.target>1.7</maven.compiler.target>
    </properties>
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.3.2</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>java</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <killAfter>-1</killAfter>
                    <mainClass>sub.optimal.jai.Main</mainClass>
                    <systemProperties>
                        <systemProperty>
                            <key>user.dir</key>
                            <value>${project.build.directory}\classes</value>
                        </systemProperty>
                    </systemProperties>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

./src/main/java/sub/optimal/jai/Main.java

package sub.optimal.jai;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import javax.imageio.ImageIO;

/**
 *
 * @author SubOptimal
 */
public class Main {

    public static void main(String[] args) throws IOException {
        String outFormat = "%-17s: %s%n";

        String filesDirectory = System.getProperty("user.dir");
        System.out.printf(outFormat, "files directory", filesDirectory);

        System.out.printf(outFormat, "supported formats", Arrays.toString(ImageIO.getWriterFormatNames()));

        Path inputFile = Paths.get(filesDirectory, "pond.ppm");
        System.out.printf(outFormat, "input file", inputFile.toAbsolutePath());
        InputStream is = Files.newInputStream(inputFile, StandardOpenOption.READ);
        BufferedImage image = ImageIO.read(is);

        File outputFile = Paths.get(filesDirectory, "output.jpg").toAbsolutePath().toFile();
        System.out.printf(outFormat, "output file", outputFile.getAbsolutePath());

        boolean writeSuccess = ImageIO.write(image, "JPEG", outputFile);
        System.out.printf(outFormat, "write successful", writeSuccess);
    }
}

./src/main/resources/pond.ppm
The file is one of the images provided in jai-1_1_2-unix-sample.tar.gz

Build and run the example code.

mvn clean compile exec:java
Harald K
  • 26,314
  • 7
  • 65
  • 111
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
  • 2
    Thanks, I just replaced the name "JAI" with "ImageIO" in your answer, as we really don't extend or provide plugins to JAI, only standard ImageIO (which is usually all you need). :-) – Harald K Mar 20 '15 at 10:50
1

I would search for ImageMagick Application Programmer Interfaces. They have interfaces for every significant language.

I find the Java philosophy is to extensively research what already exists, find the best solution for your needs, then write the minimal code needed to interface to it. This is a pure Java way.