Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

image-filter-core

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

image-filter-core - npm Package Compare versions

Comparing version 0.0.4 to 1.0.0

23

karma.conf.js

@@ -0,1 +1,4 @@

var istanbul = require('istanbul');
var bistanbul = require('browserify-istanbul');
module.exports = function (config) {

@@ -28,10 +31,10 @@ config.set({

coverageReporter: {
// specify a common output directory
dir: 'coverage',
reporters: [
// reporters not supporting the `file` property
{ type: 'html', subdir: 'report-html' },
{ type: 'lcov', subdir: 'lcov' },
{ type: 'html', subdir: 'html' },
{ type: 'text-summary' },
{ type: 'lcov'}
]
{ type: 'cobertura', subdir: '.' }
],
sourceStore : istanbul.Store.create('fslookup')
},

@@ -41,4 +44,10 @@

debug: true,
plugin: ['proxyquire-universal'],
transform: [
'babelify'
bistanbul({
instrumenterConfig: {
noCompact: true
},
ignore: ['**/node_modules/**', '**/tests/**', '**/sandbox/**']
})
]

@@ -58,4 +67,4 @@ },

// see what is going on
logLevel: 'LOG_INFO'
logLevel: 'LOG_ERROR'
});
};
{
"name": "image-filter-core",
"version": "0.0.4",
"version": "1.0.0",
"description": "Core module for image-filter",
"main": "src/index.js",
"scripts": {
"build": "browserify -t babelify sandbox/sandbox.js > sandbox/bundle.js",
"test": "./node_modules/.bin/karma start",
"codecov": "cat coverage/*/lcov.info | ./node_modules/codecov.io/bin/codecov.io.js",
"codecov": "cat coverage/*/lcov.info | codecov",
"serve": "http-server sandbox"

@@ -32,4 +31,2 @@ },

"devDependencies": {
"babel-polyfill": "^6.9.0",
"babelify": "^7.2.0",
"browserify": "^13.0.0",

@@ -48,2 +45,5 @@ "browserify-istanbul": "^1.0.0",

"phantomjs-prebuilt": "^2.1.5",
"proxyquire": "^1.7.10",
"proxyquire-universal": "^1.0.8",
"proxyquireify": "^3.2.1",
"sinon": "^1.17.3",

@@ -53,10 +53,5 @@ "watchify": "^3.7.0"

"dependencies": {
"webworkify": "^1.2.1",
"babel-preset-es2015": "^6.5.0"
},
"browserify": {
"transform": [
"babelify"
]
"es6-promise": "^4.0.5",
"webworkify": "^1.2.1"
}
}

@@ -1,10 +0,12 @@

import work from 'webworkify';
require('es6-promise/auto');
var work = require('webworkify');
/**
* It returns a canvas with the given width and height
* @name getCanvas
* @param {number} w - width
* @param {number} h - height
* @returns {object}
* @param {Number} w - width
* @param {Number} h - height
* @returns {Object}
*/
export function getCanvas(w, h) {
exports.getCanvas = function (w, h) {
var canvas = document.createElement('canvas');

@@ -15,28 +17,56 @@ canvas.width = w;

return canvas;
}
};
/**
* Given a ImageData it returns the dataURL
* @name convertImageDataToCanvasURL
* @param {ImageData} imageData
* @returns {String}
*/
exports.convertImageDataToCanvasURL = function (imageData) {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width = imageData.width;
canvas.height = imageData.height;
ctx.putImageData(imageData, 0, 0);
return canvas.toDataURL();
};
/**
* Given a worker file with the transformation the work is split
* between the configured number of workers and the transformation is applied
* returning a Promise
* @name apply
* @param {object} worker
* @param {number} nWorkers
* @param {object} canvas
* @param {object} context
* @param {number} params
* @param {number} blockSize
* @param {number} segmentLength
* @returns {promise}
* @param {Object} worker
* @param {Number} nWorkers
* @param {Object} canvas
* @param {Object} context
* @param {Number} params
* @returns {Promise}
*/
export function apply(worker, nWorkers, canvas, context, params, blockSize, segmentLength) {
let w;
let finished = 0;
exports.apply = function (worker, nWorkers, canvas, context, params) {
var w;
var finished = 0;
var len = canvas.width * canvas.height * 4;
var segmentLength;
var blockSize;
return new Promise((resolve) => {
for (let index = 0; index < nWorkers; index++) {
// Minimum number of workers = 1
if (!nWorkers) {
nWorkers = 1;
}
segmentLength = len / nWorkers; // This is the length of array sent to the worker
blockSize = canvas.height / nWorkers; // Height of the picture chunck for every worker
return new Promise(function (resolve) {
for (var index = 0; index < nWorkers; index++) {
w = work(worker);
w.addEventListener('message', (e) => {
w.addEventListener('message', function (e) {
// Data is retrieved using a memory clone operation
const resultCanvasData = e.data.result;
const index = e.data.index;
var resultCanvasData = e.data.result;
var index = e.data.index;

@@ -56,3 +86,3 @@ // Copying back canvas data to canvas

// Getting the picture
const canvasData = context.getImageData(0, blockSize * index, canvas.width, blockSize);
var canvasData = context.getImageData(0, blockSize * index, canvas.width, blockSize);

@@ -62,8 +92,8 @@ // Sending canvas data to the worker using a copy memory operation

data: canvasData,
index,
index: index,
length: segmentLength,
params
params: params
});
}
});
}
};

@@ -1,8 +0,18 @@

import { expect } from 'chai';
import * as utils from '../src/index';
const expect = require('chai').expect;
const sinon = require('sinon');
const proxyquire = require('proxyquireify')(require);
describe('utils', function () {
var sandbox;
before(function () {
sandbox = sinon.sandbox.create();
});
context('#getCanvas', () => {
afterEach(function () {
sandbox.restore();
});
describe('#getCanvas()', function () {
it('should return a canvas of 100 x 100', function () {
const utils = require('../src/index');
const element = utils.getCanvas(100, 100);

@@ -15,2 +25,141 @@

});
describe('#convertImageDataToCanvasURL()', function () {
it('should create canvas and call toDataURL', function () {
const utils = require('../src/index');
const expectedResult = 'TEST';
const ctx = {
putImageData: sandbox.stub()
};
const canvas = {
getContext: sandbox.stub().returns(ctx),
toDataURL: sandbox.stub().returns(expectedResult)
};
sandbox.stub(document, 'createElement').returns(canvas);
const imageData = {
width: 100,
height: 150
};
const result = utils.convertImageDataToCanvasURL(imageData);
expect(document.createElement.calledWith('canvas')).to.equal(true);
expect(canvas.getContext.calledWith('2d')).to.equal(true);
expect(canvas.width).to.equal(imageData.width);
expect(canvas.height).to.equal(imageData.height);
expect(ctx.putImageData.calledWith(imageData, 0, 0)).to.equal(true);
expect(result).to.equal(expectedResult);
});
});
describe('#apply()', function () {
context('when nWorkers is bigger then zero', function () {
it('should call postMessage and putImageData nWorker times', function (done) {
var eventListenerCallback;
const expectedResult = 'TEST';
const ctx = {
putImageData: sandbox.stub(),
getImageData: sandbox.stub()
};
const canvas = {
getContext: sandbox.stub().returns(ctx),
toDataURL: sandbox.stub().returns(expectedResult)
};
const worker = {
addEventListener: function addEventListener(evt, fn) {
eventListenerCallback = fn;
},
postMessage: sandbox.stub()
};
const utils = proxyquire('../src/index', {
'webworkify': function () { return worker; }
});
const nWorkers = 4;
const result = utils.apply(
'DUMMY',
nWorkers,
canvas,
ctx,
{}
);
const evt = { data: { result: 'DUMMY', index: 1 } };
eventListenerCallback(evt);
eventListenerCallback(evt);
eventListenerCallback(evt);
eventListenerCallback(evt);
result.then(function () {
// One for each worker and one for the final result
expect(ctx.getImageData.callCount).to.equal(nWorkers + 1);
// One for each worker
expect(ctx.putImageData.callCount).to.equal(nWorkers);
expect(worker.postMessage.callCount).to.equal(nWorkers);
done();
});
});
});
context('when nWorkers is zero', function () {
it('should call postMessage and putImageData one time', function (done) {
var eventListenerCallback;
const expectedResult = 'TEST';
const ctx = {
putImageData: sandbox.stub(),
getImageData: sandbox.stub()
};
const canvas = {
getContext: sandbox.stub().returns(ctx),
toDataURL: sandbox.stub().returns(expectedResult)
};
const worker = {
addEventListener: function addEventListener(evt, fn) {
eventListenerCallback = fn;
},
postMessage: sandbox.stub()
};
const utils = proxyquire('../src/index', {
'webworkify': function () { return worker; }
});
const nWorkers = 0;
const result = utils.apply(
'DUMMY',
nWorkers,
canvas,
ctx,
{}
);
const evt = { data: { result: 'DUMMY', index: 1 } };
eventListenerCallback(evt);
result.then(function () {
// One for each worker and one for the final result
expect(ctx.getImageData.callCount).to.equal(2);
// One for each worker
expect(ctx.putImageData.callCount).to.equal(1);
expect(worker.postMessage.callCount).to.equal(1);
done();
});
});
});
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc