-
Notifications
You must be signed in to change notification settings - Fork 11
/
vips.c
2229 lines (1837 loc) · 51.5 KB
/
vips.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
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* C layer of the php-vips binding.
*/
/* Uncomment for some logging.
#define VIPS_DEBUG
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include "php_ini.h"
#include "ext/standard/info.h"
#include "php_vips.h"
#include <vips/vips.h>
#include <vips/debug.h>
#include <vips/vector.h>
/* If you declare any globals in php_vips.h uncomment this:
ZEND_DECLARE_MODULE_GLOBALS(vips)
*/
/* backward compat macros */
#ifndef IS_VOID
# define IS_VOID 0
#endif /* !defined(IS_VOID) */
#ifndef IS_MIXED
# define IS_MIXED 0
#endif /* !defined(IS_MIXED) */
#ifndef ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX
# define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(name, return_reference, num_args, type) \
ZEND_BEGIN_ARG_INFO_EX(name, 0, return_reference, num_args)
#endif /* !defined(ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX) */
#ifndef ZEND_ARG_TYPE_MASK
# define ZEND_ARG_TYPE_MASK(pass_by_ref, name, type_mask, default_value) \
ZEND_ARG_TYPE_INFO(pass_by_ref, name, 0, 0)
#endif /* !defined(ZEND_ARG_TYPE_MASK) */
#ifndef ZEND_ARG_VARIADIC_TYPE_INFO
# define ZEND_ARG_VARIADIC_TYPE_INFO(pass_by_ref, name, type_hint, allow_null) { #name, NULL, type_hint, pass_by_ref, allow_null, 1 },
#endif /* !defined(ZEND_ARG_VARIADIC_TYPE_INFO) */
#ifndef ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE
# define ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(pass_by_ref, name, type_hint, allow_null, default_value) \
ZEND_ARG_TYPE_INFO(pass_by_ref, name, type_hint, allow_null)
#endif /* !defined(ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE) */
#if PHP_VERSION_ID < 70200
# undef ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX
# define ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(name, return_reference, required_num_args, class_name, allow_null) \
static const zend_internal_arg_info name[] = { \
{ (const char*)(zend_uintptr_t)(required_num_args), ( #class_name ), 0, return_reference, allow_null, 0 },
#endif /* PHP_VERSION_ID < 70200 */
#include "vips_arginfo.h"
/* True global resources - no need for thread safety here */
static int le_gobject;
/* {{{ PHP_INI
*/
/* Remove comments and fill if you need to have entries in php.ini
PHP_INI_BEGIN()
STD_PHP_INI_ENTRY("vips.global_value", "42", PHP_INI_ALL, OnUpdateLong, global_value, zend_vips_globals, vips_globals)
STD_PHP_INI_ENTRY("vips.global_string", "foobar", PHP_INI_ALL, OnUpdateString, global_string, zend_vips_globals, vips_globals)
PHP_INI_END()
*/
/* }}} */
/* {{{ proto static int vips_php_call_array(const char *operation_name, zval *instance, const char *option_string, int argc, zval *argv, zval *return_value)
Call any vips operation. */
/* Track stuff during a call to a vips operation in one of these.
*/
typedef struct _VipsPhpCall {
/* Parameters.
*/
const char *operation_name;
zval *instance;
const char *option_string;
int argc;
zval *argv;
/* The operation we are calling.
*/
VipsOperation *operation;
/* The num of args this operation needs from php. This does not include the
* @instance zval.
*/
int args_required;
/* If we've already used the instance zval.
*/
gboolean used_instance;
/* Extra php array of optional args.
*/
zval *options;
/* The first image arg ... the thing we expand constants to match.
*/
VipsImage *match_image;
} VipsPhpCall;
static void
vips_php_call_free(VipsPhpCall *call)
{
VIPS_DEBUG_MSG("vips_php_call_free:\n");
VIPS_UNREF(call->operation);
g_free(call);
}
static VipsPhpCall *
vips_php_call_new(const char *operation_name, zval *instance,
const char *option_string, int argc, zval *argv)
{
VipsPhpCall *call;
VIPS_DEBUG_MSG("vips_php_call_new: %s\n", operation_name );
VIPS_DEBUG_MSG(" option_string = \"%s\", argc = %d\n",
option_string, argc);
call = g_new0( VipsPhpCall, 1 );
call->operation_name = operation_name;
call->instance = instance;
call->option_string = option_string;
call->argc = argc;
call->argv = argv;
if (!(call->operation = vips_operation_new(operation_name))) {
vips_php_call_free(call);
return NULL;
}
return call;
}
/* Get a non-reference zval. In php7, zvalues can be references to other zvals
* ... chase down a chain of refs to get a real zval.
* the ref pointer chain.
*/
static inline zval *
zval_get_nonref(zval *zvalue)
{
while (Z_TYPE_P(zvalue) == IS_REFERENCE)
zvalue = Z_REFVAL_P(zvalue);
return zvalue;
}
/* First pass over our arguments: find the first image arg and note as
* match_image.
*/
static void
vips_php_analyze_arg(VipsPhpCall *call, zval *zvalue)
{
zvalue = zval_get_nonref(zvalue);
if (Z_TYPE_P(zvalue) == IS_ARRAY) {
const int n = zend_hash_num_elements(Z_ARRVAL_P(zvalue));
int i;
for (i = 0; i < n; i++) {
zval *item = zend_hash_index_find(Z_ARRVAL_P(zvalue), i);
if (item) {
vips_php_analyze_arg(call, item);
}
}
}
else if (Z_TYPE_P(zvalue) == IS_RESOURCE) {
VipsImage *image;
if( (image = (VipsImage *)zend_fetch_resource(Z_RES_P(zvalue),
"GObject", le_gobject)) != NULL) {
if (!call->match_image) {
call->match_image = image;
}
}
}
}
static int
vips_php_blob_free(void *buf, void *area)
{
g_free(buf);
return 0;
}
/* Expand a constant (eg. 12, "12" or [1, 2, 3]) into an image using
* @match_image as a guide.
*/
static VipsImage *
expand_constant(VipsImage *match_image, zval *constant)
{
VipsImage *result;
VipsImage *x;
if (vips_black(&result, 1, 1, NULL)) {
return NULL;
}
constant = zval_get_nonref(constant);
if (Z_TYPE_P(constant) == IS_ARRAY) {
const int n = zend_hash_num_elements(Z_ARRVAL_P(constant));
double *ones;
double *offsets;
int i;
ones = VIPS_ARRAY(result, n, double);
offsets = VIPS_ARRAY(result, n, double);
for (i = 0; i < n; i++) {
zval *ele;
ones[i] = 1.0;
if ((ele = zend_hash_index_find(Z_ARRVAL_P(constant), i)) != NULL) {
offsets[i] = zval_get_double(ele);
}
}
if (vips_linear(result, &x, ones, offsets, n, NULL)) {
return NULL;
}
g_object_unref(result);
result = x;
}
else {
if (vips_linear1(result, &x, 1.0, zval_get_double(constant), NULL)) {
return NULL;
}
g_object_unref(result);
result = x;
}
if (vips_cast(result, &x, match_image->BandFmt, NULL)) {
return NULL;
}
g_object_unref(result);
result = x;
if (vips_embed(result, &x, 0, 0, match_image->Xsize, match_image->Ysize,
"extend", VIPS_EXTEND_COPY,
NULL)) {
return NULL;
}
g_object_unref(result);
result = x;
result->Type = match_image->Type;
result->Xres = match_image->Xres;
result->Yres = match_image->Yres;
result->Xoffset = match_image->Xoffset;
result->Yoffset = match_image->Yoffset;
return result;
}
/* Is a zval a rectangular 2D array.
*/
static gboolean
is_2D(zval *array)
{
int height;
zval *row;
int width;
int y;
array = zval_get_nonref(array);
if (Z_TYPE_P(array) != IS_ARRAY) {
return FALSE;
}
height = zend_hash_num_elements(Z_ARRVAL_P(array));
if ((row = zend_hash_index_find(Z_ARRVAL_P(array), 0)) == NULL ||
!(row = zval_get_nonref(row)) ||
Z_TYPE_P(row) != IS_ARRAY) {
return FALSE;
}
width = zend_hash_num_elements(Z_ARRVAL_P(row));
for (y = 1; y < height; y++) {
if ((row = zend_hash_index_find(Z_ARRVAL_P(array), y)) == NULL ||
!(row = zval_get_nonref(row)) ||
Z_TYPE_P(row) != IS_ARRAY ||
zend_hash_num_elements(Z_ARRVAL_P(row)) != width) {
return FALSE;
}
}
return TRUE;
}
/* Make a vips matrix image from a 2D zval. @array must have passed is_2D()
* before calling this.
*/
static VipsImage *
matrix_from_zval(zval *array)
{
int width;
int height;
zval *row;
VipsImage *mat;
int x, y;
array = zval_get_nonref(array);
height = zend_hash_num_elements(Z_ARRVAL_P(array));
row = zend_hash_index_find(Z_ARRVAL_P(array), 0);
row = zval_get_nonref(row);
g_assert(Z_TYPE_P(row) == IS_ARRAY);
width = zend_hash_num_elements(Z_ARRVAL_P(row));
mat = vips_image_new_matrix(width, height);
for (y = 0; y < height; y++) {
row = zend_hash_index_find(Z_ARRVAL_P(array), y);
row = zval_get_nonref(row);
g_assert(Z_TYPE_P(row) == IS_ARRAY);
g_assert(zend_hash_num_elements(Z_ARRVAL_P(row)) == width);
for (x = 0; x < width; x++) {
zval *ele;
ele = zend_hash_index_find(Z_ARRVAL_P(row), x);
*VIPS_MATRIX(mat, x, y) = zval_get_double(ele);
}
}
return mat;
}
/* Turn a zval into an image. An image stays an image, a 2D array of numbers
* becomes a matrix image, a 1D array or a simple constant is expanded to
* match @match_image.
*/
static VipsImage *
imageize(VipsImage *match_image, zval *zvalue)
{
VipsImage *image;
zvalue = zval_get_nonref(zvalue);
if (Z_TYPE_P(zvalue) == IS_RESOURCE &&
(image = (VipsImage *)
zend_fetch_resource(Z_RES_P(zvalue), "GObject", le_gobject))) {
return image;
}
else if (is_2D(zvalue)) {
return matrix_from_zval(zvalue);
}
else if (match_image) {
return expand_constant(match_image, zvalue);
}
else {
php_error_docref(NULL, E_WARNING, "not a VipsImage");
return NULL;
}
}
static int
zval_to_array_image(VipsImage *match_image, zval *zvalue, GValue *gvalue)
{
VipsImage **arr;
VipsImage *image;
int n;
int i;
zvalue = zval_get_nonref(zvalue);
if (Z_TYPE_P(zvalue) == IS_ARRAY) {
n = zend_hash_num_elements(Z_ARRVAL_P(zvalue));
}
else {
n = 1;
}
vips_value_set_array_image(gvalue, n);
arr = vips_value_get_array_image(gvalue, NULL);
if (Z_TYPE_P(zvalue) == IS_ARRAY) {
for (i = 0; i < n; i++) {
zval *ele;
ele = zend_hash_index_find(Z_ARRVAL_P(zvalue), i);
if (!ele) {
php_error_docref(NULL, E_WARNING, "element missing from array");
return -1;
}
if (!(image = imageize(match_image, ele))) {
return -1;
}
arr[i] = image;
g_object_ref(image);
}
}
else {
if (!(image = imageize(match_image, zvalue))) {
return -1;
}
arr[0] = image;
g_object_ref(image);
}
return 0;
}
/* Set a gvalue from a php value.
*
* You must set the type of the gvalue before calling this to hint what kind
* of gvalue to make. For example if type is an enum, a zval string will be
* used to look up the enum nick.
*
* If non-NULL, @match_image is used to turn constants into images.
*/
static int
vips_php_zval_to_gval(VipsImage *match_image, zval *zvalue, GValue *gvalue)
{
GType type = G_VALUE_TYPE(gvalue);
/* The fundamental type ... eg. G_TYPE_ENUM for a VIPS_TYPE_KERNEL, or
* G_TYPE_OBJECT for VIPS_TYPE_IMAGE().
*/
GType fundamental = G_TYPE_FUNDAMENTAL(type);
VipsImage *image;
zend_string *zstr;
int enum_value;
switch (fundamental) {
case G_TYPE_STRING:
/* These are GStrings, vips refstrings are handled by boxed, see
* below.
*/
zstr = zval_get_string(zvalue);
g_value_set_string(gvalue, ZSTR_VAL(zstr));
zend_string_release(zstr);
break;
case G_TYPE_OBJECT:
if (!(image = imageize(match_image, zvalue))) {
return -1;
}
g_value_set_object(gvalue, image);
break;
case G_TYPE_INT:
g_value_set_int(gvalue, zval_get_long(zvalue));
break;
case G_TYPE_UINT64:
g_value_set_uint64(gvalue, zval_get_long(zvalue));
break;
case G_TYPE_BOOLEAN:
g_value_set_boolean(gvalue, zval_get_long(zvalue));
break;
case G_TYPE_ENUM:
zvalue = zval_get_nonref(zvalue);
if (Z_TYPE_P(zvalue) == IS_LONG) {
enum_value = Z_LVAL_P(zvalue);
}
else if (Z_TYPE_P(zvalue) == IS_DOUBLE) {
enum_value = Z_DVAL_P(zvalue);
}
else {
zstr = zval_get_string(zvalue);
enum_value = vips_enum_from_nick("enum", type, ZSTR_VAL(zstr));
if (enum_value < 0) {
zend_string_release(zstr);
return -1;
}
zend_string_release(zstr);
}
g_value_set_enum(gvalue, enum_value);
break;
case G_TYPE_FLAGS:
g_value_set_flags(gvalue, zval_get_long(zvalue));
break;
case G_TYPE_DOUBLE:
g_value_set_double(gvalue, zval_get_double(zvalue));
break;
case G_TYPE_BOXED:
if (type == VIPS_TYPE_REF_STRING) {
zstr = zval_get_string(zvalue);
vips_value_set_ref_string(gvalue, ZSTR_VAL(zstr));
zend_string_release(zstr);
}
else if (type == VIPS_TYPE_BLOB) {
void *buf;
zvalue = zval_get_nonref(zvalue);
zstr = zval_get_string(zvalue);
buf = g_malloc(ZSTR_LEN(zstr));
memcpy(buf, ZSTR_VAL(zstr), ZSTR_LEN(zstr));
zend_string_release(zstr);
vips_value_set_blob(gvalue,
vips_php_blob_free, buf, Z_STRLEN_P(zvalue));
}
else if (type == VIPS_TYPE_ARRAY_INT) {
int *arr;
int n;
int i;
zvalue = zval_get_nonref(zvalue);
if (Z_TYPE_P(zvalue) == IS_ARRAY) {
n = zend_hash_num_elements(Z_ARRVAL_P(zvalue));
}
else {
n = 1;
}
vips_value_set_array_int(gvalue, NULL, n);
arr = vips_value_get_array_int(gvalue, NULL);
if (Z_TYPE_P(zvalue) == IS_ARRAY) {
for (i = 0; i < n; i++) {
zval *ele;
ele = zend_hash_index_find(Z_ARRVAL_P(zvalue), i);
if (ele) {
arr[i] = zval_get_long(ele);
}
}
}
else {
arr[0] = zval_get_long(zvalue);
}
}
else if (type == VIPS_TYPE_ARRAY_DOUBLE) {
double *arr;
int n;
int i;
zvalue = zval_get_nonref(zvalue);
if (Z_TYPE_P(zvalue) == IS_ARRAY) {
n = zend_hash_num_elements(Z_ARRVAL_P(zvalue));
}
else {
n = 1;
}
vips_value_set_array_double(gvalue, NULL, n);
arr = vips_value_get_array_double(gvalue, NULL);
if (Z_TYPE_P(zvalue) == IS_ARRAY) {
for (i = 0; i < n; i++) {
zval *ele;
ele = zend_hash_index_find(Z_ARRVAL_P(zvalue), i);
if (ele) {
arr[i] = zval_get_double(ele);
}
}
}
else {
arr[0] = zval_get_double(zvalue);
}
}
else if (type == VIPS_TYPE_ARRAY_IMAGE) {
if (zval_to_array_image(match_image, zvalue, gvalue)) {
return -1;
}
}
else {
g_warning( "%s: unimplemented boxed type %s",
G_STRLOC, g_type_name(type) );
}
break;
default:
g_warning( "%s: unimplemented GType %s",
G_STRLOC, g_type_name(fundamental) );
break;
}
return 0;
}
static int
vips_php_set_value(VipsPhpCall *call,
GParamSpec *pspec, VipsArgumentFlags flags, zval *zvalue)
{
const char *name = g_param_spec_get_name(pspec);
GType pspec_type = G_PARAM_SPEC_VALUE_TYPE(pspec);
GValue gvalue = { 0 };
g_value_init(&gvalue, pspec_type);
if (vips_php_zval_to_gval(call->match_image, zvalue, &gvalue)) {
g_value_unset(&gvalue);
return -1;
}
/* If we are setting a MODIFY VipsArgument with an image, we need to take a
* copy.
*/
if (g_type_is_a(pspec_type, VIPS_TYPE_IMAGE) &&
(flags & VIPS_ARGUMENT_MODIFY)) {
VipsImage *image;
VipsImage *memory;
VIPS_DEBUG_MSG("vips_php_set_value: copying image\n");
image = (VipsImage *) g_value_get_object(&gvalue);
memory = vips_image_new_memory();
if (vips_image_write(image, memory)) {
g_object_unref(memory);
g_value_unset(&gvalue);
return -1;
}
g_value_unset(&gvalue);
g_value_init(&gvalue, pspec_type);
g_value_set_object(&gvalue, memory);
}
#ifdef VIPS_DEBUG
{
char *str_value;
str_value = g_strdup_value_contents(&gvalue);
VIPS_DEBUG_MSG(" %s.%s = %s\n", call->operation_name, name, str_value);
g_free(str_value);
}
#endif/*VIPS_DEBUG*/
g_object_set_property(G_OBJECT(call->operation), name, &gvalue);
g_value_unset(&gvalue);
return 0;
}
static void *
vips_php_set_required_input(VipsObject *object,
GParamSpec *pspec, VipsArgumentClass *argument_class,
VipsArgumentInstance *argument_instance,
void *a, void *b)
{
VipsPhpCall *call = (VipsPhpCall *) a;
if ((argument_class->flags & VIPS_ARGUMENT_REQUIRED) &&
(argument_class->flags & VIPS_ARGUMENT_CONSTRUCT) &&
(argument_class->flags & VIPS_ARGUMENT_INPUT) &&
!(argument_class->flags & VIPS_ARGUMENT_DEPRECATED) &&
!argument_instance->assigned) {
zval *arg;
/* If this arg needs an image, we use instance, if we can.
*/
arg = NULL;
if (G_PARAM_SPEC_VALUE_TYPE(pspec) == VIPS_TYPE_IMAGE &&
call->instance &&
!call->used_instance) {
arg = call->instance;
call->used_instance = TRUE;
}
else if (call->args_required < call->argc) {
/* Pick the next zval off the supplied arg list.
*/
arg = &call->argv[call->args_required];
call->args_required += 1;
}
if (arg &&
vips_php_set_value(call, pspec, argument_class->flags, arg)) {
return call;
}
}
return NULL;
}
/* Set all optional arguments.
*/
static int
vips_php_set_optional_input(VipsPhpCall *call, zval *options)
{
zend_string *key;
zval *value;
VIPS_DEBUG_MSG("vips_php_set_optional_input:\n");
options = zval_get_nonref(options);
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(options), key, value) {
char *name;
GParamSpec *pspec;
VipsArgumentClass *argument_class;
VipsArgumentInstance *argument_instance;
if (key == NULL) {
continue;
}
name = ZSTR_VAL(key);
if (vips_object_get_argument(VIPS_OBJECT(call->operation), name,
&pspec, &argument_class, &argument_instance)) {
return -1;
}
if (!(argument_class->flags & VIPS_ARGUMENT_REQUIRED) &&
(argument_class->flags & VIPS_ARGUMENT_INPUT) &&
!(argument_class->flags & VIPS_ARGUMENT_DEPRECATED) &&
vips_php_set_value(call, pspec, argument_class->flags, value)) {
return -1;
}
} ZEND_HASH_FOREACH_END();
return 0;
}
/* Set a php zval from a gvalue.
*/
static int
vips_php_gval_to_zval(GValue *gvalue, zval *zvalue)
{
GType type = G_VALUE_TYPE(gvalue);
/* The fundamental type ... eg. G_TYPE_ENUM for a VIPS_TYPE_KERNEL, or
* G_TYPE_OBJECT for VIPS_TYPE_IMAGE().
*/
GType fundamental = G_TYPE_FUNDAMENTAL(type);
const char *str;
GObject *gobject;
zend_resource *resource;
int enum_value;
switch (fundamental) {
case G_TYPE_STRING:
/* These are GStrings, vips refstrings are handled by boxed, see
* below.
*/
str = g_value_get_string(gvalue);
ZVAL_STRING(zvalue, str);
break;
case G_TYPE_OBJECT:
gobject = g_value_get_object(gvalue);
resource = zend_register_resource(gobject, le_gobject);
ZVAL_RES(zvalue, resource);
break;
case G_TYPE_INT:
ZVAL_LONG(zvalue, g_value_get_int(gvalue));
break;
case G_TYPE_UINT64:
ZVAL_LONG(zvalue, g_value_get_uint64(gvalue));
break;
case G_TYPE_BOOLEAN:
ZVAL_LONG(zvalue, g_value_get_boolean(gvalue));
break;
case G_TYPE_ENUM:
enum_value = g_value_get_enum(gvalue);
str = vips_enum_nick(type, enum_value);
ZVAL_STRING(zvalue, str);
break;
case G_TYPE_FLAGS:
ZVAL_LONG(zvalue, g_value_get_flags(gvalue));
break;
case G_TYPE_DOUBLE:
ZVAL_DOUBLE(zvalue, g_value_get_double(gvalue));
break;
case G_TYPE_BOXED:
if (type == VIPS_TYPE_REF_STRING ||
type == VIPS_TYPE_BLOB) {
const char *str;
size_t str_len;
str = vips_value_get_ref_string(gvalue, &str_len);
ZVAL_STRINGL(zvalue, str, str_len);
}
else if (type == VIPS_TYPE_ARRAY_DOUBLE) {
double *arr;
int n;
int i;
arr = vips_value_get_array_double(gvalue, &n);
array_init(zvalue);
for (i = 0; i < n; i++) {
add_next_index_double(zvalue, arr[i]);
}
}
else if (type == VIPS_TYPE_ARRAY_INT) {
int *arr;
int n;
int i;
arr = vips_value_get_array_int(gvalue, &n);
array_init(zvalue);
for (i = 0; i < n; i++) {
add_next_index_long(zvalue, arr[i]);
}
}
else if (type == VIPS_TYPE_ARRAY_IMAGE) {
VipsImage **arr;
int n;
int i;
arr = vips_value_get_array_image(gvalue, &n);
array_init(zvalue);
for (i = 0; i < n; i++) {
zval x;
g_object_ref(arr[i]);
resource = zend_register_resource(arr[i], le_gobject);
ZVAL_RES(&x, resource);
add_next_index_zval(zvalue, &x);
}
}
else {
g_warning( "%s: unimplemented boxed type %s",
G_STRLOC, g_type_name(type));
}
break;
default:
g_warning( "%s: unimplemented GType %s",
G_STRLOC, g_type_name(fundamental));
break;
}
return 0;
}
static int
vips_php_get_value(VipsPhpCall *call, GParamSpec *pspec, zval *zvalue)
{
const char *name = g_param_spec_get_name(pspec);
GType pspec_type = G_PARAM_SPEC_VALUE_TYPE(pspec);
GValue gvalue = { 0 };
g_value_init(&gvalue, pspec_type);
g_object_get_property(G_OBJECT(call->operation), name, &gvalue);
if (vips_php_gval_to_zval(&gvalue, zvalue)) {
g_value_unset(&gvalue);
return -1;
}
#ifdef VIPS_DEBUG
{
char *str_value;
str_value = g_strdup_value_contents(&gvalue);
VIPS_DEBUG_MSG(" %s.%s = %s\n", call->operation_name, name, str_value);
g_free(str_value);
}
#endif/*VIPS_DEBUG*/
g_value_unset(&gvalue);
return 0;
}
static void *
vips_php_get_required_output(VipsObject *object,
GParamSpec *pspec, VipsArgumentClass *argument_class,
VipsArgumentInstance *argument_instance,
void *a, void *b)
{
VipsPhpCall *call = (VipsPhpCall *) a;
zval *return_value = (zval *) b;
/* We get output objects, and we get input objects that are tagged as
* MODIFY --- they are copied on set, see above.
*/
if ((argument_class->flags & VIPS_ARGUMENT_REQUIRED) &&
(argument_class->flags & (VIPS_ARGUMENT_OUTPUT| VIPS_ARGUMENT_MODIFY)) &&
!(argument_class->flags & VIPS_ARGUMENT_DEPRECATED)) {
const char *name = g_param_spec_get_name(pspec);
zval zvalue;
if (vips_php_get_value(call, pspec, &zvalue)) {
return call;
}
add_assoc_zval(return_value, name, &zvalue);
}
return NULL;
}
static int
vips_php_get_optional_output(VipsPhpCall *call, zval *options,
zval *return_value)
{
zend_string *key;
zval *value;
VIPS_DEBUG_MSG("vips_php_get_optional_output:\n");
options = zval_get_nonref(options);
ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(options), key, value) {
char *name;
GParamSpec *pspec;
VipsArgumentClass *argument_class;
VipsArgumentInstance *argument_instance;
if (key == NULL) {
continue;
}
/* value should always be TRUE.
*/
value = zval_get_nonref(value);
if (Z_TYPE_P(value) != IS_TRUE) {
continue;
}
name = ZSTR_VAL(key);
if (vips_object_get_argument(VIPS_OBJECT(call->operation), name,
&pspec, &argument_class, &argument_instance)) {
return -1;
}
if (!(argument_class->flags & VIPS_ARGUMENT_REQUIRED) &&
(argument_class->flags & VIPS_ARGUMENT_OUTPUT) &&
!(argument_class->flags & VIPS_ARGUMENT_DEPRECATED)) {
zval zvalue;
if (vips_php_get_value(call, pspec, &zvalue)) {
return -1;
}
add_assoc_zval(return_value, name, &zvalue);
}
} ZEND_HASH_FOREACH_END();
return 0;
}
/* Call any vips operation, with the arguments coming from an array of zval.
* argv can have an extra final arg, which is an associative array of
* optional arguments.
*/
static int
vips_php_call_array(const char *operation_name, zval *instance,
const char *option_string, int argc, zval *argv, zval *return_value)
{
VipsPhpCall *call;
int i;
VIPS_DEBUG_MSG("vips_php_call_array:\n");
if (!(call = vips_php_call_new(operation_name, instance, option_string,
argc, argv))) {
return -1;
}
/* Some initial analysis of our args. Loop over them all, including the
* special 'instance' arg.
*/
VIPS_DEBUG_MSG("vips_php_call_array: analyzing input args ...\n");
if (call->instance) {
vips_php_analyze_arg(call, call->instance);
}
for (i = 0; i < argc; i++) {
vips_php_analyze_arg(call, &call->argv[i]);
}
/* Set str options before vargs options, so the user can't
* override things we set deliberately.
*/
VIPS_DEBUG_MSG("vips_php_call_array: setting args from option_string ...\n");
if (option_string &&
vips_object_set_from_string(VIPS_OBJECT(call->operation),
option_string)) {
vips_object_unref_outputs(VIPS_OBJECT(call->operation));