1
|
|
|
/* |
2
|
|
|
* from-bytes: convert bytes to numbers and strings. |
3
|
|
|
* Copyright (c) 2017 Rafael da Silva Rocha. |
4
|
|
|
* https://github.com/rochars/byte-data |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
const pad = require("../src/byte-padding.js"); |
|
|
|
|
8
|
|
|
const endianness = require("endianness"); |
9
|
|
|
const reader = require("../src/read-bytes.js"); |
10
|
|
|
const bitDepths = require("../src/bit-depth.js"); |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Turn a array of bytes into an array of what the bytes should represent. |
14
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
15
|
|
|
* @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. |
16
|
|
|
* @param {Object} params The options. They are: |
17
|
|
|
* - "signed", defaults to false |
18
|
|
|
* - "float", defaults to false, true for floats. |
19
|
|
|
* float is available for 16, 32 and 64 bit depths. |
20
|
|
|
* - "base", defaults to 10, can be 2, 10 or 16 |
21
|
|
|
* - "char", defaults to false, true for strings |
22
|
|
|
* - "be", defaults to false, true for big endian |
23
|
|
|
* @return {!Array<number>|string} The values represented in the bytes. |
24
|
|
|
*/ |
25
|
|
|
function fromBytes(bytes, bitDepth, params={}) { |
26
|
|
|
let base = 10; |
27
|
|
|
if ("base" in params) { |
28
|
|
|
base = params.base; |
29
|
|
|
} |
30
|
|
|
if (params.be) { |
31
|
|
|
endianness.endianness(bytes, bitDepth / 8); |
32
|
|
|
} |
33
|
|
|
return readBytes( |
34
|
|
|
bytes, bitDepth, params.char, params.signed, params.float, base); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Turn a array of bytes into an array of what the bytes should represent. |
39
|
|
|
* @param {!Array<number>|Uint8Array} bytes An array of bytes. |
40
|
|
|
* @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. |
41
|
|
|
* @param {boolean} isChar True if it is a string. |
42
|
|
|
* @param {boolean} isSigned True if the values should be signed. |
43
|
|
|
* @param {boolean} isFloat True if the values are IEEE floating point numbers. |
44
|
|
|
* @param {number} base The base, one of 2, 10 or 16. |
45
|
|
|
* @return {!Array<number>|string} The values represented in the bytes. |
46
|
|
|
*/ |
47
|
|
|
function readBytes(bytes, bitDepth, isChar, isSigned, isFloat, base) { |
48
|
|
|
let numbers = []; |
49
|
|
|
let i = 0; |
50
|
|
|
let j = 0; |
51
|
|
|
let offset = bitDepths.bitDepthOffsets[bitDepth]; |
52
|
|
|
let len = bytes.length - (offset -1); |
53
|
|
|
let maxBitDepthValue = bitDepths.maxBitDepth[bitDepth]; |
54
|
|
|
bytesToInt(bytes, base); |
55
|
|
|
let bitReader = getBitReader(bitDepth, isFloat, isChar); |
56
|
|
|
let signFunction = isSigned ? signed : function(x,y){return x;}; |
|
|
|
|
57
|
|
|
while (i < len) { |
58
|
|
|
numbers[j] = signFunction(bitReader(bytes, i), maxBitDepthValue); |
59
|
|
|
i += offset; |
60
|
|
|
j++; |
61
|
|
|
} |
62
|
|
|
if (isChar) { |
63
|
|
|
numbers = numbers.join(""); |
64
|
|
|
} |
65
|
|
|
return numbers; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Return a function to read binary data. |
70
|
|
|
* @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. |
71
|
|
|
* @param {boolean} isFloat True if the values are IEEE floating point numbers. |
72
|
|
|
* @param {boolean} isChar True if it is a string. |
73
|
|
|
* @return {Function} |
74
|
|
|
*/ |
75
|
|
|
function getBitReader(bitDepth, isFloat, isChar) { |
76
|
|
|
let bitReader; |
77
|
|
|
if (isChar) { |
78
|
|
|
bitReader = reader.readChar; |
79
|
|
|
} else { |
80
|
|
|
bitReader = reader[getReaderFunctionName(bitDepth, isFloat)]; |
81
|
|
|
} |
82
|
|
|
return bitReader; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Build a bit reading function name based on the arguments. |
87
|
|
|
* @param {number} bitDepth The bitDepth. 1, 2, 4, 8, 16, 24, 32, 40, 48, 64. |
88
|
|
|
* @param {boolean} isFloat True if the values are IEEE floating point numbers. |
89
|
|
|
* @return {string} |
90
|
|
|
*/ |
91
|
|
|
function getReaderFunctionName(bitDepth, isFloat) { |
92
|
|
|
return 'read' + |
93
|
|
|
((bitDepth == 2 || bitDepth == 4) ? 8 : bitDepth) + |
94
|
|
|
'Bit' + |
95
|
|
|
(isFloat ? "Float" : ""); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Turn bytes to base 10. |
100
|
|
|
* @param {!Array<number>|Uint8Array} bytes The bytes as binary or hex strings. |
101
|
|
|
* @param {number} base The base. |
102
|
|
|
*/ |
103
|
|
|
function bytesToInt(bytes, base) { |
104
|
|
|
if (base != 10) { |
105
|
|
|
let i = 0; |
106
|
|
|
let len = bytes.length; |
107
|
|
|
while(i < len) { |
108
|
|
|
bytes[i] = parseInt(bytes[i], base); |
109
|
|
|
i++; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Turn a unsigned number to a signed number. |
116
|
|
|
* @param {number} number The number. |
117
|
|
|
* @param {number} maxValue The max range for the number bit depth. |
118
|
|
|
*/ |
119
|
|
|
function signed(number, maxValue) { |
120
|
|
|
if (number > parseInt(maxValue / 2, 10) - 1) { |
121
|
|
|
number -= maxValue; |
122
|
|
|
} |
123
|
|
|
return number; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
module.exports.fromBytes = fromBytes; |
127
|
|
|
|