jbig2_stream.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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.Jbig2Stream = undefined;
  20. var _primitives = require('./primitives');
  21. var _stream = require('./stream');
  22. var _jbig = require('./jbig2');
  23. var _util = require('../shared/util');
  24. var Jbig2Stream = function Jbig2StreamClosure() {
  25. function Jbig2Stream(stream, maybeLength, dict, params) {
  26. this.stream = stream;
  27. this.maybeLength = maybeLength;
  28. this.dict = dict;
  29. this.params = params;
  30. _stream.DecodeStream.call(this, maybeLength);
  31. }
  32. Jbig2Stream.prototype = Object.create(_stream.DecodeStream.prototype);
  33. Object.defineProperty(Jbig2Stream.prototype, 'bytes', {
  34. get: function get() {
  35. return (0, _util.shadow)(this, 'bytes', this.stream.getBytes(this.maybeLength));
  36. },
  37. configurable: true
  38. });
  39. Jbig2Stream.prototype.ensureBuffer = function (req) {
  40. if (this.bufferLength) {
  41. return;
  42. }
  43. var jbig2Image = new _jbig.Jbig2Image();
  44. var chunks = [];
  45. if ((0, _primitives.isDict)(this.params)) {
  46. var globalsStream = this.params.get('JBIG2Globals');
  47. if ((0, _primitives.isStream)(globalsStream)) {
  48. var globals = globalsStream.getBytes();
  49. chunks.push({
  50. data: globals,
  51. start: 0,
  52. end: globals.length
  53. });
  54. }
  55. }
  56. chunks.push({
  57. data: this.bytes,
  58. start: 0,
  59. end: this.bytes.length
  60. });
  61. var data = jbig2Image.parseChunks(chunks);
  62. var dataLength = data.length;
  63. for (var i = 0; i < dataLength; i++) {
  64. data[i] ^= 0xFF;
  65. }
  66. this.buffer = data;
  67. this.bufferLength = dataLength;
  68. this.eof = true;
  69. };
  70. return Jbig2Stream;
  71. }();
  72. exports.Jbig2Stream = Jbig2Stream;