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
RawTile.h
1
2
3/* IIPImage Server
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 _RAWTILE_H
24#define _RAWTILE_H
25#include <cstring>
26#include <string>
27#include <cstdlib>
28#include <ctime>
29
30#if !( (__cplusplus >= 201103L) || ((defined(_MSC_VER) && _MSC_VER >= 1900)) )
31#include <stdint.h> // Required for C++98
32#endif
33
34
36enum ColourSpaces { NONE, GREYSCALE, sRGB, CIELAB, BINARY };
37
39enum CompressionType { UNCOMPRESSED, JPEG, DEFLATE, PNG, WEBP };
40
42enum SampleType { FIXEDPOINT, FLOATINGPOINT };
43
44
45
47class RawTile {
48
49 public:
50
52 std::string filename;
53
55 unsigned int width;
56
58 unsigned int height;
59
62
64 int bpc;
65
67 SampleType sampleType;
68
70 CompressionType compressionType;
71
74
76 time_t timestamp;
77
80
83
86
89
91 uint32_t capacity;
92
94 uint32_t dataLength;
95
98
100
102 void *data;
103
104
106
115 RawTile( int tn = 0, int res = 0, int hs = 0, int vs = 0,
116 int w = 0, int h = 0, int c = 0, int b = 0 )
117 : width( w ),
118 height( h ),
119 channels( c ),
120 bpc( b ),
121 sampleType( FIXEDPOINT ),
122 compressionType( UNCOMPRESSED ),
123 quality( 0 ),
124 timestamp( 0 ),
125 tileNum( tn ),
126 resolution( res ),
127 hSequence( hs ),
128 vSequence( vs ),
129 capacity( 0 ),
130 dataLength( 0 ),
131 memoryManaged( 1 ),
132 data( NULL ) {};
133
134
135
139 }
140
141
142
144 RawTile( const RawTile& tile )
145 : filename( tile.filename ),
146 width( tile.width ),
147 height( tile.height ),
148 channels( tile.channels ),
149 bpc( tile.bpc ),
150 sampleType( tile.sampleType ),
152 quality( tile.quality ),
153 timestamp( tile.timestamp ),
154 tileNum( tile.tileNum ),
155 resolution( tile.resolution ),
156 hSequence( tile.hSequence ),
157 vSequence( tile.vSequence ),
158 capacity( tile.capacity ),
159 dataLength( tile.dataLength ),
161 data( NULL )
162 {
163
164 if( tile.data && tile.dataLength > 0 ){
165 allocate( tile.dataLength );
166 memcpy( data, tile.data, tile.dataLength );
167 memoryManaged = 1;
168 }
169
170 }
171
172
173
175 RawTile& operator= ( const RawTile& tile ) {
176
177 if( this != &tile ){
178
179 tileNum = tile.tileNum;
180 resolution = tile.resolution;
181 hSequence = tile.hSequence;
182 vSequence = tile.vSequence;
184 quality = tile.quality;
185 filename = tile.filename;
186 timestamp = tile.timestamp;
188 dataLength = tile.dataLength;
189 width = tile.width;
190 height = tile.height;
191 channels = tile.channels;
192 bpc = tile.bpc;
193 sampleType = tile.sampleType;
194 capacity = tile.capacity;
195
196 if( tile.data && tile.dataLength > 0 ){
197 allocate( tile.dataLength );
198 memcpy( data, tile.data, tile.dataLength );
199 memoryManaged = 1;
200 }
201 }
202
203 return *this;
204 }
205
206
207
209
211 void allocate( uint32_t size = 0 ) {
212
213 if( size == 0 ) size = (uint32_t) width * height * channels * (bpc/8);
214
215 switch( bpc ){
216 case 32:
217 if( sampleType == FLOATINGPOINT ) data = new float[size/4];
218 else data = new int[size/4];
219 break;
220 case 16:
221 data = new unsigned short[size/2];
222 break;
223 default:
224 data = new unsigned char[size];
225 break;
226 }
227
228 memoryManaged = 1;
229 capacity = size;
230 };
231
232
233
235 void deallocate( void* buffer ) {
236
237 if( buffer ){
238 switch( bpc ){
239 case 32:
240 if( sampleType == FLOATINGPOINT ) delete[] (float*) buffer;
241 else delete[] (unsigned int*) buffer;
242 break;
243 case 16:
244 delete[] (unsigned short*) buffer;
245 break;
246 default:
247 delete[] (unsigned char*) buffer;
248 break;
249 }
250
251 buffer = NULL;
252 capacity = 0;
253 dataLength = 0;
254 }
255 };
256
257
258
260
263 void crop( const unsigned int w, const unsigned int h ) {
264
265 // Keep track of original data buffer and whether we manage it
266 void* buffer = data;
267 int mm = memoryManaged;
268
269 // Create a new buffer with the new size
270 unsigned int len = w * h * channels * (bpc/8);
271 allocate( len );
272
273 unsigned char* src_ptr = (unsigned char*) buffer;
274 unsigned char* dst_ptr = (unsigned char*) data;
275
276 // Copy one scanline at a time
277 unsigned int dlen = w * channels * (bpc/8);
278 unsigned int slen = width * channels * (bpc/8);
279
280 for( unsigned int i=0; i<h; i++ ){
281 memcpy( dst_ptr, src_ptr, dlen );
282 dst_ptr += dlen;
283 src_ptr += slen;
284 }
285
286 // Delete original memory buffer
287 if( mm ) deallocate( buffer );
288
289 // Set the new tile dimensions and data storage size
290 capacity = len; // Need to set this manually as deallocate sets this to zero
291 dataLength = len;
292 width = w;
293 height = h;
294 };
295
296
297
299 friend int operator == ( const RawTile& A, const RawTile& B ) {
300 if( (A.tileNum == B.tileNum) &&
301 (A.resolution == B.resolution) &&
302 (A.hSequence == B.hSequence) &&
303 (A.vSequence == B.vSequence) &&
305 (A.quality == B.quality) &&
306 (A.filename == B.filename) ){
307 return 1;
308 }
309 else return 0;
310 }
311
312
313
315 friend int operator != ( const RawTile& A, const RawTile& B ) {
316 if( (A.tileNum == B.tileNum) &&
317 (A.resolution == B.resolution) &&
318 (A.hSequence == B.hSequence) &&
319 (A.vSequence == B.vSequence) &&
321 (A.quality == B.quality) &&
322 (A.filename == B.filename) ){
323 return 0;
324 }
325 else return 1;
326 }
327
328
329
330 // Memory efficient move operators possible in C++11 onwards
331#if (__cplusplus >= 201103L) || ((defined(_MSC_VER) && _MSC_VER >= 1900))
332
334 RawTile( RawTile&& tile ) noexcept
335 : filename( tile.filename ),
336 width( tile.width ),
337 height( tile.height ),
338 channels( tile.channels ),
339 bpc( tile.bpc ),
340 sampleType( tile.sampleType ),
341 compressionType( tile.compressionType ),
342 quality( tile.quality ),
343 timestamp( tile.timestamp ),
344 tileNum( tile.tileNum ),
345 resolution( tile.resolution ),
346 hSequence( tile.hSequence ),
347 vSequence( tile.vSequence ),
348 capacity( tile.capacity ),
349 dataLength( tile.dataLength ),
350 memoryManaged( tile.memoryManaged ),
351 data( NULL )
352 {
353
354 if( tile.memoryManaged == 1 ){
355
356 // Transfer ownership of data
357 data = tile.data;
358
359 // Free data from other RawTile
360 tile.data = nullptr;
361 tile.dataLength = 0;
362 tile.capacity = 0;
363 tile.memoryManaged = 0;
364 }
365 // Need to copy data if other tile is not owner of its data
366 else if( tile.data && tile.dataLength > 0 ){
367 allocate( tile.dataLength );
368 memcpy( data, tile.data, tile.dataLength );
369 memoryManaged = 1;
370 }
371 }
372
373
374
376 RawTile& operator= ( RawTile&& tile ) noexcept {
377
378 if( this != &tile ){
379
380 if( data && dataLength>0 ) delete this;
381
382 // Use move for std::string. The other fields are of basic type
383 filename = std::move( tile.filename );
384 tileNum = tile.tileNum;
385 resolution = tile.resolution;
386 hSequence = tile.hSequence;
387 vSequence = tile.vSequence;
388 compressionType = tile.compressionType;
389 quality = tile.quality;
390 timestamp = tile.timestamp;
391 memoryManaged = tile.memoryManaged;
392 capacity = tile.capacity;
393 dataLength = tile.dataLength;
394 width = tile.width;
395 height = tile.height;
396 channels = tile.channels;
397 bpc = tile.bpc;
398 sampleType = tile.sampleType;
399
400 if( tile.memoryManaged == 1 ){
401
402 // Transfer ownership of raw data
403 data = tile.data;
404
405 // Free data from other tile
406 tile.data = nullptr;
407 tile.dataLength = 0;
408 tile.capacity = 0;
409 tile.memoryManaged = 0;
410 }
411 else if( tile.data && tile.dataLength > 0 ){
412 allocate( tile.dataLength );
413 memcpy( data, tile.data, tile.dataLength );
414 memoryManaged = 1;
415 }
416 }
417
418 return *this;
419 }
420
421#endif
422
423};
424
425#endif
Class to represent a single image tile.
Definition: RawTile.h:47
RawTile(int tn=0, int res=0, int hs=0, int vs=0, int w=0, int h=0, int c=0, int b=0)
Main constructor.
Definition: RawTile.h:115
unsigned int width
The width in pixels of this tile.
Definition: RawTile.h:55
uint32_t dataLength
The size of the data pointed to by the data pointer in bytes.
Definition: RawTile.h:94
std::string filename
Name of the file from which this tile comes.
Definition: RawTile.h:52
int bpc
The number of bits per channel for this tile.
Definition: RawTile.h:64
void allocate(uint32_t size=0)
Allocate memory for the tile.
Definition: RawTile.h:211
friend int operator!=(const RawTile &A, const RawTile &B)
Overloaded non-equality operator.
Definition: RawTile.h:315
time_t timestamp
Tile timestamp.
Definition: RawTile.h:76
int vSequence
The vertical angle to which this tile belongs.
Definition: RawTile.h:88
RawTile & operator=(const RawTile &tile)
Copy assignment constructor.
Definition: RawTile.h:175
friend int operator==(const RawTile &A, const RawTile &B)
Overloaded equality operator.
Definition: RawTile.h:299
~RawTile()
Destructor to free the data array if is has previously be allocated locally.
Definition: RawTile.h:137
uint32_t capacity
Amount of memory actually allocated in bytes.
Definition: RawTile.h:91
void crop(const unsigned int w, const unsigned int h)
Crop tile to the defined dimensions.
Definition: RawTile.h:263
int tileNum
The tile number for this tile.
Definition: RawTile.h:79
int resolution
The resolution number to which this tile belongs.
Definition: RawTile.h:82
void deallocate(void *buffer)
Free our data buffer.
Definition: RawTile.h:235
void * data
Pointer to the image data.
Definition: RawTile.h:102
RawTile(const RawTile &tile)
Copy constructor - handles copying of data buffer.
Definition: RawTile.h:144
SampleType sampleType
Sample format type (fixed or floating point)
Definition: RawTile.h:67
CompressionType compressionType
Compression type.
Definition: RawTile.h:70
int memoryManaged
Definition: RawTile.h:99
int channels
The number of channels for this tile.
Definition: RawTile.h:61
int quality
Compression rate or quality.
Definition: RawTile.h:73
int hSequence
The horizontal angle to which this tile belongs.
Definition: RawTile.h:85
unsigned int height
The height in pixels of this tile.
Definition: RawTile.h:58