-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsmartcrop.c
530 lines (469 loc) · 16.9 KB
/
smartcrop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
/*
+----------------------------------------------------------------------+
| PHP Version 7 |
+----------------------------------------------------------------------+
| Copyright (c) 1997-2016 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Xiyuan Mak <[email protected]> |
+----------------------------------------------------------------------+
*/
/* $Id$ */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_smartcrop.h"
#include "ext/gd/php_gd.h"
#include <math.h>
#define OPTION_DETAIL_WEIGHT 0.2
#define OPTION_SKIN_COLOR_R 0.78
#define OPTION_SKIN_COLOR_G 0.57
#define OPTION_SKIN_COLOR_B 0.44
#define OPTION_SKIN_BIAS 0.01
#define OPTION_SKIN_BRIGHTNESS_MIN 0.2
#define OPTION_SKIN_BRIGHTNESS_MAX 1.0
#define OPTION_SKIN_THRESHOLD 0.8
#define OPTION_SKIN_WEIGHT 1.8
#define OPTION_SATURATION_BRIGHTNESS_MIN 0.05
#define OPTION_SATURATION_BRIGHTNESS_MAX 0.9
#define OPTION_SATURATION_THRESHOLD 0.4
#define OPTION_SATURATION_BIAS 0.2
#define OPTION_SATURATION_WEIGHT 0.3
#define OPTION_SCORE_DOWN_SAMPLE 8
#define OPTION_STEP 8
#define OPTION_SCALE_STEP 0.1
#define OPTION_MIN_SCALE 1.0
#define OPTION_MAX_SCALE 1.0
#define OPTION_EDGE_RADIUS 0.4
#define OPTION_EDGE_WEIGHT -20.0
#define OPTION_OUTSIDE_IMPORTANCE -0.5
#define OPTION_BOOST_WEIGHT 100
#define OPTION_RULE_OF_THIRDS 1
/* If you declare any globals in php_smartcrop.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(smartcrop)
*/
/* True global resources - no need for thread safety here */
static int le_smartcrop;
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("smartcrop.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_smartcrop_globals, smartcrop_globals)
STD_PHP_INI_ENTRY("smartcrop.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_smartcrop_globals, smartcrop_globals)
PHP_INI_END()
*/
/* }}} */
/* Remove the following function when you have successfully modified config.m4
so that your module can be compiled into PHP, it exists only for testing
purposes. */
/* Every user-visible function in PHP should document itself in the source */
/* {{{ proto string confirm_smartcrop_compiled(string arg)
Return a string to confirm that the module is compiled in */
static int *getRgbColorAt(zval *im, zend_long x, zend_long y){
zval functionName, retval, aParams[3];
static int aRgb[4];
int rgb;
ZVAL_STRING(&functionName, "imagecolorat");
ZVAL_ZVAL(&aParams[0], im, 1, 0);
ZVAL_LONG(&aParams[1], x);
ZVAL_LONG(&aParams[2], y);
call_user_function(EG(function_table),NULL,&functionName, &retval,3,aParams TSRMLS_CC);
rgb = Z_LVAL_P(&retval);
aRgb[0] = rgb >> 16;
aRgb[1] = rgb >> 8 & 255;
aRgb[2] = rgb & 255;
return aRgb;
}
static float cie(float r, float g, float b) {
return 0.5126 * b + 0.7152 * g + 0.0722 * r;
}
static float sample(zval *im, zend_long x, zend_long y){
int *aRgb;
aRgb = getRgbColorAt(im,x,y);
return cie(*aRgb,*(aRgb+1),*(aRgb+2));
}
static float edgeDetect(zval *im, zend_long x, zend_long y, zend_long w, zend_long h){
float lightness, leftLightness, topLightness,bottomLightness;
static float centerLightness, rightLightness;
if (x == 0 || x >= w-1 || y == 0 || y >= h-1) {
lightness = sample(im, x, y);
} else {
if ( x > 1) {
leftLightness = centerLightness;
centerLightness = rightLightness;
}
rightLightness = sample(im, x+1, y);
topLightness = sample(im, x, y-1);
bottomLightness = sample(im, x, y+1);
lightness = centerLightness * 4.0 - leftLightness - rightLightness - topLightness - bottomLightness;
}
return lightness;
}
static float skinColor(float r, float g, float b) {
float mag, rd, gd, bd, d;
mag = sqrt(r * r + g * g + b * b);
rd = (r / mag - OPTION_SKIN_COLOR_R);
gd = (g / mag - OPTION_SKIN_COLOR_G);
bd = (b / mag - OPTION_SKIN_COLOR_B);
d = sqrt(rd * rd + gd * gd + bd * bd);
return 1.0 - d;
}
static float skinDetect(float r, float g, float b, float lightness) {
float skin;
int isSkinColor, isSkinBrightness;
lightness = lightness / 255;
skin = skinColor(r, g, b);
isSkinColor = skin > OPTION_SKIN_THRESHOLD ? 1 : 0;
isSkinBrightness = lightness > OPTION_SKIN_BRIGHTNESS_MIN && lightness <= OPTION_SKIN_BRIGHTNESS_MAX ? 1 : 0;
if (isSkinColor && isSkinBrightness) {
return (skin - OPTION_SKIN_THRESHOLD) * (255 / (1 - OPTION_SKIN_THRESHOLD));
} else {
return 0;
}
}
static float saturation(float r, float g, float b){
float maximum, minimum, l, d;
maximum = (r / 255) > (g / 255) ? (r / 255) : (g / 255);
maximum = maximum > (b / 255) ? maximum : (b / 255);
minimum = (r / 255) < (g / 255) ? (r / 255) : (g / 255);
minimum = minimum < (b / 255) ? minimum : (b / 255);
if (maximum == minimum) {
return 0;
}
l = (maximum + minimum) / 2;
d = (maximum - minimum);
return l > 0.5 ? d / (2 - maximum - minimum) : (d / (maximum + minimum));
}
static float saturationDetect(float r, float g, float b, float lightness){
float sat;
int acceptableSaturation, acceptableLightness;
lightness = lightness / 255;
sat = saturation(r, g, b);
acceptableSaturation = sat > OPTION_SATURATION_THRESHOLD ? 1 : 0;
acceptableLightness = (lightness >= OPTION_SATURATION_BRIGHTNESS_MIN && lightness <= OPTION_SATURATION_BRIGHTNESS_MAX) ? 1 : 0;
if (acceptableLightness && acceptableSaturation) {
return (sat - OPTION_SATURATION_THRESHOLD) * (255 / (1 - OPTION_SATURATION_THRESHOLD));
} else {
return 0;
}
}
static int *downSample(int w, int h, float *od){
int p;
float width, height, ifactor2, x, y, u, v, r, g, b, a, mr, mg, mb, pR, pG, pB, pA;
static float *data;
width = floor(w / OPTION_SCORE_DOWN_SAMPLE);
height = floor(h / OPTION_SCORE_DOWN_SAMPLE);
ifactor2 = 1.0 / (OPTION_SCORE_DOWN_SAMPLE * OPTION_SCORE_DOWN_SAMPLE);
data = (float*) emalloc(width * height * 4 * sizeof(float));
for (y = 0; y < height; y++) {
for (x = 0; x < width; x++) {
r = g = b = a = mr = mg = mb = 0.0;
for(v = 0; v < OPTION_SCORE_DOWN_SAMPLE; v++) {
for (u = 0; u < OPTION_SCORE_DOWN_SAMPLE; u++) {
p = (y * OPTION_SCORE_DOWN_SAMPLE + v) * w * 3 + (x * OPTION_SCORE_DOWN_SAMPLE + u) * 3;
pR = *(od + p);
pG = *(od + p + 1);
pB = *(od + p + 2);
pA = 0.0;
r += pR;
g += pG;
b += pB;
a += pA;
mr = mr > pR ? mr : pR;
mg = mg > pG ? mg : pG;
mb = mb > pB ? mb : pB;
}
}
p = y * width * 4 + x * 4;
data[p] = r * ifactor2 * 0.5 + mr * 0.5;
data[p+1] = g * ifactor2 * 0.7 + mg * 0.3;
data[p+2] = b * ifactor2;
data[p+3] = a * ifactor2;
}
}
return data;
}
static int *generateCrops(int w, int h, int cw, int ch){
int cropWidth, cropHeight, x, y, p;
static int *result;
cropWidth = cw;
cropHeight = ch;
if (cw == w) {
result = (int*) emalloc(sizeof(int) * (h - cropHeight) / OPTION_STEP * 2);
} else {
result = (int*) emalloc(sizeof(int) * (w - cropWidth) / OPTION_STEP * 2);
}
p = 0;
for (y = 0; y + cropHeight <= h; y += OPTION_STEP) {
for (x = 0; x + cropWidth <= w; x += OPTION_STEP) {
result[p]=x;
result[p + 1]=y;
p += 2;
}
}
return result;
}
static float thirds(float x) {
x = ((float)((int)(x - (1.0 / 3.0) + 1.0) % 2) * 0.5 - 0.5) * 16.0;
return (1.0 - x * x) > 0.0 ? (1.0 - x * x) : 0.0;
}
static float importance(float cx, float cy, float cw, float ch, float x, float y) {
float px, py, dx, dy, d, s;
if (cx > x || x >= cx + cw || cy > y || y > cy + ch) {
return OPTION_OUTSIDE_IMPORTANCE;
}
x = (x - cx) / cw;
y = (y - cy) / ch;
px = fabs(0.5 - x) * 2.0;
py = fabs(0.5 - y) * 2,0;
dx = (px - 1.0 + OPTION_EDGE_RADIUS);
dx = dx > 0.0 ? dx : 0.0;
dy = (py - 1.0 + OPTION_EDGE_RADIUS);
dy = dy > 0.0 ? dy : 0.0;
d = (dx * dx + dy * dy) * OPTION_EDGE_WEIGHT;
s = 1.41 - sqrt(px * px + py * py);
if (OPTION_RULE_OF_THIRDS) {
s += (0.0 > (s + d + 0.5) ? 0.0 : (s + d + 0.5)) * 1.2 * (thirds(px) + thirds(py));
}
return s+d;
}
static float score(float *output, int cx, int cy, int cw, int ch, int w, int h) {
float downSample, invDownSample,i,detail,skin,saturation, total;
int outputHeightDownSample, outputWidthDownSample, outputWidth, y, x, p;
downSample = OPTION_SCORE_DOWN_SAMPLE;
invDownSample = 1 /downSample;
outputHeightDownSample = h;
outputWidthDownSample = w;
outputWidth = w / downSample;
detail = skin = saturation = 0;
for (y = 0; y < outputHeightDownSample; y += downSample) {
for (x = 0; x < outputWidthDownSample; x += downSample) {
i = importance(cx, cy, cw, ch, x, y);
p = floor(y / downSample) * outputWidth * 4 + floor(x / downSample) * 4;
detail += (*(output+p+1) / 255) * i;
skin += *(output + p) / 255 * (detail + OPTION_SKIN_BIAS) * i;
saturation += *(output + p + 2) / 255 * (detail + OPTION_SATURATION_BIAS) * i;
}
}
total = (detail * OPTION_DETAIL_WEIGHT + skin * OPTION_SKIN_WEIGHT + saturation * OPTION_SATURATION_WEIGHT) / (cw * ch);
return total;
}
PHP_FUNCTION(smartcrop)
{
zval *sim;
zend_long DW, DH;
zval functionName;
zval retval;
zend_long SW, SH, RW, RH;
zval rim;
float dw, dh, rw, rh, scale;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "zll", &sim, &DW, &DH )
== FAILURE)
{
RETURN_FALSE;
}
zval params[1];
ZVAL_ZVAL(¶ms[0], sim, 1, 0);
ZVAL_STRING(&functionName, "imagesx");
call_user_function(EG(function_table),NULL,&functionName,&retval,1,params TSRMLS_CC);
SW = Z_LVAL_P(&retval);
ZVAL_STRING(&functionName, "imagesy");
call_user_function(EG(function_table),NULL,&functionName,&retval,1,params TSRMLS_CC);
SH = Z_LVAL_P(&retval);
dw = DW;
dh = DH;
scale = (dw / SW) > (dh / SH) ? (dw / SW) : (dh / SH);
RW = floor(SW * scale);
RH = floor(SH * scale);
zval canvas_params[2];
ZVAL_LONG(&canvas_params[0], RW);
ZVAL_LONG(&canvas_params[1], RH);
ZVAL_STRING(&functionName, "imagecreatetruecolor");
call_user_function(EG(function_table),NULL,&functionName,&rim,2,canvas_params TSRMLS_CC);
zval resize_params[10];
ZVAL_ZVAL(&resize_params[0], &rim, 1, 0);
ZVAL_ZVAL(&resize_params[1], sim, 1, 0);
ZVAL_LONG(&resize_params[2],0);
ZVAL_LONG(&resize_params[3],0);
ZVAL_LONG(&resize_params[4],0);
ZVAL_LONG(&resize_params[5],0);
ZVAL_LONG(&resize_params[6],RW);
ZVAL_LONG(&resize_params[7],RH);
ZVAL_LONG(&resize_params[8],SW);
ZVAL_LONG(&resize_params[9],SH);
ZVAL_STRING(&functionName, "imagecopyresampled");
call_user_function(EG(function_table),NULL,&functionName,&retval,10,resize_params TSRMLS_CC);
int x,y,p,z;
rw = RW;
rh = RH;
int *rgb_ptr;
float *od;
float *scoreOutput;
int *crops;
od = (float*)emalloc(RW*RH*3*sizeof(float));
for (y = 0; y < rh; y++) {
for (x = 0; x < rw; x++) {
rgb_ptr = getRgbColorAt(&rim,x,y);
p = y * rw * 3 + x * 3;
od[p+1] = edgeDetect(&rim,x,y,rw,rh);
od[p] = skinDetect(*(rgb_ptr),*(rgb_ptr+1),*(rgb_ptr+2),sample(&rim,x,y));
od[p+2] = saturationDetect(*(rgb_ptr),*(rgb_ptr+1),*(rgb_ptr+2),sample(&rim,x,y));
}
}
scoreOutput = downSample(rw, rh, od);
crops = generateCrops(rw, rh, dw, dh);
float topScore = -1.0/0.0;
int topCrop;
float scoreTmp;
int cropsNum;
if (dw == rw) {
cropsNum = (rh - dh)/OPTION_STEP;
} else {
cropsNum = (rw - dw)/OPTION_STEP;
}
for (z = 0; z <= cropsNum*2; z += 2){
scoreTmp = score(scoreOutput, *(crops+z), *(crops+z+1),dw,dh,rw,rh);
if (topScore < scoreTmp){
topScore = scoreTmp;
topCrop = z;
}
}
zval cim;
ZVAL_LONG(&canvas_params[0], DW);
ZVAL_LONG(&canvas_params[1], DH);
ZVAL_STRING(&functionName, "imagecreatetruecolor");
call_user_function(EG(function_table),NULL,&functionName,&cim,2,canvas_params TSRMLS_CC);
ZVAL_ZVAL(&resize_params[0], &cim, 1, 0);
ZVAL_ZVAL(&resize_params[1], &rim, 1, 0);
ZVAL_LONG(&resize_params[2],0);
ZVAL_LONG(&resize_params[3],0);
ZVAL_LONG(&resize_params[4],*(crops+topCrop));
ZVAL_LONG(&resize_params[5],*(crops+topCrop+1));
ZVAL_LONG(&resize_params[6],DW);
ZVAL_LONG(&resize_params[7],DH);
ZVAL_LONG(&resize_params[8],DW);
ZVAL_LONG(&resize_params[9],DH);
ZVAL_STRING(&functionName, "imagecopyresampled");
call_user_function(EG(function_table),NULL,&functionName,&retval,10,resize_params TSRMLS_CC);
RETURN_ZVAL(&cim,1,0);
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
function definition, where the functions purpose is also documented. Please
follow this convention for the convenience of others editing your code.
*/
/* {{{ php_smartcrop_init_globals
*/
/* Uncomment this function if you have INI entries
static void php_smartcrop_init_globals(zend_smartcrop_globals *smartcrop_globals)
{
smartcrop_globals->global_value = 0;
smartcrop_globals->global_string = NULL;
}
*/
/* }}} */
/* {{{ PHP_MINIT_FUNCTION
*/
PHP_MINIT_FUNCTION(smartcrop)
{
/* If you have INI entries, uncomment these lines
REGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MSHUTDOWN_FUNCTION
*/
PHP_MSHUTDOWN_FUNCTION(smartcrop)
{
/* uncomment this line if you have INI entries
UNREGISTER_INI_ENTRIES();
*/
return SUCCESS;
}
/* }}} */
/* The previous line is meant for vim and emacs, so it can correctly fold and
unfold functions in source code. See the corresponding marks just before
function definition, where the functions purpose is also documented. Please
follow this convention for the convenience of others editing your code.
*/
/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
*/
PHP_RINIT_FUNCTION(smartcrop)
{
#if defined(COMPILE_DL_SMARTCROP) && defined(ZTS)
ZEND_TSRMLS_CACHE_UPDATE();
#endif
return SUCCESS;
}
/* }}} */
/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
*/
PHP_RSHUTDOWN_FUNCTION(smartcrop)
{
return SUCCESS;
}
/* }}} */
/* {{{ PHP_MINFO_FUNCTION
*/
PHP_MINFO_FUNCTION(smartcrop)
{
php_info_print_table_start();
php_info_print_table_header(2, "smartcrop support", "enabled");
php_info_print_table_end();
/* Remove comments if you have entries in php.ini
DISPLAY_INI_ENTRIES();
*/
}
/* }}} */
/* {{{ smartcrop_functions[]
*
* Every user visible function must have an entry in smartcrop_functions[].
*/
const zend_function_entry smartcrop_functions[] = {
PHP_FE(smartcrop,NULL) /* For testing, remove later. */
PHP_FE_END /* Must be the last line in smartcrop_functions[] */
};
/* }}} */
/* {{{ smartcrop_module_entry
*/
zend_module_entry smartcrop_module_entry = {
STANDARD_MODULE_HEADER,
"smartcrop",
smartcrop_functions,
PHP_MINIT(smartcrop),
PHP_MSHUTDOWN(smartcrop),
PHP_RINIT(smartcrop), /* Replace with NULL if there's nothing to do at request start */
PHP_RSHUTDOWN(smartcrop), /* Replace with NULL if there's nothing to do at request end */
PHP_MINFO(smartcrop),
PHP_SMARTCROP_VERSION,
STANDARD_MODULE_PROPERTIES
};
/* }}} */
#ifdef COMPILE_DL_SMARTCROP
#ifdef ZTS
ZEND_TSRMLS_CACHE_DEFINE()
#endif
ZEND_GET_MODULE(smartcrop)
#endif
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/