This repository has been archived by the owner on Mar 10, 2024. It is now read-only.
/
buildGui.js
140 lines (128 loc) · 4.03 KB
/
buildGui.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
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
var Jimp = require("jimp")
, Promise = require('bluebird')
, fs = Promise.promisifyAll(require("fs"))
, _ = require('underscore')
, _inJobs = 0;
function toHex(i) {
return "0x" + i.toString(16).toUpperCase();
}
function RGB565(color) {
var r = color >> 24 & 0xFF;
var g = color >> 16 & 0xFF;
var b = color >> 8 & 0xFF;
var a = color & 0xFF;
var rgb565 = ((r & 0xF8) << 8) | ((g & 0xFC) << 3) | (b >> 3);
return rgb565;
}
var imgToBuf = function(img) {
return new Promise(function(resolve, reject) {
Jimp.read("./gui/png/"+img).then(function (image) {
var fbuf = new Buffer(0);
image.dither565();
for (var x=0;x<image.bitmap.width;x++) {
for (var y=0;y<image.bitmap.height;y++) {
var b = image.getPixelColor(x,y);
var rgba = Jimp.intToRGBA(b);
var _p = rgba.r << 8 | rgba.g << 3 | rgba.b >> 3;
var buf = new Buffer(2);
buf.writeUInt16LE(_p);
fbuf = Buffer.concat([fbuf, buf])
}
}
resolve([fbuf, image.bitmap.width, image.bitmap.height]);
});
});
}
var cImgToBuf = function(img) {
return new Promise(function(resolve, reject) {
var lastColor = 0;
var colorCounter = 0;
console.log(img);
Jimp.read("./gui/png/"+img).then(function (image) {
var fbuf = new Buffer(0);
for (var x=0;x<image.bitmap.width;x++) {
for (var y=0;y<image.bitmap.height;y++) {
//Read 32-bit color value from image
var b = image.getPixelColor(x,y);
//Compress to RGB565 format
b = RGB565(b);
if (colorCounter == 0) {
lastColor = b;
colorCounter=1;
} else {
//Write to buffer if color is different or colorCounter will overflow (as it's only one byte we can only store up to 255 chunks of data)
if (b != lastColor || colorCounter >= 255) {
console.log(x + "," + y + ":" + toHex(b) + ":" + toHex(lastColor) + ":" + colorCounter);
//We finished this chunk of data, write to file
var buf = new Buffer(3);
buf.writeUInt8(colorCounter,0);
buf.writeUInt16LE(lastColor,1);
console.log(buf);
fbuf = Buffer.concat([fbuf, buf])
colorCounter = 1;
lastColor = b;
} else {
colorCounter++;
}
}
}
}
//Make sure we also store the last chunk of data
if (colorCounter > 0) {
var buf = new Buffer(3);
buf.writeUInt8(colorCounter,0);
buf.writeUInt16LE(lastColor,1);
fbuf = Buffer.concat([fbuf, buf])
}
console.info("RESOLVING ", fbuf.length)
resolve(fbuf);
});
});
}
var promises = []
, pngs = [];
fs.openAsync('./gui/ui', 'w')
fs.openAsync('./gui/ui.min', 'w')
.then(fs.openAsync('./gui/struct.h', 'w'))
.then(fs.readdirSync('./gui/png').forEach(function(file) {
if (file[0] != ".")
//if (pngs.length < 1)
pngs.push(file);
}))
.then(function() {
// reduce here
return Promise.reduce(pngs, function(offset, png) {
return createCImg(png);
}, 0);
})
.then(function() {
// reduce here
return Promise.reduce(pngs, function(offset, png) {
return createImg(png, offset);
}, 0);
})
var createCImg = function(imgPath) {
return new Promise(function(resolve, reject) {
var ps = cImgToBuf(imgPath).then(function(ibuf) {
console.info(ibuf.length)
fs.appendFileAsync('./gui/ui.min', ibuf)
resolve(ibuf.length);
})
})
}
var createImg = function(imgPath, offset) {
return new Promise(function(resolve, reject) {
var ps = imgToBuf(imgPath).spread(function(ibuf, w, h) {
fs.appendFileAsync('./gui/ui', ibuf)
.then(function() {
var name = imgPath.split(".")[0];
fs.appendFileAsync('./gui/struct.h', 'UIBitmap ' + name + ' = {' + (offset) + ',' + ibuf.length + ',' + w + ',' + h + '};\n')
.then(function() {
setTimeout(function() {
resolve(offset + ibuf.length);
}, 100);
})
})
})
})
}