-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from studio-YOLO/9-functions-optimization
Functions optimization
- Loading branch information
Showing
4 changed files
with
60 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,25 @@ | ||
/** | ||
* Functiom that given array in the format [R, G, B, alpha,..., R, G, B, alpha] it converts in white and black in the format of a list. Possible values are just 0 and 255 | ||
* @param {number[][]} pixelArray: image that has to be encrypt in the format [R, G, B, alpha,..., R, G, B, alpha] | ||
* @returns {number[]} blackWhitePixelArray: black and white image in the format of a list. Possible values are just 0 and 255 | ||
* Function to convert an image from color to black and white. | ||
* @param {number[]} pixelArray: Image pixel array in the format [R, G, B, alpha,..., R, G, B, alpha]. | ||
* @returns {number[]} array of black and white image pixels. | ||
*/ | ||
function convertToBW(pixelArray) { | ||
const blackWhitePixelArray = []; | ||
for (let i = 0; i < pixelArray.length / 4; i++) { | ||
for (let i = 0; i < pixelArray.length; i += 4) { | ||
const grayValue = | ||
0.299 * pixelArray[i * 4] + | ||
0.587 * pixelArray[i * 4 + 1] + | ||
0.114 * pixelArray[i * 4 + 2]; | ||
if (grayValue >= 128) blackWhitePixelArray.push(255, 255, 255, pixelArray[i * 4 + 3]); | ||
else blackWhitePixelArray.push(0, 0, 0, pixelArray[i * 4 + 3]); | ||
0.299 * pixelArray[i] + | ||
0.587 * pixelArray[i + 1] + | ||
0.114 * pixelArray[i + 2]; | ||
if (grayValue >= 128) { | ||
pixelArray[i] = 255; | ||
pixelArray[i + 1] = 255; | ||
pixelArray[i + 2] = 255; | ||
}else { | ||
pixelArray[i] = 0; | ||
pixelArray[i + 1] = 0; | ||
pixelArray[i + 2] = 0; | ||
} | ||
} | ||
return blackWhitePixelArray; | ||
return pixelArray; | ||
} | ||
|
||
export default convertToBW; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,22 @@ | ||
import utils from "../utils.js"; | ||
|
||
/** | ||
* Function to change the saturation of an image | ||
* @param {number[]} pixelArray: Image pixel array in the format [R, G, B, alpha,..., R, G, B, alpha] | ||
* @param {number} factor: factor to adjust the saturation (-100 to 100) | ||
* @returns {number[]} adjustedPixelArray: Adjusted image pixel array in the format [R, G, B, alpha,..., R, G, B, alpha] | ||
* Function to change the saturation of an image. | ||
* @param {number[]} pixelArray: Image pixel array in the format [R, G, B, alpha,..., R, G, B, alpha]. | ||
* @param {number} factor: factor to adjust the saturation (-100 to 100). | ||
* @returns {number[]} pixel array of the saturated or desaturated image. | ||
*/ | ||
function changeSaturation(pixelArray, factor) { | ||
const adjustedPixelArray = []; | ||
for (let i = 0; i < pixelArray.length; i += 4) { | ||
const hsl = utils.rgbToHsl(pixelArray[i], pixelArray[i + 1], pixelArray[i + 2]); | ||
hsl[1] = Math.max(0, Math.min(100, hsl[1] += factor)); | ||
const rgb = utils.hslToRgb(hsl[0], hsl[1], hsl[2]); | ||
|
||
adjustedPixelArray.push(rgb[0], rgb[1], rgb[2], pixelArray[i + 3]); | ||
pixelArray[i] = rgb[0]; | ||
pixelArray[i + 1] = rgb[1]; | ||
pixelArray[i + 2] = rgb[2]; | ||
} | ||
return adjustedPixelArray; | ||
return pixelArray; | ||
} | ||
|
||
export default changeSaturation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
/** | ||
* Functiom that given array in the format [R, G, B, alfa,..., R, G, B, alfa] it converts it gray scale in format [g1,g1,g1,alfa,...,gn,gn,gn,alfa] | ||
* @param {number[]} pixelArray: image that has to be encrypt in the format [R, G, B, alfa,..., R, G, B, alfa] | ||
* @returns {number[][]} grayScaledPixelArray: gray scale image in the format [g1, g1, g1, alfa,..., gn, gn, gn, alfa] | ||
* Function to convert an image to grayscale. | ||
* @param {number[]} pixelArray: Image pixel array in the format [R, G, B, alpha,..., R, G, B, alpha]. | ||
* @returns {number[]} array of grayscale pixels of an image. | ||
*/ | ||
function convertToGrayScale(pixelArray) { | ||
const grayScaledPixelArray = []; | ||
for (let i = 0; i < pixelArray.length / 4; i++) { | ||
for (let i = 0; i < pixelArray.length; i += 4) { | ||
const grayValue = | ||
0.299 * pixelArray[4 * i] + | ||
0.587 * pixelArray[4 * i + 1] + | ||
0.114 * pixelArray[4 * i + 2]; | ||
grayScaledPixelArray.push(grayValue, grayValue, grayValue, pixelArray[4 * i + 3]); | ||
0.299 * pixelArray[i] + | ||
0.587 * pixelArray[i + 1] + | ||
0.114 * pixelArray[i + 2]; | ||
|
||
pixelArray[i] = grayValue; | ||
pixelArray[i + 1] = grayValue; | ||
pixelArray[i + 2] = grayValue; | ||
} | ||
return grayScaledPixelArray; | ||
return pixelArray; | ||
} | ||
|
||
export default convertToGrayScale; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters