Skip to content

Commit 9f881ef

Browse files
committed
Merge branch 'revise-buffer-save4'
2 parents 86144f8 + ac24957 commit 9f881ef

File tree

3 files changed

+54
-34
lines changed

3 files changed

+54
-34
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
* add pagesplit(), pagejoin(), get_page_height(), get_n_pages() [jcupitt]
99
* add atan2() [jcupitt]
1010
* don't generate docs for deprecated arguments [jcupitt]
11+
* buffer save tries with the target API first [jcupitt]
1112

1213
## Version 2.1.15 (27 Dec 2020)
1314

pyvips/vdecls.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ def cdefs(features):
8181
8282
const char* vips_error_buffer (void);
8383
void vips_error_clear (void);
84+
void vips_error_freeze (void);
85+
void vips_error_thaw (void);
8486
8587
typedef struct _GValue {
8688
GType g_type;

pyvips/vimage.py

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -583,45 +583,33 @@ def write_to_buffer(self, format_string, **kwargs):
583583
"""
584584
format_string = _to_bytes(format_string)
585585

586+
filename = vips_lib.vips_filename_get_filename(format_string)
587+
586588
pointer = vips_lib.vips_filename_get_options(format_string)
587589
options = _to_string_copy(pointer)
588590

589-
pointer = vips_lib.vips_foreign_find_save_buffer(format_string)
590-
if pointer == ffi.NULL:
591-
raise Error('unable to write to buffer')
592-
name = _to_string(pointer)
593-
594-
return pyvips.Operation.call(name, self,
595-
string_options=options, **kwargs)
596-
597-
def write_to_memory(self):
598-
"""Write the image to a large memory array.
599-
600-
A large area of memory is allocated, the image is rendered to that
601-
memory array, and the array is returned as a buffer.
602-
603-
For example, if you have a 2x2 uchar image containing the bytes 1, 2,
604-
3, 4, read left-to-right, top-to-bottom, then::
605-
606-
buf = image.write_to_memory()
607-
608-
will return a four byte buffer containing the values 1, 2, 3, 4.
609-
610-
Returns:
611-
buffer
612-
613-
Raises:
614-
:class:`.Error`
615-
616-
"""
591+
pointer = ffi.NULL
592+
if at_least_libvips(8, 9):
593+
vips_lib.vips_error_freeze()
594+
pointer = vips_lib.vips_foreign_find_save_target(filename)
595+
vips_lib.vips_error_thaw()
596+
597+
if pointer != ffi.NULL:
598+
name = _to_string(pointer)
599+
target = pyvips.Target.new_to_memory()
600+
pyvips.Operation.call(name, self, target,
601+
string_options=options, **kwargs)
602+
buffer = target.get("blob")
603+
else:
604+
pointer = vips_lib.vips_foreign_find_save_buffer(filename)
605+
if pointer == ffi.NULL:
606+
raise Error('unable to write to buffer')
617607

618-
psize = ffi.new('size_t *')
619-
pointer = vips_lib.vips_image_write_to_memory(self.pointer, psize)
620-
if pointer == ffi.NULL:
621-
raise Error('unable to write to memory')
622-
pointer = ffi.gc(pointer, glib_lib.g_free)
608+
name = _to_string(pointer)
609+
buffer = pyvips.Operation.call(name, self,
610+
string_options=options, **kwargs)
623611

624-
return ffi.buffer(pointer, psize[0])
612+
return buffer
625613

626614
def write_to_target(self, target, format_string, **kwargs):
627615
"""Write an image to a target.
@@ -672,6 +660,35 @@ def write_to_target(self, target, format_string, **kwargs):
672660
return pyvips.Operation.call(name, self, target,
673661
string_options=options, **kwargs)
674662

663+
def write_to_memory(self):
664+
"""Write the image to a large memory array.
665+
666+
A large area of memory is allocated, the image is rendered to that
667+
memory array, and the array is returned as a buffer.
668+
669+
For example, if you have a 2x2 uchar image containing the bytes 1, 2,
670+
3, 4, read left-to-right, top-to-bottom, then::
671+
672+
buf = image.write_to_memory()
673+
674+
will return a four byte buffer containing the values 1, 2, 3, 4.
675+
676+
Returns:
677+
buffer
678+
679+
Raises:
680+
:class:`.Error`
681+
682+
"""
683+
684+
psize = ffi.new('size_t *')
685+
pointer = vips_lib.vips_image_write_to_memory(self.pointer, psize)
686+
if pointer == ffi.NULL:
687+
raise Error('unable to write to memory')
688+
pointer = ffi.gc(pointer, glib_lib.g_free)
689+
690+
return ffi.buffer(pointer, psize[0])
691+
675692
def write(self, other):
676693
"""Write an image to another image.
677694

0 commit comments

Comments
 (0)