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
AVIFCompressor.h
1/* AVIF Compressor Class:
2 Handles alpha channels, 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 _AVIFCOMPRESSOR_H
24#define _AVIFCOMPRESSOR_H
25
26
27#include "Compressor.h"
28#include <avif/avif.h>
29
30
31
33class AVIFCompressor : public Compressor {
34
35 private:
36
38 avifEncoder *encoder;
39 avifImage *avif;
40 avifCodecChoice codec;
41
42
44 RawTile tile;
45 unsigned int chunk_size;
46 size_t current_chunk;
47
48
50 void writeICCProfile();
51
53 void writeXMPMetadata();
54
56 void writeExifMetadata();
57
58
59 public:
60
62
64 AVIFCompressor( int quality ) : Compressor(quality) {};
65
68
69
71
76 void InitCompression( const RawTile& rawtile, unsigned int strip_height );
77
78
80
85 unsigned int CompressStrip( unsigned char* source, unsigned char* output, unsigned int tile_height );
86
87
89
92 unsigned int Finish( unsigned char* output );
93
94
96
99 unsigned int Compress( RawTile& t );
100
101
103 inline unsigned int getHeaderSize() const { return header_size; }
104
106 inline unsigned char* getHeader() { return header; }
107
109 inline const char* getMimeType() const { return "image/avif"; }
110
112 inline const char* getSuffix() const { return "avif"; }
113
115 inline ImageEncoding getImageEncoding() const { return ImageEncoding::AVIF; };
116
117
119
120 inline int getQuality() const { return Q; }
121
122
124
126 inline void setQuality( int quality ){
127
128 // AVIF quality ranges from 0 (best compression - smallest size) to 100 (worst compression - largest size)
129 this->Q = quality;
130
131 // AVIF compression level
132 if( Q < -1 ) Q = -1; // Lossless
133 else if( Q > 100 ) Q = 100;
134
135 }
136
137
139
141 inline void setCodec( unsigned int codec )
142 {
143 this->codec = AVIFCompressor::getCodecChoice( codec );
144 }
145
146
148
152 inline static avifCodecChoice getCodecChoice( unsigned int codec )
153 {
154 if( codec == 1 ) return AVIF_CODEC_CHOICE_AOM;
155 else if( codec == 2 ) return AVIF_CODEC_CHOICE_RAV1E;
156 else if( codec == 3 ) return AVIF_CODEC_CHOICE_SVT;
157 else return AVIF_CODEC_CHOICE_AUTO;
158 }
159
160
162
166 inline static const char* getCodecName( unsigned int codec )
167 {
168 avifCodecChoice choice = AVIFCompressor::getCodecChoice( codec );
169 const char* name = avifCodecName( choice, AVIF_CODEC_FLAG_CAN_ENCODE );
170 if( name == NULL ) return "unsupported codec - will not be able to encode to avif";
171 else return name;
172 }
173
174};
175
176#endif
Wrapper class to AVIF library: Handles 8 bit and alpha channels.
Definition AVIFCompressor.h:33
const char * getSuffix() const
Return the image filename suffix.
Definition AVIFCompressor.h:112
unsigned int Compress(RawTile &t)
Compress an entire buffer of image data at once in one command.
Definition AVIFCompressor.cc:90
void setQuality(int quality)
Set the compression level.
Definition AVIFCompressor.h:126
unsigned int getHeaderSize() const
Return the WebP header size.
Definition AVIFCompressor.h:103
AVIFCompressor(int quality)
Constructor.
Definition AVIFCompressor.h:64
static const char * getCodecName(unsigned int codec)
Static function: Get codec name from IIPImage codec option code.
Definition AVIFCompressor.h:166
const char * getMimeType() const
Return the WebP mime type.
Definition AVIFCompressor.h:109
void InitCompression(const RawTile &rawtile, unsigned int strip_height)
Initialize strip based compression.
Definition AVIFCompressor.cc:33
unsigned char * getHeader()
Return a pointer to the header itself.
Definition AVIFCompressor.h:106
int getQuality() const
Get the current compression level.
Definition AVIFCompressor.h:120
static avifCodecChoice getCodecChoice(unsigned int codec)
Static function: Convert from our option native system to libavif's codec choices.
Definition AVIFCompressor.h:152
unsigned int Finish(unsigned char *output)
Finish the strip based compression and free memory.
Definition AVIFCompressor.cc:74
~AVIFCompressor()
Destructor.
Definition AVIFCompressor.h:67
void setCodec(unsigned int codec)
Set codec for use during encoding - note that not all may be enabled in libavif.
Definition AVIFCompressor.h:141
ImageEncoding getImageEncoding() const
Get compression type.
Definition AVIFCompressor.h:115
unsigned int CompressStrip(unsigned char *source, unsigned char *output, unsigned int tile_height)
Compress a strip of image data.
Definition AVIFCompressor.cc:54
Base class for IIP output images.
Definition Compressor.h:31
unsigned int header_size
Size of the header data.
Definition Compressor.h:45
int Q
Quality or compression level for all image types.
Definition Compressor.h:36
unsigned char * header
Pointer to the header data for the output image.
Definition Compressor.h:42
Class to represent a single image tile.
Definition RawTile.h:45