Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / libraries / libLidar / src / com / dielmo / lidar / ByteUtilities.java @ 25423

History | View | Annotate | Download (7.57 KB)

1
/* DielmoOpenLiDAR
2
 *
3
 * Copyright (C) 2008 DIELMO 3D S.L. (DIELMO) and Infrastructures  
4
 * and Transports Department of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 *
21
 * For more information, contact:
22
 *
23
 * DIELMO 3D S.L.
24
 * Plaza Vicente Andr?s Estell?s 1 Bajo E
25
 * 46950 Xirivella, Valencia
26
 * SPAIN
27
 *   
28
 * +34 963137212
29
 * dielmo@dielmo.com
30
 * www.dielmo.com
31
 * 
32
 * or
33
 * 
34
 * Generalitat Valenciana
35
 * Conselleria d'Infraestructures i Transport
36
 * Av. Blasco Ib??ez, 50
37
 * 46010 VALENCIA
38
 * SPAIN
39
 *
40
 * +34 963862235
41
 * gvsig@gva.es
42
 * www.gvsig.gva.es
43
 */
44

    
45
/*
46
 * AUTHORS (In addition to DIELMO and CIT):
47
 *  
48
 */
49

    
50
package com.dielmo.lidar;
51

    
52
/**
53
 * Static class to work with a given vector of bytes.
54
 *
55
 * @author Oscar Garcia
56
 */
57
public class ByteUtilities {
58

    
59
        // operaciones de vectores de bytes a tipos de java
60
        /**
61
         * From an array of bytes (byte[]), takes over from the position given as start
62
         * and gives value to int variable using the following 4 bytes.
63
         * 
64
         * @param arr array of bytes
65
         * @param start initial position
66
         */
67
        public static int arr2Int (byte[] arr, int start) {
68
                int i = 0;
69
                int length = 4;
70
                int cnt = 0;
71
                byte[] tmp = new byte[length];
72
                for (i = start; i < (start + length); i++) {
73
                        tmp[cnt] = arr[i];
74
                        //System.out.println(java.lang.Byte.toString(arr[i]) + " " + i);
75
                        cnt++;
76
                }
77
                int accum = 0;
78
                i = 0;
79
                for ( int shiftBy = 0; shiftBy < 32; shiftBy += 8 ) {
80
                        accum |= ( (int)( tmp[i] & 0xff ) ) << shiftBy;
81
                        i++;
82
                }
83
                return accum;
84
        }
85

    
86
        /**
87
         * From an array of bytes (byte[]), takes over from the position given as start
88
         * and gives value to double variable using the following 8 bytes.
89
         * 
90
         * @param arr array of bytes
91
         * @param start initial position
92
         */
93
        public static double arr2Double (byte[] arr, int start) {
94
                int i = 0;
95
                int length = 8;
96
                int cnt = 0;
97
                byte[] tmp = new byte[length];
98
                for (i = start; i < (start + length); i++) {
99
                        tmp[cnt] = arr[i];
100
                        //System.out.println(java.lang.Byte.toString(arr[i]) + " " + i);
101
                        cnt++;
102
                }
103
                long accum = 0;
104
                i = 0;
105
                for ( int shiftBy = 0; shiftBy < 64; shiftBy += 8 ) {
106
                        accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy;
107
                        i++;
108
                }
109
                return Double.longBitsToDouble(accum);
110
        }
111

    
112
        /**
113
         * From an array of bytes (byte[]), takes over from the position given as start
114
         * and gives value to long variable using the following 4 bytes.
115
         * 
116
         * @param arr array of bytes
117
         * @param start initial position
118
         */
119
        public static long arr2UnsignedInt (byte[] arr, int start) {
120
                int i = 0;
121
                int length = 4;
122
                int cnt = 0;
123
                byte[] tmp = new byte[length];
124
                for (i = start; i < (start + length); i++) {
125
                        tmp[cnt] = arr[i];
126
                        //System.out.println(java.lang.Byte.toString(arr[i]) + " " + i);
127
                        cnt++;
128
                }
129
                long accum = 0;
130
                i = 0;
131
                for ( int shiftBy = 0; shiftBy < 32; shiftBy += 8 ) {
132
                        accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy;
133
                        i++;
134
                }
135
                return accum;
136
        }
137
        
138
        /**
139
         * From an array of bytes (byte[]), takes over from the position given as start
140
         * and gives value to long variable using the following 8 bytes.
141
         * 
142
         * @param arr array of bytes
143
         * @param start initial position
144
         */
145
        public static long arr2Long(byte[] arr, int start) {
146
                int i = 0;
147
                int length = 8;
148
                int cnt = 0;
149
                byte[] tmp = new byte[length];
150
                for (i = start; i < (start + length); i++) {
151
                        tmp[cnt] = arr[i];
152
                        //System.out.println(java.lang.Byte.toString(arr[i]) + " " + i);
153
                        cnt++;
154
                }
155
                long accum = 0;
156
                i = 0;
157
                for ( int shiftBy = 0; shiftBy < 64; shiftBy += 8 ) {
158
                        accum |= ( (long)( tmp[i] & 0xff ) ) << shiftBy;
159
                        i++;
160
                }
161
                return accum;
162
        }
163
        
164
        /**
165
         * From an array of bytes (byte[]), takes over from the position given as start
166
         * and gives value to int variable using the following 2 bytes.
167
         * 
168
         * @param arr array of bytes
169
         * @param start initial position
170
         */
171
        public static int arr2Unsignedshort (byte[] arr, int start) {
172
                
173
                int i = 0;
174
                int length = 2;
175
                int cnt = 0;
176
                byte[] tmp = new byte[length];
177
                for (i = start; i < (start + length); i++) {
178
                        tmp[cnt] = arr[i];
179
                        //System.out.println(java.lang.Byte.toString(arr[i]) + " " + i);
180
                        cnt++;
181
                }
182
                int accum = 0;
183
                i = 0;
184
                for ( int shiftBy = 0; shiftBy < 16; shiftBy += 8 ) {
185
                        accum |= ( (int)( tmp[i] & 0xff ) ) << shiftBy;
186
                        i++;
187
                }
188
                return accum;
189
        }        
190

    
191
        /**
192
         * Write the bytes of "var" into new byte array.
193
         *
194
         * @param var the int to encode
195
         * @param arrayBytes The byte array to store into.
196
         * @param startIndex index data to begin write.
197
         */
198
        public static void int2Arr(int var, byte[] arrayBytes, int startIndex) {
199

    
200
                int length = 4;
201
        
202
                if (arrayBytes != null && startIndex+length <= arrayBytes.length) {
203
                        for (int j = startIndex; j < startIndex+length; j++) {
204
                                arrayBytes[j] = (byte) var; // se copian los 8 primeros bits de la variable
205
                                var >>= 8;
206
                        }
207
                }
208
        }
209
        
210
        /**
211
         * Write the bytes of "var" into new byte array.
212
         *
213
         * @param var the int that represents a unsigned short of 2 bytes to encode
214
         * @param arrayBytes The byte array to store into.
215
         * @param startIndex index data to begin write.
216
         */
217
        public static void unsignedShort2Arr(int var, byte[] arrayBytes, int startIndex) {
218

    
219
                int length = 2;
220
        
221
                if (arrayBytes != null && startIndex+length <= arrayBytes.length) {
222
                        for (int j = startIndex; j < startIndex+length; j++) {
223
                                arrayBytes[j] = (byte) var; // se copian los 8 primeros bits de la variable
224
                                var >>= 8;
225
                        }
226
                }
227
        }
228
        
229
        /**
230
         * Write the bytes of "var" into new byte array.
231
         *
232
         * @param var the long that represents a unsigned int of 4 bytes to encode
233
         * @param arrayBytes The byte array to store into.
234
         * @param startIndex index data to begin write.
235
         */
236
        public static void unsignedInt2Arr(long var, byte[] arrayBytes, int startIndex) {
237

    
238
                int length = 4;
239
        
240
                if (arrayBytes != null && startIndex+length <= arrayBytes.length) {
241
                        for (int j = startIndex; j < startIndex+length; j++) {
242
                                arrayBytes[j] = (byte) var; // se copian los 8 primeros bits de la variable
243
                                var >>= 8;
244
                        }
245
                }
246
        }
247
        
248
        /**
249
         * Write the bytes of "var" into new byte array.
250
         *
251
         * @param var the long of 8 bytes to encode
252
         * return tmp The byte array to store into.
253
         */
254
        public static void long2Arr(long var, byte[] arrayBytes, int startIndex) {
255
                
256
                int length = 8;
257
                
258
                if (arrayBytes != null && startIndex+length <= arrayBytes.length) {
259
                        for (int j = startIndex; j < startIndex+length; j++) {
260
                                arrayBytes[j] = (byte) var; // se copian los 8 primeros bits de la variable
261
                                var >>= 8;
262
                        }
263
                }
264
        }
265
        
266
        /**
267
         * Write the bytes of "var" into new byte array.
268
         *
269
         * @param var the double of 8 bytes to encode
270
         * return tmp The byte array.
271
         */
272
        public static void double2Arr(double var, byte[] arrayBytes, int startIndex) {
273
        
274
                long bits = Double.doubleToLongBits(var);
275
                long2Arr(bits, arrayBytes, startIndex);
276
        }
277
}