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
WebPCompressor.h
1/* WebP Compressor Class:
2 Handles alpha channels, ICC profiles and XMP metadata
3
4 Copyright (C) 2022-2024 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 _WEBPCOMPRESSOR_H
24#define _WEBPCOMPRESSOR_H
25
26
27#include "Compressor.h"
28#include <webp/encode.h>
29#include <webp/mux.h>
30
31
32
34class WebPCompressor : public Compressor {
35
36 private:
37
39 WebPConfig config;
40 WebPPicture pic;
41 WebPMemoryWriter writer;
42 WebPMux* mux;
43
45 RawTile tile;
46 unsigned int chunk_size;
47 size_t current_chunk;
48
49
51 void writeICCProfile();
52
54 void writeXMPMetadata();
55
57 void writeExifMetadata();
58
59
60 public:
61
63
65 WebPCompressor( int compressionLevel ) : Compressor(compressionLevel) {
66
67 // Initialize our configuration structure with default values
68 WebPConfigInit( &config );
69 config.method = 0; // Fastest encoding
70 config.thread_level = 1; // Enable threading
71
72 // Update our WebP config structure depending on whether lossless or lossy compression requested
73 if( compressionLevel == -1 ){
74 // -1 indicates lossless
75 config.lossless = 1;
76 config.quality = 0; // Zero means fastest for lossless
77 }
78 // WebP's lossy quality range is 0-100
79 else config.quality = this->Q;
80
81 // Create our muxer object
82 mux = WebPMuxNew();
83
84 };
85
86
88 // Delete our muxer object
89 WebPMuxDelete( mux );
90 };
91
92
94
99 void InitCompression( const RawTile& rawtile, unsigned int strip_height );
100
101
103
108 unsigned int CompressStrip( unsigned char* source, unsigned char* output, unsigned int tile_height );
109
110
112
115 unsigned int Finish( unsigned char* output );
116
117
119
122 unsigned int Compress( RawTile& t );
123
124
126 inline const char* getMimeType() const { return "image/webp"; }
127
129 inline const char* getSuffix() const { return "webp"; }
130
132 inline ImageEncoding getImageEncoding() const { return ImageEncoding::WEBP; };
133
134
136
138 inline void setQuality( int quality ){
139
140 // Flag that user has manually changed quality level
141 default_quality = false;
142
143 // WebP quality ranges from 0 (best compression - smallest size) to 100 (worst compression - largest size)
144 this->Q = quality;
145
146 // Limit to WebP quality range - negative values indicate lossless
147 if( this->Q < 1 ) Q = -1;
148 else if( Q > 100 ) Q = 100;
149
150 // Update our WebP config structure depending on whether lossless or lossy compression requested
151 if( this->Q == -1 ){
152 // -1 indicates lossless
153 config.lossless = 1;
154 config.quality = 0; // Zero means fastest for lossless
155 }
156 // WebP's lossy quality range is 0-100
157 else config.quality = this->Q;
158 }
159
160
162
164 void injectMetadata( RawTile& r );
165
166
167};
168
169#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
Wrapper class to WebP library: Handles 8 and 16 bit PNG as well as alpha transparency.
Definition WebPCompressor.h:34
const char * getMimeType() const
Return the WebP mime type.
Definition WebPCompressor.h:126
unsigned int Compress(RawTile &t)
Compress an entire buffer of image data at once in one command.
Definition WebPCompressor.cc:86
const char * getSuffix() const
Return the image filename suffix.
Definition WebPCompressor.h:129
WebPCompressor(int compressionLevel)
Constructor.
Definition WebPCompressor.h:65
void injectMetadata(RawTile &r)
Inject metadata into raw WebP bitstream.
Definition WebPCompressor.cc:234
unsigned int CompressStrip(unsigned char *source, unsigned char *output, unsigned int tile_height)
Compress a strip of image data.
Definition WebPCompressor.cc:50
void setQuality(int quality)
Set the compression level.
Definition WebPCompressor.h:138
void InitCompression(const RawTile &rawtile, unsigned int strip_height)
Initialize strip based compression.
Definition WebPCompressor.cc:30
unsigned int Finish(unsigned char *output)
Finish the strip based compression and free memory.
Definition WebPCompressor.cc:70
ImageEncoding getImageEncoding() const
Get compression type.
Definition WebPCompressor.h:132