-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathFilesProcessor.js
103 lines (87 loc) · 3.74 KB
/
FilesProcessor.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
let Jimp = require("jimp");
let PackProcessor = require("./PackProcessor");
let TextureRenderer = require("./utils/TextureRenderer");
let tinify = require("tinify");
let startExporter = require("./exporters/index").startExporter;
class FilesProcessor {
static start(images, options, callback, errorCallback) {
PackProcessor.pack(images, options,
(res) => {
let packResult = [];
let resFiles = [];
let readyParts = 0;
for(let data of res) {
new TextureRenderer(data, options, (renderResult) => {
packResult.push({
data: renderResult.data,
buffer: renderResult.buffer
});
if(packResult.length >= res.length) {
const suffix = options.suffix;
let ix = options.suffixInitialValue;
for(let item of packResult) {
let fName = options.textureName + (packResult.length > 1 ? suffix + ix : "");
FilesProcessor.processPackResultItem(fName, item, options, (files) => {
resFiles = resFiles.concat(files);
readyParts++;
if(readyParts >= packResult.length) {
callback(resFiles);
}
});
ix++;
}
}
});
}
},
(error) => {
if(errorCallback) errorCallback(error);
});
}
static processPackResultItem(fName, item, options, callback) {
let files = [];
let pixelFormat = options.textureFormat == "png" ? "RGBA8888" : "RGB888";
let mime = options.textureFormat == "png" ? Jimp.MIME_PNG : Jimp.MIME_JPEG;
item.buffer.getBuffer(mime, (err, srcBuffer) => {
FilesProcessor.tinifyImage(srcBuffer, options, (buffer) => {
let opts = {
imageName: fName + "." + options.textureFormat,
imageData: buffer.toString("base64"),
format: pixelFormat,
textureFormat: options.textureFormat,
imageWidth: item.buffer.bitmap.width,
imageHeight: item.buffer.bitmap.height,
removeFileExtension: options.removeFileExtension,
prependFolderName: options.prependFolderName,
base64Export: options.base64Export,
scale: options.scale,
appInfo: options.appInfo,
trimMode: options.trimMode
};
files.push({
name: fName + "." + options.exporter.fileExt,
buffer: Buffer.from(startExporter(options.exporter, item.data, opts))
});
if(!options.base64Export) {
files.push({
name: fName + "." + options.textureFormat,
buffer: buffer
});
}
callback(files);
});
});
}
static tinifyImage(buffer, options, callback) {
if(!options.tinify) {
callback(buffer);
return;
}
tinify.key = options.tinifyKey;
tinify.fromBuffer(buffer).toBuffer(function(err, result) {
if (err) throw err;
callback(result);
});
}
}
module.exports = FilesProcessor;