UNPKG

2.82 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * JavaScript code in this page
4 *
5 * Copyright 2022 Mozilla Foundation
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * @licend The above is the entire license notice for the
20 * JavaScript code in this page
21 */
22"use strict";
23
24Object.defineProperty(exports, "__esModule", {
25 value: true
26});
27exports.JpxStream = void 0;
28
29var _decode_stream = require("./decode_stream.js");
30
31var _jpx = require("./jpx.js");
32
33var _util = require("../shared/util.js");
34
35class JpxStream extends _decode_stream.DecodeStream {
36 constructor(stream, maybeLength, params) {
37 super(maybeLength);
38 this.stream = stream;
39 this.dict = stream.dict;
40 this.maybeLength = maybeLength;
41 this.params = params;
42 }
43
44 get bytes() {
45 return (0, _util.shadow)(this, "bytes", this.stream.getBytes(this.maybeLength));
46 }
47
48 ensureBuffer(requested) {}
49
50 readBlock() {
51 if (this.eof) {
52 return;
53 }
54
55 const jpxImage = new _jpx.JpxImage();
56 jpxImage.parse(this.bytes);
57 const width = jpxImage.width;
58 const height = jpxImage.height;
59 const componentsCount = jpxImage.componentsCount;
60 const tileCount = jpxImage.tiles.length;
61
62 if (tileCount === 1) {
63 this.buffer = jpxImage.tiles[0].items;
64 } else {
65 const data = new Uint8ClampedArray(width * height * componentsCount);
66
67 for (let k = 0; k < tileCount; k++) {
68 const tileComponents = jpxImage.tiles[k];
69 const tileWidth = tileComponents.width;
70 const tileHeight = tileComponents.height;
71 const tileLeft = tileComponents.left;
72 const tileTop = tileComponents.top;
73 const src = tileComponents.items;
74 let srcPosition = 0;
75 let dataPosition = (width * tileTop + tileLeft) * componentsCount;
76 const imgRowSize = width * componentsCount;
77 const tileRowSize = tileWidth * componentsCount;
78
79 for (let j = 0; j < tileHeight; j++) {
80 const rowBytes = src.subarray(srcPosition, srcPosition + tileRowSize);
81 data.set(rowBytes, dataPosition);
82 srcPosition += tileRowSize;
83 dataPosition += imgRowSize;
84 }
85 }
86
87 this.buffer = data;
88 }
89
90 this.bufferLength = this.buffer.length;
91 this.eof = true;
92 }
93
94}
95
96exports.JpxStream = JpxStream;
\No newline at end of file