com php-src: - (s)rgb distance works way better for now, re enable threshold: ext/gd/gd.c ext/gd/libgd/gd_crop.c
| From: | Pierre Joye | Date: | Sun, 03 Mar 2013 04:30:12 +0000 |
| Subject: | com php-src: - (s)rgb distance works way better for now, re enable threshold: ext/gd/gd.c ext/gd/libgd/gd_crop.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-71392@lists.php.net to get a copy of this message | ||
Commit: 0a55c4b1dd84382c7d53c460462b78e8ac9c7d8b
Author: Pierre Joye <pierre.php@gmail.com> Sun, 3 Mar 2013 05:30:12 +0100
Parents: 7698bc5735d188964cb98e6b6387c4cca26fcb0e
Branches: master
Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=0a55c4b1dd84382c7d53c460462b78e8ac9c7d8b
Log:
- (s)rgb distance works way better for now, re enable threshold
Changed paths:
M ext/gd/gd.c
M ext/gd/libgd/gd_crop.c
Diff:
diff --git a/ext/gd/gd.c b/ext/gd/gd.c
index 7a2e214..be9501e 100644
--- a/ext/gd/gd.c
+++ b/ext/gd/gd.c
@@ -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;
diff --git a/ext/gd/libgd/gd_crop.c b/ext/gd/libgd/gd_crop.c
index 562098f..9ce4827 100644
--- a/ext/gd/libgd/gd_crop.c
+++ b/ext/gd/libgd/gd_crop.c
@@ -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;
}
/*