/////////////////////////////////////////////////////////////////////////////
-// Name: imagiff.h
+// Name: src/common/imagiff.h
// Purpose: wxImage handler for Amiga IFF images
// Author: Steffen Gutmann, Thomas Meyer
// RCS-ID: $Id$
#include "wx/wxprec.h"
#ifdef __BORLANDC__
-# pragma hdrstop
+ #pragma hdrstop
#endif
+#if wxUSE_IMAGE && wxUSE_IFF
+
#ifndef WX_PRECOMP
-# include "wx/defs.h"
+ #include "wx/log.h"
+ #include "wx/intl.h"
#endif
-#if wxUSE_IMAGE && wxUSE_IFF
-
#include "wx/imagiff.h"
#include "wx/wfstream.h"
-#include "wx/log.h"
-#include "wx/intl.h"
#if wxUSE_PALETTE
#include "wx/palette.h"
if ( !m_f->Read(buf, WXSIZEOF(buf)) )
return false;
- m_f->SeekI(-(wxFileOffset)WXSIZEOF(buf), wxFromCurrent);
+ if ( m_f->SeekI(-(wxFileOffset)WXSIZEOF(buf), wxFromCurrent) == wxInvalidOffset )
+ return false;
return (memcmp(buf, "FORM", 4) == 0) && (memcmp(buf+8, "ILBM", 4) == 0);
}
#define IFFDEBUG 0
/*************************************************************************
- void decomprle(source, destination, source length, buffer size)
+void decomprle(source, destination, source length, buffer size)
- Decompress run-length encoded data from source to destination. Terminates
- when source is decoded completely or destination buffer is full.
+Decompress run-length encoded data from source to destination. Terminates
+when source is decoded completely or destination buffer is full.
- The decruncher is as optimized as I could make it, without risking
- safety in case of corrupt BODY chunks.
+The decruncher is as optimized as I could make it, without risking
+safety in case of corrupt BODY chunks.
**************************************************************************/
static void decomprle(const byte *sptr, byte *dptr, long slen, long dlen)
m_image = new IFFImage();
if (m_image == 0) {
- Destroy();
- return wxIFF_MEMERR;
+ Destroy();
+ return wxIFF_MEMERR;
}
// compute file length
wxFileOffset currentPos = m_f->TellI();
- m_f->SeekI(0, wxFromEnd);
+ if (m_f->SeekI(0, wxFromEnd) == wxInvalidOffset) {
+ Destroy();
+ return wxIFF_MEMERR;
+ }
+
long filesize = m_f->TellI();
- m_f->SeekI(currentPos, wxFromStart);
+ if (m_f->SeekI(currentPos, wxFromStart) == wxInvalidOffset) {
+ Destroy();
+ return wxIFF_MEMERR;
+ }
// allocate memory for complete file
if ((databuf = new byte[filesize]) == 0) {
- Destroy();
- return wxIFF_MEMERR;
+ Destroy();
+ return wxIFF_MEMERR;
}
m_f->Read(databuf, filesize);
// check for minmal size
if (dataptr + 12 > dataend) {
- Destroy();
- return wxIFF_INVFORMAT;
+ Destroy();
+ return wxIFF_INVFORMAT;
}
// check if we really got an IFF file
if (strncmp((char *)dataptr, "FORM", 4) != 0) {
- Destroy();
- return wxIFF_INVFORMAT;
+ Destroy();
+ return wxIFF_INVFORMAT;
}
dataptr = dataptr + 8; // skip ID and length of FORM
// check if the IFF file is an ILBM (picture) file
if (strncmp((char *) dataptr, "ILBM", 4) != 0) {
- Destroy();
- return wxIFF_INVFORMAT;
+ Destroy();
+ return wxIFF_INVFORMAT;
}
wxLogTrace(_T("iff"), _T("IFF ILBM file recognized"));
// main decoding loop. searches IFF chunks and handles them.
// terminates when BODY chunk was found or dataptr ran over end of file
//
- bool BMHDok = false, CMAPok = false, CAMGok = false;
+ bool BMHDok = false, CAMGok = false;
int bmhd_width = 0, bmhd_height = 0, bmhd_bitplanes = 0, bmhd_transcol = -1;
- byte bmhd_masking = 0, bmhd_compression = 0;
+ byte bmhd_compression = 0;
long camg_viewmode = 0;
int colors = 0;
while (dataptr + 8 <= dataend) {
// get chunk length and make even
- size_t chunkLen = (iff_getlong(dataptr + 4) + 1) & 0xfffffffe;
-#ifdef __VMS
- // Silence compiler warning
- int chunkLen_;
- chunkLen_ = chunkLen;
- if (chunkLen_ < 0) { // format error?
-#else
- if (chunkLen < 0) { // format error?
-#endif
- break;
+ long chunkLen = (iff_getlong(dataptr + 4) + 1) & 0xfffffffe;
+ if (chunkLen < 0) { // format error?
+ break;
}
bool truncated = (dataptr + 8 + chunkLen > dataend);
bmhd_width = iff_getword(dataptr + 8); // width of picture
bmhd_height= iff_getword(dataptr + 8 + 2); // height of picture
bmhd_bitplanes = *(dataptr + 8 + 8); // # of bitplanes
- bmhd_masking = *(dataptr + 8 + 9);
+ // bmhd_masking = *(dataptr + 8 + 9); -- unused currently
bmhd_compression = *(dataptr + 8 + 10); // get compression
bmhd_transcol = iff_getword(dataptr + 8 + 12);
BMHDok = true; // got BMHD
wxLogTrace(_T("iff"), _T("Read %d colors from IFF file."),
colors);
- CMAPok = true; // got CMAP
dataptr += 8 + chunkLen; // to next chunk
} else if (strncmp((char *)dataptr, "CAMG", 4) == 0) { // CAMG ?
if (chunkLen < 4 || truncated) {
int c = (col & 0x0f);
switch (col & 0x30) {
case 0x00: if (c >= 0 && c < colors) {
- rval = pal[3*c + 0];
- gval = pal[3*c + 1];
- bval = pal[3*c + 2];
- }
- break;
+ rval = pal[3*c + 0];
+ gval = pal[3*c + 1];
+ bval = pal[3*c + 2];
+ }
+ break;
case 0x10: bval = c * 17;
- break;
+ break;
case 0x20: rval = c * 17;
- break;
+ break;
case 0x30: gval = c * 17;
- break;
+ break;
}
} else if (fmt == ILBM_HAM8) {
int c = (col & 0x3f);
switch(col & 0xc0) {
case 0x00: if (c >= 0 && c < colors) {
- rval = pal[3*c + 0];
- gval = pal[3*c + 1];
- bval = pal[3*c + 2];
- }
- break;
+ rval = pal[3*c + 0];
+ gval = pal[3*c + 1];
+ bval = pal[3*c + 2];
+ }
+ break;
case 0x40: bval = (bval & 3) | (c << 2);
- break;
+ break;
case 0x80: rval = (rval & 3) | (c << 2);
- break;
+ break;
case 0xc0: gval = (rval & 3) | (c << 2);
}