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
IIPImage.h
1// IIPImage class
2
3/* IIP fcgi server module
4
5 Copyright (C) 2000-2023 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// Supported image formats
52enum ImageFormat { TIF, JPEG2000, UNSUPPORTED };
53
54
55// Multi-resolution pyramid type
56enum PyramidType { NORMAL, SUBIFD };
57
58
59// Structure for storing basic information on image stacks
60// - for now just stores stack name and scaling factor
61struct Stack {
62 std::string name;
63 float scale;
64 Stack() : scale(1) {};
65};
66
67
68
70
76class IIPImage {
77
78 private:
79
81 std::string imagePath;
82
84 std::string fileSystemPrefix;
85
87 std::string fileSystemSuffix;
88
90 std::string fileNamePattern;
91
93 bool isFile;
94
96 std::string suffix;
97
99 void testImageType();
100
102 void measureHorizontalAngles();
103
105 void measureVerticalAngles();
106
107
108 protected:
109
111 std::list <int> horizontalAnglesList;
112
114 std::list <int> verticalAnglesList;
115
117 std::vector <int> lut;
118
120 unsigned int virtual_levels;
121
123 ImageFormat format;
124
126 PyramidType pyramid;
127
129 std::list <Stack> stack;
130
131 std::vector <uint32_t> resolution_ids;
132
133 public:
134
136 std::vector <unsigned int> image_widths, image_heights;
137
139 std::vector <unsigned int> tile_widths, tile_heights;
140
142 ColourSpaces colourspace;
143
145 float dpi_x, dpi_y;
146
148
150
152 unsigned int numResolutions;
153
155 unsigned int bpc;
156
158 unsigned int channels;
159
161 SampleType sampleType;
162
164 std::vector <float> min, max;
165
167 unsigned int quality_layers;
168
170 bool isSet;
171
173 int currentX, currentY;
174
176 std::vector<unsigned int> histogram;
177
179 std::map <const std::string, std::string> metadata;
180
182 time_t timestamp;
183
185 static bool logging;
186
187
188 public:
189
192 isFile( false ),
193 virtual_levels( 0 ),
194 format( UNSUPPORTED ),
195 pyramid( NORMAL ),
196 colourspace( NONE ),
197 dpi_x( 0 ),
198 dpi_y( 0 ),
199 dpi_units( 0 ),
200 numResolutions( 0 ),
201 bpc( 0 ),
202 channels( 0 ),
203 sampleType( FIXEDPOINT ),
204 quality_layers( 0 ),
205 isSet( false ),
206 currentX( 0 ),
207 currentY( 90 ),
208 timestamp( 0 ) {};
209
211
213 IIPImage( const std::string& s ) :
214 imagePath( s ),
215 isFile( false ),
216 virtual_levels( 0 ),
217 format( UNSUPPORTED ),
218 pyramid( NORMAL ),
219 colourspace( NONE ),
220 dpi_x( 0 ),
221 dpi_y( 0 ),
222 dpi_units( 0 ),
223 numResolutions( 0 ),
224 bpc( 0 ),
225 channels( 0 ),
226 sampleType( FIXEDPOINT ),
227 quality_layers( 0 ),
228 isSet( false ),
229 currentX( 0 ),
230 currentY( 90 ),
231 timestamp( 0 ) {};
232
234
236 IIPImage( const IIPImage& image ) :
237 imagePath( image.imagePath ),
238 fileSystemPrefix( image.fileSystemPrefix ),
239 fileSystemSuffix( image.fileSystemSuffix ),
240 fileNamePattern( image.fileNamePattern ),
241 isFile( image.isFile ),
242 suffix( image.suffix ),
245 lut( image.lut ),
247 format( image.format ),
248 pyramid( image.pyramid ),
249 stack( image.stack ),
250 resolution_ids( image.resolution_ids ),
251 image_widths( image.image_widths ),
252 image_heights( image.image_heights ),
253 tile_widths( image.tile_widths ),
254 tile_heights( image.tile_heights ),
255 colourspace( image.colourspace ),
256 dpi_x( image.dpi_x ),
257 dpi_y( image.dpi_y ),
258 dpi_units( image.dpi_units ),
260 bpc( image.bpc ),
261 channels( image.channels ),
262 sampleType( image.sampleType ),
263 min( image.min ),
264 max( image.max ),
266 isSet( image.isSet ),
267 currentX( image.currentX ),
268 currentY( image.currentY ),
269 histogram( image.histogram ),
270 metadata( image.metadata ),
271 timestamp( image.timestamp ) {};
272
274 virtual ~IIPImage() {};
275
278
280
283 void swap( IIPImage& a, IIPImage& b );
284
286 std::list <int> getVerticalViewsList() const { return verticalAnglesList; };
287
289 std::list <int> getHorizontalViewsList() const { return horizontalAnglesList; };
290
292 const std::string& getImagePath() const { return imagePath; };
293
295
298 const std::string getFileName( int x, int y );
299
301 ImageFormat getImageFormat() const { return format; };
302
304
306 void updateTimestamp( const std::string& s );
307
309 const std::string getTimestamp();
310
312 bool set() const { return isSet; };
313
315 void setFileSystemPrefix( const std::string& prefix ) { fileSystemPrefix = prefix; };
316
318 void setFileSystemSuffix( const std::string& s ) { fileSystemSuffix = s; };
319
321 void setFileNamePattern( const std::string& pattern ) { fileNamePattern = pattern; };
322
324 unsigned int getNumResolutions() const { return numResolutions; };
325
327
329 int getNativeResolution( const int res ) const { return numResolutions - res - 1; };
330
332 unsigned int getNumBitsPerPixel() const { return bpc; };
333
335 unsigned int getNumChannels() const { return channels; };
336
338
340 float getMinValue( int n=0 ) const { return min[n]; };
341
343
345 float getMaxValue( int n=0 ) const { return max[n]; };
346
348 SampleType getSampleType() const { return sampleType; };
349
351
353 unsigned int getImageWidth( int n=0 ) const { return image_widths[n]; };
354
356
358 unsigned int getImageHeight( int n=0 ) const { return image_heights[n]; };
359
361
363 unsigned int getTileWidth( int n=-1 ) const {
364 if( n == -1 ) n = 0;
365 else n = getNativeResolution( n );
366 if( tile_widths.size() < (size_t) n+1 ) n = 0;
367 return tile_widths[n];
368 };
369
371
373 unsigned int getTileHeight( int n=-1 ) const {
374 if( n == -1 ) n = 0;
375 else n = getNativeResolution( n );
376 if( tile_heights.size() < (size_t) n+1 ) n = 0;
377 return tile_heights[n];
378 };
379
381 ColourSpaces getColourSpace() const { return colourspace; };
382
384 bool isStack() const {
385 if( stack.size() > 0 ) return true;
386 return false;
387 };
388
390 std::list <Stack> getStack() const { return stack; };
391
393
394 const std::string& getMetadata( const std::string& index ){
395 return metadata[index];
396 };
397
399 float getHorizontalDPI() const { return (dpi_units==2) ? dpi_x*10.0 : ( (dpi_units==1) ? dpi_x*25.4 : dpi_x ); };
400
402 float getVerticalDPI() const { return (dpi_units==2) ? dpi_y*10.0 : ( (dpi_units==1) ? dpi_y*25.4 : dpi_y ); };
403
405 virtual bool regionDecoding(){ return false; };
406
408
411 virtual void Load( const std::string& module ) {};
412
414 virtual std::string getDescription() const { return std::string( "IIPImage Base Class" ); };
415
417 virtual void openImage() { throw file_error( "IIPImage openImage called" ); };
418
420
423 virtual void loadImageInfo( int x, int y ) {};
424
426 virtual void closeImage() {};
427
428
430
437 virtual RawTile getTile( int h, int v, unsigned int r, int l, unsigned int t ) { return RawTile(); };
438
439
441
452 virtual RawTile getRegion( int ha, int va, unsigned int r, int layers, int x, int y, unsigned int w, unsigned int h ){ return RawTile(); };
453
454
456
458 swap( *this, image );
459 return *this;
460 };
461
462
464 static void setupLogging(){;};
465
466
468 friend int operator == ( const IIPImage&, const IIPImage& );
469
471 friend int operator != ( const IIPImage&, const IIPImage& );
472
473};
474
475
476#endif
Main class to handle the pyramidal image source.
Definition: IIPImage.h:76
std::vector< float > min
The min and max sample value for each channel.
Definition: IIPImage.h:164
unsigned int virtual_levels
Number of resolution levels that don't physically exist in file.
Definition: IIPImage.h:120
const std::string getTimestamp()
Get a HTTP RFC 1123 formatted timestamp.
IIPImage()
Default Constructor.
Definition: IIPImage.h:191
unsigned int getNumChannels() const
Return the number of channels for this image.
Definition: IIPImage.h:335
bool set() const
Check whether this object has been initialised.
Definition: IIPImage.h:312
IIPImage(const std::string &s)
Constructer taking the image path as parameter.
Definition: IIPImage.h:213
ColourSpaces getColourSpace() const
Return the colour space for this image.
Definition: IIPImage.h:381
ImageFormat getImageFormat() const
Get the image format.
Definition: IIPImage.h:301
std::vector< int > lut
LUT.
Definition: IIPImage.h:117
void swap(IIPImage &a, IIPImage &b)
Swap function.
const std::string getFileName(int x, int y)
Return the full file path for a particular horizontal and vertical angle.
bool isSet
Indicate whether we have opened and initialised some parameters for this image.
Definition: IIPImage.h:170
float dpi_x
Native physical resolution in both X and Y.
Definition: IIPImage.h:145
ImageFormat format
Return the image format e.g. tif.
Definition: IIPImage.h:123
float getVerticalDPI() const
Return physical resolution (DPI) in pixels/meter vertically.
Definition: IIPImage.h:402
std::list< int > horizontalAnglesList
The list of available horizontal angles (for image sequences)
Definition: IIPImage.h:111
IIPImage & operator=(IIPImage image)
Assignment operator.
Definition: IIPImage.h:457
IIPImage(const IIPImage &image)
Copy Constructor taking reference to another IIPImage object.
Definition: IIPImage.h:236
friend int operator!=(const IIPImage &, const IIPImage &)
Comparison non-equality operator.
float getMinValue(int n=0) const
Return the minimum sample value for each channel.
Definition: IIPImage.h:340
void updateTimestamp(const std::string &s)
Get the image timestamp.
virtual ~IIPImage()
Virtual Destructor.
Definition: IIPImage.h:274
std::vector< unsigned int > image_widths
The image pixel dimensions.
Definition: IIPImage.h:136
std::list< int > verticalAnglesList
The list of available vertical angles (for image sequences)
Definition: IIPImage.h:114
unsigned int getImageHeight(int n=0) const
Return the image height in pixels for a given resolution.
Definition: IIPImage.h:358
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:452
int getNativeResolution(const int res) const
Return index of the resolution within the image file.
Definition: IIPImage.h:329
time_t timestamp
Image modification timestamp.
Definition: IIPImage.h:182
unsigned int getTileHeight(int n=-1) const
Return the tile height in pixels for a given resolution.
Definition: IIPImage.h:373
static void setupLogging()
Setup logging for codec library errors and warnings.
Definition: IIPImage.h:464
virtual std::string getDescription() const
Return codec description: Overloaded by child class.
Definition: IIPImage.h:414
virtual void loadImageInfo(int x, int y)
Load information about the image eg. number of channels, tile size etc.
Definition: IIPImage.h:423
std::vector< unsigned int > tile_widths
The tile dimensions for each resolution.
Definition: IIPImage.h:139
virtual bool regionDecoding()
Return whether this image type directly handles region decoding.
Definition: IIPImage.h:405
SampleType sampleType
The sample format type (fixed or floating point)
Definition: IIPImage.h:161
unsigned int getNumBitsPerPixel() const
Return the number of bits per pixel for this image.
Definition: IIPImage.h:332
std::map< const std::string, std::string > metadata
STL map to hold string metadata.
Definition: IIPImage.h:179
unsigned int numResolutions
The number of available resolutions in this image.
Definition: IIPImage.h:152
unsigned int getTileWidth(int n=-1) const
Return the tile width in pixels for a given resolution.
Definition: IIPImage.h:363
const std::string & getMetadata(const std::string &index)
Return image metadata.
Definition: IIPImage.h:394
unsigned int getImageWidth(int n=0) const
Return the image width in pixels for a given resolution.
Definition: IIPImage.h:353
unsigned int getNumResolutions() const
Return the number of available resolutions in the image.
Definition: IIPImage.h:324
void setFileSystemSuffix(const std::string &s)
Set a file system suffix.
Definition: IIPImage.h:318
std::list< int > getHorizontalViewsList() const
Return a list of horizontal angles.
Definition: IIPImage.h:289
bool isStack() const
Return whether image is a single-file image stack.
Definition: IIPImage.h:384
PyramidType pyramid
Define how pyramid is structured.
Definition: IIPImage.h:126
const std::string & getImagePath() const
Return the image path.
Definition: IIPImage.h:292
virtual void closeImage()
Close the image: Overloaded by child class.
Definition: IIPImage.h:426
virtual void Load(const std::string &module)
Load the appropriate codec module for this image type.
Definition: IIPImage.h:411
unsigned int quality_layers
Quality layers.
Definition: IIPImage.h:167
int currentX
If we have an image sequence, the current X and Y position.
Definition: IIPImage.h:173
SampleType getSampleType() const
Return the sample format type.
Definition: IIPImage.h:348
static bool logging
Our logging stream - declared statically.
Definition: IIPImage.h:185
friend int operator==(const IIPImage &, const IIPImage &)
Comparison equality operator.
virtual void openImage()
Open the image: Overloaded by child class.
Definition: IIPImage.h:417
ColourSpaces colourspace
The colour space of the image.
Definition: IIPImage.h:142
std::vector< unsigned int > histogram
Image histogram.
Definition: IIPImage.h:176
void Initialise()
Test the image and initialise some parameters.
float getMaxValue(int n=0) const
Return the minimum sample value for each channel.
Definition: IIPImage.h:345
void setFileSystemPrefix(const std::string &prefix)
Set a file system prefix for added security.
Definition: IIPImage.h:315
std::list< Stack > stack
Whether we have an image stack consisting of multiple images within a single file.
Definition: IIPImage.h:129
float getHorizontalDPI() const
Return physical resolution (DPI) in pixels/meter horizontally.
Definition: IIPImage.h:399
std::list< int > getVerticalViewsList() const
Return a list of available vertical angles.
Definition: IIPImage.h:286
int dpi_units
Units for native physical resolution.
Definition: IIPImage.h:149
virtual RawTile getTile(int h, int v, unsigned int r, int l, unsigned int t)
Return an individual tile for a given angle and resolution.
Definition: IIPImage.h:437
std::list< Stack > getStack() const
Load stack info.
Definition: IIPImage.h:390
void setFileNamePattern(const std::string &pattern)
Set the file name pattern used in image sequences.
Definition: IIPImage.h:321
unsigned int channels
The number of channels for this image.
Definition: IIPImage.h:158
unsigned int bpc
The bits per channel for this image.
Definition: IIPImage.h:155
Class to represent a single image tile.
Definition: RawTile.h:47
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:61