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