-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.js
118 lines (107 loc) · 3.28 KB
/
image.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
const jimp = require("jimp");
const th = 39; // tile height
const tw = 27; // tile width
const bw = 5; // border width
const base_path = "./tiles/";
function calc_width(sets) {
var res = bw;
for (i in sets) {
const set = sets[i];
for (j in set) {
const tile = set[j];
res += (tile.length == 3) ? th : tw;
}
res += bw;
}
return res;
}
function tiles_to_sets(tiles) {
var sets = [];
for (var i = 0; i < tiles.length; ++i) {
set = tiles[i];
const outer_reg = /((?:[0-9]\.?)+)([pmsz])/g;
var part;
var temp = [];
while ((part = outer_reg.exec(set)) !== null) {
const suit = part[2];
const nums = part[1];
const reg = /[0-9]\.?/g;
var result;
while ((result = reg.exec(nums)) !== null) {
temp.push(result + suit);
}
}
sets.push(temp);
}
console.log("sets: " + sets);
return sets;
}
/*
async function render(tiles) {
console.log("rendering: " + tiles);
const sets = tiles_to_sets(tiles);
const w = calc_width(sets);
const h = th;
var base = await new jimp(w, h, function(err, base) {
var x = bw;
for (var i = 0; i < sets.length; ++i) {
const set = sets[i];
for (var j = 0; j < set.length; ++j) {
const tile = set[j];
if (tile.length == 3) { // sideways
const path = base_path + tile[0] + tile[2] + ".png";
jimp.read(path).then(function (image) {
image.rotate(90);
base.blit(image, xx, th-tw);
});
x += th;
} else {
const path = base_path + tile + ".png";
jimp.read(path).then(function (image) {
base.blit(image, xx, 0);
});
x += tw;
}
}
x += bw;
}
});
return base;
}
*/
function render(tiles) {
console.log("rendering: " + tiles);
const sets = tiles_to_sets(tiles);
const w = calc_width(sets);
const h = th;
var base = new jimp(w, h, function(err, base) {
var x = bw;
for (var i = 0; i < sets.length; ++i) {
const set = sets[i];
for (var j = 0; j < set.length; ++j) {
const tile = set[j];
if (tile.length == 3) { // sideways
const path = base_path + tile[0] + tile[2] + ".png";
(function(xx){
jimp.read(path).then(function (image) {
image.rotate(90);
base.blit(image, xx, th-tw);
});
})(x);
x += th;
} else {
const path = base_path + tile + ".png";
(function(xx){
jimp.read(path).then(function (image) {
base.blit(image, xx, 0);
});
})(x);
x += tw;
}
}
x += bw;
}
});
return base;
}
module.exports.render = render;