6869
|
1 /* |
|
2 |
|
3 Copyright (C) 2007 David Bateman |
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
7016
|
9 Free Software Foundation; either version 3 of the License, or (at your |
|
10 option) any later version. |
6869
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
7016
|
18 along with Octave; see the file COPYING. If not, see |
|
19 <http://www.gnu.org/licenses/>. |
6869
|
20 |
|
21 */ |
|
22 |
|
23 #ifdef HAVE_CONFIG_H |
|
24 #include <config.h> |
|
25 #endif |
|
26 |
6948
|
27 #include <algorithm> |
6869
|
28 #include <string> |
|
29 |
|
30 #include "oct.h" |
|
31 |
|
32 template <class LT, class RT> |
7229
|
33 /* static */ void |
6869
|
34 typecast (const Array<RT>& x, Array<LT>& y) |
|
35 { |
|
36 octave_idx_type n = x.length (); |
|
37 size_t ns = sizeof (RT); |
|
38 size_t ms = sizeof (LT); |
|
39 |
|
40 if (n * ns % ms != 0) |
7229
|
41 error ("typecast: incorrect number of input values to make output value"); |
6869
|
42 else |
|
43 { |
|
44 octave_idx_type m = n * ns / ms; |
|
45 dim_vector dv = x.dims (); |
|
46 for (octave_idx_type i = 0; i < dv.length(); i++) |
|
47 if (dv(i) == n) |
|
48 { |
|
49 dv(i) = m; |
|
50 break; |
|
51 } |
|
52 y.resize (dv); |
7229
|
53 const unsigned char *xp |
|
54 = reinterpret_cast<const unsigned char *> (x.fortran_vec ()); |
6869
|
55 unsigned char *yp = reinterpret_cast<unsigned char *>(y.fortran_vec ()); |
|
56 for (octave_idx_type i = 0; |
|
57 i < n * static_cast<octave_idx_type>(ns); i++) |
|
58 *yp++ = *xp++; |
|
59 } |
|
60 } |
|
61 |
|
62 template <class T> |
|
63 static octave_value |
|
64 typecast (const T& x, std::string type) |
|
65 { |
|
66 octave_value retval; |
|
67 if (type == "uint8") |
|
68 { |
|
69 uint8NDArray y; |
|
70 typecast (x, y); |
|
71 retval = octave_value (y); |
|
72 } |
|
73 else if (type == "uint16") |
|
74 { |
|
75 uint16NDArray y; |
|
76 typecast (x, y); |
|
77 retval = octave_value (y); |
|
78 } |
|
79 else if (type == "uint32") |
|
80 { |
|
81 uint32NDArray y; |
|
82 typecast (x, y); |
|
83 retval = octave_value (y); |
|
84 } |
|
85 else if (type == "uint64") |
|
86 { |
|
87 uint64NDArray y; |
|
88 typecast (x, y); |
|
89 retval = octave_value (y); |
|
90 } |
|
91 else if (type == "int8") |
|
92 { |
|
93 int8NDArray y; |
|
94 typecast (x, y); |
|
95 retval = octave_value (y); |
|
96 } |
|
97 else if (type == "int16") |
|
98 { |
|
99 int16NDArray y; |
|
100 typecast (x, y); |
|
101 retval = octave_value (y); |
|
102 } |
|
103 else if (type == "int32") |
|
104 { |
|
105 int32NDArray y; |
|
106 typecast (x, y); |
|
107 retval = octave_value (y); |
|
108 } |
|
109 else if (type == "int64") |
|
110 { |
|
111 int64NDArray y; |
|
112 typecast (x, y); |
|
113 retval = octave_value (y); |
|
114 } |
|
115 else |
|
116 { |
|
117 NDArray y; |
|
118 typecast (x, y); |
|
119 retval = octave_value (y); |
|
120 } |
|
121 |
|
122 return retval; |
|
123 } |
|
124 |
|
125 DEFUN_DLD (typecast, args, , |
|
126 " -*- texinfo -*-\n\ |
|
127 @deftypefn {Loadable Function} {} typecast (@var{x}, @var{type})\n\ |
|
128 Converts from one datatype to another without changing the underlying\n\ |
|
129 data. The argument @var{type} defines the type of the return argument\n\ |
|
130 and must be one of 'uint8', 'uint16', 'uint32', 'uint64', 'int8', 'int16',\n\ |
|
131 'int32', 'int64', 'single' or 'double'.\n\ |
|
132 \n\ |
|
133 An example of the use of typecast on a little-endian machine is\n\ |
|
134 \n\ |
|
135 @example\n\ |
|
136 @group\n\ |
|
137 @var{x} = uint16 ([1, 65535]);\n\ |
|
138 typecast (@var{x}, 'uint8')\n\ |
|
139 @result{} [ 0, 1, 255, 255]\n\ |
|
140 @end group\n\ |
|
141 @end example\n\ |
|
142 @seealso{cast, swapbytes}\n\ |
|
143 @end deftypefn") |
|
144 { |
|
145 int nargin = args.length (); |
|
146 octave_value retval; |
|
147 |
|
148 if (nargin != 2) |
|
149 print_usage (); |
|
150 else |
|
151 { |
|
152 std::string type = args (1).string_value (); |
|
153 |
|
154 if (! error_state) |
|
155 { |
|
156 std::transform (type.begin (), type.end (), type.begin (), tolower); |
|
157 |
|
158 if (type == "single") |
|
159 error ("typecast: type 'single' is not supported"); |
7229
|
160 else if (type != "uint8" && type != "uint16" |
|
161 && type != "uint32" && type != "uint64" |
|
162 && type != "int8" && type != "int16" |
|
163 && type != "int32" && type != "int64" |
|
164 && type != "single" && type != "double") |
6869
|
165 error ("typecast: unrecognized or invalid type"); |
|
166 else if (args(0).is_sparse_type () || args(0).is_complex_type ()) |
|
167 error ("typecast: sparse and complex types are invalid"); |
|
168 else |
|
169 { |
|
170 dim_vector dv = args(0).dims (); |
|
171 bool seen = false; |
|
172 |
|
173 for (octave_idx_type i = 0; i < dv.length(); i++) |
|
174 if (dv (i) != 1) |
|
175 { |
|
176 if (seen) |
|
177 { |
|
178 error ("typecast: argument must be a vector"); |
|
179 break; |
|
180 } |
|
181 else |
|
182 seen = true; |
|
183 } |
|
184 |
|
185 if (!error_state) |
|
186 { |
|
187 if (args(0).is_uint8_type ()) |
|
188 retval = typecast (args(0).uint8_array_value (), type); |
|
189 else if (args(0).is_uint16_type ()) |
|
190 retval = typecast (args(0).uint16_array_value (), type); |
|
191 else if (args(0).is_uint32_type ()) |
|
192 retval = typecast (args(0).uint32_array_value (), type); |
|
193 else if (args(0).is_uint64_type ()) |
|
194 retval = typecast (args(0).uint64_array_value (), type); |
|
195 else if (args(0).is_int8_type ()) |
|
196 retval = typecast (args(0).int8_array_value (), type); |
|
197 else if (args(0).is_int16_type ()) |
|
198 retval = typecast (args(0).int16_array_value (), type); |
|
199 else if (args(0).is_int32_type ()) |
|
200 retval = typecast (args(0).int32_array_value (), type); |
|
201 else if (args(0).is_int64_type ()) |
|
202 retval = typecast (args(0).int64_array_value (), type); |
|
203 else |
|
204 retval = typecast (args(0).array_value (), type); |
|
205 } |
|
206 } |
|
207 } |
|
208 } |
|
209 |
|
210 return retval; |
|
211 } |