@@ -5,85 +5,111 @@ const yargs = require('yargs');
5
5
const os = require ( 'os' ) ;
6
6
const processImage_1 = require ( './utils/processImage' ) ;
7
7
const processPath_1 = require ( './utils/processPath' ) ;
8
- // Parse command line arguments
9
- const options = yargs
10
- . option ( 'f' , {
11
- alias : 'filename' ,
12
- describe : 'Input image filename' ,
13
- type : 'string' ,
14
- } )
15
- . option ( 'i' , {
16
- alias : 'folderPath' ,
17
- describe : 'Input folder' ,
18
- type : 'string' ,
19
- } )
20
- . option ( 'w' , {
21
- alias : 'width' ,
22
- describe : 'Width of each slice' ,
23
- type : 'number' ,
24
- demandOption : true ,
25
- coerce : ( value ) => {
26
- if ( value < 1 ) {
27
- throw new Error ( 'width should not be lower than 1' ) ;
28
- }
29
- return Math . round ( value ) ;
30
- } ,
31
- } )
32
- . option ( 'h' , {
33
- alias : 'height' ,
34
- describe : 'Height of each slice' ,
35
- type : 'number' ,
36
- coerce : ( value ) => {
37
- if ( value !== undefined && value < 1 ) {
38
- throw new Error ( 'height should not be lower than 1' ) ;
39
- }
40
- return Math . round ( value ) ;
41
- } ,
42
- } )
43
- . option ( 'd' , {
44
- alias : 'canvasWidth' ,
45
- describe : 'Width of canvas for final output' ,
46
- type : 'number' ,
47
- coerce : ( value ) => {
48
- if ( value !== undefined && value < 1 ) {
49
- throw new Error ( 'canvasWidth should not be lower than 1' ) ;
50
- }
51
- return Math . round ( value ) ;
52
- } ,
53
- } )
54
- . option ( 'g' , {
55
- alias : 'canvasHeight' ,
56
- describe : 'Height of canvas for final output' ,
57
- type : 'number' ,
58
- coerce : ( value ) => {
59
- if ( value !== undefined && value < 1 ) {
60
- throw new Error ( 'canvasHeight should not be lower than 1' ) ;
61
- }
62
- return Math . round ( value ) ;
63
- } ,
64
- } ) . argv ;
65
- if ( options . filename ) {
66
- // Process a single image
67
- const { filename, width, height, canvasWidth, canvasHeight } = options ;
68
- ( 0 , processImage_1 . sliceImage ) (
69
- filename ,
70
- width ,
71
- height ,
72
- canvasWidth ,
73
- canvasHeight ,
74
- ) ;
75
- } else if ( options . folderPath ) {
76
- // Process all images in a folder, splitting the task into threads
77
- let numCores = 2 ;
78
- try {
79
- numCores = os . cpus ( ) . length ;
80
- } catch ( err ) {
81
- console . error ( err ) ;
8
+ function main ( ) {
9
+ // Parse command line arguments
10
+ const options = yargs
11
+ . option ( 'f' , {
12
+ alias : 'filename' ,
13
+ describe : 'Input image filename' ,
14
+ type : 'string' ,
15
+ } )
16
+ . option ( 'i' , {
17
+ alias : 'folderPath' ,
18
+ describe : 'Input folder' ,
19
+ type : 'string' ,
20
+ } )
21
+ . option ( 'w' , {
22
+ alias : 'width' ,
23
+ describe : 'Width of each slice' ,
24
+ type : 'number' ,
25
+ demandOption : true ,
26
+ coerce : ( value ) => {
27
+ if ( value < 1 ) {
28
+ throw new Error ( 'width should not be lower than 1' ) ;
29
+ }
30
+ return Math . round ( value ) ;
31
+ } ,
32
+ } )
33
+ . option ( 'h' , {
34
+ alias : 'height' ,
35
+ describe : 'Height of each slice' ,
36
+ type : 'number' ,
37
+ coerce : ( value ) => {
38
+ if ( value !== undefined && value < 1 ) {
39
+ throw new Error ( 'height should not be lower than 1' ) ;
40
+ }
41
+ return Math . round ( value ) ;
42
+ } ,
43
+ } )
44
+ . option ( 'd' , {
45
+ alias : 'canvasWidth' ,
46
+ describe : 'Width of canvas for final output' ,
47
+ type : 'number' ,
48
+ coerce : ( value ) => {
49
+ if ( value !== undefined && value < 1 ) {
50
+ throw new Error ( 'canvasWidth should not be lower than 1' ) ;
51
+ }
52
+ return Math . round ( value ) ;
53
+ } ,
54
+ } )
55
+ . option ( 'g' , {
56
+ alias : 'canvasHeight' ,
57
+ describe : 'Height of canvas for final output' ,
58
+ type : 'number' ,
59
+ coerce : ( value ) => {
60
+ if ( value !== undefined && value < 1 ) {
61
+ throw new Error ( 'canvasHeight should not be lower than 1' ) ;
62
+ }
63
+ return Math . round ( value ) ;
64
+ } ,
65
+ } )
66
+ . option ( 's' , {
67
+ alias : 'scale' ,
68
+ describe :
69
+ 'Rescale the image up or down by this factor, after slicing, but before canvas resize' ,
70
+ type : 'number' ,
71
+ default : 1 ,
72
+ coerce : ( value ) => {
73
+ if ( value <= 0 ) {
74
+ throw new Error ( 'scale should be > 0' ) ;
75
+ }
76
+ return value ;
77
+ } ,
78
+ } )
79
+ . option ( 'c' , {
80
+ alias : 'cubic' ,
81
+ describe :
82
+ 'Uses bicubic interpolation instead of nearest neighbour if rescaling' ,
83
+ type : 'boolean' ,
84
+ default : false ,
85
+ } ) . argv ;
86
+ if ( options . filename ) {
87
+ // Process a single image
88
+ const { filename, width, height, canvasWidth, canvasHeight, scale, cubic } =
89
+ options ;
90
+ ( 0 , processImage_1 . sliceImage ) (
91
+ filename ,
92
+ width ,
93
+ height ,
94
+ canvasWidth ,
95
+ canvasHeight ,
96
+ scale ,
97
+ cubic ,
98
+ ) ;
99
+ } else if ( options . folderPath ) {
100
+ // Process all images in a folder, splitting the task into threads
101
+ let numCores = 2 ;
102
+ try {
103
+ numCores = os . cpus ( ) . length ;
104
+ } catch ( err ) {
105
+ console . error ( err ) ;
106
+ }
107
+ numCores = Math . max ( numCores - 1 , 1 ) ;
108
+ ( 0 , processPath_1 . processPath ) ( options . folderPath , options , numCores ) ;
109
+ } else {
110
+ console . log (
111
+ 'Requires either `filename` or `folderPath`. Run `slice --help` for help.' ,
112
+ ) ;
82
113
}
83
- numCores = Math . max ( numCores - 1 , 1 ) ;
84
- ( 0 , processPath_1 . processPath ) ( options . folderPath , options , numCores ) ;
85
- } else {
86
- console . log (
87
- 'Requires either `filename` or `folderPath`. Run `slice --help` for help.' ,
88
- ) ;
89
114
}
115
+ main ( ) ;
0 commit comments