Path: blob/master/node_modules/@jimp/plugin-normalize/es/index.js
1126 views
"use strict";12Object.defineProperty(exports, "__esModule", {3value: true4});5exports["default"] = void 0;67var _utils = require("@jimp/utils");89/**10* Get an image's histogram11* @return {object} An object with an array of color occurrence counts for each channel (r,g,b)12*/13function histogram() {14var histogram = {15r: new Array(256).fill(0),16g: new Array(256).fill(0),17b: new Array(256).fill(0)18};19this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, index) {20histogram.r[this.bitmap.data[index + 0]]++;21histogram.g[this.bitmap.data[index + 1]]++;22histogram.b[this.bitmap.data[index + 2]]++;23});24return histogram;25}26/**27* Normalize values28* @param {integer} value Pixel channel value.29* @param {integer} min Minimum value for channel30* @param {integer} max Maximum value for channel31* @return {integer} normalized values32*/333435var _normalize = function normalize(value, min, max) {36return (value - min) * 255 / (max - min);37};3839var getBounds = function getBounds(histogramChannel) {40return [histogramChannel.findIndex(function (value) {41return value > 0;42}), 255 - histogramChannel.slice().reverse().findIndex(function (value) {43return value > 0;44})];45};46/**47* Normalizes the image48* @param {function(Error, Jimp)} cb (optional) a callback for when complete49* @returns {Jimp} this for chaining of methods50*/515253var _default = function _default() {54return {55normalize: function normalize(cb) {56var h = histogram.call(this); // store bounds (minimum and maximum values)5758var bounds = {59r: getBounds(h.r),60g: getBounds(h.g),61b: getBounds(h.b)62}; // apply value transformations6364this.scanQuiet(0, 0, this.bitmap.width, this.bitmap.height, function (x, y, idx) {65var r = this.bitmap.data[idx + 0];66var g = this.bitmap.data[idx + 1];67var b = this.bitmap.data[idx + 2];68this.bitmap.data[idx + 0] = _normalize(r, bounds.r[0], bounds.r[1]);69this.bitmap.data[idx + 1] = _normalize(g, bounds.g[0], bounds.g[1]);70this.bitmap.data[idx + 2] = _normalize(b, bounds.b[0], bounds.b[1]);71});7273if ((0, _utils.isNodePattern)(cb)) {74cb.call(this, null, this);75}7677return this;78}79};80};8182exports["default"] = _default;83//# sourceMappingURL=index.js.map8485