-
-
Notifications
You must be signed in to change notification settings - Fork 30.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix repo build for M1 MacBooks (#1029)
- Loading branch information
Showing
8 changed files
with
1,365 additions
and
2,231 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
85 changes: 85 additions & 0 deletions
85
src/algorithms/image-processing/seam-carving/__tests__/resizeImageWidth.node.js
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 |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import fs from 'fs'; | ||
import { PNG } from 'pngjs'; | ||
|
||
import resizeImageWidth from '../resizeImageWidth'; | ||
|
||
const testImageBeforePath = './src/algorithms/image-processing/seam-carving/__tests__/test-image-before.png'; | ||
const testImageAfterPath = './src/algorithms/image-processing/seam-carving/__tests__/test-image-after.png'; | ||
|
||
/** | ||
* Compares two images and finds the number of different pixels. | ||
* | ||
* @param {ImageData} imgA - ImageData for the first image. | ||
* @param {ImageData} imgB - ImageData for the second image. | ||
* @param {number} threshold - Color difference threshold [0..255]. Smaller - stricter. | ||
* @returns {number} - Number of different pixels. | ||
*/ | ||
function pixelsDiff(imgA, imgB, threshold = 0) { | ||
if (imgA.width !== imgB.width || imgA.height !== imgB.height) { | ||
throw new Error('Images must have the same size'); | ||
} | ||
|
||
let differentPixels = 0; | ||
const numColorParams = 4; // RGBA | ||
|
||
for (let pixelIndex = 0; pixelIndex < imgA.data.length; pixelIndex += numColorParams) { | ||
// Get pixel's color for each image. | ||
const [aR, aG, aB] = imgA.data.subarray(pixelIndex, pixelIndex + numColorParams); | ||
const [bR, bG, bB] = imgB.data.subarray(pixelIndex, pixelIndex + numColorParams); | ||
|
||
// Get average pixel's color for each image (make them greyscale). | ||
const aAvgColor = Math.floor((aR + aG + aB) / 3); | ||
const bAvgColor = Math.floor((bR + bG + bB) / 3); | ||
|
||
// Compare pixel colors. | ||
if (Math.abs(aAvgColor - bAvgColor) > threshold) { | ||
differentPixels += 1; | ||
} | ||
} | ||
|
||
return differentPixels; | ||
} | ||
|
||
const pngLoad = (path) => new Promise((resolve) => { | ||
fs.createReadStream(path) | ||
.pipe(new PNG()) | ||
.on('parsed', function Parsed() { | ||
/** @type {ImageData} */ | ||
const imageData = { | ||
colorSpace: 'srgb', | ||
width: this.width, | ||
height: this.height, | ||
data: this.data, | ||
}; | ||
resolve(imageData); | ||
}); | ||
}); | ||
|
||
describe('resizeImageWidth', () => { | ||
it('should perform content-aware image width reduction', async () => { | ||
const imgBefore = await pngLoad(testImageBeforePath); | ||
const imgAfter = await pngLoad(testImageAfterPath); | ||
|
||
const toWidth = Math.floor(imgBefore.width / 2); | ||
|
||
const { | ||
img: imgResized, | ||
size: resizedSize, | ||
} = resizeImageWidth({ img: imgBefore, toWidth }); | ||
|
||
expect(imgResized).toBeDefined(); | ||
expect(resizedSize).toBeDefined(); | ||
|
||
expect(resizedSize).toEqual({ w: toWidth, h: imgBefore.height }); | ||
expect(imgResized.width).toBe(imgAfter.width); | ||
expect(imgResized.height).toBe(imgAfter.height); | ||
|
||
const colorThreshold = 50; | ||
const differentPixels = pixelsDiff(imgResized, imgAfter, colorThreshold); | ||
|
||
// Allow 10% of pixels to be different | ||
const pixelsThreshold = Math.floor((imgAfter.width * imgAfter.height) / 10); | ||
|
||
expect(differentPixels).toBeLessThanOrEqual(pixelsThreshold); | ||
}); | ||
}); |
91 changes: 0 additions & 91 deletions
91
src/algorithms/image-processing/seam-carving/__tests__/resizeImageWidth.test.js
This file was deleted.
Oops, something went wrong.
Binary file removed
BIN
-5.95 KB
src/algorithms/image-processing/seam-carving/__tests__/test-image-after.jpg
Binary file not shown.
Binary file added
BIN
+6.57 KB
src/algorithms/image-processing/seam-carving/__tests__/test-image-after.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 removed
BIN
-3.32 KB
src/algorithms/image-processing/seam-carving/__tests__/test-image-before.jpg
Binary file not shown.
Binary file added
BIN
+11.5 KB
src/algorithms/image-processing/seam-carving/__tests__/test-image-before.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.