image-contrast
Advanced tools
Comparing version 0.0.6 to 0.0.7
56
index.js
@@ -1,7 +0,32 @@ | ||
function copyImageData(ctx, src) { | ||
var dst = ctx.createImageData(src.width, src.height); | ||
dst.data.set(src.data); | ||
return dst; | ||
/** | ||
* @name getCanvas | ||
* @param {number} w - width | ||
* @param {number} h - height | ||
* Create a canvas with the currect size | ||
*/ | ||
function getCanvas(w, h) { | ||
var canvas = document.createElement('canvas'); | ||
canvas.width = w; | ||
canvas.height = h; | ||
return canvas; | ||
} | ||
/** | ||
* @name getPixels | ||
* @param {object} canvas | ||
* @param {object} context | ||
* @param {object} imageData | ||
* Get a deep copy of the image data so we don't change the original imageData | ||
*/ | ||
function getPixels(canvas, context, imageData) { | ||
context.putImageData(imageData, 0, 0); | ||
return context.getImageData(0, 0, canvas.width, canvas.height); | ||
} | ||
/** | ||
* @name getFactor | ||
* @param {number} contrast | ||
* Get the contrast factor based on the provided contrast value | ||
*/ | ||
function getFactor(contrast) { | ||
@@ -11,9 +36,17 @@ return (259 * (contrast + 255)) / (255 * (259 - contrast)); | ||
/** | ||
* @name transform | ||
* @param {object} canvas | ||
* @param {object} context | ||
* @param {object} imageData | ||
* @param {number} factor | ||
* Iterate over the array applying the contrast transformation | ||
*/ | ||
function transform(canvas, context, imageData, factor) { | ||
var data = imageData.data; | ||
data.forEach(function (item, i) { | ||
for (var i = 0; i < data.length; i+= 4) { | ||
data[i] = factor * (data[i] - 128) + 128; | ||
data[i+1] = factor * (data[i+1] - 128) + 128; | ||
data[i+2] = factor * (data[i+2] - 128) + 128; | ||
}); | ||
} | ||
@@ -33,11 +66,14 @@ context.putImageData(imageData, 0, 0); | ||
var result; | ||
var canvas = document.createElement('canvas'); | ||
var context = canvas.getContext('2d'); | ||
var canvas; | ||
var context; | ||
if (!options.data || !options.contrast) { | ||
throw new Error('image-brightness:: invalid options provided'); | ||
throw new Error('image-contrast:: invalid options provided'); | ||
} | ||
options.data = copyImageData(context, options.data); | ||
canvas = getCanvas(options.data.width, options.data.height); | ||
context = canvas.getContext('2d'); | ||
options.data = getPixels(canvas, context, options.data); | ||
factor = getFactor(options.contrast) | ||
@@ -44,0 +80,0 @@ result = transform(canvas, context, options.data, factor); |
{ | ||
"name": "image-contrast", | ||
"version": "0.0.6", | ||
"version": "0.0.7", | ||
"description": "Small library to apply a contrast transformation to a image", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -16,6 +16,8 @@ var imageContrast = require('../index'); | ||
var canvas = document.createElement('canvas'); | ||
var context = canvas.getContext('2d'); | ||
var img = new Image; | ||
img.onload = function(){ | ||
var canvas = document.createElement('canvas'); | ||
canvas.width = img.width; | ||
canvas.height = img.height; | ||
var context = canvas.getContext('2d'); | ||
context.drawImage(img,0,0); | ||
@@ -27,3 +29,3 @@ | ||
data: data, | ||
contrast: 30 | ||
contrast: 50 | ||
}); | ||
@@ -34,3 +36,3 @@ applyResults('#target-1', results1); | ||
data: data, | ||
contrast: 70 | ||
contrast: 100 | ||
}); | ||
@@ -37,0 +39,0 @@ applyResults('#target-2', results2); |
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
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
5686
101