summaryrefslogtreecommitdiffstats
path: root/conversion/convertor.cc
blob: 9c6f3537bee7840c69064e50de3ce81c9f8fbde5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
* This file is part of rasdaman community.
*
* Rasdaman community is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rasdaman community is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with rasdaman community.  If not, see <http://www.gnu.org/licenses/>.
*
* Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Peter Baumann /
rasdaman GmbH.
*
* For more information please see <http://www.rasdaman.org>
* or contact Peter Baumann via <baumann@rasdaman.com>.
/
/**
 * SOURCE: convertor.cc
 *
 * MODULE:  conversion
 *
 * CLASSES: r_Convertor, r_Convert_Memory
 *
 * PURPOPSE:
 * Provides interface to convert data to other formats.
 *
 * COMMENTS:
 * - FIXME: r_Convertor::get_internal_type(): every structType is recognised as RGB!
 *
*/

#include "conversion/convertor.hh"
#include "conversion/memfs.hh"
#include "raslib/error.hh"
#include "raslib/parseparams.hh"
#include "raslib/rminit.hh"
#include "raslib/primitivetype.hh"


/*
 *  r_Convertor class
 */

void r_Convertor::initShare( const char *src, const r_Minterval &interv )
{
	desc.src = src; 
	desc.srcInterv = interv;
	desc.srcType = NULL; 
	desc.destType = NULL;
	desc.dest = NULL;
	params = NULL;
	destroySrc = false;
}


r_Convertor::r_Convertor( void )
{
	desc.srcType = NULL;
	desc.destType = NULL;
	desc.dest = NULL; 
	desc.src = NULL; 
	desc.baseType = ctype_void;
	params = NULL;
}


r_Convertor::r_Convertor( const char *src, const r_Minterval &interv, const r_Type *tp, bool fullTypes ) throw(r_Error)
{
	initShare(src, interv);

	desc.srcType = tp;

	if (tp == NULL)
	{
		RMInit::logOut << "Error: in conversion: type is null." << endl;
		throw r_Error();
	}
	
	// Initialise desc.baseType from desc.srcType
	desc.baseType=get_internal_type(tp, fullTypes);

	if (!fullTypes) {
	  switch (tp->type_id()) {
	  case r_Type::FLOAT:
	    this->applyColorScheme<float>();
	    break;
	  case r_Type::DOUBLE:
	    applyColorScheme<double>();
	    break;
	  case r_Type::USHORT:
	    applyColorScheme<unsigned short>();
	    break;
	  case r_Type::SHORT:
	    applyColorScheme<short>();
	    break;
	  }
	}
}


r_Convertor::r_Convertor(const char *src, const r_Minterval &interv, int type) throw(r_Error)
{
	initShare(src, interv);

	desc.baseType = type;
}


r_Convertor::~r_Convertor(void)
{
	// Don't delete the resulting object pointer (desc->dest) !
	//   This is the job of the external application. 
	if (params!=NULL)
	{
		 delete params;
		 params=NULL;
	}
	if (destroySrc)
	{
	  delete desc.src;
	  destroySrc=false;
	  }
}


void r_Convertor::set_storage_handler( const r_Storage_Man &newStore )
{
	mystore = newStore;
}


const r_Storage_Man& 
r_Convertor::get_storage_handler( ) const
{
	return mystore;
}



r_Type *r_Convertor::get_external_type( int ctype ) throw(r_Error)
{
	r_Type* retval=NULL;
	switch (ctype)
	{
		case ctype_bool:
		  retval=r_Type::get_any_type("boolean");
		  break;
		case ctype_char:
		case ctype_uint8:
		  retval=r_Type::get_any_type("char");
		  break;
		case ctype_int8:
		  retval=r_Type::get_any_type("octet");
		  break;
		case ctype_int16:
		  retval=r_Type::get_any_type("short");
		  break;
		case ctype_uint16:
		  retval=r_Type::get_any_type("ushort");
		  break;
		case ctype_int32:
		  retval=r_Type::get_any_type("long");
		  break;
		case ctype_uint32:
		  retval=r_Type::get_any_type("ulong");
		  break;
		case ctype_int64:
		  retval=r_Type::get_any_type("double");	// currently unsupported
		  break;
		case ctype_uint64:
		  retval=r_Type::get_any_type("double");	// currently unsupported
		  break;
		case ctype_float32:
		  retval=r_Type::get_any_type("float");
		  break;
		case ctype_float64:
		  retval=r_Type::get_any_type("double");
		  break;
		case ctype_rgb:
		  retval=r_Type::get_any_type("struct {char, char, char}");
		  break;
		default:
		  RMInit::logOut << "Error: in conversion: unsupported type " << ctype << endl;
		  r_Error err(r_Error::r_Error_General);
		  throw(err);
	}
	return retval;
}


template <class baseType>
void r_Convertor::applyColorScheme() {
  baseType *data = (baseType*)desc.src;
  baseType min=data[0], max=data[0];
  int i, size = desc.srcInterv.cell_count();
  unsigned char *t, *img = new unsigned char[size*3];
  for (i=1; i<size; ++i) {
    if (min > data[i])
      min=data[i];
    if (max < data[i])
      max=data[i];
  }
  for (i=0, t=img; i<size; ++i) {
    float n = (data[i]-min)/(max-min);
    if (n<0.5) {
      *t=(unsigned char)((0.5-n)*500); t++;
      *t=(unsigned char)(n*500); t++;
      *t=0; t++;
    } else
      {
	*t=0;t++;
	*t=(unsigned char)((1-n)*500); t++;
	*t=(unsigned char)((n-0.5)*500); t++;
      }
  }  
  destroySrc = true;
  desc.src = (char*)img;
}

r_Convertor::convert_type_e
r_Convertor::get_internal_type(const r_Type* tp, bool fullTypes) throw(r_Error) {
	r_Convertor::convert_type_e retval=ctype_void;

	if (tp == NULL)
		return retval;

	//check if tp is structure type
	if (tp->isStructType())
	{
		  // make life easy and always interpret as RGB
		  retval = ctype_rgb;
	}
	else
	{
		//is primitive type
		if (fullTypes == false)
		{
			// restricted types for ``classic'' image formats
			switch (tp->type_id())
			{
				case r_Type::BOOL:
		  			retval = ctype_bool; break;
				case r_Type::CHAR:
				case r_Type::OCTET:
					retval = ctype_char; break;
				// added (U)LONG -- PB 2005-apr-27
				case r_Type::LONG:
		  			retval = ctype_int32; break;
				case r_Type::ULONG:
		  			retval = ctype_uint32; break;
				// set to defined value (FIXME: still not good) -- PB 2005-apr-27
				default:
					RMInit::logOut << "Error: in conversion: unknown type " << tp->type_id() << ", setting to void." << endl;
		  			retval = ctype_rgb;
					//retval = ctype_char;
					
		  			break;
			}
		}
		else
		{
			// full types for more generic convertors
			switch (tp->type_id())
			{
				case r_Type::BOOL:
		//FIXME that was in hdf.cc retval = ctype_uint8; break;	  
		  			retval = ctype_bool; break;
				case r_Type::CHAR:
		  			retval = ctype_uint8; break;
				case r_Type::OCTET:
		  			retval = ctype_int8; break;
				case r_Type::SHORT:
		  			retval = ctype_int16; break;
				case r_Type::USHORT:
		  			retval = ctype_uint16; break;
				case r_Type::LONG:
		  			retval = ctype_int32; break;
				case r_Type::ULONG:
		  			retval = ctype_uint32; break;
				case r_Type::FLOAT:
		  			retval = ctype_float32; break;
				case r_Type::DOUBLE:
		  			retval = ctype_float64; break;
				default:
		  			break;
			}
		}//endif fullTypes
		if (retval == ctype_void)
		{
			RMInit::logOut << "Warning: in conversion: this type overrides base type: " << tp->type_id() << "; using char." << endl;
			retval = ctype_char;
		}    
	}//endif structuretype    
		 
	return retval;
}

std::ostream& operator<<(std::ostream& os, r_Convertor::convert_type_e& cte)
{
	switch(cte)
	{
		case r_Convertor::ctype_bool:
			os << "bool";
			break;
		case r_Convertor::ctype_char:
			os << "char";
			break;      
		case r_Convertor::ctype_uint8:
			os << "uint8";
			break;
		case r_Convertor::ctype_int8:
			os << "int8";
			break;
		case r_Convertor::ctype_int16:
			os << "int16";
			break;
		case r_Convertor::ctype_uint16:
			os << "uint16";
			break;
		case r_Convertor::ctype_int32:
			os << "int32";
			break;
		case r_Convertor::ctype_uint32:
			os << "uint32";
			break;
		case r_Convertor::ctype_int64:
			os << "int64";	// currently unsupported
			break;
		case r_Convertor::ctype_uint64:
			os << "uint64"; // currently unsupported
			break;
		case r_Convertor::ctype_float32:
			os << "float32";
			break;
		case r_Convertor::ctype_float64:
			os << "float64";
			break;
		case r_Convertor::ctype_rgb:
			os << "rgb";
			break;
		default:
			os  << "r_Convertor::convert_type_e unknown type: " << cte << endl;
			break;
	}
		  
	return os;
}

/*
 *  r_Convert_Memory class
 */

void r_Convert_Memory::initMemory( void ) throw(r_Error)
{
	int status = -1;
 
	memFS=NULL;
	handle=NULL;

	memFS = new memFSContext;
	if ( memFS != NULL)
	{
		handle = (void*)memFS;
		if (memfs_initfs(handle) >= 0) 
			status = 0;
	}
	if (status < 0)
	{
		RMInit::logOut << "Error: cannot allocate memory for conversion." << endl;
		r_Error err(MEMMORYALLOCATIONERROR);
		throw(err);
	}
}


r_Convert_Memory::r_Convert_Memory( const char *src, const r_Minterval &interv, const r_Type *tp, int fullTypes ) throw(r_Error)
: r_Convertor(src, interv, tp, fullTypes) 
{
	initMemory();
}


r_Convert_Memory::r_Convert_Memory( const char *src, const r_Minterval &interv, int type ) throw(r_Error)
: r_Convertor(src, interv, type) 
{
	initMemory();
}


r_Convert_Memory::~r_Convert_Memory( void )
{
	memfs_killfs(handle);
	if(memFS!=NULL)
	{
		 delete memFS;
		 memFS=NULL;
	}
	handle=NULL;
}