Skip to content

Commit 14c5636

Browse files
authored
Fix repo build for M1 MacBooks (trekhleb#1029)
1 parent bbbfd32 commit 14c5636

File tree

8 files changed

+1365
-2231
lines changed

8 files changed

+1365
-2231
lines changed

package-lock.json

Lines changed: 1278 additions & 2138 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@
3838
"@babel/cli": "7.20.7",
3939
"@babel/preset-env": "7.20.2",
4040
"@types/jest": "29.4.0",
41-
"canvas": "2.11.0",
4241
"eslint": "8.33.0",
4342
"eslint-config-airbnb": "19.0.4",
4443
"eslint-plugin-import": "2.27.5",
4544
"eslint-plugin-jest": "27.2.1",
4645
"eslint-plugin-jsx-a11y": "6.7.1",
4746
"husky": "8.0.3",
48-
"jest": "29.4.1"
47+
"jest": "29.4.1",
48+
"pngjs": "^7.0.0"
4949
},
5050
"engines": {
5151
"node": ">=16.15.0",
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import fs from 'fs';
2+
import { PNG } from 'pngjs';
3+
4+
import resizeImageWidth from '../resizeImageWidth';
5+
6+
const testImageBeforePath = './src/algorithms/image-processing/seam-carving/__tests__/test-image-before.png';
7+
const testImageAfterPath = './src/algorithms/image-processing/seam-carving/__tests__/test-image-after.png';
8+
9+
/**
10+
* Compares two images and finds the number of different pixels.
11+
*
12+
* @param {ImageData} imgA - ImageData for the first image.
13+
* @param {ImageData} imgB - ImageData for the second image.
14+
* @param {number} threshold - Color difference threshold [0..255]. Smaller - stricter.
15+
* @returns {number} - Number of different pixels.
16+
*/
17+
function pixelsDiff(imgA, imgB, threshold = 0) {
18+
if (imgA.width !== imgB.width || imgA.height !== imgB.height) {
19+
throw new Error('Images must have the same size');
20+
}
21+
22+
let differentPixels = 0;
23+
const numColorParams = 4; // RGBA
24+
25+
for (let pixelIndex = 0; pixelIndex < imgA.data.length; pixelIndex += numColorParams) {
26+
// Get pixel's color for each image.
27+
const [aR, aG, aB] = imgA.data.subarray(pixelIndex, pixelIndex + numColorParams);
28+
const [bR, bG, bB] = imgB.data.subarray(pixelIndex, pixelIndex + numColorParams);
29+
30+
// Get average pixel's color for each image (make them greyscale).
31+
const aAvgColor = Math.floor((aR + aG + aB) / 3);
32+
const bAvgColor = Math.floor((bR + bG + bB) / 3);
33+
34+
// Compare pixel colors.
35+
if (Math.abs(aAvgColor - bAvgColor) > threshold) {
36+
differentPixels += 1;
37+
}
38+
}
39+
40+
return differentPixels;
41+
}
42+
43+
const pngLoad = (path) => new Promise((resolve) => {
44+
fs.createReadStream(path)
45+
.pipe(new PNG())
46+
.on('parsed', function Parsed() {
47+
/** @type {ImageData} */
48+
const imageData = {
49+
colorSpace: 'srgb',
50+
width: this.width,
51+
height: this.height,
52+
data: this.data,
53+
};
54+
resolve(imageData);
55+
});
56+
});
57+
58+
describe('resizeImageWidth', () => {
59+
it('should perform content-aware image width reduction', async () => {
60+
const imgBefore = await pngLoad(testImageBeforePath);
61+
const imgAfter = await pngLoad(testImageAfterPath);
62+
63+
const toWidth = Math.floor(imgBefore.width / 2);
64+
65+
const {
66+
img: imgResized,
67+
size: resizedSize,
68+
} = resizeImageWidth({ img: imgBefore, toWidth });
69+
70+
expect(imgResized).toBeDefined();
71+
expect(resizedSize).toBeDefined();
72+
73+
expect(resizedSize).toEqual({ w: toWidth, h: imgBefore.height });
74+
expect(imgResized.width).toBe(imgAfter.width);
75+
expect(imgResized.height).toBe(imgAfter.height);
76+
77+
const colorThreshold = 50;
78+
const differentPixels = pixelsDiff(imgResized, imgAfter, colorThreshold);
79+
80+
// Allow 10% of pixels to be different
81+
const pixelsThreshold = Math.floor((imgAfter.width * imgAfter.height) / 10);
82+
83+
expect(differentPixels).toBeLessThanOrEqual(pixelsThreshold);
84+
});
85+
});

src/algorithms/image-processing/seam-carving/__tests__/resizeImageWidth.test.js

Lines changed: 0 additions & 91 deletions
This file was deleted.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy