casacore
Loading...
Searching...
No Matches
ModcompConversion.h
Go to the documentation of this file.
1// # ModCompConversion.h: A class with static functions to convert ModComp format
2// # Copyright (C) 1998,1999,2001
3// # Associated Universities, Inc. Washington DC, USA.
4// #
5// # This library is free software; you can redistribute it and/or modify it
6// # under the terms of the GNU Library General Public License as published by
7// # the Free Software Foundation; either version 2 of the License, or (at your
8// # option) any later version.
9// #
10// # This library is distributed in the hope that it will be useful, but WITHOUT
11// # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12// # FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
13// # License for more details.
14// #
15// # You should have received a copy of the GNU Library General Public License
16// # along with this library; if not, write to the Free Software Foundation,
17// # Inc., 675 Massachusetts Ave, Cambridge, MA 02139, USA.
18// #
19// # Correspondence concerning AIPS++ should be addressed as follows:
20// # Internet email: casa-feedback@nrao.edu.
21// # Postal address: AIPS++ Project Office
22// # National Radio Astronomy Observatory
23// # 520 Edgemont Road
24// # Charlottesville, VA 22903-2475 USA
25
26#ifndef CASA_MODCOMPCONVERSION_H
27#define CASA_MODCOMPCONVERSION_H
28
29#include <casacore/casa/aips.h>
30#include <casacore/casa/Utilities/Assert.h>
31#include <casacore/casa/OS/CanonicalConversion.h>
32
33namespace casacore { // # NAMESPACE CASACORE - BEGIN
34
35// Define the canonical sizes of the built-in data types.
36// These are the same for all machine architectures.
37
38#define SIZE_MODCOMP_CHAR 1
39#define SIZE_MODCOMP_UCHAR 1
40#define SIZE_MODCOMP_SHORT 2
41#define SIZE_MODCOMP_USHORT 2
42#define SIZE_MODCOMP_INT 4
43#define SIZE_MODCOMP_UINT 4
44#define SIZE_MODCOMP_INT64 4
45#define SIZE_MODCOMP_UINT64 4
46#define SIZE_MODCOMP_FLOAT 4
47#define SIZE_MODCOMP_DOUBLE 8
48
49// Define for each data format if a conversion is needed from the ModComp
50// format to the local format (or vice-versa). This allows for optimizations
51// in, for example, AipsIO.
52
53// The ModComp format is ASCII for strings, a proprietary floating point format
54// and 2-complement for integers (all most significant bit first) with the
55// lengths as shown above.
56
57// Change the definitions below if new architectures (whith different lengths
58// for these integer types) are being used.
59#define CONVERT_MODCOMP_CHAR 0
60#define CONVERT_MODCOMP_UCHAR 0
61// Conversion is needed for little endian architectures (like DEC and Intel),
62// because the bytes have to be swapped (thus not for data with length 1).
63#if defined(AIPS_LITTLE_ENDIAN)
64#define CONVERT_MODCOMP_SHORT 1
65#define CONVERT_MODCOMP_USHORT 1
66#define CONVERT_MODCOMP_INT 1
67#define CONVERT_MODCOMP_UINT 1
68#define CONVERT_MODCOMP_INT64 1
69#define CONVERT_MODCOMP_UINT64 1
70#else
71// Conversion is not needed for integers if the local lengths for Shorts and
72// Ints is 2,4 respectively.
73#define CONVERT_MODCOMP_SHORT 0
74#define CONVERT_MODCOMP_USHORT 0
75#define CONVERT_MODCOMP_INT 0
76#define CONVERT_MODCOMP_UINT 0
77#define CONVERT_MODCOMP_INT64 1
78#define CONVERT_MODCOMP_UINT64 1
79#endif
80// Conversion is always needed for floating point data.
81#define CONVERT_MODCOMP_FLOAT 1
82#define CONVERT_MODCOMP_DOUBLE 1
83
84// <summary>Static functions to convert Modcomp numeric formats</summary>
85
86// <use visibility=export>
87
88// <reviewed reviewer="" date="yyyy/mm/dd" tests="tModcompConversion" demos="">
89// </reviewed>
90
91// <synopsis>
92// This class contains static toLocal functions to convert data from Modcomp
93// format to local format and vice-versa.
94//
95// The functions work on both big-endian and little-endian machines. They
96// convert between Modcomp 2-byte integers and Shorts (or uShorts) and Modcomp
97// 4-byte integers and Ints (or uInts, Int64s or uInt64s).
98//
99// It is currently not possible to convert floating point numbers from local
100// format to Modcomp. Attempting to do so will throw an exception (AipsError).
101// </synopsis>
102
103// <motivation>
104// The VLA data is stored using Modcomp numeric data formats and needs to be
105// converted to the local format to be manipulated.
106// </motivation>
107
108// <todo asof="$DATE$">
109// <li> Support conversion of floating point data to Modcomp format
110// <li> Support data type long double.
111// </todo>
112
114 public:
115 // Convert one value from Modcomp format to local format.
116 // The from and to buffer should not overlap.
117 // <group>
118 static size_t toLocal(Char& to, const void* from);
119 static size_t toLocal(uChar& to, const void* from);
120 static size_t toLocal(Short& to, const void* from);
121 static size_t toLocal(uShort& to, const void* from);
122 static size_t toLocal(Int& to, const void* from);
123 static size_t toLocal(uInt& to, const void* from);
124 static size_t toLocal(Int64& to, const void* from);
125 static size_t toLocal(uInt64& to, const void* from);
126 static size_t toLocal(Float& to, const void* from);
127 static size_t toLocal(Double& to, const void* from);
128 // </group>
129
130 // Convert nr values from Modcomp format to local format.
131 // The from and to buffer should not overlap.
132 // <group>
133 static size_t toLocal(Char* to, const void* from, size_t nr);
134 static size_t toLocal(uChar* to, const void* from, size_t nr);
135 static size_t toLocal(Short* to, const void* from, size_t nr);
136 static size_t toLocal(uShort* to, const void* from, size_t nr);
137 static size_t toLocal(Int* to, const void* from, size_t nr);
138 static size_t toLocal(uInt* to, const void* from, size_t nr);
139 static size_t toLocal(Int64* to, const void* from, size_t nr);
140 static size_t toLocal(uInt64* to, const void* from, size_t nr);
141 static size_t toLocal(Float* to, const void* from, size_t nr);
142 static size_t toLocal(Double* to, const void* from, size_t nr);
143 // </group>
144
145 // Convert one value from local format to Modcomp format. The from and to
146 // buffer should not overlap. The floating point functions will throw
147 // exceptions as they are not implemented yet.
148 // <group>
149 static size_t fromLocal(void* to, Char from);
150 static size_t fromLocal(void* to, uChar from);
151 static size_t fromLocal(void* to, Short from);
152 static size_t fromLocal(void* to, uShort from);
153 static size_t fromLocal(void* to, Int from);
154 static size_t fromLocal(void* to, uInt from);
155 static size_t fromLocal(void* to, Int64 from);
156 static size_t fromLocal(void* to, uInt64 from);
157 static size_t fromLocal(void* to, Float from);
158 static size_t fromLocal(void* to, Double from);
159 // </group>
160
161 // Convert nr values from local format to Modcomp format. The from and to
162 // buffer should not overlap. The floating point functions will throw
163 // exceptions as they are not implemented yet.
164 // <group>
165 static size_t fromLocal(void* to, const Char* from, size_t nr);
166 static size_t fromLocal(void* to, const uChar* from, size_t nr);
167 static size_t fromLocal(void* to, const Short* from, size_t nr);
168 static size_t fromLocal(void* to, const uShort* from, size_t nr);
169 static size_t fromLocal(void* to, const Int* from, size_t nr);
170 static size_t fromLocal(void* to, const uInt* from, size_t nr);
171 static size_t fromLocal(void* to, const Int64* from, size_t nr);
172 static size_t fromLocal(void* to, const uInt64* from, size_t nr);
173 static size_t fromLocal(void* to, const Float* from, size_t nr);
174 static size_t fromLocal(void* to, const Double* from, size_t nr);
175 // </group>
176
177 private:
178 // This class should not be constructed
179 // (so declare the constructor private).
181};
182
183inline size_t ModcompConversion::toLocal(Char& to, const void* from) {
184 return CanonicalConversion::toLocal(to, from);
185}
186
187inline size_t ModcompConversion::toLocal(uChar& to, const void* from) {
188 return CanonicalConversion::toLocal(to, from);
189}
190
191inline size_t ModcompConversion::toLocal(Short& to, const void* from) {
192 return CanonicalConversion::toLocal(to, from);
193}
194
195inline size_t ModcompConversion::toLocal(uShort& to, const void* from) {
196 return CanonicalConversion::toLocal(to, from);
197}
198
199inline size_t ModcompConversion::toLocal(Int& to, const void* from) {
200 return CanonicalConversion::toLocal(to, from);
201}
202
203inline size_t ModcompConversion::toLocal(uInt& to, const void* from) {
204 return CanonicalConversion::toLocal(to, from);
205}
206
207inline size_t ModcompConversion::toLocal(Int64& to, const void* from) {
208 Int tmp;
209 size_t res = toLocal(tmp, from);
210 to = tmp;
211 return res;
212}
213
214inline size_t ModcompConversion::toLocal(uInt64& to, const void* from) {
215 uInt tmp;
216 size_t res = toLocal(tmp, from);
217 to = tmp;
218 return res;
219}
220
221inline size_t ModcompConversion::toLocal(Float& to, const void* from) {
222 return ModcompConversion::toLocal(&to, from, 1u);
223}
224
225inline size_t ModcompConversion::toLocal(Double& to, const void* from) {
226 return ModcompConversion::toLocal(&to, from, 1u);
227}
228
229inline size_t ModcompConversion::toLocal(Char* to, const void* from, size_t nr) {
230 return CanonicalConversion::toLocalChar(to, from, nr);
231}
232
233inline size_t ModcompConversion::toLocal(uChar* to, const void* from, size_t nr) {
234 return CanonicalConversion::toLocalUChar(to, from, nr);
235}
236
237inline size_t ModcompConversion::toLocal(Short* to, const void* from, size_t nr) {
238 return CanonicalConversion::toLocalShort(to, from, nr);
239}
240
241inline size_t ModcompConversion::toLocal(uShort* to, const void* from, size_t nr) {
242 return CanonicalConversion::toLocalUShort(to, from, nr);
243}
244
245inline size_t ModcompConversion::toLocal(Int* to, const void* from, size_t nr) {
246 return CanonicalConversion::toLocalInt(to, from, nr);
247}
248
249inline size_t ModcompConversion::toLocal(uInt* to, const void* from, size_t nr) {
250 return CanonicalConversion::toLocalUInt(to, from, nr);
251}
252
253inline size_t ModcompConversion::fromLocal(void* to, Char from) {
254 return CanonicalConversion::fromLocal(to, from);
255}
256
257inline size_t ModcompConversion::fromLocal(void* to, uChar from) {
258 return CanonicalConversion::fromLocal(to, from);
259}
260
261inline size_t ModcompConversion::fromLocal(void* to, Short from) {
262 return CanonicalConversion::fromLocal(to, from);
263}
264
265inline size_t ModcompConversion::fromLocal(void* to, uShort from) {
266 return CanonicalConversion::fromLocal(to, from);
267}
268
269inline size_t ModcompConversion::fromLocal(void* to, Int from) {
270 return CanonicalConversion::fromLocal(to, from);
271}
272
273inline size_t ModcompConversion::fromLocal(void* to, uInt from) {
274 return CanonicalConversion::fromLocal(to, from);
275}
276
277inline size_t ModcompConversion::fromLocal(void* to, Int64 from) {
278 return CanonicalConversion::fromLocal(to, (Int)from);
279}
280
281inline size_t ModcompConversion::fromLocal(void* to, uInt64 from) {
282 return CanonicalConversion::fromLocal(to, (uInt)from);
283}
284
285inline size_t ModcompConversion::fromLocal(void* to, Float from) {
286 return ModcompConversion::fromLocal(to, &from, 1u);
287}
288
289inline size_t ModcompConversion::fromLocal(void* to, Double from) {
290 return ModcompConversion::fromLocal(to, &from, 1u);
291}
292
293inline size_t ModcompConversion::fromLocal(void* to, const Char* from, size_t nr) {
294 return CanonicalConversion::fromLocalChar(to, from, nr);
295}
296
297inline size_t ModcompConversion::fromLocal(void* to, const uChar* from, size_t nr) {
298 return CanonicalConversion::fromLocalUChar(to, from, nr);
299}
300
301inline size_t ModcompConversion::fromLocal(void* to, const Short* from, size_t nr) {
302 return CanonicalConversion::fromLocalShort(to, from, nr);
303}
304
305inline size_t ModcompConversion::fromLocal(void* to, const uShort* from, size_t nr) {
306 return CanonicalConversion::fromLocalUShort(to, from, nr);
307}
308
309inline size_t ModcompConversion::fromLocal(void* to, const Int* from, size_t nr) {
310 return CanonicalConversion::fromLocalInt(to, from, nr);
311}
312
313inline size_t ModcompConversion::fromLocal(void* to, const uInt* from, size_t nr) {
314 return CanonicalConversion::fromLocalUInt(to, from, nr);
315}
316
317} // namespace casacore
318
319#endif
static size_t toLocalChar(void *to, const void *from, size_t nr)
Convert nr values from canonical format to local format.
static size_t fromLocalChar(void *to, const void *from, size_t nr)
Convert nr values from local format to canonical format.
static size_t fromLocalUShort(void *to, const void *from, size_t nr)
static size_t fromLocalUChar(void *to, const void *from, size_t nr)
static size_t toLocalInt(void *to, const void *from, size_t nr)
static size_t fromLocal(void *to, const char &from)
Convert one value from local format to canonical format.
static size_t toLocal(char &to, const void *from)
Convert one value from canonical format to local format.
static size_t toLocalUChar(void *to, const void *from, size_t nr)
static size_t fromLocalShort(void *to, const void *from, size_t nr)
static size_t toLocalUInt(void *to, const void *from, size_t nr)
static size_t toLocalUShort(void *to, const void *from, size_t nr)
static size_t fromLocalInt(void *to, const void *from, size_t nr)
static size_t toLocalShort(void *to, const void *from, size_t nr)
static size_t fromLocalUInt(void *to, const void *from, size_t nr)
static size_t fromLocal(void *to, Char from)
Convert one value from local format to Modcomp format.
static size_t toLocal(Char &to, const void *from)
Convert one value from Modcomp format to local format.
static size_t fromLocal(void *to, const Int64 *from, size_t nr)
static size_t toLocal(Float *to, const void *from, size_t nr)
static size_t fromLocal(void *to, const Double *from, size_t nr)
ModcompConversion()
This class should not be constructed (so declare the constructor private).
static size_t toLocal(uInt64 *to, const void *from, size_t nr)
static size_t toLocal(Int64 *to, const void *from, size_t nr)
static size_t toLocal(Double *to, const void *from, size_t nr)
static size_t fromLocal(void *to, const Float *from, size_t nr)
static size_t fromLocal(void *to, const uInt64 *from, size_t nr)
For temporary backward namespace compatibility, use casa as alias for casacore.
Definition mainpage.dox:28
unsigned char uChar
Definition aipstype.h:45
short Short
Definition aipstype.h:46
unsigned int uInt
Definition aipstype.h:49
unsigned short uShort
Definition aipstype.h:47
long long Int64
Define the extra non-standard types used by Casacore (like proposed uSize, Size).
Definition aipsxtype.h:36
float Float
Definition aipstype.h:52
int Int
Definition aipstype.h:48
double Double
Definition aipstype.h:53
char Char
Definition aipstype.h:44
unsigned long long uInt64
Definition aipsxtype.h:37