Skip to content

Commit 333b22d

Browse files
committed
return built-in enums as strings
we now return strings for things like `vips_image_get($image, "interpretation")` previously these came back as ints due to the way libvips handles the getter for built-in properties
1 parent 303fec6 commit 333b22d

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

ChangeLog

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ Version 0.1.1 (2016-10-03)
55
* Support draw operations
66
* Add vips_error_buffer(), remove docref messages
77
* Return 0/-1 everywhere for error
8-
* Vips_image_get() returns ["out" => value] | -1
8+
* vips_image_get() returns ["out" => value] | -1
9+
* vips_image_get() returns built-in enums as strings
910
* Fix type conversions
1011
* Add vips_image_remove()
1112
* Add vips_cache_set_*()

tests/004.phpt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,3 @@ vips can get image header fields
1313
?>
1414
--EXPECT--
1515
pass
16-
--CLEAN--
17-
<?php
18-
$output_filename = dirname(__FILE__) . "/x.tif";
19-
unlink($output_filename);
20-
?>
21-

tests/030.phpt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
enum fields are returned as strings
3+
--SKIPIF--
4+
<?php if (!extension_loaded("vips")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$filename = dirname(__FILE__) . "/images/img_0076.jpg";
8+
$image = vips_image_new_from_file($filename)["out"];
9+
$interpretation = vips_image_get($image, "interpretation")["out"];
10+
if ($interpretation == "srgb") {
11+
echo("pass\n");
12+
}
13+
?>
14+
--EXPECT--
15+
pass

vips.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,7 @@ PHP_FUNCTION(vips_image_get)
12541254
VipsImage *image;
12551255
GValue gvalue = { 0 };
12561256
zval zvalue;
1257+
GParamSpec *pspec;
12571258

12581259
if (zend_parse_parameters(ZEND_NUM_ARGS(), "rs",
12591260
&im, &field_name, &field_name_len) == FAILURE) {
@@ -1265,7 +1266,20 @@ PHP_FUNCTION(vips_image_get)
12651266
RETURN_LONG(-1);
12661267
}
12671268

1268-
if (vips_image_get(image, field_name, &gvalue)) {
1269+
/* Ugly: older libvipses would return enums from the true header fields
1270+
* (eg. ->interpretation) as ints, but we want to send a string back
1271+
* for things like this.
1272+
*
1273+
* Test if field_name exists as a regular glib property and if it does, use
1274+
* g_object_get(). Otherwise use vips_image_get(), since it can read extra
1275+
* image metadata.
1276+
*/
1277+
if ((pspec = g_object_class_find_property(G_OBJECT_GET_CLASS(image),
1278+
field_name))) {
1279+
g_value_init(&gvalue, G_PARAM_SPEC_VALUE_TYPE(pspec));
1280+
g_object_get_property(G_OBJECT(image), field_name, &gvalue);
1281+
}
1282+
else if (vips_image_get(image, field_name, &gvalue)) {
12691283
RETURN_LONG(-1);
12701284
}
12711285

0 commit comments

Comments
 (0)