index.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports["default"] = void 0;
  7. var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
  8. var _pngjs = require("pngjs");
  9. var _utils = require("@jimp/utils");
  10. var MIME_TYPE = 'image/png'; // PNG filter types
  11. var PNG_FILTER_AUTO = -1;
  12. var PNG_FILTER_NONE = 0;
  13. var PNG_FILTER_SUB = 1;
  14. var PNG_FILTER_UP = 2;
  15. var PNG_FILTER_AVERAGE = 3;
  16. var PNG_FILTER_PATH = 4;
  17. var _default = function _default() {
  18. return {
  19. mime: (0, _defineProperty2["default"])({}, MIME_TYPE, ['png']),
  20. constants: {
  21. MIME_PNG: MIME_TYPE,
  22. PNG_FILTER_AUTO: PNG_FILTER_AUTO,
  23. PNG_FILTER_NONE: PNG_FILTER_NONE,
  24. PNG_FILTER_SUB: PNG_FILTER_SUB,
  25. PNG_FILTER_UP: PNG_FILTER_UP,
  26. PNG_FILTER_AVERAGE: PNG_FILTER_AVERAGE,
  27. PNG_FILTER_PATH: PNG_FILTER_PATH
  28. },
  29. hasAlpha: (0, _defineProperty2["default"])({}, MIME_TYPE, true),
  30. decoders: (0, _defineProperty2["default"])({}, MIME_TYPE, _pngjs.PNG.sync.read),
  31. encoders: (0, _defineProperty2["default"])({}, MIME_TYPE, function (data) {
  32. var png = new _pngjs.PNG({
  33. width: data.bitmap.width,
  34. height: data.bitmap.height
  35. });
  36. png.data = data.bitmap.data;
  37. return _pngjs.PNG.sync.write(png, {
  38. width: data.bitmap.width,
  39. height: data.bitmap.height,
  40. deflateLevel: data._deflateLevel,
  41. deflateStrategy: data._deflateStrategy,
  42. filterType: data._filterType,
  43. colorType: typeof data._colorType === 'number' ? data._colorType : data._rgba ? 6 : 2,
  44. inputHasAlpha: data._rgba
  45. });
  46. }),
  47. "class": {
  48. _deflateLevel: 9,
  49. _deflateStrategy: 3,
  50. _filterType: PNG_FILTER_AUTO,
  51. _colorType: null,
  52. /**
  53. * Sets the deflate level used when saving as PNG format (default is 9)
  54. * @param {number} l Deflate level to use 0-9. 0 is no compression. 9 (default) is maximum compression.
  55. * @param {function(Error, Jimp)} cb (optional) a callback for when complete
  56. * @returns {Jimp} this for chaining of methods
  57. */
  58. deflateLevel: function deflateLevel(l, cb) {
  59. if (typeof l !== 'number') {
  60. return _utils.throwError.call(this, 'l must be a number', cb);
  61. }
  62. if (l < 0 || l > 9) {
  63. return _utils.throwError.call(this, 'l must be a number 0 - 9', cb);
  64. }
  65. this._deflateLevel = Math.round(l);
  66. if ((0, _utils.isNodePattern)(cb)) {
  67. cb.call(this, null, this);
  68. }
  69. return this;
  70. },
  71. /**
  72. * Sets the deflate strategy used when saving as PNG format (default is 3)
  73. * @param {number} s Deflate strategy to use 0-3.
  74. * @param {function(Error, Jimp)} cb (optional) a callback for when complete
  75. * @returns {Jimp} this for chaining of methods
  76. */
  77. deflateStrategy: function deflateStrategy(s, cb) {
  78. if (typeof s !== 'number') {
  79. return _utils.throwError.call(this, 's must be a number', cb);
  80. }
  81. if (s < 0 || s > 3) {
  82. return _utils.throwError.call(this, 's must be a number 0 - 3', cb);
  83. }
  84. this._deflateStrategy = Math.round(s);
  85. if ((0, _utils.isNodePattern)(cb)) {
  86. cb.call(this, null, this);
  87. }
  88. return this;
  89. },
  90. /**
  91. * Sets the filter type used when saving as PNG format (default is automatic filters)
  92. * @param {number} f The quality to use -1-4.
  93. * @param {function(Error, Jimp)} cb (optional) a callback for when complete
  94. * @returns {Jimp} this for chaining of methods
  95. */
  96. filterType: function filterType(f, cb) {
  97. if (typeof f !== 'number') {
  98. return _utils.throwError.call(this, 'n must be a number', cb);
  99. }
  100. if (f < -1 || f > 4) {
  101. return _utils.throwError.call(this, 'n must be -1 (auto) or a number 0 - 4', cb);
  102. }
  103. this._filterType = Math.round(f);
  104. if ((0, _utils.isNodePattern)(cb)) {
  105. cb.call(this, null, this);
  106. }
  107. return this;
  108. },
  109. /**
  110. * Sets the color type used when saving as PNG format
  111. * @param {number} s color type to use 0, 2, 4, 6.
  112. * @param {function(Error, Jimp)} cb (optional) a callback for when complete
  113. * @returns {Jimp} this for chaining of methods
  114. */
  115. colorType: function colorType(s, cb) {
  116. if (typeof s !== 'number') {
  117. return _utils.throwError.call(this, 's must be a number', cb);
  118. }
  119. if (s !== 0 && s !== 2 && s !== 4 && s !== 6) {
  120. return _utils.throwError.call(this, 's must be a number 0, 2, 4, 6.', cb);
  121. }
  122. this._colorType = Math.round(s);
  123. if ((0, _utils.isNodePattern)(cb)) {
  124. cb.call(this, null, this);
  125. }
  126. return this;
  127. }
  128. }
  129. };
  130. };
  131. exports["default"] = _default;
  132. //# sourceMappingURL=index.js.map