File: convert_shape.cpp

package info (click to toggle)
scummvm 2.9.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 450,580 kB
  • sloc: cpp: 4,299,825; asm: 28,322; python: 12,901; sh: 11,302; java: 9,289; xml: 7,895; perl: 2,639; ansic: 2,465; yacc: 1,670; javascript: 1,020; makefile: 933; lex: 578; awk: 275; objc: 82; sed: 11; php: 1
file content (752 lines) | stat: -rw-r--r-- 20,705 bytes parent folder | download | duplicates (2)
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
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
/* 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 3 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, see <http://www.gnu.org/licenses/>.
 *
 */

#include "common/memstream.h"
#include "ultima/ultima.h"
#include "ultima/ultima8/convert/convert_shape.h"
#include "ultima/ultima8/misc/stream_util.h"
#include "ultima/ultima8/misc/debugger.h"

namespace Ultima {
namespace Ultima8 {

void ConvertShapeFrame::Free() {
	delete [] _line_offsets;
	_line_offsets = nullptr;

	delete [] _rle_data;
	_rle_data = nullptr;
}

ConvertShape::ConvertShape() : _num_frames(0), _frames(nullptr) {
}

void ConvertShape::Free() {
	if (_frames)
		for(uint32 i = 0; i < _num_frames; ++i)
			_frames[i].Free();

	delete [] _frames;
	_frames = nullptr;
	_num_frames = 0;
}

void ConvertShape::Read(Common::SeekableReadStream &source, const ConvertShapeFormat *csf, uint32 real_len) {
	// Just to be safe
	uint32 start_pos = source.pos();

	// Read the ident
	if (csf->_bytes_ident) {
		char ident[4];
		source.read(ident, csf->_bytes_ident);

		if (memcmp(ident, csf->_ident, csf->_bytes_ident)) {
			warning("Corrupt shape");
			return;
		}
	}

	// Read special buffer
	uint8 special[256];
	if (csf->_bytes_special) {
		memset(special, 0, 256);
		for (uint32 i = 0; i < csf->_bytes_special; i++) special[source.readByte()&0xFF] = i + 2;
	}

	// Read the header unknown
	if (csf->_bytes_header_unk) source.read(_header_unknown, csf->_bytes_header_unk);

	// Now read _num_frames
	_num_frames = 1;
	if (csf->_bytes_num_frames) _num_frames = readX(source, csf->_bytes_num_frames);
	if (_num_frames == 0) _num_frames = CalcNumFrames(source,csf,real_len,start_pos);

//	if (_num_frames == 0xFFFF || _num_frames == 0xFFFFFF || _num_frames == -1)
//	{
//		warning("Corrupt shape?);
//		_num_frames = 0;
//		_frames = 0;
//		return;
//	}

	// Create _frames array
	_frames = new ConvertShapeFrame[_num_frames]();

	// Now read the _frames
	for(uint32 f = 0; f < _num_frames; ++f) {
		ConvertShapeFrame *frame = _frames + f;

		// Seek to initial pos
		source.seek(start_pos + csf->_len_header + (csf->_len_frameheader * f));

		// Read the offset
		uint32 frame_offset = csf->_len_header + (csf->_len_frameheader * f);
		if (csf->_bytes_frame_offset) frame_offset = readX(source, csf->_bytes_frame_offset);

		// Read the unknown
		if (csf->_bytes_frameheader_unk) source.read(frame->_header_unknown, csf->_bytes_frameheader_unk);

		// Read frame_length
		uint32 frame_length = real_len-frame_offset;
		if (csf->_bytes_frame_length) frame_length = readX(source, csf->_bytes_frame_length) + csf->_bytes_frame_length_kludge;

		// Seek to start of frame
		source.seek(start_pos + frame_offset + csf->_bytes_special);

		if (csf->_bytes_special)
			frame->ReadCmpFrame(source, csf, special, f > 0 ? _frames + f - 1 : 0);
		else
			frame->Read(source, csf, frame_length);
	}
}

void ConvertShapeFrame::Read(Common::SeekableReadStream &source, const ConvertShapeFormat *csf, uint32 frame_length) {
	// Read unknown
	if (csf->_bytes_frame_unknown) source.read(_unknown, csf->_bytes_frame_unknown);

	// Frame details
	_compression = readX(source, csf->_bytes_frame_compression);
	_width = readXS(source, csf->_bytes_frame_width);
	_height = readXS(source, csf->_bytes_frame_height);
	_xoff = readXS(source, csf->_bytes_frame_xoff);
	_yoff = readXS(source, csf->_bytes_frame_yoff);

	if (_compression != 0 && _compression != 1) {
		_compression = 0;
		_width = 0;
		_height = 0;
		_xoff = 0;
		_yoff = 0;
		warning("Corrupt frame?");
	}

	if (_height) {
		// Line offsets
		_line_offsets = new uint32 [_height];

		for (int32 i = 0; i < _height; ++i)  {
			_line_offsets[i] = readX(source, csf->_bytes_line_offset);

			// Now fudge with the value and turn it into an offset into the rle data
			// If required
			if (!csf->_line_offset_absolute)
				_line_offsets[i] -= (_height - i) * csf->_bytes_line_offset;
		}

		// Calculate the number of bytes of RLE data
		_bytes_rle = frame_length - (csf->_len_frameheader2 + (_height * csf->_bytes_line_offset));

		if (_bytes_rle < 0) {
			_bytes_rle = 0;
			warning("Corrupt frame?");
		}
	} else
		_line_offsets = nullptr;

	// Read the RLE Data
	if (_bytes_rle) {
		_rle_data = new uint8[_bytes_rle];
		source.read(_rle_data, _bytes_rle);
	} else
		_rle_data = nullptr;
}

void ConvertShapeFrame::ReadCmpFrame(Common::SeekableReadStream &source, const ConvertShapeFormat *csf, const uint8 special[256], ConvertShapeFrame *prev) {
	Common::MemoryWriteStreamDynamic rlebuf(DisposeAfterUse::YES);
	uint8 outbuf[512];

	// Read unknown
	if (csf->_bytes_frame_unknown) source.read(_unknown, csf->_bytes_frame_unknown);

	// Frame details
	_compression = readX(source, csf->_bytes_frame_compression);
	_width = readXS(source, csf->_bytes_frame_width);
	_height = readXS(source, csf->_bytes_frame_height);
	_xoff = readXS(source, csf->_bytes_frame_xoff);
	_yoff = readXS(source, csf->_bytes_frame_yoff);

	_line_offsets = new uint32 [_height];

	for(int32 y = 0; y < _height; ++y) {
		_line_offsets[y] = rlebuf.pos();

		int32 xpos = 0;

		do {
			uint8 skip = source.readByte();
			xpos += skip;

			if (xpos > _width) {
				source.seek(-1, SEEK_CUR);
				skip = _width-(xpos-skip);
			}

			rlebuf.writeByte(skip);

			if (xpos >= _width) break;

			uint32 dlen = source.readByte();
			uint8 *o = outbuf;

			// Is this required???? It seems hacky and pointless
			if (dlen == 0 || dlen == 1) {
				source.seek(-1, SEEK_CUR);
				rlebuf.seek(-1, SEEK_CUR);
				rlebuf.writeByte(skip + (_width - xpos));
				break;
			}

			int type = 0;

			if (_compression)  {
				type = dlen & 1;
				dlen >>= 1;
			}

			if (!type) {

				uint32 extra = 0;

				for (uint32 j = 0; j < dlen; j++) {

					uint8 c = source.readByte();

					if (special[c] && prev) {
						int32 count = special[c];
						prev->GetPixels(o, count, xpos - _xoff, y - _yoff);
						o+=count;
						extra += count - 1;
						xpos += count;
					} else if (c == 0xFF && prev) {
						int32 count = source.readByte();
						prev->GetPixels(o, count, xpos - _xoff, y - _yoff);
						o+=count;
						extra += count - 2;
						xpos += count;
						j++;
					} else {
						*o++ = c;
						xpos++;
					}
				}

				if (((dlen+extra) << _compression) > 255) {
					warning("Corrupt Frame. RLE dlen too large");
				}

				rlebuf.writeByte((dlen+extra) << _compression);
				rlebuf.write(outbuf,dlen+extra);
			} else {
				rlebuf.writeByte((dlen << 1) | 1);
				rlebuf.writeByte(source.readByte());
				xpos+=dlen;
			}

		} while (xpos < _width);
	}

	_bytes_rle = rlebuf.pos();
	_rle_data = new uint8[_bytes_rle];
	memcpy(_rle_data, rlebuf.getData(), _bytes_rle);
}

void ConvertShapeFrame::GetPixels(uint8 *buf, int32 count, int32 x, int32 y) {
	x += _xoff;
	y += _yoff;

	if (y > _height) return;

	int32 xpos = 0;
	const uint8 * linedata = _rle_data + _line_offsets[y];

	do {
		xpos += *linedata++;

		if (xpos == _width) break;

		int32 dlen = *linedata++;
		int type = 0;

		if (_compression) {
			type = dlen & 1;
			dlen >>= 1;
		}

		if (x >= xpos && x < (xpos+dlen)) {
			int diff = x-xpos;
			dlen-=diff;
			xpos = x;

			int num = count;
			if (dlen < count) num = dlen;

			if (!type) {
				const uint8 *l = (linedata += diff);

				while (num--) {
					*buf++ = *l++;
					count--;
					x++;
				}
			} else {
				uint8 l = *linedata;

				while (num--) {
					*buf++ = l;
					count--;
					x++;
				}
			}

			if (count == 0) return;
		}

		if (!type) linedata+=dlen;
		else linedata++;

		xpos += dlen;

	} while (xpos < _width);
}

int ConvertShape::CalcNumFrames(Common::SeekableReadStream &source, const ConvertShapeFormat *csf, uint32 real_len, uint32 start_pos) {
	int f = 0;
	uint32 first_offset = 0xFFFFFFFF;

	uint32 save_pos = source.pos();

	for (f = 0;; f++) {

		// Seek to initial pos
		source.seek(start_pos + csf->_len_header + (csf->_len_frameheader * f));

		if ((source.pos()-start_pos) >= first_offset) break;

		// Read the offset
		uint32 frame_offset = csf->_len_header + (csf->_len_frameheader * f);
		if (csf->_bytes_frame_offset) frame_offset = readX(source, csf->_bytes_frame_offset) + csf->_bytes_special;

		if (frame_offset < first_offset) first_offset = frame_offset;

		// Read the unknown
		if (csf->_bytes_frameheader_unk) source.skip(csf->_bytes_frameheader_unk);

		// Read frame_length
		uint32 frame_length = real_len-frame_offset;
		if (csf->_bytes_frame_length)
			frame_length = readX(source, csf->_bytes_frame_length) + csf->_bytes_frame_length_kludge;
		debugC(kDebugGraphics, "Frame %d length = %xh", f, frame_length);
	}

	source.seek(save_pos);

	return f;
}

bool ConvertShape::Check(Common::SeekableReadStream &source, const ConvertShapeFormat *csf, uint32 real_len) {
	debugC(kDebugGraphics, "Testing shape format %s...", csf->_name);
	bool result = true;

	// Just to be safe
	int start_pos = source.pos();

	// Read the ident
	if (csf->_bytes_ident) {
		char ident[5];
		ident[csf->_bytes_ident] = 0;
		source.read(ident, csf->_bytes_ident);

		if (memcmp(ident, csf->_ident, csf->_bytes_ident)) {
			// Return to start position
			source.seek(start_pos);
			return false;
		}
	}

	// Read the header special colour
	if (csf->_bytes_special) source.skip(csf->_bytes_special);

	// Read the header unknown
	if (csf->_bytes_header_unk) source.skip(csf->_bytes_header_unk);

	// Now read _num_frames
	int numFrames = 1;
	if (csf->_bytes_num_frames) numFrames = readX(source, csf->_bytes_num_frames);
	if (numFrames == 0) numFrames = CalcNumFrames(source,csf,real_len,start_pos);

	// Create _frames array
	ConvertShapeFrame oneframe;
	memset(&oneframe, 0, sizeof(ConvertShapeFrame));

	// Now read the _frames
	for (int f = 0; f < numFrames; f++) {
		ConvertShapeFrame *frame = &oneframe;

		// Seek to initial pos
		source.seek(start_pos + csf->_len_header + (csf->_len_frameheader * f));

		// Read the offset
		uint32 frame_offset = csf->_len_header + (csf->_len_frameheader * f);
		if (csf->_bytes_frame_offset) frame_offset = readX(source, csf->_bytes_frame_offset) + csf->_bytes_special;

		// Read the unknown
		if (csf->_bytes_frameheader_unk) source.read(frame->_header_unknown, csf->_bytes_frameheader_unk);

		// Read frame_length
		uint32 frame_length = real_len-frame_offset;
		if (csf->_bytes_frame_length) frame_length = readX(source, csf->_bytes_frame_length) + csf->_bytes_frame_length_kludge;

		// Invalid frame length
		if ((frame_length + frame_offset) > real_len) {
			result = false;
			break;
		}

		// Seek to start of frame
		source.seek(start_pos + frame_offset);

		// Read unknown
		if (csf->_bytes_frame_unknown) source.read(frame->_unknown, csf->_bytes_frame_unknown);

		// Frame details
		frame->_compression = readX(source, csf->_bytes_frame_compression);
		frame->_width = readXS(source, csf->_bytes_frame_width);
		frame->_height = readXS(source, csf->_bytes_frame_height);
		frame->_xoff = readXS(source, csf->_bytes_frame_xoff);
		frame->_yoff = readXS(source, csf->_bytes_frame_yoff);

		if ((frame->_compression != 0 && frame->_compression != 1) || frame->_width < 0 || frame->_height < 0) {
			frame->_compression = 0;
			frame->_width = 0;
			frame->_height = 0;
			frame->_xoff = 0;
			frame->_yoff = 0;
			result = false;
			break;
		}

		if (frame->_height) {
			// Line offsets
			int32 highest_offset_byte = 0;

			// Calculate the number of bytes of RLE data
			frame->_bytes_rle = frame_length - (csf->_len_frameheader2 + (frame->_height * csf->_bytes_line_offset));

			// Totally invalid shape
			if (frame->_bytes_rle < 0) {
				result = false;
				break;
			}

			// Only attempt to decompress the shape if we are not a compressed shapes
			if (!csf->_bytes_special) {

				// Seek to first in offset table
				source.seek(start_pos + frame_offset + csf->_len_frameheader2);

				// Loop through each of the _frames and find the last rle run
				for (int i = 0; i < frame->_height; i++) {
					int32 line_offset = readX(source, csf->_bytes_line_offset);

					// Now fudge with the value and turn it into an offset into the rle data
					// if required
					if (!csf->_line_offset_absolute)
						line_offset -= (frame->_height - i) * csf->_bytes_line_offset;

					if (line_offset > frame->_bytes_rle) {
						result = false;
						break;
					}

					if (line_offset > highest_offset_byte) highest_offset_byte = line_offset;
				};

				// Failed for whatever reason
				if (result == false) break;

				// Jump to the line offset and calculate the length of the run
				source.seek(highest_offset_byte + start_pos + frame_offset + csf->_len_frameheader2 + frame->_height * csf->_bytes_line_offset);
				int xpos = 0;
				uint32 dlen = 0;

				// Compressed
				if (frame->_compression) {
					do {
						xpos += source.readByte();
						if (xpos == frame->_width) break;

						dlen = source.readByte();
						int type = dlen & 1;
						dlen >>= 1;

						if (!type) source.skip(dlen);
						else source.skip(1);

						xpos += dlen;

					} while (xpos < frame->_width);
				// Uncompressed
				} else {
					do {
						xpos += source.readByte();
						if (xpos == frame->_width) break;

						dlen = source.readByte();
						source.skip(dlen);

						xpos += dlen;
					} while (xpos < frame->_width);
				}

				// Calc 'real' bytes rle
				int32 highest_rle_byte = source.pos();
				highest_rle_byte -= start_pos + frame_offset + csf->_len_frameheader2 + frame->_height * csf->_bytes_line_offset;

				// Too many bytes
				if (highest_rle_byte > frame->_bytes_rle) {
					result = false;
					break;
				}
			}
		}
	}

	// Free frames
	oneframe.Free();

	// Return to start position
	source.seek(start_pos);

	if (result)
		debugC(kDebugGraphics, "Detected Shape Format: %s", csf->_name);
	return result;
}

bool ConvertShape::CheckUnsafe(Common::SeekableReadStream &source, const ConvertShapeFormat *csf, uint32 real_len) {
	debugC(kDebugGraphics, "Testing shape format %s...", csf->_name);
	bool result = true;

	// Just to be safe
	const uint32 start_pos = source.pos();

	// Read the ident
	if (csf->_bytes_ident) {
		char ident[5];
		ident[csf->_bytes_ident] = 0;
		source.read(ident, csf->_bytes_ident);

		if (memcmp(ident, csf->_ident, csf->_bytes_ident)) {
			// Return to start position
			source.seek(start_pos);
			return false;
		}
	}

	// Read the header special colour
	if (csf->_bytes_special) source.skip(csf->_bytes_special);

	// Read the header unknown
	if (csf->_bytes_header_unk) source.skip(csf->_bytes_header_unk);

	// Now read _num_frames
	int numFrames = 1;
	if (csf->_bytes_num_frames) numFrames = readX(source, csf->_bytes_num_frames);
	if (numFrames == 0) numFrames = CalcNumFrames(source,csf,real_len,start_pos);

	// Create _frames array
	ConvertShapeFrame oneframe;
	memset(&oneframe, 0, sizeof(ConvertShapeFrame));

	// Now read the _frames
	for (int f = 0; f < numFrames; f++) {
		ConvertShapeFrame *frame = &oneframe;

		// Seek to initial pos
		source.seek(start_pos + csf->_len_header + (csf->_len_frameheader * f));

		// Read the offset
		uint32 frame_offset = csf->_len_header + (csf->_len_frameheader * f);
		if (csf->_bytes_frame_offset) frame_offset = readX(source, csf->_bytes_frame_offset) + csf->_bytes_special;

		// Read the unknown
		if (csf->_bytes_frameheader_unk) source.read(frame->_header_unknown, csf->_bytes_frameheader_unk);

		// Read frame_length
		uint32 frame_length = real_len-frame_offset;
		if (csf->_bytes_frame_length) frame_length = readX(source, csf->_bytes_frame_length) + csf->_bytes_frame_length_kludge;

		// Invalid frame length
		if ((frame_length + frame_offset) > real_len) {
			result = false;
			break;
		}

		// Seek to start of frame
		source.seek(start_pos + frame_offset);

		// Read unknown
		if (csf->_bytes_frame_unknown) source.read(frame->_unknown, csf->_bytes_frame_unknown);

		// Frame details
		frame->_compression = readX(source, csf->_bytes_frame_compression);
		frame->_width = readXS(source, csf->_bytes_frame_width);
		frame->_height = readXS(source, csf->_bytes_frame_height);
		frame->_xoff = readXS(source, csf->_bytes_frame_xoff);
		frame->_yoff = readXS(source, csf->_bytes_frame_yoff);

		if ((frame->_compression != 0 && frame->_compression != 1) || frame->_width < 0 || frame->_height < 0) {
			frame->_compression = 0;
			frame->_width = 0;
			frame->_height = 0;
			frame->_xoff = 0;
			frame->_yoff = 0;
			result = false;
			break;
		}

		if (frame->_height) {
			// Calculate the number of bytes of RLE data (may not be accurate but we don't care)
			frame->_bytes_rle = frame_length - (csf->_len_frameheader2 + (frame->_height * csf->_bytes_line_offset));

			// Totally invalid shape
			if (frame->_bytes_rle < 0) {
				result = false;
				break;
			}
		}
	}

	// Free _frames
	oneframe.Free();

	// Return to start position
	source.seek(start_pos);

	if (result)
		debugC(kDebugGraphics, "Detected Shape Format: %s", csf->_name);
	return result;
}

void ConvertShape::Write(Common::SeekableWriteStream &dest, const ConvertShapeFormat *csf, uint32 &write_len) {
	// Just to be safe
	const uint32 start_pos = dest.pos();

	// Write the ident
	if (csf->_bytes_ident) dest.write(csf->_ident, csf->_bytes_ident);

	// Write the header unknown
	if (csf->_bytes_header_unk) dest.write(_header_unknown, csf->_bytes_header_unk);

	// Now write _num_frames
	if (csf->_bytes_num_frames) writeX(dest, _num_frames, csf->_bytes_num_frames);
	else if (!csf->_bytes_num_frames && _num_frames > 1) {
		warning("Error: Unable to convert multiple frame shapes to %s", csf->_name);
		return;
	}

	// Write filler space for the frame details
	for (uint32 i = 0; i < _num_frames*csf->_len_frameheader; i++) dest.writeByte(0);

	// Now write the _frames
	for(uint32 f = 0; f < _num_frames; f++) {
		ConvertShapeFrame *frame = _frames + f;

		// Get the frame offset
		uint32 frame_offset = dest.pos() - start_pos;

		// Seek to the frame header pos
		dest.seek(start_pos + csf->_len_header + (csf->_len_frameheader * f));

		// Write the offset
		if (csf->_bytes_frame_offset) writeX(dest, frame_offset, csf->_bytes_frame_offset);

		// Write the unknown
		if (csf->_bytes_frameheader_unk) dest.write(frame->_header_unknown, csf->_bytes_frameheader_unk);

		// Calc and write frame_length
		if (csf->_bytes_frame_length) {
			uint32 frame_length = csf->_len_frameheader2 + (frame->_height * csf->_bytes_line_offset) + frame->_bytes_rle;
			writeX(dest, frame_length - csf->_bytes_frame_length_kludge, csf->_bytes_frame_length);
		}

		// Seek to start of frame
		dest.seek(start_pos + frame_offset);

		// Write unknown
		if (csf->_bytes_frame_unknown) dest.write(frame->_unknown, csf->_bytes_frame_unknown);

		// Frame details
		writeX(dest, frame->_compression, csf->_bytes_frame_compression);
		writeX(dest, frame->_width, csf->_bytes_frame_width);
		writeX(dest, frame->_height, csf->_bytes_frame_height);
		writeX(dest, frame->_xoff, csf->_bytes_frame_xoff);
		writeX(dest, frame->_yoff, csf->_bytes_frame_yoff);

		// Line offsets
		for (int32 i = 0; i < frame->_height; i++) {
			int32 actual_offset = frame->_line_offsets[i];

			// Unfudge the value and write it, if requiretd
			if (!csf->_line_offset_absolute)
				actual_offset += (frame->_height - i) * csf->_bytes_line_offset;

			writeX(dest, actual_offset, csf->_bytes_line_offset);
		}

		// Write the RLE Data
		dest.write(frame->_rle_data, frame->_bytes_rle);
	}

	// Just cheat
	write_len = dest.pos() - start_pos;
}


// Shape format configuration for Pentagram
const ConvertShapeFormat		PentagramShapeFormat = {
	"Pentagram",
	8,		// header
	"PSHP",	// ident
	4,		// bytes_ident
	0,		// bytes_special
	0,		// header_unk
	4,		// _num_frames

	8,		// frameheader
	4,		// frame_offset
	0,		// frameheader_unk
	4,		// frame_length
	0,		// frame_length_kludge

	20,		// frameheader2
	0,		// frame_unknown
	4,		// frame_compression
	4,		// frame_width
	4,		// frame_height
	4,		// frame_xoff
	4,		// frame_yoff

	4,		// line_offset
	1		// line_offset_absolute
};

} // End of namespace Ultima8
} // End of namespace Ultima