@gmod/cram 3.0.6 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/craiIndex.js +70 -87
- package/dist/craiIndex.js.map +1 -1
- package/dist/cram-bundle.js +1 -1
- package/dist/cramFile/codecs/_base.d.ts +1 -1
- package/dist/cramFile/codecs/beta.d.ts +2 -2
- package/dist/cramFile/codecs/beta.js +1 -1
- package/dist/cramFile/codecs/beta.js.map +1 -1
- package/dist/cramFile/codecs/byteArrayLength.d.ts +2 -2
- package/dist/cramFile/codecs/byteArrayStop.d.ts +4 -4
- package/dist/cramFile/codecs/byteArrayStop.js +1 -1
- package/dist/cramFile/codecs/byteArrayStop.js.map +1 -1
- package/dist/cramFile/codecs/external.d.ts +1 -1
- package/dist/cramFile/codecs/external.js +1 -1
- package/dist/cramFile/codecs/external.js.map +1 -1
- package/dist/cramFile/codecs/gamma.d.ts +1 -1
- package/dist/cramFile/codecs/gamma.js +1 -1
- package/dist/cramFile/codecs/gamma.js.map +1 -1
- package/dist/cramFile/codecs/getBits.d.ts +1 -1
- package/dist/cramFile/codecs/getBits.js.map +1 -1
- package/dist/cramFile/codecs/huffman.d.ts +1 -1
- package/dist/cramFile/codecs/huffman.js +1 -1
- package/dist/cramFile/codecs/huffman.js.map +1 -1
- package/dist/cramFile/codecs/index.d.ts +1 -1
- package/dist/cramFile/codecs/index.js +5 -5
- package/dist/cramFile/codecs/index.js.map +1 -1
- package/dist/cramFile/codecs/subexp.d.ts +2 -2
- package/dist/cramFile/codecs/subexp.js +1 -1
- package/dist/cramFile/codecs/subexp.js.map +1 -1
- package/dist/cramFile/container/compressionScheme.d.ts +2 -2
- package/dist/cramFile/container/compressionScheme.js +1 -1
- package/dist/cramFile/container/compressionScheme.js.map +1 -1
- package/dist/cramFile/container/index.d.ts +1 -2
- package/dist/cramFile/container/index.js +62 -79
- package/dist/cramFile/container/index.js.map +1 -1
- package/dist/cramFile/file.d.ts +9 -13
- package/dist/cramFile/file.js +218 -249
- package/dist/cramFile/file.js.map +1 -1
- package/dist/cramFile/record.js +2 -2
- package/dist/cramFile/record.js.map +1 -1
- package/dist/cramFile/sectionParsers.d.ts +16 -16
- package/dist/cramFile/sectionParsers.js +12 -9
- package/dist/cramFile/sectionParsers.js.map +1 -1
- package/dist/cramFile/slice/decodeRecord.d.ts +3 -3
- package/dist/cramFile/slice/decodeRecord.js +11 -11
- package/dist/cramFile/slice/decodeRecord.js.map +1 -1
- package/dist/cramFile/slice/index.d.ts +2 -2
- package/dist/cramFile/slice/index.js +241 -262
- package/dist/cramFile/slice/index.js.map +1 -1
- package/dist/cramFile/util.d.ts +4 -2
- package/dist/cramFile/util.js +29 -6
- package/dist/cramFile/util.js.map +1 -1
- package/dist/htscodecs/arith_gen.d.ts +18 -0
- package/dist/htscodecs/arith_gen.js +318 -0
- package/dist/htscodecs/arith_gen.js.map +1 -0
- package/dist/htscodecs/arith_sh.d.ts +16 -0
- package/dist/htscodecs/arith_sh.js +128 -0
- package/dist/htscodecs/arith_sh.js.map +1 -0
- package/dist/htscodecs/byte_model.d.ts +11 -0
- package/dist/htscodecs/byte_model.js +113 -0
- package/dist/htscodecs/byte_model.js.map +1 -0
- package/dist/htscodecs/fqzcomp.d.ts +1 -0
- package/dist/htscodecs/fqzcomp.js +325 -0
- package/dist/htscodecs/fqzcomp.js.map +1 -0
- package/dist/htscodecs/index.d.ts +5 -0
- package/dist/htscodecs/index.js +70 -0
- package/dist/htscodecs/index.js.map +1 -0
- package/dist/htscodecs/iostream.d.ts +27 -0
- package/dist/htscodecs/iostream.js +243 -0
- package/dist/htscodecs/iostream.js.map +1 -0
- package/dist/htscodecs/rans.d.ts +1 -0
- package/dist/htscodecs/rans.js +213 -0
- package/dist/htscodecs/rans.js.map +1 -0
- package/dist/htscodecs/rans4x16.d.ts +1 -0
- package/dist/htscodecs/rans4x16.js +405 -0
- package/dist/htscodecs/rans4x16.js.map +1 -0
- package/dist/htscodecs/tok3.d.ts +2 -0
- package/dist/htscodecs/tok3.js +363 -0
- package/dist/htscodecs/tok3.js.map +1 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/indexedCramFile.d.ts +4 -4
- package/dist/indexedCramFile.js +97 -108
- package/dist/indexedCramFile.js.map +1 -1
- package/dist/io/index.d.ts +2 -2
- package/dist/io/index.js +6 -6
- package/dist/io/index.js.map +1 -1
- package/dist/rans/constants.d.ts +1 -1
- package/dist/rans/constants.js +1 -1
- package/dist/rans/constants.js.map +1 -1
- package/dist/rans/d04.js.map +1 -1
- package/dist/rans/decoding.js.map +1 -1
- package/dist/rans/frequencies.js.map +1 -1
- package/dist/rans/index.js +5 -5
- package/dist/rans/index.js.map +1 -1
- package/dist/unzip.d.ts +1 -1
- package/dist/unzip.js +5 -3
- package/dist/unzip.js.map +1 -1
- package/esm/craiIndex.js +10 -12
- package/esm/craiIndex.js.map +1 -1
- package/esm/cramFile/codecs/_base.d.ts +1 -1
- package/esm/cramFile/codecs/beta.d.ts +2 -2
- package/esm/cramFile/codecs/beta.js +1 -1
- package/esm/cramFile/codecs/beta.js.map +1 -1
- package/esm/cramFile/codecs/byteArrayLength.d.ts +2 -2
- package/esm/cramFile/codecs/byteArrayStop.d.ts +4 -4
- package/esm/cramFile/codecs/byteArrayStop.js +1 -1
- package/esm/cramFile/codecs/byteArrayStop.js.map +1 -1
- package/esm/cramFile/codecs/external.d.ts +1 -1
- package/esm/cramFile/codecs/external.js +1 -1
- package/esm/cramFile/codecs/external.js.map +1 -1
- package/esm/cramFile/codecs/gamma.d.ts +1 -1
- package/esm/cramFile/codecs/gamma.js +1 -1
- package/esm/cramFile/codecs/gamma.js.map +1 -1
- package/esm/cramFile/codecs/getBits.d.ts +1 -1
- package/esm/cramFile/codecs/getBits.js.map +1 -1
- package/esm/cramFile/codecs/huffman.d.ts +1 -1
- package/esm/cramFile/codecs/huffman.js +1 -1
- package/esm/cramFile/codecs/huffman.js.map +1 -1
- package/esm/cramFile/codecs/index.d.ts +1 -1
- package/esm/cramFile/codecs/index.js +5 -5
- package/esm/cramFile/codecs/index.js.map +1 -1
- package/esm/cramFile/codecs/subexp.d.ts +2 -2
- package/esm/cramFile/codecs/subexp.js +1 -1
- package/esm/cramFile/codecs/subexp.js.map +1 -1
- package/esm/cramFile/container/compressionScheme.d.ts +2 -2
- package/esm/cramFile/container/compressionScheme.js +1 -1
- package/esm/cramFile/container/compressionScheme.js.map +1 -1
- package/esm/cramFile/container/index.d.ts +1 -2
- package/esm/cramFile/container/index.js +4 -7
- package/esm/cramFile/container/index.js.map +1 -1
- package/esm/cramFile/file.d.ts +9 -13
- package/esm/cramFile/file.js +54 -61
- package/esm/cramFile/file.js.map +1 -1
- package/esm/cramFile/record.js +2 -2
- package/esm/cramFile/record.js.map +1 -1
- package/esm/cramFile/sectionParsers.d.ts +16 -16
- package/esm/cramFile/sectionParsers.js +7 -7
- package/esm/cramFile/sectionParsers.js.map +1 -1
- package/esm/cramFile/slice/decodeRecord.d.ts +3 -3
- package/esm/cramFile/slice/decodeRecord.js +11 -11
- package/esm/cramFile/slice/decodeRecord.js.map +1 -1
- package/esm/cramFile/slice/index.d.ts +2 -2
- package/esm/cramFile/slice/index.js +7 -6
- package/esm/cramFile/slice/index.js.map +1 -1
- package/esm/cramFile/util.d.ts +4 -2
- package/esm/cramFile/util.js +19 -2
- package/esm/cramFile/util.js.map +1 -1
- package/esm/htscodecs/arith_gen.d.ts +18 -0
- package/esm/htscodecs/arith_gen.js +318 -0
- package/esm/htscodecs/arith_gen.js.map +1 -0
- package/esm/htscodecs/arith_sh.d.ts +16 -0
- package/esm/htscodecs/arith_sh.js +128 -0
- package/esm/htscodecs/arith_sh.js.map +1 -0
- package/esm/htscodecs/byte_model.d.ts +11 -0
- package/esm/htscodecs/byte_model.js +113 -0
- package/esm/htscodecs/byte_model.js.map +1 -0
- package/esm/htscodecs/fqzcomp.d.ts +1 -0
- package/esm/htscodecs/fqzcomp.js +325 -0
- package/esm/htscodecs/fqzcomp.js.map +1 -0
- package/esm/htscodecs/index.d.ts +5 -0
- package/esm/htscodecs/index.js +70 -0
- package/esm/htscodecs/index.js.map +1 -0
- package/esm/htscodecs/iostream.d.ts +27 -0
- package/esm/htscodecs/iostream.js +243 -0
- package/esm/htscodecs/iostream.js.map +1 -0
- package/esm/htscodecs/rans.d.ts +1 -0
- package/esm/htscodecs/rans.js +213 -0
- package/esm/htscodecs/rans.js.map +1 -0
- package/esm/htscodecs/rans4x16.d.ts +1 -0
- package/esm/htscodecs/rans4x16.js +405 -0
- package/esm/htscodecs/rans4x16.js.map +1 -0
- package/esm/htscodecs/tok3.d.ts +2 -0
- package/esm/htscodecs/tok3.js +363 -0
- package/esm/htscodecs/tok3.js.map +1 -0
- package/esm/index.d.ts +1 -1
- package/esm/index.js +1 -1
- package/esm/index.js.map +1 -1
- package/esm/indexedCramFile.d.ts +4 -4
- package/esm/indexedCramFile.js +10 -10
- package/esm/indexedCramFile.js.map +1 -1
- package/esm/io/index.d.ts +2 -2
- package/esm/io/index.js +1 -1
- package/esm/io/index.js.map +1 -1
- package/esm/rans/constants.d.ts +1 -1
- package/esm/rans/constants.js +1 -1
- package/esm/rans/constants.js.map +1 -1
- package/esm/rans/d04.js.map +1 -1
- package/esm/rans/decoding.js.map +1 -1
- package/esm/rans/frequencies.js.map +1 -1
- package/esm/rans/index.js +5 -5
- package/esm/rans/index.js.map +1 -1
- package/esm/unzip.d.ts +1 -1
- package/esm/unzip.js +4 -1
- package/esm/unzip.js.map +1 -1
- package/package.json +7 -10
- package/src/craiIndex.ts +17 -16
- package/src/cramFile/codecs/_base.ts +1 -1
- package/src/cramFile/codecs/beta.ts +3 -3
- package/src/cramFile/codecs/byteArrayLength.ts +1 -1
- package/src/cramFile/codecs/byteArrayStop.ts +3 -4
- package/src/cramFile/codecs/external.ts +3 -3
- package/src/cramFile/codecs/gamma.ts +2 -2
- package/src/cramFile/codecs/getBits.ts +1 -1
- package/src/cramFile/codecs/huffman.ts +2 -3
- package/src/cramFile/codecs/index.ts +7 -8
- package/src/cramFile/codecs/subexp.ts +3 -3
- package/src/cramFile/container/compressionScheme.ts +3 -3
- package/src/cramFile/container/index.ts +7 -10
- package/src/cramFile/file.ts +78 -86
- package/src/cramFile/record.ts +3 -3
- package/src/cramFile/sectionParsers.ts +27 -22
- package/src/cramFile/slice/decodeRecord.ts +17 -17
- package/src/cramFile/slice/index.ts +8 -9
- package/src/cramFile/util.ts +24 -6
- package/src/htscodecs/arith_gen.js +344 -0
- package/src/htscodecs/arith_sh.js +138 -0
- package/src/htscodecs/byte_model.js +126 -0
- package/src/htscodecs/fqzcomp.js +360 -0
- package/src/htscodecs/index.js +77 -0
- package/src/htscodecs/iostream.js +257 -0
- package/src/htscodecs/rans.js +233 -0
- package/src/htscodecs/rans4x16.js +452 -0
- package/src/htscodecs/tok3.js +413 -0
- package/src/index.ts +1 -1
- package/src/indexedCramFile.ts +6 -5
- package/src/io/index.ts +5 -4
- package/src/rans/constants.ts +1 -1
- package/src/rans/d04.ts +0 -1
- package/src/rans/decoding.ts +0 -1
- package/src/rans/frequencies.ts +0 -1
- package/src/rans/index.ts +5 -7
- package/src/unzip.ts +5 -1
- package/dist/cramFile/filehandle.d.ts +0 -1
- package/dist/cramFile/filehandle.js +0 -3
- package/dist/cramFile/filehandle.js.map +0 -1
- package/dist/unzip-pako.d.ts +0 -2
- package/dist/unzip-pako.js +0 -9
- package/dist/unzip-pako.js.map +0 -1
- package/esm/cramFile/filehandle.d.ts +0 -1
- package/esm/cramFile/filehandle.js +0 -2
- package/esm/cramFile/filehandle.js.map +0 -1
- package/esm/unzip-pako.d.ts +0 -2
- package/esm/unzip-pako.js +0 -6
- package/esm/unzip-pako.js.map +0 -1
- package/src/cramFile/filehandle.ts +0 -1
- package/src/typings/htscodecs.d.ts +0 -6
- package/src/unzip-pako.ts +0 -6
package/src/cramFile/util.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import md5 from 'md5'
|
|
2
1
|
import Long from 'long'
|
|
2
|
+
import md5 from 'md5'
|
|
3
|
+
|
|
3
4
|
import { CramBufferOverrunError } from './codecs/getBits'
|
|
4
5
|
|
|
5
6
|
export function itf8Size(v: number) {
|
|
@@ -60,7 +61,8 @@ export function parseItf8(buffer: Uint8Array, initialOffset: number) {
|
|
|
60
61
|
return [result, offset - initialOffset] as const
|
|
61
62
|
}
|
|
62
63
|
|
|
63
|
-
export function parseLtf8(buffer:
|
|
64
|
+
export function parseLtf8(buffer: Uint8Array, initialOffset: number) {
|
|
65
|
+
const dataView = new DataView(buffer.buffer)
|
|
64
66
|
let offset = initialOffset
|
|
65
67
|
const countFlags = buffer[offset]!
|
|
66
68
|
let n: number | Long
|
|
@@ -76,7 +78,7 @@ export function parseLtf8(buffer: Buffer, initialOffset: number) {
|
|
|
76
78
|
(buffer[offset + 1]! << 8) |
|
|
77
79
|
buffer[offset + 2]!) &
|
|
78
80
|
0x1fffff
|
|
79
|
-
n = ((countFlags & 63) << 16) |
|
|
81
|
+
n = ((countFlags & 63) << 16) | dataView.getUint16(offset + 1, true)
|
|
80
82
|
offset += 3
|
|
81
83
|
} else if (countFlags < 0xf0) {
|
|
82
84
|
n =
|
|
@@ -142,8 +144,8 @@ export function parseLtf8(buffer: Buffer, initialOffset: number) {
|
|
|
142
144
|
}
|
|
143
145
|
|
|
144
146
|
export function parseItem<T>(
|
|
145
|
-
buffer:
|
|
146
|
-
parser: (buffer:
|
|
147
|
+
buffer: Uint8Array,
|
|
148
|
+
parser: (buffer: Uint8Array, offset: number) => { offset: number; value: T },
|
|
147
149
|
startBufferPosition = 0,
|
|
148
150
|
startFilePosition = 0,
|
|
149
151
|
) {
|
|
@@ -167,7 +169,6 @@ export function tinyMemoize(_class: any, methodName: any) {
|
|
|
167
169
|
const res = method.call(this)
|
|
168
170
|
this[memoAttrName] = res
|
|
169
171
|
Promise.resolve(res).catch(() => {
|
|
170
|
-
|
|
171
172
|
delete this[memoAttrName]
|
|
172
173
|
})
|
|
173
174
|
}
|
|
@@ -178,3 +179,20 @@ export function tinyMemoize(_class: any, methodName: any) {
|
|
|
178
179
|
export function sequenceMD5(seq: string) {
|
|
179
180
|
return md5(seq.toUpperCase().replaceAll(/[^\u0021-\u007e]/g, ''))
|
|
180
181
|
}
|
|
182
|
+
|
|
183
|
+
export function sum(array: Uint8Array[]) {
|
|
184
|
+
let sum = 0
|
|
185
|
+
for (const entry of array) {
|
|
186
|
+
sum += entry.length
|
|
187
|
+
}
|
|
188
|
+
return sum
|
|
189
|
+
}
|
|
190
|
+
export function concatUint8Array(args: Uint8Array[]) {
|
|
191
|
+
const mergedArray = new Uint8Array(sum(args))
|
|
192
|
+
let offset = 0
|
|
193
|
+
for (const entry of args) {
|
|
194
|
+
mergedArray.set(entry, offset)
|
|
195
|
+
offset += entry.length
|
|
196
|
+
}
|
|
197
|
+
return mergedArray
|
|
198
|
+
}
|
|
@@ -0,0 +1,344 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2019,2020 Genome Research Ltd.
|
|
3
|
+
* Author(s): James Bonfield
|
|
4
|
+
*
|
|
5
|
+
* Redistribution and use in source and binary forms, with or without
|
|
6
|
+
* modification, are permitted provided that the following conditions are met:
|
|
7
|
+
*
|
|
8
|
+
* 1. Redistributions of source code must retain the above copyright notice,
|
|
9
|
+
* this list of conditions and the following disclaimer.
|
|
10
|
+
*
|
|
11
|
+
* 2. Redistributions in binary form must reproduce the above
|
|
12
|
+
* copyright notice, this list of conditions and the following
|
|
13
|
+
* disclaimer in the documentation and/or other materials provided
|
|
14
|
+
* with the distribution.
|
|
15
|
+
*
|
|
16
|
+
* 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
|
|
17
|
+
* Institute nor the names of its contributors may be used to endorse
|
|
18
|
+
* or promote products derived from this software without specific
|
|
19
|
+
* prior written permission.
|
|
20
|
+
*
|
|
21
|
+
* THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS
|
|
22
|
+
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
23
|
+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
24
|
+
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH
|
|
25
|
+
* LTD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
26
|
+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
27
|
+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
28
|
+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
29
|
+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
30
|
+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
31
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
const RangeCoder = require('./arith_sh')
|
|
35
|
+
const IOStream = require('./iostream')
|
|
36
|
+
const ByteModel = require('./byte_model')
|
|
37
|
+
const bzip2 = require('bzip2')
|
|
38
|
+
|
|
39
|
+
function sum(array) {
|
|
40
|
+
let sum = 0
|
|
41
|
+
for (const entry of array) {
|
|
42
|
+
sum += entry.length
|
|
43
|
+
}
|
|
44
|
+
return sum
|
|
45
|
+
}
|
|
46
|
+
function concatUint8Array(args) {
|
|
47
|
+
const mergedArray = new Uint8Array(sum(args))
|
|
48
|
+
let offset = 0
|
|
49
|
+
for (const entry of args) {
|
|
50
|
+
mergedArray.set(entry, offset)
|
|
51
|
+
offset += entry.length
|
|
52
|
+
}
|
|
53
|
+
return mergedArray
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const ARITH_ORDER = 1
|
|
57
|
+
const ARITH_EXT = 4
|
|
58
|
+
const ARITH_STRIPE = 8
|
|
59
|
+
const ARITH_NOSIZE = 16
|
|
60
|
+
const ARITH_CAT = 32
|
|
61
|
+
const ARITH_RLE = 64
|
|
62
|
+
const ARITH_PACK = 128
|
|
63
|
+
|
|
64
|
+
module.exports = class RangeCoderGen {
|
|
65
|
+
decode(src) {
|
|
66
|
+
this.stream = new IOStream(src)
|
|
67
|
+
return this.decodeStream(this.stream)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
decodeStream(stream, n_out = 0) {
|
|
71
|
+
var flags = this.stream.ReadByte()
|
|
72
|
+
if (!(flags & ARITH_NOSIZE)) n_out = this.stream.ReadUint7()
|
|
73
|
+
var e_len = n_out
|
|
74
|
+
|
|
75
|
+
var order = flags & ARITH_ORDER
|
|
76
|
+
|
|
77
|
+
// 4-way recursion
|
|
78
|
+
if (flags & ARITH_STRIPE) return this.decodeStripe(this.stream, n_out)
|
|
79
|
+
|
|
80
|
+
// Meta data
|
|
81
|
+
if (flags & ARITH_PACK) {
|
|
82
|
+
var P
|
|
83
|
+
;[P, e_len] = this.decodePackMeta(this.stream)
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// NOP, useful for tiny blocks
|
|
87
|
+
if (flags & ARITH_CAT) var data = this.decodeCat(this.stream, e_len)
|
|
88
|
+
// Entropy decode
|
|
89
|
+
else if (flags & ARITH_EXT) {
|
|
90
|
+
var data = this.decodeExt(this.stream, e_len)
|
|
91
|
+
} else if (flags & ARITH_RLE) {
|
|
92
|
+
var data = order
|
|
93
|
+
? this.decodeRLE1(this.stream, e_len)
|
|
94
|
+
: this.decodeRLE0(this.stream, e_len)
|
|
95
|
+
} else {
|
|
96
|
+
var data = order
|
|
97
|
+
? this.decode1(this.stream, e_len)
|
|
98
|
+
: this.decode0(this.stream, e_len)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Transforms
|
|
102
|
+
if (flags & ARITH_PACK) data = this.decodePack(data, P, n_out)
|
|
103
|
+
|
|
104
|
+
return data
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
//----------------------------------------------------------------------
|
|
108
|
+
// Order-0 codec
|
|
109
|
+
decode0(stream, n_out) {
|
|
110
|
+
var output = new Uint8Array(n_out)
|
|
111
|
+
|
|
112
|
+
var max_sym = stream.ReadByte()
|
|
113
|
+
if (max_sym == 0) max_sym = 256
|
|
114
|
+
|
|
115
|
+
var byte_model = new ByteModel(max_sym)
|
|
116
|
+
|
|
117
|
+
var rc = new RangeCoder(stream)
|
|
118
|
+
rc.RangeStartDecode(stream)
|
|
119
|
+
|
|
120
|
+
for (var i = 0; i < n_out; i++)
|
|
121
|
+
output[i] = byte_model.ModelDecode(stream, rc)
|
|
122
|
+
|
|
123
|
+
return output
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
//----------------------------------------------------------------------
|
|
127
|
+
// Order-1 codec
|
|
128
|
+
|
|
129
|
+
decode1(stream, n_out) {
|
|
130
|
+
var output = new Uint8Array(n_out)
|
|
131
|
+
|
|
132
|
+
var max_sym = stream.ReadByte()
|
|
133
|
+
if (max_sym == 0) max_sym = 256
|
|
134
|
+
|
|
135
|
+
var byte_model = new Array(max_sym)
|
|
136
|
+
for (var i = 0; i < max_sym; i++) byte_model[i] = new ByteModel(max_sym)
|
|
137
|
+
|
|
138
|
+
var rc = new RangeCoder(stream)
|
|
139
|
+
rc.RangeStartDecode(stream)
|
|
140
|
+
|
|
141
|
+
var last = 0
|
|
142
|
+
for (var i = 0; i < n_out; i++) {
|
|
143
|
+
output[i] = byte_model[last].ModelDecode(stream, rc)
|
|
144
|
+
last = output[i]
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return output
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
//----------------------------------------------------------------------
|
|
151
|
+
// External codec
|
|
152
|
+
decodeExt(stream, n_out) {
|
|
153
|
+
const bits = bzip2.array(stream.buf.slice(stream.pos))
|
|
154
|
+
let size = bzip2.header(bits)
|
|
155
|
+
let chunk
|
|
156
|
+
const chunks = []
|
|
157
|
+
do {
|
|
158
|
+
chunk = bzip2.decompress(bits, size)
|
|
159
|
+
if (chunk !== -1) {
|
|
160
|
+
chunks.push(chunk)
|
|
161
|
+
size -= chunk.length
|
|
162
|
+
}
|
|
163
|
+
} while (chunk !== -1)
|
|
164
|
+
return concatUint8Array(chunks)
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
//----------------------------------------------------------------------
|
|
168
|
+
// Order-0 RLE codec
|
|
169
|
+
decodeRLE0(stream, n_out) {
|
|
170
|
+
var output = new Uint8Array(n_out)
|
|
171
|
+
|
|
172
|
+
var max_sym = stream.ReadByte()
|
|
173
|
+
if (max_sym == 0) max_sym = 256
|
|
174
|
+
|
|
175
|
+
var model_lit = new ByteModel(max_sym)
|
|
176
|
+
var model_run = new Array(258)
|
|
177
|
+
for (var i = 0; i <= 257; i++) model_run[i] = new ByteModel(4)
|
|
178
|
+
|
|
179
|
+
var rc = new RangeCoder(stream)
|
|
180
|
+
rc.RangeStartDecode(stream)
|
|
181
|
+
|
|
182
|
+
var i = 0
|
|
183
|
+
while (i < n_out) {
|
|
184
|
+
output[i] = model_lit.ModelDecode(stream, rc)
|
|
185
|
+
var part = model_run[output[i]].ModelDecode(stream, rc)
|
|
186
|
+
var run = part
|
|
187
|
+
var rctx = 256
|
|
188
|
+
while (part == 3) {
|
|
189
|
+
part = model_run[rctx].ModelDecode(stream, rc)
|
|
190
|
+
rctx = 257
|
|
191
|
+
run += part
|
|
192
|
+
}
|
|
193
|
+
for (var j = 1; j <= run; j++) output[i + j] = output[i]
|
|
194
|
+
i += run + 1
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
return output
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
//----------------------------------------------------------------------
|
|
201
|
+
// Order-1 RLE codec
|
|
202
|
+
|
|
203
|
+
decodeRLE1(stream, n_out) {
|
|
204
|
+
var output = new Uint8Array(n_out)
|
|
205
|
+
|
|
206
|
+
var max_sym = stream.ReadByte()
|
|
207
|
+
if (max_sym == 0) max_sym = 256
|
|
208
|
+
|
|
209
|
+
var model_lit = new Array(max_sym)
|
|
210
|
+
for (var i = 0; i < max_sym; i++) model_lit[i] = new ByteModel(max_sym)
|
|
211
|
+
|
|
212
|
+
var model_run = new Array(258)
|
|
213
|
+
for (var i = 0; i <= 257; i++) model_run[i] = new ByteModel(4)
|
|
214
|
+
|
|
215
|
+
var rc = new RangeCoder(stream)
|
|
216
|
+
rc.RangeStartDecode(stream)
|
|
217
|
+
|
|
218
|
+
var last = 0
|
|
219
|
+
var i = 0
|
|
220
|
+
while (i < n_out) {
|
|
221
|
+
output[i] = model_lit[last].ModelDecode(stream, rc)
|
|
222
|
+
last = output[i]
|
|
223
|
+
var part = model_run[output[i]].ModelDecode(stream, rc)
|
|
224
|
+
var run = part
|
|
225
|
+
var rctx = 256
|
|
226
|
+
while (part == 3) {
|
|
227
|
+
part = model_run[rctx].ModelDecode(stream, rc)
|
|
228
|
+
rctx = 257
|
|
229
|
+
run += part
|
|
230
|
+
}
|
|
231
|
+
for (var j = 1; j <= run; j++) output[i + j] = output[i]
|
|
232
|
+
i += run + 1
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
return output
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
//----------------------------------------------------------------------
|
|
239
|
+
// Pack method
|
|
240
|
+
decodePackMeta(stream) {
|
|
241
|
+
this.nsym = stream.ReadByte()
|
|
242
|
+
|
|
243
|
+
var M = new Array(this.nsym)
|
|
244
|
+
for (var i = 0; i < this.nsym; i++) M[i] = stream.ReadByte()
|
|
245
|
+
|
|
246
|
+
var e_len = stream.ReadUint7() // Could be derived data from nsym and n_out
|
|
247
|
+
|
|
248
|
+
return [M, e_len]
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
decodePack(data, M, len) {
|
|
252
|
+
var out = new Uint8Array(len)
|
|
253
|
+
|
|
254
|
+
if (this.nsym <= 1) {
|
|
255
|
+
// Constant value
|
|
256
|
+
for (var i = 0; i < len; i++) out[i] = M[0]
|
|
257
|
+
} else if (this.nsym <= 2) {
|
|
258
|
+
// 1 bit per value
|
|
259
|
+
for (var i = 0, j = 0; i < len; i++) {
|
|
260
|
+
if (i % 8 == 0) var v = data[j++]
|
|
261
|
+
out[i] = M[v & 1]
|
|
262
|
+
v >>= 1
|
|
263
|
+
}
|
|
264
|
+
} else if (this.nsym <= 4) {
|
|
265
|
+
// 2 bits per value
|
|
266
|
+
for (var i = 0, j = 0; i < len; i++) {
|
|
267
|
+
if (i % 4 == 0) var v = data[j++]
|
|
268
|
+
out[i] = M[v & 3]
|
|
269
|
+
v >>= 2
|
|
270
|
+
}
|
|
271
|
+
} else if (this.nsym <= 16) {
|
|
272
|
+
// 4 bits per value
|
|
273
|
+
for (var i = 0, j = 0; i < len; i++) {
|
|
274
|
+
if (i % 2 == 0) var v = data[j++]
|
|
275
|
+
out[i] = M[v & 15]
|
|
276
|
+
v >>= 4
|
|
277
|
+
}
|
|
278
|
+
} else {
|
|
279
|
+
// 8 bits per value: NOP
|
|
280
|
+
return data
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return out
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Compute M array and return meta-data stream
|
|
287
|
+
packMeta(src) {
|
|
288
|
+
var stream = new IOStream('', 0, 1024)
|
|
289
|
+
|
|
290
|
+
// Count symbols
|
|
291
|
+
var M = new Array(256)
|
|
292
|
+
for (var i = 0; i < src.length; i++) M[src[i]] = 1
|
|
293
|
+
|
|
294
|
+
// Write Map
|
|
295
|
+
for (var nsym = 0, i = 0; i < 256; i++) if (M[i]) M[i] = ++nsym // map to 1..N
|
|
296
|
+
stream.WriteByte(nsym)
|
|
297
|
+
|
|
298
|
+
// FIXME: add check for nsym > 16?
|
|
299
|
+
// Or just accept it as an inefficient waste of time.
|
|
300
|
+
for (var i = 0; i < 256; i++) {
|
|
301
|
+
if (M[i]) {
|
|
302
|
+
stream.WriteByte(i) // adjust to 0..N-1
|
|
303
|
+
M[i]--
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
return [stream, M, nsym]
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
decodeStripe(stream, len) {
|
|
311
|
+
var N = stream.ReadByte()
|
|
312
|
+
|
|
313
|
+
// Retrieve lengths
|
|
314
|
+
var clen = new Array(N)
|
|
315
|
+
var ulen = new Array(N)
|
|
316
|
+
for (var j = 0; j < N; j++) clen[j] = stream.ReadUint7()
|
|
317
|
+
|
|
318
|
+
// Decode streams
|
|
319
|
+
var T = new Array(N)
|
|
320
|
+
for (var j = 0; j < N; j++) {
|
|
321
|
+
ulen[j] = Math.floor(len / N) + (len % N > j)
|
|
322
|
+
T[j] = this.decodeStream(stream, ulen[j])
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Transpose
|
|
326
|
+
var out = new Uint8Array(len)
|
|
327
|
+
for (var j = 0; j < N; j++) {
|
|
328
|
+
for (var i = 0; i < ulen[j]; i++) {
|
|
329
|
+
out[i * N + j] = T[j][i]
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return out
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
//----------------------------------------------------------------------
|
|
337
|
+
// Cat method
|
|
338
|
+
decodeCat(stream, len) {
|
|
339
|
+
var out = new Uint8Array(len)
|
|
340
|
+
for (var i = 0; i < len; i++) out[i] = stream.ReadByte()
|
|
341
|
+
|
|
342
|
+
return out
|
|
343
|
+
}
|
|
344
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2019 Genome Research Ltd.
|
|
3
|
+
* Author(s): James Bonfield
|
|
4
|
+
*
|
|
5
|
+
* Redistribution and use in source and binary forms, with or without
|
|
6
|
+
* modification, are permitted provided that the following conditions are met:
|
|
7
|
+
*
|
|
8
|
+
* 1. Redistributions of source code must retain the above copyright notice,
|
|
9
|
+
* this list of conditions and the following disclaimer.
|
|
10
|
+
*
|
|
11
|
+
* 2. Redistributions in binary form must reproduce the above
|
|
12
|
+
* copyright notice, this list of conditions and the following
|
|
13
|
+
* disclaimer in the documentation and/or other materials provided
|
|
14
|
+
* with the distribution.
|
|
15
|
+
*
|
|
16
|
+
* 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
|
|
17
|
+
* Institute nor the names of its contributors may be used to endorse
|
|
18
|
+
* or promote products derived from this software without specific
|
|
19
|
+
* prior written permission.
|
|
20
|
+
*
|
|
21
|
+
* THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS
|
|
22
|
+
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
23
|
+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
24
|
+
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH
|
|
25
|
+
* LTD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
26
|
+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
27
|
+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
28
|
+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
29
|
+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
30
|
+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
31
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
// An arithmetic coder, based on Eugene Shelwien's reimplementation of
|
|
35
|
+
// Michael Schindler range coder.
|
|
36
|
+
//
|
|
37
|
+
// Order-0 byte stream of ~/scratch/data/q40b
|
|
38
|
+
// C: 3.1s decode (approx same vs 32-bit and 64-bit)
|
|
39
|
+
// Arith_sh.js 6.7s decode (32-bit with carries)
|
|
40
|
+
// Arith.js 317.0s decode (64-bit no carries); int64 crippling it.
|
|
41
|
+
|
|
42
|
+
//----------------------------------------------------------------------
|
|
43
|
+
// Arithmetic (range) coder
|
|
44
|
+
module.exports = class RangeCoder {
|
|
45
|
+
constructor(src) {
|
|
46
|
+
this.low = 0
|
|
47
|
+
this.range = 0xffffffff
|
|
48
|
+
this.code = 0
|
|
49
|
+
this.FFnum = 0
|
|
50
|
+
this.carry = 0
|
|
51
|
+
this.cache = 0
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
RangeStartDecode(src) {
|
|
55
|
+
for (var i = 0; i < 5; i++) this.code = (this.code << 8) + src.ReadByte()
|
|
56
|
+
this.code &= 0xffffffff
|
|
57
|
+
this.code >>>= 0 // force to be +ve int
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
RangeGetFrequency(tot_freq) {
|
|
61
|
+
this.range = Math.floor(this.range / tot_freq)
|
|
62
|
+
//return this.code / this.range;
|
|
63
|
+
return Math.floor(this.code / this.range)
|
|
64
|
+
|
|
65
|
+
// Conceptual scenario; return freq only and don't modify range yet
|
|
66
|
+
//return Math.floor(this.code / (Math.floor(this.range / tot_freq)));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
RangeDecode(src, sym_low, sym_freq, tot_freq) {
|
|
70
|
+
// Conceptually we divide range here, but in practice we cached it earlier
|
|
71
|
+
//this.range = Math.floor(this.range / tot_freq);
|
|
72
|
+
|
|
73
|
+
this.code -= sym_low * this.range
|
|
74
|
+
this.range *= sym_freq
|
|
75
|
+
|
|
76
|
+
while (this.range < 1 << 24) {
|
|
77
|
+
this.range *= 256
|
|
78
|
+
this.code = this.code * 256 + src.ReadByte()
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
RangeShiftLow(dst) {
|
|
83
|
+
// We know range is < (1<<24) as we got here. We already have a
|
|
84
|
+
// cached copy of 8 bits from low. Is this correct, or does it need
|
|
85
|
+
// fixing? Possible scenarios.
|
|
86
|
+
// 1. Low < 0xff000000 thus low+range < 0xffffffff and cache
|
|
87
|
+
// cannot possibly change. Output cache and as many ffs as needed.
|
|
88
|
+
// 2. We already detected an overflow in RangeEncode, setting carry.
|
|
89
|
+
// In this case output cached byte + 1 and any 00s needed.
|
|
90
|
+
// 3. Neither case - range is low but we haven't yet detected if we're
|
|
91
|
+
// XXffffff or XY000000 scenario. Increase counter for ff/00s.
|
|
92
|
+
|
|
93
|
+
if ((this.low < 0xff000000) | this.carry) {
|
|
94
|
+
// cached byte if no overflow, byte+1 otherwise
|
|
95
|
+
dst.WriteByte(this.cache + this.carry)
|
|
96
|
+
|
|
97
|
+
// Flush any tracked FFs (no carry) or 00s (carry).
|
|
98
|
+
while (this.FFnum) {
|
|
99
|
+
dst.WriteByte(this.carry - 1)
|
|
100
|
+
this.FFnum--
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Take a copy of top byte ready for next flush
|
|
104
|
+
this.cache = this.low >>> 24
|
|
105
|
+
this.carry = 0
|
|
106
|
+
} else {
|
|
107
|
+
this.FFnum++ // keep track of number of trailing ff/00 bytes to write
|
|
108
|
+
}
|
|
109
|
+
this.low <<= 8
|
|
110
|
+
this.low >>>= 0 // force to be +ve int
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
RangeEncode(dst, sym_low, sym_freq, tot_freq) {
|
|
114
|
+
var old_low = this.low
|
|
115
|
+
this.range = Math.floor(this.range / tot_freq)
|
|
116
|
+
this.low += sym_low * this.range
|
|
117
|
+
this.low >>>= 0 // Truncate to +ve int so we can spot overflow
|
|
118
|
+
this.range *= sym_freq
|
|
119
|
+
|
|
120
|
+
// "low + sym*range < old_low" means we overflow; set carry.
|
|
121
|
+
// NB: can this.low < old_low occur twice before range < (1<<24)?
|
|
122
|
+
// We claim not, but prove it!
|
|
123
|
+
if (this.low < old_low) {
|
|
124
|
+
if (this.carry != 0) console.log('ERROR: Multiple carry')
|
|
125
|
+
this.carry = 1
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Renormalise if range gets too small
|
|
129
|
+
while (this.range < 1 << 24) {
|
|
130
|
+
this.range *= 256
|
|
131
|
+
this.RangeShiftLow(dst)
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
RangeFinishEncode(dst) {
|
|
136
|
+
for (var i = 0; i < 5; i++) this.RangeShiftLow(dst)
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) 2019 Genome Research Ltd.
|
|
3
|
+
* Author(s): James Bonfield
|
|
4
|
+
*
|
|
5
|
+
* Redistribution and use in source and binary forms, with or without
|
|
6
|
+
* modification, are permitted provided that the following conditions are met:
|
|
7
|
+
*
|
|
8
|
+
* 1. Redistributions of source code must retain the above copyright notice,
|
|
9
|
+
* this list of conditions and the following disclaimer.
|
|
10
|
+
*
|
|
11
|
+
* 2. Redistributions in binary form must reproduce the above
|
|
12
|
+
* copyright notice, this list of conditions and the following
|
|
13
|
+
* disclaimer in the documentation and/or other materials provided
|
|
14
|
+
* with the distribution.
|
|
15
|
+
*
|
|
16
|
+
* 3. Neither the names Genome Research Ltd and Wellcome Trust Sanger
|
|
17
|
+
* Institute nor the names of its contributors may be used to endorse
|
|
18
|
+
* or promote products derived from this software without specific
|
|
19
|
+
* prior written permission.
|
|
20
|
+
*
|
|
21
|
+
* THIS SOFTWARE IS PROVIDED BY GENOME RESEARCH LTD AND CONTRIBUTORS "AS
|
|
22
|
+
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
|
|
23
|
+
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
24
|
+
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENOME RESEARCH
|
|
25
|
+
* LTD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
26
|
+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
27
|
+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
28
|
+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
29
|
+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
30
|
+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
31
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
// An adaptive probability model for encoding and decoding of symbols
|
|
35
|
+
// within a given alphabet, using the range coder to get/put the
|
|
36
|
+
// compressed data.
|
|
37
|
+
|
|
38
|
+
const MAX_FREQ = (1 << 16) - 17
|
|
39
|
+
const STEP = 16
|
|
40
|
+
|
|
41
|
+
module.exports = class ByteModel {
|
|
42
|
+
constructor(max_sym = 256) {
|
|
43
|
+
this.total_freq = max_sym
|
|
44
|
+
this.max_sym = max_sym - 1
|
|
45
|
+
this.S = new Array()
|
|
46
|
+
this.F = new Array()
|
|
47
|
+
|
|
48
|
+
for (var i = 0; i <= this.max_sym; i++) {
|
|
49
|
+
this.S[i] = i
|
|
50
|
+
this.F[i] = 1
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
ModelDecode(src, rc) {
|
|
55
|
+
// Find symbol
|
|
56
|
+
var freq = rc.RangeGetFrequency(this.total_freq)
|
|
57
|
+
|
|
58
|
+
// Linear scan to find cumulative frequency 'freq'
|
|
59
|
+
var acc = 0
|
|
60
|
+
var x = 0
|
|
61
|
+
while (acc + this.F[x] <= freq) acc += this.F[x++]
|
|
62
|
+
|
|
63
|
+
// for (var acc = 0; (acc += this.F[x]) <= freq; x++)
|
|
64
|
+
// ;
|
|
65
|
+
// acc -= this.F[x];
|
|
66
|
+
|
|
67
|
+
// Update range coder
|
|
68
|
+
rc.RangeDecode(src, acc, this.F[x], this.total_freq)
|
|
69
|
+
|
|
70
|
+
// Update model
|
|
71
|
+
this.F[x] += STEP
|
|
72
|
+
this.total_freq += STEP
|
|
73
|
+
if (this.total_freq > MAX_FREQ) this.ModelRenormalise()
|
|
74
|
+
|
|
75
|
+
// Keep symbols approximately frequency sorted
|
|
76
|
+
var sym = this.S[x]
|
|
77
|
+
if (x > 0 && this.F[x] > this.F[x - 1]) {
|
|
78
|
+
var tmp = this.F[x]
|
|
79
|
+
this.F[x] = this.F[x - 1]
|
|
80
|
+
this.F[x - 1] = tmp
|
|
81
|
+
|
|
82
|
+
tmp = this.S[x]
|
|
83
|
+
this.S[x] = this.S[x - 1]
|
|
84
|
+
this.S[x - 1] = tmp
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
return sym
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
ModelRenormalise() {
|
|
91
|
+
// Halve all the frequencies, being careful not to hit zero
|
|
92
|
+
this.total_freq = 0
|
|
93
|
+
for (var i = 0; i <= this.max_sym; i++) {
|
|
94
|
+
this.F[i] -= Math.floor(this.F[i] / 2)
|
|
95
|
+
this.total_freq += this.F[i]
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
ModelEncode(dst, rc, sym) {
|
|
100
|
+
// Find cumulative frequency
|
|
101
|
+
var acc = 0
|
|
102
|
+
for (var x = 0; this.S[x] != sym; x++) acc += this.F[x]
|
|
103
|
+
|
|
104
|
+
// Encode
|
|
105
|
+
rc.RangeEncode(dst, acc, this.F[x], this.total_freq)
|
|
106
|
+
|
|
107
|
+
// Update model
|
|
108
|
+
this.F[x] += STEP
|
|
109
|
+
this.total_freq += STEP
|
|
110
|
+
if (this.total_freq > MAX_FREQ)
|
|
111
|
+
// FIXME x2
|
|
112
|
+
this.ModelRenormalise()
|
|
113
|
+
|
|
114
|
+
// Keep symbols approximately frequency sorted
|
|
115
|
+
var sym = this.S[x]
|
|
116
|
+
if (x > 0 && this.F[x] > this.F[x - 1]) {
|
|
117
|
+
var tmp = this.F[x]
|
|
118
|
+
this.F[x] = this.F[x - 1]
|
|
119
|
+
this.F[x - 1] = tmp
|
|
120
|
+
|
|
121
|
+
tmp = this.S[x]
|
|
122
|
+
this.S[x] = this.S[x - 1]
|
|
123
|
+
this.S[x - 1] = tmp
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|