-
Notifications
You must be signed in to change notification settings - Fork 11
Add new_from_memory / write_to_memory #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
4f5f49f
0b42b22
8c7ecb7
1eeadec
2032b9f
c347899
2880405
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
--TEST-- | ||
can make an image from memory | ||
--SKIPIF-- | ||
<?php if (!extension_loaded("vips")) print "skip"; ?> | ||
--FILE-- | ||
<?php | ||
$binary_str = pack("c*", ...array_fill(0, 200, 0)); | ||
$image = vips_image_new_from_memory($binary_str, 20, 10, 1, "uchar")["out"]; | ||
$width = vips_image_get($image, "width")["out"]; | ||
$height = vips_image_get($image, "height")["out"]; | ||
$format = vips_image_get($image, "format")["out"]; | ||
$bands = vips_image_get($image, "bands")["out"]; | ||
$avg = vips_call("avg", $image)["out"]; | ||
|
||
if ($width == 20 && | ||
$height == 10 && | ||
$format == 'uchar' && | ||
$bands == 1 && | ||
$avg == 0) { | ||
echo "pass"; | ||
} | ||
?> | ||
--EXPECT-- | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--TEST-- | ||
can write to memory | ||
--SKIPIF-- | ||
<?php if (!extension_loaded("vips")) print "skip"; ?> | ||
--FILE-- | ||
<?php | ||
$binary_str = pack("c*", ...array_fill(0, 200, 0)); | ||
$image = vips_image_new_from_memory($binary_str, 20, 10, 1, "uchar")["out"]; | ||
$mem_str = vips_image_write_to_memory($image); | ||
|
||
if ($binary_str === $mem_str) { | ||
echo "pass"; | ||
} | ||
?> | ||
--EXPECT-- | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1375,8 +1375,7 @@ PHP_FUNCTION(vips_image_copy_memory) | |
RETURN_LONG(-1); | ||
} | ||
|
||
new_image = vips_image_copy_memory(image); | ||
if (!new_image) { | ||
if (!(new_image = vips_image_copy_memory(image))) { | ||
RETURN_LONG(-1); | ||
} | ||
|
||
|
@@ -1389,6 +1388,77 @@ PHP_FUNCTION(vips_image_copy_memory) | |
} | ||
/* }}} */ | ||
|
||
/* {{{ proto resource vips_image_new_from_memory(string data, integer width, integer height, integer bands, string format) | ||
Wrap an image around a memory array. */ | ||
PHP_FUNCTION(vips_image_new_from_memory) | ||
{ | ||
char *bstr; | ||
size_t bstr_len; | ||
long width; | ||
long height; | ||
long bands; | ||
char *format; | ||
size_t format_len; | ||
int format_value; | ||
VipsBandFormat band_format; | ||
VipsImage *image; | ||
zend_resource *resource; | ||
zval zvalue; | ||
|
||
VIPS_DEBUG_MSG("vips_image_new_from_memory:\n"); | ||
|
||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "slllp", | ||
&bstr, &bstr_len, &width, &height, &bands, &format, &format_len) == FAILURE) { | ||
RETURN_LONG(-1); | ||
} | ||
|
||
if ((format_value = vips_enum_from_nick("php-vips", VIPS_TYPE_BAND_FORMAT, format)) < 0) { | ||
RETURN_LONG(-1); | ||
} | ||
band_format = format_value; | ||
|
||
if (!(image = vips_image_new_from_memory_copy(bstr, bstr_len, width, height, bands, | ||
band_format))) { | ||
RETURN_LONG(-1); | ||
} | ||
|
||
/* Return as an array for all OK, -1 for error. | ||
*/ | ||
array_init(return_value); | ||
resource = zend_register_resource(image, le_gobject); | ||
ZVAL_RES(&zvalue, resource); | ||
add_assoc_zval(return_value, "out", &zvalue); | ||
} | ||
/* }}} */ | ||
|
||
/* {{{ proto string vips_image_write_to_memory(resource image) | ||
Write an image to a memory array. */ | ||
PHP_FUNCTION(vips_image_write_to_memory) | ||
{ | ||
zval *IM; | ||
VipsImage *image; | ||
size_t arr_len; | ||
uint8_t *arr; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we expect larger values than There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's always bytes there, so it's fine. |
||
|
||
VIPS_DEBUG_MSG("vips_image_write_to_memory:\n"); | ||
|
||
if (zend_parse_parameters(ZEND_NUM_ARGS(), "r", &IM) == FAILURE) { | ||
RETURN_LONG(-1); | ||
} | ||
|
||
if ((image = (VipsImage *)zend_fetch_resource(Z_RES_P(IM), | ||
"GObject", le_gobject)) == NULL) { | ||
RETURN_LONG(-1); | ||
} | ||
|
||
if (!(arr = vips_image_write_to_memory(image, &arr_len))) { | ||
RETURN_LONG(-1); | ||
} | ||
|
||
RETVAL_STRINGL((char *)arr, arr_len); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. RETVAL_STRINGL will take a copy of the string, so we need a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed in 2880405. |
||
} | ||
/* }}} */ | ||
|
||
/* {{{ proto string|long vips_foreign_find_load(string filename) | ||
Find a loader for a file */ | ||
PHP_FUNCTION(vips_foreign_find_load) | ||
|
@@ -1922,6 +1992,18 @@ ZEND_BEGIN_ARG_INFO(arginfo_vips_image_copy_memory, 0) | |
ZEND_ARG_INFO(0, image) | ||
ZEND_END_ARG_INFO() | ||
|
||
ZEND_BEGIN_ARG_INFO(arginfo_vips_image_new_from_memory, 0) | ||
ZEND_ARG_INFO(0, array) | ||
ZEND_ARG_INFO(0, width) | ||
ZEND_ARG_INFO(0, height) | ||
ZEND_ARG_INFO(0, bands) | ||
ZEND_ARG_INFO(0, format) | ||
ZEND_END_ARG_INFO() | ||
|
||
ZEND_BEGIN_ARG_INFO(arginfo_vips_image_write_to_memory, 0) | ||
ZEND_ARG_INFO(0, image) | ||
ZEND_END_ARG_INFO() | ||
|
||
ZEND_BEGIN_ARG_INFO(arginfo_vips_foreign_find_load, 0) | ||
ZEND_ARG_INFO(0, filename) | ||
ZEND_END_ARG_INFO() | ||
|
@@ -1990,6 +2072,8 @@ const zend_function_entry vips_functions[] = { | |
PHP_FE(vips_image_write_to_file, arginfo_vips_image_write_to_file) | ||
PHP_FE(vips_image_write_to_buffer, arginfo_vips_image_write_to_buffer) | ||
PHP_FE(vips_image_copy_memory, arginfo_vips_image_copy_memory) | ||
PHP_FE(vips_image_new_from_memory, arginfo_vips_image_new_from_memory) | ||
PHP_FE(vips_image_write_to_memory, arginfo_vips_image_write_to_memory) | ||
PHP_FE(vips_foreign_find_load, arginfo_vips_foreign_find_load) | ||
PHP_FE(vips_foreign_find_load_buffer, arginfo_vips_foreign_find_load_buffer) | ||
PHP_FE(vips_interpolate_new, arginfo_vips_interpolate_new) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I use
"C*"
(unsigned char) here instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure. Probably, yes.