jbig2_stream.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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.Jbig2Stream = void 0;
  27. var _base_stream = require("./base_stream.js");
  28. var _decode_stream = require("./decode_stream.js");
  29. var _primitives = require("./primitives.js");
  30. var _jbig = require("./jbig2.js");
  31. var _util = require("../shared/util.js");
  32. class Jbig2Stream extends _decode_stream.DecodeStream {
  33. constructor(stream, maybeLength, params) {
  34. super(maybeLength);
  35. this.stream = stream;
  36. this.dict = stream.dict;
  37. this.maybeLength = maybeLength;
  38. this.params = params;
  39. }
  40. get bytes() {
  41. return (0, _util.shadow)(this, "bytes", this.stream.getBytes(this.maybeLength));
  42. }
  43. ensureBuffer(requested) {}
  44. readBlock() {
  45. if (this.eof) {
  46. return;
  47. }
  48. const jbig2Image = new _jbig.Jbig2Image();
  49. const chunks = [];
  50. if (this.params instanceof _primitives.Dict) {
  51. const globalsStream = this.params.get("JBIG2Globals");
  52. if (globalsStream instanceof _base_stream.BaseStream) {
  53. const globals = globalsStream.getBytes();
  54. chunks.push({
  55. data: globals,
  56. start: 0,
  57. end: globals.length
  58. });
  59. }
  60. }
  61. chunks.push({
  62. data: this.bytes,
  63. start: 0,
  64. end: this.bytes.length
  65. });
  66. const data = jbig2Image.parseChunks(chunks);
  67. const dataLength = data.length;
  68. for (let i = 0; i < dataLength; i++) {
  69. data[i] ^= 0xff;
  70. }
  71. this.buffer = data;
  72. this.bufferLength = dataLength;
  73. this.eof = true;
  74. }
  75. }
  76. exports.Jbig2Stream = Jbig2Stream;