- wxFileInputStream stream(filename);
- wxBufferedInputStream bstream( stream );
- return LoadFile(bstream, type);
- }
- else
- {
- wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() );
-
- return FALSE;
- }
-#else // !wxUSE_STREAMS
- return FALSE;
-#endif // wxUSE_STREAMS
-}
-
-bool wxImage::LoadFile( const wxString& filename, const wxString& mimetype )
-{
-#if wxUSE_STREAMS
- if (wxFileExists(filename))
- {
- wxFileInputStream stream(filename);
- wxBufferedInputStream bstream( stream );
- return LoadFile(bstream, mimetype);
- }
- else
- {
- wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() );
-
- return FALSE;
- }
-#else // !wxUSE_STREAMS
- return FALSE;
-#endif // wxUSE_STREAMS
-}
-
-bool wxImage::SaveFile( const wxString& filename, int type )
-{
-#if wxUSE_STREAMS
- wxFileOutputStream stream(filename);
-
- if ( stream.LastError() == wxStream_NOERROR )
- {
- wxBufferedOutputStream bstream( stream );
- return SaveFile(bstream, type);
- }
- else
-#endif // wxUSE_STREAMS
- return FALSE;
-}
-
-bool wxImage::SaveFile( const wxString& filename, const wxString& mimetype )
-{
-#if wxUSE_STREAMS
- wxFileOutputStream stream(filename);
-
- if ( stream.LastError() == wxStream_NOERROR )
- {
- wxBufferedOutputStream bstream( stream );
- return SaveFile(bstream, mimetype);
- }
- else
-#endif // wxUSE_STREAMS
- return FALSE;
-}
-
-bool wxImage::CanRead( const wxString &name )
-{
-#if wxUSE_STREAMS
- wxFileInputStream stream(name);
- return CanRead(stream);
-#else
- return FALSE;
-#endif
-}
-
-#if wxUSE_STREAMS
-
-bool wxImage::CanRead( wxInputStream &stream )
-{
- wxList &list=GetHandlers();
-
- for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() )
- {
- wxImageHandler *handler=(wxImageHandler*)node->GetData();
- if (handler->CanRead( stream ))
- return TRUE;
- }
-
- return FALSE;
-}
-
-bool wxImage::LoadFile( wxInputStream& stream, long type )
-{
- UnRef();
-
- m_refData = new wxImageRefData;
-
- wxImageHandler *handler;
-
- if (type==wxBITMAP_TYPE_ANY)
- {
- wxList &list=GetHandlers();
-
- for ( wxList::Node *node = list.GetFirst(); node; node = node->GetNext() )
- {
- handler=(wxImageHandler*)node->GetData();
- if (handler->CanRead( stream ))
- return handler->LoadFile( this, stream );
-
- }
-
- wxLogWarning( _("No handler found for image type.") );
- return FALSE;
- }
-
- handler = FindHandler(type);
-
- if (handler == NULL)
- {
- wxLogWarning( _("No image handler for type %d defined."), type );
-
- return FALSE;
- }
-
- return handler->LoadFile( this, stream );
-}
-
-bool wxImage::LoadFile( wxInputStream& stream, const wxString& mimetype )
-{
- UnRef();
-
- m_refData = new wxImageRefData;
-
- wxImageHandler *handler = FindHandlerMime(mimetype);
-
- if (handler == NULL)
- {
- wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
-
- return FALSE;
- }
-
- return handler->LoadFile( this, stream );
-}
-
-bool wxImage::SaveFile( wxOutputStream& stream, int type )
-{
- wxCHECK_MSG( Ok(), FALSE, wxT("invalid image") );
-
- wxImageHandler *handler = FindHandler(type);
-
- if (handler == NULL)
- {
- wxLogWarning( _("No image handler for type %d defined."), type );
-
- return FALSE;
- }
-
- return handler->SaveFile( this, stream );
-}
-
-bool wxImage::SaveFile( wxOutputStream& stream, const wxString& mimetype )
-{
- wxCHECK_MSG( Ok(), FALSE, wxT("invalid image") );
-
- wxImageHandler *handler = FindHandlerMime(mimetype);
-
- if (handler == NULL)
- {
- wxLogWarning( _("No image handler for type %s defined."), mimetype.GetData() );
-
- return FALSE;
- }
-
- return handler->SaveFile( this, stream );
-}
-#endif // wxUSE_STREAMS
-
-void wxImage::AddHandler( wxImageHandler *handler )
-{
- // make sure that the memory will be freed at the program end
- sm_handlers.DeleteContents(TRUE);
-
- sm_handlers.Append( handler );
-}
-
-void wxImage::InsertHandler( wxImageHandler *handler )
-{
- // make sure that the memory will be freed at the program end
- sm_handlers.DeleteContents(TRUE);
-
- sm_handlers.Insert( handler );
-}
-
-bool wxImage::RemoveHandler( const wxString& name )
-{
- wxImageHandler *handler = FindHandler(name);
- if (handler)
- {
- sm_handlers.DeleteObject(handler);
- return TRUE;
- }
- else
- return FALSE;
-}
-
-wxImageHandler *wxImage::FindHandler( const wxString& name )
-{
- wxNode *node = sm_handlers.First();
- while (node)
- {
- wxImageHandler *handler = (wxImageHandler*)node->Data();
- if (handler->GetName().Cmp(name) == 0) return handler;
-
- node = node->Next();
- }
- return (wxImageHandler *)NULL;
-}
-
-wxImageHandler *wxImage::FindHandler( const wxString& extension, long bitmapType )
-{
- wxNode *node = sm_handlers.First();
- while (node)
- {
- wxImageHandler *handler = (wxImageHandler*)node->Data();
- if ( (handler->GetExtension().Cmp(extension) == 0) &&
- (bitmapType == -1 || handler->GetType() == bitmapType) )
- return handler;
- node = node->Next();
- }
- return (wxImageHandler*)NULL;
-}
-
-wxImageHandler *wxImage::FindHandler( long bitmapType )
-{
- wxNode *node = sm_handlers.First();
- while (node)
- {
- wxImageHandler *handler = (wxImageHandler *)node->Data();
- if (handler->GetType() == bitmapType) return handler;
- node = node->Next();
- }
- return NULL;
-}
-
-wxImageHandler *wxImage::FindHandlerMime( const wxString& mimetype )
-{
- wxNode *node = sm_handlers.First();
- while (node)
- {
- wxImageHandler *handler = (wxImageHandler *)node->Data();
- if (handler->GetMimeType().IsSameAs(mimetype, FALSE)) return handler;
- node = node->Next();
- }
- return NULL;
-}
-
-void wxImage::InitStandardHandlers()
-{
- AddHandler( new wxBMPHandler );
-}
-
-void wxImage::CleanUpHandlers()
-{
- wxNode *node = sm_handlers.First();
- while (node)
- {
- wxImageHandler *handler = (wxImageHandler *)node->Data();
- wxNode *next = node->Next();
- delete handler;
- delete node;
- node = next;
- }
-}
-
-//-----------------------------------------------------------------------------
-// wxImageHandler
-//-----------------------------------------------------------------------------
-
-IMPLEMENT_ABSTRACT_CLASS(wxImageHandler,wxObject)
-
-#if wxUSE_STREAMS
-bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream), bool WXUNUSED(verbose), int WXUNUSED(index) )
-{
- return FALSE;
-}
-
-bool wxImageHandler::SaveFile( wxImage *WXUNUSED(image), wxOutputStream& WXUNUSED(stream), bool WXUNUSED(verbose) )
-{
- return FALSE;
-}
-
-int wxImageHandler::GetImageCount( wxInputStream& WXUNUSED(stream) )
-{
- return 1;
-}
-
-bool wxImageHandler::CanRead( const wxString& name )
-{
- if (wxFileExists(name))
- {
- wxFileInputStream stream(name);
- return CanRead(stream);
- }
-
- else {
- wxLogError( _("Can't check image format of file '%s': file does not exist."), name.c_str() );
-
- return FALSE;
- }
-// return FALSE;
-}
-
-#endif // wxUSE_STREAMS
-
-//-----------------------------------------------------------------------------
-// MSW conversion routines
-//-----------------------------------------------------------------------------
-
-#ifdef __WXMSW__
-
-wxBitmap wxImage::ConvertToBitmap() const
-{
- if ( !Ok() )
- return wxNullBitmap;
-
- // sizeLimit is the MS upper limit for the DIB size
-#ifdef WIN32
- int sizeLimit = 1024*768*3;
-#else
- int sizeLimit = 0x7fff ;
-#endif
-
- // width and height of the device-dependent bitmap
- int width = GetWidth();
- int bmpHeight = GetHeight();
-
- // calc the number of bytes per scanline and padding
- int bytePerLine = width*3;
- int sizeDWORD = sizeof( DWORD );
- int lineBoundary = bytePerLine % sizeDWORD;
- int padding = 0;
- if( lineBoundary > 0 )
- {
- padding = sizeDWORD - lineBoundary;
- bytePerLine += padding;
- }
- // calc the number of DIBs and heights of DIBs
- int numDIB = 1;
- int hRemain = 0;
- int height = sizeLimit/bytePerLine;
- if( height >= bmpHeight )
- height = bmpHeight;
- else
- {
- numDIB = bmpHeight / height;
- hRemain = bmpHeight % height;
- if( hRemain >0 ) numDIB++;
- }
-
- // set bitmap parameters
- wxBitmap bitmap;
- wxCHECK_MSG( Ok(), bitmap, wxT("invalid image") );
- bitmap.SetWidth( width );
- bitmap.SetHeight( bmpHeight );
- bitmap.SetDepth( wxDisplayDepth() );
-
- // create a DIB header
- int headersize = sizeof(BITMAPINFOHEADER);
- BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
- wxCHECK_MSG( lpDIBh, bitmap, wxT("could not allocate memory for DIB header") );
- // Fill in the DIB header
- lpDIBh->bmiHeader.biSize = headersize;
- lpDIBh->bmiHeader.biWidth = (DWORD)width;
- lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
- lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
- // the general formula for biSizeImage:
- // ( ( ( ((DWORD)width*24) +31 ) & ~31 ) >> 3 ) * height;
- lpDIBh->bmiHeader.biPlanes = 1;
- lpDIBh->bmiHeader.biBitCount = 24;
- lpDIBh->bmiHeader.biCompression = BI_RGB;
- lpDIBh->bmiHeader.biClrUsed = 0;
- // These seem not really needed for our purpose here.
- lpDIBh->bmiHeader.biClrImportant = 0;
- lpDIBh->bmiHeader.biXPelsPerMeter = 0;
- lpDIBh->bmiHeader.biYPelsPerMeter = 0;
- // memory for DIB data
- unsigned char *lpBits;
- lpBits = (unsigned char *)malloc( lpDIBh->bmiHeader.biSizeImage );
- if( !lpBits )
- {
- wxFAIL_MSG( wxT("could not allocate memory for DIB") );
- free( lpDIBh );
- return bitmap;
- }
-
- // create and set the device-dependent bitmap
- HDC hdc = ::GetDC(NULL);
- HDC memdc = ::CreateCompatibleDC( hdc );
- HBITMAP hbitmap;
- hbitmap = ::CreateCompatibleBitmap( hdc, width, bmpHeight );
- ::SelectObject( memdc, hbitmap);
-
- // copy image data into DIB data and then into DDB (in a loop)
- unsigned char *data = GetData();
- int i, j, n;
- int origin = 0;
- unsigned char *ptdata = data;
- unsigned char *ptbits;