1 : /******************************************************************************
2 : *
3 : * Purpose: Various public (documented) utility functions.
4 : *
5 : ******************************************************************************
6 : * Copyright (c) 2009
7 : * PCI Geomatics, 50 West Wilmot Street, Richmond Hill, Ont, Canada
8 : *
9 : * Permission is hereby granted, free of charge, to any person obtaining a
10 : * copy of this software and associated documentation files (the "Software"),
11 : * to deal in the Software without restriction, including without limitation
12 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13 : * and/or sell copies of the Software, and to permit persons to whom the
14 : * Software is furnished to do so, subject to the following conditions:
15 : *
16 : * The above copyright notice and this permission notice shall be included
17 : * in all copies or substantial portions of the Software.
18 : *
19 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 : * DEALINGS IN THE SOFTWARE.
26 : ****************************************************************************/
27 : #include "pcidsk_config.h"
28 : #include "pcidsk_types.h"
29 : #include "pcidsk_exception.h"
30 : #include "core/pcidsk_utils.h"
31 : #include <cstdlib>
32 : #include <cstring>
33 :
34 : using namespace PCIDSK;
35 :
36 : /************************************************************************/
37 : /* DataTypeSize() */
38 : /************************************************************************/
39 :
40 : /**
41 : * Return size of data type.
42 : *
43 : * Note that type CHN_BIT exists to represent one bit backed data from
44 : * bitmap segments, but because the return of this functions is measured
45 : * in bytes, the size of a CHN_BIT pixel cannot be properly returned (one
46 : * eighth of a byte), so "1" is returned instead.
47 : *
48 : * @param chan_type the channel type enumeration value.
49 : *
50 : * @return the size of the passed data type in bytes, or zero for unknown
51 : * values.
52 : */
53 :
54 1421 : int PCIDSK::DataTypeSize( eChanType chan_type )
55 :
56 : {
57 1421 : switch( chan_type )
58 : {
59 : case CHN_8U:
60 543 : return 1;
61 : case CHN_16S:
62 102 : return 2;
63 : case CHN_16U:
64 426 : return 2;
65 : case CHN_32R:
66 102 : return 4;
67 : case CHN_C16U:
68 44 : return 4;
69 : case CHN_C16S:
70 102 : return 4;
71 : case CHN_C32R:
72 102 : return 8;
73 : case CHN_BIT:
74 0 : return 1; // not really accurate!
75 : default:
76 0 : return 0;
77 : }
78 : }
79 :
80 : /************************************************************************/
81 : /* DataTypeName() */
82 : /************************************************************************/
83 :
84 : /**
85 : * Return name for the data type.
86 : *
87 : * The returned values are suitable for display to people, and matches
88 : * the portion of the name after the underscore (ie. "8U" for CHN_8U.
89 : *
90 : * @param chan_type the channel type enumeration value to be translated.
91 : *
92 : * @return a string representing the data type.
93 : */
94 :
95 85 : std::string PCIDSK::DataTypeName( eChanType chan_type )
96 :
97 : {
98 85 : switch( chan_type )
99 : {
100 : case CHN_8U:
101 57 : return "8U";
102 : case CHN_16S:
103 5 : return "16S";
104 : case CHN_16U:
105 8 : return "16U";
106 : case CHN_32R:
107 5 : return "32R";
108 : case CHN_C16U:
109 0 : return "C16U";
110 : case CHN_C16S:
111 5 : return "C16S";
112 : case CHN_C32R:
113 5 : return "C32R";
114 : case CHN_BIT:
115 0 : return "BIT";
116 : default:
117 0 : return "UNK";
118 : }
119 : }
120 :
121 : /************************************************************************/
122 : /* GetDataTypeFromName() */
123 : /************************************************************************/
124 :
125 : /**
126 : * @brief Return the segment type code based on the contents of type_name
127 : *
128 : * @param the type name, as a string
129 : *
130 : * @return the channel type code
131 : */
132 242 : eChanType PCIDSK::GetDataTypeFromName(std::string const& type_name)
133 : {
134 242 : if (type_name.find("8U") != std::string::npos) {
135 160 : return CHN_8U;
136 82 : } else if (type_name.find("C16U") != std::string::npos) {
137 0 : return CHN_C16U;
138 82 : } else if (type_name.find("C16S") != std::string::npos) {
139 14 : return CHN_C16S;
140 68 : } else if (type_name.find("C32R") != std::string::npos) {
141 14 : return CHN_C32R;
142 54 : } else if (type_name.find("16U") != std::string::npos) {
143 26 : return CHN_16U;
144 28 : } else if (type_name.find("16S") != std::string::npos) {
145 14 : return CHN_16S;
146 14 : } else if (type_name.find("32R") != std::string::npos) {
147 14 : return CHN_32R;
148 0 : } else if (type_name.find("BIT") != std::string::npos) {
149 0 : return CHN_BIT;
150 : } else {
151 0 : return CHN_UNKNOWN;
152 : }
153 : }
154 :
155 : /************************************************************************/
156 : /* IsDataTypeComplex() */
157 : /************************************************************************/
158 :
159 : /**
160 : * @brief Return whether or not the data type is complex
161 : *
162 : * @param the type
163 : *
164 : * @return true if the data type is complex, false otherwise
165 : */
166 0 : bool PCIDSK::IsDataTypeComplex(eChanType type)
167 : {
168 0 : switch(type)
169 : {
170 : case CHN_C32R:
171 : case CHN_C16U:
172 : case CHN_C16S:
173 0 : return true;
174 : default:
175 0 : return false;
176 : }
177 : }
178 :
179 : /************************************************************************/
180 : /* SegmentTypeName() */
181 : /************************************************************************/
182 :
183 : /**
184 : * Return name for segment type.
185 : *
186 : * Returns a short name for the segment type code passed in. This is normally
187 : * the portion of the enumeration name that comes after the underscore - ie.
188 : * "BIT" for SEG_BIT.
189 : *
190 : * @param type the segment type code.
191 : *
192 : * @return the string for the segment type.
193 : */
194 :
195 121 : std::string PCIDSK::SegmentTypeName( eSegType type )
196 :
197 : {
198 121 : switch( type )
199 : {
200 : case SEG_BIT:
201 0 : return "BIT";
202 : case SEG_VEC:
203 0 : return "VEC";
204 : case SEG_SIG:
205 0 : return "SIG";
206 : case SEG_TEX:
207 0 : return "TEX";
208 : case SEG_GEO:
209 88 : return "GEO";
210 : case SEG_ORB:
211 0 : return "ORB";
212 : case SEG_LUT:
213 0 : return "LUT";
214 : case SEG_PCT:
215 2 : return "PCT";
216 : case SEG_BLUT:
217 0 : return "BLUT";
218 : case SEG_BPCT:
219 0 : return "BPCT";
220 : case SEG_BIN:
221 0 : return "BIN";
222 : case SEG_ARR:
223 0 : return "ARR";
224 : case SEG_SYS:
225 31 : return "SYS";
226 : case SEG_GCPOLD:
227 0 : return "GCPOLD";
228 : case SEG_GCP2:
229 0 : return "GCP2";
230 : default:
231 0 : return "UNKNOWN";
232 : }
233 : }
234 :
|