aboutsummaryrefslogtreecommitdiff
path: root/engines/sword25/gfx/image/vectorimage.cpp
blob: 9e3e2fef71fb5abb62eb73d80563f4403c0d1591 (plain)
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

/*
 * This code is based on Broken Sword 2.5 engine
 *
 * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
 *
 * Licensed under GNU GPL v2
 *
 */

// -----------------------------------------------------------------------------
// Includes
// -----------------------------------------------------------------------------

#include "sword25/kernel/bs_stdint.h"
#include "sword25/gfx/image/vectorimage.h"
#include <vector>
#include <stdexcept>

#include "agg_bounding_rect.h"

using namespace std;

namespace Sword25 {

#define BS_LOG_PREFIX "VECTORIMAGE"


// -----------------------------------------------------------------------------
// SWF Datentypen
// -----------------------------------------------------------------------------

typedef uint8_t u8;
typedef uint32_t u32;
typedef uint16_t u16;
typedef int16_t s32;


// -----------------------------------------------------------------------------
// Bitstream Hilfsklasse
// -----------------------------------------------------------------------------
// Das Parsen von SWF-Dateien erfordert sowohl bitweises Auslesen als auch an
// Bytegrenzen ausgerichtetes Lesen.
// Diese Klasse ist speziell daf�r ausgestattet.
// -----------------------------------------------------------------------------

class BS_VectorImage::SWFBitStream {
public:
	SWFBitStream(const unsigned char *pData, unsigned int DataSize) :
		m_Pos(pData), m_End(pData + DataSize), m_WordMask(0)
	{}

	inline u32 GetBits(unsigned int BitCount) {
		if (BitCount == 0 || BitCount > 32) {
			error("SWFBitStream::GetBits() must read at least 1 and at most 32 bits at a time");
		}

		u32 value = 0;
		while (BitCount) {
			if (m_WordMask == 0) FlushByte();

			value <<= 1;
			value |= ((m_Word & m_WordMask) != 0) ? 1 : 0;
			m_WordMask >>= 1;

			--BitCount;
		}

		return value;
	}

	inline s32 GetSignedBits(unsigned int BitCount) {
		// Bits einlesen
		u32 Temp = GetBits(BitCount);

		// Falls das Sign-Bit gesetzt ist, den Rest des R�ckgabewertes mit 1-Bits auff�llen (Sign Extension)
		if (Temp & 1 << (BitCount - 1))
			return (0xffffffff << BitCount) | Temp;
		else
			return Temp;
	}

	inline u32 GetU32() {
		u32 Byte1 = GetU8();
		u32 Byte2 = GetU8();
		u32 Byte3 = GetU8();
		u32 Byte4 = GetU8();

		return Byte1 | (Byte2 << 8) | (Byte3 << 16) | (Byte4 << 24);
	}

	inline u16 GetU16() {
		u32 Byte1 = GetU8();
		u32 Byte2 = GetU8();

		return Byte1 | (Byte2 << 8);
	}

	inline u8 GetU8() {
		FlushByte();
		u8 Value = m_Word;
		m_WordMask = 0;
		FlushByte();

		return Value;
	}

	inline void FlushByte() {
		if (m_WordMask != 128) {
			if (m_Pos >= m_End) {
				error("Attempted to read past end of file");
			} else {
				m_Word = *m_Pos++;
				m_WordMask = 128;
			}
		}
	}

	inline void SkipBytes(unsigned int SkipLength) {
		FlushByte();
		if (m_Pos + SkipLength >= m_End) {
			error("Attempted to read past end of file");
		} else {
			m_Pos += SkipLength;
			m_Word = *(m_Pos - 1);
		}
	}

private:
	const unsigned char    *m_Pos;
	const unsigned char    *m_End;

	u8                      m_Word;
	unsigned int            m_WordMask;
};


// -----------------------------------------------------------------------------
// Konstanten und Hilfsfunktionen
// -----------------------------------------------------------------------------

namespace {
// -----------------------------------------------------------------------------
// Konstanten
// -----------------------------------------------------------------------------

const u32 MAX_ACCEPTED_FLASH_VERSION = 3;   // Die h�chste Flash-Dateiversion, die vom Lader akzeptiert wird


// -----------------------------------------------------------------------------
// Konvertiert SWF-Rechteckdaten in einem Bitstrom in BS_Rect-Objekte
// -----------------------------------------------------------------------------

BS_Rect FlashRectToBSRect(BS_VectorImage::SWFBitStream &bs) {
	bs.FlushByte();

	// Feststellen mit wie vielen Bits die einzelnen Komponenten kodiert sind
	u32 BitsPerValue = bs.GetBits(5);

	// Die einzelnen Komponenten einlesen
	s32 XMin = bs.GetSignedBits(BitsPerValue);
	s32 XMax = bs.GetSignedBits(BitsPerValue);
	s32 YMin = bs.GetSignedBits(BitsPerValue);
	s32 YMax = bs.GetSignedBits(BitsPerValue);

	return BS_Rect(XMin, YMin, XMax + 1, YMax + 1);
}


// -----------------------------------------------------------------------------
// Konvertiert SWF-Farben in AntiGrain Farben
// -----------------------------------------------------------------------------

agg::rgba8 FlashColorToAGGRGBA8(unsigned int FlashColor) {
	agg::rgba8 ResultColor((FlashColor >> 16) & 0xff, (FlashColor >> 8) & 0xff, FlashColor & 0xff, FlashColor >> 24);
	ResultColor.premultiply();
	return ResultColor;
}


// -----------------------------------------------------------------------------
// Berechnet die Bounding-Box eines BS_VectorImageElement
// -----------------------------------------------------------------------------

struct CBBGetId {
	CBBGetId(const BS_VectorImageElement &VectorImageElement_) : VectorImageElement(VectorImageElement_) {}
	unsigned operator [](unsigned i) const {
		return VectorImageElement.GetPathInfo(i).GetID();
	}
	const BS_VectorImageElement &VectorImageElement;
};

BS_Rect CalculateBoundingBox(const BS_VectorImageElement &VectorImageElement) {
	agg::path_storage Path = VectorImageElement.GetPaths();
	CBBGetId IdSource(VectorImageElement);

	double x1, x2, y1, y2;
	agg::bounding_rect(Path, IdSource, 0, VectorImageElement.GetPathCount(), &x1, &y1, &x2, &y2);

	return BS_Rect(static_cast<int>(x1), static_cast<int>(y1), static_cast<int>(x2) + 1, static_cast<int>(y2) + 1);
}
}


// -----------------------------------------------------------------------------
// Konstruktion
// -----------------------------------------------------------------------------

BS_VectorImage::BS_VectorImage(const unsigned char *pFileData, unsigned int FileSize, bool &Success) {
	Success = false;

	// Bitstream-Objekt erzeugen
	// Im Folgenden werden die Dateidaten aus diesem ausgelesen.
	SWFBitStream bs(pFileData, FileSize);

	// SWF-Signatur �berpr�fen
	u32 Signature[3];
	Signature[0] = bs.GetU8();
	Signature[1] = bs.GetU8();
	Signature[2] = bs.GetU8();
	if (Signature[0] != 'F' ||
		Signature[1] != 'W' ||
		Signature[2] != 'S') {
		BS_LOG_ERRORLN("File is not a valid SWF-file");
		return;
	}

	// Versionsangabe �berpr�fen
	u32 Version = bs.GetU8();
	if (Version > MAX_ACCEPTED_FLASH_VERSION) {
		BS_LOG_ERRORLN("File is of version %d. Highest accepted version is %d.", Version, MAX_ACCEPTED_FLASH_VERSION);
		return;
	}

	// Dateigr��e auslesen und mit der tats�chlichen Gr��e vergleichen
	u32 StoredFileSize = bs.GetU32();
	if (StoredFileSize != FileSize) {
		BS_LOG_ERRORLN("File is not a valid SWF-file");
		return;
	}

	// SWF-Ma�e auslesen
	BS_Rect MovieRect = FlashRectToBSRect(bs);

	// Framerate und Frameanzahl auslesen
	u32 FrameRate = bs.GetU16();
	u32 FrameCount = bs.GetU16();

	// Tags parsen
	// Da wir uns nur f�r das erste DefineShape-Tag interessieren
	bool KeepParsing = true;
	while (KeepParsing) {
		// Tags beginnen immer an Bytegrenzen
		bs.FlushByte();

		// Tagtyp und L�nge auslesen
		u16 TagTypeAndLength = bs.GetU16();
		u32 TagType = TagTypeAndLength >> 6;
		u32 TagLength = TagTypeAndLength & 0x3f;
		if (TagLength == 0x3f)
			TagLength = bs.GetU32();

		switch (TagType) {
		case 2:
			// DefineShape
			Success = ParseDefineShape(2, bs);
			return;
		case 22:
			// DefineShape2
			Success = ParseDefineShape(2, bs);
			return;
		case 32:
			Success = ParseDefineShape(3, bs);
			return;
		default:
			// Unbekannte Tags ignorieren
			bs.SkipBytes(TagLength);
		}
	}

	// Die Ausf�hrung darf nicht an dieser Stelle ankommen: Entweder es wird ein Shape gefunden, dann wird die Funktion mit vorher verlassen, oder
	// es wird keines gefunden, dann tritt eine Exception auf sobald �ber das Ende der Datei hinaus gelesen wird.
	BS_ASSERT(false);
}

// -----------------------------------------------------------------------------

bool BS_VectorImage::ParseDefineShape(unsigned int ShapeType, SWFBitStream &bs) {
	u32 ShapeID = bs.GetU16();

	// Bounding Box auslesen
	m_BoundingBox = FlashRectToBSRect(bs);

	// Erstes Image-Element erzeugen
	m_Elements.resize(1);

	// Styles einlesen
	unsigned int NumFillBits;
	unsigned int NumLineBits;
	if (!ParseStyles(ShapeType, bs, NumFillBits, NumLineBits))
		return false;

	unsigned int LineStyle = 0;
	unsigned int FillStyle0 = 0;
	unsigned int FillStyle1 = 0;

	// Shaperecord parsen
	// ------------------

	bool EndOfShapeDiscovered = false;
	while (!EndOfShapeDiscovered) {
		u32 TypeFlag = bs.GetBits(1);

		// Non-Edge Record
		if (TypeFlag == 0) {
			// Feststellen welche Parameter gesetzt werden
			u32 StateNewStyles = bs.GetBits(1);
			u32 StateLineStyle = bs.GetBits(1);
			u32 StateFillStyle1 = bs.GetBits(1);
			u32 StateFillStyle0 = bs.GetBits(1);
			u32 StateMoveTo = bs.GetBits(1);

			// End der Shape-Definition erreicht?
			if (!StateNewStyles && !StateLineStyle && !StateFillStyle0 && !StateFillStyle1 && !StateMoveTo)
				EndOfShapeDiscovered = true;
			// Parameter dekodieren
			else {
				s32 MoveDeltaX = 0;
				s32 MoveDeltaY = 0;
				if (StateMoveTo) {
					u32 MoveToBits = bs.GetBits(5);
					MoveDeltaX = bs.GetSignedBits(MoveToBits);
					MoveDeltaY = bs.GetSignedBits(MoveToBits);
				}

				if (StateFillStyle0) {
					if (NumFillBits > 0)
						FillStyle0 = bs.GetBits(NumFillBits);
					else
						FillStyle0 = 0;
				}

				if (StateFillStyle1) {
					if (NumFillBits > 0)
						FillStyle1 = bs.GetBits(NumFillBits);
					else
						FillStyle1 = 0;
				}

				if (StateLineStyle) {
					if (NumLineBits)
						LineStyle = bs.GetBits(NumLineBits);
					else
						NumLineBits = 0;
				}

				if (StateNewStyles) {
					// An dieser Stelle werden in Flash die alten Style-Definitionen verworfen und mit den neuen �berschrieben.
					// Es wird ein neues Element begonnen.
					m_Elements.resize(m_Elements.size() + 1);
					if (!ParseStyles(ShapeType, bs, NumFillBits, NumLineBits)) return false;
				}

				// Ein neuen Pfad erzeugen, es sei denn, es wurden nur neue Styles definiert
				if (StateLineStyle || StateFillStyle0 || StateFillStyle1 || StateMoveTo) {
					// Letzte Zeichenposition merken, beim Aufruf von start_new_path() wird die Zeichenpostionen auf 0, 0 zur�ckgesetzt
					double LastX = m_Elements.back().m_Paths.last_x();
					double LastY = m_Elements.back().m_Paths.last_y();

					// Neue Pfadinformation erzeugen
					m_Elements.back().m_PathInfos.push_back(BS_VectorPathInfo(m_Elements.back().m_Paths.start_new_path(), LineStyle, FillStyle0, FillStyle1));

					// Falls eine Bewegung definiert wurde, wird die Zeichenpositionen an die entsprechende Position gesetzt.
					// Ansonsten wird die Zeichenposition auf die letzte Zeichenposition gesetzt.
					if (StateMoveTo)
						m_Elements.back().m_Paths.move_to(MoveDeltaX, MoveDeltaY);
					else
						m_Elements.back().m_Paths.move_to(LastX, LastY);
				}
			}
		} else {
			// Edge Record
			u32 EdgeFlag = bs.GetBits(1);
			u32 NumBits = bs.GetBits(4) + 2;

			// Curved edge
			if (EdgeFlag == 0) {
				s32 ControlDeltaX = bs.GetSignedBits(NumBits);
				s32 ControlDeltaY = bs.GetSignedBits(NumBits);
				s32 AnchorDeltaX = bs.GetSignedBits(NumBits);
				s32 AnchorDeltaY = bs.GetSignedBits(NumBits);

				double ControlX = m_Elements.back().m_Paths.last_x() + ControlDeltaX;
				double ControlY = m_Elements.back().m_Paths.last_y() + ControlDeltaY;
				double AnchorX = ControlX + AnchorDeltaX;
				double AnchorY = ControlY + AnchorDeltaY;
				m_Elements.back().m_Paths.curve3(ControlX, ControlY, AnchorX, AnchorY);
			} else {
				// Staight edge
				s32 DeltaX = 0;
				s32 DeltaY = 0;

				u32 GeneralLineFlag = bs.GetBits(1);
				if (GeneralLineFlag) {
					DeltaX = bs.GetSignedBits(NumBits);
					DeltaY = bs.GetSignedBits(NumBits);
				} else {
					u32 VertLineFlag = bs.GetBits(1);
					if (VertLineFlag)
						DeltaY = bs.GetSignedBits(NumBits);
					else
						DeltaX = bs.GetSignedBits(NumBits);
				}

				m_Elements.back().m_Paths.line_rel(DeltaX, DeltaY);
			}
		}
	}

	// Bounding-Boxes der einzelnen Elemente berechnen
	vector<BS_VectorImageElement>::iterator it = m_Elements.begin();
	for (; it != m_Elements.end(); ++it) it->m_BoundingBox = CalculateBoundingBox(*it);

	return true;
}


// -----------------------------------------------------------------------------

bool BS_VectorImage::ParseStyles(unsigned int ShapeType, SWFBitStream &bs, unsigned int &NumFillBits, unsigned int &NumLineBits) {
	bs.FlushByte();

	// Fillstyles parsen
	// -----------------

	// Anzahl an Fillstyles bestimmen
	unsigned int FillStyleCount = bs.GetU8();
	if (FillStyleCount == 0xff) FillStyleCount = bs.GetU16();

	// Alle Fillstyles einlesen, falls ein Fillstyle mit Typ != 0 gefunden wird, wird das Parsen abgebrochen.
	// Es wird nur "solid fill" (Typ 0) unterst�tzt.
	m_Elements.back().m_FillStyles.reserve(FillStyleCount);
	for (unsigned int i = 0; i < FillStyleCount; ++i) {
		u8 Type = bs.GetU8();
		u32 Color;
		if (ShapeType == 3) {
			Color = (bs.GetU8() << 16) | (bs.GetU8() << 8) | bs.GetU8() | (bs.GetU8() << 24);
		} else
			Color = bs.GetBits(24) | (0xff << 24);
		if (Type != 0) return false;

		m_Elements.back().m_FillStyles.push_back(FlashColorToAGGRGBA8(Color));
	}

	// Linestyles parsen
	// -----------------

	// Anzahl an Linestyles bestimmen
	unsigned int LineStyleCount = bs.GetU8();
	if (LineStyleCount == 0xff) LineStyleCount = bs.GetU16();

	// Alle Linestyles einlesen
	m_Elements.back().m_LineStyles.reserve(LineStyleCount);
	for (unsigned int i = 0; i < LineStyleCount; ++i) {
		double Width = bs.GetU16();
		u32 Color;
		if (ShapeType == 3)
			Color = (bs.GetU8() << 16) | (bs.GetU8() << 8) | bs.GetU8() | (bs.GetU8() << 24);
		else
			Color = bs.GetBits(24) | (0xff << 24);

		m_Elements.back().m_LineStyles.push_back(BS_VectorImageElement::LineStyleType(Width, FlashColorToAGGRGBA8(Color)));
	}

	// Bitbreite f�r die folgenden Styleindizes auslesen
	NumFillBits = bs.GetBits(4);
	NumLineBits = bs.GetBits(4);

	return true;
}


// -----------------------------------------------------------------------------

bool BS_VectorImage::Fill(const BS_Rect *pFillRect, unsigned int Color) {
	BS_LOG_ERRORLN("Fill() is not supported.");
	return false;
}


// -----------------------------------------------------------------------------

unsigned int BS_VectorImage::GetPixel(int X, int Y) {
	BS_LOG_ERRORLN("GetPixel() is not supported. Returning black.");
	return 0;
}

// -----------------------------------------------------------------------------

bool BS_VectorImage::SetContent(const byte *Pixeldata, unsigned int Offset, unsigned int Stride) {
	BS_LOG_ERRORLN("SetContent() is not supported.");
	return 0;
}

} // End of namespace Sword25