- (s)rgb distance works way better for now, re enable threshold

This commit is contained in:
Pierre Joye 2013-03-03 05:30:12 +01:00
parent 7698bc5735
commit 0a55c4b1dd
2 changed files with 14 additions and 19 deletions

View 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_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_WHITE", GD_CROP_WHITE, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("IMG_CROP_SIDES", GD_CROP_SIDES, 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); REGISTER_LONG_CONSTANT("IMG_CROP_THRESHOLD", GD_CROP_THRESHOLD, CONST_CS | CONST_PERSISTENT);
#endif
#else #else
REGISTER_LONG_CONSTANT("GD_BUNDLED", 0, CONST_CS | CONST_PERSISTENT); REGISTER_LONG_CONSTANT("GD_BUNDLED", 0, CONST_CS | CONST_PERSISTENT);
#endif #endif
@ -5160,39 +5157,39 @@ PHP_FUNCTION(imagecrop)
double threshold = 0.5f; double threshold = 0.5f;
gdImagePtr im; gdImagePtr im;
gdImagePtr im_crop; gdImagePtr im_crop;
HashTable rect_hash;
gdRect rect; gdRect rect;
zval *z_rect;
zval **tmp; 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; return;
} }
ZEND_FETCH_RESOURCE(im, gdImagePtr, &IM, -1, "Image", le_gd); 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); rect.x = Z_LVAL_PP(tmp);
} else { } else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing x position"); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing x position");
RETURN_FALSE; 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); rect.y = Z_LVAL_PP(tmp);
} else { } else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing y position"); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing y position");
RETURN_FALSE; 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); rect.width = Z_LVAL_PP(tmp);
} else { } else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing width"); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing width");
RETURN_FALSE; RETURN_FALSE;
} }
if (zend_hash_find(&rect_hash, "height", strlen("x"), (void **)&tmp) != FAILURE) { if (zend_hash_find(HASH_OF(z_rect), "height", sizeof("height"), (void **)&tmp) != FAILURE) {
rect.width = Z_LVAL_PP(tmp); rect.height = Z_LVAL_PP(tmp);
} else { } else {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing height"); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Missing height");
RETURN_FALSE; RETURN_FALSE;
@ -5200,9 +5197,6 @@ PHP_FUNCTION(imagecrop)
im_crop = gdImageCrop(im, &rect); im_crop = gdImageCrop(im, &rect);
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown flip mode");
RETURN_FALSE;
if (im_crop == NULL) { if (im_crop == NULL) {
RETURN_FALSE; RETURN_FALSE;
} else { } else {
@ -5238,7 +5232,7 @@ PHP_FUNCTION(imagecropauto)
case GD_CROP_SIDES: case GD_CROP_SIDES:
im_crop = gdImageCropAuto(im, mode); im_crop = gdImageCropAuto(im, mode);
break; break;
#ifdef GD_ENABLE_CROP_THRESHOLD
case GD_CROP_THRESHOLD: case GD_CROP_THRESHOLD:
if (color < 0) { if (color < 0) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Color argument missing with threshold mode"); 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); im_crop = gdImageCropThreshold(im, color, (float) threshold);
break; break;
#endif
default: default:
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown flip mode"); php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown flip mode");
RETURN_FALSE; RETURN_FALSE;

View File

@ -22,6 +22,7 @@
#include <gd.h> #include <gd.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <math.h>
static int gdGuessBackgroundColorFromCorners(gdImagePtr im, int *color); static int gdGuessBackgroundColorFromCorners(gdImagePtr im, int *color);
static int gdColorMatch(gdImagePtr im, int col1, int col2, float threshold); 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; return NULL;
} else { } else {
int y = crop->y; int y = crop->y;
unsigned int dst_y = 0;
if (src->trueColor) { if (src->trueColor) {
unsigned int dst_y = 0; unsigned int dst_y = 0;
while (y < (crop->y + (crop->height - 1))) { 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 dg = gdImageGreen(im, col1) - gdImageGreen(im, col2);
const int db = gdImageBlue(im, col1) - gdImageBlue(im, col2); const int db = gdImageBlue(im, col1) - gdImageBlue(im, col2);
const int da = gdImageAlpha(im, col1) - gdImageAlpha(im, col2); const int da = gdImageAlpha(im, col1) - gdImageAlpha(im, col2);
const int dist = dr * dr + dg * dg + db * db + da * da; 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 (100.0 * dist / 195075) < threshold; return (dist_perc <= threshold);
//return (100.0 * dist / 195075) < threshold;
} }
/* /*