Skip to content

Commit bb4b4c9

Browse files
committed
start adding write to array
First quick hack, untested. See libvips/php-vips#90
1 parent 4dde2af commit bb4b4c9

File tree

4 files changed

+108
-5
lines changed

4 files changed

+108
-5
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
vips extension changelog
22

3+
Version 1.0.11 (2019-9-18)
4+
--------------------------
5+
* Add vips_image_write_to_array() [jcupitt]
6+
https://github.com/libvips/php-vips/issues/90
7+
38
Version 1.0.10 (2018-12-xx)
49
--------------------------
510
* Fix win32 build [TBK]
File renamed without changes.

package.xml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ http://pear.php.net/dtd/package-2.0.xsd">
1515
<email>jcupitt@php.net</email>
1616
<active>yes</active>
1717
</lead>
18-
<date>2018-12-12</date>
18+
<date>2019-9-19</date>
1919
<version>
20-
<release>1.0.10</release>
20+
<release>1.0.11</release>
2121
<api>1.0.0</api>
2222
</version>
2323
<stability>
@@ -26,16 +26,15 @@ http://pear.php.net/dtd/package-2.0.xsd">
2626
</stability>
2727
<license filesource="LICENSE.txt">MIT</license>
2828
<notes>
29-
* Update links for new home [jcupitt]
30-
* Fix win32 build [TBK]
29+
* Add vips_image_write_to_array() [jcupitt]
3130
</notes>
3231
<contents>
3332
<dir name="/">
3433
<file role='doc' name='API-1.0.0'/>
3534
<file role='doc' name='CREDITS'/>
3635
<file role='doc' name='LICENSE.txt'/>
3736
<file role='doc' name='README.md'/>
38-
<file role='doc' name='RELEASE-1.0.10'/>
37+
<file role='doc' name='RELEASE-1.0.11'/>
3938
<file role='doc' name='ChangeLog'/>
4039

4140
<file role='src' name='config.m4'/>
@@ -99,6 +98,15 @@ http://pear.php.net/dtd/package-2.0.xsd">
9998
</extsrcrelease>
10099
<changelog>
101100

101+
<release>
102+
<stability><release>stable</release><api>stable</api></stability>
103+
<version><release>1.0.11</release><api>1.0.0</api></version>
104+
<date>2019-9-18</date>
105+
<notes>
106+
* Add vips_image_write_to_array() [jcupitt]
107+
</notes>
108+
</release>
109+
102110
<release>
103111
<stability><release>stable</release><api>stable</api></stability>
104112
<version><release>1.0.10</release><api>1.0.0</api></version>

vips.c

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,91 @@ PHP_FUNCTION(vips_image_write_to_memory)
14611461
}
14621462
/* }}} */
14631463

1464+
#define ADD_ELEMENTS(TYPE, APPEND, N) { \
1465+
TYPE *p = arr; \
1466+
size_t i; \
1467+
\
1468+
for (i = 0; i < (N); i++) \
1469+
APPEND(return_value, p[i]); \
1470+
}
1471+
1472+
/* {{{ proto array vips_image_write_to_array(resource image)
1473+
Write an image to a PHP array. */
1474+
PHP_FUNCTION(vips_image_write_to_array)
1475+
{
1476+
zval *IM;
1477+
VipsImage *image;
1478+
size_t arr_len;
1479+
uint8_t *arr;
1480+
size_t n;
1481+
1482+
VIPS_DEBUG_MSG("vips_image_write_to_array:\n");
1483+
1484+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &IM) == FAILURE) {
1485+
RETURN_LONG(-1);
1486+
}
1487+
1488+
if ((image = (VipsImage *)zend_fetch_resource(Z_RES_P(IM),
1489+
"GObject", le_gobject)) == NULL) {
1490+
RETURN_LONG(-1);
1491+
}
1492+
1493+
if (!(arr = vips_image_write_to_memory(image, &arr_len))) {
1494+
RETURN_LONG(-1);
1495+
}
1496+
1497+
array_init(return_value);
1498+
n = arr_len / vips_format_sizeof(image->BandFmt);
1499+
g_assert(arr_len % vips_format_sizeof(image->BandFmt) == 0);
1500+
switch (image->BandFmt) {
1501+
case VIPS_FORMAT_UCHAR:
1502+
ADD_ELEMENTS (unsigned char, add_next_index_long, n);
1503+
break;
1504+
1505+
case VIPS_FORMAT_CHAR:
1506+
ADD_ELEMENTS (signed char, add_next_index_long, n);
1507+
break;
1508+
1509+
case VIPS_FORMAT_USHORT:
1510+
ADD_ELEMENTS (unsigned short, add_next_index_long, n);
1511+
break;
1512+
1513+
case VIPS_FORMAT_SHORT:
1514+
ADD_ELEMENTS (signed short, add_next_index_long, n);
1515+
break;
1516+
1517+
case VIPS_FORMAT_UINT:
1518+
ADD_ELEMENTS (unsigned int, add_next_index_long, n);
1519+
break;
1520+
1521+
case VIPS_FORMAT_INT:
1522+
ADD_ELEMENTS (signed int, add_next_index_long, n);
1523+
break;
1524+
1525+
case VIPS_FORMAT_FLOAT:
1526+
ADD_ELEMENTS (float, add_next_index_double, n);
1527+
break;
1528+
1529+
case VIPS_FORMAT_DOUBLE:
1530+
ADD_ELEMENTS (double, add_next_index_double, n);
1531+
break;
1532+
1533+
case VIPS_FORMAT_COMPLEX:
1534+
ADD_ELEMENTS (float, add_next_index_double, n * 2);
1535+
break;
1536+
1537+
case VIPS_FORMAT_DPCOMPLEX:
1538+
ADD_ELEMENTS (double, add_next_index_double, n * 2);
1539+
break;
1540+
1541+
default:
1542+
break;
1543+
}
1544+
1545+
g_free(arr);
1546+
}
1547+
/* }}} */
1548+
14641549
/* {{{ proto string|long vips_foreign_find_load(string filename)
14651550
Find a loader for a file */
14661551
PHP_FUNCTION(vips_foreign_find_load)
@@ -2075,6 +2160,10 @@ ZEND_BEGIN_ARG_INFO(arginfo_vips_image_write_to_memory, 0)
20752160
ZEND_ARG_INFO(0, image)
20762161
ZEND_END_ARG_INFO()
20772162

2163+
ZEND_BEGIN_ARG_INFO(arginfo_vips_image_write_to_array, 0)
2164+
ZEND_ARG_INFO(0, image)
2165+
ZEND_END_ARG_INFO()
2166+
20782167
ZEND_BEGIN_ARG_INFO(arginfo_vips_foreign_find_load, 0)
20792168
ZEND_ARG_INFO(0, filename)
20802169
ZEND_END_ARG_INFO()
@@ -2162,6 +2251,7 @@ const zend_function_entry vips_functions[] = {
21622251
PHP_FE(vips_image_copy_memory, arginfo_vips_image_copy_memory)
21632252
PHP_FE(vips_image_new_from_memory, arginfo_vips_image_new_from_memory)
21642253
PHP_FE(vips_image_write_to_memory, arginfo_vips_image_write_to_memory)
2254+
PHP_FE(vips_image_write_to_array, arginfo_vips_image_write_to_array)
21652255
PHP_FE(vips_foreign_find_load, arginfo_vips_foreign_find_load)
21662256
PHP_FE(vips_foreign_find_load_buffer, arginfo_vips_foreign_find_load_buffer)
21672257
PHP_FE(vips_interpolate_new, arginfo_vips_interpolate_new)

0 commit comments

Comments
 (0)