UNPKG

3.61 kBJavaScriptView Raw
1/**
2 * @licstart The following is the entire license notice for the
3 * Javascript code in this page
4 *
5 * Copyright 2018 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 _util = require("../shared/util");
30
31var _stream = require("./stream");
32
33var _primitives = require("./primitives");
34
35var _jpg = require("./jpg");
36
37var JpegStream = function JpegStreamClosure() {
38 function JpegStream(stream, maybeLength, dict, params) {
39 var ch;
40
41 while ((ch = stream.getByte()) !== -1) {
42 if (ch === 0xFF) {
43 stream.skip(-1);
44 break;
45 }
46 }
47
48 this.stream = stream;
49 this.maybeLength = maybeLength;
50 this.dict = dict;
51 this.params = params;
52
53 _stream.DecodeStream.call(this, maybeLength);
54 }
55
56 JpegStream.prototype = Object.create(_stream.DecodeStream.prototype);
57 Object.defineProperty(JpegStream.prototype, 'bytes', {
58 get: function JpegStream_bytes() {
59 return (0, _util.shadow)(this, 'bytes', this.stream.getBytes(this.maybeLength));
60 },
61 configurable: true
62 });
63
64 JpegStream.prototype.ensureBuffer = function (requested) {};
65
66 JpegStream.prototype.readBlock = function () {
67 if (this.eof) {
68 return;
69 }
70
71 var jpegOptions = {
72 decodeTransform: undefined,
73 colorTransform: undefined
74 };
75 var decodeArr = this.dict.getArray('Decode', 'D');
76
77 if (this.forceRGB && Array.isArray(decodeArr)) {
78 var bitsPerComponent = this.dict.get('BitsPerComponent') || 8;
79 var decodeArrLength = decodeArr.length;
80 var transform = new Int32Array(decodeArrLength);
81 var transformNeeded = false;
82 var maxValue = (1 << bitsPerComponent) - 1;
83
84 for (var i = 0; i < decodeArrLength; i += 2) {
85 transform[i] = (decodeArr[i + 1] - decodeArr[i]) * 256 | 0;
86 transform[i + 1] = decodeArr[i] * maxValue | 0;
87
88 if (transform[i] !== 256 || transform[i + 1] !== 0) {
89 transformNeeded = true;
90 }
91 }
92
93 if (transformNeeded) {
94 jpegOptions.decodeTransform = transform;
95 }
96 }
97
98 if ((0, _primitives.isDict)(this.params)) {
99 var colorTransform = this.params.get('ColorTransform');
100
101 if (Number.isInteger(colorTransform)) {
102 jpegOptions.colorTransform = colorTransform;
103 }
104 }
105
106 var jpegImage = new _jpg.JpegImage(jpegOptions);
107 jpegImage.parse(this.bytes);
108 var data = jpegImage.getData({
109 width: this.drawWidth,
110 height: this.drawHeight,
111 forceRGB: this.forceRGB,
112 isSourcePDF: true
113 });
114 this.buffer = data;
115 this.bufferLength = data.length;
116 this.eof = true;
117 };
118
119 JpegStream.prototype.getIR = function () {
120 var forceDataSchema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
121 return (0, _util.createObjectURL)(this.bytes, 'image/jpeg', forceDataSchema);
122 };
123
124 return JpegStream;
125}();
126
127exports.JpegStream = JpegStream;
\No newline at end of file