aboutsummaryrefslogtreecommitdiff
path: root/deduper/libpuzzle/php/libpuzzle/libpuzzle.c
blob: 82e84c36f667833e5518a95ea073c6efa103a197 (plain) (blame)
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
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include <puzzle.h>
#include "php_libpuzzle.h"

ZEND_DECLARE_MODULE_GLOBALS(libpuzzle)

/* True global resources - no need for thread safety here */
static int le_libpuzzle;

/* {{{ libpuzzle_functions[]
 */
zend_function_entry libpuzzle_functions[] = {
        PHP_FE(puzzle_set_max_width, NULL)
        PHP_FE(puzzle_set_max_height, NULL)
        PHP_FE(puzzle_set_lambdas, NULL)
        PHP_FE(puzzle_set_noise_cutoff, NULL)
        PHP_FE(puzzle_set_p_ratio, NULL)
        PHP_FE(puzzle_set_contrast_barrier_for_cropping, NULL)
        PHP_FE(puzzle_set_max_cropping_ratio, NULL)
        PHP_FE(puzzle_set_autocrop, NULL)
    
        PHP_FE(puzzle_fill_cvec_from_file, NULL)
        PHP_FE(puzzle_compress_cvec, NULL)
        PHP_FE(puzzle_uncompress_cvec, NULL)
        PHP_FE(puzzle_vector_normalized_distance, NULL)                

        {NULL, NULL, NULL}      /* Must be the last line in libpuzzle_functions[] */
};
/* }}} */

/* {{{ libpuzzle_module_entry
 */
zend_module_entry libpuzzle_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
        STANDARD_MODULE_HEADER,
#endif
        "libpuzzle",
        libpuzzle_functions,
        PHP_MINIT(libpuzzle),
        PHP_MSHUTDOWN(libpuzzle),
        PHP_RINIT(libpuzzle),           /* Replace with NULL if there's nothing to do at request start */
        PHP_RSHUTDOWN(libpuzzle),       /* Replace with NULL if there's nothing to do at request end */
        PHP_MINFO(libpuzzle),
#if ZEND_MODULE_API_NO >= 20010901
        "0.10", /* Replace with version number for your extension */
#endif
        STANDARD_MODULE_PROPERTIES
};
/* }}} */

#ifdef COMPILE_DL_LIBPUZZLE
ZEND_GET_MODULE(libpuzzle)
#endif


/* {{{ PHP_MINIT_FUNCTION
 */
PHP_MINIT_FUNCTION(libpuzzle)
{
    REGISTER_DOUBLE_CONSTANT("PUZZLE_CVEC_SIMILARITY_THRESHOLD",
                             PUZZLE_CVEC_SIMILARITY_THRESHOLD,
                             CONST_CS | CONST_PERSISTENT);
    REGISTER_DOUBLE_CONSTANT("PUZZLE_CVEC_SIMILARITY_HIGH_THRESHOLD",
                             PUZZLE_CVEC_SIMILARITY_HIGH_THRESHOLD,
                             CONST_CS | CONST_PERSISTENT);
    REGISTER_DOUBLE_CONSTANT("PUZZLE_CVEC_SIMILARITY_LOW_THRESHOLD",
                             PUZZLE_CVEC_SIMILARITY_LOW_THRESHOLD,
                             CONST_CS | CONST_PERSISTENT);
    REGISTER_DOUBLE_CONSTANT("PUZZLE_CVEC_SIMILARITY_LOWER_THRESHOLD",
                             PUZZLE_CVEC_SIMILARITY_LOWER_THRESHOLD,
                             CONST_CS | CONST_PERSISTENT);
    return SUCCESS;
}
/* }}} */

/* {{{ PHP_MSHUTDOWN_FUNCTION
 */
PHP_MSHUTDOWN_FUNCTION(libpuzzle)
{
        return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request start */
/* {{{ PHP_RINIT_FUNCTION
 */
PHP_RINIT_FUNCTION(libpuzzle)
{
    puzzle_init_context(&LIBPUZZLE_G(global_context));
    return SUCCESS;
}
/* }}} */

/* Remove if there's nothing to do at request end */
/* {{{ PHP_RSHUTDOWN_FUNCTION
 */
PHP_RSHUTDOWN_FUNCTION(libpuzzle)
{
    puzzle_free_context(&LIBPUZZLE_G(global_context));
    return SUCCESS;
}
/* }}} */

/* {{{ PHP_MINFO_FUNCTION
 */
PHP_MINFO_FUNCTION(libpuzzle)
{
        php_info_print_table_start();
        php_info_print_table_header(2, "libpuzzle support", "enabled");
        php_info_print_table_end();
}
/* }}} */

/* {{{ proto string puzzle_fill_cvec_from_file(string filename)
 * Creates a signature out of an image file */
PHP_FUNCTION(puzzle_fill_cvec_from_file)
{    
    char *arg = NULL;
    int arg_len;
    PuzzleContext *context;
    PuzzleCvec cvec;
    
    context = &LIBPUZZLE_G(global_context);
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
                              "s", &arg, &arg_len) == FAILURE ||
        arg_len <= 0) {
        RETURN_FALSE;
    }
    puzzle_init_cvec(context, &cvec);
    if (puzzle_fill_cvec_from_file(context, &cvec, arg) != 0) {
        puzzle_free_cvec(context, &cvec);
        RETURN_FALSE;
    }
    RETVAL_STRINGL(cvec.vec, cvec.sizeof_vec, 1);
    puzzle_free_cvec(context, &cvec);
}
/* }}} */

/* {{{ proto string puzzle_compress_cvec(string cvec)
 * Compress a signature to save storage space */
PHP_FUNCTION(puzzle_compress_cvec)
{    
    char *arg = NULL;
    int arg_len;
    PuzzleContext *context;
    PuzzleCompressedCvec compressed_cvec;
    PuzzleCvec cvec;
    
    context = &LIBPUZZLE_G(global_context);
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
                              "s", &arg, &arg_len) == FAILURE ||
        arg_len <= 0) {
        RETURN_FALSE;
    }
    puzzle_init_compressed_cvec(context, &compressed_cvec);
    puzzle_init_cvec(context, &cvec);
    cvec.vec = arg;
    cvec.sizeof_vec = (size_t) arg_len;    
    if (puzzle_compress_cvec(context, &compressed_cvec, &cvec) != 0) {
        puzzle_free_compressed_cvec(context, &compressed_cvec);
        cvec.vec = NULL;
        puzzle_free_cvec(context, &cvec);        
        RETURN_FALSE;
    }
    RETVAL_STRINGL(compressed_cvec.vec,
                   compressed_cvec.sizeof_compressed_vec, 1);
    puzzle_free_compressed_cvec(context, &compressed_cvec);
    cvec.vec = NULL;
    puzzle_free_cvec(context, &cvec);    
}
/* }}} */

/* {{{ proto string puzzle_uncompress_cvec(string compressed_cvec)
 * Uncompress a compressed signature so that it can be used for computations */
PHP_FUNCTION(puzzle_uncompress_cvec)
{    
    char *arg = NULL;
    int arg_len;
    PuzzleContext *context;
    PuzzleCompressedCvec compressed_cvec;
    PuzzleCvec cvec;
    
    context = &LIBPUZZLE_G(global_context);
    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC,
                              "s", &arg, &arg_len) == FAILURE ||
        arg_len <= 0) {
        RETURN_FALSE;
    }
    puzzle_init_compressed_cvec(context, &compressed_cvec);
    puzzle_init_cvec(context, &cvec);
    compressed_cvec.vec = arg;
    compressed_cvec.sizeof_compressed_vec = (size_t) arg_len;    
    if (puzzle_uncompress_cvec(context, &compressed_cvec, &cvec) != 0) {
        puzzle_free_cvec(context, &cvec);
        compressed_cvec.vec = NULL;
        puzzle_free_compressed_cvec(context, &compressed_cvec);
        RETURN_FALSE;
    }
    RETVAL_STRINGL(cvec.vec, cvec.sizeof_vec, 1);
    puzzle_free_cvec(context, &cvec);
    compressed_cvec.vec = NULL;
    puzzle_free_compressed_cvec(context, &compressed_cvec);    
}
/* }}} */

/* {{{ proto double puzzle_vector_normalized_distance(string cvec1, string cvec2 [, bool fix_for_texts])
 * Computes the distance between two signatures. Result is between 0.0 and 1.0 */
PHP_FUNCTION(puzzle_vector_normalized_distance)
{    
    char *vec1 = NULL, *vec2 = NULL;
    int vec1_len, vec2_len;
    PuzzleContext *context;
    PuzzleCvec cvec1, cvec2;
    double d;
    zend_bool fix_for_texts;
    
    context = &LIBPUZZLE_G(global_context);
    if (zend_parse_parameters
        (ZEND_NUM_ARGS() TSRMLS_CC, "ss|b",
         &vec1, &vec1_len, &vec2, &vec2_len, &fix_for_texts) == FAILURE ||
        vec1_len <= 0 || vec2_len <= 0) {
        RETURN_FALSE;
    }
    if (ZEND_NUM_ARGS() TSRMLS_CC < 3) {
        fix_for_texts = (zend_bool) 1;
    }
    puzzle_init_cvec(context, &cvec1);
    puzzle_init_cvec(context, &cvec2);    
    cvec1.vec = vec1;
    cvec1.sizeof_vec = (size_t) vec1_len;
    cvec2.vec = vec2;
    cvec2.sizeof_vec = (size_t) vec2_len;
    d = puzzle_vector_normalized_distance(context, &cvec1, &cvec2,
                                          (int) fix_for_texts);
    cvec1.vec = cvec2.vec = NULL;
    puzzle_free_cvec(context, &cvec1);
    puzzle_free_cvec(context, &cvec2);
    RETVAL_DOUBLE(d);
}
/* }}} */

/* {{{ proto bool puzzle_set_max_width(int width)
 * Set the maximum picture width */
PHP_FUNCTION(puzzle_set_max_width)
{
    PuzzleContext *context;
    long width;
    
    context = &LIBPUZZLE_G(global_context);
    if (zend_parse_parameters
        (ZEND_NUM_ARGS() TSRMLS_CC, "l", &width) == FAILURE ||
        width <= 0L || width > INT_MAX) {
        RETURN_FALSE;
    }
    if (puzzle_set_max_width(context, (unsigned int) width) != 0) {
        RETURN_FALSE;
    }
    RETVAL_TRUE;    
}
/* }}} */

/* {{{ proto bool puzzle_set_max_height(int height)
 * Set the maximum picture height */
PHP_FUNCTION(puzzle_set_max_height)
{
    PuzzleContext *context;
    long height;
    
    context = &LIBPUZZLE_G(global_context);
    if (zend_parse_parameters
        (ZEND_NUM_ARGS() TSRMLS_CC, "l", &height) == FAILURE ||
        height <= 0L || height > INT_MAX) {
        RETURN_FALSE;
    }
    if (puzzle_set_max_height(context, (unsigned int) height) != 0) {
        RETURN_FALSE;
    }
    RETVAL_TRUE;    
}
/* }}} */

/* {{{ proto bool puzzle_set_lambdas(int lambdas)
 * Set the size of the computation grid */
PHP_FUNCTION(puzzle_set_lambdas)
{
    PuzzleContext *context;
    long lambdas;
    
    context = &LIBPUZZLE_G(global_context);
    if (zend_parse_parameters
        (ZEND_NUM_ARGS() TSRMLS_CC, "l", &lambdas) == FAILURE ||
        lambdas <= 0L || lambdas > INT_MAX) {
        RETURN_FALSE;
    }
    if (puzzle_set_lambdas(context, (unsigned int) lambdas) != 0) {
        RETURN_FALSE;
    }
    RETVAL_TRUE;    
}
/* }}} */

/* {{{ proto bool puzzle_set_noise_cutoff(double cutoff)
 * Set the noise cutoff level */
PHP_FUNCTION(puzzle_set_noise_cutoff)
{
    PuzzleContext *context;
    double cutoff;
    
    context = &LIBPUZZLE_G(global_context);
    if (zend_parse_parameters
        (ZEND_NUM_ARGS() TSRMLS_CC, "d", &cutoff) == FAILURE) {
        RETURN_FALSE;
    }
    if (puzzle_set_noise_cutoff(context, cutoff) != 0) {
        RETURN_FALSE;
    }
    RETVAL_TRUE;    
}
/* }}} */

/* {{{ proto bool puzzle_set_p_ratio(double ratio)
 * Set the p_ratio */
PHP_FUNCTION(puzzle_set_p_ratio)
{
    PuzzleContext *context;
    double p_ratio;
    
    context = &LIBPUZZLE_G(global_context);
    if (zend_parse_parameters
        (ZEND_NUM_ARGS() TSRMLS_CC, "d", &p_ratio) == FAILURE) {
        RETURN_FALSE;
    }
    if (puzzle_set_p_ratio(context, p_ratio) != 0) {
        RETURN_FALSE;
    }
    RETVAL_TRUE;
}
/* }}} */

/* {{{ proto bool puzzle_set_contrast_barrier_for_cropping(double barrier)
 * Set the tolerance level for cropping */
PHP_FUNCTION(puzzle_set_contrast_barrier_for_cropping)
{
    PuzzleContext *context;
    double barrier;
    
    context = &LIBPUZZLE_G(global_context);
    if (zend_parse_parameters
        (ZEND_NUM_ARGS() TSRMLS_CC, "d", &barrier) == FAILURE) {
        RETURN_FALSE;
    }
    if (puzzle_set_contrast_barrier_for_cropping(context, barrier) != 0) {
        RETURN_FALSE;
    }
    RETVAL_TRUE;
}
/* }}} */

/* {{{ proto bool puzzle_set_max_cropping_ratio(double ratio)
 * Set the maximum ratio between the cropped area and the whole picture */
PHP_FUNCTION(puzzle_set_max_cropping_ratio)
{
    PuzzleContext *context;
    double ratio;
    
    context = &LIBPUZZLE_G(global_context);
    if (zend_parse_parameters
        (ZEND_NUM_ARGS() TSRMLS_CC, "d", &ratio) == FAILURE) {
        RETURN_FALSE;
    }
    if (puzzle_set_max_cropping_ratio(context, ratio) != 0) {
        RETURN_FALSE;
    }
    RETVAL_TRUE;
}
/* }}} */

/* {{{ proto bool puzzle_set_autocrop(bool autocrop)
 * TRUE to enable autocropping, FALSE to disable */
PHP_FUNCTION(puzzle_set_autocrop)
{
    PuzzleContext *context;
    zend_bool autocrop;
    
    context = &LIBPUZZLE_G(global_context);
    if (zend_parse_parameters
        (ZEND_NUM_ARGS() TSRMLS_CC, "b", &autocrop) == FAILURE) {
        RETURN_FALSE;
    }
    if (puzzle_set_autocrop(context, (int) autocrop) != 0) {
        RETURN_FALSE;
    }
    RETVAL_TRUE;
}
/* }}} */

/*
 * 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
 */