1 // RawTile class
2 
3 /*  IIP Image Server
4 
5     Copyright (C) 2000-2019 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 
26 #include <cstring>
27 #include <string>
28 #include <cstdlib>
29 #include <ctime>
30 
31 
32 
33 /// Colour spaces - GREYSCALE, sRGB and CIELAB
34 enum ColourSpaces { NONE, GREYSCALE, sRGB, CIELAB, BINARY };
35 
36 /// Compression Types
37 enum CompressionType { UNCOMPRESSED, JPEG, DEFLATE, PNG };
38 
39 /// Sample Types
40 enum SampleType { FIXEDPOINT, FLOATINGPOINT };
41 
42 
43 /// Class to represent a single image tile
44 
45 class RawTile{
46 
47  public:
48 
49   /// The tile number for this tile
50   int tileNum;
51 
52   /// The resolution to which this tile belongs
53   int resolution;
54 
55   /// The horizontal angle to which this tile belongs
56   int hSequence;
57 
58   /// The vertical angle to which this tile belongs
59   int vSequence;
60 
61   /// Compression type
62   CompressionType compressionType;
63 
64   /// Compression rate or quality
65   int quality;
66 
67   /// Name of the file from which this tile comes
68   std::string filename;
69 
70   /// Tile timestamp
71   time_t timestamp;
72 
73   /// Pointer to the image data
74   void *data;
75 
76   /// This tracks whether we have allocated memory locally for data
77   /// or whether it is simply a pointer
78   /** This is used in the destructor to make sure we deallocate correctly */
79   int memoryManaged;
80 
81   /// The size of the data pointed to by data
82   unsigned int dataLength;
83 
84   /// The width in pixels of this tile
85   unsigned int width;
86 
87   /// The height in pixels of this tile
88   unsigned int height;
89 
90   /// The number of channels for this tile
91   int channels;
92 
93   /// The number of bits per channel for this tile
94   int bpc;
95 
96   /// Sample format type (fixed or floating point)
97   SampleType sampleType;
98 
99   /// Padded
100   bool padded;
101 
102 
103   /// Main constructor
104   /** @param tn tile number
105       @param res resolution
106       @param hs horizontal sequence angle
107       @param vs vertical sequence angle
108       @param w tile width
109       @param h tile height
110       @param c number of channels
111       @param b bits per channel per sample
112   */
113   RawTile( int tn = 0, int res = 0, int hs = 0, int vs = 0,
114 	   int w = 0, int h = 0, int c = 0, int b = 0 ) {
115     width = w; height = h; bpc = b; dataLength = 0; data = NULL;
116     tileNum = tn; resolution = res; hSequence = hs ; vSequence = vs;
117     memoryManaged = 1; channels = c; compressionType = UNCOMPRESSED; quality = 0;
118     timestamp = 0; sampleType = FIXEDPOINT; padded = false;
119   };
120 
121 
122   /// Destructor to free the data array if is has previously be allocated locally
~RawTile()123   ~RawTile() {
124     if( data && memoryManaged ){
125       switch( bpc ){
126       case 32:
127         if( sampleType == FLOATINGPOINT ) delete[] (float*) data;
128         else delete[] (unsigned int*) data;
129         break;
130       case 16:
131 	delete[] (unsigned short*) data;
132         break;
133       default:
134 	delete[] (unsigned char*) data;
135         break;
136       }
137     }
138   }
139 
140 
141   /// Copy constructor - handles copying of data buffer
RawTile(const RawTile & tile)142   RawTile( const RawTile& tile ) {
143 
144     tileNum = tile.tileNum;
145     resolution = tile.resolution;
146     hSequence = tile.hSequence;
147     vSequence = tile.vSequence;
148     compressionType = tile.compressionType;
149     quality = tile.quality;
150     filename = tile.filename;
151     timestamp = tile.timestamp;
152     memoryManaged = tile.memoryManaged;
153     dataLength = tile.dataLength;
154     width = tile.width;
155     height = tile.height;
156     channels = tile.channels;
157     bpc = tile.bpc;
158     sampleType = tile.sampleType;
159     padded = tile.padded;
160 
161     switch( bpc ){
162       case 32:
163 	if( sampleType == FLOATINGPOINT ) data = new float[dataLength/4];
164 	else data = new unsigned int[dataLength/4];
165 	break;
166       case 16:
167 	data = new unsigned short[dataLength/2];
168 	break;
169       default:
170 	data = new unsigned char[dataLength];
171 	break;
172     }
173 
174     if( data && (dataLength > 0) && tile.data ){
175       memcpy( data, tile.data, dataLength );
176       memoryManaged = 1;
177     }
178   }
179 
180 
181   /// Copy assignment constructor
182   RawTile& operator= ( const RawTile& tile ) {
183 
184     tileNum = tile.tileNum;
185     resolution = tile.resolution;
186     hSequence = tile.hSequence;
187     vSequence = tile.vSequence;
188     compressionType = tile.compressionType;
189     quality = tile.quality;
190     filename = tile.filename;
191     timestamp = tile.timestamp;
192     memoryManaged = tile.memoryManaged;
193     dataLength = tile.dataLength;
194     width = tile.width;
195     height = tile.height;
196     channels = tile.channels;
197     bpc = tile.bpc;
198     sampleType = tile.sampleType;
199     padded = tile.padded;
200 
201     switch( bpc ){
202       case 32:
203 	if( sampleType == FLOATINGPOINT ) data = new float[dataLength/4];
204 	else data = new int[dataLength/4];
205 	break;
206       case 16:
207 	data = new unsigned short[dataLength/2];
208 	break;
209       default:
210 	data = new unsigned char[dataLength];
211 	break;
212     }
213 
214     if( data && (dataLength > 0) && tile.data ){
215       memcpy( data, tile.data, dataLength );
216       memoryManaged = 1;
217     }
218 
219     return *this;
220   }
221 
222 
223   /// Return the size of the data
size()224   unsigned int size() { return dataLength; }
225 
226 
227   /// Overloaded equality operator
228   friend int operator == ( const RawTile& A, const RawTile& B ) {
229     if( (A.tileNum == B.tileNum) &&
230 	(A.resolution == B.resolution) &&
231 	(A.hSequence == B.hSequence) &&
232 	(A.vSequence == B.vSequence) &&
233 	(A.compressionType == B.compressionType) &&
234 	(A.quality == B.quality) &&
235 	(A.filename == B.filename) ){
236       return( 1 );
237     }
238     else return( 0 );
239   }
240 
241 
242   /// Overloaded non-equality operator
243   friend int operator != ( const RawTile& A, const RawTile& B ) {
244     if( (A.tileNum == B.tileNum) &&
245 	(A.resolution == B.resolution) &&
246 	(A.hSequence == B.hSequence) &&
247 	(A.vSequence == B.vSequence) &&
248 	(A.compressionType == B.compressionType) &&
249 	(A.quality == B.quality) &&
250 	(A.filename == B.filename) ){
251       return( 0 );
252     }
253     else return( 1 );
254   }
255 
256 
257 };
258 
259 
260 #endif
261