1
|
|
|
/* |
2
|
|
|
* Copyright (c) 2017-2018 Rafael da Silva Rocha. |
3
|
|
|
* |
4
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
5
|
|
|
* a copy of this software and associated documentation files (the |
6
|
|
|
* "Software"), to deal in the Software without restriction, including |
7
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
8
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
9
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
10
|
|
|
* the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be |
13
|
|
|
* included in all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
16
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
17
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
18
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
19
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
20
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
21
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @fileoverview Sserialize and deserialize integers and floats. |
27
|
|
|
* @see https://github.com/rochars/byte-data |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
import IntBuffer from './int-buffer.js'; |
31
|
|
|
import {validateType} from './validation.js'; |
32
|
|
|
import {unpack, pack} from 'ieee754-buffer'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* A class to pack and unpack integers and floating-point numbers. |
36
|
|
|
* Signed integers are two's complement. |
37
|
|
|
* Floating point are IEEE 754 standard. |
38
|
|
|
* @extends {IntBuffer} |
39
|
|
|
*/ |
40
|
|
|
export default class NumberBuffer extends IntBuffer { |
41
|
|
|
|
42
|
|
|
constructor(theType) { |
43
|
|
|
validateType(theType); |
44
|
|
|
theType.signed = theType.float ? false : theType.signed; |
45
|
|
|
super(theType); |
46
|
|
|
this.offset = this.parser.bytes; |
47
|
|
|
this.parser.bytes = this.parser.bits === 64 ? 4 : this.parser.bytes; |
48
|
|
|
this.setReaderAndWriter_(theType); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Read 1 16-bit float from bytes. |
53
|
|
|
* @see https://stackoverflow.com/a/8796597 |
54
|
|
|
* @param {!Uint8Array|!Array<number>} bytes An array of bytes. |
55
|
|
|
* @param {number=} i The index to read. |
56
|
|
|
* @return {number} |
57
|
|
|
* @private |
58
|
|
|
*/ |
59
|
|
|
read16F_(bytes, i=0) { |
60
|
|
|
return unpack(bytes, i, 5, 11); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Read 1 32-bit float from bytes using a TypedArray. |
65
|
|
|
* @param {!Uint8Array|!Array<number>} bytes An array of bytes. |
66
|
|
|
* @param {number=} i The index to read. |
67
|
|
|
* @return {number} |
68
|
|
|
* @private |
69
|
|
|
*/ |
70
|
|
|
read32F_(bytes, i=0) { |
71
|
|
|
//this.ui32_[0] = super.unpack(bytes, i); |
72
|
|
|
//return this.f32_[0]; |
73
|
|
|
return unpack(bytes, i, 8, 23); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Read 1 64-bit float from bytes using a TypedArray. |
78
|
|
|
* Thanks https://gist.github.com/kg/2192799 |
79
|
|
|
* @param {!Uint8Array|!Array<number>} bytes An array of bytes. |
80
|
|
|
* @param {number=} i The index to read. |
81
|
|
|
* @return {number} |
82
|
|
|
* @private |
83
|
|
|
*/ |
84
|
|
|
read64F_(bytes, i=0) { |
85
|
|
|
//this.ui32_[this.HIGH_] = super.unpack(bytes, i); |
86
|
|
|
//this.ui32_[this.LOW_] = super.unpack(bytes, i + 4); |
87
|
|
|
//return this.f64_[0]; |
88
|
|
|
return unpack(bytes, i, 11, 52); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Write one 16-bit float as a binary value. |
93
|
|
|
* @param {!Uint8Array|!Array<number>} bytes An array of bytes. |
94
|
|
|
* @param {number} num The number to write as bytes. |
95
|
|
|
* @param {number=} j The index being written in the byte buffer. |
96
|
|
|
* @return {number} The next index to write on the byte buffer. |
97
|
|
|
* @private |
98
|
|
|
*/ |
99
|
|
|
write16F_(bytes, num, j=0) { |
100
|
|
|
return pack(bytes, j, num, 5, 11); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Write one 32-bit float as a binary value using a TypedArray. |
105
|
|
|
* @param {!Uint8Array|!Array<number>} bytes An array of bytes. |
106
|
|
|
* @param {number} num The number to write as bytes. |
107
|
|
|
* @param {number=} j The index being written in the byte buffer. |
108
|
|
|
* @return {number} The next index to write on the byte buffer. |
109
|
|
|
* @private |
110
|
|
|
*/ |
111
|
|
|
write32F_(bytes, num, j=0) { |
112
|
|
|
//if (num !== num) { |
113
|
|
|
return pack(bytes, j, num, 8, 23); |
114
|
|
|
//} |
115
|
|
|
//this.f32_[0] = num; |
116
|
|
|
//return super.pack(bytes, this.ui32_[0], j); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Write one 64-bit float as a binary value using a TypedArray. |
121
|
|
|
* @param {!Uint8Array|!Array<number>} bytes An array of bytes. |
122
|
|
|
* @param {number} num The number to write as bytes. |
123
|
|
|
* @param {number=} j The index being written in the byte buffer. |
124
|
|
|
* @return {number} The next index to write on the byte buffer. |
125
|
|
|
* @private |
126
|
|
|
*/ |
127
|
|
|
write64F_(bytes, num, j=0) { |
128
|
|
|
//if (num !== num) { |
129
|
|
|
return pack(bytes, j, num, 11, 52); |
130
|
|
|
//} |
131
|
|
|
//this.f64_[0] = num; |
132
|
|
|
//j = super.pack(bytes, this.ui32_[this.HIGH_], j); |
133
|
|
|
//return super.pack(bytes, this.ui32_[this.LOW_], j); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Set the functions to pack and unpack numbers. |
138
|
|
|
* @param {!Object} theType The type definition. |
139
|
|
|
* @private |
140
|
|
|
*/ |
141
|
|
|
setReaderAndWriter_(theType) { |
142
|
|
|
if (theType.float) { |
143
|
|
|
if (theType.bits == 16) { |
144
|
|
|
this.unpack = this.read16F_; |
145
|
|
|
this.pack = this.write16F_; |
146
|
|
|
} else if(theType.bits == 32) { |
147
|
|
|
this.unpack = this.read32F_; |
148
|
|
|
this.pack = this.write32F_; |
149
|
|
|
} else { |
150
|
|
|
this.unpack = this.read64F_; |
151
|
|
|
this.pack = this.write64F_; |
152
|
|
|
} |
153
|
|
|
} else { |
154
|
|
|
this.unpack = super.unpack; |
155
|
|
|
this.pack = super.pack; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|