modules/rgba_support.js

  1. /**
  2. * @license
  3. *
  4. * Copyright (c) 2021 Antti Palola, https://github.com/Pantura
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the
  8. * "Software"), to deal in the Software without restriction, including
  9. * without limitation the rights to use, copy, modify, merge, publish,
  10. * distribute, sublicense, and/or sell copies of the Software, and to
  11. * permit persons to whom the Software is furnished to do so, subject to
  12. * the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  21. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  22. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. * ====================================================================
  25. */
  26. import { jsPDF } from "../jspdf.js";
  27. /**
  28. * jsPDF RGBA array PlugIn
  29. * @name rgba_support
  30. * @module
  31. */
  32. (function(jsPDFAPI) {
  33. "use strict";
  34. /**
  35. * @name processRGBA
  36. * @function
  37. *
  38. * Process RGBA Array. This is a one-dimension array with pixel data [red, green, blue, alpha, red, green, ...].
  39. * RGBA array data can be obtained from DOM canvas getImageData.
  40. * @ignore
  41. */
  42. jsPDFAPI.processRGBA = function(imageData, index, alias) {
  43. "use strict";
  44. var imagePixels = imageData.data;
  45. var length = imagePixels.length;
  46. // jsPDF takes alpha data separately so extract that.
  47. var rgbOut = new Uint8Array((length / 4) * 3);
  48. var alphaOut = new Uint8Array(length / 4);
  49. var outIndex = 0;
  50. var alphaIndex = 0;
  51. for (var i = 0; i < length; i += 4) {
  52. var r = imagePixels[i];
  53. var g = imagePixels[i + 1];
  54. var b = imagePixels[i + 2];
  55. var alpha = imagePixels[i + 3];
  56. rgbOut[outIndex++] = r;
  57. rgbOut[outIndex++] = g;
  58. rgbOut[outIndex++] = b;
  59. alphaOut[alphaIndex++] = alpha;
  60. }
  61. var rgbData = this.__addimage__.arrayBufferToBinaryString(rgbOut);
  62. var alphaData = this.__addimage__.arrayBufferToBinaryString(alphaOut);
  63. return {
  64. alpha: alphaData,
  65. data: rgbData,
  66. index: index,
  67. alias: alias,
  68. colorSpace: "DeviceRGB",
  69. bitsPerComponent: 8,
  70. width: imageData.width,
  71. height: imageData.height
  72. };
  73. };
  74. })(jsPDF.API);