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
JPEGCompressor.h
1/* JPEG class wrapper to ijg libjpeg library
2
3 Copyright (C) 2000-2025 Ruven Pillay.
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18*/
19
20
21
22#ifndef _JPEGCOMPRESSOR_H
23#define _JPEGCOMPRESSOR_H
24
25
26#include "Compressor.h"
27
28
29// Fix missing snprintf in Windows
30#if _MSC_VER
31#define snprintf _snprintf
32#endif
33
34
35extern "C"{
36/* Undefine this to prevent compiler warning
37 */
38#undef HAVE_STDLIB_H
39#include <jpeglib.h>
40}
41
42
43
45typedef struct {
46
47 struct jpeg_destination_mgr pub;
48
49 unsigned char* source;
50 size_t source_size;
51 size_t written;
52 unsigned int strip_height;
53
55
56
57
60
61 private:
62
64 unsigned int width, height, channels;
65
67 struct jpeg_compress_struct cinfo;
68 struct jpeg_error_mgr jerr;
69 iip_destination_mgr dest_mgr;
71
73 void writeResolution();
74
76 void writeICCProfile();
77
79 void writeXMPMetadata();
80
82 void writeExifMetadata();
83
84
85 public:
86
88
89 JPEGCompressor( int quality ) : Compressor(quality), dest(NULL) {
90 dest = &dest_mgr;
91 };
92
93
95
96 inline void setQuality( int factor ) {
97
98 // Flag that user has manually changed quality level
99 default_quality = false;
100
101 if( factor < 0 ) Q = 0;
102 else if( factor > 100 ) Q = 100;
103 else Q = factor;
104 };
105
106
107
109
115 void InitCompression( const RawTile& rawtile, unsigned int strip_height );
116
118
122 unsigned int CompressStrip( unsigned char* s, unsigned char* o, unsigned int tile_height );
123
125
128 unsigned int Finish( unsigned char* output );
129
131
132 unsigned int Compress( RawTile& t );
133
135 inline const char* getMimeType() const { return "image/jpeg"; }
136
138 inline const char* getSuffix() const { return "jpg"; }
139
141 inline ImageEncoding getImageEncoding() const { return ImageEncoding::JPEG; };
142
144
146 void injectMetadata( RawTile& r );
147
148};
149
150
151#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
Wrapper class to the IJG JPEG library.
Definition JPEGCompressor.h:59
JPEGCompressor(int quality)
Constructor.
Definition JPEGCompressor.h:89
void InitCompression(const RawTile &rawtile, unsigned int strip_height)
Initialise strip based compression.
Definition JPEGCompressor.cc:153
const char * getMimeType() const
Return the JPEG mime type.
Definition JPEGCompressor.h:135
unsigned int Compress(RawTile &t)
Compress an entire buffer of image data at once in one command.
Definition JPEGCompressor.cc:308
void setQuality(int factor)
Set the compression quality.
Definition JPEGCompressor.h:96
unsigned int CompressStrip(unsigned char *s, unsigned char *o, unsigned int tile_height)
Compress a strip of image data.
Definition JPEGCompressor.cc:247
ImageEncoding getImageEncoding() const
Get compression type.
Definition JPEGCompressor.h:141
unsigned int Finish(unsigned char *output)
Finish the strip based compression and free memory.
Definition JPEGCompressor.cc:282
void injectMetadata(RawTile &r)
Inject metadata into raw JPEG bitstream.
Definition JPEGCompressor.cc:548
const char * getSuffix() const
Return the image filename suffix.
Definition JPEGCompressor.h:138
Class to represent a single image tile.
Definition RawTile.h:45
Expanded data destination object for buffered output used by IJG JPEG library.
Definition JPEGCompressor.h:45
size_t source_size
size of output buffer
Definition JPEGCompressor.h:50
unsigned char * source
output data buffer pointer
Definition JPEGCompressor.h:49
size_t written
number of bytes written to buffer
Definition JPEGCompressor.h:51
unsigned int strip_height
used for stream-based encoding
Definition JPEGCompressor.h:52