]> git.saurik.com Git - wxWidgets.git/blobdiff - src/common/image.cpp
Make wxSpinCtrlGeneric usable as wxDVC editor control.
[wxWidgets.git] / src / common / image.cpp
index 050ec43488f5190d30d89b6a52f4b555cb82138e..de87a34257f985fd3111ed2ca12d8c6f5bf2f6f1 100644 (file)
@@ -442,23 +442,16 @@ wxImage::Scale( int width, int height, wxImageResizeQuality quality ) const
     if ( old_width == width && old_height == height )
         return *this;
 
-    // resample the image using either the nearest neighbourhood, bilinear or
-    // bicubic method as specified
-    switch ( quality )
+    if (quality == wxIMAGE_QUALITY_HIGH)
     {
-        case wxIMAGE_QUALITY_BICUBIC:
-        case wxIMAGE_QUALITY_BILINEAR:
-            // both of these algorithms should be used for up-sampling the
-            // image only, when down-sampling always use box averaging for best
-            // results
-            if ( width < old_width && height < old_height )
-                image = ResampleBox(width, height);
-            else if ( quality == wxIMAGE_QUALITY_BILINEAR )
-                image = ResampleBilinear(width, height);
-            else if ( quality == wxIMAGE_QUALITY_BICUBIC )
-                image = ResampleBicubic(width, height);
-            break;
+        quality = (width < old_width && height < old_height)
+            ? wxIMAGE_QUALITY_BOX_AVERAGE
+            : wxIMAGE_QUALITY_BICUBIC;
+    }
 
+    // Resample the image using the method as specified.
+    switch ( quality )
+    {
         case wxIMAGE_QUALITY_NEAREST:
             if ( old_width % width == 0 && old_width >= width &&
                 old_height % height == 0 && old_height >= height )
@@ -468,6 +461,18 @@ wxImage::Scale( int width, int height, wxImageResizeQuality quality ) const
 
             image = ResampleNearest(width, height);
             break;
+
+        case wxIMAGE_QUALITY_BILINEAR:
+            image = ResampleBilinear(width, height);
+            break;
+
+        case wxIMAGE_QUALITY_BICUBIC:
+            image = ResampleBicubic(width, height);
+            break;
+
+        case wxIMAGE_QUALITY_BOX_AVERAGE:
+            image = ResampleBox(width, height);
+            break;
     }
 
     // If the original image has a mask, apply the mask to the new image
@@ -589,16 +594,16 @@ wxImage wxImage::ResampleBox(int width, int height) const
             averaged_pixels = 0;
             sum_r = sum_g = sum_b = sum_a = 0.0;
 
-            for ( int j = int(src_y - scale_factor_y/2.0 + 1);
-                  j <= int(src_y + scale_factor_y_2);
+            for ( int j = int(src_y - scale_factor_y/2.0 + 1), k = j;
+                  j <= int(src_y + scale_factor_y_2) || j < k + 2;
                   j++ )
             {
                 // We don't care to average pixels that don't exist (edges)
                 if ( j < 0 || j > M_IMGDATA->m_height - 1 )
                     continue;
 
-                for ( int i = int(src_x - scale_factor_x/2.0 + 1);
-                      i <= src_x + scale_factor_x_2;
+                for ( int i = int(src_x - scale_factor_x/2.0 + 1), e = i;
+                      i <= src_x + scale_factor_x_2 || i < e + 2;
                       i++ )
                 {
                     // Don't average edge pixels
@@ -1089,24 +1094,37 @@ wxImage wxImage::Rotate90( bool clockwise ) const
     }
 
     unsigned char *data = image.GetData();
-    const unsigned char *source_data = M_IMGDATA->m_data;
     unsigned char *target_data;
 
-    for (long j = 0; j < height; j++)
+    // we rotate the image in 21-pixel (63-byte) wide strips
+    // to make better use of cpu cache - memory transfers
+    // (note: while much better than single-pixel "strips",
+    //  our vertical strips will still generally straddle 64-byte cachelines)
+    for (long ii = 0; ii < width; )
     {
-        for (long i = 0; i < width; i++)
+        long next_ii = wxMin(ii + 21, width);
+
+        for (long j = 0; j < height; j++)
         {
-            if ( clockwise )
-            {
-                target_data = data + (((i+1)*height) - j - 1)*3;
-            }
-            else
+            const unsigned char *source_data
+                                     = M_IMGDATA->m_data + (j*width + ii)*3;
+
+            for (long i = ii; i < next_ii; i++)
             {
-                target_data = data + ((height*(width-1)) + j - (i*height))*3;
+                if ( clockwise )
+                {
+                    target_data = data + ((i + 1)*height - j - 1)*3;
+                }
+                else
+                {
+                    target_data = data + (height*(width - 1 - i) + j)*3;
+                }
+                memcpy( target_data, source_data, 3 );
+                source_data += 3;
             }
-            memcpy( target_data, source_data, 3 );
-            source_data += 3;
         }
+
+        ii = next_ii;
     }
 
     const unsigned char *source_alpha = M_IMGDATA->m_alpha;
@@ -1116,21 +1134,30 @@ wxImage wxImage::Rotate90( bool clockwise ) const
         unsigned char *alpha_data = image.GetAlpha();
         unsigned char *target_alpha = 0 ;
 
-        for (long j = 0; j < height; j++)
+        for (long ii = 0; ii < width; )
         {
-            for (long i = 0; i < width; i++)
+            long next_ii = wxMin(ii + 64, width);
+
+            for (long j = 0; j < height; j++)
             {
-                if ( clockwise )
-                {
-                    target_alpha = alpha_data + (((i+1)*height) - j - 1);
-                }
-                else
+                source_alpha = M_IMGDATA->m_alpha + j*width + ii;
+
+                for (long i = ii; i < next_ii; i++)
                 {
-                    target_alpha = alpha_data + ((height*(width-1)) + j - (i*height));
-                }
+                    if ( clockwise )
+                    {
+                        target_alpha = alpha_data + (i+1)*height - j - 1;
+                    }
+                    else
+                    {
+                        target_alpha = alpha_data + height*(width - i - 1) + j;
+                    }
 
-                *target_alpha = *source_alpha++;
+                    *target_alpha = *source_alpha++;
+                }
             }
+
+            ii = next_ii;
         }
     }
 
@@ -1401,12 +1428,15 @@ void wxImage::Paste( const wxImage &image, int x, int y )
     if (width < 1) return;
     if (height < 1) return;
 
-    if ((!HasMask() && !image.HasMask()) ||
-        (HasMask() && !image.HasMask()) ||
-       ((HasMask() && image.HasMask() &&
+    // If we can, copy the data using memcpy() as this is the fastest way. But
+    // for this  the image being pasted must have "compatible" mask with this
+    // one meaning that either it must not have one at all or it must use the
+    // same masked colour.
+    if ( !image.HasMask() ||
+        ((HasMask() &&
          (GetMaskRed()==image.GetMaskRed()) &&
          (GetMaskGreen()==image.GetMaskGreen()) &&
-         (GetMaskBlue()==image.GetMaskBlue()))))
+         (GetMaskBlue()==image.GetMaskBlue()))) )
     {
         const unsigned char* source_data = image.GetData() + 3*(xx + yy*image.GetWidth());
         int source_step = image.GetWidth()*3;