// For memcpy
#include <string.h>
+#include <math.h>
#ifdef __SALFORDC__
#undef FAR
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;
- }
+ {
+ 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 )
if (wxFileExists(filename))
{
wxFileInputStream stream(filename);
- wxBufferedInputStream bstream( stream );
+ wxBufferedInputStream bstream( stream );
return LoadFile(bstream, type);
}
else
if (wxFileExists(filename))
{
wxFileInputStream stream(filename);
- wxBufferedInputStream bstream( stream );
+ wxBufferedInputStream bstream( stream );
return LoadFile(bstream, mimetype);
}
else
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;
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);
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 );
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;
}
ptbits += 3;
}
ptbits += padding;
- }
+ }
// similarly, set data according to the possible mask bitmap
if( bitmap.GetMask() && bitmap.GetMask()->GetMaskBitmap() )
#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:
}
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())
{
GdkVisual *visual = gdk_window_get_visual( bitmap.GetPixmap() );
- if (visual == NULL) visual = gdk_window_get_visual( (GdkWindow*) &gdk_root_parent );
+ if (visual == NULL) visual = gdk_window_get_visual( wxRootWindow->window );
bpp = visual->depth;
- if ((bpp == 16) && (visual->red_mask != 0xf800)) bpp = 15;
+ 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 == 1)
- {
- if (pixel == 0)
- {
- data[pos] = 0;
+ 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;
+ }
+ else
+ {
+ data[pos] = 255;
data[pos+1] = 255;
data[pos+2] = 255;
- }
- } else 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)
+ }
+ }
+ 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;
}
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;
+}