image_utils.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2020 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.js");
  28. var _jpeg_stream = require("./jpeg_stream.js");
  29. var _stream = require("./stream.js");
  30. class NativeImageDecoder {
  31. constructor({
  32. xref,
  33. resources,
  34. handler,
  35. forceDataSchema = false,
  36. pdfFunctionFactory
  37. }) {
  38. this.xref = xref;
  39. this.resources = resources;
  40. this.handler = handler;
  41. this.forceDataSchema = forceDataSchema;
  42. this.pdfFunctionFactory = pdfFunctionFactory;
  43. }
  44. canDecode(image) {
  45. return image instanceof _jpeg_stream.JpegStream && NativeImageDecoder.isDecodable(image, this.xref, this.resources, this.pdfFunctionFactory) && image.maybeValidDimensions;
  46. }
  47. decode(image) {
  48. const dict = image.dict;
  49. let colorSpace = dict.get("ColorSpace", "CS");
  50. colorSpace = _colorspace.ColorSpace.parse(colorSpace, this.xref, this.resources, this.pdfFunctionFactory);
  51. return this.handler.sendWithPromise("JpegDecode", [image.getIR(this.forceDataSchema), colorSpace.numComps]).then(function ({
  52. data,
  53. width,
  54. height
  55. }) {
  56. return new _stream.Stream(data, 0, data.length, dict);
  57. });
  58. }
  59. static isSupported(image, xref, res, pdfFunctionFactory) {
  60. const dict = image.dict;
  61. if (dict.has("DecodeParms") || dict.has("DP")) {
  62. return false;
  63. }
  64. const cs = _colorspace.ColorSpace.parse(dict.get("ColorSpace", "CS"), xref, res, pdfFunctionFactory);
  65. return (cs.name === "DeviceGray" || cs.name === "DeviceRGB") && cs.isDefaultDecode(dict.getArray("Decode", "D"));
  66. }
  67. static isDecodable(image, xref, res, pdfFunctionFactory) {
  68. const dict = image.dict;
  69. if (dict.has("DecodeParms") || dict.has("DP")) {
  70. return false;
  71. }
  72. const cs = _colorspace.ColorSpace.parse(dict.get("ColorSpace", "CS"), xref, res, pdfFunctionFactory);
  73. const bpc = dict.get("BitsPerComponent", "BPC") || 1;
  74. return (cs.numComps === 1 || cs.numComps === 3) && cs.isDefaultDecode(dict.getArray("Decode", "D"), bpc);
  75. }
  76. }
  77. exports.NativeImageDecoder = NativeImageDecoder;