jpeg_stream.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /* Copyright 2017 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. 'use strict';
  16. Object.defineProperty(exports, "__esModule", {
  17. value: true
  18. });
  19. exports.JpegStream = undefined;
  20. var _util = require('../shared/util');
  21. var _stream = require('./stream');
  22. var _primitives = require('./primitives');
  23. var _jpg = require('./jpg');
  24. var JpegStream = function JpegStreamClosure() {
  25. function JpegStream(stream, maybeLength, dict, params) {
  26. var ch = void 0;
  27. while ((ch = stream.getByte()) !== -1) {
  28. if (ch === 0xFF) {
  29. stream.skip(-1);
  30. break;
  31. }
  32. }
  33. this.stream = stream;
  34. this.maybeLength = maybeLength;
  35. this.dict = dict;
  36. this.params = params;
  37. _stream.DecodeStream.call(this, maybeLength);
  38. }
  39. JpegStream.prototype = Object.create(_stream.DecodeStream.prototype);
  40. Object.defineProperty(JpegStream.prototype, 'bytes', {
  41. get: function JpegStream_bytes() {
  42. return (0, _util.shadow)(this, 'bytes', this.stream.getBytes(this.maybeLength));
  43. },
  44. configurable: true
  45. });
  46. JpegStream.prototype.ensureBuffer = function (requested) {};
  47. JpegStream.prototype.readBlock = function () {
  48. if (this.eof) {
  49. return;
  50. }
  51. var jpegImage = new _jpg.JpegImage();
  52. var decodeArr = this.dict.getArray('Decode', 'D');
  53. if (this.forceRGB && Array.isArray(decodeArr)) {
  54. var bitsPerComponent = this.dict.get('BitsPerComponent') || 8;
  55. var decodeArrLength = decodeArr.length;
  56. var transform = new Int32Array(decodeArrLength);
  57. var transformNeeded = false;
  58. var maxValue = (1 << bitsPerComponent) - 1;
  59. for (var i = 0; i < decodeArrLength; i += 2) {
  60. transform[i] = (decodeArr[i + 1] - decodeArr[i]) * 256 | 0;
  61. transform[i + 1] = decodeArr[i] * maxValue | 0;
  62. if (transform[i] !== 256 || transform[i + 1] !== 0) {
  63. transformNeeded = true;
  64. }
  65. }
  66. if (transformNeeded) {
  67. jpegImage.decodeTransform = transform;
  68. }
  69. }
  70. if ((0, _primitives.isDict)(this.params)) {
  71. var colorTransform = this.params.get('ColorTransform');
  72. if (Number.isInteger(colorTransform)) {
  73. jpegImage.colorTransform = colorTransform;
  74. }
  75. }
  76. jpegImage.parse(this.bytes);
  77. var data = jpegImage.getData(this.drawWidth, this.drawHeight, this.forceRGB);
  78. this.buffer = data;
  79. this.bufferLength = data.length;
  80. this.eof = true;
  81. };
  82. JpegStream.prototype.getBytes = function (length) {
  83. this.readBlock();
  84. return this.buffer;
  85. };
  86. JpegStream.prototype.getIR = function () {
  87. var forceDataSchema = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
  88. return (0, _util.createObjectURL)(this.bytes, 'image/jpeg', forceDataSchema);
  89. };
  90. return JpegStream;
  91. }();
  92. exports.JpegStream = JpegStream;