pdfjs-dist 2.0.943 → 2.4.456
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.
Potentially problematic release.
This version of pdfjs-dist might be problematic. Click here for more details.
- package/CODE_OF_CONDUCT.md +15 -0
- package/README.md +4 -0
- package/bower.json +1 -1
- package/build/pdf.js +8286 -14230
- package/build/pdf.js.map +1 -1
- package/build/pdf.min.js +22 -1
- package/build/pdf.worker.entry.js +5 -3
- package/build/pdf.worker.js +25500 -26628
- package/build/pdf.worker.js.map +1 -1
- package/build/pdf.worker.min.js +22 -1
- package/es5/build/pdf.js +25473 -0
- package/es5/build/pdf.js.map +1 -0
- package/{lib/shared/global_scope.js → es5/build/pdf.worker.entry.js} +5 -10
- package/es5/build/pdf.worker.js +57878 -0
- package/es5/build/pdf.worker.js.map +1 -0
- package/es5/web/images/annotation-check.svg +11 -0
- package/es5/web/images/annotation-comment.svg +16 -0
- package/es5/web/images/annotation-help.svg +26 -0
- package/es5/web/images/annotation-insert.svg +10 -0
- package/es5/web/images/annotation-key.svg +11 -0
- package/es5/web/images/annotation-newparagraph.svg +11 -0
- package/es5/web/images/annotation-noicon.svg +7 -0
- package/es5/web/images/annotation-note.svg +42 -0
- package/es5/web/images/annotation-paragraph.svg +16 -0
- package/es5/web/images/loading-icon.gif +0 -0
- package/es5/web/images/shadow.png +0 -0
- package/es5/web/images/texture.png +0 -0
- package/es5/web/pdf_viewer.css +407 -0
- package/es5/web/pdf_viewer.js +7757 -0
- package/es5/web/pdf_viewer.js.map +1 -0
- package/image_decoders/pdf.image_decoders.js +2887 -4094
- package/image_decoders/pdf.image_decoders.js.map +1 -1
- package/image_decoders/pdf.image_decoders.min.js +22 -1
- package/lib/README.md +7 -0
- package/lib/core/annotation.js +855 -778
- package/lib/core/arithmetic_decoder.js +325 -311
- package/lib/core/bidi.js +117 -50
- package/lib/core/ccitt.js +251 -89
- package/lib/core/ccitt_stream.js +26 -16
- package/lib/core/cff_parser.js +525 -197
- package/lib/core/charsets.js +6 -5
- package/lib/core/chunked_stream.js +541 -406
- package/lib/core/cmap.js +368 -253
- package/lib/core/colorspace.js +781 -800
- package/lib/core/core_utils.js +152 -0
- package/lib/core/crypto.js +609 -422
- package/lib/core/document.js +649 -481
- package/lib/core/encodings.js +33 -24
- package/lib/core/evaluator.js +1471 -736
- package/lib/core/font_renderer.js +289 -149
- package/lib/core/fonts.js +1067 -413
- package/lib/core/function.js +517 -287
- package/lib/core/glyphlist.js +4529 -4527
- package/lib/core/image.js +232 -114
- package/lib/core/image_utils.js +94 -0
- package/lib/core/jbig2.js +711 -342
- package/lib/core/jbig2_stream.js +31 -19
- package/lib/core/jpeg_stream.js +151 -26
- package/lib/core/jpg.js +433 -181
- package/lib/core/jpx.js +551 -143
- package/lib/core/jpx_stream.js +40 -28
- package/lib/core/metrics.js +2931 -2931
- package/lib/core/murmurhash3.js +104 -97
- package/lib/core/obj.js +1561 -1053
- package/lib/core/operator_list.js +192 -64
- package/lib/core/parser.js +1162 -864
- package/lib/core/pattern.js +224 -75
- package/lib/core/pdf_manager.js +154 -285
- package/lib/core/primitives.js +145 -69
- package/lib/core/ps_parser.js +212 -162
- package/lib/core/standard_fonts.js +245 -244
- package/lib/core/stream.js +353 -81
- package/lib/core/type1_parser.js +218 -68
- package/lib/core/unicode.js +1682 -1655
- package/lib/core/worker.js +233 -302
- package/lib/core/worker_stream.js +168 -0
- package/lib/display/annotation_layer.js +808 -862
- package/lib/display/api.js +1778 -1462
- package/lib/display/api_compatibility.js +14 -9
- package/lib/display/canvas.js +463 -140
- package/lib/display/content_disposition.js +86 -58
- package/lib/display/display_utils.js +524 -0
- package/lib/display/fetch_stream.js +202 -274
- package/lib/display/font_loader.js +311 -333
- package/lib/display/metadata.js +98 -88
- package/lib/display/network.js +343 -347
- package/lib/display/network_utils.js +46 -26
- package/lib/display/node_stream.js +326 -404
- package/lib/display/pattern_helper.js +168 -69
- package/lib/display/svg.js +1296 -885
- package/lib/display/text_layer.js +229 -103
- package/lib/display/transport_stream.js +290 -250
- package/lib/display/webgl.js +116 -83
- package/lib/display/worker_options.js +6 -5
- package/lib/display/xml_parser.js +358 -337
- package/lib/examples/node/domstubs.js +95 -39
- package/lib/pdf.js +49 -31
- package/lib/pdf.worker.js +7 -5
- package/lib/shared/compatibility.js +3 -145
- package/lib/shared/is_node.js +8 -7
- package/lib/shared/message_handler.js +367 -314
- package/lib/shared/util.js +421 -415
- package/lib/test/unit/annotation_spec.js +1570 -690
- package/lib/test/unit/api_spec.js +855 -493
- package/lib/test/unit/bidi_spec.js +12 -12
- package/lib/test/unit/cff_parser_spec.js +88 -61
- package/lib/test/unit/clitests_helper.js +9 -12
- package/lib/test/unit/cmap_spec.js +140 -88
- package/lib/test/unit/colorspace_spec.js +204 -152
- package/lib/test/unit/core_utils_spec.js +211 -0
- package/lib/test/unit/crypto_spec.js +194 -182
- package/lib/test/unit/custom_spec.js +50 -64
- package/lib/test/unit/display_svg_spec.js +53 -38
- package/lib/test/unit/display_utils_spec.js +263 -0
- package/lib/test/unit/document_spec.js +17 -22
- package/lib/test/unit/encodings_spec.js +15 -57
- package/lib/test/unit/evaluator_spec.js +90 -83
- package/lib/test/unit/fetch_stream_spec.js +111 -0
- package/lib/test/unit/function_spec.js +219 -205
- package/lib/test/unit/jasmine-boot.js +68 -39
- package/lib/test/unit/message_handler_spec.js +187 -160
- package/lib/test/unit/metadata_spec.js +87 -34
- package/lib/test/unit/murmurhash3_spec.js +13 -13
- package/lib/test/unit/network_spec.js +26 -59
- package/lib/test/unit/network_utils_spec.js +187 -121
- package/lib/test/unit/node_stream_spec.js +98 -90
- package/lib/test/unit/parser_spec.js +173 -131
- package/lib/test/unit/pdf_find_controller_spec.js +148 -67
- package/lib/test/unit/pdf_find_utils_spec.js +35 -34
- package/lib/test/unit/pdf_history_spec.js +45 -33
- package/lib/test/unit/primitives_spec.js +161 -126
- package/lib/test/unit/stream_spec.js +22 -15
- package/lib/test/unit/test_utils.js +149 -98
- package/lib/test/unit/testreporter.js +36 -18
- package/lib/test/unit/type1_parser_spec.js +46 -44
- package/lib/test/unit/ui_utils_spec.js +388 -372
- package/lib/test/unit/unicode_spec.js +49 -46
- package/lib/test/unit/util_spec.js +144 -248
- package/lib/web/annotation_layer_builder.js +75 -95
- package/lib/web/app.js +1538 -1147
- package/lib/web/app_options.js +116 -104
- package/lib/web/base_viewer.js +950 -775
- package/lib/web/chromecom.js +217 -225
- package/lib/web/debugger.js +236 -148
- package/lib/web/download_manager.js +50 -50
- package/lib/web/firefox_print_service.js +51 -33
- package/lib/web/firefoxcom.js +225 -352
- package/lib/web/genericcom.js +30 -93
- package/lib/web/genericl10n.js +26 -143
- package/lib/web/grab_to_pan.js +57 -33
- package/lib/web/interfaces.js +105 -232
- package/lib/web/overlay_manager.js +73 -227
- package/lib/web/password_prompt.js +44 -62
- package/lib/web/pdf_attachment_viewer.js +118 -123
- package/lib/web/pdf_cursor_tools.js +89 -93
- package/lib/web/pdf_document_properties.js +242 -281
- package/lib/web/pdf_find_bar.js +157 -163
- package/lib/web/pdf_find_controller.js +598 -454
- package/lib/web/pdf_find_utils.js +32 -16
- package/lib/web/pdf_history.js +481 -355
- package/lib/web/pdf_link_service.js +355 -323
- package/lib/web/pdf_outline_viewer.js +167 -152
- package/lib/web/pdf_page_view.js +511 -457
- package/lib/web/pdf_presentation_mode.js +347 -335
- package/lib/web/pdf_print_service.js +133 -103
- package/lib/web/pdf_rendering_queue.js +98 -100
- package/lib/web/pdf_sidebar.js +323 -299
- package/lib/web/pdf_sidebar_resizer.js +107 -108
- package/lib/web/pdf_single_page_viewer.js +94 -146
- package/lib/web/pdf_thumbnail_view.js +319 -269
- package/lib/web/pdf_thumbnail_viewer.js +219 -199
- package/lib/web/pdf_viewer.component.js +111 -32
- package/lib/web/pdf_viewer.js +61 -101
- package/lib/web/preferences.js +87 -272
- package/lib/web/secondary_toolbar.js +207 -220
- package/lib/web/text_layer_builder.js +322 -322
- package/lib/web/toolbar.js +227 -180
- package/lib/web/ui_utils.js +476 -421
- package/lib/web/view_history.js +59 -208
- package/lib/web/viewer_compatibility.js +9 -6
- package/package.json +2 -9
- package/web/pdf_viewer.css +36 -22
- package/web/pdf_viewer.js +4407 -4516
- package/web/pdf_viewer.js.map +1 -1
- package/webpack.js +14 -5
- package/external/streams/streams-lib.js +0 -3962
- package/external/url/url-lib.js +0 -627
- package/lib/display/dom_utils.js +0 -441
- package/lib/shared/streams_polyfill.js +0 -39
- package/lib/shared/url_polyfill.js +0 -50
- package/lib/test/unit/dom_utils_spec.js +0 -89
- package/lib/web/dom_events.js +0 -140
package/lib/core/jbig2.js
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
* @licstart The following is the entire license notice for the
|
3
3
|
* Javascript code in this page
|
4
4
|
*
|
5
|
-
* Copyright
|
5
|
+
* Copyright 2020 Mozilla Foundation
|
6
6
|
*
|
7
7
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
8
8
|
* you may not use this file except in compliance with the License.
|
@@ -19,82 +19,106 @@
|
|
19
19
|
* @licend The above is the entire license notice for the
|
20
20
|
* Javascript code in this page
|
21
21
|
*/
|
22
|
-
|
22
|
+
"use strict";
|
23
23
|
|
24
24
|
Object.defineProperty(exports, "__esModule", {
|
25
25
|
value: true
|
26
26
|
});
|
27
|
-
exports.Jbig2Image =
|
27
|
+
exports.Jbig2Image = void 0;
|
28
28
|
|
29
|
-
var _util = require(
|
29
|
+
var _util = require("../shared/util.js");
|
30
30
|
|
31
|
-
var
|
31
|
+
var _core_utils = require("./core_utils.js");
|
32
32
|
|
33
|
-
var
|
33
|
+
var _arithmetic_decoder = require("./arithmetic_decoder.js");
|
34
34
|
|
35
|
-
var
|
36
|
-
|
37
|
-
|
35
|
+
var _ccitt = require("./ccitt.js");
|
36
|
+
|
37
|
+
class Jbig2Error extends _util.BaseException {
|
38
|
+
constructor(msg) {
|
39
|
+
super(`JBIG2 error: ${msg}`);
|
38
40
|
}
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
return Jbig2Error;
|
43
|
-
}();
|
41
|
+
|
42
|
+
}
|
43
|
+
|
44
44
|
var Jbig2Image = function Jbig2ImageClosure() {
|
45
45
|
function ContextCache() {}
|
46
|
+
|
46
47
|
ContextCache.prototype = {
|
47
|
-
getContexts
|
48
|
+
getContexts(id) {
|
48
49
|
if (id in this) {
|
49
50
|
return this[id];
|
50
51
|
}
|
52
|
+
|
51
53
|
return this[id] = new Int8Array(1 << 16);
|
52
54
|
}
|
55
|
+
|
53
56
|
};
|
57
|
+
|
54
58
|
function DecodingContext(data, start, end) {
|
55
59
|
this.data = data;
|
56
60
|
this.start = start;
|
57
61
|
this.end = end;
|
58
62
|
}
|
63
|
+
|
59
64
|
DecodingContext.prototype = {
|
60
65
|
get decoder() {
|
61
66
|
var decoder = new _arithmetic_decoder.ArithmeticDecoder(this.data, this.start, this.end);
|
62
|
-
return (0, _util.shadow)(this,
|
67
|
+
return (0, _util.shadow)(this, "decoder", decoder);
|
63
68
|
},
|
69
|
+
|
64
70
|
get contextCache() {
|
65
71
|
var cache = new ContextCache();
|
66
|
-
return (0, _util.shadow)(this,
|
72
|
+
return (0, _util.shadow)(this, "contextCache", cache);
|
67
73
|
}
|
74
|
+
|
68
75
|
};
|
76
|
+
|
69
77
|
function decodeInteger(contextCache, procedure, decoder) {
|
70
78
|
var contexts = contextCache.getContexts(procedure);
|
71
79
|
var prev = 1;
|
80
|
+
|
72
81
|
function readBits(length) {
|
73
82
|
var v = 0;
|
83
|
+
|
74
84
|
for (var i = 0; i < length; i++) {
|
75
85
|
var bit = decoder.readBit(contexts, prev);
|
76
86
|
prev = prev < 256 ? prev << 1 | bit : (prev << 1 | bit) & 511 | 256;
|
77
87
|
v = v << 1 | bit;
|
78
88
|
}
|
89
|
+
|
79
90
|
return v >>> 0;
|
80
91
|
}
|
92
|
+
|
81
93
|
var sign = readBits(1);
|
82
94
|
var value = readBits(1) ? readBits(1) ? readBits(1) ? readBits(1) ? readBits(1) ? readBits(32) + 4436 : readBits(12) + 340 : readBits(8) + 84 : readBits(6) + 20 : readBits(4) + 4 : readBits(2);
|
83
|
-
|
95
|
+
|
96
|
+
if (sign === 0) {
|
97
|
+
return value;
|
98
|
+
} else if (value > 0) {
|
99
|
+
return -value;
|
100
|
+
}
|
101
|
+
|
102
|
+
return null;
|
84
103
|
}
|
104
|
+
|
85
105
|
function decodeIAID(contextCache, decoder, codeLength) {
|
86
|
-
var contexts = contextCache.getContexts(
|
106
|
+
var contexts = contextCache.getContexts("IAID");
|
87
107
|
var prev = 1;
|
108
|
+
|
88
109
|
for (var i = 0; i < codeLength; i++) {
|
89
110
|
var bit = decoder.readBit(contexts, prev);
|
90
111
|
prev = prev << 1 | bit;
|
91
112
|
}
|
113
|
+
|
92
114
|
if (codeLength < 31) {
|
93
115
|
return prev & (1 << codeLength) - 1;
|
94
116
|
}
|
95
|
-
|
117
|
+
|
118
|
+
return prev & 0x7fffffff;
|
96
119
|
}
|
97
|
-
|
120
|
+
|
121
|
+
var SegmentTypes = ["SymbolDictionary", null, null, null, "IntermediateTextRegion", null, "ImmediateTextRegion", "ImmediateLosslessTextRegion", null, null, null, null, null, null, null, null, "PatternDictionary", null, null, null, "IntermediateHalftoneRegion", null, "ImmediateHalftoneRegion", "ImmediateLosslessHalftoneRegion", null, null, null, null, null, null, null, null, null, null, null, null, "IntermediateGenericRegion", null, "ImmediateGenericRegion", "ImmediateLosslessGenericRegion", "IntermediateGenericRefinementRegion", null, "ImmediateGenericRefinementRegion", "ImmediateLosslessGenericRefinementRegion", null, null, null, null, "PageInformation", "EndOfPage", "EndOfStripe", "EndOfFile", "Profiles", "Tables", null, null, null, null, null, null, null, null, "Extension"];
|
98
122
|
var CodingTemplates = [[{
|
99
123
|
x: -1,
|
100
124
|
y: -2
|
@@ -292,11 +316,12 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
292
316
|
y: 1
|
293
317
|
}]
|
294
318
|
}];
|
295
|
-
var ReusedContexts = [
|
319
|
+
var ReusedContexts = [0x9b25, 0x0795, 0x00e5, 0x0195];
|
296
320
|
var RefinementReusedContexts = [0x0020, 0x0008];
|
321
|
+
|
297
322
|
function decodeBitmapTemplate0(width, height, decodingContext) {
|
298
323
|
var decoder = decodingContext.decoder;
|
299
|
-
var contexts = decodingContext.contextCache.getContexts(
|
324
|
+
var contexts = decodingContext.contextCache.getContexts("GB");
|
300
325
|
var contextLabel,
|
301
326
|
i,
|
302
327
|
j,
|
@@ -305,27 +330,33 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
305
330
|
row1,
|
306
331
|
row2,
|
307
332
|
bitmap = [];
|
308
|
-
var OLD_PIXEL_MASK =
|
333
|
+
var OLD_PIXEL_MASK = 0x7bf7;
|
334
|
+
|
309
335
|
for (i = 0; i < height; i++) {
|
310
336
|
row = bitmap[i] = new Uint8Array(width);
|
311
337
|
row1 = i < 1 ? row : bitmap[i - 1];
|
312
338
|
row2 = i < 2 ? row : bitmap[i - 2];
|
313
339
|
contextLabel = row2[0] << 13 | row2[1] << 12 | row2[2] << 11 | row1[0] << 7 | row1[1] << 6 | row1[2] << 5 | row1[3] << 4;
|
340
|
+
|
314
341
|
for (j = 0; j < width; j++) {
|
315
342
|
row[j] = pixel = decoder.readBit(contexts, contextLabel);
|
316
343
|
contextLabel = (contextLabel & OLD_PIXEL_MASK) << 1 | (j + 3 < width ? row2[j + 3] << 11 : 0) | (j + 4 < width ? row1[j + 4] << 4 : 0) | pixel;
|
317
344
|
}
|
318
345
|
}
|
346
|
+
|
319
347
|
return bitmap;
|
320
348
|
}
|
349
|
+
|
321
350
|
function decodeBitmap(mmr, width, height, templateIndex, prediction, skip, at, decodingContext) {
|
322
351
|
if (mmr) {
|
323
|
-
|
352
|
+
const input = new Reader(decodingContext.data, decodingContext.start, decodingContext.end);
|
324
353
|
return decodeMMRBitmap(input, width, height, false);
|
325
354
|
}
|
355
|
+
|
326
356
|
if (templateIndex === 0 && !skip && !prediction && at.length === 4 && at[0].x === 3 && at[0].y === -1 && at[1].x === -3 && at[1].y === -1 && at[2].x === 2 && at[2].y === -2 && at[3].x === -2 && at[3].y === -2) {
|
327
357
|
return decodeBitmapTemplate0(width, height, decodingContext);
|
328
358
|
}
|
359
|
+
|
329
360
|
var useskip = !!skip;
|
330
361
|
var template = CodingTemplates[templateIndex].concat(at);
|
331
362
|
template.sort(function (a, b) {
|
@@ -340,28 +371,33 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
340
371
|
maxX = 0,
|
341
372
|
minY = 0;
|
342
373
|
var c, k;
|
374
|
+
|
343
375
|
for (k = 0; k < templateLength; k++) {
|
344
376
|
templateX[k] = template[k].x;
|
345
377
|
templateY[k] = template[k].y;
|
346
378
|
minX = Math.min(minX, template[k].x);
|
347
379
|
maxX = Math.max(maxX, template[k].x);
|
348
380
|
minY = Math.min(minY, template[k].y);
|
381
|
+
|
349
382
|
if (k < templateLength - 1 && template[k].y === template[k + 1].y && template[k].x === template[k + 1].x - 1) {
|
350
383
|
reuseMask |= 1 << templateLength - 1 - k;
|
351
384
|
} else {
|
352
385
|
changingTemplateEntries.push(k);
|
353
386
|
}
|
354
387
|
}
|
388
|
+
|
355
389
|
var changingEntriesLength = changingTemplateEntries.length;
|
356
390
|
var changingTemplateX = new Int8Array(changingEntriesLength);
|
357
391
|
var changingTemplateY = new Int8Array(changingEntriesLength);
|
358
392
|
var changingTemplateBit = new Uint16Array(changingEntriesLength);
|
393
|
+
|
359
394
|
for (c = 0; c < changingEntriesLength; c++) {
|
360
395
|
k = changingTemplateEntries[c];
|
361
396
|
changingTemplateX[c] = template[k].x;
|
362
397
|
changingTemplateY[c] = template[k].y;
|
363
398
|
changingTemplateBit[c] = 1 << templateLength - 1 - k;
|
364
399
|
}
|
400
|
+
|
365
401
|
var sbb_left = -minX;
|
366
402
|
var sbb_top = -minY;
|
367
403
|
var sbb_right = width - maxX;
|
@@ -369,7 +405,7 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
369
405
|
var row = new Uint8Array(width);
|
370
406
|
var bitmap = [];
|
371
407
|
var decoder = decodingContext.decoder;
|
372
|
-
var contexts = decodingContext.contextCache.getContexts(
|
408
|
+
var contexts = decodingContext.contextCache.getContexts("GB");
|
373
409
|
var ltp = 0,
|
374
410
|
j,
|
375
411
|
i0,
|
@@ -377,28 +413,35 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
377
413
|
contextLabel = 0,
|
378
414
|
bit,
|
379
415
|
shift;
|
416
|
+
|
380
417
|
for (var i = 0; i < height; i++) {
|
381
418
|
if (prediction) {
|
382
419
|
var sltp = decoder.readBit(contexts, pseudoPixelContext);
|
383
420
|
ltp ^= sltp;
|
421
|
+
|
384
422
|
if (ltp) {
|
385
423
|
bitmap.push(row);
|
386
424
|
continue;
|
387
425
|
}
|
388
426
|
}
|
427
|
+
|
389
428
|
row = new Uint8Array(row);
|
390
429
|
bitmap.push(row);
|
430
|
+
|
391
431
|
for (j = 0; j < width; j++) {
|
392
432
|
if (useskip && skip[i][j]) {
|
393
433
|
row[j] = 0;
|
394
434
|
continue;
|
395
435
|
}
|
436
|
+
|
396
437
|
if (j >= sbb_left && j < sbb_right && i >= sbb_top) {
|
397
438
|
contextLabel = contextLabel << 1 & reuseMask;
|
439
|
+
|
398
440
|
for (k = 0; k < changingEntriesLength; k++) {
|
399
441
|
i0 = i + changingTemplateY[k];
|
400
442
|
j0 = j + changingTemplateX[k];
|
401
443
|
bit = bitmap[i0][j0];
|
444
|
+
|
402
445
|
if (bit) {
|
403
446
|
bit = changingTemplateBit[k];
|
404
447
|
contextLabel |= bit;
|
@@ -407,12 +450,16 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
407
450
|
} else {
|
408
451
|
contextLabel = 0;
|
409
452
|
shift = templateLength - 1;
|
453
|
+
|
410
454
|
for (k = 0; k < templateLength; k++, shift--) {
|
411
455
|
j0 = j + templateX[k];
|
456
|
+
|
412
457
|
if (j0 >= 0 && j0 < width) {
|
413
458
|
i0 = i + templateY[k];
|
459
|
+
|
414
460
|
if (i0 >= 0) {
|
415
461
|
bit = bitmap[i0][j0];
|
462
|
+
|
416
463
|
if (bit) {
|
417
464
|
contextLabel |= bit << shift;
|
418
465
|
}
|
@@ -420,121 +467,151 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
420
467
|
}
|
421
468
|
}
|
422
469
|
}
|
470
|
+
|
423
471
|
var pixel = decoder.readBit(contexts, contextLabel);
|
424
472
|
row[j] = pixel;
|
425
473
|
}
|
426
474
|
}
|
475
|
+
|
427
476
|
return bitmap;
|
428
477
|
}
|
478
|
+
|
429
479
|
function decodeRefinement(width, height, templateIndex, referenceBitmap, offsetX, offsetY, prediction, at, decodingContext) {
|
430
480
|
var codingTemplate = RefinementTemplates[templateIndex].coding;
|
481
|
+
|
431
482
|
if (templateIndex === 0) {
|
432
483
|
codingTemplate = codingTemplate.concat([at[0]]);
|
433
484
|
}
|
485
|
+
|
434
486
|
var codingTemplateLength = codingTemplate.length;
|
435
487
|
var codingTemplateX = new Int32Array(codingTemplateLength);
|
436
488
|
var codingTemplateY = new Int32Array(codingTemplateLength);
|
437
489
|
var k;
|
490
|
+
|
438
491
|
for (k = 0; k < codingTemplateLength; k++) {
|
439
492
|
codingTemplateX[k] = codingTemplate[k].x;
|
440
493
|
codingTemplateY[k] = codingTemplate[k].y;
|
441
494
|
}
|
495
|
+
|
442
496
|
var referenceTemplate = RefinementTemplates[templateIndex].reference;
|
497
|
+
|
443
498
|
if (templateIndex === 0) {
|
444
499
|
referenceTemplate = referenceTemplate.concat([at[1]]);
|
445
500
|
}
|
501
|
+
|
446
502
|
var referenceTemplateLength = referenceTemplate.length;
|
447
503
|
var referenceTemplateX = new Int32Array(referenceTemplateLength);
|
448
504
|
var referenceTemplateY = new Int32Array(referenceTemplateLength);
|
505
|
+
|
449
506
|
for (k = 0; k < referenceTemplateLength; k++) {
|
450
507
|
referenceTemplateX[k] = referenceTemplate[k].x;
|
451
508
|
referenceTemplateY[k] = referenceTemplate[k].y;
|
452
509
|
}
|
510
|
+
|
453
511
|
var referenceWidth = referenceBitmap[0].length;
|
454
512
|
var referenceHeight = referenceBitmap.length;
|
455
513
|
var pseudoPixelContext = RefinementReusedContexts[templateIndex];
|
456
514
|
var bitmap = [];
|
457
515
|
var decoder = decodingContext.decoder;
|
458
|
-
var contexts = decodingContext.contextCache.getContexts(
|
516
|
+
var contexts = decodingContext.contextCache.getContexts("GR");
|
459
517
|
var ltp = 0;
|
518
|
+
|
460
519
|
for (var i = 0; i < height; i++) {
|
461
520
|
if (prediction) {
|
462
521
|
var sltp = decoder.readBit(contexts, pseudoPixelContext);
|
463
522
|
ltp ^= sltp;
|
523
|
+
|
464
524
|
if (ltp) {
|
465
|
-
throw new Jbig2Error(
|
525
|
+
throw new Jbig2Error("prediction is not supported");
|
466
526
|
}
|
467
527
|
}
|
528
|
+
|
468
529
|
var row = new Uint8Array(width);
|
469
530
|
bitmap.push(row);
|
531
|
+
|
470
532
|
for (var j = 0; j < width; j++) {
|
471
533
|
var i0, j0;
|
472
534
|
var contextLabel = 0;
|
535
|
+
|
473
536
|
for (k = 0; k < codingTemplateLength; k++) {
|
474
537
|
i0 = i + codingTemplateY[k];
|
475
538
|
j0 = j + codingTemplateX[k];
|
539
|
+
|
476
540
|
if (i0 < 0 || j0 < 0 || j0 >= width) {
|
477
541
|
contextLabel <<= 1;
|
478
542
|
} else {
|
479
543
|
contextLabel = contextLabel << 1 | bitmap[i0][j0];
|
480
544
|
}
|
481
545
|
}
|
546
|
+
|
482
547
|
for (k = 0; k < referenceTemplateLength; k++) {
|
483
548
|
i0 = i + referenceTemplateY[k] - offsetY;
|
484
549
|
j0 = j + referenceTemplateX[k] - offsetX;
|
550
|
+
|
485
551
|
if (i0 < 0 || i0 >= referenceHeight || j0 < 0 || j0 >= referenceWidth) {
|
486
552
|
contextLabel <<= 1;
|
487
553
|
} else {
|
488
554
|
contextLabel = contextLabel << 1 | referenceBitmap[i0][j0];
|
489
555
|
}
|
490
556
|
}
|
557
|
+
|
491
558
|
var pixel = decoder.readBit(contexts, contextLabel);
|
492
559
|
row[j] = pixel;
|
493
560
|
}
|
494
561
|
}
|
562
|
+
|
495
563
|
return bitmap;
|
496
564
|
}
|
565
|
+
|
497
566
|
function decodeSymbolDictionary(huffman, refinement, symbols, numberOfNewSymbols, numberOfExportedSymbols, huffmanTables, templateIndex, at, refinementTemplateIndex, refinementAt, decodingContext, huffmanInput) {
|
498
567
|
if (huffman && refinement) {
|
499
|
-
throw new Jbig2Error(
|
568
|
+
throw new Jbig2Error("symbol refinement with Huffman is not supported");
|
500
569
|
}
|
570
|
+
|
501
571
|
var newSymbols = [];
|
502
572
|
var currentHeight = 0;
|
503
|
-
var symbolCodeLength = (0,
|
573
|
+
var symbolCodeLength = (0, _core_utils.log2)(symbols.length + numberOfNewSymbols);
|
504
574
|
var decoder = decodingContext.decoder;
|
505
575
|
var contextCache = decodingContext.contextCache;
|
506
|
-
|
507
|
-
|
576
|
+
let tableB1, symbolWidths;
|
577
|
+
|
508
578
|
if (huffman) {
|
509
579
|
tableB1 = getStandardTable(1);
|
510
580
|
symbolWidths = [];
|
511
581
|
symbolCodeLength = Math.max(symbolCodeLength, 1);
|
512
582
|
}
|
583
|
+
|
513
584
|
while (newSymbols.length < numberOfNewSymbols) {
|
514
|
-
var deltaHeight = huffman ? huffmanTables.tableDeltaHeight.decode(huffmanInput) : decodeInteger(contextCache,
|
585
|
+
var deltaHeight = huffman ? huffmanTables.tableDeltaHeight.decode(huffmanInput) : decodeInteger(contextCache, "IADH", decoder);
|
515
586
|
currentHeight += deltaHeight;
|
516
|
-
|
587
|
+
let currentWidth = 0,
|
517
588
|
totalWidth = 0;
|
518
|
-
|
589
|
+
const firstSymbol = huffman ? symbolWidths.length : 0;
|
590
|
+
|
519
591
|
while (true) {
|
520
|
-
var deltaWidth = huffman ? huffmanTables.tableDeltaWidth.decode(huffmanInput) : decodeInteger(contextCache,
|
592
|
+
var deltaWidth = huffman ? huffmanTables.tableDeltaWidth.decode(huffmanInput) : decodeInteger(contextCache, "IADW", decoder);
|
593
|
+
|
521
594
|
if (deltaWidth === null) {
|
522
595
|
break;
|
523
596
|
}
|
597
|
+
|
524
598
|
currentWidth += deltaWidth;
|
525
599
|
totalWidth += currentWidth;
|
526
600
|
var bitmap;
|
601
|
+
|
527
602
|
if (refinement) {
|
528
|
-
var numberOfInstances = decodeInteger(contextCache,
|
603
|
+
var numberOfInstances = decodeInteger(contextCache, "IAAI", decoder);
|
604
|
+
|
529
605
|
if (numberOfInstances > 1) {
|
530
606
|
bitmap = decodeTextRegion(huffman, refinement, currentWidth, currentHeight, 0, numberOfInstances, 1, symbols.concat(newSymbols), symbolCodeLength, 0, 0, 1, 0, huffmanTables, refinementTemplateIndex, refinementAt, decodingContext, 0, huffmanInput);
|
531
607
|
} else {
|
532
608
|
var symbolId = decodeIAID(contextCache, decoder, symbolCodeLength);
|
533
|
-
var rdx = decodeInteger(contextCache,
|
534
|
-
var rdy = decodeInteger(contextCache,
|
609
|
+
var rdx = decodeInteger(contextCache, "IARDX", decoder);
|
610
|
+
var rdy = decodeInteger(contextCache, "IARDY", decoder);
|
535
611
|
var symbol = symbolId < symbols.length ? symbols[symbolId] : newSymbols[symbolId - symbols.length];
|
536
612
|
bitmap = decodeRefinement(currentWidth, currentHeight, refinementTemplateIndex, symbol, rdx, rdy, false, refinementAt, decodingContext);
|
537
613
|
}
|
614
|
+
|
538
615
|
newSymbols.push(bitmap);
|
539
616
|
} else if (huffman) {
|
540
617
|
symbolWidths.push(currentWidth);
|
@@ -543,180 +620,230 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
543
620
|
newSymbols.push(bitmap);
|
544
621
|
}
|
545
622
|
}
|
623
|
+
|
546
624
|
if (huffman && !refinement) {
|
547
|
-
|
625
|
+
const bitmapSize = huffmanTables.tableBitmapSize.decode(huffmanInput);
|
548
626
|
huffmanInput.byteAlign();
|
549
|
-
|
627
|
+
let collectiveBitmap;
|
628
|
+
|
550
629
|
if (bitmapSize === 0) {
|
551
630
|
collectiveBitmap = readUncompressedBitmap(huffmanInput, totalWidth, currentHeight);
|
552
631
|
} else {
|
553
|
-
|
554
|
-
|
632
|
+
const originalEnd = huffmanInput.end;
|
633
|
+
const bitmapEnd = huffmanInput.position + bitmapSize;
|
555
634
|
huffmanInput.end = bitmapEnd;
|
556
635
|
collectiveBitmap = decodeMMRBitmap(huffmanInput, totalWidth, currentHeight, false);
|
557
636
|
huffmanInput.end = originalEnd;
|
558
637
|
huffmanInput.position = bitmapEnd;
|
559
638
|
}
|
560
|
-
|
639
|
+
|
640
|
+
const numberOfSymbolsDecoded = symbolWidths.length;
|
641
|
+
|
561
642
|
if (firstSymbol === numberOfSymbolsDecoded - 1) {
|
562
643
|
newSymbols.push(collectiveBitmap);
|
563
644
|
} else {
|
564
|
-
|
565
|
-
y
|
645
|
+
let i,
|
646
|
+
y,
|
566
647
|
xMin = 0,
|
567
|
-
xMax
|
568
|
-
bitmapWidth
|
569
|
-
symbolBitmap
|
570
|
-
|
571
|
-
|
648
|
+
xMax,
|
649
|
+
bitmapWidth,
|
650
|
+
symbolBitmap;
|
651
|
+
|
652
|
+
for (i = firstSymbol; i < numberOfSymbolsDecoded; i++) {
|
653
|
+
bitmapWidth = symbolWidths[i];
|
572
654
|
xMax = xMin + bitmapWidth;
|
573
655
|
symbolBitmap = [];
|
656
|
+
|
574
657
|
for (y = 0; y < currentHeight; y++) {
|
575
658
|
symbolBitmap.push(collectiveBitmap[y].subarray(xMin, xMax));
|
576
659
|
}
|
660
|
+
|
577
661
|
newSymbols.push(symbolBitmap);
|
578
662
|
xMin = xMax;
|
579
663
|
}
|
580
664
|
}
|
581
665
|
}
|
582
666
|
}
|
667
|
+
|
583
668
|
var exportedSymbols = [];
|
584
669
|
var flags = [],
|
585
670
|
currentFlag = false;
|
586
671
|
var totalSymbolsLength = symbols.length + numberOfNewSymbols;
|
672
|
+
|
587
673
|
while (flags.length < totalSymbolsLength) {
|
588
|
-
var runLength = huffman ? tableB1.decode(huffmanInput) : decodeInteger(contextCache,
|
674
|
+
var runLength = huffman ? tableB1.decode(huffmanInput) : decodeInteger(contextCache, "IAEX", decoder);
|
675
|
+
|
589
676
|
while (runLength--) {
|
590
677
|
flags.push(currentFlag);
|
591
678
|
}
|
679
|
+
|
592
680
|
currentFlag = !currentFlag;
|
593
681
|
}
|
682
|
+
|
594
683
|
for (var i = 0, ii = symbols.length; i < ii; i++) {
|
595
684
|
if (flags[i]) {
|
596
685
|
exportedSymbols.push(symbols[i]);
|
597
686
|
}
|
598
687
|
}
|
688
|
+
|
599
689
|
for (var j = 0; j < numberOfNewSymbols; i++, j++) {
|
600
690
|
if (flags[i]) {
|
601
691
|
exportedSymbols.push(newSymbols[j]);
|
602
692
|
}
|
603
693
|
}
|
694
|
+
|
604
695
|
return exportedSymbols;
|
605
696
|
}
|
697
|
+
|
606
698
|
function decodeTextRegion(huffman, refinement, width, height, defaultPixelValue, numberOfSymbolInstances, stripSize, inputSymbols, symbolCodeLength, transposed, dsOffset, referenceCorner, combinationOperator, huffmanTables, refinementTemplateIndex, refinementAt, decodingContext, logStripSize, huffmanInput) {
|
607
699
|
if (huffman && refinement) {
|
608
|
-
throw new Jbig2Error(
|
700
|
+
throw new Jbig2Error("refinement with Huffman is not supported");
|
609
701
|
}
|
702
|
+
|
610
703
|
var bitmap = [];
|
611
704
|
var i, row;
|
705
|
+
|
612
706
|
for (i = 0; i < height; i++) {
|
613
707
|
row = new Uint8Array(width);
|
708
|
+
|
614
709
|
if (defaultPixelValue) {
|
615
710
|
for (var j = 0; j < width; j++) {
|
616
711
|
row[j] = defaultPixelValue;
|
617
712
|
}
|
618
713
|
}
|
714
|
+
|
619
715
|
bitmap.push(row);
|
620
716
|
}
|
717
|
+
|
621
718
|
var decoder = decodingContext.decoder;
|
622
719
|
var contextCache = decodingContext.contextCache;
|
623
|
-
var stripT = huffman ? -huffmanTables.tableDeltaT.decode(huffmanInput) : -decodeInteger(contextCache,
|
720
|
+
var stripT = huffman ? -huffmanTables.tableDeltaT.decode(huffmanInput) : -decodeInteger(contextCache, "IADT", decoder);
|
624
721
|
var firstS = 0;
|
625
722
|
i = 0;
|
723
|
+
|
626
724
|
while (i < numberOfSymbolInstances) {
|
627
|
-
var deltaT = huffman ? huffmanTables.tableDeltaT.decode(huffmanInput) : decodeInteger(contextCache,
|
725
|
+
var deltaT = huffman ? huffmanTables.tableDeltaT.decode(huffmanInput) : decodeInteger(contextCache, "IADT", decoder);
|
628
726
|
stripT += deltaT;
|
629
|
-
var deltaFirstS = huffman ? huffmanTables.tableFirstS.decode(huffmanInput) : decodeInteger(contextCache,
|
727
|
+
var deltaFirstS = huffman ? huffmanTables.tableFirstS.decode(huffmanInput) : decodeInteger(contextCache, "IAFS", decoder);
|
630
728
|
firstS += deltaFirstS;
|
631
729
|
var currentS = firstS;
|
730
|
+
|
632
731
|
do {
|
633
|
-
|
732
|
+
let currentT = 0;
|
733
|
+
|
634
734
|
if (stripSize > 1) {
|
635
|
-
currentT = huffman ? huffmanInput.readBits(logStripSize) : decodeInteger(contextCache,
|
735
|
+
currentT = huffman ? huffmanInput.readBits(logStripSize) : decodeInteger(contextCache, "IAIT", decoder);
|
636
736
|
}
|
737
|
+
|
637
738
|
var t = stripSize * stripT + currentT;
|
638
739
|
var symbolId = huffman ? huffmanTables.symbolIDTable.decode(huffmanInput) : decodeIAID(contextCache, decoder, symbolCodeLength);
|
639
|
-
var applyRefinement = refinement && (huffman ? huffmanInput.readBit() : decodeInteger(contextCache,
|
740
|
+
var applyRefinement = refinement && (huffman ? huffmanInput.readBit() : decodeInteger(contextCache, "IARI", decoder));
|
640
741
|
var symbolBitmap = inputSymbols[symbolId];
|
641
742
|
var symbolWidth = symbolBitmap[0].length;
|
642
743
|
var symbolHeight = symbolBitmap.length;
|
744
|
+
|
643
745
|
if (applyRefinement) {
|
644
|
-
var rdw = decodeInteger(contextCache,
|
645
|
-
var rdh = decodeInteger(contextCache,
|
646
|
-
var rdx = decodeInteger(contextCache,
|
647
|
-
var rdy = decodeInteger(contextCache,
|
746
|
+
var rdw = decodeInteger(contextCache, "IARDW", decoder);
|
747
|
+
var rdh = decodeInteger(contextCache, "IARDH", decoder);
|
748
|
+
var rdx = decodeInteger(contextCache, "IARDX", decoder);
|
749
|
+
var rdy = decodeInteger(contextCache, "IARDY", decoder);
|
648
750
|
symbolWidth += rdw;
|
649
751
|
symbolHeight += rdh;
|
650
752
|
symbolBitmap = decodeRefinement(symbolWidth, symbolHeight, refinementTemplateIndex, symbolBitmap, (rdw >> 1) + rdx, (rdh >> 1) + rdy, false, refinementAt, decodingContext);
|
651
753
|
}
|
754
|
+
|
652
755
|
var offsetT = t - (referenceCorner & 1 ? 0 : symbolHeight - 1);
|
653
756
|
var offsetS = currentS - (referenceCorner & 2 ? symbolWidth - 1 : 0);
|
654
757
|
var s2, t2, symbolRow;
|
758
|
+
|
655
759
|
if (transposed) {
|
656
760
|
for (s2 = 0; s2 < symbolHeight; s2++) {
|
657
761
|
row = bitmap[offsetS + s2];
|
762
|
+
|
658
763
|
if (!row) {
|
659
764
|
continue;
|
660
765
|
}
|
766
|
+
|
661
767
|
symbolRow = symbolBitmap[s2];
|
662
768
|
var maxWidth = Math.min(width - offsetT, symbolWidth);
|
769
|
+
|
663
770
|
switch (combinationOperator) {
|
664
771
|
case 0:
|
665
772
|
for (t2 = 0; t2 < maxWidth; t2++) {
|
666
773
|
row[offsetT + t2] |= symbolRow[t2];
|
667
774
|
}
|
775
|
+
|
668
776
|
break;
|
777
|
+
|
669
778
|
case 2:
|
670
779
|
for (t2 = 0; t2 < maxWidth; t2++) {
|
671
780
|
row[offsetT + t2] ^= symbolRow[t2];
|
672
781
|
}
|
782
|
+
|
673
783
|
break;
|
784
|
+
|
674
785
|
default:
|
675
|
-
throw new Jbig2Error(
|
786
|
+
throw new Jbig2Error(`operator ${combinationOperator} is not supported`);
|
676
787
|
}
|
677
788
|
}
|
789
|
+
|
678
790
|
currentS += symbolHeight - 1;
|
679
791
|
} else {
|
680
792
|
for (t2 = 0; t2 < symbolHeight; t2++) {
|
681
793
|
row = bitmap[offsetT + t2];
|
794
|
+
|
682
795
|
if (!row) {
|
683
796
|
continue;
|
684
797
|
}
|
798
|
+
|
685
799
|
symbolRow = symbolBitmap[t2];
|
800
|
+
|
686
801
|
switch (combinationOperator) {
|
687
802
|
case 0:
|
688
803
|
for (s2 = 0; s2 < symbolWidth; s2++) {
|
689
804
|
row[offsetS + s2] |= symbolRow[s2];
|
690
805
|
}
|
806
|
+
|
691
807
|
break;
|
808
|
+
|
692
809
|
case 2:
|
693
810
|
for (s2 = 0; s2 < symbolWidth; s2++) {
|
694
811
|
row[offsetS + s2] ^= symbolRow[s2];
|
695
812
|
}
|
813
|
+
|
696
814
|
break;
|
815
|
+
|
697
816
|
default:
|
698
|
-
throw new Jbig2Error(
|
817
|
+
throw new Jbig2Error(`operator ${combinationOperator} is not supported`);
|
699
818
|
}
|
700
819
|
}
|
820
|
+
|
701
821
|
currentS += symbolWidth - 1;
|
702
822
|
}
|
823
|
+
|
703
824
|
i++;
|
704
|
-
var deltaS = huffman ? huffmanTables.tableDeltaS.decode(huffmanInput) : decodeInteger(contextCache,
|
825
|
+
var deltaS = huffman ? huffmanTables.tableDeltaS.decode(huffmanInput) : decodeInteger(contextCache, "IADS", decoder);
|
826
|
+
|
705
827
|
if (deltaS === null) {
|
706
828
|
break;
|
707
829
|
}
|
830
|
+
|
708
831
|
currentS += deltaS + dsOffset;
|
709
832
|
} while (true);
|
710
833
|
}
|
834
|
+
|
711
835
|
return bitmap;
|
712
836
|
}
|
837
|
+
|
713
838
|
function decodePatternDictionary(mmr, patternWidth, patternHeight, maxPatternIndex, template, decodingContext) {
|
714
|
-
|
839
|
+
const at = [];
|
840
|
+
|
715
841
|
if (!mmr) {
|
716
842
|
at.push({
|
717
843
|
x: -patternWidth,
|
718
844
|
y: 0
|
719
845
|
});
|
846
|
+
|
720
847
|
if (template === 0) {
|
721
848
|
at.push({
|
722
849
|
x: -3,
|
@@ -732,58 +859,65 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
732
859
|
});
|
733
860
|
}
|
734
861
|
}
|
735
|
-
|
736
|
-
|
737
|
-
|
738
|
-
|
739
|
-
|
740
|
-
|
741
|
-
|
742
|
-
|
743
|
-
|
744
|
-
|
745
|
-
|
746
|
-
xMax = xMin + patternWidth;
|
747
|
-
for (y = 0; y < patternHeight; y++) {
|
862
|
+
|
863
|
+
const collectiveWidth = (maxPatternIndex + 1) * patternWidth;
|
864
|
+
const collectiveBitmap = decodeBitmap(mmr, collectiveWidth, patternHeight, template, false, null, at, decodingContext);
|
865
|
+
const patterns = [];
|
866
|
+
|
867
|
+
for (let i = 0; i <= maxPatternIndex; i++) {
|
868
|
+
const patternBitmap = [];
|
869
|
+
const xMin = patternWidth * i;
|
870
|
+
const xMax = xMin + patternWidth;
|
871
|
+
|
872
|
+
for (let y = 0; y < patternHeight; y++) {
|
748
873
|
patternBitmap.push(collectiveBitmap[y].subarray(xMin, xMax));
|
749
874
|
}
|
875
|
+
|
750
876
|
patterns.push(patternBitmap);
|
751
|
-
i++;
|
752
877
|
}
|
878
|
+
|
753
879
|
return patterns;
|
754
880
|
}
|
881
|
+
|
755
882
|
function decodeHalftoneRegion(mmr, patterns, template, regionWidth, regionHeight, defaultPixelValue, enableSkip, combinationOperator, gridWidth, gridHeight, gridOffsetX, gridOffsetY, gridVectorX, gridVectorY, decodingContext) {
|
756
|
-
|
883
|
+
const skip = null;
|
884
|
+
|
757
885
|
if (enableSkip) {
|
758
|
-
throw new Jbig2Error(
|
886
|
+
throw new Jbig2Error("skip is not supported");
|
759
887
|
}
|
888
|
+
|
760
889
|
if (combinationOperator !== 0) {
|
761
|
-
throw new Jbig2Error(
|
890
|
+
throw new Jbig2Error("operator " + combinationOperator + " is not supported in halftone region");
|
762
891
|
}
|
763
|
-
|
764
|
-
|
765
|
-
|
766
|
-
|
892
|
+
|
893
|
+
const regionBitmap = [];
|
894
|
+
let i, j, row;
|
895
|
+
|
767
896
|
for (i = 0; i < regionHeight; i++) {
|
768
897
|
row = new Uint8Array(regionWidth);
|
898
|
+
|
769
899
|
if (defaultPixelValue) {
|
770
900
|
for (j = 0; j < regionWidth; j++) {
|
771
901
|
row[j] = defaultPixelValue;
|
772
902
|
}
|
773
903
|
}
|
904
|
+
|
774
905
|
regionBitmap.push(row);
|
775
906
|
}
|
776
|
-
|
777
|
-
|
778
|
-
|
779
|
-
|
780
|
-
|
781
|
-
|
907
|
+
|
908
|
+
const numberOfPatterns = patterns.length;
|
909
|
+
const pattern0 = patterns[0];
|
910
|
+
const patternWidth = pattern0[0].length,
|
911
|
+
patternHeight = pattern0.length;
|
912
|
+
const bitsPerValue = (0, _core_utils.log2)(numberOfPatterns);
|
913
|
+
const at = [];
|
914
|
+
|
782
915
|
if (!mmr) {
|
783
916
|
at.push({
|
784
917
|
x: template <= 1 ? 3 : 2,
|
785
918
|
y: -1
|
786
919
|
});
|
920
|
+
|
787
921
|
if (template === 0) {
|
788
922
|
at.push({
|
789
923
|
x: -3,
|
@@ -799,60 +933,65 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
799
933
|
});
|
800
934
|
}
|
801
935
|
}
|
802
|
-
|
803
|
-
|
804
|
-
|
936
|
+
|
937
|
+
const grayScaleBitPlanes = [];
|
938
|
+
let mmrInput, bitmap;
|
939
|
+
|
805
940
|
if (mmr) {
|
806
941
|
mmrInput = new Reader(decodingContext.data, decodingContext.start, decodingContext.end);
|
807
942
|
}
|
943
|
+
|
808
944
|
for (i = bitsPerValue - 1; i >= 0; i--) {
|
809
945
|
if (mmr) {
|
810
946
|
bitmap = decodeMMRBitmap(mmrInput, gridWidth, gridHeight, true);
|
811
947
|
} else {
|
812
948
|
bitmap = decodeBitmap(false, gridWidth, gridHeight, template, false, skip, at, decodingContext);
|
813
949
|
}
|
950
|
+
|
814
951
|
grayScaleBitPlanes[i] = bitmap;
|
815
952
|
}
|
816
|
-
|
817
|
-
|
818
|
-
|
819
|
-
patternIndex = void 0,
|
820
|
-
patternBitmap = void 0,
|
821
|
-
x = void 0,
|
822
|
-
y = void 0,
|
823
|
-
patternRow = void 0,
|
824
|
-
regionRow = void 0;
|
953
|
+
|
954
|
+
let mg, ng, bit, patternIndex, patternBitmap, x, y, patternRow, regionRow;
|
955
|
+
|
825
956
|
for (mg = 0; mg < gridHeight; mg++) {
|
826
957
|
for (ng = 0; ng < gridWidth; ng++) {
|
827
958
|
bit = 0;
|
828
959
|
patternIndex = 0;
|
960
|
+
|
829
961
|
for (j = bitsPerValue - 1; j >= 0; j--) {
|
830
962
|
bit = grayScaleBitPlanes[j][mg][ng] ^ bit;
|
831
963
|
patternIndex |= bit << j;
|
832
964
|
}
|
965
|
+
|
833
966
|
patternBitmap = patterns[patternIndex];
|
834
967
|
x = gridOffsetX + mg * gridVectorY + ng * gridVectorX >> 8;
|
835
968
|
y = gridOffsetY + mg * gridVectorX - ng * gridVectorY >> 8;
|
969
|
+
|
836
970
|
if (x >= 0 && x + patternWidth <= regionWidth && y >= 0 && y + patternHeight <= regionHeight) {
|
837
971
|
for (i = 0; i < patternHeight; i++) {
|
838
972
|
regionRow = regionBitmap[y + i];
|
839
973
|
patternRow = patternBitmap[i];
|
974
|
+
|
840
975
|
for (j = 0; j < patternWidth; j++) {
|
841
976
|
regionRow[x + j] |= patternRow[j];
|
842
977
|
}
|
843
978
|
}
|
844
979
|
} else {
|
845
|
-
|
846
|
-
|
980
|
+
let regionX, regionY;
|
981
|
+
|
847
982
|
for (i = 0; i < patternHeight; i++) {
|
848
983
|
regionY = y + i;
|
984
|
+
|
849
985
|
if (regionY < 0 || regionY >= regionHeight) {
|
850
986
|
continue;
|
851
987
|
}
|
988
|
+
|
852
989
|
regionRow = regionBitmap[regionY];
|
853
990
|
patternRow = patternBitmap[i];
|
991
|
+
|
854
992
|
for (j = 0; j < patternWidth; j++) {
|
855
993
|
regionX = x + j;
|
994
|
+
|
856
995
|
if (regionX >= 0 && regionX < regionWidth) {
|
857
996
|
regionRow[regionX] |= patternRow[j];
|
858
997
|
}
|
@@ -861,16 +1000,20 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
861
1000
|
}
|
862
1001
|
}
|
863
1002
|
}
|
1003
|
+
|
864
1004
|
return regionBitmap;
|
865
1005
|
}
|
1006
|
+
|
866
1007
|
function readSegmentHeader(data, start) {
|
867
1008
|
var segmentHeader = {};
|
868
|
-
segmentHeader.number = (0,
|
1009
|
+
segmentHeader.number = (0, _core_utils.readUint32)(data, start);
|
869
1010
|
var flags = data[start + 4];
|
870
|
-
var segmentType = flags &
|
1011
|
+
var segmentType = flags & 0x3f;
|
1012
|
+
|
871
1013
|
if (!SegmentTypes[segmentType]) {
|
872
|
-
throw new Jbig2Error(
|
1014
|
+
throw new Jbig2Error("invalid segment type: " + segmentType);
|
873
1015
|
}
|
1016
|
+
|
874
1017
|
segmentHeader.type = segmentType;
|
875
1018
|
segmentHeader.typeName = SegmentTypes[segmentType];
|
876
1019
|
segmentHeader.deferredNonRetain = !!(flags & 0x80);
|
@@ -879,90 +1022,127 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
879
1022
|
var referredToCount = referredFlags >> 5 & 7;
|
880
1023
|
var retainBits = [referredFlags & 31];
|
881
1024
|
var position = start + 6;
|
1025
|
+
|
882
1026
|
if (referredFlags === 7) {
|
883
|
-
referredToCount = (0,
|
1027
|
+
referredToCount = (0, _core_utils.readUint32)(data, position - 1) & 0x1fffffff;
|
884
1028
|
position += 3;
|
885
1029
|
var bytes = referredToCount + 7 >> 3;
|
886
1030
|
retainBits[0] = data[position++];
|
1031
|
+
|
887
1032
|
while (--bytes > 0) {
|
888
1033
|
retainBits.push(data[position++]);
|
889
1034
|
}
|
890
1035
|
} else if (referredFlags === 5 || referredFlags === 6) {
|
891
|
-
throw new Jbig2Error(
|
1036
|
+
throw new Jbig2Error("invalid referred-to flags");
|
892
1037
|
}
|
1038
|
+
|
893
1039
|
segmentHeader.retainBits = retainBits;
|
894
|
-
|
1040
|
+
let referredToSegmentNumberSize = 4;
|
1041
|
+
|
1042
|
+
if (segmentHeader.number <= 256) {
|
1043
|
+
referredToSegmentNumberSize = 1;
|
1044
|
+
} else if (segmentHeader.number <= 65536) {
|
1045
|
+
referredToSegmentNumberSize = 2;
|
1046
|
+
}
|
1047
|
+
|
895
1048
|
var referredTo = [];
|
896
1049
|
var i, ii;
|
1050
|
+
|
897
1051
|
for (i = 0; i < referredToCount; i++) {
|
898
|
-
|
1052
|
+
let number;
|
1053
|
+
|
1054
|
+
if (referredToSegmentNumberSize === 1) {
|
1055
|
+
number = data[position];
|
1056
|
+
} else if (referredToSegmentNumberSize === 2) {
|
1057
|
+
number = (0, _core_utils.readUint16)(data, position);
|
1058
|
+
} else {
|
1059
|
+
number = (0, _core_utils.readUint32)(data, position);
|
1060
|
+
}
|
1061
|
+
|
899
1062
|
referredTo.push(number);
|
900
1063
|
position += referredToSegmentNumberSize;
|
901
1064
|
}
|
1065
|
+
|
902
1066
|
segmentHeader.referredTo = referredTo;
|
1067
|
+
|
903
1068
|
if (!pageAssociationFieldSize) {
|
904
1069
|
segmentHeader.pageAssociation = data[position++];
|
905
1070
|
} else {
|
906
|
-
segmentHeader.pageAssociation = (0,
|
1071
|
+
segmentHeader.pageAssociation = (0, _core_utils.readUint32)(data, position);
|
907
1072
|
position += 4;
|
908
1073
|
}
|
909
|
-
|
1074
|
+
|
1075
|
+
segmentHeader.length = (0, _core_utils.readUint32)(data, position);
|
910
1076
|
position += 4;
|
911
|
-
|
1077
|
+
|
1078
|
+
if (segmentHeader.length === 0xffffffff) {
|
912
1079
|
if (segmentType === 38) {
|
913
1080
|
var genericRegionInfo = readRegionSegmentInformation(data, position);
|
914
1081
|
var genericRegionSegmentFlags = data[position + RegionSegmentInformationFieldLength];
|
915
1082
|
var genericRegionMmr = !!(genericRegionSegmentFlags & 1);
|
916
1083
|
var searchPatternLength = 6;
|
917
1084
|
var searchPattern = new Uint8Array(searchPatternLength);
|
1085
|
+
|
918
1086
|
if (!genericRegionMmr) {
|
919
|
-
searchPattern[0] =
|
920
|
-
searchPattern[1] =
|
1087
|
+
searchPattern[0] = 0xff;
|
1088
|
+
searchPattern[1] = 0xac;
|
921
1089
|
}
|
922
|
-
|
923
|
-
searchPattern[
|
924
|
-
searchPattern[
|
925
|
-
searchPattern[
|
1090
|
+
|
1091
|
+
searchPattern[2] = genericRegionInfo.height >>> 24 & 0xff;
|
1092
|
+
searchPattern[3] = genericRegionInfo.height >> 16 & 0xff;
|
1093
|
+
searchPattern[4] = genericRegionInfo.height >> 8 & 0xff;
|
1094
|
+
searchPattern[5] = genericRegionInfo.height & 0xff;
|
1095
|
+
|
926
1096
|
for (i = position, ii = data.length; i < ii; i++) {
|
927
1097
|
var j = 0;
|
1098
|
+
|
928
1099
|
while (j < searchPatternLength && searchPattern[j] === data[i + j]) {
|
929
1100
|
j++;
|
930
1101
|
}
|
1102
|
+
|
931
1103
|
if (j === searchPatternLength) {
|
932
1104
|
segmentHeader.length = i + searchPatternLength;
|
933
1105
|
break;
|
934
1106
|
}
|
935
1107
|
}
|
936
|
-
|
937
|
-
|
1108
|
+
|
1109
|
+
if (segmentHeader.length === 0xffffffff) {
|
1110
|
+
throw new Jbig2Error("segment end was not found");
|
938
1111
|
}
|
939
1112
|
} else {
|
940
|
-
throw new Jbig2Error(
|
1113
|
+
throw new Jbig2Error("invalid unknown segment length");
|
941
1114
|
}
|
942
1115
|
}
|
1116
|
+
|
943
1117
|
segmentHeader.headerEnd = position;
|
944
1118
|
return segmentHeader;
|
945
1119
|
}
|
1120
|
+
|
946
1121
|
function readSegments(header, data, start, end) {
|
947
1122
|
var segments = [];
|
948
1123
|
var position = start;
|
1124
|
+
|
949
1125
|
while (position < end) {
|
950
1126
|
var segmentHeader = readSegmentHeader(data, position);
|
951
1127
|
position = segmentHeader.headerEnd;
|
952
1128
|
var segment = {
|
953
1129
|
header: segmentHeader,
|
954
|
-
data
|
1130
|
+
data
|
955
1131
|
};
|
1132
|
+
|
956
1133
|
if (!header.randomAccess) {
|
957
1134
|
segment.start = position;
|
958
1135
|
position += segmentHeader.length;
|
959
1136
|
segment.end = position;
|
960
1137
|
}
|
1138
|
+
|
961
1139
|
segments.push(segment);
|
1140
|
+
|
962
1141
|
if (segmentHeader.type === 51) {
|
963
1142
|
break;
|
964
1143
|
}
|
965
1144
|
}
|
1145
|
+
|
966
1146
|
if (header.randomAccess) {
|
967
1147
|
for (var i = 0, ii = segments.length; i < ii; i++) {
|
968
1148
|
segments[i].start = position;
|
@@ -970,28 +1150,33 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
970
1150
|
segments[i].end = position;
|
971
1151
|
}
|
972
1152
|
}
|
1153
|
+
|
973
1154
|
return segments;
|
974
1155
|
}
|
1156
|
+
|
975
1157
|
function readRegionSegmentInformation(data, start) {
|
976
1158
|
return {
|
977
|
-
width: (0,
|
978
|
-
height: (0,
|
979
|
-
x: (0,
|
980
|
-
y: (0,
|
1159
|
+
width: (0, _core_utils.readUint32)(data, start),
|
1160
|
+
height: (0, _core_utils.readUint32)(data, start + 4),
|
1161
|
+
x: (0, _core_utils.readUint32)(data, start + 8),
|
1162
|
+
y: (0, _core_utils.readUint32)(data, start + 12),
|
981
1163
|
combinationOperator: data[start + 16] & 7
|
982
1164
|
};
|
983
1165
|
}
|
1166
|
+
|
984
1167
|
var RegionSegmentInformationFieldLength = 17;
|
1168
|
+
|
985
1169
|
function processSegment(segment, visitor) {
|
986
1170
|
var header = segment.header;
|
987
1171
|
var data = segment.data,
|
988
1172
|
position = segment.start,
|
989
1173
|
end = segment.end;
|
990
1174
|
var args, at, i, atLength;
|
1175
|
+
|
991
1176
|
switch (header.type) {
|
992
1177
|
case 0:
|
993
1178
|
var dictionary = {};
|
994
|
-
var dictionaryFlags = (0,
|
1179
|
+
var dictionaryFlags = (0, _core_utils.readUint16)(data, position);
|
995
1180
|
dictionary.huffman = !!(dictionaryFlags & 1);
|
996
1181
|
dictionary.refinement = !!(dictionaryFlags & 2);
|
997
1182
|
dictionary.huffmanDHSelector = dictionaryFlags >> 2 & 3;
|
@@ -1003,41 +1188,49 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
1003
1188
|
dictionary.template = dictionaryFlags >> 10 & 3;
|
1004
1189
|
dictionary.refinementTemplate = dictionaryFlags >> 12 & 1;
|
1005
1190
|
position += 2;
|
1191
|
+
|
1006
1192
|
if (!dictionary.huffman) {
|
1007
1193
|
atLength = dictionary.template === 0 ? 4 : 1;
|
1008
1194
|
at = [];
|
1195
|
+
|
1009
1196
|
for (i = 0; i < atLength; i++) {
|
1010
1197
|
at.push({
|
1011
|
-
x: (0,
|
1012
|
-
y: (0,
|
1198
|
+
x: (0, _core_utils.readInt8)(data, position),
|
1199
|
+
y: (0, _core_utils.readInt8)(data, position + 1)
|
1013
1200
|
});
|
1014
1201
|
position += 2;
|
1015
1202
|
}
|
1203
|
+
|
1016
1204
|
dictionary.at = at;
|
1017
1205
|
}
|
1206
|
+
|
1018
1207
|
if (dictionary.refinement && !dictionary.refinementTemplate) {
|
1019
1208
|
at = [];
|
1209
|
+
|
1020
1210
|
for (i = 0; i < 2; i++) {
|
1021
1211
|
at.push({
|
1022
|
-
x: (0,
|
1023
|
-
y: (0,
|
1212
|
+
x: (0, _core_utils.readInt8)(data, position),
|
1213
|
+
y: (0, _core_utils.readInt8)(data, position + 1)
|
1024
1214
|
});
|
1025
1215
|
position += 2;
|
1026
1216
|
}
|
1217
|
+
|
1027
1218
|
dictionary.refinementAt = at;
|
1028
1219
|
}
|
1029
|
-
|
1220
|
+
|
1221
|
+
dictionary.numberOfExportedSymbols = (0, _core_utils.readUint32)(data, position);
|
1030
1222
|
position += 4;
|
1031
|
-
dictionary.numberOfNewSymbols = (0,
|
1223
|
+
dictionary.numberOfNewSymbols = (0, _core_utils.readUint32)(data, position);
|
1032
1224
|
position += 4;
|
1033
1225
|
args = [dictionary, header.number, header.referredTo, data, position, end];
|
1034
1226
|
break;
|
1227
|
+
|
1035
1228
|
case 6:
|
1036
1229
|
case 7:
|
1037
1230
|
var textRegion = {};
|
1038
1231
|
textRegion.info = readRegionSegmentInformation(data, position);
|
1039
1232
|
position += RegionSegmentInformationFieldLength;
|
1040
|
-
var textRegionSegmentFlags = (0,
|
1233
|
+
var textRegionSegmentFlags = (0, _core_utils.readUint16)(data, position);
|
1041
1234
|
position += 2;
|
1042
1235
|
textRegion.huffman = !!(textRegionSegmentFlags & 1);
|
1043
1236
|
textRegion.refinement = !!(textRegionSegmentFlags & 2);
|
@@ -1049,8 +1242,9 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
1049
1242
|
textRegion.defaultPixelValue = textRegionSegmentFlags >> 9 & 1;
|
1050
1243
|
textRegion.dsOffset = textRegionSegmentFlags << 17 >> 27;
|
1051
1244
|
textRegion.refinementTemplate = textRegionSegmentFlags >> 15 & 1;
|
1245
|
+
|
1052
1246
|
if (textRegion.huffman) {
|
1053
|
-
var textRegionHuffmanFlags = (0,
|
1247
|
+
var textRegionHuffmanFlags = (0, _core_utils.readUint16)(data, position);
|
1054
1248
|
position += 2;
|
1055
1249
|
textRegion.huffmanFS = textRegionHuffmanFlags & 3;
|
1056
1250
|
textRegion.huffmanDS = textRegionHuffmanFlags >> 2 & 3;
|
@@ -1061,57 +1255,64 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
1061
1255
|
textRegion.huffmanRefinementDY = textRegionHuffmanFlags >> 12 & 3;
|
1062
1256
|
textRegion.huffmanRefinementSizeSelector = !!(textRegionHuffmanFlags & 0x4000);
|
1063
1257
|
}
|
1258
|
+
|
1064
1259
|
if (textRegion.refinement && !textRegion.refinementTemplate) {
|
1065
1260
|
at = [];
|
1261
|
+
|
1066
1262
|
for (i = 0; i < 2; i++) {
|
1067
1263
|
at.push({
|
1068
|
-
x: (0,
|
1069
|
-
y: (0,
|
1264
|
+
x: (0, _core_utils.readInt8)(data, position),
|
1265
|
+
y: (0, _core_utils.readInt8)(data, position + 1)
|
1070
1266
|
});
|
1071
1267
|
position += 2;
|
1072
1268
|
}
|
1269
|
+
|
1073
1270
|
textRegion.refinementAt = at;
|
1074
1271
|
}
|
1075
|
-
|
1272
|
+
|
1273
|
+
textRegion.numberOfSymbolInstances = (0, _core_utils.readUint32)(data, position);
|
1076
1274
|
position += 4;
|
1077
1275
|
args = [textRegion, header.referredTo, data, position, end];
|
1078
1276
|
break;
|
1277
|
+
|
1079
1278
|
case 16:
|
1080
|
-
|
1081
|
-
|
1279
|
+
const patternDictionary = {};
|
1280
|
+
const patternDictionaryFlags = data[position++];
|
1082
1281
|
patternDictionary.mmr = !!(patternDictionaryFlags & 1);
|
1083
1282
|
patternDictionary.template = patternDictionaryFlags >> 1 & 3;
|
1084
1283
|
patternDictionary.patternWidth = data[position++];
|
1085
1284
|
patternDictionary.patternHeight = data[position++];
|
1086
|
-
patternDictionary.maxPatternIndex = (0,
|
1285
|
+
patternDictionary.maxPatternIndex = (0, _core_utils.readUint32)(data, position);
|
1087
1286
|
position += 4;
|
1088
1287
|
args = [patternDictionary, header.number, data, position, end];
|
1089
1288
|
break;
|
1289
|
+
|
1090
1290
|
case 22:
|
1091
1291
|
case 23:
|
1092
|
-
|
1292
|
+
const halftoneRegion = {};
|
1093
1293
|
halftoneRegion.info = readRegionSegmentInformation(data, position);
|
1094
1294
|
position += RegionSegmentInformationFieldLength;
|
1095
|
-
|
1295
|
+
const halftoneRegionFlags = data[position++];
|
1096
1296
|
halftoneRegion.mmr = !!(halftoneRegionFlags & 1);
|
1097
1297
|
halftoneRegion.template = halftoneRegionFlags >> 1 & 3;
|
1098
1298
|
halftoneRegion.enableSkip = !!(halftoneRegionFlags & 8);
|
1099
1299
|
halftoneRegion.combinationOperator = halftoneRegionFlags >> 4 & 7;
|
1100
1300
|
halftoneRegion.defaultPixelValue = halftoneRegionFlags >> 7 & 1;
|
1101
|
-
halftoneRegion.gridWidth = (0,
|
1301
|
+
halftoneRegion.gridWidth = (0, _core_utils.readUint32)(data, position);
|
1102
1302
|
position += 4;
|
1103
|
-
halftoneRegion.gridHeight = (0,
|
1303
|
+
halftoneRegion.gridHeight = (0, _core_utils.readUint32)(data, position);
|
1104
1304
|
position += 4;
|
1105
|
-
halftoneRegion.gridOffsetX = (0,
|
1305
|
+
halftoneRegion.gridOffsetX = (0, _core_utils.readUint32)(data, position) & 0xffffffff;
|
1106
1306
|
position += 4;
|
1107
|
-
halftoneRegion.gridOffsetY = (0,
|
1307
|
+
halftoneRegion.gridOffsetY = (0, _core_utils.readUint32)(data, position) & 0xffffffff;
|
1108
1308
|
position += 4;
|
1109
|
-
halftoneRegion.gridVectorX = (0,
|
1309
|
+
halftoneRegion.gridVectorX = (0, _core_utils.readUint16)(data, position);
|
1110
1310
|
position += 2;
|
1111
|
-
halftoneRegion.gridVectorY = (0,
|
1311
|
+
halftoneRegion.gridVectorY = (0, _core_utils.readUint16)(data, position);
|
1112
1312
|
position += 2;
|
1113
1313
|
args = [halftoneRegion, header.referredTo, data, position, end];
|
1114
1314
|
break;
|
1315
|
+
|
1115
1316
|
case 38:
|
1116
1317
|
case 39:
|
1117
1318
|
var genericRegion = {};
|
@@ -1121,32 +1322,39 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
1121
1322
|
genericRegion.mmr = !!(genericRegionSegmentFlags & 1);
|
1122
1323
|
genericRegion.template = genericRegionSegmentFlags >> 1 & 3;
|
1123
1324
|
genericRegion.prediction = !!(genericRegionSegmentFlags & 8);
|
1325
|
+
|
1124
1326
|
if (!genericRegion.mmr) {
|
1125
1327
|
atLength = genericRegion.template === 0 ? 4 : 1;
|
1126
1328
|
at = [];
|
1329
|
+
|
1127
1330
|
for (i = 0; i < atLength; i++) {
|
1128
1331
|
at.push({
|
1129
|
-
x: (0,
|
1130
|
-
y: (0,
|
1332
|
+
x: (0, _core_utils.readInt8)(data, position),
|
1333
|
+
y: (0, _core_utils.readInt8)(data, position + 1)
|
1131
1334
|
});
|
1132
1335
|
position += 2;
|
1133
1336
|
}
|
1337
|
+
|
1134
1338
|
genericRegion.at = at;
|
1135
1339
|
}
|
1340
|
+
|
1136
1341
|
args = [genericRegion, data, position, end];
|
1137
1342
|
break;
|
1343
|
+
|
1138
1344
|
case 48:
|
1139
1345
|
var pageInfo = {
|
1140
|
-
width: (0,
|
1141
|
-
height: (0,
|
1142
|
-
resolutionX: (0,
|
1143
|
-
resolutionY: (0,
|
1346
|
+
width: (0, _core_utils.readUint32)(data, position),
|
1347
|
+
height: (0, _core_utils.readUint32)(data, position + 4),
|
1348
|
+
resolutionX: (0, _core_utils.readUint32)(data, position + 8),
|
1349
|
+
resolutionY: (0, _core_utils.readUint32)(data, position + 12)
|
1144
1350
|
};
|
1145
|
-
|
1351
|
+
|
1352
|
+
if (pageInfo.height === 0xffffffff) {
|
1146
1353
|
delete pageInfo.height;
|
1147
1354
|
}
|
1355
|
+
|
1148
1356
|
var pageSegmentFlags = data[position + 16];
|
1149
|
-
(0,
|
1357
|
+
(0, _core_utils.readUint16)(data, position + 17);
|
1150
1358
|
pageInfo.lossless = !!(pageSegmentFlags & 1);
|
1151
1359
|
pageInfo.refinement = !!(pageSegmentFlags & 2);
|
1152
1360
|
pageInfo.defaultPixelValue = pageSegmentFlags >> 2 & 1;
|
@@ -1155,93 +1363,118 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
1155
1363
|
pageInfo.combinationOperatorOverride = !!(pageSegmentFlags & 64);
|
1156
1364
|
args = [pageInfo];
|
1157
1365
|
break;
|
1366
|
+
|
1158
1367
|
case 49:
|
1159
1368
|
break;
|
1369
|
+
|
1160
1370
|
case 50:
|
1161
1371
|
break;
|
1372
|
+
|
1162
1373
|
case 51:
|
1163
1374
|
break;
|
1375
|
+
|
1164
1376
|
case 53:
|
1165
1377
|
args = [header.number, data, position, end];
|
1166
1378
|
break;
|
1379
|
+
|
1167
1380
|
case 62:
|
1168
1381
|
break;
|
1382
|
+
|
1169
1383
|
default:
|
1170
|
-
throw new Jbig2Error(
|
1384
|
+
throw new Jbig2Error(`segment type ${header.typeName}(${header.type})` + " is not implemented");
|
1171
1385
|
}
|
1172
|
-
|
1386
|
+
|
1387
|
+
var callbackName = "on" + header.typeName;
|
1388
|
+
|
1173
1389
|
if (callbackName in visitor) {
|
1174
1390
|
visitor[callbackName].apply(visitor, args);
|
1175
1391
|
}
|
1176
1392
|
}
|
1393
|
+
|
1177
1394
|
function processSegments(segments, visitor) {
|
1178
1395
|
for (var i = 0, ii = segments.length; i < ii; i++) {
|
1179
1396
|
processSegment(segments[i], visitor);
|
1180
1397
|
}
|
1181
1398
|
}
|
1399
|
+
|
1182
1400
|
function parseJbig2Chunks(chunks) {
|
1183
1401
|
var visitor = new SimpleSegmentVisitor();
|
1402
|
+
|
1184
1403
|
for (var i = 0, ii = chunks.length; i < ii; i++) {
|
1185
1404
|
var chunk = chunks[i];
|
1186
1405
|
var segments = readSegments({}, chunk.data, chunk.start, chunk.end);
|
1187
1406
|
processSegments(segments, visitor);
|
1188
1407
|
}
|
1408
|
+
|
1189
1409
|
return visitor.buffer;
|
1190
1410
|
}
|
1411
|
+
|
1191
1412
|
function parseJbig2(data) {
|
1192
|
-
|
1193
|
-
|
1194
|
-
|
1195
|
-
|
1413
|
+
const end = data.length;
|
1414
|
+
let position = 0;
|
1415
|
+
|
1416
|
+
if (data[position] !== 0x97 || data[position + 1] !== 0x4a || data[position + 2] !== 0x42 || data[position + 3] !== 0x32 || data[position + 4] !== 0x0d || data[position + 5] !== 0x0a || data[position + 6] !== 0x1a || data[position + 7] !== 0x0a) {
|
1417
|
+
throw new Jbig2Error("parseJbig2 - invalid header.");
|
1196
1418
|
}
|
1197
|
-
|
1419
|
+
|
1420
|
+
const header = Object.create(null);
|
1198
1421
|
position += 8;
|
1199
|
-
|
1422
|
+
const flags = data[position++];
|
1200
1423
|
header.randomAccess = !(flags & 1);
|
1424
|
+
|
1201
1425
|
if (!(flags & 2)) {
|
1202
|
-
header.numberOfPages = (0,
|
1426
|
+
header.numberOfPages = (0, _core_utils.readUint32)(data, position);
|
1203
1427
|
position += 4;
|
1204
1428
|
}
|
1205
|
-
var segments = readSegments(header, data, position, end);
|
1206
|
-
var visitor = new SimpleSegmentVisitor();
|
1207
|
-
processSegments(segments, visitor);
|
1208
|
-
var _visitor$currentPageI = visitor.currentPageInfo,
|
1209
|
-
width = _visitor$currentPageI.width,
|
1210
|
-
height = _visitor$currentPageI.height;
|
1211
1429
|
|
1212
|
-
|
1213
|
-
|
1214
|
-
|
1430
|
+
const segments = readSegments(header, data, position, end);
|
1431
|
+
const visitor = new SimpleSegmentVisitor();
|
1432
|
+
processSegments(segments, visitor);
|
1433
|
+
const {
|
1434
|
+
width,
|
1435
|
+
height
|
1436
|
+
} = visitor.currentPageInfo;
|
1437
|
+
const bitPacked = visitor.buffer;
|
1438
|
+
const imgData = new Uint8ClampedArray(width * height);
|
1439
|
+
let q = 0,
|
1215
1440
|
k = 0;
|
1216
|
-
|
1217
|
-
|
1218
|
-
|
1219
|
-
|
1441
|
+
|
1442
|
+
for (let i = 0; i < height; i++) {
|
1443
|
+
let mask = 0,
|
1444
|
+
buffer;
|
1445
|
+
|
1446
|
+
for (let j = 0; j < width; j++) {
|
1220
1447
|
if (!mask) {
|
1221
1448
|
mask = 128;
|
1222
1449
|
buffer = bitPacked[k++];
|
1223
1450
|
}
|
1451
|
+
|
1224
1452
|
imgData[q++] = buffer & mask ? 0 : 255;
|
1225
1453
|
mask >>= 1;
|
1226
1454
|
}
|
1227
1455
|
}
|
1456
|
+
|
1228
1457
|
return {
|
1229
|
-
imgData
|
1230
|
-
width
|
1231
|
-
height
|
1458
|
+
imgData,
|
1459
|
+
width,
|
1460
|
+
height
|
1232
1461
|
};
|
1233
1462
|
}
|
1463
|
+
|
1234
1464
|
function SimpleSegmentVisitor() {}
|
1465
|
+
|
1235
1466
|
SimpleSegmentVisitor.prototype = {
|
1236
1467
|
onPageInformation: function SimpleSegmentVisitor_onPageInformation(info) {
|
1237
1468
|
this.currentPageInfo = info;
|
1238
1469
|
var rowSize = info.width + 7 >> 3;
|
1239
1470
|
var buffer = new Uint8ClampedArray(rowSize * info.height);
|
1471
|
+
|
1240
1472
|
if (info.defaultPixelValue) {
|
1241
1473
|
for (var i = 0, ii = buffer.length; i < ii; i++) {
|
1242
|
-
buffer[i] =
|
1474
|
+
buffer[i] = 0xff;
|
1243
1475
|
}
|
1244
1476
|
}
|
1477
|
+
|
1245
1478
|
this.buffer = buffer;
|
1246
1479
|
},
|
1247
1480
|
drawBitmap: function SimpleSegmentVisitor_drawBitmap(regionInfo, bitmap) {
|
@@ -1254,43 +1487,56 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
1254
1487
|
var mask0 = 128 >> (regionInfo.x & 7);
|
1255
1488
|
var offset0 = regionInfo.y * rowSize + (regionInfo.x >> 3);
|
1256
1489
|
var i, j, mask, offset;
|
1490
|
+
|
1257
1491
|
switch (combinationOperator) {
|
1258
1492
|
case 0:
|
1259
1493
|
for (i = 0; i < height; i++) {
|
1260
1494
|
mask = mask0;
|
1261
1495
|
offset = offset0;
|
1496
|
+
|
1262
1497
|
for (j = 0; j < width; j++) {
|
1263
1498
|
if (bitmap[i][j]) {
|
1264
1499
|
buffer[offset] |= mask;
|
1265
1500
|
}
|
1501
|
+
|
1266
1502
|
mask >>= 1;
|
1503
|
+
|
1267
1504
|
if (!mask) {
|
1268
1505
|
mask = 128;
|
1269
1506
|
offset++;
|
1270
1507
|
}
|
1271
1508
|
}
|
1509
|
+
|
1272
1510
|
offset0 += rowSize;
|
1273
1511
|
}
|
1512
|
+
|
1274
1513
|
break;
|
1514
|
+
|
1275
1515
|
case 2:
|
1276
1516
|
for (i = 0; i < height; i++) {
|
1277
1517
|
mask = mask0;
|
1278
1518
|
offset = offset0;
|
1519
|
+
|
1279
1520
|
for (j = 0; j < width; j++) {
|
1280
1521
|
if (bitmap[i][j]) {
|
1281
1522
|
buffer[offset] ^= mask;
|
1282
1523
|
}
|
1524
|
+
|
1283
1525
|
mask >>= 1;
|
1526
|
+
|
1284
1527
|
if (!mask) {
|
1285
1528
|
mask = 128;
|
1286
1529
|
offset++;
|
1287
1530
|
}
|
1288
1531
|
}
|
1532
|
+
|
1289
1533
|
offset0 += rowSize;
|
1290
1534
|
}
|
1535
|
+
|
1291
1536
|
break;
|
1537
|
+
|
1292
1538
|
default:
|
1293
|
-
throw new Jbig2Error(
|
1539
|
+
throw new Jbig2Error(`operator ${combinationOperator} is not supported`);
|
1294
1540
|
}
|
1295
1541
|
},
|
1296
1542
|
onImmediateGenericRegion: function SimpleSegmentVisitor_onImmediateGenericRegion(region, data, start, end) {
|
@@ -1303,43 +1549,53 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
1303
1549
|
this.onImmediateGenericRegion.apply(this, arguments);
|
1304
1550
|
},
|
1305
1551
|
onSymbolDictionary: function SimpleSegmentVisitor_onSymbolDictionary(dictionary, currentSegment, referredSegments, data, start, end) {
|
1306
|
-
|
1307
|
-
|
1552
|
+
let huffmanTables, huffmanInput;
|
1553
|
+
|
1308
1554
|
if (dictionary.huffman) {
|
1309
1555
|
huffmanTables = getSymbolDictionaryHuffmanTables(dictionary, referredSegments, this.customTables);
|
1310
1556
|
huffmanInput = new Reader(data, start, end);
|
1311
1557
|
}
|
1558
|
+
|
1312
1559
|
var symbols = this.symbols;
|
1560
|
+
|
1313
1561
|
if (!symbols) {
|
1314
1562
|
this.symbols = symbols = {};
|
1315
1563
|
}
|
1564
|
+
|
1316
1565
|
var inputSymbols = [];
|
1566
|
+
|
1317
1567
|
for (var i = 0, ii = referredSegments.length; i < ii; i++) {
|
1318
|
-
|
1568
|
+
const referredSymbols = symbols[referredSegments[i]];
|
1569
|
+
|
1319
1570
|
if (referredSymbols) {
|
1320
1571
|
inputSymbols = inputSymbols.concat(referredSymbols);
|
1321
1572
|
}
|
1322
1573
|
}
|
1574
|
+
|
1323
1575
|
var decodingContext = new DecodingContext(data, start, end);
|
1324
1576
|
symbols[currentSegment] = decodeSymbolDictionary(dictionary.huffman, dictionary.refinement, inputSymbols, dictionary.numberOfNewSymbols, dictionary.numberOfExportedSymbols, huffmanTables, dictionary.template, dictionary.at, dictionary.refinementTemplate, dictionary.refinementAt, decodingContext, huffmanInput);
|
1325
1577
|
},
|
1326
1578
|
onImmediateTextRegion: function SimpleSegmentVisitor_onImmediateTextRegion(region, referredSegments, data, start, end) {
|
1327
1579
|
var regionInfo = region.info;
|
1328
|
-
|
1329
|
-
huffmanInput = void 0;
|
1580
|
+
let huffmanTables, huffmanInput;
|
1330
1581
|
var symbols = this.symbols;
|
1331
1582
|
var inputSymbols = [];
|
1583
|
+
|
1332
1584
|
for (var i = 0, ii = referredSegments.length; i < ii; i++) {
|
1333
|
-
|
1585
|
+
const referredSymbols = symbols[referredSegments[i]];
|
1586
|
+
|
1334
1587
|
if (referredSymbols) {
|
1335
1588
|
inputSymbols = inputSymbols.concat(referredSymbols);
|
1336
1589
|
}
|
1337
1590
|
}
|
1338
|
-
|
1591
|
+
|
1592
|
+
var symbolCodeLength = (0, _core_utils.log2)(inputSymbols.length);
|
1593
|
+
|
1339
1594
|
if (region.huffman) {
|
1340
1595
|
huffmanInput = new Reader(data, start, end);
|
1341
1596
|
huffmanTables = getTextRegionHuffmanTables(region, referredSegments, this.customTables, inputSymbols.length, huffmanInput);
|
1342
1597
|
}
|
1598
|
+
|
1343
1599
|
var decodingContext = new DecodingContext(data, start, end);
|
1344
1600
|
var bitmap = decodeTextRegion(region.huffman, region.refinement, regionInfo.width, regionInfo.height, region.defaultPixelValue, region.numberOfSymbolInstances, region.stripSize, inputSymbols, symbolCodeLength, region.transposed, region.dsOffset, region.referenceCorner, region.combinationOperator, huffmanTables, region.refinementTemplate, region.refinementAt, decodingContext, region.logStripSize, huffmanInput);
|
1345
1601
|
this.drawBitmap(regionInfo, bitmap);
|
@@ -1347,32 +1603,42 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
1347
1603
|
onImmediateLosslessTextRegion: function SimpleSegmentVisitor_onImmediateLosslessTextRegion() {
|
1348
1604
|
this.onImmediateTextRegion.apply(this, arguments);
|
1349
1605
|
},
|
1350
|
-
|
1351
|
-
|
1606
|
+
|
1607
|
+
onPatternDictionary(dictionary, currentSegment, data, start, end) {
|
1608
|
+
let patterns = this.patterns;
|
1609
|
+
|
1352
1610
|
if (!patterns) {
|
1353
1611
|
this.patterns = patterns = {};
|
1354
1612
|
}
|
1355
|
-
|
1613
|
+
|
1614
|
+
const decodingContext = new DecodingContext(data, start, end);
|
1356
1615
|
patterns[currentSegment] = decodePatternDictionary(dictionary.mmr, dictionary.patternWidth, dictionary.patternHeight, dictionary.maxPatternIndex, dictionary.template, decodingContext);
|
1357
1616
|
},
|
1358
|
-
|
1359
|
-
|
1360
|
-
|
1361
|
-
|
1362
|
-
|
1617
|
+
|
1618
|
+
onImmediateHalftoneRegion(region, referredSegments, data, start, end) {
|
1619
|
+
const patterns = this.patterns[referredSegments[0]];
|
1620
|
+
const regionInfo = region.info;
|
1621
|
+
const decodingContext = new DecodingContext(data, start, end);
|
1622
|
+
const bitmap = decodeHalftoneRegion(region.mmr, patterns, region.template, regionInfo.width, regionInfo.height, region.defaultPixelValue, region.enableSkip, region.combinationOperator, region.gridWidth, region.gridHeight, region.gridOffsetX, region.gridOffsetY, region.gridVectorX, region.gridVectorY, decodingContext);
|
1363
1623
|
this.drawBitmap(regionInfo, bitmap);
|
1364
1624
|
},
|
1365
|
-
|
1625
|
+
|
1626
|
+
onImmediateLosslessHalftoneRegion() {
|
1366
1627
|
this.onImmediateHalftoneRegion.apply(this, arguments);
|
1367
1628
|
},
|
1368
|
-
|
1369
|
-
|
1629
|
+
|
1630
|
+
onTables(currentSegment, data, start, end) {
|
1631
|
+
let customTables = this.customTables;
|
1632
|
+
|
1370
1633
|
if (!customTables) {
|
1371
1634
|
this.customTables = customTables = {};
|
1372
1635
|
}
|
1636
|
+
|
1373
1637
|
customTables[currentSegment] = decodeTablesSegment(data, start, end);
|
1374
1638
|
}
|
1639
|
+
|
1375
1640
|
};
|
1641
|
+
|
1376
1642
|
function HuffmanLine(lineData) {
|
1377
1643
|
if (lineData.length === 2) {
|
1378
1644
|
this.isOOB = true;
|
@@ -1387,11 +1653,13 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
1387
1653
|
this.prefixLength = lineData[1];
|
1388
1654
|
this.rangeLength = lineData[2];
|
1389
1655
|
this.prefixCode = lineData[3];
|
1390
|
-
this.isLowerRange = lineData[4] ===
|
1656
|
+
this.isLowerRange = lineData[4] === "lower";
|
1391
1657
|
}
|
1392
1658
|
}
|
1659
|
+
|
1393
1660
|
function HuffmanTreeNode(line) {
|
1394
1661
|
this.children = [];
|
1662
|
+
|
1395
1663
|
if (line) {
|
1396
1664
|
this.isLeaf = true;
|
1397
1665
|
this.rangeLength = line.rangeLength;
|
@@ -1402,178 +1670,226 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
1402
1670
|
this.isLeaf = false;
|
1403
1671
|
}
|
1404
1672
|
}
|
1673
|
+
|
1405
1674
|
HuffmanTreeNode.prototype = {
|
1406
|
-
buildTree
|
1407
|
-
|
1675
|
+
buildTree(line, shift) {
|
1676
|
+
const bit = line.prefixCode >> shift & 1;
|
1677
|
+
|
1408
1678
|
if (shift <= 0) {
|
1409
1679
|
this.children[bit] = new HuffmanTreeNode(line);
|
1410
1680
|
} else {
|
1411
|
-
|
1681
|
+
let node = this.children[bit];
|
1682
|
+
|
1412
1683
|
if (!node) {
|
1413
1684
|
this.children[bit] = node = new HuffmanTreeNode(null);
|
1414
1685
|
}
|
1686
|
+
|
1415
1687
|
node.buildTree(line, shift - 1);
|
1416
1688
|
}
|
1417
1689
|
},
|
1418
|
-
|
1690
|
+
|
1691
|
+
decodeNode(reader) {
|
1419
1692
|
if (this.isLeaf) {
|
1420
1693
|
if (this.isOOB) {
|
1421
1694
|
return null;
|
1422
1695
|
}
|
1423
|
-
|
1696
|
+
|
1697
|
+
const htOffset = reader.readBits(this.rangeLength);
|
1424
1698
|
return this.rangeLow + (this.isLowerRange ? -htOffset : htOffset);
|
1425
1699
|
}
|
1426
|
-
|
1700
|
+
|
1701
|
+
const node = this.children[reader.readBit()];
|
1702
|
+
|
1427
1703
|
if (!node) {
|
1428
|
-
throw new Jbig2Error(
|
1704
|
+
throw new Jbig2Error("invalid Huffman data");
|
1429
1705
|
}
|
1706
|
+
|
1430
1707
|
return node.decodeNode(reader);
|
1431
1708
|
}
|
1709
|
+
|
1432
1710
|
};
|
1711
|
+
|
1433
1712
|
function HuffmanTable(lines, prefixCodesDone) {
|
1434
1713
|
if (!prefixCodesDone) {
|
1435
1714
|
this.assignPrefixCodes(lines);
|
1436
1715
|
}
|
1716
|
+
|
1437
1717
|
this.rootNode = new HuffmanTreeNode(null);
|
1438
|
-
|
1439
|
-
|
1440
|
-
|
1441
|
-
|
1442
|
-
line = lines[i];
|
1718
|
+
|
1719
|
+
for (let i = 0, ii = lines.length; i < ii; i++) {
|
1720
|
+
const line = lines[i];
|
1721
|
+
|
1443
1722
|
if (line.prefixLength > 0) {
|
1444
1723
|
this.rootNode.buildTree(line, line.prefixLength - 1);
|
1445
1724
|
}
|
1446
1725
|
}
|
1447
1726
|
}
|
1727
|
+
|
1448
1728
|
HuffmanTable.prototype = {
|
1449
|
-
decode
|
1729
|
+
decode(reader) {
|
1450
1730
|
return this.rootNode.decodeNode(reader);
|
1451
1731
|
},
|
1452
|
-
|
1453
|
-
|
1454
|
-
|
1455
|
-
|
1456
|
-
|
1732
|
+
|
1733
|
+
assignPrefixCodes(lines) {
|
1734
|
+
const linesLength = lines.length;
|
1735
|
+
let prefixLengthMax = 0;
|
1736
|
+
|
1737
|
+
for (let i = 0; i < linesLength; i++) {
|
1457
1738
|
prefixLengthMax = Math.max(prefixLengthMax, lines[i].prefixLength);
|
1458
1739
|
}
|
1459
|
-
|
1460
|
-
|
1740
|
+
|
1741
|
+
const histogram = new Uint32Array(prefixLengthMax + 1);
|
1742
|
+
|
1743
|
+
for (let i = 0; i < linesLength; i++) {
|
1461
1744
|
histogram[lines[i].prefixLength]++;
|
1462
1745
|
}
|
1463
|
-
|
1746
|
+
|
1747
|
+
let currentLength = 1,
|
1464
1748
|
firstCode = 0,
|
1465
|
-
currentCode
|
1466
|
-
currentTemp
|
1467
|
-
line
|
1749
|
+
currentCode,
|
1750
|
+
currentTemp,
|
1751
|
+
line;
|
1468
1752
|
histogram[0] = 0;
|
1753
|
+
|
1469
1754
|
while (currentLength <= prefixLengthMax) {
|
1470
1755
|
firstCode = firstCode + histogram[currentLength - 1] << 1;
|
1471
1756
|
currentCode = firstCode;
|
1472
1757
|
currentTemp = 0;
|
1758
|
+
|
1473
1759
|
while (currentTemp < linesLength) {
|
1474
1760
|
line = lines[currentTemp];
|
1761
|
+
|
1475
1762
|
if (line.prefixLength === currentLength) {
|
1476
1763
|
line.prefixCode = currentCode;
|
1477
1764
|
currentCode++;
|
1478
1765
|
}
|
1766
|
+
|
1479
1767
|
currentTemp++;
|
1480
1768
|
}
|
1769
|
+
|
1481
1770
|
currentLength++;
|
1482
1771
|
}
|
1483
1772
|
}
|
1773
|
+
|
1484
1774
|
};
|
1775
|
+
|
1485
1776
|
function decodeTablesSegment(data, start, end) {
|
1486
|
-
|
1487
|
-
|
1488
|
-
|
1489
|
-
|
1490
|
-
|
1491
|
-
|
1492
|
-
|
1493
|
-
|
1494
|
-
rangeLength
|
1777
|
+
const flags = data[start];
|
1778
|
+
const lowestValue = (0, _core_utils.readUint32)(data, start + 1) & 0xffffffff;
|
1779
|
+
const highestValue = (0, _core_utils.readUint32)(data, start + 5) & 0xffffffff;
|
1780
|
+
const reader = new Reader(data, start + 9, end);
|
1781
|
+
const prefixSizeBits = (flags >> 1 & 7) + 1;
|
1782
|
+
const rangeSizeBits = (flags >> 4 & 7) + 1;
|
1783
|
+
const lines = [];
|
1784
|
+
let prefixLength,
|
1785
|
+
rangeLength,
|
1495
1786
|
currentRangeLow = lowestValue;
|
1787
|
+
|
1496
1788
|
do {
|
1497
1789
|
prefixLength = reader.readBits(prefixSizeBits);
|
1498
1790
|
rangeLength = reader.readBits(rangeSizeBits);
|
1499
1791
|
lines.push(new HuffmanLine([currentRangeLow, prefixLength, rangeLength, 0]));
|
1500
1792
|
currentRangeLow += 1 << rangeLength;
|
1501
1793
|
} while (currentRangeLow < highestValue);
|
1794
|
+
|
1502
1795
|
prefixLength = reader.readBits(prefixSizeBits);
|
1503
|
-
lines.push(new HuffmanLine([lowestValue - 1, prefixLength, 32, 0,
|
1796
|
+
lines.push(new HuffmanLine([lowestValue - 1, prefixLength, 32, 0, "lower"]));
|
1504
1797
|
prefixLength = reader.readBits(prefixSizeBits);
|
1505
1798
|
lines.push(new HuffmanLine([highestValue, prefixLength, 32, 0]));
|
1799
|
+
|
1506
1800
|
if (flags & 1) {
|
1507
1801
|
prefixLength = reader.readBits(prefixSizeBits);
|
1508
1802
|
lines.push(new HuffmanLine([prefixLength, 0]));
|
1509
1803
|
}
|
1804
|
+
|
1510
1805
|
return new HuffmanTable(lines, false);
|
1511
1806
|
}
|
1512
|
-
|
1807
|
+
|
1808
|
+
const standardTablesCache = {};
|
1809
|
+
|
1513
1810
|
function getStandardTable(number) {
|
1514
|
-
|
1811
|
+
let table = standardTablesCache[number];
|
1812
|
+
|
1515
1813
|
if (table) {
|
1516
1814
|
return table;
|
1517
1815
|
}
|
1518
|
-
|
1816
|
+
|
1817
|
+
let lines;
|
1818
|
+
|
1519
1819
|
switch (number) {
|
1520
1820
|
case 1:
|
1521
1821
|
lines = [[0, 1, 4, 0x0], [16, 2, 8, 0x2], [272, 3, 16, 0x6], [65808, 3, 32, 0x7]];
|
1522
1822
|
break;
|
1823
|
+
|
1523
1824
|
case 2:
|
1524
|
-
lines = [[0, 1, 0, 0x0], [1, 2, 0, 0x2], [2, 3, 0, 0x6], [3, 4, 3,
|
1825
|
+
lines = [[0, 1, 0, 0x0], [1, 2, 0, 0x2], [2, 3, 0, 0x6], [3, 4, 3, 0xe], [11, 5, 6, 0x1e], [75, 6, 32, 0x3e], [6, 0x3f]];
|
1525
1826
|
break;
|
1827
|
+
|
1526
1828
|
case 3:
|
1527
|
-
lines = [[-256, 8, 8,
|
1829
|
+
lines = [[-256, 8, 8, 0xfe], [0, 1, 0, 0x0], [1, 2, 0, 0x2], [2, 3, 0, 0x6], [3, 4, 3, 0xe], [11, 5, 6, 0x1e], [-257, 8, 32, 0xff, "lower"], [75, 7, 32, 0x7e], [6, 0x3e]];
|
1528
1830
|
break;
|
1831
|
+
|
1529
1832
|
case 4:
|
1530
|
-
lines = [[1, 1, 0, 0x0], [2, 2, 0, 0x2], [3, 3, 0, 0x6], [4, 4, 3,
|
1833
|
+
lines = [[1, 1, 0, 0x0], [2, 2, 0, 0x2], [3, 3, 0, 0x6], [4, 4, 3, 0xe], [12, 5, 6, 0x1e], [76, 5, 32, 0x1f]];
|
1531
1834
|
break;
|
1835
|
+
|
1532
1836
|
case 5:
|
1533
|
-
lines = [[-255, 7, 8,
|
1837
|
+
lines = [[-255, 7, 8, 0x7e], [1, 1, 0, 0x0], [2, 2, 0, 0x2], [3, 3, 0, 0x6], [4, 4, 3, 0xe], [12, 5, 6, 0x1e], [-256, 7, 32, 0x7f, "lower"], [76, 6, 32, 0x3e]];
|
1534
1838
|
break;
|
1839
|
+
|
1535
1840
|
case 6:
|
1536
|
-
lines = [[-2048, 5, 10,
|
1841
|
+
lines = [[-2048, 5, 10, 0x1c], [-1024, 4, 9, 0x8], [-512, 4, 8, 0x9], [-256, 4, 7, 0xa], [-128, 5, 6, 0x1d], [-64, 5, 5, 0x1e], [-32, 4, 5, 0xb], [0, 2, 7, 0x0], [128, 3, 7, 0x2], [256, 3, 8, 0x3], [512, 4, 9, 0xc], [1024, 4, 10, 0xd], [-2049, 6, 32, 0x3e, "lower"], [2048, 6, 32, 0x3f]];
|
1537
1842
|
break;
|
1843
|
+
|
1538
1844
|
case 7:
|
1539
|
-
lines = [[-1024, 4, 9, 0x8], [-512, 3, 8, 0x0], [-256, 4, 7, 0x9], [-128, 5, 6,
|
1845
|
+
lines = [[-1024, 4, 9, 0x8], [-512, 3, 8, 0x0], [-256, 4, 7, 0x9], [-128, 5, 6, 0x1a], [-64, 5, 5, 0x1b], [-32, 4, 5, 0xa], [0, 4, 5, 0xb], [32, 5, 5, 0x1c], [64, 5, 6, 0x1d], [128, 4, 7, 0xc], [256, 3, 8, 0x1], [512, 3, 9, 0x2], [1024, 3, 10, 0x3], [-1025, 5, 32, 0x1e, "lower"], [2048, 5, 32, 0x1f]];
|
1540
1846
|
break;
|
1847
|
+
|
1541
1848
|
case 8:
|
1542
|
-
lines = [[-15, 8, 3,
|
1849
|
+
lines = [[-15, 8, 3, 0xfc], [-7, 9, 1, 0x1fc], [-5, 8, 1, 0xfd], [-3, 9, 0, 0x1fd], [-2, 7, 0, 0x7c], [-1, 4, 0, 0xa], [0, 2, 1, 0x0], [2, 5, 0, 0x1a], [3, 6, 0, 0x3a], [4, 3, 4, 0x4], [20, 6, 1, 0x3b], [22, 4, 4, 0xb], [38, 4, 5, 0xc], [70, 5, 6, 0x1b], [134, 5, 7, 0x1c], [262, 6, 7, 0x3c], [390, 7, 8, 0x7d], [646, 6, 10, 0x3d], [-16, 9, 32, 0x1fe, "lower"], [1670, 9, 32, 0x1ff], [2, 0x1]];
|
1543
1850
|
break;
|
1851
|
+
|
1544
1852
|
case 9:
|
1545
|
-
lines = [[-31, 8, 4,
|
1853
|
+
lines = [[-31, 8, 4, 0xfc], [-15, 9, 2, 0x1fc], [-11, 8, 2, 0xfd], [-7, 9, 1, 0x1fd], [-5, 7, 1, 0x7c], [-3, 4, 1, 0xa], [-1, 3, 1, 0x2], [1, 3, 1, 0x3], [3, 5, 1, 0x1a], [5, 6, 1, 0x3a], [7, 3, 5, 0x4], [39, 6, 2, 0x3b], [43, 4, 5, 0xb], [75, 4, 6, 0xc], [139, 5, 7, 0x1b], [267, 5, 8, 0x1c], [523, 6, 8, 0x3c], [779, 7, 9, 0x7d], [1291, 6, 11, 0x3d], [-32, 9, 32, 0x1fe, "lower"], [3339, 9, 32, 0x1ff], [2, 0x0]];
|
1546
1854
|
break;
|
1855
|
+
|
1547
1856
|
case 10:
|
1548
|
-
lines = [[-21, 7, 4,
|
1857
|
+
lines = [[-21, 7, 4, 0x7a], [-5, 8, 0, 0xfc], [-4, 7, 0, 0x7b], [-3, 5, 0, 0x18], [-2, 2, 2, 0x0], [2, 5, 0, 0x19], [3, 6, 0, 0x36], [4, 7, 0, 0x7c], [5, 8, 0, 0xfd], [6, 2, 6, 0x1], [70, 5, 5, 0x1a], [102, 6, 5, 0x37], [134, 6, 6, 0x38], [198, 6, 7, 0x39], [326, 6, 8, 0x3a], [582, 6, 9, 0x3b], [1094, 6, 10, 0x3c], [2118, 7, 11, 0x7d], [-22, 8, 32, 0xfe, "lower"], [4166, 8, 32, 0xff], [2, 0x2]];
|
1549
1858
|
break;
|
1859
|
+
|
1550
1860
|
case 11:
|
1551
|
-
lines = [[1, 1, 0, 0x0], [2, 2, 1, 0x2], [4, 4, 0,
|
1861
|
+
lines = [[1, 1, 0, 0x0], [2, 2, 1, 0x2], [4, 4, 0, 0xc], [5, 4, 1, 0xd], [7, 5, 1, 0x1c], [9, 5, 2, 0x1d], [13, 6, 2, 0x3c], [17, 7, 2, 0x7a], [21, 7, 3, 0x7b], [29, 7, 4, 0x7c], [45, 7, 5, 0x7d], [77, 7, 6, 0x7e], [141, 7, 32, 0x7f]];
|
1552
1862
|
break;
|
1863
|
+
|
1553
1864
|
case 12:
|
1554
|
-
lines = [[1, 1, 0, 0x0], [2, 2, 0, 0x2], [3, 3, 1, 0x6], [5, 5, 0,
|
1865
|
+
lines = [[1, 1, 0, 0x0], [2, 2, 0, 0x2], [3, 3, 1, 0x6], [5, 5, 0, 0x1c], [6, 5, 1, 0x1d], [8, 6, 1, 0x3c], [10, 7, 0, 0x7a], [11, 7, 1, 0x7b], [13, 7, 2, 0x7c], [17, 7, 3, 0x7d], [25, 7, 4, 0x7e], [41, 8, 5, 0xfe], [73, 8, 32, 0xff]];
|
1555
1866
|
break;
|
1867
|
+
|
1556
1868
|
case 13:
|
1557
|
-
lines = [[1, 1, 0, 0x0], [2, 3, 0, 0x4], [3, 4, 0,
|
1869
|
+
lines = [[1, 1, 0, 0x0], [2, 3, 0, 0x4], [3, 4, 0, 0xc], [4, 5, 0, 0x1c], [5, 4, 1, 0xd], [7, 3, 3, 0x5], [15, 6, 1, 0x3a], [17, 6, 2, 0x3b], [21, 6, 3, 0x3c], [29, 6, 4, 0x3d], [45, 6, 5, 0x3e], [77, 7, 6, 0x7e], [141, 7, 32, 0x7f]];
|
1558
1870
|
break;
|
1871
|
+
|
1559
1872
|
case 14:
|
1560
1873
|
lines = [[-2, 3, 0, 0x4], [-1, 3, 0, 0x5], [0, 1, 0, 0x0], [1, 3, 0, 0x6], [2, 3, 0, 0x7]];
|
1561
1874
|
break;
|
1875
|
+
|
1562
1876
|
case 15:
|
1563
|
-
lines = [[-24, 7, 4,
|
1877
|
+
lines = [[-24, 7, 4, 0x7c], [-8, 6, 2, 0x3c], [-4, 5, 1, 0x1c], [-2, 4, 0, 0xc], [-1, 3, 0, 0x4], [0, 1, 0, 0x0], [1, 3, 0, 0x5], [2, 4, 0, 0xd], [3, 5, 1, 0x1d], [5, 6, 2, 0x3d], [9, 7, 4, 0x7d], [-25, 7, 32, 0x7e, "lower"], [25, 7, 32, 0x7f]];
|
1564
1878
|
break;
|
1879
|
+
|
1565
1880
|
default:
|
1566
|
-
throw new Jbig2Error(
|
1881
|
+
throw new Jbig2Error(`standard table B.${number} does not exist`);
|
1567
1882
|
}
|
1568
|
-
|
1569
|
-
|
1570
|
-
for (i = 0; i < length; i++) {
|
1883
|
+
|
1884
|
+
for (let i = 0, ii = lines.length; i < ii; i++) {
|
1571
1885
|
lines[i] = new HuffmanLine(lines[i]);
|
1572
1886
|
}
|
1887
|
+
|
1573
1888
|
table = new HuffmanTable(lines, true);
|
1574
1889
|
standardTablesCache[number] = table;
|
1575
1890
|
return table;
|
1576
1891
|
}
|
1892
|
+
|
1577
1893
|
function Reader(data, start, end) {
|
1578
1894
|
this.data = data;
|
1579
1895
|
this.start = start;
|
@@ -1582,88 +1898,107 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
1582
1898
|
this.shift = -1;
|
1583
1899
|
this.currentByte = 0;
|
1584
1900
|
}
|
1901
|
+
|
1585
1902
|
Reader.prototype = {
|
1586
|
-
readBit
|
1903
|
+
readBit() {
|
1587
1904
|
if (this.shift < 0) {
|
1588
1905
|
if (this.position >= this.end) {
|
1589
|
-
throw new Jbig2Error(
|
1906
|
+
throw new Jbig2Error("end of data while reading bit");
|
1590
1907
|
}
|
1908
|
+
|
1591
1909
|
this.currentByte = this.data[this.position++];
|
1592
1910
|
this.shift = 7;
|
1593
1911
|
}
|
1594
|
-
|
1912
|
+
|
1913
|
+
const bit = this.currentByte >> this.shift & 1;
|
1595
1914
|
this.shift--;
|
1596
1915
|
return bit;
|
1597
1916
|
},
|
1598
|
-
|
1599
|
-
|
1600
|
-
|
1917
|
+
|
1918
|
+
readBits(numBits) {
|
1919
|
+
let result = 0,
|
1920
|
+
i;
|
1921
|
+
|
1601
1922
|
for (i = numBits - 1; i >= 0; i--) {
|
1602
1923
|
result |= this.readBit() << i;
|
1603
1924
|
}
|
1925
|
+
|
1604
1926
|
return result;
|
1605
1927
|
},
|
1606
|
-
|
1928
|
+
|
1929
|
+
byteAlign() {
|
1607
1930
|
this.shift = -1;
|
1608
1931
|
},
|
1609
|
-
|
1932
|
+
|
1933
|
+
next() {
|
1610
1934
|
if (this.position >= this.end) {
|
1611
1935
|
return -1;
|
1612
1936
|
}
|
1937
|
+
|
1613
1938
|
return this.data[this.position++];
|
1614
1939
|
}
|
1940
|
+
|
1615
1941
|
};
|
1942
|
+
|
1616
1943
|
function getCustomHuffmanTable(index, referredTo, customTables) {
|
1617
|
-
|
1618
|
-
|
1619
|
-
|
1620
|
-
|
1621
|
-
|
1622
|
-
table = customTables[referredTo[i]];
|
1944
|
+
let currentIndex = 0;
|
1945
|
+
|
1946
|
+
for (let i = 0, ii = referredTo.length; i < ii; i++) {
|
1947
|
+
const table = customTables[referredTo[i]];
|
1948
|
+
|
1623
1949
|
if (table) {
|
1624
1950
|
if (index === currentIndex) {
|
1625
1951
|
return table;
|
1626
1952
|
}
|
1953
|
+
|
1627
1954
|
currentIndex++;
|
1628
1955
|
}
|
1629
1956
|
}
|
1630
|
-
|
1957
|
+
|
1958
|
+
throw new Jbig2Error("can't find custom Huffman table");
|
1631
1959
|
}
|
1960
|
+
|
1632
1961
|
function getTextRegionHuffmanTables(textRegion, referredTo, customTables, numberOfSymbols, reader) {
|
1633
|
-
|
1634
|
-
|
1635
|
-
|
1636
|
-
|
1637
|
-
codeLength = reader.readBits(4);
|
1962
|
+
const codes = [];
|
1963
|
+
|
1964
|
+
for (let i = 0; i <= 34; i++) {
|
1965
|
+
const codeLength = reader.readBits(4);
|
1638
1966
|
codes.push(new HuffmanLine([i, codeLength, 0, 0]));
|
1639
1967
|
}
|
1640
|
-
|
1968
|
+
|
1969
|
+
const runCodesTable = new HuffmanTable(codes, false);
|
1641
1970
|
codes.length = 0;
|
1642
|
-
|
1643
|
-
|
1971
|
+
|
1972
|
+
for (let i = 0; i < numberOfSymbols;) {
|
1973
|
+
const codeLength = runCodesTable.decode(reader);
|
1974
|
+
|
1644
1975
|
if (codeLength >= 32) {
|
1645
|
-
|
1646
|
-
|
1647
|
-
j = void 0;
|
1976
|
+
let repeatedLength, numberOfRepeats, j;
|
1977
|
+
|
1648
1978
|
switch (codeLength) {
|
1649
1979
|
case 32:
|
1650
1980
|
if (i === 0) {
|
1651
|
-
throw new Jbig2Error(
|
1981
|
+
throw new Jbig2Error("no previous value in symbol ID table");
|
1652
1982
|
}
|
1983
|
+
|
1653
1984
|
numberOfRepeats = reader.readBits(2) + 3;
|
1654
1985
|
repeatedLength = codes[i - 1].prefixLength;
|
1655
1986
|
break;
|
1987
|
+
|
1656
1988
|
case 33:
|
1657
1989
|
numberOfRepeats = reader.readBits(3) + 3;
|
1658
1990
|
repeatedLength = 0;
|
1659
1991
|
break;
|
1992
|
+
|
1660
1993
|
case 34:
|
1661
1994
|
numberOfRepeats = reader.readBits(7) + 11;
|
1662
1995
|
repeatedLength = 0;
|
1663
1996
|
break;
|
1997
|
+
|
1664
1998
|
default:
|
1665
|
-
throw new Jbig2Error(
|
1999
|
+
throw new Jbig2Error("invalid code length in symbol ID table");
|
1666
2000
|
}
|
2001
|
+
|
1667
2002
|
for (j = 0; j < numberOfRepeats; j++) {
|
1668
2003
|
codes.push(new HuffmanLine([i, repeatedLength, 0, 0]));
|
1669
2004
|
i++;
|
@@ -1673,182 +2008,216 @@ var Jbig2Image = function Jbig2ImageClosure() {
|
|
1673
2008
|
i++;
|
1674
2009
|
}
|
1675
2010
|
}
|
2011
|
+
|
1676
2012
|
reader.byteAlign();
|
1677
|
-
|
1678
|
-
|
1679
|
-
tableFirstS
|
1680
|
-
tableDeltaS
|
1681
|
-
tableDeltaT
|
2013
|
+
const symbolIDTable = new HuffmanTable(codes, false);
|
2014
|
+
let customIndex = 0,
|
2015
|
+
tableFirstS,
|
2016
|
+
tableDeltaS,
|
2017
|
+
tableDeltaT;
|
2018
|
+
|
1682
2019
|
switch (textRegion.huffmanFS) {
|
1683
2020
|
case 0:
|
1684
2021
|
case 1:
|
1685
2022
|
tableFirstS = getStandardTable(textRegion.huffmanFS + 6);
|
1686
2023
|
break;
|
2024
|
+
|
1687
2025
|
case 3:
|
1688
2026
|
tableFirstS = getCustomHuffmanTable(customIndex, referredTo, customTables);
|
1689
2027
|
customIndex++;
|
1690
2028
|
break;
|
2029
|
+
|
1691
2030
|
default:
|
1692
|
-
throw new Jbig2Error(
|
2031
|
+
throw new Jbig2Error("invalid Huffman FS selector");
|
1693
2032
|
}
|
2033
|
+
|
1694
2034
|
switch (textRegion.huffmanDS) {
|
1695
2035
|
case 0:
|
1696
2036
|
case 1:
|
1697
2037
|
case 2:
|
1698
2038
|
tableDeltaS = getStandardTable(textRegion.huffmanDS + 8);
|
1699
2039
|
break;
|
2040
|
+
|
1700
2041
|
case 3:
|
1701
2042
|
tableDeltaS = getCustomHuffmanTable(customIndex, referredTo, customTables);
|
1702
2043
|
customIndex++;
|
1703
2044
|
break;
|
2045
|
+
|
1704
2046
|
default:
|
1705
|
-
throw new Jbig2Error(
|
2047
|
+
throw new Jbig2Error("invalid Huffman DS selector");
|
1706
2048
|
}
|
2049
|
+
|
1707
2050
|
switch (textRegion.huffmanDT) {
|
1708
2051
|
case 0:
|
1709
2052
|
case 1:
|
1710
2053
|
case 2:
|
1711
2054
|
tableDeltaT = getStandardTable(textRegion.huffmanDT + 11);
|
1712
2055
|
break;
|
2056
|
+
|
1713
2057
|
case 3:
|
1714
2058
|
tableDeltaT = getCustomHuffmanTable(customIndex, referredTo, customTables);
|
1715
2059
|
customIndex++;
|
1716
2060
|
break;
|
2061
|
+
|
1717
2062
|
default:
|
1718
|
-
throw new Jbig2Error(
|
2063
|
+
throw new Jbig2Error("invalid Huffman DT selector");
|
1719
2064
|
}
|
2065
|
+
|
1720
2066
|
if (textRegion.refinement) {
|
1721
|
-
throw new Jbig2Error(
|
2067
|
+
throw new Jbig2Error("refinement with Huffman is not supported");
|
1722
2068
|
}
|
2069
|
+
|
1723
2070
|
return {
|
1724
|
-
symbolIDTable
|
1725
|
-
tableFirstS
|
1726
|
-
tableDeltaS
|
1727
|
-
tableDeltaT
|
2071
|
+
symbolIDTable,
|
2072
|
+
tableFirstS,
|
2073
|
+
tableDeltaS,
|
2074
|
+
tableDeltaT
|
1728
2075
|
};
|
1729
2076
|
}
|
2077
|
+
|
1730
2078
|
function getSymbolDictionaryHuffmanTables(dictionary, referredTo, customTables) {
|
1731
|
-
|
1732
|
-
tableDeltaHeight
|
1733
|
-
tableDeltaWidth
|
2079
|
+
let customIndex = 0,
|
2080
|
+
tableDeltaHeight,
|
2081
|
+
tableDeltaWidth;
|
2082
|
+
|
1734
2083
|
switch (dictionary.huffmanDHSelector) {
|
1735
2084
|
case 0:
|
1736
2085
|
case 1:
|
1737
2086
|
tableDeltaHeight = getStandardTable(dictionary.huffmanDHSelector + 4);
|
1738
2087
|
break;
|
2088
|
+
|
1739
2089
|
case 3:
|
1740
2090
|
tableDeltaHeight = getCustomHuffmanTable(customIndex, referredTo, customTables);
|
1741
2091
|
customIndex++;
|
1742
2092
|
break;
|
2093
|
+
|
1743
2094
|
default:
|
1744
|
-
throw new Jbig2Error(
|
2095
|
+
throw new Jbig2Error("invalid Huffman DH selector");
|
1745
2096
|
}
|
2097
|
+
|
1746
2098
|
switch (dictionary.huffmanDWSelector) {
|
1747
2099
|
case 0:
|
1748
2100
|
case 1:
|
1749
2101
|
tableDeltaWidth = getStandardTable(dictionary.huffmanDWSelector + 2);
|
1750
2102
|
break;
|
2103
|
+
|
1751
2104
|
case 3:
|
1752
2105
|
tableDeltaWidth = getCustomHuffmanTable(customIndex, referredTo, customTables);
|
1753
2106
|
customIndex++;
|
1754
2107
|
break;
|
2108
|
+
|
1755
2109
|
default:
|
1756
|
-
throw new Jbig2Error(
|
2110
|
+
throw new Jbig2Error("invalid Huffman DW selector");
|
1757
2111
|
}
|
1758
|
-
|
1759
|
-
|
2112
|
+
|
2113
|
+
let tableBitmapSize, tableAggregateInstances;
|
2114
|
+
|
1760
2115
|
if (dictionary.bitmapSizeSelector) {
|
1761
2116
|
tableBitmapSize = getCustomHuffmanTable(customIndex, referredTo, customTables);
|
1762
2117
|
customIndex++;
|
1763
2118
|
} else {
|
1764
2119
|
tableBitmapSize = getStandardTable(1);
|
1765
2120
|
}
|
2121
|
+
|
1766
2122
|
if (dictionary.aggregationInstancesSelector) {
|
1767
2123
|
tableAggregateInstances = getCustomHuffmanTable(customIndex, referredTo, customTables);
|
1768
2124
|
} else {
|
1769
2125
|
tableAggregateInstances = getStandardTable(1);
|
1770
2126
|
}
|
2127
|
+
|
1771
2128
|
return {
|
1772
|
-
tableDeltaHeight
|
1773
|
-
tableDeltaWidth
|
1774
|
-
tableBitmapSize
|
1775
|
-
tableAggregateInstances
|
2129
|
+
tableDeltaHeight,
|
2130
|
+
tableDeltaWidth,
|
2131
|
+
tableBitmapSize,
|
2132
|
+
tableAggregateInstances
|
1776
2133
|
};
|
1777
2134
|
}
|
2135
|
+
|
1778
2136
|
function readUncompressedBitmap(reader, width, height) {
|
1779
|
-
|
1780
|
-
|
1781
|
-
|
1782
|
-
|
1783
|
-
for (y = 0; y < height; y++) {
|
1784
|
-
row = new Uint8Array(width);
|
2137
|
+
const bitmap = [];
|
2138
|
+
|
2139
|
+
for (let y = 0; y < height; y++) {
|
2140
|
+
const row = new Uint8Array(width);
|
1785
2141
|
bitmap.push(row);
|
1786
|
-
|
2142
|
+
|
2143
|
+
for (let x = 0; x < width; x++) {
|
1787
2144
|
row[x] = reader.readBit();
|
1788
2145
|
}
|
2146
|
+
|
1789
2147
|
reader.byteAlign();
|
1790
2148
|
}
|
2149
|
+
|
1791
2150
|
return bitmap;
|
1792
2151
|
}
|
2152
|
+
|
1793
2153
|
function decodeMMRBitmap(input, width, height, endOfBlock) {
|
1794
|
-
|
2154
|
+
const params = {
|
1795
2155
|
K: -1,
|
1796
2156
|
Columns: width,
|
1797
2157
|
Rows: height,
|
1798
2158
|
BlackIs1: true,
|
1799
2159
|
EndOfBlock: endOfBlock
|
1800
2160
|
};
|
1801
|
-
|
1802
|
-
|
1803
|
-
|
1804
|
-
y = void 0,
|
1805
|
-
row = void 0,
|
1806
|
-
currentByte = void 0,
|
1807
|
-
shift = void 0,
|
2161
|
+
const decoder = new _ccitt.CCITTFaxDecoder(input, params);
|
2162
|
+
const bitmap = [];
|
2163
|
+
let currentByte,
|
1808
2164
|
eof = false;
|
1809
|
-
|
1810
|
-
|
2165
|
+
|
2166
|
+
for (let y = 0; y < height; y++) {
|
2167
|
+
const row = new Uint8Array(width);
|
1811
2168
|
bitmap.push(row);
|
1812
|
-
shift = -1;
|
1813
|
-
|
2169
|
+
let shift = -1;
|
2170
|
+
|
2171
|
+
for (let x = 0; x < width; x++) {
|
1814
2172
|
if (shift < 0) {
|
1815
2173
|
currentByte = decoder.readNextChar();
|
2174
|
+
|
1816
2175
|
if (currentByte === -1) {
|
1817
2176
|
currentByte = 0;
|
1818
2177
|
eof = true;
|
1819
2178
|
}
|
2179
|
+
|
1820
2180
|
shift = 7;
|
1821
2181
|
}
|
2182
|
+
|
1822
2183
|
row[x] = currentByte >> shift & 1;
|
1823
2184
|
shift--;
|
1824
2185
|
}
|
1825
2186
|
}
|
2187
|
+
|
1826
2188
|
if (endOfBlock && !eof) {
|
1827
|
-
|
1828
|
-
|
2189
|
+
const lookForEOFLimit = 5;
|
2190
|
+
|
2191
|
+
for (let i = 0; i < lookForEOFLimit; i++) {
|
1829
2192
|
if (decoder.readNextChar() === -1) {
|
1830
2193
|
break;
|
1831
2194
|
}
|
1832
2195
|
}
|
1833
2196
|
}
|
2197
|
+
|
1834
2198
|
return bitmap;
|
1835
2199
|
}
|
2200
|
+
|
1836
2201
|
function Jbig2Image() {}
|
2202
|
+
|
1837
2203
|
Jbig2Image.prototype = {
|
1838
|
-
parseChunks
|
2204
|
+
parseChunks(chunks) {
|
1839
2205
|
return parseJbig2Chunks(chunks);
|
1840
2206
|
},
|
1841
|
-
parse: function parse(data) {
|
1842
|
-
var _parseJbig = parseJbig2(data),
|
1843
|
-
imgData = _parseJbig.imgData,
|
1844
|
-
width = _parseJbig.width,
|
1845
|
-
height = _parseJbig.height;
|
1846
2207
|
|
2208
|
+
parse(data) {
|
2209
|
+
const {
|
2210
|
+
imgData,
|
2211
|
+
width,
|
2212
|
+
height
|
2213
|
+
} = parseJbig2(data);
|
1847
2214
|
this.width = width;
|
1848
2215
|
this.height = height;
|
1849
2216
|
return imgData;
|
1850
2217
|
}
|
2218
|
+
|
1851
2219
|
};
|
1852
2220
|
return Jbig2Image;
|
1853
2221
|
}();
|
2222
|
+
|
1854
2223
|
exports.Jbig2Image = Jbig2Image;
|