1 /*
2  * Medical Image Registration ToolKit (MIRTK)
3  *
4  * Copyright 2008-2015 Imperial College London
5  * Copyright 2008-2013 Daniel Rueckert, Julia Schnabel
6  * Copyright 2013-2015 Andreas Schuh
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #include "mirtk/Voxel.h"
22 
23 
24 namespace mirtk {
25 
26 
27 // -----------------------------------------------------------------------------
ToString(const ImageDataType & value,int w,char c,bool left)28 template <> string ToString(const ImageDataType &value, int w, char c, bool left)
29 {
30   const char *str = "unknown";
31   switch (value) {
32     case MIRTK_VOXEL_CHAR:           str = "char"; break;
33     case MIRTK_VOXEL_UNSIGNED_CHAR:  str = "uchar"; break;
34     case MIRTK_VOXEL_SHORT:          str = "short"; break;
35     case MIRTK_VOXEL_UNSIGNED_SHORT: str = "ushort"; break;
36     case MIRTK_VOXEL_INT:            str = "int"; break;
37     case MIRTK_VOXEL_UNSIGNED_INT:   str = "uint"; break;
38     case MIRTK_VOXEL_FLOAT:          str = "float"; break;
39     case MIRTK_VOXEL_DOUBLE:         str = "double"; break;
40     case MIRTK_VOXEL_RGB:            str = "rgb"; break;
41     case MIRTK_VOXEL_FLOAT1:         str = "float1"; break;
42     case MIRTK_VOXEL_FLOAT2:         str = "float2"; break;
43     case MIRTK_VOXEL_FLOAT3:         str = "float3"; break;
44     case MIRTK_VOXEL_FLOAT4:         str = "float4"; break;
45     case MIRTK_VOXEL_FLOAT9:         str = "float9"; break;
46     case MIRTK_VOXEL_FLOAT1x1:       str = "float1x1"; break;
47     case MIRTK_VOXEL_FLOAT2x2:       str = "float2x2"; break;
48     case MIRTK_VOXEL_FLOAT3x3:       str = "float3x3"; break;
49     case MIRTK_VOXEL_FLOAT3x4:       str = "float3x4"; break;
50     case MIRTK_VOXEL_FLOAT4x4:       str = "float4x4"; break;
51     case MIRTK_VOXEL_DOUBLE1:        str = "double1"; break;
52     case MIRTK_VOXEL_DOUBLE2:        str = "double2"; break;
53     case MIRTK_VOXEL_DOUBLE3:        str = "double3"; break;
54     case MIRTK_VOXEL_DOUBLE4:        str = "double4"; break;
55     case MIRTK_VOXEL_DOUBLE9:        str = "double9"; break;
56     case MIRTK_VOXEL_DOUBLE1x1:      str = "double1x1"; break;
57     case MIRTK_VOXEL_DOUBLE2x2:      str = "double2x2"; break;
58     case MIRTK_VOXEL_DOUBLE3x3:      str = "double3x3"; break;
59     case MIRTK_VOXEL_DOUBLE3x4:      str = "double3x4"; break;
60     case MIRTK_VOXEL_DOUBLE4x4:      str = "double4x4"; break;
61     default:                         str = "unknown"; break;
62   }
63   return ToString(str, w, c, left);
64 }
65 
66 // -----------------------------------------------------------------------------
FromString(const char * str,ImageDataType & value)67 template <> bool FromString(const char *str, ImageDataType &value)
68 {
69   const string lstr = ToLower(str);
70   value = MIRTK_VOXEL_UNKNOWN;
71 
72   if      (lstr == "binary")  value = MIRTK_VOXEL_BINARY;
73   else if (lstr == "grey")    value = MIRTK_VOXEL_GREY;
74   else if (lstr == "real")    value = MIRTK_VOXEL_REAL;
75   else if (lstr == "real1")   value = MIRTK_VOXEL_REAL1;
76   else if (lstr == "real2")   value = MIRTK_VOXEL_REAL2;
77   else if (lstr == "real3")   value = MIRTK_VOXEL_REAL3;
78   else if (lstr == "real4")   value = MIRTK_VOXEL_REAL4;
79   else if (lstr == "real9")   value = MIRTK_VOXEL_REAL9;
80   else if (lstr == "real2x2") value = MIRTK_VOXEL_REAL2x2;
81   else if (lstr == "real3x3") value = MIRTK_VOXEL_REAL3x3;
82   else if (lstr == "real3x4") value = MIRTK_VOXEL_REAL3x4;
83   else if (lstr == "real4x4") value = MIRTK_VOXEL_REAL4x4;
84 
85   if (value == MIRTK_VOXEL_UNKNOWN) {
86     value = static_cast<ImageDataType>(MIRKT_VOXEL_LAST - 1);
87     while (value != MIRTK_VOXEL_UNKNOWN) {
88       if (ToString(value) == lstr) break;
89       value = static_cast<ImageDataType>(value - 1);
90     }
91   }
92 
93   return value != MIRTK_VOXEL_UNKNOWN;
94 }
95 
96 // -----------------------------------------------------------------------------
DataTypeSize(int type)97 int DataTypeSize(int type)
98 {
99   switch (type){
100     case MIRTK_VOXEL_CHAR:           return sizeof(char);
101     case MIRTK_VOXEL_UNSIGNED_CHAR:  return sizeof(unsigned char);
102     case MIRTK_VOXEL_SHORT:          return sizeof(short);
103     case MIRTK_VOXEL_UNSIGNED_SHORT: return sizeof(unsigned short);
104     case MIRTK_VOXEL_INT:            return sizeof(int);
105     case MIRTK_VOXEL_UNSIGNED_INT:   return sizeof(unsigned int);
106     case MIRTK_VOXEL_FLOAT:          return sizeof(float);
107     case MIRTK_VOXEL_DOUBLE:         return sizeof(double);
108     case MIRTK_VOXEL_FLOAT1:         return sizeof(float1);
109     case MIRTK_VOXEL_FLOAT2:         return sizeof(float2);
110     case MIRTK_VOXEL_FLOAT3:         return sizeof(float3);
111     case MIRTK_VOXEL_FLOAT4:         return sizeof(float4);
112     case MIRTK_VOXEL_FLOAT9:         return sizeof(Float9);
113     case MIRTK_VOXEL_FLOAT2x2:       return sizeof(float2x2);
114     case MIRTK_VOXEL_FLOAT3x3:       return sizeof(float3x3);
115     case MIRTK_VOXEL_FLOAT3x4:       return sizeof(float3x4);
116     case MIRTK_VOXEL_FLOAT4x4:       return sizeof(float4x4);
117     case MIRTK_VOXEL_DOUBLE1:        return sizeof(double1);
118     case MIRTK_VOXEL_DOUBLE2:        return sizeof(double2);
119     case MIRTK_VOXEL_DOUBLE3:        return sizeof(double3);
120     case MIRTK_VOXEL_DOUBLE4:        return sizeof(double4);
121     case MIRTK_VOXEL_DOUBLE9:        return sizeof(Double9);
122     case MIRTK_VOXEL_DOUBLE2x2:      return sizeof(double2x2);
123     case MIRTK_VOXEL_DOUBLE3x3:      return sizeof(double3x3);
124     case MIRTK_VOXEL_DOUBLE3x4:      return sizeof(double3x4);
125     case MIRTK_VOXEL_DOUBLE4x4:      return sizeof(double4x4);
126     default:                         return 0;
127   }
128 }
129 
130 
131 } // namespace mirtk
132