]> granicus.if.org Git - php/commitdiff
- (s)rgb distance works way better for now, re enable threshold
authorPierre Joye <pierre.php@gmail.com>
Sun, 3 Mar 2013 04:30:12 +0000 (05:30 +0100)
committerPierre Joye <pierre.php@gmail.com>
Sun, 3 Mar 2013 04:30:12 +0000 (05:30 +0100)
ext/gd/gd.c
ext/gd/libgd/gd_crop.c

index 7a2e2148a90edea2db12ea1c9a4ca4b746e1824f..be9501e55485cc1018dd888c6c40a6f871b8da42 100644 (file)
@@ -1224,10 +1224,7 @@ PHP_MINIT_FUNCTION(gd)
        REGISTER_LONG_CONSTANT("IMG_CROP_BLACK", GD_CROP_BLACK, CONST_CS | CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("IMG_CROP_WHITE", GD_CROP_WHITE, CONST_CS | CONST_PERSISTENT);
        REGISTER_LONG_CONSTANT("IMG_CROP_SIDES", GD_CROP_SIDES, CONST_CS | CONST_PERSISTENT);
-#ifdef GD_ENABLE_CROP_THRESHOLD
        REGISTER_LONG_CONSTANT("IMG_CROP_THRESHOLD", GD_CROP_THRESHOLD, CONST_CS | CONST_PERSISTENT);
-#endif
-
 #else
        REGISTER_LONG_CONSTANT("GD_BUNDLED", 0, CONST_CS | CONST_PERSISTENT);
 #endif
@@ -5160,39 +5157,39 @@ PHP_FUNCTION(imagecrop)
        double threshold = 0.5f;
        gdImagePtr im;
        gdImagePtr im_crop;
-       HashTable rect_hash;
        gdRect rect;
+       zval *z_rect;
        zval **tmp;
 
-       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|h", &IM, &rect_hash) == FAILURE)  {
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|a", &IM, &z_rect) == FAILURE)  {
                return;
        }
 
        ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd);
 
-       if (zend_hash_find(&rect_hash, "x", strlen("x"), (void **)&tmp) != FAILURE) {
+       if (zend_hash_find(HASH_OF(z_rect), "x", sizeof("x"), (void **)&tmp) != FAILURE) {
                rect.x = Z_LVAL_PP(tmp);
        } else {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing x position");
                RETURN_FALSE;
        }
 
-       if (zend_hash_find(&rect_hash, "y", strlen("x"), (void **)&tmp) != FAILURE) {
+       if (zend_hash_find(HASH_OF(z_rect), "y", sizeof("x"), (void **)&tmp) != FAILURE) {
                rect.y = Z_LVAL_PP(tmp);
        } else {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing y position");
                RETURN_FALSE;
        }
 
-       if (zend_hash_find(&rect_hash, "width", strlen("x"), (void **)&tmp) != FAILURE) {
+       if (zend_hash_find(HASH_OF(z_rect), "width", sizeof("width"), (void **)&tmp) != FAILURE) {
                rect.width = Z_LVAL_PP(tmp);
        } else {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing width");
                RETURN_FALSE;
        }
 
-       if (zend_hash_find(&rect_hash, "height", strlen("x"), (void **)&tmp) != FAILURE) {
-               rect.width = Z_LVAL_PP(tmp);
+       if (zend_hash_find(HASH_OF(z_rect), "height", sizeof("height"), (void **)&tmp) != FAILURE) {
+               rect.height = Z_LVAL_PP(tmp);
        } else {
                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing height");
                RETURN_FALSE;
@@ -5200,9 +5197,6 @@ PHP_FUNCTION(imagecrop)
 
        im_crop = gdImageCrop(im, &rect);
 
-       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown flip mode");
-       RETURN_FALSE;
-
        if (im_crop == NULL) {
                RETURN_FALSE;
        } else {
@@ -5238,7 +5232,7 @@ PHP_FUNCTION(imagecropauto)
                case GD_CROP_SIDES:
                        im_crop = gdImageCropAuto(im, mode);
                        break;
-#ifdef GD_ENABLE_CROP_THRESHOLD
+
                case GD_CROP_THRESHOLD:
                        if (color < 0) {
                                php_error_docref(NULL TSRMLS_CC, E_WARNING, "Color argument missing with threshold mode");
@@ -5246,7 +5240,7 @@ PHP_FUNCTION(imagecropauto)
                        }
                        im_crop = gdImageCropThreshold(im, color, (float) threshold);
                        break;
-#endif
+
                default:
                        php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown flip mode");
                        RETURN_FALSE;
index 562098f2a8fd355305fe17760b133b50e22c80ae..9ce48273b640bf5af8753173a183faeb9f66fde9 100644 (file)
@@ -22,6 +22,7 @@
 #include <gd.h>
 #include <stdlib.h>
 #include <string.h>
+#include <math.h>
 
 static int gdGuessBackgroundColorFromCorners(gdImagePtr im, int *color);
 static int gdColorMatch(gdImagePtr im, int col1, int col2, float threshold);
@@ -65,7 +66,6 @@ printf("rect->x: %i\nrect->y: %i\nrect->width: %i\nrect->height: %i\n", crop->x,
                return NULL;
        } else {
                int y = crop->y;
-               unsigned int dst_y = 0;
                if (src->trueColor) {
                        unsigned int dst_y = 0;
                        while (y < (crop->y + (crop->height - 1))) {
@@ -336,9 +336,10 @@ static int gdColorMatch(gdImagePtr im, int col1, int col2, float threshold)
        const int dg = gdImageGreen(im, col1) - gdImageGreen(im, col2);
        const int db = gdImageBlue(im, col1) - gdImageBlue(im, col2);
        const int da = gdImageAlpha(im, col1) - gdImageAlpha(im, col2);
-       const int dist = dr * dr + dg * dg + db * db + da * da;
-
-       return (100.0 * dist / 195075) < threshold;
+       const double dist = sqrt(dr * dr + dg * dg + db * db + da * da);
+       const double dist_perc = sqrt(dist / (255^2 + 255^2 + 255^2));
+       return (dist_perc <= threshold);
+       //return (100.0 * dist / 195075) < threshold;
 }
 
 /*