forked from hughsk/png-chunks-extract
/
index.js
293 lines (247 loc) · 8.15 KB
/
index.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/* adapted from https://github.com/hughsk/png-chunks-extract (MIT) and https://github.com/SheetJS/js-crc32 (Apache 2.0) (C) 2014-present SheetJS -- http://sheetjs.com */
window.rewrite_pHYs_chunk = function (data, ppmx, ppmy) {
/* crc32.js (C) 2014-present SheetJS -- http://sheetjs.com */
/* vim: set ts=2: */
/*exported CRC32 */
var CRC32;
(function (factory) {
/*jshint ignore:start */
if(typeof DO_NOT_EXPORT_CRC === 'undefined') {
if('object' === typeof exports) {
factory(exports);
} else if ('function' === typeof define && define.amd) {
define(function () {
var module = {};
factory(module);
return module;
});
} else {
factory(CRC32 = {});
}
} else {
factory(CRC32 = {});
}
/*jshint ignore:end */
}(function(CRC32) {
CRC32.version = '1.1.1';
/* see perf/crc32table.js */
/*global Int32Array */
function signed_crc_table() {
var c = 0, table = new Array(256);
for(var n =0; n != 256; ++n){
c = n;
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c&1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
table[n] = c;
}
return typeof Int32Array !== 'undefined' ? new Int32Array(table) : table;
}
var T = signed_crc_table();
function crc32_bstr(bstr, seed) {
var C = seed ^ -1, L = bstr.length - 1;
for(var i = 0; i < L;) {
C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF];
C = (C>>>8) ^ T[(C^bstr.charCodeAt(i++))&0xFF];
}
if(i === L) C = (C>>>8) ^ T[(C ^ bstr.charCodeAt(i))&0xFF];
return C ^ -1;
}
function crc32_buf(buf, seed) {
if(buf.length > 10000) return crc32_buf_8(buf, seed);
var C = seed ^ -1, L = buf.length - 3;
for(var i = 0; i < L;) {
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
}
while(i < L+3) C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
return C ^ -1;
}
function crc32_buf_8(buf, seed) {
var C = seed ^ -1, L = buf.length - 7;
for(var i = 0; i < L;) {
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
}
while(i < L+7) C = (C>>>8) ^ T[(C^buf[i++])&0xFF];
return C ^ -1;
}
function crc32_str(str, seed) {
var C = seed ^ -1;
for(var i = 0, L=str.length, c, d; i < L;) {
c = str.charCodeAt(i++);
if(c < 0x80) {
C = (C>>>8) ^ T[(C ^ c)&0xFF];
} else if(c < 0x800) {
C = (C>>>8) ^ T[(C ^ (192|((c>>6)&31)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
} else if(c >= 0xD800 && c < 0xE000) {
c = (c&1023)+64; d = str.charCodeAt(i++)&1023;
C = (C>>>8) ^ T[(C ^ (240|((c>>8)&7)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|((c>>2)&63)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|((d>>6)&15)|((c&3)<<4)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|(d&63)))&0xFF];
} else {
C = (C>>>8) ^ T[(C ^ (224|((c>>12)&15)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|((c>>6)&63)))&0xFF];
C = (C>>>8) ^ T[(C ^ (128|(c&63)))&0xFF];
}
}
return C ^ -1;
}
CRC32.table = T;
CRC32.bstr = crc32_bstr;
CRC32.buf = crc32_buf;
CRC32.str = crc32_str;
}));
// Used for fast-ish conversion between uint8s and uint32s/int32s.
// Also required in order to remain agnostic for both Node Buffers and
// Uint8Arrays.
var uint8 = new Uint8Array(4);
var int32 = new Int32Array(uint8.buffer);
var uint32 = new Uint32Array(uint8.buffer);
var pHYsFound = false;
if (data[0] !== 0x89 || data[1] !== 0x50 || data[2] !== 0x4E || data[3] !== 0x47 || data[4] !== 0x0D || data[5] !== 0x0A || data[6] !== 0x1A || data[7] !== 0x0A) {
throw new Error('Invalid .png file header: possibly caused by DOS-Unix line ending conversion?');
}
var ended = false
var idx = 8
while (idx < data.length) {
// Read the length of the current chunk,
// which is stored as a Uint32.
uint8[3] = data[idx++]
uint8[2] = data[idx++]
uint8[1] = data[idx++]
uint8[0] = data[idx++]
// Chunk includes name/type for CRC check (see below).
var length = uint32[0] + 4
var chunk = new Uint8Array(length)
chunk[0] = data[idx++]
chunk[1] = data[idx++]
chunk[2] = data[idx++]
chunk[3] = data[idx++]
// Get the name in ASCII for identification.
var name = (
String.fromCharCode(chunk[0]) +
String.fromCharCode(chunk[1]) +
String.fromCharCode(chunk[2]) +
String.fromCharCode(chunk[3])
);
console.log("chunk found " + name + ", length = " + (length-4));
var chunkDataStart = idx;
// Read the contents of the chunk out of the main buffer.
for (var i = 4; i < length; i++) {
chunk[i] = data[idx++];
}
var crcStart = idx;
// Read out the CRC value for comparison.
// It's stored as an Int32.
uint8[3] = data[idx++];
uint8[2] = data[idx++];
uint8[1] = data[idx++];
uint8[0] = data[idx++];
var crcActual = int32[0];
var crcExpect = CRC32.buf(chunk);
if (crcExpect !== crcActual) {
throw new Error(
'CRC values for ' + name + ' header do not match, PNG file is likely corrupted'
)
} else {
console.log("CRCs match! " + crcExpect + " " + crcActual + " " + chunk.length);
}
if (name == "IDAT") {
chunkDataStart = chunkDataStart - 8;
var len = data.length;
// create new array with pHYs chunk inserted
// 4+4+13
var data2 = new Uint8Array(len + 21);
// copy before IEND
for (var i = 0 ; i < chunkDataStart ; i++) {
data2[i] = data[i];
}
// copy IEND to end
for (var i = chunkDataStart ; i < len ; i++) {
data2[i+21] = data[i];
}
var phys = new Uint8Array(13);
var i = 0;
// length of pHYs chunk
int32[0] = 9;
data2[chunkDataStart++] = uint8[3];
data2[chunkDataStart++] = uint8[2];
data2[chunkDataStart++] = uint8[1];
data2[chunkDataStart++] = uint8[0];
// pHYs (chunk name)
phys[i++] = data2[chunkDataStart++] = 'p'.charCodeAt(0);
phys[i++] = data2[chunkDataStart++] = 'H'.charCodeAt(0);
phys[i++] = data2[chunkDataStart++] = 'Y'.charCodeAt(0);
phys[i++] = data2[chunkDataStart++] = 's'.charCodeAt(0);
// x
uint32[0] = ppmx;
phys[i++] = data2[chunkDataStart++] = uint8[3];
phys[i++] = data2[chunkDataStart++] = uint8[2];
phys[i++] = data2[chunkDataStart++] = uint8[1];
phys[i++] = data2[chunkDataStart++] = uint8[0];
// y
uint32[0] = ppmy;
phys[i++] = data2[chunkDataStart++] = uint8[3];
phys[i++] = data2[chunkDataStart++] = uint8[2];
phys[i++] = data2[chunkDataStart++] = uint8[1];
phys[i++] = data2[chunkDataStart++] = uint8[0];
// unit = meters
phys[i++] = data2[chunkDataStart++] = 1;
var physCRC = CRC32.buf(phys);
int32[0] = physCRC;
data2[chunkDataStart++] = uint8[3];
data2[chunkDataStart++] = uint8[2];
data2[chunkDataStart++] = uint8[1];
data2[chunkDataStart++] = uint8[0];
return data2;
}
if (name == "pHYs") {
console.log("pHYs chunk found, rewriting!!!!!!!!!!!!!");
var phys = new Uint8Array(13);
var i = 0;
// pHYs (chunk name)
phys[i++] = 'p'.charCodeAt(0);
phys[i++] = 'H'.charCodeAt(0);
phys[i++] = 'Y'.charCodeAt(0);
phys[i++] = 's'.charCodeAt(0);
// x
uint32[0] = ppmx;
phys[i++] = data[chunkDataStart++] = uint8[3];
phys[i++] = data[chunkDataStart++] = uint8[2];
phys[i++] = data[chunkDataStart++] = uint8[1];
phys[i++] = data[chunkDataStart++] = uint8[0];
// y
uint32[0] = ppmy;
phys[i++] = data[chunkDataStart++] = uint8[3];
phys[i++] = data[chunkDataStart++] = uint8[2];
phys[i++] = data[chunkDataStart++] = uint8[1];
phys[i++] = data[chunkDataStart++] = uint8[0];
// unit = meters
phys[i++] = data[chunkDataStart++] = 1;
var physCRC = CRC32.buf(phys);
int32[0] = physCRC;
data[crcStart++] = uint8[3];
data[crcStart++] = uint8[2];
data[crcStart++] = uint8[1];
data[crcStart++] = uint8[0];
return data;
}
}
throw new Error('.png file ended prematurely: no IEND or pHYs header was found');
}