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
IIPImage.h
1// IIPImage class
2
3/* IIP fcgi server module
4
5 Copyright (C) 2000-2025 Ruven Pillay.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software Foundation,
19 Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20*/
21
22
23#ifndef _IIPIMAGE_H
24#define _IIPIMAGE_H
25
26
27// Fix missing snprintf and vsnprintf in Windows
28#if defined _MSC_VER && _MSC_VER<1900
29#define snprintf _snprintf
30#define vsnprintf _vsnprintf
31#endif
32
33
34#include <string>
35#include <list>
36#include <vector>
37#include <map>
38#include <stdexcept>
39
40#include "RawTile.h"
41
42
44class file_error : public std::runtime_error {
45 public:
47 file_error(const std::string& s) : std::runtime_error(s) { }
48};
49
50
51
54struct Stack {
55 std::string name;
56 float scale;
57 Stack() : scale(1) {};
58};
59
60
61
63
69class IIPImage {
70
71 private:
72
74 std::string imagePath;
75
77 std::string fileSystemPrefix;
78
80 std::string fileSystemSuffix;
81
83 std::string fileNamePattern;
84
86 bool isFile;
87
89 std::string suffix;
90
92 void testImageType();
93
95 void measureHorizontalAngles();
96
98 void measureVerticalAngles();
99
100
101 protected:
102
104 enum PyramidType { NORMAL, SUBIFD };
105
107 std::list <int> horizontalAnglesList;
108
110 std::list <int> verticalAnglesList;
111
113 std::vector <int> lut;
114
116 unsigned int virtual_levels;
117
119 ImageEncoding format;
120
123
125 std::list <Stack> stack;
126
128 std::vector <uint32_t> resolution_ids;
129
130
131 public:
132
134 std::vector <unsigned int> image_widths, image_heights;
135
137 std::vector <unsigned int> tile_widths, tile_heights;
138
140 ColorSpace colorspace;
141
143 float dpi_x, dpi_y;
144
146
148
150 unsigned int numResolutions;
151
153 unsigned int bpc;
154
156 unsigned int channels;
157
159 SampleType sampleType;
160
162 std::vector <float> min, max;
163
165 unsigned int quality_layers;
166
168 bool isSet;
169
171 int currentX, currentY;
172
174 std::vector<unsigned int> histogram;
175
177 std::map <const std::string, const std::string> metadata;
178
180 time_t timestamp;
181
183 static bool logging;
184
186 static bool codec_passthrough;
187
188
189 public:
190
193 isFile( false ),
194 virtual_levels( 0 ),
195 format( ImageEncoding::UNSUPPORTED ),
196 pyramid( NORMAL ),
197 colorspace( ColorSpace::NONE ),
198 dpi_x( 0 ),
199 dpi_y( 0 ),
200 dpi_units( 0 ),
201 numResolutions( 0 ),
202 bpc( 0 ),
203 channels( 0 ),
204 sampleType( SampleType::FIXEDPOINT ),
205 quality_layers( 0 ),
206 isSet( false ),
207 currentX( 0 ),
208 currentY( 90 ),
209 timestamp( 0 ) {};
210
212
214 IIPImage( const std::string& s ) :
215 imagePath( s ),
216 isFile( false ),
217 virtual_levels( 0 ),
218 format( ImageEncoding::UNSUPPORTED ),
219 pyramid( NORMAL ),
220 colorspace( ColorSpace::NONE ),
221 dpi_x( 0 ),
222 dpi_y( 0 ),
223 dpi_units( 0 ),
224 numResolutions( 0 ),
225 bpc( 0 ),
226 channels( 0 ),
227 sampleType( SampleType::FIXEDPOINT ),
228 quality_layers( 0 ),
229 isSet( false ),
230 currentX( 0 ),
231 currentY( 90 ),
232 timestamp( 0 ) {};
233
235
237 IIPImage( const IIPImage& image ) :
238 imagePath( image.imagePath ),
239 fileSystemPrefix( image.fileSystemPrefix ),
240 fileSystemSuffix( image.fileSystemSuffix ),
241 fileNamePattern( image.fileNamePattern ),
242 isFile( image.isFile ),
243 suffix( image.suffix ),
246 lut( image.lut ),
248 format( image.format ),
249 pyramid( image.pyramid ),
250 stack( image.stack ),
252 image_widths( image.image_widths ),
253 image_heights( image.image_heights ),
254 tile_widths( image.tile_widths ),
255 tile_heights( image.tile_heights ),
256 colorspace( image.colorspace ),
257 dpi_x( image.dpi_x ),
258 dpi_y( image.dpi_y ),
259 dpi_units( image.dpi_units ),
261 bpc( image.bpc ),
262 channels( image.channels ),
263 sampleType( image.sampleType ),
264 min( image.min ),
265 max( image.max ),
267 isSet( image.isSet ),
268 currentX( image.currentX ),
269 currentY( image.currentY ),
270 histogram( image.histogram ),
271 metadata( image.metadata ),
272 timestamp( image.timestamp ) {};
273
275 virtual ~IIPImage() {};
276
278 void Initialise();
279
281
284 void swap( IIPImage& a, IIPImage& b );
285
287 std::list <int> getVerticalViewsList() const { return verticalAnglesList; };
288
290 std::list <int> getHorizontalViewsList() const { return horizontalAnglesList; };
291
293 const std::string& getImagePath() const { return imagePath; };
294
296
299 const std::string getFileName( int x, int y );
300
302 ImageEncoding getImageFormat() const { return format; };
303
305
307 void updateTimestamp( const std::string& s );
308
310 const std::string getTimestamp();
311
313 bool set() const { return isSet; };
314
316 void setFileSystemPrefix( const std::string& prefix ) { fileSystemPrefix = prefix; };
317
319 void setFileSystemSuffix( const std::string& s ) { fileSystemSuffix = s; };
320
322 void setFileNamePattern( const std::string& pattern ) { fileNamePattern = pattern; };
323
325 unsigned int getNumResolutions() const { return numResolutions; };
326
328
330 int getNativeResolution( const int res ) const { return numResolutions - res - 1; };
331
333 unsigned int getNumBitsPerPixel() const { return bpc; };
334
336 unsigned int getNumChannels() const { return channels; };
337
339
341 float getMinValue( int n=0 ) const { return min[n]; };
342
344
346 float getMaxValue( int n=0 ) const { return max[n]; };
347
349 SampleType getSampleType() const { return sampleType; };
350
352
354 unsigned int getImageWidth( int n=0 ) const { return image_widths[n]; };
355
357
359 unsigned int getImageHeight( int n=0 ) const { return image_heights[n]; };
360
362
364 unsigned int getTileWidth( int n=-1 ) const {
365 if( n == -1 ) n = 0;
366 else n = getNativeResolution( n );
367 if( tile_widths.size() < (size_t) n+1 ) n = 0;
368 return tile_widths[n];
369 };
370
372
374 unsigned int getTileHeight( int n=-1 ) const {
375 if( n == -1 ) n = 0;
376 else n = getNativeResolution( n );
377 if( tile_heights.size() < (size_t) n+1 ) n = 0;
378 return tile_heights[n];
379 };
380
382 ColorSpace getColorSpace() const { return colorspace; };
383
385 bool isStack() const {
386 if( stack.size() > 0 ) return true;
387 return false;
388 };
389
391 std::list <Stack> getStack() const { return stack; };
392
394
395 const std::string& getMetadata( const std::string& index ){
396 return metadata[index];
397 };
398
400 float getHorizontalDPI() const { return (dpi_units==2) ? dpi_x*100.0 : ( (dpi_units==1) ? dpi_x/0.0254 : dpi_x ); };
401
403 float getVerticalDPI() const { return (dpi_units==2) ? dpi_y*100.0 : ( (dpi_units==1) ? dpi_y/0.0254 : dpi_y ); };
404
406 virtual bool regionDecoding(){ return false; };
407
409
412 virtual void Load( const std::string& module ) {};
413
415 virtual std::string getDescription() const { return std::string( "IIPImage Base Class" ); };
416
418 virtual void openImage() { throw file_error( "IIPImage openImage called" ); };
419
421
424 virtual void loadImageInfo( int x, int y ) {};
425
427 virtual void closeImage() {};
428
429
431
439 virtual RawTile getTile( int h, int v, unsigned int r, int l, unsigned int t, ImageEncoding e = ImageEncoding::RAW ) { return RawTile(); };
440
441
443
454 virtual RawTile getRegion( int ha, int va, unsigned int r, int layers, int x, int y, unsigned int w, unsigned int h ){ return RawTile(); };
455
456
458
460 swap( *this, image );
461 return *this;
462 };
463
464
466 static void setupLogging(){;};
467
468
470
471 static std::string getCodecVersion(){ return "0.0.0"; };
472
473
475 friend int operator == ( const IIPImage&, const IIPImage& );
476
478 friend int operator != ( const IIPImage&, const IIPImage& );
479
480};
481
482
483#endif
Base class to handle the input image sources.
Definition IIPImage.h:69
std::vector< float > min
The min and max sample value for each channel.
Definition IIPImage.h:162
unsigned int virtual_levels
Number of resolution levels that don't physically exist in file.
Definition IIPImage.h:116
const std::string getTimestamp()
Get a HTTP RFC 1123 formatted timestamp.
Definition IIPImage.cc:213
IIPImage()
Default Constructor.
Definition IIPImage.h:192
unsigned int getNumChannels() const
Return the number of channels for this image.
Definition IIPImage.h:336
bool set() const
Check whether this object has been initialised.
Definition IIPImage.h:313
IIPImage(const std::string &s)
Constructer taking the image path as parameter.
Definition IIPImage.h:214
std::vector< int > lut
LUT.
Definition IIPImage.h:113
void swap(IIPImage &a, IIPImage &b)
Swap function.
Definition IIPImage.cc:52
bool isSet
Indicate whether we have opened and initialised some parameters for this image.
Definition IIPImage.h:168
float dpi_x
Native physical resolution in both X and Y.
Definition IIPImage.h:143
float getVerticalDPI() const
Return physical resolution (DPI) in pixels/meter vertically.
Definition IIPImage.h:403
std::list< int > horizontalAnglesList
The list of available horizontal angles (for image sequences)
Definition IIPImage.h:107
IIPImage & operator=(IIPImage image)
Assignment operator.
Definition IIPImage.h:459
IIPImage(const IIPImage &image)
Copy Constructor taking reference to another IIPImage object.
Definition IIPImage.h:237
friend int operator!=(const IIPImage &, const IIPImage &)
Comparison non-equality operator.
Definition IIPImage.cc:346
float getMinValue(int n=0) const
Return the minimum sample value for each channel.
Definition IIPImage.h:341
void updateTimestamp(const std::string &s)
Get the image timestamp.
Definition IIPImage.cc:199
virtual ~IIPImage()
Virtual Destructor.
Definition IIPImage.h:275
std::vector< uint32_t > resolution_ids
List of IFD offsets for each resolution.
Definition IIPImage.h:128
std::map< const std::string, const std::string > metadata
STL map to hold string metadata.
Definition IIPImage.h:177
std::vector< unsigned int > image_widths
The image pixel dimensions.
Definition IIPImage.h:134
std::list< int > verticalAnglesList
The list of available vertical angles (for image sequences)
Definition IIPImage.h:110
unsigned int getImageHeight(int n=0) const
Return the image height in pixels for a given resolution.
Definition IIPImage.h:359
virtual RawTile getRegion(int ha, int va, unsigned int r, int layers, int x, int y, unsigned int w, unsigned int h)
Return a region for a given angle and resolution.
Definition IIPImage.h:454
int getNativeResolution(const int res) const
Return index of the resolution within the image file.
Definition IIPImage.h:330
time_t timestamp
Image modification timestamp.
Definition IIPImage.h:180
static bool codec_passthrough
Whether codec pass-through mode is enabled.
Definition IIPImage.h:186
unsigned int getTileHeight(int n=-1) const
Return the tile height in pixels for a given resolution.
Definition IIPImage.h:374
static void setupLogging()
Setup logging for codec library errors and warnings.
Definition IIPImage.h:466
virtual std::string getDescription() const
Return codec description: Overloaded by child class.
Definition IIPImage.h:415
virtual void loadImageInfo(int x, int y)
Load information about the image eg. number of channels, tile size etc.
Definition IIPImage.h:424
std::vector< unsigned int > tile_widths
The tile dimensions for each resolution.
Definition IIPImage.h:137
virtual bool regionDecoding()
Return whether this image type directly handles region decoding.
Definition IIPImage.h:406
ColorSpace colorspace
The color space of the image.
Definition IIPImage.h:140
SampleType sampleType
The sample format type (fixed or floating point)
Definition IIPImage.h:159
unsigned int getNumBitsPerPixel() const
Return the number of bits per pixel for this image.
Definition IIPImage.h:333
ImageEncoding getImageFormat() const
Get the image format.
Definition IIPImage.h:302
unsigned int numResolutions
The number of available resolutions in this image.
Definition IIPImage.h:150
unsigned int getTileWidth(int n=-1) const
Return the tile width in pixels for a given resolution.
Definition IIPImage.h:364
virtual RawTile getTile(int h, int v, unsigned int r, int l, unsigned int t, ImageEncoding e=ImageEncoding::RAW)
Return an individual tile for a given angle and resolution.
Definition IIPImage.h:439
const std::string & getMetadata(const std::string &index)
Return image metadata.
Definition IIPImage.h:395
unsigned int getImageWidth(int n=0) const
Return the image width in pixels for a given resolution.
Definition IIPImage.h:354
unsigned int getNumResolutions() const
Return the number of available resolutions in the image.
Definition IIPImage.h:325
void setFileSystemSuffix(const std::string &s)
Set a file system suffix.
Definition IIPImage.h:319
std::list< int > getHorizontalViewsList() const
Return a list of horizontal angles.
Definition IIPImage.h:290
bool isStack() const
Return whether image is a single-file image stack.
Definition IIPImage.h:385
static std::string getCodecVersion()
Get codec version.
Definition IIPImage.h:471
PyramidType pyramid
Define how pyramid is structured.
Definition IIPImage.h:122
const std::string & getImagePath() const
Return the image path.
Definition IIPImage.h:293
const std::string getFileName(int x, int y)
Return the full file path for a particular horizontal and vertical angle.
Definition IIPImage.cc:320
virtual void closeImage()
Close the image: Overloaded by child class.
Definition IIPImage.h:427
virtual void Load(const std::string &module)
Load the appropriate codec module for this image type.
Definition IIPImage.h:412
unsigned int quality_layers
Quality layers.
Definition IIPImage.h:165
ColorSpace getColorSpace() const
Return the color space for this image.
Definition IIPImage.h:382
int currentX
If we have an image sequence, the current X and Y position.
Definition IIPImage.h:171
SampleType getSampleType() const
Return the sample format type.
Definition IIPImage.h:349
static bool logging
Our logging stream - declared statically.
Definition IIPImage.h:183
friend int operator==(const IIPImage &, const IIPImage &)
Comparison equality operator.
Definition IIPImage.cc:338
virtual void openImage()
Open the image: Overloaded by child class.
Definition IIPImage.h:418
std::vector< unsigned int > histogram
Image histogram.
Definition IIPImage.h:174
void Initialise()
Test the image and initialise some parameters.
Definition IIPImage.cc:299
float getMaxValue(int n=0) const
Return the minimum sample value for each channel.
Definition IIPImage.h:346
void setFileSystemPrefix(const std::string &prefix)
Set a file system prefix for added security.
Definition IIPImage.h:316
std::list< Stack > stack
Whether we have an image stack consisting of multiple images within a single file.
Definition IIPImage.h:125
float getHorizontalDPI() const
Return physical resolution (DPI) in pixels/meter horizontally.
Definition IIPImage.h:400
std::list< int > getVerticalViewsList() const
Return a list of available vertical angles.
Definition IIPImage.h:287
int dpi_units
Units for native physical resolution.
Definition IIPImage.h:147
PyramidType
Multi-resolution pyramid type - used currently only to distinguish TIFF types.
Definition IIPImage.h:104
std::list< Stack > getStack() const
Load stack info.
Definition IIPImage.h:391
void setFileNamePattern(const std::string &pattern)
Set the file name pattern used in image sequences.
Definition IIPImage.h:322
ImageEncoding format
Return the image format e.g. tif.
Definition IIPImage.h:119
unsigned int channels
The number of channels for this image.
Definition IIPImage.h:156
unsigned int bpc
The bits per channel for this image.
Definition IIPImage.h:153
Class to represent a single image tile.
Definition RawTile.h:45
Define our own derived exception class for file errors.
Definition IIPImage.h:44
file_error(const std::string &s)
Definition IIPImage.h:47
Definition IIPImage.h:54
std::string name
Stack layer name.
Definition IIPImage.h:55
float scale
Pixel scaling factor.
Definition IIPImage.h:56