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
PNGCompressor.h
1/* IIP PNG Compressor Class:
2 Handles alpha channels, 8 or 16 bit data, ICC profiles and XMP metadata
3
4 Copyright (C) 2012-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 _PNGCOMPRESSOR_H
24#define _PNGCOMPRESSOR_H
25
26
27#include "Compressor.h"
28#include <png.h>
29
30
31// Define ourselves a set of fast filters if necessary
32#ifndef PNG_FAST_FILTERS // libpng < 1.6
33#define PNG_FAST_FILTERS ( PNG_FILTER_NONE | PNG_FILTER_SUB | PNG_FILTER_UP )
34#endif
35
36
37
39typedef struct {
40
41 png_structp png_ptr;
42 png_infop info_ptr;
43
44 unsigned char* output;
45 size_t output_size;
46 size_t written;
47 unsigned int strip_height;
48 unsigned int bytes_per_pixel;
49
51
53
54
55
57class PNGCompressor : public Compressor {
58
59 private:
60
62 unsigned int width;
63 unsigned int height;
64 png_uint_32 channels;
65
67
68 // The Compressor class Q parameter stores the zlib compression level (0-9)
69 int filterType;
70
72 void writeMetadata();
73
75 void writeResolution();
76
78 void writeICCProfile();
79
81 void writeXMPMetadata();
82
84 void writeExifMetadata();
85
86
87 public:
88
90
92 PNGCompressor( int compressionLevel ) : Compressor(compressionLevel) {
93
94 dest.png_ptr = NULL;
95 dest.info_ptr = NULL;
96
97 // Buffer for the data written by PNG library
98 dest.output_size = 0;
99
100 width = 0;
101 height = 0;
102 channels = 0;
103
104 // Filters are an optional pre-processing step before Deflate compression
105 // - set this to the fastest set of filters
106 filterType = PNG_FAST_FILTERS;
107
108 };
109
110
112
117 void InitCompression( const RawTile& rawtile, unsigned int strip_height );
118
119
121
126 unsigned int CompressStrip( unsigned char* source, unsigned char* output, unsigned int tile_height );
127
128
130
133 unsigned int Finish( unsigned char* output );
134
135
137
140 unsigned int Compress( RawTile& t );
141
142
144 inline const char* getMimeType() const { return "image/png"; }
145
147 inline const char* getSuffix() const { return "png"; }
148
150 inline ImageEncoding getImageEncoding() const { return ImageEncoding::PNG; };
151
152
154
157 inline void setQuality( int quality ){
158
159 // Flag that user has manually changed quality level
160 default_quality = false;
161
162 // Deflate compression level
163 if( quality < 0 ) Q = 0;
164 else if( quality > 9 ) Q = 9;
165 else Q = quality;
166 }
167
168
169};
170
171#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 PNG library: Handles 8 and 16 bit PNG as well as alpha transparency.
Definition PNGCompressor.h:57
unsigned int Finish(unsigned char *output)
Finish the strip based compression and free memory.
Definition PNGCompressor.cc:210
unsigned int CompressStrip(unsigned char *source, unsigned char *output, unsigned int tile_height)
Compress a strip of image data.
Definition PNGCompressor.cc:182
unsigned int Compress(RawTile &t)
Compress an entire buffer of image data at once in one command.
Definition PNGCompressor.cc:229
void InitCompression(const RawTile &rawtile, unsigned int strip_height)
Initialize strip based compression.
Definition PNGCompressor.cc:97
const char * getMimeType() const
Return the PNG mime type.
Definition PNGCompressor.h:144
const char * getSuffix() const
Return the image filename suffix.
Definition PNGCompressor.h:147
ImageEncoding getImageEncoding() const
Get compression type.
Definition PNGCompressor.h:150
PNGCompressor(int compressionLevel)
Constructor.
Definition PNGCompressor.h:92
void setQuality(int quality)
Set the compression level.
Definition PNGCompressor.h:157
Class to represent a single image tile.
Definition RawTile.h:45
Expanded data destination object for buffered output used by PNG library.
Definition PNGCompressor.h:39
unsigned int bytes_per_pixel
bytes per pixel (1 for 8 bit and 2 for 16 bit images)
Definition PNGCompressor.h:48
png_infop info_ptr
png info pointer
Definition PNGCompressor.h:42
size_t output_size
size of output buffer
Definition PNGCompressor.h:45
unsigned int strip_height
strip height: used for stream-based encoding
Definition PNGCompressor.h:47
unsigned char * output
output buffer pointer
Definition PNGCompressor.h:44
png_structp png_ptr
png data pointer
Definition PNGCompressor.h:41
size_t written
number of bytes written to buffer
Definition PNGCompressor.h:46