forked from Kode/khamake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageTool.js
110 lines (95 loc) · 3.39 KB
/
ImageTool.js
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
"use strict";
const cp = require('child_process');
const fs = require('fs');
const os = require('os');
const path = require('path');
const Paths = require('./Paths.js');
const Files = require('./Files.js');
const log = require('./log.js');
const exec = require('./exec.js');
function getWidthAndHeight(from, to, asset, format, prealpha, callback) {
const exe = 'kraffiti' + exec.sys();
let params = ['from=' + from, 'to=' + to, 'format=' + format, 'donothing'];
if (asset.scale !== undefined && asset.scale !== 1) {
params.push('scale=' + asset.scale);
}
let status = cp.spawnSync(path.join(__dirname, '..', '..', 'Kore', 'Tools', 'kraffiti', exe), params);
if (status.status !== 0) {
log.error('kraffiti process exited with code ' + status.status + ' when trying to get size of ' + asset.name);
return {w: 0, h: 0};
}
var lines = status.stdout.toString().split('\n');
for (var l in lines) {
var line = lines[l];
if (line.startsWith('#')) {
var numbers = line.substring(1).split('x');
return {w: parseInt(numbers[0]), h: parseInt(numbers[1])};
}
}
return {w: 0, h: 0};
}
module.exports = function (from, to, asset, format, prealpha, poweroftwo) {
if (format === undefined) {
if (from.toString().endsWith('.png')) format = 'png';
else format = 'jpg';
}
if (format === 'jpg' && (asset.scale === undefined || asset.scale === 1) && asset.background === undefined) {
to = to + '.jpg';
}
else if (format === 'pvr') {
to = to + '.pvr';
}
else {
format = 'png';
if (prealpha) to = to + '.kng';
else to = to + '.png';
}
let outputformat = format;
if (format === 'png' && prealpha) {
outputformat = 'kng';
}
if (fs.existsSync(to) && fs.statSync(to).mtime.getTime() > fs.statSync(from.toString()).mtime.getTime()) {
let wh = getWidthAndHeight(from, to, asset, format, prealpha);
asset.original_width = wh.w;
asset.original_height = wh.h;
return outputformat;
}
Files.createDirectories(Paths.get(path.dirname(to)));
if (format === 'jpg') {
Files.copy(Paths.get(from), Paths.get(to), true);
let wh = getWidthAndHeight(from, to, asset, format, prealpha);
asset.original_width = wh.w;
asset.original_height = wh.h;
return outputformat;
}
const exe = 'kraffiti' + exec.sys();
let params = ['from=' + from, 'to=' + to, 'format=' + format];
if (!poweroftwo) {
params.push('filter=nearest');
}
if (prealpha) params.push('prealpha');
if (asset.scale !== undefined && asset.scale !== 1) {
params.push('scale=' + asset.scale);
}
if (asset.background !== undefined) {
params.push('transparent=' + ((asset.background.red << 24) | (asset.background.green << 16) | (asset.background.blue << 8) | 0xff).toString(16));
}
if (poweroftwo) {
params.push('poweroftwo');
}
let status = cp.spawnSync(path.join(__dirname, '..', '..', 'Kore', 'Tools', 'kraffiti', exe), params);
if (status.status !== 0) {
log.error('kraffiti process exited with code ' + status.status + ' when trying to convert ' + asset.name);
return outputformat;
}
const lines = status.stdout.toString().split('\n');
for (let line of lines) {
if (line.startsWith('#')) {
var numbers = line.substring(1).split('x');
asset.original_width = parseInt(numbers[0]);
asset.original_height = parseInt(numbers[1]);
return outputformat;
}
}
return outputformat;
};