-
Notifications
You must be signed in to change notification settings - Fork 5
/
png.js
52 lines (41 loc) 路 1.11 KB
/
png.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
exports.isPNG = function (buffer) {
var header = buffer.slice(0, 8).toString('hex')
return (header === '89504e470d0a1a0a') // \211 P N G \r \n \032 'n
}
exports.isAnimated = function (buffer) {
var hasACTL = false
var hasIDAT = false
var hasFDAT = false
var previousChunkType = null
var offset = 8
while (offset < buffer.length) {
var chunkLength = buffer.readUInt32BE(offset)
var chunkType = buffer.slice(offset + 4, offset + 8).toString('ascii')
switch (chunkType) {
case 'acTL':
hasACTL = true
break
case 'IDAT':
if (!hasACTL) {
return false
}
if (previousChunkType !== 'fcTL' && previousChunkType !== 'IDAT') {
return false
}
hasIDAT = true
break
case 'fdAT':
if (!hasIDAT) {
return false
}
if (previousChunkType !== 'fcTL' && previousChunkType !== 'fdAT') {
return false
}
hasFDAT = true
break
}
previousChunkType = chunkType
offset += 4 + 4 + chunkLength + 4
}
return (hasACTL && hasIDAT && hasFDAT)
}