X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/63f8abca79edfcf92641b21b0a16687ac84d152b..8244507f687f59a6493948cc4482ee19f82e66c5:/src/common/imagiff.cpp diff --git a/src/common/imagiff.cpp b/src/common/imagiff.cpp index bd6fb87cfe..16e0348a54 100644 --- a/src/common/imagiff.cpp +++ b/src/common/imagiff.cpp @@ -1,36 +1,37 @@ ///////////////////////////////////////////////////////////////////////////// -// Name: imagiff.h +// Name: src/common/imagiff.h // Purpose: wxImage handler for Amiga IFF images -// Author: Steffen Gutmann +// Author: Steffen Gutmann, Thomas Meyer // RCS-ID: $Id$ // Copyright: (c) Steffen Gutmann, 2002 // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// -// parts of the source are based on xviff by Thomas Meyer -// Permission for use in wxWindows has been gratefully given. - -#ifdef __GNUG__ -#pragma implementation "imagiff.h" -#endif +// Parts of this source are based on the iff loading algorithm found +// in xviff.c. Permission by the original author, Thomas Meyer, and +// by the author of xv, John Bradley for using the iff loading part +// in wxWidgets has been gratefully given. // For compilers that support precompilation, includes "wx.h". #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" +#endif // wxUSE_PALETTE #include #include @@ -66,7 +67,7 @@ public: unsigned int w; /* width */ unsigned int h; /* height */ int transparent; /* transparent color (-1 = none) */ - int colors; /* number of colors */ + int colors; /* number of colors */ unsigned char *p; /* bitmap */ unsigned char *pal; /* palette */ @@ -77,10 +78,10 @@ public: class WXDLLEXPORT wxIFFDecoder { private: - IFFImage *m_image; // image data - wxInputStream *m_f; // input stream - unsigned char *databuf; - unsigned char *picptr; + IFFImage *m_image; // image data + wxInputStream *m_f; // input stream + unsigned char *databuf; + unsigned char *picptr; unsigned char *decomp_mem; void Destroy(); @@ -97,7 +98,10 @@ public: // 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; }; @@ -117,12 +121,9 @@ wxIFFDecoder::wxIFFDecoder(wxInputStream *s) 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); } //--------------------------------------------------------------------------- @@ -140,7 +141,7 @@ bool wxIFFDecoder::ConvertToImage(wxImage *image) const image->Create(GetWidth(), GetHeight()); if (!image->Ok()) - return FALSE; + return false; unsigned char *pal = GetPalette(); unsigned char *src = GetData(); @@ -169,7 +170,7 @@ bool wxIFFDecoder::ConvertToImage(wxImage *image) const image->SetMaskColour(255, 0, 255); } else - image->SetMask(FALSE); + image->SetMask(false); #if wxUSE_PALETTE if (pal && colors > 0) @@ -201,7 +202,7 @@ bool wxIFFDecoder::ConvertToImage(wxImage *image) const dst[2] = src[2]; } - return TRUE; + return true; } @@ -224,34 +225,34 @@ int wxIFFDecoder::GetTransparentColour() const { return m_image->transparent; } // // CanRead: -// Returns TRUE if the file looks like a valid IFF, FALSE otherwise. +// Returns true if the file looks like a valid IFF, false otherwise. // bool wxIFFDecoder::CanRead() { - unsigned char buf[12] = ""; + unsigned char buf[12]; - m_f->Read(buf, 12); - m_f->SeekI(-12, wxFromCurrent); + if ( !m_f->Read(buf, WXSIZEOF(buf)) ) + return false; - return (memcmp(buf, "FORM", 4) == 0 && memcmp(buf+8, "ILBM", 4) == 0); + return (memcmp(buf, "FORM", 4) == 0) && (memcmp(buf+8, "ILBM", 4) == 0); } // ReadIFF: // Based on xv source code by Thomas Meyer -// Permission for use in wxWindows has been gratefully given. +// Permission for use in wxWidgets has been gratefully given. typedef unsigned char byte; #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) @@ -326,20 +327,27 @@ int wxIFFDecoder::ReadIFF() m_image = new IFFImage(); if (m_image == 0) { - Destroy(); - return wxIFF_MEMERR; + Destroy(); + return wxIFF_MEMERR; } // compute file length - off_t currentPos = m_f->TellI(); - m_f->SeekI(0, wxFromEnd); + wxFileOffset currentPos = m_f->TellI(); + 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); @@ -350,25 +358,25 @@ int wxIFFDecoder::ReadIFF() // 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 @@ -376,23 +384,16 @@ int wxIFFDecoder::ReadIFF() // 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); @@ -403,7 +404,7 @@ int wxIFFDecoder::ReadIFF() 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 @@ -416,8 +417,7 @@ int wxIFFDecoder::ReadIFF() 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]; @@ -434,10 +434,9 @@ int wxIFFDecoder::ReadIFF() } } - wxLogTrace(_T("iff"), _T("Read %d colors from IFF file."), + wxLogTrace(wxT("iff"), wxT("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) { @@ -476,8 +475,7 @@ int wxIFFDecoder::ReadIFF() 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. @@ -498,8 +496,8 @@ int wxIFFDecoder::ReadIFF() } } - 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" : @@ -509,8 +507,8 @@ int wxIFFDecoder::ReadIFF() 1<= 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); } @@ -644,7 +642,7 @@ int wxIFFDecoder::ReadIFF() } } 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; @@ -701,12 +699,12 @@ int wxIFFDecoder::ReadIFF() 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 @@ -727,7 +725,7 @@ IMPLEMENT_DYNAMIC_CLASS(wxIFFHandler, wxImageHandler) #if wxUSE_STREAMS -bool wxIFFHandler::LoadFile(wxImage *image, wxInputStream& stream, +bool wxIFFHandler::LoadFile(wxImage *image, wxInputStream& stream, bool verbose, int WXUNUSED(index)) { wxIFFDecoder *decod; @@ -755,7 +753,7 @@ bool wxIFFHandler::LoadFile(wxImage *image, wxInputStream& stream, } } delete decod; - return FALSE; + return false; } if ((error == wxIFF_TRUNCATED) && verbose) @@ -774,21 +772,19 @@ bool wxIFFHandler::SaveFile(wxImage * WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool verbose) { if (verbose) + { wxLogDebug(wxT("IFF: the handler is read-only!!")); + } - return FALSE; + return false; } bool wxIFFHandler::DoCanRead(wxInputStream& stream) { - wxIFFDecoder *decod; - bool ok; - - decod = new wxIFFDecoder(&stream); - ok = decod->CanRead(); - delete decod; + wxIFFDecoder decod(&stream); - return ok; + return decod.CanRead(); + // it's ok to modify the stream position here } #endif // wxUSE_STREAMS