Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
getPixelIndex(x, y, edgeHandling, cb) {
let xi;
let yi;
if (typeof edgeHandling === 'function' && typeof cb === 'undefined') {
cb = edgeHandling;
edgeHandling = null;
}
if (!edgeHandling) {
edgeHandling = Jimp.EDGE_EXTEND;
}
if (typeof x !== 'number' || typeof y !== 'number') {
return throwError.call(this, 'x and y must be numbers', cb);
}
// round input
x = Math.round(x);
y = Math.round(y);
xi = x;
yi = y;
if (edgeHandling === Jimp.EDGE_EXTEND) {
if (x < 0) xi = 0;
if (x >= this.bitmap.width) xi = this.bitmap.width - 1;
if (y < 0) yi = 0;
if (y >= this.bitmap.height) yi = this.bitmap.height - 1;
}
if (edgeHandling === Jimp.EDGE_WRAP) {
hash(base, cb) {
base = base || 64;
if (typeof base === 'function') {
cb = base;
base = 64;
}
if (typeof base !== 'number') {
return throwError.call(this, 'base must be a number', cb);
}
if (base < 2 || base > 64) {
return throwError.call(
this,
'base must be a number between 2 and 64',
cb
);
}
let hash = this.pHash();
hash = anyBase(anyBase.BIN, alphabet.slice(0, base))(hash);
while (hash.length < maxHashLength[base]) {
hash = '0' + hash; // pad out with leading zeros
}
if (isNodePattern(cb)) {
cb.call(this, null, hash);
}
gaussian(r, cb) {
// http://blog.ivank.net/fastest-gaussian-blur.html
if (typeof r !== 'number') {
return throwError.call(this, 'r must be a number', cb);
}
if (r < 1) {
return throwError.call(this, 'r must be greater than 0', cb);
}
const rs = Math.ceil(r * 2.57); // significant radius
const range = rs * 2 + 1;
const rr2 = r * r * 2;
const rr2pi = rr2 * Math.PI;
const weights = [];
for (let y = 0; y < range; y++) {
weights[y] = [];
for (let x = 0; x < range; x++) {
event('crop', function(x, y, w, h, cb) {
if (typeof x !== 'number' || typeof y !== 'number')
return throwError.call(this, 'x and y must be numbers', cb);
if (typeof w !== 'number' || typeof h !== 'number')
return throwError.call(this, 'w and h must be numbers', cb);
// round input
x = Math.round(x);
y = Math.round(y);
w = Math.round(w);
h = Math.round(h);
if (x === 0 && w === this.bitmap.width) {
// shortcut
const start = (w * y + x) << 2;
const end = (start + h * w) << 2;
this.bitmap.data = this.bitmap.data.slice(start, end);
} else {
const bitmap = Buffer.allocUnsafe(w * h * 4);
let offset = 0;
resize(w, h, mode, cb) {
if (typeof w !== 'number' || typeof h !== 'number') {
return throwError.call(this, 'w and h must be numbers', cb);
}
if (typeof mode === 'function' && typeof cb === 'undefined') {
cb = mode;
mode = null;
}
if (w === this.constructor.AUTO && h === this.constructor.AUTO) {
return throwError.call(this, 'w and h cannot both be set to auto', cb);
}
if (w === this.constructor.AUTO) {
w = this.bitmap.width * (h / this.bitmap.height);
}
if (h === this.constructor.AUTO) {
h = this.bitmap.height * (w / this.bitmap.width);
}
if (w < 0 || h < 0) {
return throwError.call(this, 'w and h must be positive numbers', cb);
}
// round inputs
w = Math.round(w);
getPixelColor(x, y, cb) {
if (typeof x !== 'number' || typeof y !== 'number')
return throwError.call(this, 'x and y must be numbers', cb);
// round input
x = Math.round(x);
y = Math.round(y);
const idx = this.getPixelIndex(x, y);
const hex = this.bitmap.data.readUInt32BE(idx);
if (isNodePattern(cb)) {
cb.call(this, null, hex);
}
return hex;
}
event('crop', function(x, y, w, h, cb) {
if (typeof x !== 'number' || typeof y !== 'number')
return throwError.call(this, 'x and y must be numbers', cb);
if (typeof w !== 'number' || typeof h !== 'number')
return throwError.call(this, 'w and h must be numbers', cb);
// round input
x = Math.round(x);
y = Math.round(y);
w = Math.round(w);
h = Math.round(h);
if (x === 0 && w === this.bitmap.width) {
// shortcut
const start = (w * y + x) << 2;
const end = (start + h * w) << 2;
this.bitmap.data = this.bitmap.data.slice(start, end);
} else {
yi = y % this.bitmap.height;
}
}
let i = (this.bitmap.width * yi + xi) << 2;
// if out of bounds index is -1
if (xi < 0 || xi >= this.bitmap.width) {
i = -1;
}
if (yi < 0 || yi >= this.bitmap.height) {
i = -1;
}
if (isNodePattern(cb)) {
cb.call(this, null, i);
}
return i;
}
const alignH = hbits >> 1; // 0, 1, 2
const alignV = vbits >> 1; // 0, 1, 2
const f =
w / h > this.bitmap.width / this.bitmap.height
? w / this.bitmap.width
: h / this.bitmap.height;
this.scale(f, mode);
this.crop(
((this.bitmap.width - w) / 2) * alignH,
((this.bitmap.height - h) / 2) * alignV,
w,
h
);
if (isNodePattern(cb)) {
cb.call(this, null, this);
}
return this;
}
});
const bitmap = Buffer.allocUnsafe(w * h * 4);
let offset = 0;
this.scanQuiet(x, y, w, h, function(x, y, idx) {
const data = this.bitmap.data.readUInt32BE(idx, true);
bitmap.writeUInt32BE(data, offset, true);
offset += 4;
});
this.bitmap.data = bitmap;
}
this.bitmap.width = w;
this.bitmap.height = h;
if (isNodePattern(cb)) {
// @ts-ignore because cb can be undefined and isNodePattern doesn't narrow
cb.call(this, null, this);
}
return this;
});
}