jpeg_stream.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.JpegStream = void 0;
  27. var _util = require("../shared/util.js");
  28. var _stream = require("./stream.js");
  29. var _primitives = require("./primitives.js");
  30. var _jpg = require("./jpg.js");
  31. const JpegStream = function JpegStreamClosure() {
  32. function JpegStream(stream, maybeLength, dict, params) {
  33. let ch;
  34. while ((ch = stream.getByte()) !== -1) {
  35. if (ch === 0xff) {
  36. stream.skip(-1);
  37. break;
  38. }
  39. }
  40. this.stream = stream;
  41. this.maybeLength = maybeLength;
  42. this.dict = dict;
  43. this.params = params;
  44. _stream.DecodeStream.call(this, maybeLength);
  45. }
  46. JpegStream.prototype = Object.create(_stream.DecodeStream.prototype);
  47. Object.defineProperty(JpegStream.prototype, "bytes", {
  48. get: function JpegStream_bytes() {
  49. return (0, _util.shadow)(this, "bytes", this.stream.getBytes(this.maybeLength));
  50. },
  51. configurable: true
  52. });
  53. JpegStream.prototype.ensureBuffer = function (requested) {};
  54. JpegStream.prototype.readBlock = function () {
  55. if (this.eof) {
  56. return;
  57. }
  58. const jpegOptions = {
  59. decodeTransform: undefined,
  60. colorTransform: undefined
  61. };
  62. const decodeArr = this.dict.getArray("Decode", "D");
  63. if (this.forceRGB && Array.isArray(decodeArr)) {
  64. const bitsPerComponent = this.dict.get("BitsPerComponent") || 8;
  65. const decodeArrLength = decodeArr.length;
  66. const transform = new Int32Array(decodeArrLength);
  67. let transformNeeded = false;
  68. const maxValue = (1 << bitsPerComponent) - 1;
  69. for (let i = 0; i < decodeArrLength; i += 2) {
  70. transform[i] = (decodeArr[i + 1] - decodeArr[i]) * 256 | 0;
  71. transform[i + 1] = decodeArr[i] * maxValue | 0;
  72. if (transform[i] !== 256 || transform[i + 1] !== 0) {
  73. transformNeeded = true;
  74. }
  75. }
  76. if (transformNeeded) {
  77. jpegOptions.decodeTransform = transform;
  78. }
  79. }
  80. if ((0, _primitives.isDict)(this.params)) {
  81. const colorTransform = this.params.get("ColorTransform");
  82. if (Number.isInteger(colorTransform)) {
  83. jpegOptions.colorTransform = colorTransform;
  84. }
  85. }
  86. const jpegImage = new _jpg.JpegImage(jpegOptions);
  87. jpegImage.parse(this.bytes);
  88. const data = jpegImage.getData({
  89. width: this.drawWidth,
  90. height: this.drawHeight,
  91. forceRGB: this.forceRGB,
  92. isSourcePDF: true
  93. });
  94. this.buffer = data;
  95. this.bufferLength = data.length;
  96. this.eof = true;
  97. };
  98. Object.defineProperty(JpegStream.prototype, "maybeValidDimensions", {
  99. get: function JpegStream_maybeValidDimensions() {
  100. const {
  101. dict,
  102. stream
  103. } = this;
  104. const dictHeight = dict.get("Height", "H");
  105. const startPos = stream.pos;
  106. let validDimensions = true,
  107. foundSOF = false,
  108. b;
  109. while ((b = stream.getByte()) !== -1) {
  110. if (b !== 0xff) {
  111. continue;
  112. }
  113. switch (stream.getByte()) {
  114. case 0xc0:
  115. case 0xc1:
  116. case 0xc2:
  117. foundSOF = true;
  118. stream.pos += 2;
  119. stream.pos += 1;
  120. const scanLines = stream.getUint16();
  121. if (scanLines === dictHeight) {
  122. break;
  123. }
  124. if (scanLines === 0) {
  125. validDimensions = false;
  126. break;
  127. }
  128. if (scanLines > dictHeight * 10) {
  129. validDimensions = false;
  130. break;
  131. }
  132. break;
  133. case 0xc3:
  134. case 0xc5:
  135. case 0xc6:
  136. case 0xc7:
  137. case 0xc9:
  138. case 0xca:
  139. case 0xcb:
  140. case 0xcd:
  141. case 0xce:
  142. case 0xcf:
  143. foundSOF = true;
  144. break;
  145. case 0xc4:
  146. case 0xcc:
  147. case 0xda:
  148. case 0xdb:
  149. case 0xdc:
  150. case 0xdd:
  151. case 0xde:
  152. case 0xdf:
  153. case 0xe0:
  154. case 0xe1:
  155. case 0xe2:
  156. case 0xe3:
  157. case 0xe4:
  158. case 0xe5:
  159. case 0xe6:
  160. case 0xe7:
  161. case 0xe8:
  162. case 0xe9:
  163. case 0xea:
  164. case 0xeb:
  165. case 0xec:
  166. case 0xed:
  167. case 0xee:
  168. case 0xef:
  169. case 0xfe:
  170. const markerLength = stream.getUint16();
  171. if (markerLength > 2) {
  172. stream.skip(markerLength - 2);
  173. } else {
  174. stream.skip(-2);
  175. }
  176. break;
  177. case 0xff:
  178. stream.skip(-1);
  179. break;
  180. case 0xd9:
  181. foundSOF = true;
  182. break;
  183. }
  184. if (foundSOF) {
  185. break;
  186. }
  187. }
  188. stream.pos = startPos;
  189. return (0, _util.shadow)(this, "maybeValidDimensions", validDimensions);
  190. },
  191. configurable: true
  192. });
  193. JpegStream.prototype.getIR = function (forceDataSchema = false) {
  194. return (0, _util.createObjectURL)(this.bytes, "image/jpeg", forceDataSchema);
  195. };
  196. return JpegStream;
  197. }();
  198. exports.JpegStream = JpegStream;