forked from MercuryRising/tagpro-map-editor
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgeneratepng.js
173 lines (143 loc) · 4.63 KB
/
generatepng.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*jslint bitwise:true, plusplus: true */
/*global Base64: false*/
/*https://github.com/wheany/js-png-encoder*/
(function (globalObj) {
'use strict';
// 0x78, 7 = 2^(7+8) = 32768 window size (CMF)
// 8 = deflate compression
// 0x01, (00 0 00001) (FLG)
// bits 0 to 4 FCHECK (check bits for CMF and FLG)
// bit 5 FDICT (preset dictionary)
// bits 6 to 7 FLEVEL (compression level)
var DEFLATE_METHOD = String.fromCharCode(0x78, 0x01),
CRC_TABLE = [],
SIGNATURE = String.fromCharCode(137, 80, 78, 71, 13, 10, 26, 10),
NO_FILTER = String.fromCharCode(0),
make_crc_table = function () {
var n, c, k;
for (n = 0; n < 256; n++) {
c = n;
for (k = 0; k < 8; k++) {
if (c & 1) {
c = 0xedb88320 ^ (c >>> 1);
} else {
c = c >>> 1;
}
}
CRC_TABLE[n] = c;
}
},
inflateStore = function (data) {
var MAX_STORE_LENGTH = 65535,
storeBuffer = '',
i,
remaining,
blockType;
for (i = 0; i < data.length; i += MAX_STORE_LENGTH) {
remaining = data.length - i;
blockType = '';
if (remaining <= MAX_STORE_LENGTH) {
blockType = String.fromCharCode(0x01);
} else {
remaining = MAX_STORE_LENGTH;
blockType = String.fromCharCode(0x00);
}
// little-endian
storeBuffer += blockType + String.fromCharCode((remaining & 0xFF), (remaining & 0xFF00) >>> 8);
storeBuffer += String.fromCharCode(((~remaining) & 0xFF), ((~remaining) & 0xFF00) >>> 8);
storeBuffer += data.substring(i, i + remaining);
}
return storeBuffer;
},
adler32 = function (data) {
var MOD_ADLER = 65521,
a = 1,
b = 0,
i;
for (i = 0; i < data.length; i++) {
a = (a + data.charCodeAt(i)) % MOD_ADLER;
b = (b + a) % MOD_ADLER;
}
return (b << 16) | a;
},
update_crc = function (crc, buf) {
var c = crc, n, b;
for (n = 0; n < buf.length; n++) {
b = buf.charCodeAt(n);
c = CRC_TABLE[(c ^ b) & 0xff] ^ (c >>> 8);
}
return c;
},
crc = function crc(buf) {
return update_crc(0xffffffff, buf) ^ 0xffffffff;
},
dwordAsString = function (dword) {
return String.fromCharCode((dword & 0xFF000000) >>> 24, (dword & 0x00FF0000) >>> 16, (dword & 0x0000FF00) >>> 8, (dword & 0x000000FF));
},
createChunk = function (length, type, data) {
var CRC = crc(type + data);
return dwordAsString(length) +
type +
data +
dwordAsString(CRC);
},
IEND,
createIHDR = function (width, height) {
var IHDRdata;
IHDRdata = dwordAsString(width);
IHDRdata += dwordAsString(height);
// bit depth
IHDRdata += String.fromCharCode(8);
// color type: 6=truecolor with alpha
IHDRdata += String.fromCharCode(3);
// compression method: 0=deflate, only allowed value
IHDRdata += String.fromCharCode(0);
// filtering: 0=adaptive, only allowed value
IHDRdata += String.fromCharCode(0);
// interlacing: 0=none
IHDRdata += String.fromCharCode(0);
return createChunk(13, 'IHDR', IHDRdata);
},
createPLTE = function(palette) {
var strsByIndex = [];
for (var color in palette) {
strsByIndex[palette[color]] = color.substring(0,3);
}
return createChunk(strsByIndex.length*3, 'PLTE', strsByIndex.join(''));
},
makePalette = function(rgba) {
var colorCount = 0;
var palette = {};
for (var i=0; i<rgba.length; i+=4) {
var color = rgba.substr(i, 4);
if (color in palette) continue;
palette[color] = colorCount;
colorCount++;
}
return palette;
},
png = function (width, height, rgba) {
var IHDR = createIHDR(width, height),
IDAT,
scanlines = '',
scanline,
y,
x,
compressedScanlines,
palette = makePalette(rgba),
PLTE = createPLTE(palette);
for (y = 0; y < rgba.length; y += width * 4) {
scanline = NO_FILTER;
for (x = 0; x < width; x++) {
scanline += String.fromCharCode(palette[rgba.substr(y+x*4, 4)]);
}
scanlines += scanline;
}
compressedScanlines = DEFLATE_METHOD + inflateStore(scanlines) + dwordAsString(adler32(scanlines));
IDAT = createChunk(compressedScanlines.length, 'IDAT', compressedScanlines);
return SIGNATURE + IHDR + PLTE + IDAT + IEND;
};
make_crc_table();
IEND = createChunk(0, 'IEND', '');
globalObj.generatePng = png;
}(this));