10000 improve docs · libvips/ruby-vips@9cf2ea6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9cf2ea6

Browse files
committed
improve docs
1 parent 9e8e51d commit 9cf2ea6

File tree

2 files changed

+33
-9
lines changed

2 files changed

+33
-9
lines changed

lib/vips/image.rb

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,8 @@ def self.new_from_file name, **opts
278278
Operation.call loader, [filename], opts, option_string
279279
end
280280

281-
# Create a new {Image} for an image encoded, in a format such as
282-
# JPEG, in a binary string. Load options may be passed as
281+
# Create a new {Image} for an image encoded in a format such as
282+
# JPEG in a binary string. Load options may be passed as
283283
# strings or appended as a hash. For example:
284284
#
285285
# ```
@@ -316,7 +316,22 @@ def self.new_from_buffer data, option_string, **opts
316316
Vips::Operation.call loader, [data], opts, option_string
317317
end
318318

319-
# Create a new {Image} from memory
319+
# Create a new {Image} from a C-style array held in memory. For example:
320+
#
321+
# ```
322+
# image = Vips::Image.black(16, 16) + 128
323+
# data = image.write_to_memory
324+
#
325+
# x = Vips::Image.new_from_memory data,
326+
# image.width, image.height, image.bands, image.format
327+
# ```
328+
#
329+
# {new_from_memory} keeps a reference to the array of pixels you pass in
330+
# to try to prevent that memory from being freed by the Ruby GC while it
331+
# is being used.
332+
#
333+
# See {new_from_memory_copy} for a version of this method which does not
334+
# keep a reference.
320335
#
321336
# @param data [String, FFI::Pointer] the data to load from
322337
# @param width [Integer] width in pixels
@@ -333,7 +348,8 @@ def self.new_from_memory data, width, height, bands, format
333348
end
334349

335350
format_number = GObject::GValue.from_nick BAND_FORMAT_TYPE, format
336-
vi = Vips::vips_image_new_from_memory data, size, width, height, bands, format_number
351+
vi = Vips::vips_image_new_from_memory data, size,
352+
width, height, bands, format_number
337353
raise Vips::Error if vi.null?
338354
image = new(vi)
339355

@@ -345,7 +361,9 @@ def self.new_from_memory data, width, height, bands, format
345361
image
346362
end
347363

348-
# Create a new {Image} from memory and copies the memory area
364+
# Create a new {Image} from memory and copies the memory area. See
365+
# {new_from_memory} for a version of this method which does not copy the
366+
# memory area.
349367
#
350368
# @param data [String, FFI::Pointer] the data to load from
351369
# @param width [Integer] width in pixels
@@ -355,7 +373,8 @@ def self.new_from_memory data, width, height, bands, format
355373
# @return [Image] the loaded image
356374
def self.new_from_memory_copy data, width, height, bands, format
357375
format_number = GObject::GValue.from_nick BAND_FORMAT_TYPE, format
358-
vi = Vips::vips_image_new_from_memory_copy data, data.bytesize, width, height, bands, format_number
376+
vi = Vips::vips_image_new_from_memory_copy data, data.bytesize,
377+
width, height, bands, format_number
359378
raise Vips::Error if vi.null?
360379
new(vi)
361380
end

spec/image_spec.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ def has_jpeg?
3131
image = Vips::Image.black(16, 16) + 128
3232
data = image.write_to_memory
3333

34-
x = Vips::Image.new_from_memory data, image.width, image.height, image.bands, image.format
34+
x = Vips::Image.new_from_memory data,
35+
image.width, image.height, image.bands, image.format
3536

36-
GC.start # make sure the memory isn't freed
37+
# GC to try to trigger a segv if data hasn't been reffed by
38+
# new_from_memory
39+
data = nil
40+
GC.start
3741

3842
expect(x.width).to eq(16)
3943
expect(x.height).to eq(16)
@@ -45,7 +49,8 @@ def has_jpeg?
4549
image = Vips::Image.black(16, 16) + 128
4650
data = image.write_to_memory
4751

48-
x = Vips::Image.new_from_memory_copy data, image.width, image.height, image.bands, image.format
52+
x = Vips::Image.new_from_memory_copy data,
53+
image.width, image.height, image.bands, image.format
4954

5055
expect(x.width).to eq(16)
5156
expect(x.height).to eq(16)

0 commit comments

Comments
 (0)
0