2
0

jpx_stream.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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.JpxStream = undefined;
  20. var _stream = require('./stream');
  21. var _jpx = require('./jpx');
  22. var _util = require('../shared/util');
  23. var JpxStream = function JpxStreamClosure() {
  24. function JpxStream(stream, maybeLength, dict, params) {
  25. this.stream = stream;
  26. this.maybeLength = maybeLength;
  27. this.dict = dict;
  28. this.params = params;
  29. _stream.DecodeStream.call(this, maybeLength);
  30. }
  31. JpxStream.prototype = Object.create(_stream.DecodeStream.prototype);
  32. Object.defineProperty(JpxStream.prototype, 'bytes', {
  33. get: function JpxStream_bytes() {
  34. return (0, _util.shadow)(this, 'bytes', this.stream.getBytes(this.maybeLength));
  35. },
  36. configurable: true
  37. });
  38. JpxStream.prototype.ensureBuffer = function (requested) {};
  39. JpxStream.prototype.readBlock = function () {
  40. if (this.eof) {
  41. return;
  42. }
  43. var jpxImage = new _jpx.JpxImage();
  44. jpxImage.parse(this.bytes);
  45. var width = jpxImage.width;
  46. var height = jpxImage.height;
  47. var componentsCount = jpxImage.componentsCount;
  48. var tileCount = jpxImage.tiles.length;
  49. if (tileCount === 1) {
  50. this.buffer = jpxImage.tiles[0].items;
  51. } else {
  52. var data = new Uint8ClampedArray(width * height * componentsCount);
  53. for (var k = 0; k < tileCount; k++) {
  54. var tileComponents = jpxImage.tiles[k];
  55. var tileWidth = tileComponents.width;
  56. var tileHeight = tileComponents.height;
  57. var tileLeft = tileComponents.left;
  58. var tileTop = tileComponents.top;
  59. var src = tileComponents.items;
  60. var srcPosition = 0;
  61. var dataPosition = (width * tileTop + tileLeft) * componentsCount;
  62. var imgRowSize = width * componentsCount;
  63. var tileRowSize = tileWidth * componentsCount;
  64. for (var j = 0; j < tileHeight; j++) {
  65. var rowBytes = src.subarray(srcPosition, srcPosition + tileRowSize);
  66. data.set(rowBytes, dataPosition);
  67. srcPosition += tileRowSize;
  68. dataPosition += imgRowSize;
  69. }
  70. }
  71. this.buffer = data;
  72. }
  73. this.bufferLength = this.buffer.length;
  74. this.eof = true;
  75. };
  76. return JpxStream;
  77. }();
  78. exports.JpxStream = JpxStream;