iipsrv 1.2
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-2023 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
56
57 public:
58
60
62 WebPCompressor( int compressionLevel ) : Compressor(compressionLevel) {
63
64 // Initialize our configuration structure with default values
65 WebPConfigInit( &config );
66 config.method = 0; // Fastest encoding
67 config.thread_level = 1; // Enable threading
68 config.quality = this->Q; // WebP's quality range is 0-100
69
70 // Create our muxer object
71 mux = WebPMuxNew();
72
73 };
74
75
77 // Delete our muxer object
78 WebPMuxDelete( mux );
79 };
80
81
83
88 void InitCompression( const RawTile& rawtile, unsigned int strip_height );
89
90
92
97 unsigned int CompressStrip( unsigned char* source, unsigned char* output, unsigned int tile_height );
98
99
101
104 unsigned int Finish( unsigned char* output );
105
106
108
111 unsigned int Compress( RawTile& t );
112
113
115 inline unsigned int getHeaderSize() const { return header_size; }
116
118 inline unsigned char* getHeader() { return header; }
119
121 inline const char* getMimeType() const { return "image/webp"; }
122
124 inline const char* getSuffix() const { return "webp"; }
125
127 inline CompressionType getCompressionType() const { return WEBP; };
128
129
131
132 inline int getQuality() const { return Q; }
133
134
136
138 inline void setQuality( int quality ){
139
140 // WebP quality ranges from 0 (best compression - smallest size) to 100 (worst compression - largest size)
141 this->Q = quality;
142
143 // WebP compression level
144 if( Q < 0 ) Q = 0;
145 else if( Q > 100 ) Q = 100;
146
147 // Update our WebP config structure
148 config.quality = Q;
149 }
150
151
152};
153
154#endif
Base class for IIP output images.
Definition: Compressor.h:32
unsigned int header_size
Size of the header data.
Definition: Compressor.h:43
int Q
Quality or compression level for all image types.
Definition: Compressor.h:37
unsigned char * header
Pointer to the header data for the output image.
Definition: Compressor.h:40
Class to represent a single image tile.
Definition: RawTile.h:47
Wrapper class to WebP library: Handles 8 and 16 bit PNG as well as alpha transparency.
Definition: WebPCompressor.h:34
unsigned int getHeaderSize() const
Return the WebP header size.
Definition: WebPCompressor.h:115
const char * getMimeType() const
Return the WebP mime type.
Definition: WebPCompressor.h:121
CompressionType getCompressionType() const
Get compression type.
Definition: WebPCompressor.h:127
unsigned char * getHeader()
Return a pointer to the header itself.
Definition: WebPCompressor.h:118
unsigned int Compress(RawTile &t)
Compress an entire buffer of image data at once in one command.
const char * getSuffix() const
Return the image filename suffix.
Definition: WebPCompressor.h:124
WebPCompressor(int compressionLevel)
Constructor.
Definition: WebPCompressor.h:62
unsigned int CompressStrip(unsigned char *source, unsigned char *output, unsigned int tile_height)
Compress a strip of image data.
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.
int getQuality() const
Get the current compression level.
Definition: WebPCompressor.h:132
unsigned int Finish(unsigned char *output)
Finish the strip based compression and free memory.