0
# Compression Configuration
1
2
Advanced compression settings and write parameters for optimizing TIFF output with support for 9 different compression algorithms including both lossless and lossy options.
3
4
## Capabilities
5
6
### TIFF Write Parameters
7
8
Configuration class for controlling TIFF compression, quality settings, and write behavior.
9
10
```java { .api }
11
/**
12
* Configuration for TIFF writing operations with compression support
13
*/
14
class TIFFImageWriteParam extends ImageWriteParam {
15
16
// Available compression types (accessed via getCompressionTypes())
17
// String constants for setCompressionType() method:
18
19
// "None" - No compression (default) - fastest writing, largest files
20
// "CCITT RLE" - CCITT Modified Huffman RLE - for bi-level (black & white) images
21
// "CCITT T.4" - CCITT Group 3 Fax compression - for bi-level images
22
// "CCITT T.6" - CCITT Group 4 Fax compression - for bi-level images
23
// "LZW" - Lempel-Ziv-Welch lossless compression - good general purpose
24
// "JPEG" - JPEG compression - lossy, best for photographs
25
// "ZLib" - Adobe-style Deflate compression - lossless
26
// "PackBits" - PackBits compression - simple lossless compression
27
// "Deflate" - PKZIP-style Deflate compression - lossless
28
29
/**
30
* Constructor creates write parameters with default settings
31
*/
32
public TIFFImageWriteParam();
33
34
/**
35
* Get available compression quality values for current compression type
36
* @return array of quality values (typically 0.0 to 1.0)
37
*/
38
public float[] getCompressionQualityValues();
39
40
/**
41
* Get human-readable descriptions for compression quality values
42
* @return array of quality descriptions
43
*/
44
public String[] getCompressionQualityDescriptions();
45
}
46
```
47
48
**Usage Examples:**
49
50
```java
51
import javax.imageio.ImageIO;
52
import javax.imageio.ImageWriteParam;
53
import javax.imageio.ImageWriter;
54
import javax.imageio.IIOImage;
55
import com.twelvemonkeys.imageio.plugins.tiff.TIFFImageWriteParam;
56
import java.awt.image.BufferedImage;
57
import java.io.File;
58
59
// Writing with LZW compression (lossless, good compression ratio)
60
ImageWriter writer = ImageIO.getImageWritersByFormatName("TIFF").next();
61
writer.setOutput(ImageIO.createImageOutputStream(new File("lzw_compressed.tiff")));
62
63
TIFFImageWriteParam writeParam = (TIFFImageWriteParam) writer.getDefaultWriteParam();
64
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
65
writeParam.setCompressionType("LZW");
66
67
BufferedImage image = // ... load or create image
68
writer.write(null, new IIOImage(image, null, null), writeParam);
69
writer.dispose();
70
71
// Writing with JPEG compression (lossy, smallest files for photos)
72
writer = ImageIO.getImageWritersByFormatName("TIFF").next();
73
writer.setOutput(ImageIO.createImageOutputStream(new File("jpeg_compressed.tiff")));
74
75
writeParam = (TIFFImageWriteParam) writer.getDefaultWriteParam();
76
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
77
writeParam.setCompressionType("JPEG");
78
writeParam.setCompressionQuality(0.8f); // 80% quality
79
80
writer.write(null, new IIOImage(image, null, null), writeParam);
81
writer.dispose();
82
83
// Writing black & white image with CCITT Group 4 compression
84
BufferedImage bwImage = // ... black and white image
85
writer = ImageIO.getImageWritersByFormatName("TIFF").next();
86
writer.setOutput(ImageIO.createImageOutputStream(new File("fax_compressed.tiff")));
87
88
writeParam = (TIFFImageWriteParam) writer.getDefaultWriteParam();
89
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
90
writeParam.setCompressionType("CCITT T.6");
91
92
writer.write(null, new IIOImage(bwImage, null, null), writeParam);
93
writer.dispose();
94
```
95
96
### Compression Types and Use Cases
97
98
Each compression algorithm is optimized for specific types of images and use cases:
99
100
#### None (Uncompressed)
101
- **Best for**: When write speed is critical and file size is not a concern
102
- **Compression ratio**: 1:1 (no compression)
103
- **Quality**: Lossless
104
- **Supported images**: All image types
105
106
#### CCITT RLE (Modified Huffman)
107
- **Best for**: Bi-level (black & white) images with moderate complexity
108
- **Compression ratio**: 2:1 to 10:1 depending on image content
109
- **Quality**: Lossless
110
- **Supported images**: Bi-level only
111
112
#### CCITT T.4 (Group 3 Fax)
113
- **Best for**: Bi-level images like scanned documents and fax transmissions
114
- **Compression ratio**: 5:1 to 20:1 for typical documents
115
- **Quality**: Lossless
116
- **Supported images**: Bi-level only
117
118
#### CCITT T.6 (Group 4 Fax)
119
- **Best for**: Bi-level images with the best compression for documents
120
- **Compression ratio**: 10:1 to 50:1 for typical documents
121
- **Quality**: Lossless
122
- **Supported images**: Bi-level only
123
124
#### LZW (Lempel-Ziv-Welch)
125
- **Best for**: General purpose lossless compression for all image types
126
- **Compression ratio**: 1.5:1 to 3:1 depending on image content
127
- **Quality**: Lossless
128
- **Supported images**: All image types
129
130
#### JPEG
131
- **Best for**: Photographic images where some quality loss is acceptable
132
- **Compression ratio**: 5:1 to 100:1 depending on quality setting
133
- **Quality**: Lossy (configurable quality 0.0 to 1.0)
134
- **Supported images**: Grayscale and RGB images
135
136
#### ZLib (Adobe Deflate)
137
- **Best for**: Lossless compression with good compression ratios
138
- **Compression ratio**: 1.5:1 to 4:1 depending on image content
139
- **Quality**: Lossless
140
- **Supported images**: All image types
141
142
#### PackBits
143
- **Best for**: Simple lossless compression with fast encoding/decoding
144
- **Compression ratio**: 1.1:1 to 2:1 depending on image content
145
- **Quality**: Lossless
146
- **Supported images**: All image types
147
148
#### Deflate (PKZIP-style)
149
- **Best for**: Lossless compression compatible with ZIP/PNG deflate
150
- **Compression ratio**: 1.5:1 to 4:1 depending on image content
151
- **Quality**: Lossless
152
- **Supported images**: All image types
153
154
### Quality Settings
155
156
For lossy compression (JPEG), quality can be controlled using compression quality parameters:
157
158
```java
159
// Set JPEG compression with specific quality
160
writeParam.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
161
writeParam.setCompressionType("JPEG");
162
writeParam.setCompressionQuality(0.9f); // 90% quality (higher = better quality, larger file)
163
164
// Get available quality values and descriptions
165
float[] qualityValues = writeParam.getCompressionQualityValues();
166
String[] qualityDescriptions = writeParam.getCompressionQualityDescriptions();
167
168
for (int i = 0; i < qualityValues.length; i++) {
169
System.out.println("Quality " + qualityValues[i] + ": " + qualityDescriptions[i]);
170
}
171
```
172
173
### Best Practices
174
175
**For Document Scanning:**
176
- Use CCITT T.6 for black & white documents (best compression)
177
- Use LZW for color/grayscale documents (good compression, lossless)
178
179
**For Photography:**
180
- Use JPEG with quality 0.8-0.95 for web/display use
181
- Use LZW or no compression for archival/editing purposes
182
183
**For Scientific/Medical Images:**
184
- Always use lossless compression (LZW, Deflate, or None)
185
- Preserve all bit depth and color information
186
187
**For Large Images:**
188
- Consider BigTIFF format for files >4GB
189
- Use tiling for efficient partial image access
190
- Use LZW or Deflate for good compression without quality loss
191
192
**Performance Considerations:**
193
- None: Fastest write, largest files
194
- PackBits: Fast compression, moderate compression ratio
195
- LZW/Deflate: Moderate speed, good compression
196
- JPEG: Fast compression, smallest files (lossy)
197
- CCITT: Fastest for bi-level, excellent compression