|
13 | 13 | * limitations under the License. |
14 | 14 | */ |
15 | 15 |
|
| 16 | +import { shadow, warn } from "../shared/util.js"; |
16 | 17 | import { CCITTFaxDecoder } from "./ccitt.js"; |
17 | 18 | import { DecodeStream } from "./decode_stream.js"; |
18 | 19 | import { Dict } from "./primitives.js"; |
| 20 | +import { JBig2CCITTFaxWasmImage } from "./jbig2_ccittFax_wasm.js"; |
19 | 21 |
|
20 | 22 | class CCITTFaxStream extends DecodeStream { |
21 | 23 | constructor(str, maybeLength, params) { |
22 | 24 | super(maybeLength); |
23 | 25 |
|
24 | 26 | this.stream = str; |
| 27 | + this.maybeLength = maybeLength; |
25 | 28 | this.dict = str.dict; |
26 | 29 |
|
27 | 30 | if (!(params instanceof Dict)) { |
28 | 31 | params = Dict.empty; |
29 | 32 | } |
30 | 33 |
|
31 | | - const source = { |
32 | | - next() { |
33 | | - return str.getByte(); |
34 | | - }, |
| 34 | + this.params = { |
| 35 | + K: params.get("K") || 0, |
| 36 | + EndOfLine: !!params.get("EndOfLine"), |
| 37 | + EncodedByteAlign: !!params.get("EncodedByteAlign"), |
| 38 | + Columns: params.get("Columns") || 1728, |
| 39 | + Rows: params.get("Rows") || 0, |
| 40 | + EndOfBlock: !!(params.get("EndOfBlock") ?? true), |
| 41 | + BlackIs1: !!params.get("BlackIs1"), |
35 | 42 | }; |
36 | | - this.ccittFaxDecoder = new CCITTFaxDecoder(source, { |
37 | | - K: params.get("K"), |
38 | | - EndOfLine: params.get("EndOfLine"), |
39 | | - EncodedByteAlign: params.get("EncodedByteAlign"), |
40 | | - Columns: params.get("Columns"), |
41 | | - Rows: params.get("Rows"), |
42 | | - EndOfBlock: params.get("EndOfBlock"), |
43 | | - BlackIs1: params.get("BlackIs1"), |
44 | | - }); |
| 43 | + } |
| 44 | + |
| 45 | + get bytes() { |
| 46 | + // If `this.maybeLength` is null, we'll get the entire stream. |
| 47 | + return shadow(this, "bytes", this.stream.getBytes(this.maybeLength)); |
45 | 48 | } |
46 | 49 |
|
47 | 50 | readBlock() { |
| 51 | + this.decodeImageFallback(); |
| 52 | + } |
| 53 | + |
| 54 | + get isImageStream() { |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + get isAsyncDecoder() { |
| 59 | + return true; |
| 60 | + } |
| 61 | + |
| 62 | + async decodeImage(bytes, length, _decoderOptions) { |
| 63 | + if (this.eof) { |
| 64 | + return this.buffer; |
| 65 | + } |
| 66 | + if (!bytes) { |
| 67 | + bytes = this.stream.isAsync |
| 68 | + ? (await this.stream.asyncGetBytes()) || this.bytes |
| 69 | + : this.bytes; |
| 70 | + } |
| 71 | + |
| 72 | + try { |
| 73 | + this.buffer = await JBig2CCITTFaxWasmImage.decode( |
| 74 | + bytes, |
| 75 | + this.dict.get("W", "Width"), |
| 76 | + this.dict.get("H", "Height"), |
| 77 | + null, |
| 78 | + this.params |
| 79 | + ); |
| 80 | + } catch { |
| 81 | + warn("CCITTFaxStream: Falling back to JS CCITTFax decoder."); |
| 82 | + return this.decodeImageFallback(bytes, length); |
| 83 | + } |
| 84 | + this.bufferLength = this.buffer.length; |
| 85 | + this.eof = true; |
| 86 | + |
| 87 | + return this.buffer; |
| 88 | + } |
| 89 | + |
| 90 | + decodeImageFallback(bytes, length) { |
| 91 | + if (this.eof) { |
| 92 | + return this.buffer; |
| 93 | + } |
| 94 | + const { params } = this; |
| 95 | + if (!bytes) { |
| 96 | + this.stream.reset(); |
| 97 | + bytes = this.bytes; |
| 98 | + } |
| 99 | + let pos = 0; |
| 100 | + const source = { |
| 101 | + next() { |
| 102 | + return bytes[pos++] ?? -1; |
| 103 | + }, |
| 104 | + }; |
| 105 | + if (length && this.buffer.byteLength < length) { |
| 106 | + this.buffer = new Uint8Array(length); |
| 107 | + } |
| 108 | + this.ccittFaxDecoder = new CCITTFaxDecoder(source, params); |
| 109 | + let outPos = 0; |
48 | 110 | while (!this.eof) { |
49 | 111 | const c = this.ccittFaxDecoder.readNextChar(); |
50 | 112 | if (c === -1) { |
51 | 113 | this.eof = true; |
52 | | - return; |
| 114 | + break; |
53 | 115 | } |
54 | | - this.ensureBuffer(this.bufferLength + 1); |
55 | | - this.buffer[this.bufferLength++] = c; |
| 116 | + if (!length) { |
| 117 | + this.ensureBuffer(outPos + 1); |
| 118 | + } |
| 119 | + this.buffer[outPos++] = c; |
56 | 120 | } |
57 | | - } |
58 | 121 |
|
59 | | - get isImageStream() { |
60 | | - return true; |
| 122 | + this.bufferLength = this.buffer.length; |
| 123 | + return this.buffer.subarray(0, length || this.bufferLength); |
61 | 124 | } |
62 | 125 | } |
63 | 126 |
|
|
0 commit comments