Index: image.gd.inc
===================================================================
RCS file: /cvs/drupal/contributions/modules/smartcrop/Attic/image.gd.inc,v
retrieving revision 1.1.2.4
diff -u -r1.1.2.4 image.gd.inc
--- image.gd.inc	7 Dec 2010 19:35:18 -0000	1.1.2.4
+++ image.gd.inc	21 Dec 2010 18:24:46 -0000
@@ -49,17 +49,25 @@
   // Slice from left and right edges until the correct width is reached.
   while ($dx) {
     $slice = min($dx, 10);
+    $big_slice = min($slice*3,imagesx($image));
 
     // Calculate the entropy of the new slice.
     if (!$left_entropy) {
-      $left_entropy = _smartcrop_gd_entropy_slice($image_data, $left, $top, $slice, imagesy($image));
+      $left_entropy = _smartcrop_gd_entropy_slice($image_data, $left, $top, $big_slice, imagesy($image));
     }
     if (!$right_entropy) {
-      $right_entropy = _smartcrop_gd_entropy_slice($image_data, $right - $slice, $top, $slice, imagesy($image));
+      $right_entropy = _smartcrop_gd_entropy_slice($image_data, $right - $big_slice, $top, $big_slice, imagesy($image));
     }
 
     // Remove the lowest entropy slice.
-    if ($left_entropy >= $right_entropy) {
+    if (abs($left_entropy - $right_entropy)<0.2) {
+      // Entropy is almost equal so cut both sides.  
+      $right -= round($slice/2);
+      $right_entropy = 0;
+      $left += $slice - round($slice/2);
+      $left_entropy = 0;
+    }
+    elseif ($left_entropy > $right_entropy) {
       $right -= $slice;
       $right_entropy = 0;
     }
@@ -73,17 +81,25 @@
   // Slice from the top and bottom edges until the correct width is reached.
   while ($dy) {
     $slice = min($dy, 10);
+    $big_slice = min($slice*3,imagesy($image));
 
     // Calculate the entropy of the new slice.
     if (!$top_entropy) {
-      $top_entropy = _smartcrop_gd_entropy_slice($image_data, $left, $top, $requested_x, $slice);
+      $top_entropy = _smartcrop_gd_entropy_slice($image_data, $left, $top, $requested_x, $big_slice);
     }
     if (!$bottom_entropy) {
-      $bottom_entropy = _smartcrop_gd_entropy_slice($image_data, $left, $bottom - $slice, $requested_x, $slice);
+      $bottom_entropy = _smartcrop_gd_entropy_slice($image_data, $left, $bottom - $big_slice, $requested_x, $big_slice);
     }
-
+ 
     // Remove the lowest entropy slice.
-    if ($top_entropy >= $bottom_entropy) {
+    if (abs($top_entropy - $bottom_entropy)<0.2) {
+      // Entropy is almost equal so cut both sides.  
+      $bottom -= $slice - round($slice/2);
+      $bottom_entropy = 0;
+      $top += round($slice/2);
+      $top_entropy = 0;
+    }
+    elseif ($top_entropy > $bottom_entropy) {
       $bottom -= $slice;
       $bottom_entropy = 0;
     }
@@ -142,21 +158,24 @@
 }
 
 /**
- * Compute a histogram of an image.
+ * Compute a histogram of an image filtered to show only edges.
  * @param resource $img GD image resource.
  * @return array histogram as an array.
  */
 function _smartcrop_gd_histogram($img) {
+  $img2 = imagecreatetruecolor(imagesx($img), imagesy($img));
+  imagecopy($img2, $img, 0, 0, 0, 0, imagesx($img), imagesy($img));
+  imagefilter($img2, IMG_FILTER_EDGEDETECT);  
   $histogram = array_fill(0, 768, 0);
-  for ($i = 0; $i < imagesx($img); $i++) {
-    for ($j = 0; $j < imagesy($img); $j++) {
-      $rgb = imagecolorat($img, $i, $j);
+  for ($x = 0; $x < imagesx($img); $x++) {
+    for ($y = 0; $y < imagesy($img); $y++) {
+      $rgb = imagecolorat($img2, $x, $y);
       $r = ($rgb >> 16) & 0xFF;
       $g = ($rgb >> 8) & 0xFF;
       $b = $rgb & 0xFF;
-      $histogram[$r]++;
-      $histogram[$g + 256]++;
-      $histogram[$b + 512]++;
+      //counting geometrical distance
+      $value = round(sqrt(pow($r,2)+pow($g,2)+pow($b,2)));
+      $histogram[$value]++;
     }
   }
   return $histogram;
