summaryrefslogtreecommitdiffstats
path: root/conversion/tor.cc
blob: 0663fd4529e0e90623f50e22c64526a55e87fcef (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
/*
* 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>.
*/

#include "conversion/tor.hh"
#include "raslib/endian.hh"
#include "raslib/rminit.hh"
#include "raslib/parseparams.hh"
#include "raslib/primitivetype.hh"

void r_Conv_TOR::initTOR()
{
 if(params ==NULL)
 {
  params = new r_Parse_Params(3); 
 }
}

r_Conv_TOR::r_Conv_TOR(const char* source, const r_Minterval& lengthordomain, const r_Type* tp) throw(r_Error)
	:	r_Convertor(source, lengthordomain, tp, true)
	{
          initTOR();  
	}

r_Conv_TOR::r_Conv_TOR(const char* source, const r_Minterval& lengthordomain, int tp) throw(r_Error)
	:	r_Convertor(source, lengthordomain, tp)
	{
 	  initTOR();
	}

r_convDesc&
r_Conv_TOR::convertFrom(const char* options) throw (r_Error)
	{
	int swap = 0;
	int rescale = 0;
	char* domain = NULL;
	params->add("swapendianness", &swap, r_Parse_Params::param_type_int);
	params->add("rescale", &rescale, r_Parse_Params::param_type_int);
	params->add("domain", &domain, r_Parse_Params::param_type_string);
	params->process(options);
	r_Minterval t;
	RMInit::logOut  << "r_Conv_TOR::convert swap " << swap << " rescale " << rescale << " domain " << (domain?domain:"NULL") << std::endl;
	if (domain == NULL)
		{
		RMInit::logOut << "r_Conv_TOR::convertFrom no domain specified in options string" << std::endl;
		throw r_Error();
		}
	try	{
		t = r_Minterval(domain);
		}
	catch (r_Eno_interval& e)
		{
		RMInit::logOut << "r_Conv_TOR::convertFrom no correct domain specified in options string" << std::endl;
		throw r_Error();
		}
	if (t.dimension() != 2)
		{
		RMInit::logOut << "r_Conv_TOR::convertFrom domain in options string must have 2 dimensions" << std::endl;
		throw r_Error();
		}
	r_Range x = t[0].high() - t[0].low() + 1;
	r_Range y = t[1].high() - t[1].low() + 1;
	r_UShort* array = (r_UShort*)mystore.storage_alloc(x * y * sizeof(r_UShort));
	double max = 0;
	double min = 0;
	r_Char tc;
	double maxu = 0;
	double minu = 0;
	r_UShort tu;
	switch (desc.baseType)
		{
		case ctype_bool:
		case ctype_char:
		case ctype_uint8:
			if (desc.srcInterv[0].high() - desc.srcInterv[0].low() + 1 != t.cell_count() * sizeof(r_Char))
				{
				RMInit::logOut << "r_Conv_TOR::convertFrom the supplied data is " << desc.srcInterv[0].high() - desc.srcInterv[0].low() + 1 << " bytes, but " << t.cell_count() << " bytes were expected" << std::endl;
				throw r_Error();
				}
			get_limits(&tc, min, max);
			get_limits(&tu, minu, maxu);
			for(int i=0; i < x; i++)
				{
				for(int j=0; j < y; j++)
					{
					if (rescale)
						array[i * x + j] = ((r_Octet*)desc.src)[j * x + i] / max * maxu;
					else
						array[i * x + j] = ((r_Octet*)desc.src)[j * x + i];
					}
				}
			break;
		case ctype_uint16:
			if (desc.srcInterv[0].high() - desc.srcInterv[0].low() + 1 != t.cell_count() * sizeof(r_UShort))
				{
				RMInit::logOut << "r_Conv_TOR::convertFrom the supplied data is " << desc.srcInterv[0].high() - desc.srcInterv[0].low() + 1 << " bytes, but " << t.cell_count() << " bytes were expected" << std::endl;
				throw r_Error();
				}
			for(int i=0; i < x; i++)
				{
				for(int j=0; j < y; j++)
					{
					array[i * x + j] = ((r_UShort*)desc.src)[j * x + i];
					}
				}
			break;
		default:
			RMInit::logOut << "r_Conv_TOR unknown base type!" << std::endl;
			throw r_Error();
		}
	if (swap)
		{
		r_UShort* temp = (r_UShort*)mystore.storage_alloc(x * y * sizeof(r_UShort));
		r_Endian::swap_array(x * y * sizeof(r_UShort), array, temp);
		mystore.storage_free(array);
		array = temp;
		}
	
	desc.dest = (char*)array;
	desc.destInterv = t;
	desc.destType = new r_Primitive_Type("UShort", r_Type::USHORT);
	return desc;
	}

r_convDesc&
r_Conv_TOR::convertTo(const char* options) throw (r_Error)
	{
	int swap = 0;
	int rescale = 0;
	params->add("swapendianness", &swap, r_Parse_Params::param_type_int);
	params->add("rescale", &rescale, r_Parse_Params::param_type_int);
	params->process(options);
	if (desc.srcInterv.dimension() != 2)
		{
		RMInit::logOut << "r_Conv_TOR::convertFrom domain in options string must have 2 dimensions" << std::endl;
		throw r_Error();
		}
	r_Range x = desc.srcInterv[0].high() - desc.srcInterv[0].low() + 1;
	r_Range y = desc.srcInterv[1].high() - desc.srcInterv[1].low() + 1;
	r_UShort* array = (r_UShort*)mystore.storage_alloc(x * y * sizeof(r_UShort));
	double max = 0;
	double min = 0;
	r_Char tc;
	double maxu = 0;
	double minu = 0;
	r_UShort tu;
	switch (desc.baseType)
		{
		case ctype_bool:
		case ctype_char:
		case ctype_uint8:
			get_limits(&tc, min, max);
			get_limits(&tu, minu, maxu);
			for(int i=0; i < x; i++)
				{
				for(int j=0; j < y; j++)
					{
					if (rescale)
						array[i * x + j] = ((r_Octet*)desc.src)[j * x + i] / max * maxu;
					else
						array[i * x + j] = ((r_Octet*)desc.src)[j * x + i];
					}
				}
			break;
		case ctype_uint16:
			for(int i=0; i < x; i++)
				{
				for(int j=0; j < y; j++)
					{
					array[i * x + j] = ((r_UShort*)desc.src)[j * x + i];
					}
				}
			break;
		default:
			RMInit::logOut << "r_Conv_TOR unknown base type!" << std::endl;
			throw r_Error();
		}
	if (swap)
		{
		r_UShort* temp =(r_UShort*)mystore.storage_alloc(x * y * sizeof(r_UShort)); 
		r_Endian::swap_array(x * y * sizeof(r_UShort), array, temp);
		mystore.storage_free(array);
		array = temp;
		}
	
	desc.dest = (char*)array;
	r_Minterval td(1);
	td << r_Sinterval((r_Range)0, (r_Range)(desc.srcInterv.cell_count() * sizeof(r_UShort) - 1));
	desc.destInterv = td;
	desc.destType = new r_Primitive_Type("Char", r_Type::CHAR);
	return desc;
	}

const char*
r_Conv_TOR::get_name() const
	{
	return get_name_from_data_format(r_TOR);
	}

r_Data_Format
r_Conv_TOR::get_data_format() const
	{
	return r_TOR;
	}

r_Convertor* 
r_Conv_TOR::clone() const
	{
	return new r_Conv_TOR(desc.src, desc.srcInterv, desc.srcType);
	}