-
Notifications
You must be signed in to change notification settings - Fork 0
/
px.cjs
277 lines (277 loc) · 6.65 KB
/
px.cjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#!/usr/bin/env node
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const yargs = require('yargs');
const os = require('os');
const processImage_1 = require('./utils/processImage');
const processPath_1 = require('./utils/processPath');
async function main() {
// console.time('Done in');
// Parse command line arguments
const options = await yargs(process.argv.slice(2))
.option('f', {
alias: 'filename',
describe: 'Input image filename',
type: 'string',
coerce: (value) => {
if (Array.isArray(value)) {
value = value.join('');
}
return value;
},
})
.option('i', {
alias: 'folderPath',
describe: 'Input folder',
type: 'string',
coerce: (value) => {
if (Array.isArray(value)) {
value = value.join('');
}
return value;
},
})
.option('s', {
alias: 'scale',
describe: 'Scale the image up or down by this factor',
type: 'number',
default: 1,
coerce: (value) => {
if (value <= 0) {
throw new Error('scale should be > 0');
}
return value;
},
})
.option('a', {
alias: 'afterScale',
describe: 'Rescale the image up or down by this factor, as the last step',
type: 'number',
default: 1,
coerce: (value) => {
if (value <= 0) {
throw new Error('scale should be > 0');
}
return value;
},
})
.option('x', {
alias: 'contrast',
describe: 'Adjust contrast by a value between -1 and +1',
type: 'number',
default: 0,
coerce: (value) => {
if (value < -1 || value > 1) {
throw new Error('contrast should be between -1 and 1');
}
return value;
},
})
.option('z', {
alias: 'pixelSize',
describe: 'Adjust blockiness by pixel size',
type: 'number',
default: 0,
coerce: (value) => {
if (value < 0) {
throw new Error('pixelSize should not be lower than 0');
}
return Math.round(value);
},
})
.option('d', {
alias: 'ditherAlgo',
describe: 'Dithering algorithm: floyd, bayer, atkinson or none',
type: 'string',
default: 'none',
choices: ['floyd', 'bayer', 'atkinson', 'none'],
})
.option('t', {
alias: 'alphaThreshold',
describe: 'Adjust transparent pixels to hide/show based on threshold',
type: 'number',
default: 0.6,
coerce: (value) => {
if (value < 0.000001 || value > 0.999999) {
throw new Error('alphaThreshold should be a range > 0 and < 1');
}
return value;
},
})
.option('c', {
alias: 'colorLimit',
describe: 'Limit number of colours',
type: 'number',
default: 8,
coerce: (value) => {
if (value < 2) {
throw new Error('colorLimit should not be lower than 2');
}
return Math.round(value);
},
})
.option('p', {
alias: 'palette',
describe: 'Use a predefined custom palette',
type: 'string',
choices: [
'APPLE_II_HI',
'GAMEBOY_ORIGINAL',
'GAMEBOY_COMBO_UP',
'GAMEBOY_COMBO_DOWN',
'GAMEBOY_COMBO_LEFT',
'GAMEBOY_COMBO_RIGHT',
'GAMEBOY_A_UP',
'GAMEBOY_A_DOWN',
'GAMEBOY_A_LEFT',
'GAMEBOY_A_RIGHT',
'GAMEBOY_B_UP',
'GAMEBOY_B_DOWN',
'GAMEBOY_B_LEFT',
'GAMEBOY_B_RIGHT',
'GAMEBOY_POCKET',
'GAMEBOY_VIRTUALBOY',
'TELETEXT',
'BBC_MICRO',
'CGA_MODE4_PAL1',
'CGA_MODE5_PAL1',
'CGA_MODE4_PAL2',
'ZX_SPECTRUM',
'APPLE_II_LO',
'COMMODORE_64',
'MICROSOFT_WINDOWS_16',
'MICROSOFT_WINDOWS_20',
'PICO_8',
'MSX',
'MONO_OBRADINN_IBM',
'MONO_OBRADINN_MAC',
'MONO_BJG',
'MONO_BW',
'MONO_PHOSPHOR_AMBER',
'MONO_PHOSPHOR_LTAMBER',
'MONO_PHOSPHOR_GREEN1',
'MONO_PHOSPHOR_GREEN2',
'MONO_PHOSPHOR_GREEN3',
'MONO_PHOSPHOR_APPLE',
'APPLE_II_MONO',
'MONO_PHOSPHOR_APPLEC',
'APPLE_II_MONOC',
'MICROSOFT_WINDOWS_PAINT',
'AMSTRAD_CPC',
'ATARI_2600',
'ATARI_5200',
'ATARI_7800',
'ATARI_LYNX',
],
})
.option('o', {
alias: 'customPalette',
describe: 'Custom palette in the format [[r, g, b], [r, g, b], ...]',
type: 'array',
coerce: (value) => {
// Convert the string to a proper array representation
const parsedArray = JSON.parse(`[${value.join('')}]`).pop();
// Validate the format and range of values
if (
Array.isArray(parsedArray) &&
parsedArray.every(
(color) =>
Array.isArray(color) &&
color.length === 3 &&
color.every(
(channel) =>
typeof channel === 'number' && channel >= 0 && channel <= 255,
),
)
) {
parsedArray.map((color) =>
color.map((channel) => Math.round(channel)),
);
return parsedArray.map((color) => ({
r: color[0],
g: color[1],
b: color[2],
}));
} else {
throw new Error(
'Invalid customPalette format. It should be an array of arrays, each containing three numbers in the range 0 to 255.',
);
}
},
})
.option('l', {
alias: 'lowPass',
describe: 'Use lowpass filter to reduce noise',
type: 'boolean',
default: false,
})
.option('u', {
alias: 'cubic',
describe:
'Uses bicubic interpolation instead of bilinear for initial resizing',
type: 'boolean',
default: true,
})
.option('r', {
alias: 'randomColor',
describe: 'Use randomness when reducing palette to colorLimit',
type: 'boolean',
default: true,
})
.option('n', {
alias: 'normalize',
describe: 'Normalize image colour for consistency',
type: 'boolean',
default: true,
})
.option('g', {
alias: 'grayScale',
describe: 'Transform image to grayscale',
type: 'boolean',
default: false,
})
.option('w', {
alias: 'width',
describe: 'Output image width',
type: 'number',
coerce: (value) => {
if (value !== undefined && value < 1) {
throw new Error('width should not be lower than 1');
}
return Math.round(value);
},
})
.option('h', {
alias: 'height',
describe: 'Output image height',
type: 'number',
coerce: (value) => {
if (value !== undefined && value < 1) {
throw new Error('height should not be lower than 1');
}
return Math.round(value);
},
})
.parse();
if (options.filename) {
// Process a single image
(0, processImage_1.processImage)(options);
} else if (options.folderPath) {
// Process all images in a folder, splitting the task into threads
let numCores = 1;
try {
numCores = os.cpus().length;
} catch (err) {
console.error(err);
}
numCores = Math.max(numCores - 1, 1); // Min 1
numCores = Math.min(numCores, 32); // Max 32
await (0, processPath_1.processPath)(options.folderPath, options, numCores);
} else {
console.error(
'Error: Requires either `filename` or `folderPath`. Run `px --help` for help.',
);
}
// console.timeEnd('Done in');
}
main();