Skip to content

Commit daee894

Browse files
committed
Add read_ptr method to Vips::Image
1 parent 6c1a9dd commit daee894

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

lib/vips/image.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -1235,10 +1235,20 @@ def to_a
12351235
to_enum.to_a
12361236
end
12371237

1238+
# Return a read-only pointer to the pixel data, if possible.
1239+
#
1240+
# @return [FFI::Pointer] pointer to the pixel data
1241+
def read_ptr
1242+
copy = copy_memory
1243+
ptr = copy.unsafe_data_ptr
1244+
ptr.instance_variable_set(:@_vips_reference, copy)
1245+
ptr
1246+
end
1247+
12381248
# Return a pointer to the pixel data, if possible.
12391249
#
12401250
# @return [FFI::Pointer] pointer to the pixel data
1241-
def data_ptr
1251+
def unsafe_data_ptr
12421252
ptr = Vips.vips_image_get_data self
12431253
raise Vips::Error if ptr.null?
12441254

spec/image_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,9 @@
127127
expect { Vips::Image.new_from_memory_copy(data, 16, 16, 1, :uchar) }.to raise_error(Vips::Error)
128128
end
129129

130-
it "can load an image from a data pointer" do
130+
it "can load an image from a read pointer" do
131131
image = Vips::Image.black(16, 16) + 128
132-
data = image.data_ptr
132+
data = image.read_ptr
133133
expect(data).to be_a(FFI::Pointer)
134134
expect(data.size).to eq(1024)
135135

0 commit comments

Comments
 (0)