TwelveMonkeys ImageIO (TGA plugin)
| Language | Java |
|---|---|
| License | BSD 3-Clause |
| Repository | github.com/haraldk/TwelveMonkeys |
| Documentation | README |
| Depends on | imageio-core and common-* modules (pulled in by Maven/Gradle) |
| Read / write | yes / 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#
- TGA has no magic bytes, so the TGA reader is chosen by header checks. When you know a file is TGA, get the reader with
getImageReadersByFormatName("TGA")instead ofImageIO.read. - Add other TwelveMonkeys plugins (
imageio-tiff,imageio-psd,imageio-webp) the same way to read more formats with the same API.