image-filter-core
Advanced tools
Comparing version 0.0.4 to 1.0.0
@@ -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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
14040
271
1
45
18
1
+ Addedes6-promise@^4.0.5
+ Addedes6-promise@4.2.8(transitive)
- Removedbabel-preset-es2015@^6.5.0
- Removedansi-regex@2.1.1(transitive)
- Removedansi-styles@2.2.1(transitive)
- Removedbabel-code-frame@6.26.0(transitive)
- Removedbabel-helper-call-delegate@6.24.1(transitive)
- Removedbabel-helper-define-map@6.26.0(transitive)
- Removedbabel-helper-function-name@6.24.1(transitive)
- Removedbabel-helper-get-function-arity@6.24.1(transitive)
- Removedbabel-helper-hoist-variables@6.24.1(transitive)
- Removedbabel-helper-optimise-call-expression@6.24.1(transitive)
- Removedbabel-helper-regex@6.26.0(transitive)
- Removedbabel-helper-replace-supers@6.24.1(transitive)
- Removedbabel-messages@6.23.0(transitive)
- Removedbabel-plugin-check-es2015-constants@6.22.0(transitive)
- Removedbabel-plugin-transform-es2015-arrow-functions@6.22.0(transitive)
- Removedbabel-plugin-transform-es2015-block-scoped-functions@6.22.0(transitive)
- Removedbabel-plugin-transform-es2015-block-scoping@6.26.0(transitive)
- Removedbabel-plugin-transform-es2015-classes@6.24.1(transitive)
- Removedbabel-plugin-transform-es2015-computed-properties@6.24.1(transitive)
- Removedbabel-plugin-transform-es2015-destructuring@6.23.0(transitive)
- Removedbabel-plugin-transform-es2015-duplicate-keys@6.24.1(transitive)
- Removedbabel-plugin-transform-es2015-for-of@6.23.0(transitive)
- Removedbabel-plugin-transform-es2015-function-name@6.24.1(transitive)
- Removedbabel-plugin-transform-es2015-literals@6.22.0(transitive)
- Removedbabel-plugin-transform-es2015-modules-amd@6.24.1(transitive)
- Removedbabel-plugin-transform-es2015-modules-commonjs@6.26.2(transitive)
- Removedbabel-plugin-transform-es2015-modules-systemjs@6.24.1(transitive)
- Removedbabel-plugin-transform-es2015-modules-umd@6.24.1(transitive)
- Removedbabel-plugin-transform-es2015-object-super@6.24.1(transitive)
- Removedbabel-plugin-transform-es2015-parameters@6.24.1(transitive)
- Removedbabel-plugin-transform-es2015-shorthand-properties@6.24.1(transitive)
- Removedbabel-plugin-transform-es2015-spread@6.22.0(transitive)
- Removedbabel-plugin-transform-es2015-sticky-regex@6.24.1(transitive)
- Removedbabel-plugin-transform-es2015-template-literals@6.22.0(transitive)
- Removedbabel-plugin-transform-es2015-typeof-symbol@6.23.0(transitive)
- Removedbabel-plugin-transform-es2015-unicode-regex@6.24.1(transitive)
- Removedbabel-plugin-transform-regenerator@6.26.0(transitive)
- Removedbabel-plugin-transform-strict-mode@6.24.1(transitive)
- Removedbabel-preset-es2015@6.24.1(transitive)
- Removedbabel-runtime@6.26.0(transitive)
- Removedbabel-template@6.26.0(transitive)
- Removedbabel-traverse@6.26.0(transitive)
- Removedbabel-types@6.26.0(transitive)
- Removedbabylon@6.18.0(transitive)
- Removedchalk@1.1.3(transitive)
- Removedcore-js@2.6.12(transitive)
- Removeddebug@2.6.9(transitive)
- Removedescape-string-regexp@1.0.5(transitive)
- Removedesutils@2.0.3(transitive)
- Removedglobals@9.18.0(transitive)
- Removedhas-ansi@2.0.0(transitive)
- Removedinvariant@2.2.4(transitive)
- Removedjs-tokens@3.0.2(transitive)
- Removedjsesc@0.5.0(transitive)
- Removedlodash@4.17.21(transitive)
- Removedloose-envify@1.4.0(transitive)
- Removedms@2.0.0(transitive)
- Removedprivate@0.1.8(transitive)
- Removedregenerate@1.4.2(transitive)
- Removedregenerator-runtime@0.11.1(transitive)
- Removedregenerator-transform@0.10.1(transitive)
- Removedregexpu-core@2.0.0(transitive)
- Removedregjsgen@0.2.0(transitive)
- Removedregjsparser@0.1.5(transitive)
- Removedstrip-ansi@3.0.1(transitive)
- Removedsupports-color@2.0.0(transitive)
- Removedto-fast-properties@1.0.3(transitive)