/
Portrait.ts
150 lines (134 loc) · 4.9 KB
/
Portrait.ts
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
/*
* Copyright (C) 2016 Klaus Reimer <k@ailis.de>
* See LICENSE.md for licensing information.
*/
import { PicImage } from "../image/PicImage";
import { BinaryReader } from "../io/BinaryReader";
import { decodeVxorInplace } from "../io/vxor";
import { decodeHuffman } from "../io/huffman";
import { PortraitUpdate } from "./PortraitUpdate";
import { PortraitScript } from "./PortraitScript";
import { Animation } from "../image/Animation";
import { BaseImage } from "../image/BaseImage";
import { PortraitPlayer } from "./PortraitPlayer";
/**
* An animated portrait image. Contains the base frame image but also provides methods to access the animation
* information. To simply play the animation it is recommended to use the [[PortraitPlayer]] class.
*/
export class Portrait extends PicImage implements Animation {
/** The animation scripts. */
private scripts: PortraitScript[];
/** The animation updates. */
private updates: PortraitUpdate[];
/**
* Creates a new portrait with the given base frame, animation scripts and animation updates.
*
* @param baseFrame The image data of the base frame.
* @param scripts The animation scripts.
* @param updates The animation updates.
*/
private constructor(baseFrame: Uint8Array, scripts: PortraitScript[], updates: PortraitUpdate[]) {
super(baseFrame, 96, 84);
this.scripts = scripts;
this.updates = updates;
}
/**
* Returns the animation scripts.
*
* @return The animation scripts.
*/
public getScripts(): PortraitScript[] {
return this.scripts.slice();
}
/**
* Returns the animation script with the given index.
*
* @param index Animation script index.
* @return The animation script.
*/
public getScript(index: number): PortraitScript {
if (index < 0 || index >= this.scripts.length) {
throw new Error("Index out of bounds: " + index);
}
return this.scripts[index];
}
/**
* Returns the number of animation scripts.
*
* @return The number of animation scripts.
*/
public getNumScripts(): number {
return this.scripts.length;
}
/**
* Returns the animation updates.
*
* @return The animation updates.
*/
public getUpdates(): PortraitUpdate[] {
return this.updates.slice();
}
/**
* Returns the animation update with the given index.
*
* @param index Animation update index.
* @return The animation update.
*/
public getUpdate(index: number): PortraitUpdate {
if (index < 0 || index >= this.updates.length) {
throw new Error("Index out of bounds: " + index);
}
return this.updates[index];
}
/**
* Returns the number of animation updates.
*
* @return The number of animation updates.
*/
public getNumUpdates(): number {
return this.updates.length;
}
public createPlayer(onDraw: (frame: BaseImage) => void): PortraitPlayer {
return new PortraitPlayer(this, onDraw);
}
/**
* Reads a portrait from the given reader and returns it.
*
* @param reader The reader to read the two encoded MSQ blocks with the base frame and animation data from.
* @return The parsed portrait.
*/
public static read(reader: BinaryReader): Portrait {
// Parse base frame from first MSQ block
const imageSize = reader.readUint32();
const imageMsq = reader.readString(3);
const imageDisk = reader.readUint8();
if (imageMsq !== "msq" || (imageDisk !== 0 && imageDisk !== 1)) {
throw new Error("Invalid base frame data block");
}
const baseFrame = decodeVxorInplace(decodeHuffman(reader, imageSize), 48);
// Parse animation frames from second MSQ block
const animSize = reader.readUint32();
const animMsq = reader.readString(3);
const animDisk = reader.readUint8();
if (animMsq !== "msq" || animDisk !== 0) {
throw new Error("Invalid animation data block: " + animMsq + animDisk + imageDisk);
}
const animData = decodeHuffman(reader, animSize);
const animReader = new BinaryReader(animData);
// Parse animation scripts
const scriptsSize = animReader.readUint16();
const scripts: PortraitScript[] = [];
while (animReader.getByteIndex() - 2 < scriptsSize) {
scripts.push(PortraitScript.read(animReader));
}
// Parse animation update blocks
const updatesSize = animReader.readUint16();
const startIndex = animReader.getByteIndex();
const updates: PortraitUpdate[] = [];
while (animReader.getByteIndex() - startIndex < updatesSize) {
updates.push(PortraitUpdate.read(animReader));
}
// Create the end animation
return new Portrait(baseFrame, scripts, updates);
}
}