Java TIFF Writer
Java’s built-in ImageIO has limited support for writing TIFF files and provides little control over encoding quality and format-specific options. JDeli is a 100% Java TIFF writer that gives you full encoding control with no dependencies.
Getting started
Add JDeli to your project via Maven or Gradle, or see using JDeli with Java modules.
What is supported
The JDeli TIFF writer supports the following features:
| Feature | Supported |
|---|---|
| multi-image | yes |
| Alpha channels | Yes |
| resolution setting | Yes |
| BufferedImage input | Yes |
| Compression - DEFLATE | Yes also with settings for better compression or better speed |
| Compression - LZW | Yes |
| Compression - JPEG | Yes |
| Metadata | TIFF |
| Output to file | Yes |
| Output to OutputStream | Yes |
| Output to byte[] | Yes |
| Pure Java implementation | Yes |
Quick start or to replace in existing code using ImageIO:
The simplest way to write a TIFF image is:
JDeli.write(bufferedImage, "tiff", file);
Or to a byte array:
byte[] tiffData = JDeli.write(bufferedImage, "tiff");
Or to an OutputStream:
JDeli.write(bufferedImage, "tiff", os);
Simple usage
For better type safety, use OutputFormat:
JDeli.write(myBufferedImage, OutputFormat.TIFF, outputStreamOrFile);
OutputFormat allows setting of any supported Image Format
Benefits:
- Compile-time safety
- Cleaner code
- Recommended for new applications
Advanced usage: controlling the output
Use tiffEncoderOptions when you need control over encoding behaviour.
final TiffEncoderOptions options = new TiffEncoderOptions();
//set any options in options instance - examples below
options.setCompressionFormat(TiffCompressionFormat.DEFLATE);
options.setXmpMetaData("xmp metadata");
//set Image resolution
options.setResolutionUnit(TiffResolutionUnit.INCH);
options.setXResolution(144);
options.setYResolution(144);
//write out
JDeli.write(myBufferedImage, options, outputStreamOrFile);
TiffEncoderOptions allows setting of specific options.
Various image processing operations can be conducted on the image, detailed documentation can be found here.
Performance comparisons:
These figures were generated using jmh (as documented on our blog) with a standard set of images (also documented). They should be easy to replicate if you wish to validate, the code is on GitHub.
The higher the number, the better.
Benchmark results using JMH:
Mode: Throughput Count: 25 Units: ops/s
| Benchmark | Score | Error |
|---|---|---|
| Apache | 4.129 | ± 0.077 |
| ICafe | 6.276 | ± 0.108 |
| ImageIO/JAI_Deflate | 5.573 | ± 0.797 |
| ImageIO/TwelveMonkeys_Deflate | 6.672 | ± 0.368 |
| ImageIO/JAI_JPEG | 7.313 | ± 0.337 |
| ImageIO/TwelveMonkeys_JPEG | 6.984 | ± 0.347 |
| ImageIO/JAI_LZW | 4.957 | ± 0.291 |
| ImageIO/TwelveMonkeys_LZW | 5.067 | ± 0.178 |
| JDeli_better_comp | 5.361 | ± 0.105 |
| JDeli_better_speed | 12.906 | ± 0.125 |
| JDeli_deflate | 6.290 | ± 0.034 |
| JDeli_jpeg | 26.629 | ± 0.425 |
| JDeli_LZW | 9.393 | ± 0.039 |
Number of files: 166
| Benchmark | Size | unreadable output | AVG SSIM score | AVG Similarity |
|---|---|---|---|---|
| Apache | 11.243KB | 0 | 0.9662085194307032 | Very similar (high quality) |
| ICafe | 11.243KB | 0 | 0.9397287642483998 | Similar (noticeable but minor differences) |
| ImageIO/JAI_deflate | 12.038KB | 0 | 1.0 | Identical or virtually identical |
| ImageIO/TwelveMonkeys_Deflate | 11.924KB | 0 | 1.0 | Identical or virtually identical |
| ImageIO/JAI_jpeg | 5.325KB | 0 | 0.9964492586342384 | Identical or virtually identical |
| ImageIO/TwelveMonkeys_jpeg | 5.876KB | 0 | 0.9964492586342384 | Identical or virtually identical |
| ImageIO/JAI_lzw | 10.870KB | 0 | 0.37968285428198995 | Very different |
| ImageIO/TwelveMonkeys_lzw | 10.870KB | 0 | 0.9662085194307032 | Very similar (high quality) |
| JDeli_better_comp | 16.354KB | 0 | 1.0 | Identical or virtually identical |
| JDeli_better_speed | 17.101KB | 0 | 1.0 | Identical or virtually identical |
| JDeli_deflate | 16.359KB | 0 | 1.0 | Identical or virtually identical |
| JDeli_jpeg | 1.575KB | 0 | 0.8720826599375827 | Similar (noticeable but minor differences) |
| JDeli_lzw | 23.880KB | 0 | 1.0 | Identical or virtually identical |
Tested on 2021 14inch M1 MacBook Pro using JDK 18.0.1.1
Frequently asked questions
Does JDeli produce better TIFF encoding than Java ImageIO?
Yes. While ImageIO can write TIFF, JDeli gives you full control over encoding options such as compression and quality, and produces more consistent output across platforms.
Does writing or encoding TIFF in Java with JDeli require additional libraries?
No. JDeli is a 100% Java library with no dependencies. It runs on any platform where the JVM runs with no additional installation.
What output types does JDeli support for writing TIFF?
JDeli can write and encode TIFF to a File, OutputStream, or return the encoded data as a byte array.