How to use the @jimp/utils.throwError.call function in @jimp/utils

To help you get started, we’ve selected a few @jimp/utils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github oliver-moran / jimp / packages / core / src / index.js View on Github external
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) {
github oliver-moran / jimp / packages / core / src / index.js View on Github external
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);
    }
github oliver-moran / jimp / packages / plugin-gaussian / src / index.js View on Github external
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++) {
github oliver-moran / jimp / packages / plugin-crop / src / index.js View on Github external
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;
github oliver-moran / jimp / packages / plugin-resize / src / index.js View on Github external
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);
github oliver-moran / jimp / packages / core / src / index.js View on Github external
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;
  }
github oliver-moran / jimp / packages / plugin-crop / src / index.js View on Github external
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 {
github oliver-moran / jimp / packages / plugin-blit / src / index.js View on Github external
blit(src, x, y, srcx, srcy, srcw, srch, cb) {
    if (!(src instanceof this.constructor)) {
      return throwError.call(this, 'The source must be a Jimp image', cb);
    }

    if (typeof x !== 'number' || typeof y !== 'number') {
      return throwError.call(this, 'x and y must be numbers', cb);
    }

    if (typeof srcx === 'function') {
      cb = srcx;
      srcx = 0;
      srcy = 0;
      srcw = src.bitmap.width;
      srch = src.bitmap.height;
    } else if (
      typeof srcx === typeof srcy &&
      typeof srcy === typeof srcw &&
      typeof srcw === typeof srch
github oliver-moran / jimp / packages / plugin-print / src / index.js View on Github external
bMFont(file, (err, font) => {
          const chars = {};
          const kernings = {};

          if (err) {
            return throwError.call(this, err, cb);
          }

          for (let i = 0; i < font.chars.length; i++) {
            chars[String.fromCharCode(font.chars[i].id)] = font.chars[i];
          }

          for (let i = 0; i < font.kernings.length; i++) {
            const firstString = String.fromCharCode(font.kernings[i].first);
            kernings[firstString] = kernings[firstString] || {};
            kernings[firstString][
              String.fromCharCode(font.kernings[i].second)
            ] = font.kernings[i].amount;
          }

          loadPages(this, Path.dirname(file), font.pages).then(pages => {
            cb(null, {
github oliver-moran / jimp / packages / core / src / index.js View on Github external
jimpEvChange('background', function(hex, cb) {
  if (typeof hex !== 'number') {
    return throwError.call(this, 'hex must be a hexadecimal rgba value', cb);
  }

  this._background = hex;

  if (isNodePattern(cb)) {
    cb.call(this, null, this);
  }

  return this;
});

@jimp/utils

MIT
Latest version published 8 days ago

Package Health Score

92 / 100
Full package analysis