Skip to content

Commit 75fac20

Browse files
committed
Minor refactor
1 parent 536bc48 commit 75fac20

File tree

4 files changed

+22
-88
lines changed

4 files changed

+22
-88
lines changed

slice.cjs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -97,17 +97,7 @@ function main() {
9797
}).argv;
9898
if (options.filename) {
9999
// Process a single image
100-
const { filename, width, height, canvasWidth, canvasHeight, scale, cubic } =
101-
options;
102-
(0, processImage_1.sliceImage)(
103-
filename,
104-
width,
105-
height,
106-
canvasWidth,
107-
canvasHeight,
108-
scale,
109-
cubic,
110-
);
100+
(0, processImage_1.sliceImage)(options);
111101
} else if (options.folderPath) {
112102
// Process all images in a folder, splitting the task into threads
113103
let numCores = 1;

src/slice.ts

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,7 @@ function main() {
9999

100100
if (options.filename) {
101101
// Process a single image
102-
const { filename, width, height, canvasWidth, canvasHeight, scale, cubic } =
103-
options;
104-
sliceImage(
105-
filename,
106-
width,
107-
height,
108-
canvasWidth,
109-
canvasHeight,
110-
scale,
111-
cubic,
112-
);
102+
sliceImage(options);
113103
} else if (options.folderPath) {
114104
// Process all images in a folder, splitting the task into threads
115105
let numCores = 1;

src/utils/processImage.ts

Lines changed: 11 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,14 @@ function errorCallback(err: unknown) {
1212
/**
1313
* Function to slice an image into smaller segments
1414
*/
15-
export function sliceImage(
16-
filename: string,
17-
width: number,
18-
height: number | undefined,
19-
canvasWidth: number | undefined,
20-
canvasHeight: number | undefined,
21-
scale: number,
22-
cubic: boolean,
23-
skipExtCheck?: boolean,
24-
): void {
25-
Jimp.read(filename)
15+
export function sliceImage(options: Options, skipExtCheck?: boolean): void {
16+
const { filename } = options;
17+
Jimp.read(filename!)
2618
.then((image) => {
2719
// Continue slicing if image is successfully read
2820
if (image) {
2921
skipExtCheck = true;
30-
continueSlicing(
31-
image,
32-
width,
33-
height,
34-
canvasWidth,
35-
canvasHeight,
36-
scale,
37-
cubic,
38-
filename,
39-
);
22+
continueSlicing(image, options);
4023
}
4124
})
4225
.catch((err) => {
@@ -60,16 +43,7 @@ export function sliceImage(
6043
Jimp.read(fullFilename)
6144
.then((image) => {
6245
foundImage = true;
63-
continueSlicing(
64-
image,
65-
width,
66-
height,
67-
canvasWidth,
68-
canvasHeight,
69-
scale,
70-
cubic,
71-
fullFilename,
72-
);
46+
continueSlicing(image, options);
7347
})
7448
// Silence errors since we'll handle them later
7549
.catch(() => {})
@@ -90,20 +64,12 @@ export function sliceImage(
9064
/**
9165
* Continue slicing the image into smaller segments
9266
*/
93-
function continueSlicing(
94-
image: Jimp,
95-
width: number,
96-
height: number | undefined,
97-
canvasWidth: number | undefined,
98-
canvasHeight: number | undefined,
99-
scale: number,
100-
cubic: boolean,
101-
inputFilename: string,
102-
): void {
67+
function continueSlicing(image: Jimp, options: Options): void {
10368
console.time('Done in');
10469

70+
const { filename, width, canvasWidth, canvasHeight, scale, cubic } = options;
10571
// If height is not specified, use width as height
106-
height = height || width;
72+
const height = options.height || options.width;
10773

10874
const imageWidth = image.getWidth();
10975
const imageHeight = image.getHeight();
@@ -130,10 +96,7 @@ function continueSlicing(
13096
const slice = image.clone().crop(startX, startY, sliceWidth, sliceHeight);
13197

13298
// Incorporate the input filename into the output filename
133-
const baseFilename = path.basename(
134-
inputFilename,
135-
path.extname(inputFilename),
136-
);
99+
const baseFilename = path.basename(filename!, path.extname(filename!));
137100
const outputFilename = `${outputFolder}/${baseFilename}_${x}_${y}.png`;
138101

139102
if (canvasWidth || canvasHeight) {
@@ -179,16 +142,6 @@ function continueSlicing(
179142
if (!isMainThread) {
180143
const { filePath, options } = workerData;
181144
options.filename = filePath;
182-
const { filename, width, height, canvasWidth, canvasHeight, scale, cubic } =
183-
options;
184-
sliceImage(
185-
filename,
186-
width,
187-
height,
188-
canvasWidth,
189-
canvasHeight,
190-
scale,
191-
cubic,
192-
true,
193-
);
145+
146+
sliceImage(options, true);
194147
}

utils/processImage.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,14 @@ function errorCallback(err) {
1313
/**
1414
* Function to slice an image into smaller segments
1515
*/
16-
function sliceImage(filename, width, height, canvasWidth, canvasHeight, scale, cubic, skipExtCheck) {
16+
function sliceImage(options, skipExtCheck) {
17+
const { filename } = options;
1718
Jimp.read(filename)
1819
.then((image) => {
1920
// Continue slicing if image is successfully read
2021
if (image) {
2122
skipExtCheck = true;
22-
continueSlicing(image, width, height, canvasWidth, canvasHeight, scale, cubic, filename);
23+
continueSlicing(image, options);
2324
}
2425
})
2526
.catch((err) => {
@@ -40,7 +41,7 @@ function sliceImage(filename, width, height, canvasWidth, canvasHeight, scale, c
4041
return (Jimp.read(fullFilename)
4142
.then((image) => {
4243
foundImage = true;
43-
continueSlicing(image, width, height, canvasWidth, canvasHeight, scale, cubic, fullFilename);
44+
continueSlicing(image, options);
4445
})
4546
// Silence errors since we'll handle them later
4647
.catch(() => { }));
@@ -59,10 +60,11 @@ exports.sliceImage = sliceImage;
5960
/**
6061
* Continue slicing the image into smaller segments
6162
*/
62-
function continueSlicing(image, width, height, canvasWidth, canvasHeight, scale, cubic, inputFilename) {
63+
function continueSlicing(image, options) {
6364
console.time('Done in');
65+
const { filename, width, canvasWidth, canvasHeight, scale, cubic } = options;
6466
// If height is not specified, use width as height
65-
height = height || width;
67+
const height = options.height || options.width;
6668
const imageWidth = image.getWidth();
6769
const imageHeight = image.getHeight();
6870
// Calculate the number of slices in both dimensions
@@ -82,7 +84,7 @@ function continueSlicing(image, width, height, canvasWidth, canvasHeight, scale,
8284
const sliceHeight = Math.min(height, imageHeight - startY);
8385
const slice = image.clone().crop(startX, startY, sliceWidth, sliceHeight);
8486
// Incorporate the input filename into the output filename
85-
const baseFilename = path.basename(inputFilename, path.extname(inputFilename));
87+
const baseFilename = path.basename(filename, path.extname(filename));
8688
const outputFilename = `${outputFolder}/${baseFilename}_${x}_${y}.png`;
8789
if (canvasWidth || canvasHeight) {
8890
// Calculate canvas dimensions
@@ -114,6 +116,5 @@ function continueSlicing(image, width, height, canvasWidth, canvasHeight, scale,
114116
if (!worker_threads_1.isMainThread) {
115117
const { filePath, options } = worker_threads_1.workerData;
116118
options.filename = filePath;
117-
const { filename, width, height, canvasWidth, canvasHeight, scale, cubic } = options;
118-
sliceImage(filename, width, height, canvasWidth, canvasHeight, scale, cubic, true);
119+
sliceImage(options, true);
119120
}

0 commit comments

Comments
 (0)