@syzlm/ofdparser 0.0.1
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/.babelrc +14 -0
- package/.eslintignore +2 -0
- package/.eslintrc.json +18 -0
- package/.prettierrc.json +9 -0
- package/dist/OFD.d.ts +8 -0
- package/dist/Pages.d.ts +7 -0
- package/dist/RootDocumnet.d.ts +1 -0
- package/dist/bundle.cjs.js +64123 -0
- package/dist/bundle.esm.js +64115 -0
- package/dist/bundle.iife.js +14743 -0
- package/dist/bundle.umd.js +64045 -0
- package/dist/constant/index.d.ts +7 -0
- package/dist/constant.d.ts +6 -0
- package/dist/core/ofd-core/Annotations/AnnotationsXml.d.ts +16 -0
- package/dist/core/ofd-core/Asn.1Utils.d.ts +47 -0
- package/dist/core/ofd-core/Documents/DocumentResXml.d.ts +15 -0
- package/dist/core/ofd-core/Documents/DocumnetXml.d.ts +25 -0
- package/dist/core/ofd-core/Documents/PublicResXml.d.ts +15 -0
- package/dist/core/ofd-core/OFDElement.d.ts +232 -0
- package/dist/core/ofd-core/Ofd/OFDXml.d.ts +19 -0
- package/dist/core/ofd-core/Pages/PageXml.d.ts +20 -0
- package/dist/core/ofd-core/Signatures/Signatures.d.ts +20 -0
- package/dist/core/ofd-core/TemplatePages/TemplatePages.d.ts +20 -0
- package/dist/core/ofd-core/constant.d.ts +29 -0
- package/dist/core/ofd-core/utils.d.ts +21 -0
- package/dist/dicom/dicom.d.ts +15 -0
- package/dist/dicom/index.d.ts +1 -0
- package/dist/errors/ErrorHandle.d.ts +6 -0
- package/dist/errors/ErrorMsg.d.ts +16 -0
- package/dist/errors/FetchErrors.d.ts +4 -0
- package/dist/errors/MimeError.d.ts +4 -0
- package/dist/errors/OFDErrors.d.ts +4 -0
- package/dist/errors/UnzipErrors.d.ts +4 -0
- package/dist/errors/XmlErrors.d.ts +4 -0
- package/dist/errors/error-msg.d.ts +9 -0
- package/dist/es.d.ts +2 -0
- package/dist/index.d.ts +6 -0
- package/dist/lib/ParseFile.d.ts +7 -0
- package/dist/lib/UnitCoversion.d.ts +31 -0
- package/dist/lib/asn1/Asn.1.d.ts +34 -0
- package/dist/lib/asn1/Stream.d.ts +27 -0
- package/dist/lib/asn1/asn1.d.ts +4 -0
- package/dist/lib/asn1/base64.d.ts +4 -0
- package/dist/lib/asn1/hex.d.ts +5 -0
- package/dist/lib/asn1/index.d.ts +5 -0
- package/dist/lib/asn1/int10.d.ts +34 -0
- package/dist/lib/asn1/oids.d.ts +2 -0
- package/dist/lib/asn1/string-cut.d.ts +2 -0
- package/dist/lib/decode-html.d.ts +7 -0
- package/dist/lib/fetch.d.ts +17 -0
- package/dist/lib/is-element.d.ts +5 -0
- package/dist/lib/load-files.d.ts +3 -0
- package/dist/lib/load-opentype.d.ts +34 -0
- package/dist/lib/load-script.d.ts +1 -0
- package/dist/lib/mime.d.ts +8 -0
- package/dist/lib/read-buffer.d.ts +2 -0
- package/dist/lib/toFixeds.d.ts +7 -0
- package/dist/libs/Asn.1Utils.d.ts +47 -0
- package/dist/libs/asn1/Asn.1.d.ts +34 -0
- package/dist/libs/asn1/Stream.d.ts +27 -0
- package/dist/libs/asn1/asn1.d.ts +4 -0
- package/dist/libs/asn1/base64.d.ts +4 -0
- package/dist/libs/asn1/hex.d.ts +5 -0
- package/dist/libs/asn1/index.d.ts +5 -0
- package/dist/libs/asn1/int10.d.ts +34 -0
- package/dist/libs/asn1/oids.d.ts +2 -0
- package/dist/libs/asn1/string-cut.d.ts +2 -0
- package/dist/libs/decode-html.d.ts +7 -0
- package/dist/libs/fetch.d.ts +17 -0
- package/dist/libs/mime.d.ts +8 -0
- package/dist/libs/ofdArray.d.ts +10 -0
- package/dist/libs/ofdImageObject.d.ts +9 -0
- package/dist/libs/ofdLayer.d.ts +3 -0
- package/dist/libs/ofdName.d.ts +17 -0
- package/dist/libs/ofdPathObject.d.ts +11 -0
- package/dist/libs/ofdTextObject.d.ts +6 -0
- package/dist/libs/parseAnnotation.d.ts +5 -0
- package/dist/libs/parseColor.d.ts +5 -0
- package/dist/libs/parsePageArea.d.ts +17 -0
- package/dist/libs/parser-elements.d.ts +8 -0
- package/dist/libs/parserElements.d.ts +8 -0
- package/dist/libs/pipeExtensions.d.ts +4 -0
- package/dist/libs/read-buffer.d.ts +2 -0
- package/dist/main.d.ts +15 -0
- package/dist/ofd/ConverterDpi.d.ts +16 -0
- package/dist/ofd/index.d.ts +1 -0
- package/dist/ofd/ofd-utils.d.ts +137 -0
- package/dist/ofd/ofd.d.ts +35 -0
- package/dist/ofd/parse-ofd.d.ts +18 -0
- package/dist/ofd/render.d.ts +18 -0
- package/dist/ofd/svg-image.d.ts +12 -0
- package/dist/ofd/svg-path.d.ts +5 -0
- package/dist/ofd/svg-text.d.ts +8 -0
- package/dist/ofd.parse.d.ts +6 -0
- package/dist/ofd.xml.d.ts +1 -0
- package/dist/ofdParse.d.ts +6 -0
- package/dist/pipe/annotations.d.ts +13 -0
- package/dist/pipe/attachments.d.ts +13 -0
- package/dist/pipe/commonData.d.ts +18 -0
- package/dist/pipe/customTags.d.ts +14 -0
- package/dist/pipe/extensions.d.ts +14 -0
- package/dist/pipe/output.d.ts +7 -0
- package/dist/pipe/pages.d.ts +13 -0
- package/dist/pipe/permissions.d.ts +14 -0
- package/dist/pipe/pipeline.d.ts +63 -0
- package/dist/pipe/startPipe.d.ts +11 -0
- package/dist/xml/index.d.ts +2 -0
- package/dist/xml/render.d.ts +6 -0
- package/dist/xml/xml.d.ts +18 -0
- package/package.json +47 -0
- package/public/index.html +75 -0
- package/public/opentype.min.js +2 -0
- package/rollup.config.dev.mjs +47 -0
- package/rollup.config.mjs +70 -0
- package/src/constant/index.ts +13 -0
- package/src/core/ofd-core/Annotations/Annotation.km +206 -0
- package/src/core/ofd-core/Annotations/Annotations.km +59 -0
- package/src/core/ofd-core/Annotations/Annotations.md +13 -0
- package/src/core/ofd-core/Annotations/AnnotationsXml.ts +114 -0
- package/src/core/ofd-core/Asn.1Utils.ts +225 -0
- package/src/core/ofd-core/Documents/DocumentResXml.ts +55 -0
- package/src/core/ofd-core/Documents/Documnet.km +862 -0
- package/src/core/ofd-core/Documents/DocumnetXml.ts +118 -0
- package/src/core/ofd-core/Documents/Documnets.md +296 -0
- package/src/core/ofd-core/Documents/PublicResXml.ts +63 -0
- package/src/core/ofd-core/Documents//345/233/276/345/261/202/346/270/262/346/237/223/351/241/272/345/272/217.png +0 -0
- package/src/core/ofd-core/Documents//350/241/25022/347/272/277/346/235/241/350/277/236/346/216/245/346/240/267/345/274/217.png +0 -0
- package/src/core/ofd-core/Documents//350/241/25023/347/272/277/346/235/241/350/231/232/347/272/277/346/240/267/345/274/217.png +0 -0
- package/src/core/ofd-core/Documents//350/241/25023/347/273/223/345/220/210/347/202/271/351/225/277/345/272/246.png +0 -0
- package/src/core/ofd-core/Documents//350/241/25024/347/272/277/346/235/241/347/253/257/347/202/271/346/240/267/345/274/217.png +0 -0
- package/src/core/ofd-core/Documents//351/241/265/350/276/271/347/225/214/345/261/202/346/254/241/347/273/223/346/236/204.png +0 -0
- package/src/core/ofd-core/OFDElement.ts +724 -0
- package/src/core/ofd-core/Ofd/CT_DocInfo.km +197 -0
- package/src/core/ofd-core/Ofd/OFD.km +114 -0
- package/src/core/ofd-core/Ofd/OFD.md +99 -0
- package/src/core/ofd-core/Ofd/OFDXml.ts +69 -0
- package/src/core/ofd-core/Pages/PAGE.md +40 -0
- package/src/core/ofd-core/Pages/PageXml.ts +59 -0
- package/src/core/ofd-core/Signatures/Signature.km +308 -0
- package/src/core/ofd-core/Signatures/Signature.md +21 -0
- package/src/core/ofd-core/Signatures/Signatures.km +315 -0
- package/src/core/ofd-core/Signatures/Signatures.md +24 -0
- package/src/core/ofd-core/Signatures/Signatures.ts +247 -0
- package/src/core/ofd-core/TemplatePages/TemplatePages.ts +56 -0
- package/src/core/ofd-core/asn.md +60 -0
- package/src/core/ofd-core/constant.ts +35 -0
- package/src/core/ofd-core/index.d.ts +867 -0
- package/src/core/ofd-core/utils.ts +86 -0
- package/src/dicom/dicom.ts +42 -0
- package/src/dicom/index.ts +1 -0
- package/src/errors/ErrorHandle.ts +14 -0
- package/src/errors/FetchErrors.ts +8 -0
- package/src/errors/MimeError.ts +9 -0
- package/src/errors/OFDErrors.ts +11 -0
- package/src/errors/UnzipErrors.ts +9 -0
- package/src/errors/XmlErrors.ts +11 -0
- package/src/errors/error-msg.ts +17 -0
- package/src/es.ts +3 -0
- package/src/global.d.ts +7 -0
- package/src/index.ts +7 -0
- package/src/lib/ParseFile.ts +12 -0
- package/src/lib/README.md +69 -0
- package/src/lib/UnitCoversion.ts +55 -0
- package/src/lib/asn1/Asn.1.ts +284 -0
- package/src/lib/asn1/README.md +1 -0
- package/src/lib/asn1/Stream.ts +314 -0
- package/src/lib/asn1/asn1.ts +120 -0
- package/src/lib/asn1/base64.ts +97 -0
- package/src/lib/asn1/hex.ts +60 -0
- package/src/lib/asn1/index.ts +5 -0
- package/src/lib/asn1/int10.ts +103 -0
- package/src/lib/asn1/oids.ts +6164 -0
- package/src/lib/asn1/string-cut.ts +8 -0
- package/src/lib/decode-html.ts +37 -0
- package/src/lib/fetch.ts +70 -0
- package/src/lib/is-element.ts +13 -0
- package/src/lib/jbig/README.md +1 -0
- package/src/lib/jbig/arithmetic_decoder.js +184 -0
- package/src/lib/jbig/base_stream.js +117 -0
- package/src/lib/jbig/ccitt.js +1068 -0
- package/src/lib/jbig/ccitt_stream.js +60 -0
- package/src/lib/jbig/compatibility.js +108 -0
- package/src/lib/jbig/core_utils.js +591 -0
- package/src/lib/jbig/decode_stream.js +184 -0
- package/src/lib/jbig/is_node.js +28 -0
- package/src/lib/jbig/jbig2.js +2582 -0
- package/src/lib/jbig/jbig2_stream.js +78 -0
- package/src/lib/jbig/primitives.js +441 -0
- package/src/lib/jbig/stream.js +101 -0
- package/src/lib/jbig/util.js +1231 -0
- package/src/lib/load-files.ts +26 -0
- package/src/lib/load-opentype.ts +161 -0
- package/src/lib/load-script.ts +33 -0
- package/src/lib/mime.ts +97 -0
- package/src/lib/read-buffer.ts +19 -0
- package/src/lib/toFixeds.ts +56 -0
- package/src/ofd/ConverterDpi.ts +35 -0
- package/src/ofd/OFD.md +38 -0
- package/src/ofd/TODO.md +10 -0
- package/src/ofd/index.ts +1 -0
- package/src/ofd/ofd-utils.ts +627 -0
- package/src/ofd/ofd.demo.md +12 -0
- package/src/ofd/ofd.ts +200 -0
- package/src/ofd/ofdxml.km +162 -0
- package/src/ofd/parse-ofd.ts +103 -0
- package/src/ofd/render.ts +393 -0
- package/src/ofd/svg-image.ts +318 -0
- package/src/ofd/svg-path.ts +283 -0
- package/src/ofd/svg-text.ts +272 -0
- package/src/xml/index.ts +3 -0
- package/src/xml/render.ts +171 -0
- package/src/xml/xml.ts +136 -0
- package/tsconfig.json +21 -0
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
const REGX_HTML_DECODE = /&\w+;|&#(\d+);/g;
|
|
2
|
+
|
|
3
|
+
const HTML_DECODE: { [k: string]: string } = {
|
|
4
|
+
'<': '<',
|
|
5
|
+
'>': '>',
|
|
6
|
+
'&': '&',
|
|
7
|
+
' ': ' ',
|
|
8
|
+
'"': '"',
|
|
9
|
+
'©': '',
|
|
10
|
+
''': "'"
|
|
11
|
+
// Add more
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* html标签语义转string
|
|
16
|
+
* @param str
|
|
17
|
+
* @returns
|
|
18
|
+
*/
|
|
19
|
+
const decodeHtml = function (str: string) {
|
|
20
|
+
const htmlStr = str !== undefined ? str : String(str);
|
|
21
|
+
return typeof htmlStr != 'string'
|
|
22
|
+
? htmlStr
|
|
23
|
+
: htmlStr.replace(REGX_HTML_DECODE, function ($0: string, $1) {
|
|
24
|
+
var c = HTML_DECODE[$0];
|
|
25
|
+
if (c == undefined) {
|
|
26
|
+
// Maybe is Entity Number
|
|
27
|
+
if (!isNaN($1)) {
|
|
28
|
+
c = String.fromCharCode($1 == 160 ? 32 : $1);
|
|
29
|
+
} else {
|
|
30
|
+
c = $0;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return c;
|
|
34
|
+
});
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export default decodeHtml;
|
package/src/lib/fetch.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import VaildFetchError from '../errors/FetchErrors';
|
|
2
|
+
|
|
3
|
+
type FetchMethod = 'POST' | 'GET';
|
|
4
|
+
|
|
5
|
+
type FetchMode = 'no-cors' | 'cors' | 'same-origin';
|
|
6
|
+
|
|
7
|
+
type FetchCredentials = 'include' | 'same-origin' | 'omit';
|
|
8
|
+
|
|
9
|
+
type FetchCache =
|
|
10
|
+
| 'default'
|
|
11
|
+
| 'no-cache'
|
|
12
|
+
| 'reload'
|
|
13
|
+
| 'force-cache'
|
|
14
|
+
| 'only-if-cached';
|
|
15
|
+
interface FetchHeader {
|
|
16
|
+
[key: string]: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface FetchOptions {
|
|
20
|
+
method?: FetchMethod;
|
|
21
|
+
headers?: FetchHeader;
|
|
22
|
+
mode?: FetchMode;
|
|
23
|
+
credentials?: FetchCredentials;
|
|
24
|
+
cache?: FetchCache;
|
|
25
|
+
body?: any;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export default async (url: string, data: any, options: FetchOptions) => {
|
|
29
|
+
try {
|
|
30
|
+
const {
|
|
31
|
+
method = 'GET',
|
|
32
|
+
mode = 'cors',
|
|
33
|
+
cache = 'no-cache',
|
|
34
|
+
credentials = 'same-origin',
|
|
35
|
+
headers,
|
|
36
|
+
body,
|
|
37
|
+
...restOpts
|
|
38
|
+
} = options || {};
|
|
39
|
+
const fetchOptions: FetchOptions = {
|
|
40
|
+
method,
|
|
41
|
+
mode,
|
|
42
|
+
cache,
|
|
43
|
+
credentials
|
|
44
|
+
};
|
|
45
|
+
if (method === 'POST') {
|
|
46
|
+
fetchOptions.headers = { 'Content-type': 'application/json;' };
|
|
47
|
+
fetchOptions.body = JSON.stringify(data);
|
|
48
|
+
}
|
|
49
|
+
Object.assign(fetchOptions, { ...restOpts });
|
|
50
|
+
const response = await window.fetch(url, {
|
|
51
|
+
...fetchOptions
|
|
52
|
+
});
|
|
53
|
+
if (!response.ok) {
|
|
54
|
+
throw new Error('Network response was not OK');
|
|
55
|
+
}
|
|
56
|
+
const contentType = response.headers.get('content-type');
|
|
57
|
+
if (contentType && contentType.includes('application/json')) {
|
|
58
|
+
return response.json();
|
|
59
|
+
}
|
|
60
|
+
if (response && contentType) {
|
|
61
|
+
if (contentType.includes('application/xml')) {
|
|
62
|
+
return response.text();
|
|
63
|
+
}
|
|
64
|
+
return response.arrayBuffer();
|
|
65
|
+
}
|
|
66
|
+
} catch (err) {
|
|
67
|
+
return new VaildFetchError(500);
|
|
68
|
+
}
|
|
69
|
+
return new VaildFetchError(9999);
|
|
70
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
# JB2 图片渲染
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
/* Copyright 2012 Mozilla Foundation
|
|
2
|
+
*
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
// Table C-2
|
|
17
|
+
const QeTable = [
|
|
18
|
+
{ qe: 0x5601, nmps: 1, nlps: 1, switchFlag: 1 },
|
|
19
|
+
{ qe: 0x3401, nmps: 2, nlps: 6, switchFlag: 0 },
|
|
20
|
+
{ qe: 0x1801, nmps: 3, nlps: 9, switchFlag: 0 },
|
|
21
|
+
{ qe: 0x0ac1, nmps: 4, nlps: 12, switchFlag: 0 },
|
|
22
|
+
{ qe: 0x0521, nmps: 5, nlps: 29, switchFlag: 0 },
|
|
23
|
+
{ qe: 0x0221, nmps: 38, nlps: 33, switchFlag: 0 },
|
|
24
|
+
{ qe: 0x5601, nmps: 7, nlps: 6, switchFlag: 1 },
|
|
25
|
+
{ qe: 0x5401, nmps: 8, nlps: 14, switchFlag: 0 },
|
|
26
|
+
{ qe: 0x4801, nmps: 9, nlps: 14, switchFlag: 0 },
|
|
27
|
+
{ qe: 0x3801, nmps: 10, nlps: 14, switchFlag: 0 },
|
|
28
|
+
{ qe: 0x3001, nmps: 11, nlps: 17, switchFlag: 0 },
|
|
29
|
+
{ qe: 0x2401, nmps: 12, nlps: 18, switchFlag: 0 },
|
|
30
|
+
{ qe: 0x1c01, nmps: 13, nlps: 20, switchFlag: 0 },
|
|
31
|
+
{ qe: 0x1601, nmps: 29, nlps: 21, switchFlag: 0 },
|
|
32
|
+
{ qe: 0x5601, nmps: 15, nlps: 14, switchFlag: 1 },
|
|
33
|
+
{ qe: 0x5401, nmps: 16, nlps: 14, switchFlag: 0 },
|
|
34
|
+
{ qe: 0x5101, nmps: 17, nlps: 15, switchFlag: 0 },
|
|
35
|
+
{ qe: 0x4801, nmps: 18, nlps: 16, switchFlag: 0 },
|
|
36
|
+
{ qe: 0x3801, nmps: 19, nlps: 17, switchFlag: 0 },
|
|
37
|
+
{ qe: 0x3401, nmps: 20, nlps: 18, switchFlag: 0 },
|
|
38
|
+
{ qe: 0x3001, nmps: 21, nlps: 19, switchFlag: 0 },
|
|
39
|
+
{ qe: 0x2801, nmps: 22, nlps: 19, switchFlag: 0 },
|
|
40
|
+
{ qe: 0x2401, nmps: 23, nlps: 20, switchFlag: 0 },
|
|
41
|
+
{ qe: 0x2201, nmps: 24, nlps: 21, switchFlag: 0 },
|
|
42
|
+
{ qe: 0x1c01, nmps: 25, nlps: 22, switchFlag: 0 },
|
|
43
|
+
{ qe: 0x1801, nmps: 26, nlps: 23, switchFlag: 0 },
|
|
44
|
+
{ qe: 0x1601, nmps: 27, nlps: 24, switchFlag: 0 },
|
|
45
|
+
{ qe: 0x1401, nmps: 28, nlps: 25, switchFlag: 0 },
|
|
46
|
+
{ qe: 0x1201, nmps: 29, nlps: 26, switchFlag: 0 },
|
|
47
|
+
{ qe: 0x1101, nmps: 30, nlps: 27, switchFlag: 0 },
|
|
48
|
+
{ qe: 0x0ac1, nmps: 31, nlps: 28, switchFlag: 0 },
|
|
49
|
+
{ qe: 0x09c1, nmps: 32, nlps: 29, switchFlag: 0 },
|
|
50
|
+
{ qe: 0x08a1, nmps: 33, nlps: 30, switchFlag: 0 },
|
|
51
|
+
{ qe: 0x0521, nmps: 34, nlps: 31, switchFlag: 0 },
|
|
52
|
+
{ qe: 0x0441, nmps: 35, nlps: 32, switchFlag: 0 },
|
|
53
|
+
{ qe: 0x02a1, nmps: 36, nlps: 33, switchFlag: 0 },
|
|
54
|
+
{ qe: 0x0221, nmps: 37, nlps: 34, switchFlag: 0 },
|
|
55
|
+
{ qe: 0x0141, nmps: 38, nlps: 35, switchFlag: 0 },
|
|
56
|
+
{ qe: 0x0111, nmps: 39, nlps: 36, switchFlag: 0 },
|
|
57
|
+
{ qe: 0x0085, nmps: 40, nlps: 37, switchFlag: 0 },
|
|
58
|
+
{ qe: 0x0049, nmps: 41, nlps: 38, switchFlag: 0 },
|
|
59
|
+
{ qe: 0x0025, nmps: 42, nlps: 39, switchFlag: 0 },
|
|
60
|
+
{ qe: 0x0015, nmps: 43, nlps: 40, switchFlag: 0 },
|
|
61
|
+
{ qe: 0x0009, nmps: 44, nlps: 41, switchFlag: 0 },
|
|
62
|
+
{ qe: 0x0005, nmps: 45, nlps: 42, switchFlag: 0 },
|
|
63
|
+
{ qe: 0x0001, nmps: 45, nlps: 43, switchFlag: 0 },
|
|
64
|
+
{ qe: 0x5601, nmps: 46, nlps: 46, switchFlag: 0 }
|
|
65
|
+
];
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* This class implements the QM Coder decoding as defined in
|
|
69
|
+
* JPEG 2000 Part I Final Committee Draft Version 1.0
|
|
70
|
+
* Annex C.3 Arithmetic decoding procedure
|
|
71
|
+
* available at http://www.jpeg.org/public/fcd15444-1.pdf
|
|
72
|
+
*
|
|
73
|
+
* The arithmetic decoder is used in conjunction with context models to decode
|
|
74
|
+
* JPEG2000 and JBIG2 streams.
|
|
75
|
+
*/
|
|
76
|
+
class ArithmeticDecoder {
|
|
77
|
+
// C.3.5 Initialisation of the decoder (INITDEC)
|
|
78
|
+
constructor(data, start, end) {
|
|
79
|
+
this.data = data;
|
|
80
|
+
this.bp = start;
|
|
81
|
+
this.dataEnd = end;
|
|
82
|
+
|
|
83
|
+
this.chigh = data[start];
|
|
84
|
+
this.clow = 0;
|
|
85
|
+
|
|
86
|
+
this.byteIn();
|
|
87
|
+
|
|
88
|
+
this.chigh = ((this.chigh << 7) & 0xffff) | ((this.clow >> 9) & 0x7f);
|
|
89
|
+
this.clow = (this.clow << 7) & 0xffff;
|
|
90
|
+
// @ts-ignore
|
|
91
|
+
this.ct -= 7;
|
|
92
|
+
this.a = 0x8000;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// C.3.4 Compressed data input (BYTEIN)
|
|
96
|
+
byteIn() {
|
|
97
|
+
const data = this.data;
|
|
98
|
+
let bp = this.bp;
|
|
99
|
+
|
|
100
|
+
if (data[bp] === 0xff) {
|
|
101
|
+
if (data[bp + 1] > 0x8f) {
|
|
102
|
+
this.clow += 0xff00;
|
|
103
|
+
this.ct = 8;
|
|
104
|
+
} else {
|
|
105
|
+
bp++;
|
|
106
|
+
this.clow += data[bp] << 9;
|
|
107
|
+
this.ct = 7;
|
|
108
|
+
this.bp = bp;
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
bp++;
|
|
112
|
+
this.clow += bp < this.dataEnd ? data[bp] << 8 : 0xff00;
|
|
113
|
+
this.ct = 8;
|
|
114
|
+
this.bp = bp;
|
|
115
|
+
}
|
|
116
|
+
if (this.clow > 0xffff) {
|
|
117
|
+
this.chigh += this.clow >> 16;
|
|
118
|
+
this.clow &= 0xffff;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// C.3.2 Decoding a decision (DECODE)
|
|
123
|
+
readBit(contexts, pos) {
|
|
124
|
+
// Contexts are packed into 1 byte:
|
|
125
|
+
// highest 7 bits carry cx.index, lowest bit carries cx.mps
|
|
126
|
+
let cx_index = contexts[pos] >> 1,
|
|
127
|
+
cx_mps = contexts[pos] & 1;
|
|
128
|
+
const qeTableIcx = QeTable[cx_index];
|
|
129
|
+
const qeIcx = qeTableIcx.qe;
|
|
130
|
+
let d;
|
|
131
|
+
let a = this.a - qeIcx;
|
|
132
|
+
|
|
133
|
+
if (this.chigh < qeIcx) {
|
|
134
|
+
// exchangeLps
|
|
135
|
+
if (a < qeIcx) {
|
|
136
|
+
a = qeIcx;
|
|
137
|
+
d = cx_mps;
|
|
138
|
+
cx_index = qeTableIcx.nmps;
|
|
139
|
+
} else {
|
|
140
|
+
a = qeIcx;
|
|
141
|
+
d = 1 ^ cx_mps;
|
|
142
|
+
if (qeTableIcx.switchFlag === 1) {
|
|
143
|
+
cx_mps = d;
|
|
144
|
+
}
|
|
145
|
+
cx_index = qeTableIcx.nlps;
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
this.chigh -= qeIcx;
|
|
149
|
+
if ((a & 0x8000) !== 0) {
|
|
150
|
+
this.a = a;
|
|
151
|
+
return cx_mps;
|
|
152
|
+
}
|
|
153
|
+
// exchangeMps
|
|
154
|
+
if (a < qeIcx) {
|
|
155
|
+
d = 1 ^ cx_mps;
|
|
156
|
+
if (qeTableIcx.switchFlag === 1) {
|
|
157
|
+
cx_mps = d;
|
|
158
|
+
}
|
|
159
|
+
cx_index = qeTableIcx.nlps;
|
|
160
|
+
} else {
|
|
161
|
+
d = cx_mps;
|
|
162
|
+
cx_index = qeTableIcx.nmps;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
// C.3.3 renormD;
|
|
166
|
+
do {
|
|
167
|
+
if (this.ct === 0) {
|
|
168
|
+
this.byteIn();
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
a <<= 1;
|
|
172
|
+
this.chigh = ((this.chigh << 1) & 0xffff) | ((this.clow >> 15) & 1);
|
|
173
|
+
this.clow = (this.clow << 1) & 0xffff;
|
|
174
|
+
// @ts-ignore
|
|
175
|
+
this.ct--;
|
|
176
|
+
} while ((a & 0x8000) === 0);
|
|
177
|
+
this.a = a;
|
|
178
|
+
|
|
179
|
+
contexts[pos] = (cx_index << 1) | cx_mps;
|
|
180
|
+
return d;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
export { ArithmeticDecoder };
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
/* Copyright 2021 Mozilla Foundation
|
|
2
|
+
*
|
|
3
|
+
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
* you may not use this file except in compliance with the License.
|
|
5
|
+
* You may obtain a copy of the License at
|
|
6
|
+
*
|
|
7
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
*
|
|
9
|
+
* Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
* See the License for the specific language governing permissions and
|
|
13
|
+
* limitations under the License.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
import { bytesToString, shadow, unreachable } from './util.js';
|
|
17
|
+
|
|
18
|
+
class BaseStream {
|
|
19
|
+
constructor() {
|
|
20
|
+
if (this.constructor === BaseStream) {
|
|
21
|
+
unreachable('Cannot initialize BaseStream.');
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// eslint-disable-next-line getter-return
|
|
26
|
+
get length() {
|
|
27
|
+
return unreachable('Abstract getter `length` accessed');
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// eslint-disable-next-line getter-return
|
|
31
|
+
get isEmpty() {
|
|
32
|
+
return unreachable('Abstract getter `isEmpty` accessed');
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
get isDataLoaded() {
|
|
36
|
+
return shadow(this, 'isDataLoaded', true);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
getByte() {
|
|
40
|
+
return unreachable('Abstract method `getByte` called');
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
getBytes(length) {
|
|
44
|
+
unreachable('Abstract method `getBytes` called');
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
peekByte() {
|
|
48
|
+
const peekedByte = this.getByte();
|
|
49
|
+
// @ts-ignore
|
|
50
|
+
if (peekedByte !== -1) {
|
|
51
|
+
// @ts-ignore
|
|
52
|
+
this.pos--;
|
|
53
|
+
}
|
|
54
|
+
return peekedByte;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
peekBytes(length) {
|
|
58
|
+
const bytes = this.getBytes(length);
|
|
59
|
+
// @ts-ignore
|
|
60
|
+
this.pos -= bytes.length;
|
|
61
|
+
return bytes;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
getUint16() {
|
|
65
|
+
const b0 = this.getByte();
|
|
66
|
+
const b1 = this.getByte();
|
|
67
|
+
// @ts-ignore
|
|
68
|
+
if (b0 === -1 || b1 === -1) {
|
|
69
|
+
return -1;
|
|
70
|
+
}
|
|
71
|
+
// @ts-ignore
|
|
72
|
+
return (b0 << 8) + b1;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
getInt32() {
|
|
76
|
+
const b0 = this.getByte();
|
|
77
|
+
const b1 = this.getByte();
|
|
78
|
+
const b2 = this.getByte();
|
|
79
|
+
const b3 = this.getByte();
|
|
80
|
+
// @ts-ignore
|
|
81
|
+
return (b0 << 24) + (b1 << 16) + (b2 << 8) + b3;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
getByteRange(begin, end) {
|
|
85
|
+
unreachable('Abstract method `getByteRange` called');
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
getString(length) {
|
|
89
|
+
return bytesToString(this.getBytes(length));
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
skip(n) {
|
|
93
|
+
// @ts-ignore
|
|
94
|
+
this.pos += n || 1;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
reset() {
|
|
98
|
+
unreachable('Abstract method `reset` called');
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
moveStart() {
|
|
102
|
+
unreachable('Abstract method `moveStart` called');
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
makeSubStream(start, length, dict = null) {
|
|
106
|
+
unreachable('Abstract method `makeSubStream` called');
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* @returns {Array | null}
|
|
111
|
+
*/
|
|
112
|
+
getBaseStreams() {
|
|
113
|
+
return null;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
export { BaseStream };
|