index.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. require("core-js/modules/es.array.fill");
  3. require("core-js/modules/es.array.find-index");
  4. require("core-js/modules/es.array.reverse");
  5. require("core-js/modules/es.array.slice");
  6. Object.defineProperty(exports, "__esModule", {
  7. value: true
  8. });
  9. exports["default"] = void 0;
  10. var _utils = require("@jimp/utils");
  11. /**
  12. * Get an image's histogram
  13. * @return {object} An object with an array of color occurrence counts for each channel (r,g,b)
  14. */
  15. function histogram() {
  16. var histogram = {
  17. r: new Array(256).fill(0),
  18. g: new Array(256).fill(0),
  19. b: new Array(256).fill(0)
  20. };
  21. this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, index) {
  22. histogram.r[this.bitmap.data[index + 0]]++;
  23. histogram.g[this.bitmap.data[index + 1]]++;
  24. histogram.b[this.bitmap.data[index + 2]]++;
  25. });
  26. return histogram;
  27. }
  28. /**
  29. * Normalize values
  30. * @param {integer} value Pixel channel value.
  31. * @param {integer} min Minimum value for channel
  32. * @param {integer} max Maximum value for channel
  33. * @return {integer} normalized values
  34. */
  35. var _normalize = function normalize(value, min, max) {
  36. return (value - min) * 255 / (max - min);
  37. };
  38. var getBounds = function getBounds(histogramChannel) {
  39. return [histogramChannel.findIndex(function (value) {
  40. return value > 0;
  41. }), 255 - histogramChannel.slice().reverse().findIndex(function (value) {
  42. return value > 0;
  43. })];
  44. };
  45. /**
  46. * Normalizes the image
  47. * @param {function(Error, Jimp)} cb (optional) a callback for when complete
  48. * @returns {Jimp} this for chaining of methods
  49. */
  50. var _default = function _default() {
  51. return {
  52. normalize: function normalize(cb) {
  53. var h = histogram.call(this); // store bounds (minimum and maximum values)
  54. var bounds = {
  55. r: getBounds(h.r),
  56. g: getBounds(h.g),
  57. b: getBounds(h.b)
  58. }; // apply value transformations
  59. this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, idx) {
  60. var r = this.bitmap.data[idx + 0];
  61. var g = this.bitmap.data[idx + 1];
  62. var b = this.bitmap.data[idx + 2];
  63. this.bitmap.data[idx + 0] = _normalize(r, bounds.r[0], bounds.r[1]);
  64. this.bitmap.data[idx + 1] = _normalize(g, bounds.g[0], bounds.g[1]);
  65. this.bitmap.data[idx + 2] = _normalize(b, bounds.b[0], bounds.b[1]);
  66. });
  67. if ((0, _utils.isNodePattern)(cb)) {
  68. cb.call(this, null, this);
  69. }
  70. return this;
  71. }
  72. };
  73. };
  74. exports["default"] = _default;
  75. module.exports = exports.default;
  76. //# sourceMappingURL=index.js.map