iipsrv 1.3
iipsrv is an advanced high-performance feature-rich image server for web-based streamed viewing and zooming of ultra high-resolution images
TIFFCompressor.h
1/* IIP TIFF Compressor Class:
2 Handles alpha channels, 8 or 16 bit data, ICC profiles and XMP metadata
3
4 Copyright (C) 2024-2025 Ruven Pillay
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software Foundation,
18 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19
20*/
21
22
23#ifndef _TIFFCOMPRESSOR_H
24#define _TIFFCOMPRESSOR_H
25
26
27#include "Compressor.h"
28#include <tiffio.h>
29
30
31
33typedef struct {
34 unsigned char *buffer;
35 toff_t current;
36 toff_t end;
37 toff_t capacity;
38} tiff_mem;
39
40typedef tiff_mem* tiff_mem_ptr;
41
42
43
46
47 private:
48
49 unsigned int height;
50 tiff_mem dest;
51 TIFF *tiff;
52
53 unsigned int chunk_size;
54 size_t current_chunk;
55
57 uint16_t compression;
58
60 void writeMetadata();
61
63 void writeResolution();
64
66 void writeICCProfile();
67
69 void writeXMPMetadata();
70
72 void configure( const RawTile& );
73
74
75 public:
76
78
81 TIFFCompressor( int compression, int quality ) : Compressor(quality), height(0), tiff(NULL)
82 {
83 this->setCompression( compression );
84
85 // Limit our quality level to the max allowed for Deflate and ZStandard
86 if( compression == 2 && Q > 9 ) Q = 9;
87 else if( compression == 5 && Q > 19 ) Q = 19;
88 };
89
90
91
93
95 inline void setCompression( int compression )
96 {
97 if ( compression == 1 ) this->compression = COMPRESSION_LZW;
98 else if ( compression == 2 ) this->compression = COMPRESSION_ADOBE_DEFLATE;
99 else if ( compression == 3 ) this->compression = COMPRESSION_JPEG;
100 else if ( compression == 4 ) this->compression = COMPRESSION_WEBP;
101 else if ( compression == 5 ) this->compression = COMPRESSION_ZSTD;
102 else this->compression = COMPRESSION_NONE;
103 };
104
105
107
109 inline void setQuality( int quality )
110 {
111 // Flag that user has manually changed quality level
112 default_quality = false;
113
114 Q = quality;
115
116 // Set max depending on compression type
117 int max;
118 if( compression == COMPRESSION_ADOBE_DEFLATE ) max = 9; // Max for deflate is 9
119 else if( compression == COMPRESSION_ZSTD ) max = 19; // Max for zstd is 19
120 else max = 100; // Max for JPEG or WebP is 100
121
122 // Limit quality range
123 if ( Q < 0 ) Q = 0;
124 else if( Q > max ) Q = max;
125
126 };
127
128
130
135 void InitCompression( const RawTile& rawtile, unsigned int strip_height );
136
137
139
144 unsigned int CompressStrip( unsigned char* source, unsigned char* output, unsigned int tile_height );
145
146
147
149
152 unsigned int Finish( unsigned char* output );
153
154
156
159 unsigned int Compress( RawTile& t );
160
161
163 inline const char* getMimeType() const { return "image/tiff"; };
164
166 inline const char* getSuffix() const { return "tif"; };
167
169 inline ImageEncoding getImageEncoding() const { return ImageEncoding::TIFF; };
170
171
173
176 static inline std::string getCompressionName( int code )
177 {
178 std::string name;
179 if ( code == 1 ) name = "LZW";
180 else if( code == 2 ) name = "Deflate";
181 else if( code == 3 ) name = "JPEG";
182 else if( code == 4 ) name = "WebP";
183 else if( code == 5 ) name = "ZSTD";
184 else name = "None";
185 return name;
186 };
187
188};
189
190#endif
Base class for IIP output images.
Definition Compressor.h:31
bool default_quality
Whether compression level is default or has been set manually.
Definition Compressor.h:39
int Q
Quality or compression level for all image types.
Definition Compressor.h:36
Class to represent a single image tile.
Definition RawTile.h:45
TIFF encoder class.
Definition TIFFCompressor.h:45
unsigned int Compress(RawTile &t)
Compress an entire buffer of image data at once in one command.
Definition TIFFCompressor.cc:255
void setQuality(int quality)
Set the compression level.
Definition TIFFCompressor.h:109
const char * getMimeType() const
Return the TIFF mime type.
Definition TIFFCompressor.h:163
void setCompression(int compression)
Set compression type: 0: None, 1: LZW, 2: Deflate, 3: JPEG, 4: WebP, 5: ZStandard.
Definition TIFFCompressor.h:95
unsigned int Finish(unsigned char *output)
Finish the strip based compression and free memory.
Definition TIFFCompressor.cc:237
unsigned int CompressStrip(unsigned char *source, unsigned char *output, unsigned int tile_height)
Compress a strip of image data.
Definition TIFFCompressor.cc:216
static std::string getCompressionName(int code)
Static utility function to get compression in human-readable form.
Definition TIFFCompressor.h:176
TIFFCompressor(int compression, int quality)
Constructor.
Definition TIFFCompressor.h:81
const char * getSuffix() const
Return the image filename suffix.
Definition TIFFCompressor.h:166
void InitCompression(const RawTile &rawtile, unsigned int strip_height)
Initialize strip based compression.
Definition TIFFCompressor.cc:149
ImageEncoding getImageEncoding() const
Get compression type.
Definition TIFFCompressor.h:169
Structure to handle memory-based TIFF writing.
Definition TIFFCompressor.h:33
toff_t end
Last written byte.
Definition TIFFCompressor.h:36
unsigned char * buffer
Data buffer.
Definition TIFFCompressor.h:34
toff_t current
Current byte position within stream.
Definition TIFFCompressor.h:35
toff_t capacity
Allocated buffer size.
Definition TIFFCompressor.h:37