[1c3b2d]: gemrb / plugins / BAMImporter / BAMImporter.cpp Maximize Restore History

Download this file

BAMImporter.cpp    300 lines (269 with data), 8.1 kB

  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
/* GemRB - Infinity Engine Emulator
* Copyright (C) 2003 The GemRB Project
*
* 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.
*
*
*/
#ifdef ANDROID
#include "swab.h"
#endif
#include "BAMImporter.h"
#include "win32def.h"
#include "FileCache.h"
#include "GameData.h"
#include "Interface.h"
#include "Palette.h"
#include "Sprite2D.h"
#include "Video.h"
#include "System/FileStream.h"
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
using namespace GemRB;
BAMImporter::BAMImporter(void)
{
str = NULL;
frames = NULL;
cycles = NULL;
palette = NULL;
FramesCount = 0;
CyclesCount = 0;
}
BAMImporter::~BAMImporter(void)
{
delete str;
delete[] frames;
delete[] cycles;
gamedata->FreePalette(palette);
}
bool BAMImporter::Open(DataStream* stream)
{
unsigned int i;
if (stream == NULL) {
return false;
}
delete str;
delete[] frames;
delete[] cycles;
gamedata->FreePalette(palette);
str = stream;
char Signature[8];
str->Read( Signature, 8 );
if (strncmp( Signature, "BAMCV1 ", 8 ) == 0) {
str->Seek( 4, GEM_CURRENT_POS );
DataStream* cached = CacheCompressedStream(stream, stream->filename);
delete str;
if (!cached)
return false;
str = cached;
str->Read( Signature, 8 );
}
if (strncmp( Signature, "BAM V1 ", 8 ) != 0) {
return false;
}
str->ReadWord( &FramesCount );
str->Read( &CyclesCount, 1 );
str->Read( &CompressedColorIndex, 1 );
str->ReadDword( &FramesOffset );
str->ReadDword( &PaletteOffset );
str->ReadDword( &FLTOffset );
str->Seek( FramesOffset, GEM_STREAM_START );
frames = new FrameEntry[FramesCount];
DataStart = str->Size();
for (i = 0; i < FramesCount; i++) {
str->ReadWord( &frames[i].Width );
str->ReadWord( &frames[i].Height );
str->ReadWord( &frames[i].XPos );
str->ReadWord( &frames[i].YPos );
str->ReadDword( &frames[i].FrameData );
if ((frames[i].FrameData & 0x7FFFFFFF) < DataStart)
DataStart = (frames[i].FrameData & 0x7FFFFFFF);
}
cycles = new CycleEntry[CyclesCount];
for (i = 0; i < CyclesCount; i++) {
str->ReadWord( &cycles[i].FramesCount );
str->ReadWord( &cycles[i].FirstFrame );
}
str->Seek( PaletteOffset, GEM_STREAM_START );
palette = new Palette();
// no need to switch this
for (i = 0; i < 256; i++) {
RevColor rc;
str->Read( &rc, 4 );
palette->col[i].r = rc.r;
palette->col[i].g = rc.g;
palette->col[i].b = rc.b;
palette->col[i].a = rc.a;
}
return true;
}
int BAMImporter::GetCycleSize(unsigned char Cycle)
{
if(Cycle >= CyclesCount ) {
return -1;
}
return cycles[Cycle].FramesCount;
}
Sprite2D* BAMImporter::GetFrameInternal(unsigned short findex, unsigned char mode,
bool BAMsprite, const unsigned char* data,
AnimationFactory* datasrc)
{
Sprite2D* spr = 0;
if (BAMsprite) {
bool RLECompressed = (frames[findex].FrameData & 0x80000000) == 0;
assert(data);
const unsigned char* framedata = data;
framedata += (frames[findex].FrameData & 0x7FFFFFFF) - DataStart;
spr = core->GetVideoDriver()->CreateSpriteBAM8(frames[findex].Width,
frames[findex].Height,
RLECompressed,
framedata, datasrc,
palette,
CompressedColorIndex);
} else {
void* pixels = GetFramePixels(findex);
spr = core->GetVideoDriver()->CreateSprite8(
frames[findex].Width, frames[findex].Height, 8,
pixels, palette->col, true, 0 );
}
spr->XPos = (ieWordSigned)frames[findex].XPos;
spr->YPos = (ieWordSigned)frames[findex].YPos;
if (mode == IE_SHADED) {
// CHECKME: is this ever used? Should we modify the sprite's palette
// without creating a local copy for this sprite?
Palette* pal = spr->GetPalette();
pal->CreateShadedAlphaChannel();
pal->Release();
}
return spr;
}
void* BAMImporter::GetFramePixels(unsigned short findex)
{
if (findex >= FramesCount) {
findex = cycles[0].FirstFrame;
}
str->Seek( ( frames[findex].FrameData & 0x7FFFFFFF ), GEM_STREAM_START );
unsigned long pixelcount = frames[findex].Height * frames[findex].Width;
void* pixels = malloc( pixelcount );
bool RLECompressed = ( ( frames[findex].FrameData & 0x80000000 ) == 0 );
if (RLECompressed) {
//if RLE Compressed
unsigned long RLESize;
RLESize = ( unsigned long )
( frames[findex].Width * frames[findex].Height * 3 ) / 2 + 1;
//without partial reads, we should be careful
unsigned long remains = str->Remains();
if (RLESize > remains) {
RLESize = remains;
}
unsigned char* inpix;
inpix = (unsigned char*)malloc( RLESize );
if (str->Read( inpix, RLESize ) == GEM_ERROR) {
free( pixels );
free( inpix );
return NULL;
}
unsigned char * p = inpix;
unsigned char * Buffer = (unsigned char*)pixels;
unsigned int i = 0;
while (i < pixelcount) {
if (*p == CompressedColorIndex) {
p++;
// FIXME: Czech HOW has apparently broken frame
// #141 in REALMS.BAM. Maybe we should put
// this condition to #ifdef BROKEN_xx ?
// Or maybe rather put correct REALMS.BAM
// into override/ dir?
if (i + ( *p ) + 1 > pixelcount) {
memset( &Buffer[i], CompressedColorIndex, pixelcount - i );
print("Broken frame %d", findex);
} else {
memset( &Buffer[i], CompressedColorIndex, ( *p ) + 1 );
}
i += *p;
} else
Buffer[i] = *p;
p++;
i++;
}
free( inpix );
} else {
str->Read( pixels, pixelcount );
}
return pixels;
}
ieWord * BAMImporter::CacheFLT(unsigned int &count)
{
int i;
count = 0;
for (i = 0; i < CyclesCount; i++) {
unsigned int tmp = cycles[i].FirstFrame + cycles[i].FramesCount;
if (tmp > count) {
count = tmp;
}
}
ieWord * FLT = ( ieWord * ) calloc( count, sizeof(ieWord) );
str->Seek( FLTOffset, GEM_STREAM_START );
str->Read( FLT, count * sizeof(ieWord) );
if( DataStream::IsEndianSwitch() ) {
//msvc likes it as char *
swab( (char*) FLT, (char*) FLT, count * sizeof(ieWord) );
}
return FLT;
}
AnimationFactory* BAMImporter::GetAnimationFactory(const char* ResRef, unsigned char mode)
{
unsigned int i, count;
AnimationFactory* af = new AnimationFactory( ResRef );
ieWord *FLT = CacheFLT( count );
bool videoBAMsupport = core->GetVideoDriver()->SupportsBAMSprites();
unsigned char* data = NULL;
if (videoBAMsupport) {
str->Seek( DataStart, GEM_STREAM_START );
unsigned long length = str->Remains();
if (length == 0) return af;
//data = new unsigned char[length];
data = (unsigned char *) malloc(length);
str->Read( data, length );
af->SetFrameData(data);
}
for (i = 0; i < FramesCount; ++i) {
Sprite2D* frame = GetFrameInternal(i, mode, videoBAMsupport, data, af);
assert(!videoBAMsupport || frame->BAM);
af->AddFrame(frame);
}
for (i = 0; i < CyclesCount; ++i) {
af->AddCycle( cycles[i] );
}
af->LoadFLT ( FLT, count );
free (FLT);
return af;
}
/** Debug Function: Returns the Global Animation Palette as a Sprite2D Object.
If the Global Animation Palette is NULL, returns NULL. */
Sprite2D* BAMImporter::GetPalette()
{
unsigned char * pixels = ( unsigned char * ) malloc( 256 );
unsigned char * p = pixels;
for (int i = 0; i < 256; i++) {
*p++ = ( unsigned char ) i;
}
return core->GetVideoDriver()->CreateSprite8( 16, 16, 8, pixels, palette->col, false );
}
#include "BAMFontManager.h"
#include "plugindef.h"
GEMRB_PLUGIN(0x3AD6427A, "BAM File Importer")
PLUGIN_IE_RESOURCE(BAMFontManager, "bam", (ieWord)IE_BAM_CLASS_ID)
PLUGIN_CLASS(IE_BAM_CLASS_ID, BAMImporter)
END_PLUGIN()