UNPKG

3.13 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.JpegStream = void 0;
28
29var _decode_stream = require("./decode_stream.js");
30
31var _primitives = require("./primitives.js");
32
33var _jpg = require("./jpg.js");
34
35var _util = require("../shared/util.js");
36
37class JpegStream extends _decode_stream.DecodeStream {
38 constructor(stream, maybeLength, params) {
39 let ch;
40
41 while ((ch = stream.getByte()) !== -1) {
42 if (ch === 0xff) {
43 stream.skip(-1);
44 break;
45 }
46 }
47
48 super(maybeLength);
49 this.stream = stream;
50 this.dict = stream.dict;
51 this.maybeLength = maybeLength;
52 this.params = params;
53 }
54
55 get bytes() {
56 return (0, _util.shadow)(this, "bytes", this.stream.getBytes(this.maybeLength));
57 }
58
59 ensureBuffer(requested) {}
60
61 readBlock() {
62 if (this.eof) {
63 return;
64 }
65
66 const jpegOptions = {
67 decodeTransform: undefined,
68 colorTransform: undefined
69 };
70 const decodeArr = this.dict.getArray("D", "Decode");
71
72 if (this.forceRGB && Array.isArray(decodeArr)) {
73 const bitsPerComponent = this.dict.get("BPC", "BitsPerComponent") || 8;
74 const decodeArrLength = decodeArr.length;
75 const transform = new Int32Array(decodeArrLength);
76 let transformNeeded = false;
77 const maxValue = (1 << bitsPerComponent) - 1;
78
79 for (let i = 0; i < decodeArrLength; i += 2) {
80 transform[i] = (decodeArr[i + 1] - decodeArr[i]) * 256 | 0;
81 transform[i + 1] = decodeArr[i] * maxValue | 0;
82
83 if (transform[i] !== 256 || transform[i + 1] !== 0) {
84 transformNeeded = true;
85 }
86 }
87
88 if (transformNeeded) {
89 jpegOptions.decodeTransform = transform;
90 }
91 }
92
93 if (this.params instanceof _primitives.Dict) {
94 const colorTransform = this.params.get("ColorTransform");
95
96 if (Number.isInteger(colorTransform)) {
97 jpegOptions.colorTransform = colorTransform;
98 }
99 }
100
101 const jpegImage = new _jpg.JpegImage(jpegOptions);
102 jpegImage.parse(this.bytes);
103 const data = jpegImage.getData({
104 width: this.drawWidth,
105 height: this.drawHeight,
106 forceRGB: this.forceRGB,
107 isSourcePDF: true
108 });
109 this.buffer = data;
110 this.bufferLength = data.length;
111 this.eof = true;
112 }
113
114}
115
116exports.JpegStream = JpegStream;
\No newline at end of file