Skip to content

Commit 699a10e

Browse files
committed
Add new arguments
1 parent 66fea49 commit 699a10e

File tree

5 files changed

+60
-14
lines changed

5 files changed

+60
-14
lines changed

@types/types.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,6 @@ declare type Options = {
33
folderPath?: string;
44
width: number;
55
height?: number;
6+
canvasWidth?: number;
7+
canvasHeight?: number;
68
};

slice.cjs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,27 @@ const options = yargs
3939
}
4040
return Math.round(value);
4141
},
42+
})
43+
.option('d', {
44+
alias: 'canvasWidth',
45+
describe: 'Width of the canvas',
46+
type: 'number',
47+
})
48+
.option('g', {
49+
alias: 'canvasHeight',
50+
describe: 'Height of the canvas',
51+
type: 'number',
4252
}).argv;
4353
if (options.filename) {
4454
// Process a single image
45-
const { filename, width, height } = options;
46-
(0, processImage_1.sliceImage)(filename, width, height);
55+
const { filename, width, height, canvasWidth, canvasHeight } = options;
56+
(0, processImage_1.sliceImage)(
57+
filename,
58+
width,
59+
height,
60+
canvasWidth,
61+
canvasHeight,
62+
);
4763
} else if (options.folderPath) {
4864
// Process all images in a folder, splitting the task into threads
4965
let numCores = 2;

src/slice.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,22 @@ const options = yargs
4040
}
4141
return Math.round(value);
4242
},
43+
})
44+
.option('d', {
45+
alias: 'canvasWidth',
46+
describe: 'Width of canvas for final output',
47+
type: 'number',
48+
})
49+
.option('g', {
50+
alias: 'canvasHeight',
51+
describe: 'Height of canvas for final output',
52+
type: 'number',
4353
}).argv as unknown as Options;
4454

4555
if (options.filename) {
4656
// Process a single image
47-
const { filename, width, height } = options;
48-
sliceImage(filename, width, height);
57+
const { filename, width, height, canvasWidth, canvasHeight } = options;
58+
sliceImage(filename, width, height, canvasWidth, canvasHeight);
4959
} else if (options.folderPath) {
5060
// Process all images in a folder, splitting the task into threads
5161
let numCores = 2;

src/utils/processImage.ts

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ export function sliceImage(
1212
filename: string,
1313
width: number,
1414
height?: number,
15+
canvasWidth?: number,
16+
canvasHeight?: number,
1517
skipExtCheck?: boolean,
1618
): void {
1719
Jimp.read(filename, (err, image) => {
@@ -20,7 +22,14 @@ export function sliceImage(
2022
} else {
2123
// Continue slicing if image is successfully read
2224
if (image) {
23-
continueSlicing(image, width, height, filename);
25+
continueSlicing(
26+
image,
27+
width,
28+
height,
29+
canvasWidth,
30+
canvasHeight,
31+
filename,
32+
);
2433
return;
2534
}
2635
}
@@ -41,7 +50,14 @@ export function sliceImage(
4150
Jimp.read(fullFilename, (err, image) => {
4251
if (!foundImage && !err) {
4352
foundImage = true;
44-
continueSlicing(image, width, height, fullFilename);
53+
continueSlicing(
54+
image,
55+
width,
56+
height,
57+
canvasWidth,
58+
canvasHeight,
59+
fullFilename,
60+
);
4561
}
4662
});
4763
}
@@ -55,6 +71,8 @@ function continueSlicing(
5571
image: Jimp,
5672
width: number,
5773
height: number | undefined,
74+
canvasWidth: number | undefined,
75+
canvasHeight: number | undefined,
5876
inputFilename: string,
5977
): void {
6078
// If height is not specified, use width as height
@@ -100,6 +118,6 @@ function continueSlicing(
100118
if (!isMainThread) {
101119
const { filePath, options } = workerData;
102120
options.filename = filePath;
103-
const { filename, width, height } = options;
104-
sliceImage(filename, width, height, true);
121+
const { filename, width, height, canvasWidth, canvasHeight } = options;
122+
sliceImage(filename, width, height, canvasWidth, canvasHeight, true);
105123
}

utils/processImage.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ const outputFolder = 'output';
99
/**
1010
* Function to slice an image into smaller segments
1111
*/
12-
function sliceImage(filename, width, height, skipExtCheck) {
12+
function sliceImage(filename, width, height, canvasWidth, canvasHeight, skipExtCheck) {
1313
Jimp.read(filename, (err, image) => {
1414
if (err && skipExtCheck) {
1515
console.error(err);
1616
}
1717
else {
1818
// Continue slicing if image is successfully read
1919
if (image) {
20-
continueSlicing(image, width, height, filename);
20+
continueSlicing(image, width, height, canvasWidth, canvasHeight, filename);
2121
return;
2222
}
2323
}
@@ -35,7 +35,7 @@ function sliceImage(filename, width, height, skipExtCheck) {
3535
Jimp.read(fullFilename, (err, image) => {
3636
if (!foundImage && !err) {
3737
foundImage = true;
38-
continueSlicing(image, width, height, fullFilename);
38+
continueSlicing(image, width, height, canvasWidth, canvasHeight, fullFilename);
3939
}
4040
});
4141
}
@@ -45,7 +45,7 @@ exports.sliceImage = sliceImage;
4545
/**
4646
* Continue slicing the image into smaller segments
4747
*/
48-
function continueSlicing(image, width, height, inputFilename) {
48+
function continueSlicing(image, width, height, canvasWidth, canvasHeight, inputFilename) {
4949
// If height is not specified, use width as height
5050
height = height || width;
5151
const imageWidth = image.getWidth();
@@ -77,6 +77,6 @@ function continueSlicing(image, width, height, inputFilename) {
7777
if (!worker_threads_1.isMainThread) {
7878
const { filePath, options } = worker_threads_1.workerData;
7979
options.filename = filePath;
80-
const { filename, width, height } = options;
81-
sliceImage(filename, width, height, true);
80+
const { filename, width, height, canvasWidth, canvasHeight } = options;
81+
sliceImage(filename, width, height, canvasWidth, canvasHeight, true);
8282
}

0 commit comments

Comments
 (0)