-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpng2code.js
executable file
·157 lines (118 loc) · 3.46 KB
/
png2code.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env node
const fs = require('fs');
const PNG = require('pngjs').PNG;
const inputFile = process.argv[2];
const inputData = fs.readFileSync(inputFile);
const png = PNG.sync.read(inputData);
const colorKeys = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split('');
const colors = {};
const toHex = (value) => {
const hex = value.toString(16);
return hex.length > 1 ? hex : `0${hex}`;
}
const toHexColor = (r, g, b) => `${toHex(r)}${toHex(g)}${toHex(b)}`;
const pixels = [];
for (let y = 0; y < png.height; y += 1) {
for (let x = 0; x < png.width; x += 1) {
const i = (png.width * y + x) * 4;
const r = png.data[i + 0];
const g = png.data[i + 1];
const b = png.data[i + 2];
const color = toHexColor(r, g, b);
let colorIndex = colors[color];
if (!colorIndex) {
const usedKeys = Object.values(colors);
colorIndex = colorKeys.find((key) => !usedKeys.includes(key));
colors[color] = colorIndex;
}
pixels.push(colorIndex);
}
}
const rleAppend = (char, count) => {
const value = count > 1 ? count.toString() : '';
return `${value}${char}`;
};
const rleEncode = (data) => {
let count = 0;
let char = '';
let result = '';
data.forEach((letter) => {
count += 1;
if (letter === char) {
return;
}
if (char !== '') {
result += rleAppend(char, count);
}
char = letter;
count = 0;
});
result += rleAppend(char, count + 1);
return result;
};
const data = [];
data.push(png.width);
data.push(png.height);
data.push(Object.keys(colors).map((color) => `${colors[color]}${color}`).join(''));
data.push(rleEncode(pixels));
const code = [];
code.push(`const image = '${data.join(',')}';`)
code.push(`
const rleDecode = (data) => {
let count = '';
let result = '';
data.forEach((char) => {
if (char.match(/\\d/)) {
count += char;
return;
}
count = count ? parseInt(count, 10) : 1;
for (let i = 0; i < count; i += 1) {
result += char;
}
count = '';
});
return result;
};
`);
code.push(`
const svgDecode = (imageData) => {
let [width, height, colorData, pixels] = imageData.split(',');
const colors = {};
colorData.match(/.{1,7}/g).forEach((color) => {
const key = color.substr(0, 1);
const value = color.substr(1);
colors[key] = value;
});
pixels = rleDecode(pixels.split(''));
pixels = pixels.split('');
let svg = [];
// svg.push('<?xml version="1.0" standalone="no"?>');
svg.push(\`<svg width="\${width}" height="\${height}" xmlns="http://www.w3.org/2000/svg" version="1.1">\`);
let pixelIndex = 0;
for (let y = 0; y < height; y += 1) {
for (let x = 0; x < width; x += 1) {
const colorIndex = pixels[pixelIndex];
const fill = colors[colorIndex];
const opacity = fill !== '000000' ? 1 : 0;
svg.push(\`<rect x="\${x}" y="\${y}" width="1" height="1" fill="#\${fill}" fill-opacity="\${opacity}" stroke="none"/>\`);
pixelIndex += 1;
}
}
svg.push('</svg>');
return svg.join('');
};
`);
code.push(`
const svg = btoa(svgDecode(image));
const styles = \`.tile { background-image: url('data:image/svg+xml;base64,\${svg}'); }\`;
const css = document.createElement('style');
css.type = 'text/css';
css.appendChild(document.createTextNode(styles));
document.getElementsByTagName('head')[0].appendChild(css);
`);
const script = [];
script.push('<script type="text/javascript">');
script.push(code.join("\n"));
script.push('</script>');
console.log(script.join("\n"));