jpeg_stream.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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.JpegStream = void 0;
  27. var _decode_stream = require("./decode_stream.js");
  28. var _primitives = require("./primitives.js");
  29. var _jpg = require("./jpg.js");
  30. var _util = require("../shared/util.js");
  31. class JpegStream extends _decode_stream.DecodeStream {
  32. constructor(stream, maybeLength, params) {
  33. let ch;
  34. while ((ch = stream.getByte()) !== -1) {
  35. if (ch === 0xff) {
  36. stream.skip(-1);
  37. break;
  38. }
  39. }
  40. super(maybeLength);
  41. this.stream = stream;
  42. this.dict = stream.dict;
  43. this.maybeLength = maybeLength;
  44. this.params = params;
  45. }
  46. get bytes() {
  47. return (0, _util.shadow)(this, "bytes", this.stream.getBytes(this.maybeLength));
  48. }
  49. ensureBuffer(requested) {}
  50. readBlock() {
  51. if (this.eof) {
  52. return;
  53. }
  54. const jpegOptions = {
  55. decodeTransform: undefined,
  56. colorTransform: undefined
  57. };
  58. const decodeArr = this.dict.getArray("D", "Decode");
  59. if (this.forceRGB && Array.isArray(decodeArr)) {
  60. const bitsPerComponent = this.dict.get("BPC", "BitsPerComponent") || 8;
  61. const decodeArrLength = decodeArr.length;
  62. const transform = new Int32Array(decodeArrLength);
  63. let transformNeeded = false;
  64. const maxValue = (1 << bitsPerComponent) - 1;
  65. for (let i = 0; i < decodeArrLength; i += 2) {
  66. transform[i] = (decodeArr[i + 1] - decodeArr[i]) * 256 | 0;
  67. transform[i + 1] = decodeArr[i] * maxValue | 0;
  68. if (transform[i] !== 256 || transform[i + 1] !== 0) {
  69. transformNeeded = true;
  70. }
  71. }
  72. if (transformNeeded) {
  73. jpegOptions.decodeTransform = transform;
  74. }
  75. }
  76. if (this.params instanceof _primitives.Dict) {
  77. const colorTransform = this.params.get("ColorTransform");
  78. if (Number.isInteger(colorTransform)) {
  79. jpegOptions.colorTransform = colorTransform;
  80. }
  81. }
  82. const jpegImage = new _jpg.JpegImage(jpegOptions);
  83. jpegImage.parse(this.bytes);
  84. const data = jpegImage.getData({
  85. width: this.drawWidth,
  86. height: this.drawHeight,
  87. forceRGB: this.forceRGB,
  88. isSourcePDF: true
  89. });
  90. this.buffer = data;
  91. this.bufferLength = data.length;
  92. this.eof = true;
  93. }
  94. }
  95. exports.JpegStream = JpegStream;