Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add contrast filter #32

Merged
merged 2 commits into from
Jul 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 48 additions & 5 deletions dist/bundle.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

/*
* lena.js - 0.3.1
* lena.js - 0.5.0
* Library for image processing <https://github.com/davidsonfellipe/lena-js/>
*
* Made by Davidson Fellipe.
Expand All @@ -21,6 +21,49 @@ const invert = function (pixels) {
return pixels
};

const contrast = function (pixels, amount) {
const level = Math.pow((amount + 100) / 100, 2);

let data = pixels.data;
let r;
let g;
let b;

for (let i = 0; i < data.length; i += 4) {
r = data[i];
g = data[i + 1];
b = data[i + 2];

r = r / 255;
r -= 0.5;
r *= level;
r += 0.5;
r *= 255;

g = g / 255;
g -= 0.5;
g *= level;
g += 0.5;
g *= 255;

b = b / 255;
b -= 0.5;
b *= level;
b += 0.5;
b *= 255;

r = r < 0 ? 0 : r > 255 ? 255 : r;
g = g < 0 ? 0 : g > 255 ? 255 : g;
b = b < 0 ? 0 : b > 255 ? 255 : b;

data[i] = r;
data[i + 1] = g;
data[i + 2] = b;
}

return pixels
};

const mirror = function (pixels) {
let tmp = [];
const width = pixels.width * 4;
Expand Down Expand Up @@ -111,7 +154,7 @@ const brightness = function (pixels, amount = 0) {
return pixels
};

const sepia = function (pixels) {
const sepia = function (pixels, amount = 100) {
for (let i = 0; i < pixels.data.length; i += 4) {
let r = pixels.data[i],
g = pixels.data[i + 1],
Expand Down Expand Up @@ -151,16 +194,15 @@ const saturation = function (pixels) {
return pixels
};

const thresholding = function (pixels, args) {
const thresholding = function (pixels, amount = 128) {
for (let i = 0; i < pixels.data.length; i += 4) {
let r = pixels.data[i],
g = pixels.data[i + 1],
b = pixels.data[i + 2];

let v = 0.2126 * r + 0.7152 * g + 0.0722 * b;
let thr = args || 128;

pixels.data[i] = pixels.data[i + 1] = pixels.data[i + 2] = v > thr ? 255 : 0;
pixels.data[i] = pixels.data[i + 1] = pixels.data[i + 2] = v > amount ? 255 : 0;
}

return pixels
Expand Down Expand Up @@ -494,6 +536,7 @@ exports.bigGaussian = bigGaussian;
exports.blue = blue;
exports.brightness = brightness;
exports.canny = canny;
exports.contrast = contrast;
exports.filterImage = filterImage;
exports.gaussian = gaussian;
exports.getImage = getImage;
Expand Down
2 changes: 1 addition & 1 deletion dist/bundle.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions docs/all-filters.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<body>
<div id="result" class="board">
<div class="box">
<img id="image" src="assets/img/coffee.jpg" width="300" height="400" />
<img id="image" src="assets/img/sf.png" width="300" height="400" />
</div>
</div>

Expand All @@ -21,6 +21,12 @@
var result = document.getElementById('result')

var filters = [
{ name: 'contrast', amount: 100 },
{ name: 'contrast', amount: 50 },
{ name: 'contrast', amount: 0 },
{ name: 'contrast', amount: -50 },
{ name: 'contrast', amount: -100 },
{ name: 'grayscale', amount: 0 },
{ name: 'brightness', amount: -50 },
{ name: 'brightness', amount: 0 },
{ name: 'brightness', amount: 50 },
Expand All @@ -31,7 +37,6 @@
{ name: 'invert', amount: 0 },
{ name: 'mirror', amount: 0 },
{ name: 'thresholding', amount: 0 },
{ name: 'grayscale', amount: 0 },
{ name: 'sepia', amount: 0 },
{ name: 'saturation', amount: 0 },
{ name: 'roberts', amount: 0 },
Expand Down
Binary file added docs/assets/img/camera.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/assets/img/sf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions lib/filters/contrast.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const contrast = function (pixels, amount) {
const level = Math.pow((amount + 100) / 100, 2)

let data = pixels.data
let r
let g
let b

for (let i = 0; i < data.length; i += 4) {
r = data[i]
g = data[i + 1]
b = data[i + 2]

r = r / 255
r -= 0.5
r *= level
r += 0.5
r *= 255

g = g / 255
g -= 0.5
g *= level
g += 0.5
g *= 255

b = b / 255
b -= 0.5
b *= level
b += 0.5
b *= 255

r = r < 0 ? 0 : r > 255 ? 255 : r
g = g < 0 ? 0 : g > 255 ? 255 : g
b = b < 0 ? 0 : b > 255 ? 255 : b

data[i] = r
data[i + 1] = g
data[i + 2] = b
}

return pixels
}

export default contrast
1 change: 1 addition & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { default as invert } from './filters/invert'
export { default as contrast } from './filters/contrast'
export { default as mirror } from './filters/mirror'

// RGB filters
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "lena.js",
"version": "0.4.2",
"version": "0.5.0",
"description": "Library for image processing",
"homepage": "https://github.com/davidsonfellipe/lena-js/",
"author": {
Expand Down