jpeg_stream.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2017 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 = undefined;
  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 = void 0;
  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 jpegImage = new _jpg.JpegImage();
  59. var decodeArr = this.dict.getArray('Decode', 'D');
  60. if (this.forceRGB && Array.isArray(decodeArr)) {
  61. var bitsPerComponent = this.dict.get('BitsPerComponent') || 8;
  62. var decodeArrLength = decodeArr.length;
  63. var transform = new Int32Array(decodeArrLength);
  64. var transformNeeded = false;
  65. var maxValue = (1 << bitsPerComponent) - 1;
  66. for (var i = 0; i < decodeArrLength; i += 2) {
  67. transform[i] = (decodeArr[i + 1] - decodeArr[i]) * 256 | 0;
  68. transform[i + 1] = decodeArr[i] * maxValue | 0;
  69. if (transform[i] !== 256 || transform[i + 1] !== 0) {
  70. transformNeeded = true;
  71. }
  72. }
  73. if (transformNeeded) {
  74. jpegImage.decodeTransform = transform;
  75. }
  76. }
  77. if ((0, _primitives.isDict)(this.params)) {
  78. var colorTransform = this.params.get('ColorTransform');
  79. if (Number.isInteger(colorTransform)) {
  80. jpegImage.colorTransform = colorTransform;
  81. }
  82. }
  83. jpegImage.parse(this.bytes);
  84. var data = jpegImage.getData(this.drawWidth, this.drawHeight, this.forceRGB);
  85. this.buffer = data;
  86. this.bufferLength = data.length;
  87. this.eof = true;
  88. };
  89. JpegStream.prototype.getIR = function () {
  90. var forceDataSchema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  91. return (0, _util.createObjectURL)(this.bytes, 'image/jpeg', forceDataSchema);
  92. };
  93. return JpegStream;
  94. }();
  95. exports.JpegStream = JpegStream;