Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit14c5636

Browse files
authored
Fix repo build for M1 MacBooks (trekhleb#1029)
1 parentbbbfd32 commit14c5636

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 aboutcustomizing 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+
importfsfrom'fs';
2+
import{PNG}from'pngjs';
3+
4+
importresizeImageWidthfrom'../resizeImageWidth';
5+
6+
consttestImageBeforePath='./src/algorithms/image-processing/seam-carving/__tests__/test-image-before.png';
7+
consttestImageAfterPath='./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+
functionpixelsDiff(imgA,imgB,threshold=0){
18+
if(imgA.width!==imgB.width||imgA.height!==imgB.height){
19+
thrownewError('Images must have the same size');
20+
}
21+
22+
letdifferentPixels=0;
23+
constnumColorParams=4;// RGBA
24+
25+
for(letpixelIndex=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+
constaAvgColor=Math.floor((aR+aG+aB)/3);
32+
constbAvgColor=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+
returndifferentPixels;
41+
}
42+
43+
constpngLoad=(path)=>newPromise((resolve)=>{
44+
fs.createReadStream(path)
45+
.pipe(newPNG())
46+
.on('parsed',functionParsed(){
47+
/**@type {ImageData} */
48+
constimageData={
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+
constimgBefore=awaitpngLoad(testImageBeforePath);
61+
constimgAfter=awaitpngLoad(testImageAfterPath);
62+
63+
consttoWidth=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+
constcolorThreshold=50;
78+
constdifferentPixels=pixelsDiff(imgResized,imgAfter,colorThreshold);
79+
80+
// Allow 10% of pixels to be different
81+
constpixelsThreshold=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)

[8]ページ先頭

©2009-2025 Movatter.jp