All formats

TwelveMonkeys ImageIO (TGA plugin)

LanguageJava
LicenseBSD 3-Clause
Repositorygithub.com/haraldk/TwelveMonkeys
DocumentationREADME
Depends onimageio-core and common-* modules (pulled in by Maven/Gradle)
Read / writeyes / yes; palette, grayscale and true-color, raw or RLE, TGA 2.0 extension area

TwelveMonkeys is a set of plugins for the standard javax.imageio API. Once imageio-tga is on the classpath, ImageIO.read and ImageIO.write handle TGA like any built-in format; no TwelveMonkeys classes appear in your code. The reader handles all image types and origins and reads the TGA 2.0 extension area (thumbnail, metadata); the writer writes raw or RLE files.

Installation#

Maven:

XML
<dependency>
    <groupId>com.twelvemonkeys.imageio</groupId>
    <artifactId>imageio-tga</artifactId>
    <version>3.15.3</version>
</dependency>

Gradle:

implementation("com.twelvemonkeys.imageio:imageio-tga:3.15.3")

In a web application (servlet container), also add imageio-servlet and register IIOProviderContextListener so the plugins are found and unloaded correctly.

Basic usage: read a TGA and save it as PNG#

Java
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;

public class TgaToPng {
    public static void main(String[] args) throws IOException {
        BufferedImage image = ImageIO.read(new File("in.tga"));
        if (image == null) {
            throw new IOException("no ImageIO reader for this file: is imageio-tga on the classpath?");
        }
        System.out.println(image.getWidth() + " x " + image.getHeight());
        ImageIO.write(image, "png", new File("out.png"));
    }
}

How to convert PNG to TGA#

Java
BufferedImage image = ImageIO.read(new File("in.png"));
if (!ImageIO.write(image, "TGA", new File("out.tga"))) {
    throw new IOException("no TGA writer found");
}

Without a write param the file is written uncompressed.

How to write a TGA with RLE compression#

Java
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.IIOImage;
import javax.imageio.ImageIO;
import javax.imageio.ImageWriteParam;
import javax.imageio.ImageWriter;
import javax.imageio.stream.ImageOutputStream;

static void writeRleTga(BufferedImage image, File file) throws IOException {
    ImageWriter writer = ImageIO.getImageWritersByFormatName("TGA").next();
    ImageWriteParam param = writer.getDefaultWriteParam();
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionType("RLE");  // or "None"
    try (ImageOutputStream out = ImageIO.createImageOutputStream(file)) {
        writer.setOutput(out);
        writer.write(null, new IIOImage(image, null, null), param);
    } finally {
        writer.dispose();
    }
}

How to get TGA width and height without decoding#

Java
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;

static void printSize(File file) throws IOException {
    try (ImageInputStream in = ImageIO.createImageInputStream(file)) {
        Iterator<ImageReader> readers = ImageIO.getImageReaders(in);
        if (!readers.hasNext()) throw new IOException("not a supported image");
        ImageReader reader = readers.next();
        try {
            reader.setInput(in);
            System.out.println(reader.getFormatName() + ": " + reader.getWidth(0) + " x " + reader.getHeight(0));
        } finally {
            reader.dispose();
        }
    }
}

How to read the embedded thumbnail#

TGA 2.0 files can carry a small “postage stamp” image in the extension area:

Java
try (ImageInputStream in = ImageIO.createImageInputStream(new File("in.tga"))) {
    ImageReader reader = ImageIO.getImageReadersByFormatName("TGA").next();
    reader.setInput(in);
    if (reader.hasThumbnails(0)) {
        BufferedImage thumb = reader.readThumbnail(0, 0);
        System.out.println("thumbnail " + thumb.getWidth() + " x " + thumb.getHeight());
    }
    reader.dispose();
}

Tips#