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
|
#include "advwin32.h"
#ifndef DB_LEVEL
#define DB_LEVEL 4
#endif
#include "db.h"
#ifndef DB_COMMA
#define DB_COMMA ,
#endif
#pragma warning(disable: 4701)
#include "awTexLd.hpp"
#pragma warning(default: 4701)
#include "iff.hpp"
#include "iff_ILBM.hpp"
#include "list_tem.hpp"
#include <limits.h>
// conversion functors for IFF loader
class AwIffConvNull
{
public:
static inline unsigned DoConv (unsigned const * _colP, AwTl::Colour const * db_code1(DB_COMMA unsigned))
{
db_assert1(AwTl::pixelFormat.palettizedB);
return *_colP;
}
};
class AwIffConvNonTransp : public AwTl::Colour::ConvNonTransp
{
public:
static inline unsigned DoConv(unsigned const * pCol, AwTl::Colour const * pPalette db_code1(DB_COMMA unsigned nPaletteSize))
{
db_assert1(pPalette);
db_onlyassert1(*pCol < nPaletteSize);
return AwTl::Colour::ConvNonTransp::DoConv(&pPalette[*pCol]);
}
};
class AwIffConvTransp
{
public:
static unsigned iTranspCol; // the index of the transparent colour
static unsigned rawTranspCol; // the value of a transparent pixel on the surface
static inline unsigned DoConv(unsigned const * pCol, AwTl::Colour const * pPalette db_code1(DB_COMMA unsigned nPaletteSize))
{
using namespace AwTl;
if (*pCol == iTranspCol) return rawTranspCol;
unsigned rv = AwIffConvNonTransp::DoConv(pCol,pPalette db_code1(DB_COMMA nPaletteSize));
if (rv != rawTranspCol) return rv;
// make the colour non-transparent (nb: only an occasional case)
// OK, Here's the plan:
// First, suppose that I were to decrease either the Red, Green or Blue
// component in order to make the colour non-transparent.
// Taking Red as an example, I'll work out what the resultant red value
// will be if I decrease red to achieve a non-transparent colour
// I'll compare this to the actual red value of the colour in question
// The lower the difference, the better it'll be (ie. closer to the
// original colour).
// Obviously, I'll do the same for Green and Blue
// Then I'll repeat the process but this time considering increasing
// the components.
// I'll then have six values which are scores (the lower the better)
// for what colour component to change and how to change it.
// If any of the components cannot be decreased (ie. their resulting
// value is already zero) or increased (ie. their resulting value
// is at maximum), then I'll set the corresponding score to
// UINT_MAX so that that colour-changing operation won't be selected
// (because that'll be the one that buggers everything up).
unsigned nRedDiffDown =
(pPalette[*pCol].r < 1<<pixelFormat.redRightShift ) ? UINT_MAX
: (pPalette[*pCol].r & (1<<pixelFormat.redRightShift )-1) + ((1<<pixelFormat.redRightShift )+1)/2;
unsigned nGreenDiffDown =
(pPalette[*pCol].g < 1<<pixelFormat.greenRightShift) ? UINT_MAX
: (pPalette[*pCol].g & (1<<pixelFormat.greenRightShift)-1) + ((1<<pixelFormat.greenRightShift)+1)/2;
unsigned nBlueDiffDown =
(pPalette[*pCol].b < 1<<pixelFormat.blueRightShift ) ? UINT_MAX
: (pPalette[*pCol].b & (1<<pixelFormat.blueRightShift )-1) + ((1<<pixelFormat.blueRightShift )+1)/2;
unsigned nRedDiffUp =
(pPalette[*pCol].r >= (255 & ~((1<<pixelFormat.redRightShift )-1) ) ? UINT_MAX
: (1<<pixelFormat.redRightShift )*3/2 - (pPalette[*pCol].r & (1<<pixelFormat.redRightShift )-1));
unsigned nGreenDiffUp =
(pPalette[*pCol].g >= (255 & ~((1<<pixelFormat.greenRightShift)-1) ) ? UINT_MAX
: (1<<pixelFormat.greenRightShift)*3/2 - (pPalette[*pCol].g & (1<<pixelFormat.greenRightShift)-1));
unsigned nBlueDiffUp =
(pPalette[*pCol].b >= (255 & ~((1<<pixelFormat.blueRightShift )-1) ) ? UINT_MAX
: (1<<pixelFormat.blueRightShift )*3/2 - (pPalette[*pCol].b & (1<<pixelFormat.blueRightShift )-1));
// Pick lowest value and do the business
Colour colAdj = pPalette[*pCol];
#if defined(_MSC_VER) && _MSC_VER >= 1100
// VC5.0 gives inane warnings when += type operators
// are used on types smaller than int (even with
// explicit casting!)
#pragma warning(disable:4244)
#endif
if
(
nBlueDiffUp <= nBlueDiffDown
&& nBlueDiffUp <= nRedDiffUp
&& nBlueDiffUp <= nRedDiffDown
&& nBlueDiffUp <= nGreenDiffUp
&& nBlueDiffUp <= nGreenDiffDown
)
{
colAdj.b += static_cast<unsigned char>(1<<pixelFormat.blueRightShift);
}
else if
(
nBlueDiffDown <= nRedDiffUp
&& nBlueDiffDown <= nRedDiffDown
&& nBlueDiffDown <= nGreenDiffUp
&& nBlueDiffDown <= nGreenDiffDown
)
{
colAdj.b -= static_cast<unsigned char>(1<<pixelFormat.blueRightShift);
}
else if
(
nRedDiffUp <= nRedDiffDown
&& nRedDiffUp <= nGreenDiffUp
&& nRedDiffUp <= nGreenDiffDown
)
{
colAdj.r += static_cast<unsigned char>(1<<pixelFormat.redRightShift);
}
else if
(
nRedDiffDown <= nGreenDiffUp
&& nRedDiffDown <= nGreenDiffDown
)
{
colAdj.r -= static_cast<unsigned char>(1<<pixelFormat.redRightShift);
}
else if (nGreenDiffUp <= nGreenDiffDown)
{
colAdj.g += static_cast<unsigned char>(1<<pixelFormat.greenRightShift);
}
else
{
colAdj.g -= static_cast<unsigned char>(1<<pixelFormat.greenRightShift);
}
#if defined(_MSC_VER) && _MSC_VER == 1100
// VC5.0 gives inane warnings when += type operators
// are used on types smaller than int (even with
// explicit casting!)
#pragma warning(default:4244)
#endif
return Colour::ConvNonTransp::DoConv(&colAdj);
}
};
unsigned AwIffConvTransp::iTranspCol;
unsigned AwIffConvTransp::rawTranspCol;
// IFF Loader
class AwIffLoader : public AwTl::TexFileLoader
{
public:
AwIffLoader() : m_pPalette(NULL), m_bDecoding(false) {}
protected:
virtual ~AwIffLoader();
virtual void LoadHeaderInfo(MediaMedium * pMedium);
virtual unsigned GetNumColours();
virtual unsigned GetMinPaletteSize();
virtual bool HasTransparentMask(bool bDefault);
virtual void AllocateBuffers(bool bWantBackup, unsigned nMaxPaletteSize);
virtual void OnBeginRestoring(unsigned nMaxPaletteSize);
virtual AwTl::Colour * GetPalette();
virtual AwTl::PtrUnion GetRowPtr(unsigned nRow);
virtual void LoadNextRow(AwTl::PtrUnion pRow);
virtual void ConvertRow(AwTl::PtrUnion pDest, unsigned nDestWidth, AwTl::PtrUnionConst pSrc, unsigned nSrcOffset, unsigned nSrcWidth, AwTl::Colour * pPalette db_code1(DB_COMMA unsigned nPaletteSize));
virtual DWORD GetTransparentColour();
virtual void OnFinishLoading(bool bSuccess);
virtual void OnFinishRestoring(bool bSuccess);
virtual AwBackupTexture * CreateBackupTexture();
private:
static bool Enumerator(IFF::Chunk * pChunk, void * pData);
// list of chunks found by enumerator
List<IFF::IlbmBodyChunk *> m_listBodyChunks;
// smallest and largest palette sizes of versions of this image
unsigned m_nMaxPaletteSize;
unsigned m_nMinPaletteSize;
// buffer for palette -
// since the IFF cmap table is in a different format to what the Aw loaders require
// (maybe should think about standardizing the data types?)
AwTl::Colour * m_pPalette;
// iff data
IFF::File m_ifData;
IFF::IlbmBmhdChunk * m_pHdr;
IFF::IlbmCmapChunk * m_pCmap;
IFF::IlbmBodyChunk * m_pBody;
bool m_bDecoding;
};
AwIffLoader::~AwIffLoader()
{
if (m_pPalette) delete[] m_pPalette;
}
void AwIffLoader::LoadHeaderInfo(MediaMedium * pMedium)
{
db_log4("\tLoading an IFF file");
while (m_listBodyChunks.size())
m_listBodyChunks.delete_first_entry();
if (!m_ifData.Load(pMedium) || !m_ifData.GetContents())
{
if (NO_ERROR == (awTlLastWinErr = GetLastError()))
awTlLastErr = AW_TLE_BADFILEDATA;
else
awTlLastErr = AW_TLE_CANTREADFILE;
db_log3("AwCreateTexture(): ERROR: IFF file load failed");
}
else
{
m_nMinPaletteSize = UINT_MAX;
m_nMaxPaletteSize = 0;
m_ifData.GetContents()->EnumChildren("ILBM","BODY",Enumerator,this);
}
}
unsigned AwIffLoader::GetNumColours()
{
return m_nMaxPaletteSize;
}
unsigned AwIffLoader::GetMinPaletteSize()
{
return m_nMinPaletteSize;
}
void AwIffLoader::AllocateBuffers(bool /*bWantBackup*/, unsigned nMaxPaletteSize)
{
// we will need to allocate buffers when restoring as well as first-time loading
// so allocate buffers in OnBeginRestoring() which we'll call here
OnBeginRestoring(nMaxPaletteSize);
}
bool AwIffLoader::Enumerator(IFF::Chunk * pChunk, void * pData)
{
db_assert1(pChunk);
db_assert1(pData);
AwIffLoader * pThis = static_cast<AwIffLoader *>(pData);
IFF::Chunk * pCmap = pChunk->GetProperty("CMAP");
IFF::Chunk * pHdr = pChunk->GetProperty("BMHD");
if (pCmap && pHdr) // must have these two properties
{
unsigned nThisPaletteSize = static_cast<IFF::IlbmCmapChunk *>(pCmap)->nEntries;
db_logf4(("\tfound a %u colour %scompressed IFF body chunk",nThisPaletteSize,static_cast<IFF::IlbmBmhdChunk *>(pHdr)->eCompression ? "" : "un"));
pThis->m_listBodyChunks.add_entry(static_cast<IFF::IlbmBodyChunk *>(pChunk));
if (nThisPaletteSize < pThis->m_nMinPaletteSize)
pThis->m_nMinPaletteSize = nThisPaletteSize;
if (nThisPaletteSize > pThis->m_nMaxPaletteSize)
pThis->m_nMaxPaletteSize = nThisPaletteSize;
}
else db_log3("AwCreateTexture(): WARNING: IFF body chunk found with insufficient associated property chunks");
return true; // continue enumeration
}
void AwIffLoader::OnBeginRestoring(unsigned nMaxPaletteSize)
{
using namespace AwTl;
if (m_listBodyChunks.size())
{
// if decodeing, m_pBody will be valid
if (m_bDecoding)
{
m_pBody->EndDecode();
m_bDecoding = false;
}
m_pBody = NULL;
unsigned nBestPaletteSize = 0;
for (LIF<IFF::IlbmBodyChunk *> itChunks(&m_listBodyChunks); !itChunks.done(); itChunks.next())
{
IFF::IlbmCmapChunk * pCmap = static_cast<IFF::IlbmCmapChunk *>(itChunks()->GetProperty("CMAP"));
db_assert1(pCmap);
if ((!nMaxPaletteSize || pCmap->nEntries <= nMaxPaletteSize) && pCmap->nEntries > nBestPaletteSize)
{
m_pBody = itChunks();
m_pCmap = pCmap;
nBestPaletteSize = pCmap->nEntries;
}
}
if (m_pBody)
{
m_pHdr = static_cast<IFF::IlbmBmhdChunk *>(m_pBody->GetProperty("BMHD"));
// delete old buffers
if (m_pPalette) delete[] m_pPalette;
// allocate buffer for palette, make it extra big to cope with corrupt files
unsigned nAllocPaletteSize = m_pCmap->nEntries;
unsigned nAllocPaletteSizeShift = 0;
while (0!=(nAllocPaletteSize >>= 1))
{
++nAllocPaletteSizeShift;
}
m_pPalette = new Colour [2<<nAllocPaletteSizeShift]; // prevent corrupt data causing a crash
//m_pPalette = new Colour [m_pCmap->nEntries];
// copy the palette
for (unsigned i=0; i<m_pCmap->nEntries; ++i)
{
// hacked testa
m_pPalette[i].r = m_pCmap->pTable[i].r;
m_pPalette[i].g = m_pCmap->pTable[i].g;
m_pPalette[i].b = m_pCmap->pTable[i].b;
}
// set the width height and palette size in the base class
m_nPaletteSize = m_pCmap->nEntries;
m_nWidth = m_pHdr->width;
m_nHeight = m_pHdr->height;
// prepare to decode the data
m_pBody->BeginDecode();
m_bDecoding = true;
// set the transparent mask colours
switch (m_pHdr->eMasking)
{
case IFF::IlbmBmhdChunk::MASK_NONE:
break;
case IFF::IlbmBmhdChunk::MASK_TRANSPARENTCOL:
AwIffConvTransp::iTranspCol = m_pHdr->iTranspCol;
if (pixelFormat.palettizedB)
AwIffConvTransp::rawTranspCol = AwIffConvTransp::iTranspCol;
else
AwIffConvTransp::rawTranspCol =
static_cast<unsigned>(m_pPalette[AwIffConvTransp::iTranspCol].r)>>pixelFormat.redRightShift<<pixelFormat.redLeftShift
|static_cast<unsigned>(m_pPalette[AwIffConvTransp::iTranspCol].g)>>pixelFormat.greenRightShift<<pixelFormat.greenLeftShift
|static_cast<unsigned>(m_pPalette[AwIffConvTransp::iTranspCol].b)>>pixelFormat.blueRightShift<<pixelFormat.blueLeftShift;
break;
default:
db_log3("AwCreateTexture(): ERROR: IFF mask field wrong");
awTlLastErr = AW_TLE_BADFILEDATA;
}
}
else
{
awTlLastErr = AW_TLE_CANTPALETTIZE; // no suitable chunk found
db_log3("AwCreateTexture(): ERROR: No suitable IFF body chunk found");
}
}
else
{
awTlLastErr = AW_TLE_BADFILEDATA;
db_log3("AwCreateTexture(): ERROR: IFF file not loaded or contains no image data");
}
}
AwTl::Colour * AwIffLoader::GetPalette()
{
return m_pPalette;
}
bool AwIffLoader::HasTransparentMask(bool bDefault)
{
if (m_listBodyChunks.size())
{
IFF::IlbmBmhdChunk * pHdr = static_cast<IFF::IlbmBmhdChunk *>(m_listBodyChunks.first_entry()->GetProperty("BMHD"));
db_assert1(pHdr);
return (IFF::IlbmBmhdChunk::MASK_TRANSPARENTCOL == pHdr->eMasking);
}
else
return bDefault;
}
DWORD AwIffLoader::GetTransparentColour()
{
return AwIffConvTransp::rawTranspCol;
}
AwTl::PtrUnion AwIffLoader::GetRowPtr(unsigned /*nRow*/)
{
// the iff object has an internal buffer to which a pointer
// is returned when we decode each row
// unfortunately we have to cast constness away, but never mind
return const_cast<unsigned *>(m_pBody->DecodeNextRow());
}
void AwIffLoader::LoadNextRow(AwTl::PtrUnion /*pRow*/)
{
// GetRowPtr() has called DecodeNextRow()
// which has filled in the data already
// so do nothing here
}
void AwIffLoader::ConvertRow(AwTl::PtrUnion pDest, unsigned nDestWidth, AwTl::PtrUnionConst pSrc, unsigned nSrcOffset, unsigned nSrcWidth, AwTl::Colour * pPalette db_code1(DB_COMMA unsigned nPaletteSize))
{
using namespace AwTl;
// we have overridden this function for two reasons:
// 1. The data type for each texel in the row is unsigned int
// to allow for the fact that all images are palettized
// with no limit on the palette size. The default
// implementation would assume BYTE (unsigned char)
// 2. The transparency flag and colour is stored in the
// file and the transparency flag passed to AwCreateTexture()
// is ignored. The transparent colour does not have to be 0,0,0
// either
db_assert1(pPalette);
db_assert1(pPalette == m_pPalette);
// we can still use GenericConvertRow though, using the conversion functors above
if (pixelFormat.palettizedB)
{
GenericConvertRow<AwIffConvNull,unsigned>::Do(pDest, nDestWidth, pSrc.uintP+nSrcOffset, nSrcWidth);
}
else
{
switch (m_pHdr->eMasking)
{
case IFF::IlbmBmhdChunk::MASK_NONE:
GenericConvertRow<AwIffConvNonTransp,unsigned>::Do(pDest, nDestWidth, pSrc.uintP+nSrcOffset, nSrcWidth, pPalette db_code1(DB_COMMA nPaletteSize));
break;
case IFF::IlbmBmhdChunk::MASK_TRANSPARENTCOL:
GenericConvertRow<AwIffConvTransp,unsigned>::Do(pDest, nDestWidth, pSrc.uintP+nSrcOffset, nSrcWidth, pPalette db_code1(DB_COMMA nPaletteSize));
break;
default:
db_log3("AwCreateTexture(): ERROR: IFF mask field wrong");
awTlLastErr = AW_TLE_BADFILEDATA;
}
}
}
void AwIffLoader::OnFinishLoading(bool /*bSuccess*/)
{
if (m_bDecoding)
{
m_pBody->EndDecode();
m_bDecoding = false;
}
}
void AwIffLoader::OnFinishRestoring(bool /*bSuccess*/)
{
if (m_bDecoding)
{
m_pBody->EndDecode();
m_bDecoding = false;
}
}
AwBackupTexture * AwIffLoader::CreateBackupTexture()
{
// use the same object for restoring
AddRef();
return this;
}
// Valid file ID fields: 'FORM' 'LIST' 'CAT ' - we can load them all
#ifdef _MSC_VER
// VC5.0 tries to compile out code that is in a library
// and it thinks isn't being used
#line 427
#endif
AWTEXLD_IMPLEMENT_DYNCREATE("FORM",AwIffLoader)
AWTEXLD_IMPLEMENT_DYNCREATE("LIST",AwIffLoader)
AWTEXLD_IMPLEMENT_DYNCREATE("CAT ",AwIffLoader)
|