// For memcpy
#include <string.h>
+#include <math.h>
#ifdef __SALFORDC__
#undef FAR
wxImageRefData::~wxImageRefData()
{
- if (m_data)
+ if (m_data)
free( m_data );
}
#define M_IMGDATA ((wxImageRefData *)m_refData)
-#if !USE_SHARED_LIBRARIES
IMPLEMENT_DYNAMIC_CLASS(wxImage, wxObject)
-#endif
wxImage::wxImage()
{
return image;
}
+void wxImage::Replace( unsigned char r1, unsigned char g1, unsigned char b1,
+ unsigned char r2, unsigned char g2, unsigned char b2 )
+{
+ wxCHECK_RET( Ok(), wxT("invalid image") );
+
+ char unsigned *data = GetData();
+
+ const int w = GetWidth();
+ const int h = GetHeight();
+
+ for (int j = 0; j < h; j++)
+ for (int i = 0; i < w; i++)
+ {
+ if ((data[0] == r1) && (data[1] == g1) && (data[2] == b1))
+ {
+ data[0] = r2;
+ data[1] = g2;
+ data[2] = b2;
+ }
+ data += 3;
+ }
+}
+
void wxImage::SetRGB( int x, int y, unsigned char r, unsigned char g, unsigned char b )
{
wxCHECK_RET( Ok(), wxT("invalid image") );
if (wxFileExists(filename))
{
wxFileInputStream stream(filename);
- wxBufferedInputStream bstream( stream );
+ wxBufferedInputStream bstream( stream );
return LoadFile(bstream, type);
}
- else
+ else
{
wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() );
if (wxFileExists(filename))
{
wxFileInputStream stream(filename);
- wxBufferedInputStream bstream( stream );
+ wxBufferedInputStream bstream( stream );
return LoadFile(bstream, mimetype);
}
- else
+ else
{
wxLogError( _("Can't load image from file '%s': file does not exist."), filename.c_str() );
if ( stream.LastError() == wxStream_NOERROR )
{
- wxBufferedOutputStream bstream( stream );
+ wxBufferedOutputStream bstream( stream );
return SaveFile(bstream, type);
}
else
if ( stream.LastError() == wxStream_NOERROR )
{
- wxBufferedOutputStream bstream( stream );
+ wxBufferedOutputStream bstream( stream );
return SaveFile(bstream, mimetype);
}
else
{
wxImageHandler *handler=(wxImageHandler*)node->GetData();
if (handler->CanRead( stream ))
- return TRUE;
+ return TRUE;
}
return FALSE;
// wxImageHandler
//-----------------------------------------------------------------------------
-#if !USE_SHARED_LIBRARIES
IMPLEMENT_ABSTRACT_CLASS(wxImageHandler,wxObject)
-#endif
#if wxUSE_STREAMS
bool wxImageHandler::LoadFile( wxImage *WXUNUSED(image), wxInputStream& WXUNUSED(stream), bool WXUNUSED(verbose), int WXUNUSED(index) )
return FALSE;
}
- return FALSE;
+// return FALSE;
}
#endif // wxUSE_STREAMS
// create a DIB header
int headersize = sizeof(BITMAPINFOHEADER);
- LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
+ 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;
if( HasMask() )
{
hbitmap = ::CreateBitmap( (WORD)width, (WORD)bmpHeight, 1, 1, NULL );
- ::SelectObject( memdc, hbitmap);
+ HGDIOBJ hbmpOld = ::SelectObject( memdc, hbitmap);
if( numDIB == 1 ) height = bmpHeight;
else height = sizeLimit/bytePerLine;
lpDIBh->bmiHeader.biHeight = (DWORD)(-height);
{
for(i=0; i<width; i++ )
{
- if( (*(ptdata++)!=r) | (*(ptdata++)!=g) | (*(ptdata++)!=b) )
+ // 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;
wxMask *mask = new wxMask( bitmap, colour );
bitmap.SetMask( mask );
*/
+
+ ::SelectObject( memdc, hbmpOld );
}
// free allocated resources
- ::SelectObject( memdc, 0 );
::DeleteDC( memdc );
::ReleaseDC(NULL, hdc);
free(lpDIBh);
// create a DIB header
int headersize = sizeof(BITMAPINFOHEADER);
- LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
+ BITMAPINFO *lpDIBh = (BITMAPINFO *) malloc( headersize );
if( !lpDIBh )
{
wxFAIL_MSG( wxT("could not allocate data for DIB header") );
int height = GetHeight();
// Create picture
-
+
wxBitmap bitmap( width , height , wxDisplayDepth() ) ;
-
+
// Create mask
-
+
if (HasMask())
{
- /*
+ /*
unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
-
+
mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
-
+
wxMask *mask = new wxMask();
mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
-
+
bitmap.SetMask( mask );
*/
}
-
+
// Render
-
+
int r_mask = GetMaskRed();
int g_mask = GetMaskGreen();
int b_mask = GetMaskBlue();
-
- CGrafPtr origPort ;
- GDHandle origDevice ;
-
- GetGWorld( &origPort , &origDevice ) ;
- SetGWorld( bitmap.GetHBITMAP() , NULL ) ;
+
+ CGrafPtr origPort ;
+ GDHandle origDevice ;
+
+ GetGWorld( &origPort , &origDevice ) ;
+ SetGWorld( bitmap.GetHBITMAP() , NULL ) ;
register unsigned char* data = GetData();
-
+
int index = 0;
for (int y = 0; y < height; y++)
{
-#if 0
- unsigned char lastr = 0 ;
- unsigned char lastg = 0 ;
- unsigned char lastb = 0 ;
- RGBColor lastcolor ;
-
- MoveTo( 0 , y ) ;
- for (int x = 0; x < width; x++)
- {
- unsigned char r = data[index++];
- unsigned char g = data[index++];
- unsigned char b = data[index++];
-
- if ( r != lastr || g != lastg || b != lastb )
- {
- lastcolor.red = ( lastr << 8 ) + lastr ;
- lastcolor.green = ( lastg << 8 ) + lastg ;
- lastcolor.blue = ( lastb << 8 ) + lastb ;
- RGBForeColor( &lastcolor ) ;
- LineTo( x , y ) ;
- lastr = r ;
- lastg = g ;
- lastb = b ;
- }
- } // for width
- lastcolor.red = ( lastr << 8 ) + lastr ;
- lastcolor.green = ( lastg << 8 ) + lastg ;
- lastcolor.blue = ( lastb << 8 ) + lastb ;
- RGBForeColor( &lastcolor ) ;
- LineTo( width - 1 , y ) ;
-#else
for (int x = 0; x < width; x++)
{
- unsigned char r = data[index++];
- unsigned char g = data[index++];
- unsigned char b = data[index++];
- RGBColor color ;
- color.red = ( r << 8 ) + r ;
- color.green = ( g << 8 ) + g ;
- color.blue = ( b << 8 ) + b ;
- SetCPixel( x , y , &color ) ;
- }
-#endif
+ unsigned char r = data[index++];
+ unsigned char g = data[index++];
+ unsigned char b = data[index++];
+ RGBColor color ;
+ color.red = ( r << 8 ) + r ;
+ color.green = ( g << 8 ) + g ;
+ color.blue = ( b << 8 ) + b ;
+ SetCPixel( x , y , &color ) ;
+ }
} // for height
- SetGWorld( origPort , origDevice ) ;
-
+ SetGWorld( origPort , origDevice ) ;
+
+ if ( HasMask() )
+ {
+ wxColour colour( GetMaskRed(), GetMaskGreen(), GetMaskBlue());
+ wxMask *mask = new wxMask( bitmap, colour );
+ bitmap.SetMask( mask );
+ }
return bitmap;
-
+
}
wxImage::wxImage( const wxBitmap &bitmap )
wxFAIL_MSG( "invalid bitmap" );
return;
}
-
+
// create an wxImage object
int width = bitmap.GetWidth();
int height = bitmap.GetHeight();
- Create( width, height );
+ Create( width, height );
/*
unsigned char *data = GetData();
if( !data )
wxFAIL_MSG( "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 );
div_t lineBoundary = div( bytePerLine, sizeDWORD );
int padding = 0;
- if( lineBoundary.rem > 0 )
+ if( lineBoundary.rem > 0 )
{
padding = sizeDWORD - lineBoundary.rem;
bytePerLine += padding;
}
-
+
// create a DIB header
int headersize = sizeof(BITMAPINFOHEADER);
LPBITMAPINFO lpDIBh = (BITMAPINFO *) malloc( headersize );
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;
ptbits += 3;
}
ptbits += padding;
- }
-
+ }
+
// similarly, set data according to the possible mask bitmap
if( bitmap.GetMask() && bitmap.GetMask()->GetMaskBitmap() )
{
::SetTextColor( memdc, RGB( 0, 0, 0 ) );
::SetBkColor( memdc, RGB( 255, 255, 255 ) );
::GetDIBits( memdc, hbitmap, 0, height, lpBits, lpDIBh, DIB_RGB_COLORS );
- ::DeleteDC( memdc );
+ ::DeleteDC( memdc );
// background color set to RGB(16,16,16) in consistent with wxGTK
- unsigned char r=16, g=16, b=16;
+ unsigned char r=16, g=16, b=16;
ptdata = data;
ptbits = lpBits;
for( i=0; i<height; i++ )
ptbits += 3;
}
ptbits += padding;
- }
+ }
SetMaskColour( r, g, b );
SetMask( TRUE );
- }
+ }
else
{
SetMask( FALSE );
- }
- // free allocated resources
- ::ReleaseDC(NULL, hdc);
+ }
+ // free allocated resources
+ ::ReleaseDC(NULL, hdc);
free(lpDIBh);
free(lpBits);
*/
#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())
+ {
+ unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
+
+ mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
+
+ wxMask *mask = new wxMask();
+ mask->m_bitmap = gdk_pixmap_new( wxRootWindow->window, width, height, 1 );
+
+ bitmap.SetMask( mask );
+ }
+
+ int r_mask = GetMaskRed();
+ int g_mask = GetMaskGreen();
+ int b_mask = GetMaskBlue();
+
+ unsigned char* data = GetData();
+
+ int index = 0;
+ for (int y = 0; y < height; y++)
+ {
+ for (int x = 0; x < width; x++)
+ {
+ int r = data[index];
+ index++;
+ int g = data[index];
+ index++;
+ int b = data[index];
+ index++;
+
+ if (HasMask())
+ {
+ if ((r == r_mask) && (b == b_mask) && (g == g_mask))
+ gdk_image_put_pixel( mask_image, x, y, 1 );
+ else
+ gdk_image_put_pixel( mask_image, x, y, 0 );
+ }
+
+ if ((r == red) && (b == blue) && (g == green))
+ gdk_image_put_pixel( data_image, x, y, 1 );
+ else
+ gdk_image_put_pixel( data_image, x, y, 0 );
+
+ } // for
+ } // for
+
+ // Blit picture
+
+ GdkGC *data_gc = gdk_gc_new( bitmap.GetBitmap() );
+
+ gdk_draw_image( bitmap.GetBitmap(), data_gc, data_image, 0, 0, 0, 0, width, height );
+
+ gdk_image_destroy( data_image );
+ gdk_gc_unref( data_gc );
+
+ // Blit mask
+
+ if (HasMask())
+ {
+ GdkGC *mask_gc = gdk_gc_new( bitmap.GetMask()->GetBitmap() );
+
+ gdk_draw_image( bitmap.GetMask()->GetBitmap(), mask_gc, mask_image, 0, 0, 0, 0, width, height );
+
+ gdk_image_destroy( mask_image );
+ gdk_gc_unref( mask_gc );
+ }
+
+ return bitmap;
+}
+
+
wxBitmap wxImage::ConvertToBitmap() const
{
wxBitmap bitmap;
bitmap.SetHeight( height );
bitmap.SetWidth( width );
- bitmap.SetPixmap( gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, -1 ) );
+ bitmap.SetPixmap( gdk_pixmap_new( wxRootWindow->window, width, height, -1 ) );
- // Retrieve depth
+ // Retrieve depth
- GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() );
- if (visual == NULL) visual = gdk_visual_get_system();
+ GdkVisual *visual = gdk_window_get_visual( wxRootWindow->window );
+ wxASSERT( visual );
+
int bpp = visual->depth;
bitmap.SetDepth( bpp );
// Create picture image
GdkImage *data_image =
- gdk_image_new( GDK_IMAGE_FASTEST, gdk_visual_get_system(), width, height );
+ gdk_image_new( GDK_IMAGE_FASTEST, visual, width, height );
// Create mask image
{
unsigned char *mask_data = (unsigned char*)malloc( ((width >> 3)+8) * height );
- mask_image = gdk_image_new_bitmap( gdk_visual_get_system(), mask_data, width, height );
+ mask_image = gdk_image_new_bitmap( visual, mask_data, width, height );
wxMask *mask = new wxMask();
- mask->m_bitmap = gdk_pixmap_new( (GdkWindow*)&gdk_root_parent, width, height, 1 );
+ mask->m_bitmap = gdk_pixmap_new( wxRootWindow->window, width, height, 1 );
bitmap.SetMask( mask );
}
if (bpp >= 24)
{
- GdkVisual *visual = gdk_visual_get_system();
if ((visual->red_mask > visual->green_mask) && (visual->green_mask > visual->blue_mask)) b_o = RGB;
else if ((visual->red_mask > visual->blue_mask) && (visual->blue_mask > visual->green_mask)) b_o = RGB;
else if ((visual->blue_mask > visual->red_mask) && (visual->red_mask > visual->green_mask)) b_o = BRG;
gdk_image_put_pixel( mask_image, x, y, 0 );
}
- if (HasMask())
- {
- if ((r == r_mask) && (b == b_mask) && (g == g_mask))
- gdk_image_put_pixel( mask_image, x, y, 1 );
- else
- gdk_image_put_pixel( mask_image, x, y, 0 );
- }
-
switch (bpp)
{
case 8:
SetMaskColour( 16, 16, 16 ); // anything unlikely and dividable
}
- GdkVisual *visual = (GdkVisual*) NULL;
+ int bpp = -1;
+ int red_shift_right = 0;
+ int green_shift_right = 0;
+ int blue_shift_right = 0;
+ int red_shift_left = 0;
+ int green_shift_left = 0;
+ int blue_shift_left = 0;
+ bool use_shift = FALSE;
+
if (bitmap.GetPixmap())
- visual = gdk_window_get_visual( bitmap.GetPixmap() );
- else
- visual = gdk_window_get_visual( bitmap.GetBitmap() );
-
- if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent );
- int bpp = visual->depth;
- if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
+ {
+ GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() );
+
+ if (visual == NULL) visual = gdk_window_get_visual( wxRootWindow->window );
+ bpp = visual->depth;
+ if (bpp == 16) bpp = visual->red_prec + visual->green_prec + visual->blue_prec;
+ red_shift_right = visual->red_shift;
+ red_shift_left = 8-visual->red_prec;
+ green_shift_right = visual->green_shift;
+ green_shift_left = 8-visual->green_prec;
+ blue_shift_right = visual->blue_shift;
+ blue_shift_left = 8-visual->blue_prec;
+
+ use_shift = (visual->type == GDK_VISUAL_TRUE_COLOR) || (visual->type == GDK_VISUAL_DIRECT_COLOR);
+ }
+ if (bitmap.GetBitmap())
+ {
+ bpp = 1;
+ }
+
GdkColormap *cmap = gtk_widget_get_default_colormap();
long pos = 0;
{
for (int i = 0; i < bitmap.GetWidth(); i++)
{
- wxInt32 pixel = gdk_image_get_pixel( gdk_image, i, j );
- if (bpp <= 8)
- {
- data[pos] = cmap->colors[pixel].red >> 8;
- data[pos+1] = cmap->colors[pixel].green >> 8;
- data[pos+2] = cmap->colors[pixel].blue >> 8;
- } else if (bpp == 15)
+ wxUint32 pixel = gdk_image_get_pixel( gdk_image, i, j );
+ if (bpp == 1)
+ {
+ if (pixel == 0)
+ {
+ data[pos] = 0;
+ data[pos+1] = 0;
+ data[pos+2] = 0;
+ }
+ else
+ {
+ data[pos] = 255;
+ data[pos+1] = 255;
+ data[pos+2] = 255;
+ }
+ }
+ else if (use_shift)
{
-#if (wxBYTE_ORDER == wxBIG_ENDIAN)
- // ?
-#endif
- data[pos] = (pixel >> 7) & 0xf8;
- data[pos+1] = (pixel >> 2) & 0xf8;
- data[pos+2] = (pixel << 3) & 0xf8;
- } else if (bpp == 16)
+ data[pos] = (pixel >> red_shift_right) << red_shift_left;
+ data[pos+1] = (pixel >> green_shift_right) << green_shift_left;
+ data[pos+2] = (pixel >> blue_shift_right) << blue_shift_left;
+ }
+ else if (cmap->colors)
{
-#if (wxBYTE_ORDER == wxBIG_ENDIAN)
- // ?
-#endif
- data[pos] = (pixel >> 8) & 0xf8;
- data[pos+1] = (pixel >> 3) & 0xfc;
- data[pos+2] = (pixel << 3) & 0xf8;
- } else
+ data[pos] = cmap->colors[pixel].red >> 8;
+ data[pos+1] = cmap->colors[pixel].green >> 8;
+ data[pos+2] = cmap->colors[pixel].blue >> 8;
+ }
+ else
{
-#if (wxBYTE_ORDER == wxBIG_ENDIAN)
- data[pos] = (pixel) & 0xff; // Red
- data[pos+1] = (pixel >> 8) & 0xff; // Green
- data[pos+2] = (pixel >> 16) & 0xff; // Blue
-#else
- data[pos] = (pixel >> 16) & 0xff;
- data[pos+1] = (pixel >> 8) & 0xff;
- data[pos+2] = pixel & 0xff;
-#endif
- }
+ wxFAIL_MSG( wxT("Image conversion failed. Unknown visual type.") );
+ }
if (gdk_image_mask)
{
#include "wx/utils.h"
#include <math.h>
+/*
+
+Date: Wed, 05 Jan 2000 11:45:40 +0100
+From: Frits Boel <boel@niob.knaw.nl>
+To: julian.smart@ukonline.co.uk
+Subject: Patch for Motif ConvertToBitmap
+
+Hi Julian,
+
+I've been working on a wxWin application for image processing. From the
+beginning, I was surprised by the (lack of) speed of ConvertToBitmap,
+till I looked in the source code of image.cpp. I saw that converting a
+wxImage to a bitmap with 8-bit pixels is done with comparing every pixel
+to the 256 colors of the palet. A very time-consuming piece of code!
+
+Because I wanted a faster application, I've made a 'patch' for this. In
+short: every pixel of the image is compared to a sorted list with
+colors. If the color is found in the list, the palette entry is
+returned; if the color is not found, the color palette is searched and
+then the palette entry is returned and the color added to the sorted
+list.
+
+Maybe there is another method for this, namely changing the palette
+itself (if the colors are known, as is the case with tiffs with a
+colormap). I did not look at this, maybe someone else did?
+
+The code of the patch is attached, have a look on it, and maybe you will
+ship it with the next release of wxMotif?
+
+Regards,
+
+Frits Boel
+Software engineer at Hubrecht Laboratory, The Netherlands.
+
+*/
+
+class wxSearchColor
+{
+public:
+ wxSearchColor( void );
+ wxSearchColor( int size, XColor *colors );
+ ~wxSearchColor( void );
+
+ int SearchColor( int r, int g, int b );
+private:
+ int AddColor( unsigned int value, int pos );
+
+ int size;
+ XColor *colors;
+ unsigned int *color;
+ int *entry;
+
+ int bottom;
+ int top;
+};
+
+wxSearchColor::wxSearchColor( void )
+{
+ size = 0;
+ colors = (XColor*) NULL;
+ color = (unsigned int *) NULL;
+ entry = (int*) NULL;
+
+ bottom = 0;
+ top = 0;
+}
+
+wxSearchColor::wxSearchColor( int size_, XColor *colors_ )
+{
+ int i;
+ size = size_;
+ colors = colors_;
+ color = new unsigned int[size];
+ entry = new int [size];
+
+ for (i = 0; i < size; i++ ) {
+ entry[i] = -1;
+ }
+
+ bottom = top = ( size >> 1 );
+}
+
+wxSearchColor::~wxSearchColor( void )
+{
+ if ( color ) delete color;
+ if ( entry ) delete entry;
+}
+
+int wxSearchColor::SearchColor( int r, int g, int b )
+{
+ unsigned int value = ( ( ( r * 256 ) + g ) * 256 ) + b;
+ int begin = bottom;
+ int end = top;
+ int middle;
+
+ while ( begin <= end ) {
+
+ middle = ( begin + end ) >> 1;
+
+ if ( value == color[middle] ) {
+ return( entry[middle] );
+ } else if ( value < color[middle] ) {
+ end = middle - 1;
+ } else {
+ begin = middle + 1;
+ }
+
+ }
+
+ return AddColor( value, middle );
+}
+
+int wxSearchColor::AddColor( unsigned int value, int pos )
+{
+ int i;
+ int pixel = -1;
+ int max = 3 * (65536);
+ for ( i = 0; i < 256; i++ ) {
+ int rdiff = ((value >> 8) & 0xFF00 ) - colors[i].red;
+ int gdiff = ((value ) & 0xFF00 ) - colors[i].green;
+ int bdiff = ((value << 8) & 0xFF00 ) - colors[i].blue;
+ int sum = abs (rdiff) + abs (gdiff) + abs (bdiff);
+ if (sum < max) { pixel = i; max = sum; }
+ }
+
+ if ( entry[pos] < 0 ) {
+ color[pos] = value;
+ entry[pos] = pixel;
+ } else if ( value < color[pos] ) {
+
+ if ( bottom > 0 ) {
+ for ( i = bottom; i < pos; i++ ) {
+ color[i-1] = color[i];
+ entry[i-1] = entry[i];
+ }
+ bottom--;
+ color[pos-1] = value;
+ entry[pos-1] = pixel;
+ } else if ( top < size-1 ) {
+ for ( i = top; i >= pos; i-- ) {
+ color[i+1] = color[i];
+ entry[i+1] = entry[i];
+ }
+ top++;
+ color[pos] = value;
+ entry[pos] = pixel;
+ }
+
+ } else {
+
+ if ( top < size-1 ) {
+ for ( i = top; i > pos; i-- ) {
+ color[i+1] = color[i];
+ entry[i+1] = entry[i];
+ }
+ top++;
+ color[pos+1] = value;
+ entry[pos+1] = pixel;
+ } else if ( bottom > 0 ) {
+ for ( i = bottom; i < pos; i++ ) {
+ color[i-1] = color[i];
+ entry[i-1] = entry[i];
+ }
+ bottom--;
+ color[pos] = value;
+ entry[pos] = pixel;
+ }
+
+ }
+
+ return( pixel );
+}
+
wxBitmap wxImage::ConvertToBitmap() const
{
wxBitmap bitmap;
XQueryColors( dpy, cmap, colors, 256 );
}
+ wxSearchColor scolor( 256, colors );
unsigned char* data = GetData();
int index = 0;
{
case 8:
{
+#if 0 // Old, slower code
int pixel = -1;
/*
if (wxTheApp->m_colorCube)
/*
}
*/
+#endif
+
+ // And this is all to get the 'right' color...
+ int pixel = scolor.SearchColor( r, g, b );
XPutPixel( data_image, x, y, pixel );
break;
}
};
IMPLEMENT_DYNAMIC_CLASS(wxImageModule, wxModule)
+
+
+//-----------------------------------------------------------------------------
+
+// GRG, Dic/99
+// Counts and returns the number of different colours. Optionally stops
+// when it exceeds 'stopafter' different colours. This is useful, for
+// example, to see if the image can be saved as 8-bit (256 colour or
+// less, in this case it would be invoked as CountColours(256)). Default
+// value for stopafter is -1 (don't care).
+//
+unsigned long wxImage::CountColours( unsigned long stopafter )
+{
+ wxHashTable h;
+ wxNode *node;
+ wxHNode *hnode;
+ unsigned char r, g, b, *p;
+ unsigned long size, nentries, key;
+
+ p = GetData();
+ size = GetWidth() * GetHeight();
+ nentries = 0;
+
+ for (unsigned long j = 0; (j < size) && (nentries <= stopafter) ; j++)
+ {
+ r = *(p++);
+ g = *(p++);
+ b = *(p++);
+ key = (r << 16) | (g << 8) | b;
+
+ hnode = (wxHNode *) h.Get(key);
+
+ if (!hnode)
+ {
+ h.Put(key, (wxObject *)(new wxHNode));
+ nentries++;
+ }
+ }
+
+ // delete all HNodes
+ h.BeginFind();
+ while ((node = h.Next()) != NULL)
+ delete (wxHNode *)node->GetData();
+
+ return nentries;
+}
+
+
+// GRG, Dic/99
+// Computes the histogram of the image and fills a hash table, indexed
+// with integer keys built as 0xRRGGBB, containing wxHNode objects. Each
+// wxHNode contains an 'index' (useful to build a palette with the image
+// colours) and a 'value', which is the number of pixels in the image with
+// that colour.
+//
+unsigned long wxImage::ComputeHistogram( wxHashTable &h )
+{
+ unsigned char r, g, b, *p;
+ unsigned long size, nentries, key;
+ wxHNode *hnode;
+
+ p = GetData();
+ size = GetWidth() * GetHeight();
+ nentries = 0;
+
+ for (unsigned long j = 0; j < size; j++)
+ {
+ r = *(p++);
+ g = *(p++);
+ b = *(p++);
+ key = (r << 16) | (g << 8) | b;
+
+ hnode = (wxHNode *) h.Get(key);
+
+ if (hnode)
+ hnode->value++;
+ else
+ {
+ hnode = new wxHNode();
+ hnode->index = nentries++;
+ hnode->value = 1;
+
+ h.Put(key, (wxObject *)hnode);
+ }
+ }
+
+ return nentries;
+}
+
+/*
+ * Rotation code by Carlos Moreno
+ */
+
+struct wxRotationPixel
+{
+ unsigned char rgb[3];
+};
+
+struct wxRotationPoint
+{
+ wxRotationPoint (double _x, double _y) : x(_x), y(_y) {}
+ wxRotationPoint (const wxPoint & p) : x(p.x), y(p.y) {}
+ double x, y;
+};
+
+static const wxRotationPixel gs_BlankPixel = {0,0,0};
+static const double gs_Epsilon = 1e-10;
+
+static inline int wxCint (double x)
+{
+ return (x > 0) ? (int) (x + 0.5) : (int) (x - 0.5);
+}
+
+
+// Auxiliary function to rotate a point (x,y) with respect to point p0
+// make it inline and use a straight return to facilitate optimization
+// also, the function receives the sine and cosine of the angle to avoid
+// repeating the time-consuming calls to these functions -- sin/cos can
+// be computed and stored in the calling function.
+
+inline wxRotationPoint rotated_point (const wxRotationPoint & p, double cos_angle, double sin_angle, const wxRotationPoint & p0)
+{
+ return wxRotationPoint (p0.x + (p.x - p0.x) * cos_angle - (p.y - p0.y) * sin_angle,
+ p0.y + (p.y - p0.y) * cos_angle + (p.x - p0.x) * sin_angle);
+}
+
+inline wxRotationPoint rotated_point (double x, double y, double cos_angle, double sin_angle, const wxRotationPoint & p0)
+{
+ return rotated_point (wxRotationPoint(x,y), cos_angle, sin_angle, p0);
+}
+
+wxImage wxImage::Rotate(double angle, const wxPoint & centre_of_rotation, bool interpolating, wxPoint * offset_after_rotation) const
+{
+ const wxImage& img = * this;
+ int i;
+ angle = -angle; // screen coordinates are a mirror image of "real" coordinates
+
+ // Create pointer-based array to accelerate access to wxImage's data
+ wxRotationPixel ** data = new wxRotationPixel * [img.GetHeight()];
+
+ data[0] = (wxRotationPixel *) img.GetData();
+
+ for (i = 1; i < img.GetHeight(); i++)
+ {
+ data[i] = data[i - 1] + img.GetWidth();
+ }
+
+ // pre-compute coefficients for rotation formula (sine and cosine of the angle)
+ const double cos_angle = cos(angle);
+ const double sin_angle = sin(angle);
+
+ // Create new Image to store the result
+ // First, find rectangle that covers the rotated image; to do that,
+ // rotate the four corners
+
+ const wxRotationPoint p0 = centre_of_rotation;
+
+ wxRotationPoint p1 = rotated_point (0, 0, cos_angle, sin_angle, p0);
+ wxRotationPoint p2 = rotated_point (0, img.GetHeight(), cos_angle, sin_angle, p0);
+ wxRotationPoint p3 = rotated_point (img.GetWidth(), 0, cos_angle, sin_angle, p0);
+ wxRotationPoint p4 = rotated_point (img.GetWidth(), img.GetHeight(), cos_angle, sin_angle, p0);
+
+ int x1 = (int) floor (wxMin (wxMin(p1.x, p2.x), wxMin(p3.x, p4.x)));
+ int y1 = (int) floor (wxMin (wxMin(p1.y, p2.y), wxMin(p3.y, p4.y)));
+
+ int x2 = (int) ceil (wxMax (wxMax(p1.x, p2.x), wxMax(p3.x, p4.x)));
+ int y2 = (int) ceil (wxMax (wxMax(p1.y, p2.y), wxMax(p3.y, p4.y)));
+
+ wxImage rotated (x2 - x1 + 1, y2 - y1 + 1);
+
+ if (offset_after_rotation != NULL)
+ {
+ *offset_after_rotation = wxPoint (x1, y1);
+ }
+
+
+ wxRotationPixel ** result_data = new wxRotationPixel * [rotated.GetHeight()];
+
+ result_data[0] = (wxRotationPixel *) rotated.GetData();
+
+ for (i = 1; i < rotated.GetHeight(); i++)
+ {
+ result_data[i] = result_data[i - 1] + rotated.GetWidth();
+ }
+
+ // Now, for each point of the rotated image, find where it came from, by
+ // performing an inverse rotation (a rotation of -angle) and getting the
+ // pixel at those coordinates
+
+ int x;
+ for (x = 0; x < rotated.GetWidth(); x++)
+ {
+ for (int y = 0; y < rotated.GetHeight(); y++)
+ {
+ wxRotationPoint src = rotated_point (x + x1, y + y1, cos_angle, -sin_angle, p0);
+
+ if (interpolating)
+ {
+ if (0 < src.x && src.x < img.GetWidth() - 1 &&
+ 0 < src.y && src.y < img.GetHeight() - 1)
+ {
+ // interpolate using the 4 enclosing grid-points. Those
+ // points can be obtained using floor and ceiling of the
+ // exact coordinates of the point
+
+ const int x1 = wxCint(floor(src.x));
+ const int y1 = wxCint(floor(src.y));
+ const int x2 = wxCint(ceil(src.x));
+ const int y2 = wxCint(ceil(src.y));
+
+ // get four points and the distances (square of the distance,
+ // for efficiency reasons) for the interpolation formula
+ const wxRotationPixel & v1 = data[y1][x1];
+ const wxRotationPixel & v2 = data[y1][x2];
+ const wxRotationPixel & v3 = data[y2][x2];
+ const wxRotationPixel & v4 = data[y2][x1];
+
+ const double d1 = (src.x - x1) * (src.x - x1) + (src.y - y1) * (src.y - y1);
+ const double d2 = (src.x - x2) * (src.x - x2) + (src.y - y1) * (src.y - y1);
+ const double d3 = (src.x - x2) * (src.x - x2) + (src.y - y2) * (src.y - y2);
+ const double d4 = (src.x - x1) * (src.x - x1) + (src.y - y2) * (src.y - y2);
+
+ // Now interpolate as a weighted average of the four surrounding
+ // points, where the weights are the distances to each of those points
+
+ // If the point is exactly at one point of the grid of the source
+ // image, then don't interpolate -- just assign the pixel
+
+ if (d1 < gs_Epsilon) // d1,d2,d3,d4 are positive -- no need for abs()
+ {
+ result_data[y][x] = v1;
+ }
+ else if (d2 < gs_Epsilon)
+ {
+ result_data[y][x] = v2;
+ }
+ else if (d3 < gs_Epsilon)
+ {
+ result_data[y][x] = v3;
+ }
+ else if (d4 < gs_Epsilon)
+ {
+ result_data[y][x] = v4;
+ }
+ else
+ {
+ // weights for the weighted average are proportional to the inverse of the distance
+ const double w1 = 1/d1, w2 = 1/d2, w3 = 1/d3, w4 = 1/d4;
+
+ for (int i = 0; i < 3; i++) // repeat calculation for R, G, and B
+ {
+ result_data[y][x].rgb[i] =
+ (unsigned char) ( (w1 * v1.rgb[i] + w2 * v2.rgb[i] +
+ w3 * v3.rgb[i] + w4 * v4.rgb[i]) /
+ (w1 + w2 + w3 + w4) );
+ }
+ }
+ }
+ else
+ {
+ result_data[y][x] = gs_BlankPixel;
+ }
+ }
+ else
+ {
+ const int xs = wxCint (src.x); // wxCint performs rounding to the
+ const int ys = wxCint (src.y); // closest integer
+
+ if (0 <= xs && xs < img.GetWidth() &&
+ 0 <= ys && ys < img.GetHeight())
+ {
+ result_data[y][x] = data[ys][xs];
+ }
+ else
+ {
+ result_data[y][x] = gs_BlankPixel;
+ }
+ }
+ }
+ }
+
+ return rotated;
+}
+