modules/jpeg_support.js

  1. /**
  2. * @license
  3. *
  4. * Licensed under the MIT License.
  5. * http://opensource.org/licenses/mit-license
  6. */
  7. import { jsPDF } from "../jspdf.js";
  8. /**
  9. * jsPDF jpeg Support PlugIn
  10. *
  11. * @name jpeg_support
  12. * @module
  13. */
  14. (function(jsPDFAPI) {
  15. "use strict";
  16. /**
  17. * 0xc0 (SOF) Huffman - Baseline DCT
  18. * 0xc1 (SOF) Huffman - Extended sequential DCT
  19. * 0xc2 Progressive DCT (SOF2)
  20. * 0xc3 Spatial (sequential) lossless (SOF3)
  21. * 0xc4 Differential sequential DCT (SOF5)
  22. * 0xc5 Differential progressive DCT (SOF6)
  23. * 0xc6 Differential spatial (SOF7)
  24. * 0xc7
  25. */
  26. var markers = [0xc0, 0xc1, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7];
  27. //takes a string imgData containing the raw bytes of
  28. //a jpeg image and returns [width, height]
  29. //Algorithm from: http://www.64lines.com/jpeg-width-height
  30. var getJpegInfo = function(imgData) {
  31. var width, height, numcomponents;
  32. var blockLength = imgData.charCodeAt(4) * 256 + imgData.charCodeAt(5);
  33. var len = imgData.length;
  34. var result = { width: 0, height: 0, numcomponents: 1 };
  35. for (var i = 4; i < len; i += 2) {
  36. i += blockLength;
  37. if (markers.indexOf(imgData.charCodeAt(i + 1)) !== -1) {
  38. height = imgData.charCodeAt(i + 5) * 256 + imgData.charCodeAt(i + 6);
  39. width = imgData.charCodeAt(i + 7) * 256 + imgData.charCodeAt(i + 8);
  40. numcomponents = imgData.charCodeAt(i + 9);
  41. result = { width: width, height: height, numcomponents: numcomponents };
  42. break;
  43. } else {
  44. blockLength =
  45. imgData.charCodeAt(i + 2) * 256 + imgData.charCodeAt(i + 3);
  46. }
  47. }
  48. return result;
  49. };
  50. /**
  51. * @ignore
  52. */
  53. jsPDFAPI.processJPEG = function(
  54. data,
  55. index,
  56. alias,
  57. compression,
  58. dataAsBinaryString,
  59. colorSpace
  60. ) {
  61. var filter = this.decode.DCT_DECODE,
  62. bpc = 8,
  63. dims,
  64. result = null;
  65. if (
  66. typeof data === "string" ||
  67. this.__addimage__.isArrayBuffer(data) ||
  68. this.__addimage__.isArrayBufferView(data)
  69. ) {
  70. // if we already have a stored binary string rep use that
  71. data = dataAsBinaryString || data;
  72. data = this.__addimage__.isArrayBuffer(data)
  73. ? new Uint8Array(data)
  74. : data;
  75. data = this.__addimage__.isArrayBufferView(data)
  76. ? this.__addimage__.arrayBufferToBinaryString(data)
  77. : data;
  78. dims = getJpegInfo(data);
  79. switch (dims.numcomponents) {
  80. case 1:
  81. colorSpace = this.color_spaces.DEVICE_GRAY;
  82. break;
  83. case 4:
  84. colorSpace = this.color_spaces.DEVICE_CMYK;
  85. break;
  86. case 3:
  87. colorSpace = this.color_spaces.DEVICE_RGB;
  88. break;
  89. }
  90. result = {
  91. data: data,
  92. width: dims.width,
  93. height: dims.height,
  94. colorSpace: colorSpace,
  95. bitsPerComponent: bpc,
  96. filter: filter,
  97. index: index,
  98. alias: alias
  99. };
  100. }
  101. return result;
  102. };
  103. })(jsPDF.API);