jpeg_stream.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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");
  28. var _stream = require("./stream");
  29. var _primitives = require("./primitives");
  30. var _jpg = require("./jpg");
  31. var JpegStream = function JpegStreamClosure() {
  32. function JpegStream(stream, maybeLength, dict, params) {
  33. var 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. var jpegOptions = {
  59. decodeTransform: undefined,
  60. colorTransform: undefined
  61. };
  62. var decodeArr = this.dict.getArray('Decode', 'D');
  63. if (this.forceRGB && Array.isArray(decodeArr)) {
  64. var bitsPerComponent = this.dict.get('BitsPerComponent') || 8;
  65. var decodeArrLength = decodeArr.length;
  66. var transform = new Int32Array(decodeArrLength);
  67. var transformNeeded = false;
  68. var maxValue = (1 << bitsPerComponent) - 1;
  69. for (var 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. var colorTransform = this.params.get('ColorTransform');
  82. if (Number.isInteger(colorTransform)) {
  83. jpegOptions.colorTransform = colorTransform;
  84. }
  85. }
  86. var jpegImage = new _jpg.JpegImage(jpegOptions);
  87. jpegImage.parse(this.bytes);
  88. var 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. JpegStream.prototype.getIR = function () {
  99. var forceDataSchema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  100. return (0, _util.createObjectURL)(this.bytes, 'image/jpeg', forceDataSchema);
  101. };
  102. return JpegStream;
  103. }();
  104. exports.JpegStream = JpegStream;