index.js 2.3 KB

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