| Bsoft 1.9 Bernard's software package

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
datatypes.cpp
Go to the documentation of this file.
1
9 #include "rwimg.h"
10 #include "datatypes.h"
11 #include "utilities.h"
12
13 // Declaration of global variables
14 extern int verbose; // Level of output to the screen
15
25 unsigned long gettypesize(DataType type)
26 {
27 unsigned long size;
28
29 switch ( type ) {
30 case Bit:
31 case UCharacter: case SCharacter: size = sizeof(char); break;
32 case UShort: case Short: size = sizeof(short); break;
33 case UInteger: case Integer: size = sizeof(int); break;
34 case ULong: case Long: size = sizeof(long); break;
35 case Float: size = sizeof(float); break;
36 case Double: size = sizeof(double); break;
37 default: size = 0;
38 }
39
40 return size;
41 }
42
52 DataType getdatatype(char letter)
53 {
54 DataType type;
55
56 switch ( letter ) {
57 case '1': type = Bit; break;
58 case 'b': type = UCharacter; break;
59 case 'c': type = SCharacter; break;
60 case 'u': type = UShort; break;
61 case 's': type = Short; break;
62 case 'j': type = UInteger; break;
63 case 'i': type = Integer; break;
64 case 'k': type = ULong; break;
65 case 'l': type = Long; break;
66 case 'f': type = Float; break;
67 case 'd': type = Double; break;
68 case 'S': type = Short; break;
69 case 'I': type = Integer; break;
70 case 'F': type = Float; break;
71 case 'D': type = Double; break;
72 default: type = Unknown_Type; break;
73 }
74
75 return type;
76 }
77
89 int set_value_with_datatype(char* ptr, double value, DataType datatype)
90 {
91 int typesize = gettypesize(datatype);
92 if ( typesize < 1 ) return -1;
93
94 switch ( datatype ) {
95 case UCharacter: *ptr = (unsigned char) value; break;
96 case SCharacter: {
97 signed char cval = (signed char) value;
98 memcpy(ptr, &cval, typesize); break;
99 }
100 case UShort: {
101 unsigned short uval = (unsigned short) value;
102 memcpy(ptr, &uval, typesize); break;
103 }
104 case Short: {
105 short sval = (short) value;
106 memcpy(ptr, &sval, typesize); break;
107 }
108 case UInteger: {
109 unsigned int ival = (unsigned int) value;
110 memcpy(ptr, &ival, typesize); break;
111 }
112 case Integer: {
113 int ival = (int) value;
114 memcpy(ptr, &ival, typesize); break;
115 }
116 case ULong: {
117 unsigned long ival = (unsigned long) value;
118 memcpy(ptr, &ival, typesize); break;
119 }
120 case Long: {
121 long ival = (long) value;
122 memcpy(ptr, &ival, typesize); break;
123 }
124 case Float: {
125 float fval = (float) value;
126 memcpy(ptr, &fval, typesize); break;
127 }
128 case Double: {
129 double dval = (double) value;
130 memcpy(ptr, &dval, typesize); break;
131 }
132 default: ;
133 }
134
135 return 0;
136 }
137
ULong
Definition: Bimage.h:58
UShort
Definition: Bimage.h:54
utilities.h
Header file for general utilities.
getdatatype
DataType getdatatype(char letter)
Get the data type indicated by a single letter code.
Definition: datatypes.cpp:52
verbose
int verbose
Float
Definition: Bimage.h:60
set_value_with_datatype
int set_value_with_datatype(char *ptr, double value, DataType datatype)
Set a pointer to a value with a given data type.
Definition: datatypes.cpp:89
datatypes.h
Functions to check and convert datatypes.
Bit
Definition: Bimage.h:51
UCharacter
Definition: Bimage.h:52
DataType
DataType
Base data type specifier.
Definition: Bimage.h:49
SCharacter
Definition: Bimage.h:53
UInteger
Definition: Bimage.h:56
Unknown_Type
Definition: Bimage.h:50
Integer
Definition: Bimage.h:57
Short
Definition: Bimage.h:55
gettypesize
unsigned long gettypesize(DataType type)
Get the size of a datatype.
Definition: datatypes.cpp:25
rwimg.h
Header file for 2D and 3D image I/O.
Double
Definition: Bimage.h:61
Long
Definition: Bimage.h:59