-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
77 lines (62 loc) · 1.55 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
const zlib = require('zlib');
const Aseprite = require('./aseprite');
const KaitaiStream = require('kaitai-struct/KaitaiStream');
const ChunkTypeEnum = Aseprite.Frame.Chunk.ChunkTypeEnum;
const CelTypeEnum = Aseprite.Frame.Chunk.CelChunk.CelTypeEnum;
const cleanProps = [
'_io',
'_parent',
'_root',
'_dataView',
'_byteLength'
];
function clean(object) {
if (typeof object === 'object' && object !== null) {
if (Array.isArray(object)) {
for (const v of object) {
clean(v);
}
} else {
for (const prop of cleanProps) {
delete object[prop];
}
for (const key of Object.keys(object)) {
clean(object[key]);
}
}
}
return object;
}
function inflate(ase) {
// Iterate all frames, searching for cel chunks with compressed pixel data.
for (const frame of ase.frames) {
for (const chunk of frame.chunks) {
if (chunk.type === ChunkTypeEnum.CEL) {
const cel = chunk.data;
if (cel.type === CelTypeEnum.COMPRESSED) {
cel.pixels = zlib.inflateSync(cel.pixelsCompressed);
}
}
}
}
return ase;
}
function parse(content, options) {
options = options || {};
options.clean = options.clean === undefined || options.clean;
options.inflate = options.inflate === undefined || options.inflate;
let ase = new Aseprite(new KaitaiStream(content));
if (options.clean) {
ase = clean(ase);
}
if (options.inflate) {
ase = inflate(ase);
}
return ase;
}
exports.parse = parse;
exports.clean = clean;
exports.inflate = inflate;
exports.Frame = Aseprite.Frame;
exports.Header = Aseprite.Header;
exports.default = exports;