jpx_stream.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 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.JpxStream = void 0;
  27. var _decode_stream = require("./decode_stream.js");
  28. var _jpx = require("./jpx.js");
  29. var _util = require("../shared/util.js");
  30. class JpxStream extends _decode_stream.DecodeStream {
  31. constructor(stream, maybeLength, params) {
  32. super(maybeLength);
  33. this.stream = stream;
  34. this.dict = stream.dict;
  35. this.maybeLength = maybeLength;
  36. this.params = params;
  37. }
  38. get bytes() {
  39. return (0, _util.shadow)(this, "bytes", this.stream.getBytes(this.maybeLength));
  40. }
  41. ensureBuffer(requested) {}
  42. readBlock() {
  43. if (this.eof) {
  44. return;
  45. }
  46. const jpxImage = new _jpx.JpxImage();
  47. jpxImage.parse(this.bytes);
  48. const width = jpxImage.width;
  49. const height = jpxImage.height;
  50. const componentsCount = jpxImage.componentsCount;
  51. const tileCount = jpxImage.tiles.length;
  52. if (tileCount === 1) {
  53. this.buffer = jpxImage.tiles[0].items;
  54. } else {
  55. const data = new Uint8ClampedArray(width * height * componentsCount);
  56. for (let k = 0; k < tileCount; k++) {
  57. const tileComponents = jpxImage.tiles[k];
  58. const tileWidth = tileComponents.width;
  59. const tileHeight = tileComponents.height;
  60. const tileLeft = tileComponents.left;
  61. const tileTop = tileComponents.top;
  62. const src = tileComponents.items;
  63. let srcPosition = 0;
  64. let dataPosition = (width * tileTop + tileLeft) * componentsCount;
  65. const imgRowSize = width * componentsCount;
  66. const tileRowSize = tileWidth * componentsCount;
  67. for (let j = 0; j < tileHeight; j++) {
  68. const rowBytes = src.subarray(srcPosition, srcPosition + tileRowSize);
  69. data.set(rowBytes, dataPosition);
  70. srcPosition += tileRowSize;
  71. dataPosition += imgRowSize;
  72. }
  73. }
  74. this.buffer = data;
  75. }
  76. this.bufferLength = this.buffer.length;
  77. this.eof = true;
  78. }
  79. }
  80. exports.JpxStream = JpxStream;