Skip to content

Commit

Permalink
Merge remote-tracking branch 'refs/remotes/origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
DPende committed Apr 11, 2024
2 parents b21fa4e + e01c892 commit e07c1bb
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 26 deletions.
43 changes: 28 additions & 15 deletions src/editpix.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ EditPix.prototype.getColorPaletteWasm = async (image, colorNumber = 5, quality =
})
} else {
throw new Error("Non-existent algorithm.");
}
}
}).catch(error => { reject(error) })
})
}
Expand All @@ -72,12 +72,14 @@ EditPix.prototype.getImageFromUrl = (url) => {

EditPix.prototype.toGrayScale = (image) => {
const pixelArray = imageManager.getPixelArray(image);
return imageManager.convertToImage(convertToGrayScale(pixelArray), image.naturalWidth, image.naturalHeight);
const imageType = imageManager.getImageType(image.src);
return imageManager.convertToImage(convertToGrayScale(pixelArray), image.naturalWidth, image.naturalHeight, imageType);
}

EditPix.prototype.toBackWhite = (image) => {
const pixelArray = imageManager.getPixelArray(image);
return imageManager.convertToImage(convertToBW(pixelArray), image.naturalWidth, image.naturalHeight);
const imageType = imageManager.getImageType(image.src);
return imageManager.convertToImage(convertToBW(pixelArray), image.naturalWidth, image.naturalHeight, imageType);
}

EditPix.prototype.resizeByPercentage = (image, percentage) => {
Expand Down Expand Up @@ -106,7 +108,8 @@ EditPix.prototype.convertToRgb = (colors) => {

EditPix.prototype.toOptimizedContrast = (image) => {
const pixelArray = imageManager.getPixelArray(image);
return imageManager.convertToImage(optimizeContrast(pixelArray), image.naturalWidth, image.naturalHeight);
const imageType = imageManager.getImageType(image.src);
return imageManager.convertToImage(optimizeContrast(pixelArray), image.naturalWidth, image.naturalHeight, imageType);
}

EditPix.prototype.changeContrast = (image, factor) => {
Expand All @@ -115,29 +118,33 @@ EditPix.prototype.changeContrast = (image, factor) => {
const adjustedFactor = factor / 10 + 4.8;
const pixelArray = imageManager.getPixelArray(image);
const optimizedArray = optimizeContrast(pixelArray);
return imageManager.convertToImage(changeContrast(optimizedArray, adjustedFactor), image.naturalWidth, image.naturalHeight);
const imageType = imageManager.getImageType(image.src);
return imageManager.convertToImage(changeContrast(optimizedArray, adjustedFactor), image.naturalWidth, image.naturalHeight, imageType);
}

EditPix.prototype.changeTemperature = (image, factor) => {
if (factor < -100 || factor > 100)
throw new Error("Invalid contrast factor: must be a value between -100 and 100");
const pixelArray = imageManager.getPixelArray(image);
return imageManager.convertToImage(changeTemperature(pixelArray, factor), image.naturalWidth, image.naturalHeight);
const imageType = imageManager.getImageType(image.src);
return imageManager.convertToImage(changeTemperature(pixelArray, factor), image.naturalWidth, image.naturalHeight, imageType);
}

EditPix.prototype.changeSaturation = (image, factor) => {
if (factor < -100 || factor > 100)
throw new Error("Invalid saturation factor: must be a value between -100 and 100");
const pixelArray = imageManager.getPixelArray(image);
return imageManager.convertToImage(changeSaturation(pixelArray, factor), image.naturalWidth, image.naturalHeight);
const imageType = imageManager.getImageType(image.src);
return imageManager.convertToImage(changeSaturation(pixelArray, factor), image.naturalWidth, image.naturalHeight, imageType);

}

EditPix.prototype.changeBrightness = (image, factor) => {
if (factor < -100 || factor > 100)
throw new Error("Invalid brightness factor: must be a value between -100 and 100");
const pixelArray = imageManager.getPixelArray(image);
return imageManager.convertToImage(changeBrightness(pixelArray, factor), image.naturalWidth, image.naturalHeight);
const imageType = imageManager.getImageType(image.src);
return imageManager.convertToImage(changeBrightness(pixelArray, factor), image.naturalWidth, image.naturalHeight, imageType);
}

EditPix.prototype.rgbToHsl = (r, g, b) => {
Expand All @@ -150,49 +157,55 @@ EditPix.prototype.hslToRgb = (h, s, l) => {

EditPix.prototype.toSepia = (image) => {
const pixelArray = imageManager.getPixelArray(image);
return imageManager.convertToImage(toSepia(pixelArray), image.naturalWidth, image.naturalHeight);
const imageType = imageManager.getImageType(image.src);
return imageManager.convertToImage(toSepia(pixelArray), image.naturalWidth, image.naturalHeight, imageType);
}

EditPix.prototype.changeOpacity = (image, alpha) => {
if (alpha < 0 || alpha > 255)
throw new Error("Invalid alpha value: must be between 0 and 255")
const pixelArray = imageManager.getPixelArray(image);
return imageManager.convertToImage(changeOpacity(pixelArray, alpha), image.naturalWidth, image.naturalHeight);
return imageManager.convertToImage(changeOpacity(pixelArray, alpha), image.naturalWidth, image.naturalHeight, "png");
}

EditPix.prototype.changeTint = (image, factor) => {
if (factor < -100 || factor > 100)
throw new Error("Invalid tint factor: must be a value between -100 and 100");
const pixelArray = imageManager.getPixelArray(image);
return imageManager.convertToImage(changeTint(pixelArray, factor), image.naturalWidth, image.naturalHeight);
const imageType = imageManager.getImageType(image.src);
return imageManager.convertToImage(changeTint(pixelArray, factor), image.naturalWidth, image.naturalHeight, imageType);
}

EditPix.prototype.changeShadows = (image, factor) => {
if (factor < -100 || factor > 100)
throw new Error("Invalid shadow factor: must be a value between -100 and 100");
const pixelArray = imageManager.getPixelArray(image);
return imageManager.convertToImage(changeShadows(pixelArray, factor), image.naturalWidth, image.naturalHeight);
const imageType = imageManager.getImageType(image.src);
return imageManager.convertToImage(changeShadows(pixelArray, factor), image.naturalWidth, image.naturalHeight, imageType);
}

EditPix.prototype.changeExposure = (image, factor) => {
if (factor < -100 || factor > 100)
throw new Error("Invalid exposure factor: must be a value between -100 and 100");
const pixelArray = imageManager.getPixelArray(image);
return imageManager.convertToImage(changeExposure(pixelArray, factor), image.naturalWidth, image.naturalHeight);
const imageType = imageManager.getImageType(image.src);
return imageManager.convertToImage(changeExposure(pixelArray, factor), image.naturalWidth, image.naturalHeight, imageType);
}

EditPix.prototype.changeHighlights = (image, factor) => {
if (factor < -100 || factor > 100)
throw new Error("Invalid shadow factor: must be a value between -100 and 100");
const pixelArray = imageManager.getPixelArray(image);
return imageManager.convertToImage(changeHighlights(pixelArray, factor), image.naturalWidth, image.naturalHeight);
const imageType = imageManager.getImageType(image.src);
return imageManager.convertToImage(changeHighlights(pixelArray, factor), image.naturalWidth, image.naturalHeight, imageType);
}

EditPix.prototype.changeSharpness = (image, factor) => {
if (factor < -100 || factor > 100)
throw new Error("Invalid sharpness factor: must be a value between -100 and 100");
const pixelArray = imageManager.getPixelArray(image);
return imageManager.convertToImage(changeSharpness(pixelArray, factor), image.naturalWidth, image.naturalHeight);
const imageType = imageManager.getImageType(image.src);
return imageManager.convertToImage(changeSharpness(pixelArray, factor), image.naturalWidth, image.naturalHeight, imageType);
}

export default EditPix;
27 changes: 18 additions & 9 deletions src/image-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ function getPixelArray(image) {
}

function resizeByPercentage(image, percentage) {
if (percentage < 1 || percentage > 100) throw new Error("The percentage value is incorrect: it must be between 1 and 100");
if (percentage < 1 || percentage > 100)
throw new Error("The percentage value is incorrect: it must be between 1 and 100");
const { canvas, context } = createCanvas();
const newWidth = image.naturalWidth * (percentage / 100);
const newHeight = image.naturalHeight * (percentage / 100);
Expand All @@ -18,12 +19,13 @@ function resizeByPercentage(image, percentage) {
let resizedImage = new Image();
resizedImage.onload = () => { resolve(resizedImage) }
resizedImage.onerror = (error) => { reject(error) }
resizedImage.src = canvas.toDataURL();
resizedImage.src = canvas.toDataURL("image/" + getImageType(image.src));
})
}

function resizeByWidth(image, newWidth) {
if(newWidth < 0) throw new Error("The width entered is invalid: it must be positive");
if(newWidth < 0)
throw new Error("The width entered is invalid: it must be positive");
const { canvas, context } = createCanvas();
const newHeight = image.naturalHeight * (newWidth / image.naturalWidth);
canvas.width = newWidth;
Expand All @@ -33,12 +35,13 @@ function resizeByWidth(image, newWidth) {
let resizedImage = new Image();
resizedImage.onload = () => { resolve(resizedImage) }
resizedImage.onerror = (error) => { reject(error) }
resizedImage.src = canvas.toDataURL();
resizedImage.src = canvas.toDataURL("image/" + getImageType(image.src));
})
}

function resizeByHeight(image, newHeight) {
if(newHeight < 0) throw new Error("the height entered is invalid: it must be positive");
if(newHeight < 0)
throw new Error("the height entered is invalid: it must be positive");
const { canvas, context } = createCanvas();
const newWidth = image.naturalWidth * (newHeight / image.naturalHeight);
canvas.width = newWidth;
Expand All @@ -48,11 +51,11 @@ function resizeByHeight(image, newHeight) {
let resizedImage = new Image();
resizedImage.onload = () => { resolve(resizedImage) }
resizedImage.onerror = (error) => { reject(error) }
resizedImage.src = canvas.toDataURL();
resizedImage.src = canvas.toDataURL("image/" + getImageType(image.src));
})
}

function convertToImage(pixelArray, width, height) {
function convertToImage(pixelArray, width, height, type) {
const { canvas, context } = createCanvas();
canvas.width = width;
canvas.height = height;
Expand All @@ -63,10 +66,15 @@ function convertToImage(pixelArray, width, height) {
let image = new Image();
image.onload = () => { resolve(image) }
image.onerror = (error) => { reject(error) }
image.src = canvas.toDataURL();
image.src = canvas.toDataURL("image/" + type);
})
}

function getImageType(imageSrc){
const regex = /(jpg|jpeg|png|gif|bmp|webp)/;
return imageSrc.match(regex)[0];
}

function createCanvas() {
const canvas = document.createElement("canvas")
const context = canvas.getContext("2d")
Expand All @@ -79,5 +87,6 @@ export default {
resizeByPercentage,
resizeByWidth,
resizeByHeight,
convertToImage
convertToImage,
getImageType
}
16 changes: 14 additions & 2 deletions test/core.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -337,9 +337,15 @@ describe('changeHiglights', () => {
expect(testColor1[2]).toEqual(testColor2[2]);
});
test('should only darken highlights (luma > 128)', () => {
<<<<<<< HEAD
const testColor1 = [111, 130, 243];
const testColor2 = [111, 130, 243];
changeShadows(testColor1, -5);
=======
const testColor1 = [111, 23, 47];
const testColor2 = [111, 23, 47];
changeHighlights(testColor1, -5);
>>>>>>> refs/remotes/origin/main
expect(testColor1[0]).toEqual(testColor2[0]);
expect(testColor1[1]).toEqual(testColor2[1]);
expect(testColor1[2]).toEqual(testColor2[2]);
Expand All @@ -349,15 +355,21 @@ describe('changeHiglights', () => {
describe('changeSharpness', () => {
test('basic functionality test', () => {
const inputArray = [243, 22, 108, 255, 173, 12, 0, 255]; // An array of pixel values representing an image
const sharpenedArray = changeSharpness(inputArray, 2, 1, 32);
const copyArray = [...inputArray];
changeSharpness(inputArray, 2, 1, 32);

// Verify that the sharpened image is processed correctly
<<<<<<< HEAD
expect(sharpenedArray).toBe(inputArray)
=======
expect(inputArray).not.toBe(copyArray)
>>>>>>> refs/remotes/origin/main
});

test('handle negative factor correctly', () => {
const inputArray = [243, 22, 108, 255, 173, 12, 0, 255]; // An array of pixel values representing an image
const sharpenedArray = changeSharpness(inputArray, 2, 1, -40);
const copyArray = [...inputArray];
changeSharpness(inputArray, 2, 1, -40);

// Verify that the function correctly handles negative sharpening factor
expect(sharpenedArray).toBe(inputArray)
Expand Down

0 comments on commit e07c1bb

Please sign in to comment.