image_utils.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2019 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.NativeImageDecoder = void 0;
  27. var _colorspace = require("./colorspace");
  28. var _jpeg_stream = require("./jpeg_stream");
  29. var _stream = require("./stream");
  30. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
  31. function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
  32. function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
  33. var NativeImageDecoder =
  34. /*#__PURE__*/
  35. function () {
  36. function NativeImageDecoder(_ref) {
  37. var xref = _ref.xref,
  38. resources = _ref.resources,
  39. handler = _ref.handler,
  40. _ref$forceDataSchema = _ref.forceDataSchema,
  41. forceDataSchema = _ref$forceDataSchema === void 0 ? false : _ref$forceDataSchema,
  42. pdfFunctionFactory = _ref.pdfFunctionFactory;
  43. _classCallCheck(this, NativeImageDecoder);
  44. this.xref = xref;
  45. this.resources = resources;
  46. this.handler = handler;
  47. this.forceDataSchema = forceDataSchema;
  48. this.pdfFunctionFactory = pdfFunctionFactory;
  49. }
  50. _createClass(NativeImageDecoder, [{
  51. key: "canDecode",
  52. value: function canDecode(image) {
  53. return image instanceof _jpeg_stream.JpegStream && NativeImageDecoder.isDecodable(image, this.xref, this.resources, this.pdfFunctionFactory);
  54. }
  55. }, {
  56. key: "decode",
  57. value: function decode(image) {
  58. var dict = image.dict;
  59. var colorSpace = dict.get('ColorSpace', 'CS');
  60. colorSpace = _colorspace.ColorSpace.parse(colorSpace, this.xref, this.resources, this.pdfFunctionFactory);
  61. return this.handler.sendWithPromise('JpegDecode', [image.getIR(this.forceDataSchema), colorSpace.numComps]).then(function (_ref2) {
  62. var data = _ref2.data,
  63. width = _ref2.width,
  64. height = _ref2.height;
  65. return new _stream.Stream(data, 0, data.length, dict);
  66. });
  67. }
  68. }], [{
  69. key: "isSupported",
  70. value: function isSupported(image, xref, res, pdfFunctionFactory) {
  71. var dict = image.dict;
  72. if (dict.has('DecodeParms') || dict.has('DP')) {
  73. return false;
  74. }
  75. var cs = _colorspace.ColorSpace.parse(dict.get('ColorSpace', 'CS'), xref, res, pdfFunctionFactory);
  76. return (cs.name === 'DeviceGray' || cs.name === 'DeviceRGB') && cs.isDefaultDecode(dict.getArray('Decode', 'D'));
  77. }
  78. }, {
  79. key: "isDecodable",
  80. value: function isDecodable(image, xref, res, pdfFunctionFactory) {
  81. var dict = image.dict;
  82. if (dict.has('DecodeParms') || dict.has('DP')) {
  83. return false;
  84. }
  85. var cs = _colorspace.ColorSpace.parse(dict.get('ColorSpace', 'CS'), xref, res, pdfFunctionFactory);
  86. var bpc = dict.get('BitsPerComponent', 'BPC') || 1;
  87. return (cs.numComps === 1 || cs.numComps === 3) && cs.isDefaultDecode(dict.getArray('Decode', 'D'), bpc);
  88. }
  89. }]);
  90. return NativeImageDecoder;
  91. }();
  92. exports.NativeImageDecoder = NativeImageDecoder;