function createMinion(operation) {
  let workerHasImageData = true;
  try {
    new ImageData(10, 10);
  } catch (_) {
    workerHasImageData = false;
  }

  function newWorkerImageData(data, width, height) {
    if (workerHasImageData) {
      return new ImageData(data, width, height);
    } else {
      return {data: data, width: width, height: height};
    }
  }

  return function (data) {
    // bracket notation for minification support
    const buffers = data['buffers'];
    const meta = data['meta'];
    const imageOps = data['imageOps'];
    const width = data['width'];
    const height = data['height'];

    const numBuffers = buffers.length;
    const numBytes = buffers[0].byteLength;

    if (imageOps) {
      const images = new Array(numBuffers);
      for (let b = 0; b < numBuffers; ++b) {
        images[b] = newWorkerImageData(
          new Uint8ClampedArray(buffers[b]),
          width,
          height
        );
      }
      const output = operation(images, meta).data;
      return output.buffer;
    }

    const output = new Uint8ClampedArray(numBytes);
    const arrays = new Array(numBuffers);
    const pixels = new Array(numBuffers);
    for (let b = 0; b < numBuffers; ++b) {
      arrays[b] = new Uint8ClampedArray(buffers[b]);
      pixels[b] = [0, 0, 0, 0];
    }
    for (let i = 0; i < numBytes; i += 4) {
      for (let j = 0; j < numBuffers; ++j) {
        const array = arrays[j];
        pixels[j][0] = array[i];
        pixels[j][1] = array[i + 1];
        pixels[j][2] = array[i + 2];
        pixels[j][3] = array[i + 3];
      }
      const pixel = operation(pixels, meta);
      output[i] = pixel[0];
      output[i + 1] = pixel[1];
      output[i + 2] = pixel[2];
      output[i + 3] = pixel[3];
    }
    return output.buffer;
  };
}
/**
 * Create a function for running operations.  This function is serialized for
 * use in a worker.
 * @param {function(Array, Object):*} operation The operation.
 * @return {function(MinionData):ArrayBuffer} A function that takes an object with
 * buffers, meta, imageOps, width, and height properties and returns an array
 * buffer.
 */
Create a function for running operations.  This function is serialized for
use in a worker.
@param {function(Array, Object):*} operation The operation.
@return {function(MinionData):ArrayBuffer} A function that takes an object with
buffers, meta, imageOps, width, and height properties and returns an array
buffer.
Create a function for running operations . This function is serialized for use in a worker . @param { function ( Array Object ) : * } operation The operation . @return { function ( MinionData ) : ArrayBuffer } A function that takes an object with buffers meta imageOps width and height properties and returns an array buffer .
function createMinion(operation) {
  let workerHasImageData = true;
  try {
    new ImageData(10, 10);
  } catch (_) {
    workerHasImageData = false;
  }
  function newWorkerImageData(data, width, height) {
    if (workerHasImageData) {
      return new ImageData(data, width, height);
    } else {
      return {data: data, width: width, height: height};
    }
  }
  return function (data) {
    const buffers = data['buffers'];
    const meta = data['meta'];
    const imageOps = data['imageOps'];
    const width = data['width'];
    const height = data['height'];
    const numBuffers = buffers.length;
    const numBytes = buffers[0].byteLength;
    if (imageOps) {
      const images = new Array(numBuffers);
      for (let b = 0; b < numBuffers; ++b) {
        images[b] = newWorkerImageData(
          new Uint8ClampedArray(buffers[b]),
          width,
          height
        );
      }
      const output = operation(images, meta).data;
      return output.buffer;
    }
    const output = new Uint8ClampedArray(numBytes);
    const arrays = new Array(numBuffers);
    const pixels = new Array(numBuffers);
    for (let b = 0; b < numBuffers; ++b) {
      arrays[b] = new Uint8ClampedArray(buffers[b]);
      pixels[b] = [0, 0, 0, 0];
    }
    for (let i = 0; i < numBytes; i += 4) {
      for (let j = 0; j < numBuffers; ++j) {
        const array = arrays[j];
        pixels[j][0] = array[i];
        pixels[j][1] = array[i + 1];
        pixels[j][2] = array[i + 2];
        pixels[j][3] = array[i + 3];
      }
      const pixel = operation(pixels, meta);
      output[i] = pixel[0];
      output[i + 1] = pixel[1];
      output[i + 2] = pixel[2];
      output[i + 3] = pixel[3];
    }
    return output.buffer;
  };
}
function createMinion ( operation ) { let workerHasImageData = true ; try { new ImageData ( 10 , 10 ) ; } catch ( _ ) { workerHasImageData = false ; } function newWorkerImageData ( data , width , height ) { if ( workerHasImageData ) { return new ImageData ( data , width , height ) ; } else { return { data : data , width : width , height : height } ; } } return function ( data ) { const buffers = data [ 'buffers' ] ; const meta = data [ 'meta' ] ; const imageOps = data [ 'imageOps' ] ; const width = data [ 'width' ] ; const height = data [ 'height' ] ; const numBuffers = buffers . length ; const numBytes = buffers [ 0 ] . byteLength ; if ( imageOps ) { const images = new Array ( numBuffers ) ; for ( let b = 0 ; b < numBuffers ; ++ b ) { images [ b ] = newWorkerImageData ( new Uint8ClampedArray ( buffers [ b ] ) , width , height ) ; } const output = operation ( images , meta ) . data ; return output . buffer ; } const output = new Uint8ClampedArray ( numBytes ) ; const arrays = new Array ( numBuffers ) ; const pixels = new Array ( numBuffers ) ; for ( let b = 0 ; b < numBuffers ; ++ b ) { arrays [ b ] = new Uint8ClampedArray ( buffers [ b ] ) ; pixels [ b ] = [ 0 , 0 , 0 , 0 ] ; } for ( let i = 0 ; i < numBytes ; i += 4 ) { for ( let j = 0 ; j < numBuffers ; ++ j ) { const array = arrays [ j ] ; pixels [ j ] [ 0 ] = array [ i ] ; pixels [ j ] [ 1 ] = array [ i + 1 ] ; pixels [ j ] [ 2 ] = array [ i + 2 ] ; pixels [ j ] [ 3 ] = array [ i + 3 ] ; } const pixel = operation ( pixels , meta ) ; output [ i ] = pixel [ 0 ] ; output [ i + 1 ] = pixel [ 1 ] ; output [ i + 2 ] = pixel [ 2 ] ; output [ i + 3 ] = pixel [ 3 ] ; } return output . buffer ; } ; }
Create a function for running operations.
Create a function for running operations .
// bracket notation for minification support