- // 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);
-
- HPALETTE hOldPalette = 0;
- if (GetPalette().Ok())
- {
- hOldPalette = ::SelectPalette(memdc, (HPALETTE) GetPalette().GetHPALETTE(), FALSE);
- ::RealizePalette(memdc);
- }
-
- // 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;
-
- for( n=0; n<numDIB; n++ )
- {
- if( numDIB > 1 && n == numDIB-1 && hRemain > 0 )
- {
- // redefine height and size of the (possibly) last smaller DIB
- // memory is not reallocated
- height = hRemain;
- lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
- lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
- }
- ptbits = lpBits;
-
- for( j=0; j<height; j++ )
- {
- for( i=0; i<width; i++ )
- {
- *(ptbits++) = *(ptdata+2);
- *(ptbits++) = *(ptdata+1);
- *(ptbits++) = *(ptdata );
- ptdata += 3;
- }
- for( i=0; i< padding; i++ ) *(ptbits++) = 0;
- }
- ::StretchDIBits( memdc, 0, origin, width, height,\
- 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
- origin += height;
- // if numDIB = 1, lines below can also be used
- // hbitmap = CreateDIBitmap( hdc, &(lpDIBh->bmiHeader), CBM_INIT, lpBits, lpDIBh, DIB_RGB_COLORS );
- // The above line is equivalent to the following two lines.
- // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
- // ::SetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS);
- // or the following lines
- // hbitmap = ::CreateCompatibleBitmap( hdc, width, height );
- // HDC memdc = ::CreateCompatibleDC( hdc );
- // ::SelectObject( memdc, hbitmap);
- // ::SetDIBitsToDevice( memdc, 0, 0, width, height,
- // 0, 0, 0, height, (void *)lpBits, lpDIBh, DIB_RGB_COLORS);
- // ::SelectObject( memdc, 0 );
- // ::DeleteDC( memdc );
- }
- bitmap.SetHBITMAP( (WXHBITMAP) hbitmap );
-
- if (hOldPalette)
- SelectPalette(memdc, hOldPalette, FALSE);
-
- // similarly, created an mono-bitmap for the possible mask
- if( HasMask() )
- {
- hbitmap = ::CreateBitmap( (WORD)width, (WORD)bmpHeight, 1, 1, NULL );
- HGDIOBJ hbmpOld = ::SelectObject( memdc, hbitmap);
- if( numDIB == 1 ) height = bmpHeight;
- else height = sizeLimit/bytePerLine;
- lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
- lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
- origin = 0;
- unsigned char r = GetMaskRed();
- unsigned char g = GetMaskGreen();
- unsigned char b = GetMaskBlue();
- unsigned char zero = 0, one = 255;
- ptdata = data;
- for( n=0; n<numDIB; n++ )
- {
- if( numDIB > 1 && n == numDIB - 1 && hRemain > 0 )
- {
- // redefine height and size of the (possibly) last smaller DIB
- // memory is not reallocated
- height = hRemain;
- lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
- lpDIBh->bmiHeader.biSizeImage = bytePerLine*height;
- }
- ptbits = lpBits;
- for( int j=0; j<height; j++ )
- {
- for(i=0; i<width; i++ )
- {
- // was causing a code gen bug in cw : if( ( cr !=r) || (cg!=g) || (cb!=b) )
- unsigned char cr = (*(ptdata++)) ;
- unsigned char cg = (*(ptdata++)) ;
- unsigned char cb = (*(ptdata++)) ;
-
- if( ( cr !=r) || (cg!=g) || (cb!=b) )
- {
- *(ptbits++) = one;
- *(ptbits++) = one;
- *(ptbits++) = one;
- }
- else
- {
- *(ptbits++) = zero;
- *(ptbits++) = zero;
- *(ptbits++) = zero;
- }
- }
- for( i=0; i< padding; i++ ) *(ptbits++) = zero;
- }
- ::StretchDIBits( memdc, 0, origin, width, height,\
- 0, 0, width, height, lpBits, lpDIBh, DIB_RGB_COLORS, SRCCOPY);
- origin += height;
- }
- // create a wxMask object
- wxMask *mask = new wxMask();
- mask->SetMaskBitmap( (WXHBITMAP) hbitmap );
- bitmap.SetMask( mask );
- // It will be deleted when the wxBitmap object is deleted (as of 01/1999)
- /* The following can also be used but is slow to run
- wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
- wxMask *mask = new wxMask( bitmap, colour );
- bitmap.SetMask( mask );
- */
-
- ::SelectObject( memdc, hbmpOld );
- }
-
- // free allocated resources
- ::DeleteDC( memdc );
- ::ReleaseDC(NULL, hdc);
- free(lpDIBh);
- free(lpBits);
-
-#if WXWIN_COMPATIBILITY_2
- // check the wxBitmap object
- bitmap.GetBitmapData()->SetOk();
-#endif // WXWIN_COMPATIBILITY_2
-
- return bitmap;
-}
-
-wxImage::wxImage( const wxBitmap &bitmap )
-{
- // check the bitmap
- if( !bitmap.Ok() )
- {
- wxFAIL_MSG( wxT("invalid bitmap") );
- return;
- }
-
- // create an wxImage object
- int width = bitmap.GetWidth();
- int height = bitmap.GetHeight();
- Create( width, height );
- unsigned char *data = GetData();
- if( !data )
- {
- wxFAIL_MSG( wxT("could not allocate data for image") );
- return;
- }
-
- // calc the number of bytes per scanline and padding in the DIB
- int bytePerLine = width*3;
- int sizeDWORD = sizeof( DWORD );
- int lineBoundary = bytePerLine % sizeDWORD;
- int padding = 0;
- if( lineBoundary > 0 )
- {
- padding = sizeDWORD - lineBoundary;
- bytePerLine += padding;
- }
-
- // create a DIB header
- int headersize = sizeof(BITMAPINFOHEADER);
- BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
- if( !lpDIBh )
- {
- wxFAIL_MSG( wxT("could not allocate data for DIB header") );
- free( data );
- return;
- }
- // Fill in the DIB header
- lpDIBh->bmiHeader.biSize = headersize;
- lpDIBh->bmiHeader.biWidth = width;
- lpDIBh->bmiHeader.biHeight = -height;
- lpDIBh->bmiHeader.biSizeImage = bytePerLine * 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 data for DIB") );
- free( data );
- free( lpDIBh );
- return;
- }
-
- // copy data from the device-dependent bitmap to the DIB
- HDC hdc = ::GetDC(NULL);
- HBITMAP hbitmap;
- hbitmap = (HBITMAP) bitmap.GetHBITMAP();
- ::GetDIBits( hdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
-
- // copy DIB data into the wxImage object
- int i, j;
- unsigned char *ptdata = data;
- unsigned char *ptbits = lpBits;
- for( i=0; i<height; i++ )
- {
- for( j=0; j<width; j++ )
- {
- *(ptdata++) = *(ptbits+2);
- *(ptdata++) = *(ptbits+1);
- *(ptdata++) = *(ptbits );
- ptbits += 3;
- }
- ptbits += padding;
- }
-
- // similarly, set data according to the possible mask bitmap
- if( bitmap.GetMask() && bitmap.GetMask()->GetMaskBitmap() )
- {
- hbitmap = (HBITMAP) bitmap.GetMask()->GetMaskBitmap();
- // memory DC created, color set, data copied, and memory DC deleted
- HDC memdc = ::CreateCompatibleDC( hdc );
- ::SetTextColor( memdc, RGB( 0, 0, 0 ) );
- ::SetBkColor( memdc, RGB( 255, 255, 255 ) );
- ::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
- ::DeleteDC( memdc );
- // background color set to RGB(16,16,16) in consistent with wxGTK
- unsigned char r=16, g=16, b=16;
- ptdata = data;
- ptbits = lpBits;
- for( i=0; i<height; i++ )
- {
- for( j=0; j<width; j++ )
- {
- if( *ptbits != 0 )
- ptdata += 3;
- else
- {
- *(ptdata++) = r;
- *(ptdata++) = g;
- *(ptdata++) = b;
- }
- ptbits += 3;
- }
- ptbits += padding;
- }
- SetMaskColour( r, g, b );
- SetMask( TRUE );
- }
- else
- {
- SetMask( FALSE );
- }
- // free allocated resources
- ::ReleaseDC(NULL, hdc);
- free(lpDIBh);
- free(lpBits);
-}
-
-#endif
-
-#ifdef __WXMAC__
-
-#ifdef __UNIX__
- #include <ApplicationServices/ApplicationServices.h>
-#else
- #include <PictUtils.h>
-#endif
-
-wxBitmap wxImage::ConvertToBitmap() const
-{
- // width and height of the device-dependent bitmap
- int width = GetWidth();
- int height = GetHeight();
-
- // Create picture
-
- wxBitmap bitmap;
-
- wxCHECK_MSG( Ok(), bitmap, wxT("invalid image") );
-
- bitmap.Create( width , height , wxDisplayDepth() ) ;
- wxBitmap maskBitmap( width, height, 1);
-
- CGrafPtr origPort ;
- GDHandle origDevice ;
-
- LockPixels( GetGWorldPixMap(bitmap.GetHBITMAP()) );
- LockPixels( GetGWorldPixMap(maskBitmap.GetHBITMAP()) );
-
- GetGWorld( &origPort , &origDevice ) ;
- SetGWorld( bitmap.GetHBITMAP() , NULL ) ;
-
- // Render image
- wxColour rgb, maskcolor(GetMaskRed(), GetMaskGreen(), GetMaskBlue());
- RGBColor color;
- RGBColor white = { 0xffff, 0xffff, 0xffff };
- RGBColor black = { 0 , 0 , 0 };
-
- register unsigned char* data = GetData();
-
- int index = 0;
- for (int y = 0; y < height; y++)
- {
- for (int x = 0; x < width; x++)
- {
- rgb.Set(data[index++], data[index++], data[index++]);
- color = rgb.GetPixel();
- SetCPixel( x , y , &color ) ;
- if (HasMask())
- {
- SetGWorld(maskBitmap.GetHBITMAP(), NULL);
- if (rgb == maskcolor) {
- SetCPixel(x,y, &white);
- }
- else {
- SetCPixel(x,y, &black);
- }
- SetGWorld(bitmap.GetHBITMAP(), NULL);
- }
- }
- } // for height
-
- // Create mask
- if ( HasMask() ) {
- wxMask *mask = new wxMask( maskBitmap );
- }
-
- UnlockPixels( GetGWorldPixMap(bitmap.GetHBITMAP()) );
- UnlockPixels( GetGWorldPixMap(maskBitmap.GetHBITMAP()) );
- SetGWorld( origPort, origDevice );
-
- return bitmap;
-}
-
-wxImage::wxImage( const wxBitmap &bitmap )
-{
- // check the bitmap
- wxCHECK_RET( bitmap.Ok(), wxT("Invalid bitmap") );
-
- // create an wxImage object
- int width = bitmap.GetWidth();
- int height = bitmap.GetHeight();
- Create( width, height );
-
- unsigned char *data = GetData();
-
- wxCHECK_RET( data, wxT("Could not allocate data for image") );
-
- WXHBITMAP origPort;
- GDHandle origDevice;
- int index;
- RGBColor color;
- // background color set to RGB(16,16,16) in consistent with wxGTK
- unsigned char mask_r=16, mask_g=16, mask_b=16;
- SInt16 r,g,b;
- wxMask *mask = bitmap.GetMask();
-
- GetGWorld( &origPort, &origDevice );
- LockPixels(GetGWorldPixMap(bitmap.GetHBITMAP()));
- SetGWorld( bitmap.GetHBITMAP(), NULL);
-
- // Copy data into image
- index = 0;
- for (int yy = 0; yy < height; yy++)
- {
- for (int xx = 0; xx < width; xx++)
- {
- GetCPixel(xx,yy, &color);
- r = ((color.red ) >> 8);
- g = ((color.green ) >> 8);
- b = ((color.blue ) >> 8);
- data[index ] = r;
- data[index + 1] = g;
- data[index + 2] = b;
- if (mask)
- {
- if (mask->PointMasked(xx,yy))
- {
- data[index ] = mask_r;
- data[index + 1] = mask_g;
- data[index + 2] = mask_b;
- }
- }
- index += 3;
- }
- }
- if (mask)
- {
- SetMaskColour( mask_r, mask_g, mask_b );
- SetMask( true );
- }
-
- // Free resources
- UnlockPixels(GetGWorldPixMap(bitmap.GetHBITMAP()));
- SetGWorld(origPort, origDevice);
-}
-
-#endif
-
-//-----------------------------------------------------------------------------
-// GTK conversion routines
-//-----------------------------------------------------------------------------
-
-#ifdef __WXGTK__
-
-#include <gtk/gtk.h>
-#include <gdk/gdk.h>
-#include <gdk/gdkx.h>
-
-#if (GTK_MINOR_VERSION > 0)
-#include <gdk/gdkrgb.h>
-#endif
-
-extern GtkWidget *wxRootWindow;
-
-wxBitmap wxImage::ConvertToMonoBitmap( unsigned char red, unsigned char green, unsigned char blue )
-{
- wxBitmap bitmap;
-
- wxCHECK_MSG( Ok(), bitmap, wxT("invalid image") );
-
- int width = GetWidth();
- int height = GetHeight();
-
- bitmap.SetHeight( height );
- bitmap.SetWidth( width );
-
- bitmap.SetBitmap( gdk_pixmap_new( wxRootWindow->window, width, height, 1 ) );
-
- bitmap.SetDepth( 1 );
-
- GdkVisual *visual = gdk_window_get_visual( wxRootWindow->window );
- wxASSERT( visual );
-
- // Create picture image
-
- unsigned char *data_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
-
- GdkImage *data_image =
- gdk_image_new_bitmap( visual, data_data, width, height );
-
- // Create mask image
-
- GdkImage *mask_image = (GdkImage*) NULL;
-
- if (HasMask())