|
1
|
|
|
/* |
|
2
|
|
|
* to-bytes: bytes to numbers and strings. |
|
3
|
|
|
* Copyright (c) 2017 Rafael da Silva Rocha. |
|
4
|
|
|
* https://github.com/rochars/byte-data |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
const writer = require("../src/write-bytes.js"); |
|
8
|
|
|
const helpers = require("../src/helpers.js"); |
|
9
|
|
|
const bitDepthLib = require("../src/bit-depth.js"); |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Turn numbers and strings to bytes. |
|
13
|
|
|
* @param {!Array<number>|number|string} values The data. |
|
14
|
|
|
* @param {number} bitDepth The bit depth of the data. |
|
15
|
|
|
* Possible values are 1, 2, 4, 8, 16, 24, 32, 40, 48 or 64. |
|
16
|
|
|
* @param {Object} options The options: |
|
17
|
|
|
* - "float": True for floating point numbers. Default is false. |
|
18
|
|
|
* This option is available for 16, 32 and 64-bit numbers. |
|
19
|
|
|
* - "signed": True for signed values. Default is false. |
|
20
|
|
|
* - "base": The base of the output. Default is 10. Can be 2, 10 or 16. |
|
21
|
|
|
* - "char": If the bytes represent a string. Default is false. |
|
22
|
|
|
* - "be": If the values are big endian. Default is false (little endian). |
|
23
|
|
|
* - "buffer": If the bytes should be returned as a Uint8Array. |
|
24
|
|
|
* Default is false (bytes are returned as a regular array). |
|
25
|
|
|
* @return {!Array<number>|!Array<string>|Uint8Array} the data as a byte buffer. |
|
26
|
|
|
*/ |
|
27
|
|
|
function toBytes(values, bitDepth, options={"base": 10, "signed": false}) { |
|
28
|
|
|
if (bitDepth == 64) { |
|
29
|
|
|
options.float = true; |
|
30
|
|
|
} |
|
31
|
|
|
if (options.float) { |
|
32
|
|
|
options.signed = true; |
|
33
|
|
|
} |
|
34
|
|
|
options.bitDepth = bitDepth; |
|
35
|
|
|
values = helpers.turnToArray(values); |
|
36
|
|
|
let bytes = writeBytes(values, options, bitDepth); |
|
37
|
|
|
helpers.makeBigEndian(bytes, options.be, bitDepth); |
|
38
|
|
|
helpers.outputToBase(bytes, bitDepth, options.base); |
|
39
|
|
|
if (options.buffer) { |
|
40
|
|
|
bytes = new Uint8Array(bytes); |
|
41
|
|
|
} |
|
42
|
|
|
return bytes; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Write values as bytes. |
|
47
|
|
|
* @param {!Array<number>|number|string} values The data. |
|
48
|
|
|
* @param {Object} options The options according to the type. |
|
49
|
|
|
* @param {number} bitDepth The bitDepth of the data. |
|
50
|
|
|
* @return {!Array<number>} the bytes. |
|
51
|
|
|
*/ |
|
52
|
|
|
function writeBytes(values, options, bitDepth) { |
|
53
|
|
|
let bitWriter; |
|
54
|
|
|
if (options.char) { |
|
55
|
|
|
bitWriter = writer.writeString; |
|
56
|
|
|
} else { |
|
57
|
|
|
bitWriter = writer['write' + bitDepth + 'Bit' + (options.float ? "Float" : "")]; |
|
58
|
|
|
} |
|
59
|
|
|
let i = 0; |
|
60
|
|
|
let j = 0; |
|
61
|
|
|
let len = values.length; |
|
62
|
|
|
let bytes = []; |
|
63
|
|
|
let minMax = getBitDepthMinMax(options, bitDepth); |
|
64
|
|
|
while (i < len) { |
|
65
|
|
|
checkOverflow(values, i, minMax.min, minMax.max); |
|
66
|
|
|
j = bitWriter(bytes, values, i, j, options.signed); |
|
67
|
|
|
i++; |
|
68
|
|
|
} |
|
69
|
|
|
return bytes; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Get the minimum and maximum values accordind to the type. |
|
74
|
|
|
* @param {Object} options The options according to the type. |
|
75
|
|
|
* @param {number} bitDepth The bit depth of the data. |
|
76
|
|
|
* @return {Object} |
|
77
|
|
|
*/ |
|
78
|
|
|
function getBitDepthMinMax(options, bitDepth) { |
|
79
|
|
|
let minMax = {}; |
|
80
|
|
|
if (options.signed) { |
|
81
|
|
|
minMax.max = (bitDepthLib.BitDepthMaxValues[bitDepth] / 2) - 1; |
|
82
|
|
|
minMax.min = (bitDepthLib.BitDepthMaxValues[bitDepth] / 2) * -1; |
|
83
|
|
|
} else { |
|
84
|
|
|
minMax.max = bitDepthLib.BitDepthMaxValues[bitDepth] - 1; |
|
85
|
|
|
minMax.min = 0; |
|
86
|
|
|
} |
|
87
|
|
|
return minMax; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Limit the value according to the bit depth in case of |
|
92
|
|
|
* overflow or underflow. |
|
93
|
|
|
* @param {!Array<number>|number|string} values The data. |
|
94
|
|
|
* @param {number} index The index of the value in the array. |
|
95
|
|
|
* @param {number} min The minimum value. |
|
96
|
|
|
* @param {number} max The maximum value. |
|
97
|
|
|
*/ |
|
98
|
|
|
function checkOverflow(values, index, min, max) { |
|
99
|
|
|
if (values[index] > max) { |
|
100
|
|
|
values[index] = max; |
|
101
|
|
|
} else if(values[index] < min) { |
|
102
|
|
|
values[index] = min; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
module.exports.toBytes = toBytes; |
|
107
|
|
|
|