/////////////////////////////////////////////////////////////////////////////
-// Name: src/common/imagiff.h
+// Name: src/common/imagiff.cpp
// Purpose: wxImage handler for Amiga IFF images
// Author: Steffen Gutmann, Thomas Meyer
// RCS-ID: $Id$
IFFImage *m_image; // image data
wxInputStream *m_f; // input stream
unsigned char *databuf;
- unsigned char *picptr;
unsigned char *decomp_mem;
void Destroy();
// constructor, destructor, etc.
wxIFFDecoder(wxInputStream *s);
~wxIFFDecoder() { Destroy(); }
+
+ // NOTE: this function modifies the current stream position
bool CanRead();
+
int ReadIFF();
bool ConvertToImage(wxImage *image) const;
};
void wxIFFDecoder::Destroy()
{
- delete m_image;
- m_image = 0;
- delete [] databuf;
- databuf = 0;
- delete [] decomp_mem;
- decomp_mem = 0;
+ wxDELETE(m_image);
+ wxDELETEA(databuf);
+ wxDELETEA(decomp_mem);
}
//---------------------------------------------------------------------------
// create the image
image->Create(GetWidth(), GetHeight());
- if (!image->Ok())
+ if (!image->IsOk())
return false;
unsigned char *pal = GetPalette();
if ( !m_f->Read(buf, WXSIZEOF(buf)) )
return false;
- m_f->SeekI(-(wxFileOffset)WXSIZEOF(buf), wxFromCurrent);
-
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"));
+ wxLogTrace(wxT("iff"), wxT("IFF ILBM file recognized"));
dataptr = dataptr + 4; // skip ID
// get chunk length and make even
long chunkLen = (iff_getlong(dataptr + 4) + 1) & 0xfffffffe;
if (chunkLen < 0) { // format error?
- break;
+ break;
}
bool truncated = (dataptr + 8 + chunkLen > dataend);
const byte *cmapptr = dataptr + 8;
colors = chunkLen / 3; // calc no of colors
- delete m_image->pal;
- m_image->pal = 0;
+ wxDELETE(m_image->pal);
m_image->colors = colors;
if (colors > 0) {
m_image->pal = new byte[3*colors];
}
}
- wxLogTrace(_T("iff"), _T("Read %d colors from IFF file."),
+ wxLogTrace(wxT("iff"), wxT("Read %d colors from IFF file."),
colors);
dataptr += 8 + chunkLen; // to next chunk
decomprle(bodyptr, decomp_mem, chunkLen, decomp_bufsize);
bodyptr = decomp_mem; // -> uncompressed BODY
chunkLen = decomp_bufsize;
- delete [] databuf;
- databuf = 0;
+ wxDELETEA(databuf);
}
// the following determines the type of the ILBM file.
}
}
- wxLogTrace(_T("iff"),
- _T("LoadIFF: %s %dx%d, planes=%d (%d cols), comp=%d"),
+ wxLogTrace(wxT("iff"),
+ wxT("LoadIFF: %s %dx%d, planes=%d (%d cols), comp=%d"),
(fmt==ILBM_NORMAL) ? "Normal ILBM" :
(fmt==ILBM_HAM) ? "HAM ILBM" :
(fmt==ILBM_HAM8) ? "HAM8 ILBM" :
1<<bmhd_bitplanes, bmhd_compression);
if ((fmt==ILBM_NORMAL) || (fmt==ILBM_EHB) || (fmt==ILBM_HAM)) {
- wxLogTrace(_T("iff"),
- _T("Converting CMAP from normal ILBM CMAP"));
+ wxLogTrace(wxT("iff"),
+ wxT("Converting CMAP from normal ILBM CMAP"));
switch(fmt) {
case ILBM_NORMAL: colors = 1 << bmhd_bitplanes; break;
}
m_image->p = new byte[bmhd_width * bmhd_height * 3];
- byte *picptr = m_image->p;
+ byte *picptr = m_image->p;
if (!picptr) {
Destroy();
return wxIFF_MEMERR;
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);
}
}
} else if ((fmt == ILBM_NORMAL) || (fmt == ILBM_EHB)) {
if (fmt == ILBM_EHB) {
- wxLogTrace(_T("iff"), _T("Doubling CMAP for EHB mode"));
+ wxLogTrace(wxT("iff"), wxT("Doubling CMAP for EHB mode"));
for (int i=0; i<32; i++) {
pal[3*(i + 32) + 0] = pal[3*i + 0] >> 1;
m_image->h = height;
m_image->transparent = bmhd_transcol;
- wxLogTrace(_T("iff"), _T("Loaded IFF picture %s"),
+ wxLogTrace(wxT("iff"), wxT("Loaded IFF picture %s"),
truncated? "truncated" : "completely");
return (truncated? wxIFF_TRUNCATED : wxIFF_OK);
} else {
- wxLogTrace(_T("iff"), _T("Skipping unknown chunk '%c%c%c%c'"),
+ wxLogTrace(wxT("iff"), wxT("Skipping unknown chunk '%c%c%c%c'"),
*dataptr, *(dataptr+1), *(dataptr+2), *(dataptr+3));
dataptr = dataptr + 8 + chunkLen; // skip unknown chunk
wxOutputStream& WXUNUSED(stream), bool verbose)
{
if (verbose)
+ {
wxLogDebug(wxT("IFF: the handler is read-only!!"));
+ }
return false;
}
wxIFFDecoder decod(&stream);
return decod.CanRead();
+ // it's ok to modify the stream position here
}
#endif // wxUSE_STREAMS