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
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-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 _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 writeICCProfile();
73
75 void writeXMPMetadata();
76
77
78 public:
79
81
83 PNGCompressor( int compressionLevel ) : Compressor(compressionLevel) {
84
85 dest.png_ptr = NULL;
86 dest.info_ptr = NULL;
87
88 // Buffer for the data written by PNG library
89 dest.output_size = 0;
90
91 width = 0;
92 height = 0;
93 channels = 0;
94
95 // Filters are an optional pre-processing step before Deflate compression
96 // - set this to the fastest set of filters
97 filterType = PNG_FAST_FILTERS;
98
99 };
100
101
103
108 void InitCompression( const RawTile& rawtile, unsigned int strip_height );
109
110
112
117 unsigned int CompressStrip( unsigned char* source, unsigned char* output, unsigned int tile_height );
118
119
121
124 unsigned int Finish( unsigned char* output );
125
126
128
131 unsigned int Compress( RawTile& t );
132
133
135 inline unsigned int getHeaderSize() const { return header_size; }
136
138 inline unsigned char* getHeader() { return header; }
139
141 inline const char* getMimeType() const { return "image/png"; }
142
144 inline const char* getSuffix() const { return "png"; }
145
147 inline CompressionType getCompressionType() const { return PNG; };
148
149
151
152 inline int getQuality() const { return Q; }
153
154
156
159 inline void setQuality( int quality ){
160
161 // Deflate compression level
162 if( quality < 0 ) Q = 0;
163 else if( quality > 9 ) Q = 9;
164 else Q = quality;
165 }
166
167
168};
169
170#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
Wrapper class to PNG library: Handles 8 and 16 bit PNG as well as alpha transparency.
Definition: PNGCompressor.h:57
unsigned char * getHeader()
Return a pointer to the header itself.
Definition: PNGCompressor.h:138
unsigned int Finish(unsigned char *output)
Finish the strip based compression and free memory.
unsigned int CompressStrip(unsigned char *source, unsigned char *output, unsigned int tile_height)
Compress a strip of image data.
unsigned int Compress(RawTile &t)
Compress an entire buffer of image data at once in one command.
int getQuality() const
Get the current compression level.
Definition: PNGCompressor.h:152
void InitCompression(const RawTile &rawtile, unsigned int strip_height)
Initialize strip based compression.
const char * getMimeType() const
Return the PNG mime type.
Definition: PNGCompressor.h:141
const char * getSuffix() const
Return the image filename suffix.
Definition: PNGCompressor.h:144
CompressionType getCompressionType() const
Get compression type.
Definition: PNGCompressor.h:147
PNGCompressor(int compressionLevel)
Constructor.
Definition: PNGCompressor.h:83
void setQuality(int quality)
Set the compression level.
Definition: PNGCompressor.h:159
unsigned int getHeaderSize() const
Return the PNG header size.
Definition: PNGCompressor.h:135
Class to represent a single image tile.
Definition: RawTile.h:47
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