Skip to content

Commit 198b510

Browse files
committed
rename stream -> source/target
1 parent bbf86b0 commit 198b510

12 files changed

+419
-421
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* fix `GValue#set` for stricter metadata rules in 8.9 [jcupitt]
1010
* fix a ref leak on operation build error [jcupitt]
1111
* faster operation call [jcupitt]
12-
* add support for VipsStream [jcupitt]
12+
* add support for VipsConnection [jcupitt]
1313
* add `signal_connect` [jcupitt]
1414
* add `Image#set_kill` for progress termination [jcupitt]
1515

lib/vips.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ module GObject
212212
# memory buffers, create images that wrap C-style memory arrays, or make images
213213
# from constants.
214214
#
215-
# Use {Streamiu} and {Image.new_from_stream} to load images from any data
215+
# Use {Source} and {Image.new_from_source} to load images from any data
216216
# source, for example URIs.
217217
#
218218
# The next line:
@@ -256,7 +256,7 @@ module GObject
256256
# suffix. You can also write formatted images to memory buffers, or dump
257257
# image data to a raw memory array.
258258
#
259-
# Use {Streamou} and {Image#write_to_stream} to write formatted images to
259+
# Use {Target} and {Image#write_to_target} to write formatted images to
260260
# any data sink, for example URIs.
261261
#
262262
# # How it works
@@ -479,14 +479,14 @@ module GObject
479479
#
480480
# User streams
481481
#
482-
# You can make your own input and output stream objects with {Streamiu} and
483-
# {Streamou}. For example:
482+
# You can make your own input and output stream objects with {SourceCustom} and
483+
# {TargetCustom}. For example:
484484
#
485485
# ```ruby
486-
# source = File.open "some/file", "rb"
487-
# input_stream = Vips::Streamiu.new
488-
# input_stream.on_read { |length| source.read length }
489-
# image = Vips::Image.new_from_stream input_stream, "", access: "sequential"
486+
# file = File.open "some/file", "rb"
487+
# source = Vips::SourceCustom.new
488+
# source.on_read { |length| file.read length }
489+
# image = Vips::Image.new_from_source source, "", access: "sequential"
490490
# ```
491491
#
492492
# # Overloads
@@ -679,8 +679,8 @@ def self.get_suffixes
679679
require 'vips/interpolate'
680680
require 'vips/region'
681681
require 'vips/version'
682-
require 'vips/stream'
683-
require 'vips/streami'
684-
require 'vips/streamiu'
685-
require 'vips/streamo'
686-
require 'vips/streamou'
682+
require 'vips/connection'
683+
require 'vips/source'
684+
require 'vips/sourcecustom'
685+
require 'vips/target'
686+
require 'vips/targetcustom'

lib/vips/stream.rb renamed to lib/vips/connection.rb

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88

99
module Vips
1010
if Vips::at_least_libvips?(8, 9)
11-
attach_function :vips_stream_filename, [:pointer], :string
12-
attach_function :vips_stream_nick, [:pointer], :string
11+
attach_function :vips_connection_filename, [:pointer], :string
12+
attach_function :vips_connection_nick, [:pointer], :string
1313
end
1414

15-
# Abstract base class for streams.
16-
class Stream < Vips::Object
15+
# Abstract base class for connections.
16+
class Connection < Vips::Object
1717
# The layout of the VipsRegion struct.
18-
module StreamLayout
18+
module ConnectionLayout
1919
def self.included(base)
2020
base.class_eval do
2121
layout :parent, Vips::Object::Struct
@@ -25,19 +25,19 @@ def self.included(base)
2525
end
2626

2727
class Struct < Vips::Object::Struct
28-
include StreamLayout
28+
include ConnectionLayout
2929
end
3030

3131
class ManagedStruct < Vips::Object::ManagedStruct
32-
include StreamLayout
32+
include ConnectionLayout
3333
end
3434

3535
def filename
36-
Vips::vips_stream_filename self
36+
Vips::vips_connection_filename self
3737
end
3838

3939
def nick
40-
Vips::vips_stream_nick self
40+
Vips::vips_connection_nick self
4141
end
4242
end
4343
end

lib/vips/image.rb

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ module Vips
2626
attach_function :vips_foreign_find_save_buffer, [:string], :string
2727

2828
if Vips::at_least_libvips?(8, 9)
29-
attach_function :vips_foreign_find_load_stream, [:pointer], :string
30-
attach_function :vips_foreign_find_save_stream, [:string], :string
29+
attach_function :vips_foreign_find_load_source, [:pointer], :string
30+
attach_function :vips_foreign_find_save_target, [:string], :string
3131
end
3232

3333
attach_function :vips_image_write_to_memory,
@@ -303,43 +303,43 @@ def self.new_from_buffer data, option_string, **opts
303303
Vips::Operation.call loader, [data], opts, option_string
304304
end
305305

306-
# Create a new {Image} from an input stream. Load options may be passed as
306+
# Create a new {Image} from a source. Load options may be passed as
307307
# strings or appended as a hash. For example:
308308
#
309309
# ```
310-
# stream = Vips::Streami.new_from_file("k2.jpg")
311-
# image = Vips::Image.new_from_stream stream, "shrink=2"
310+
# source = Vips::Source.new_from_file("k2.jpg")
311+
# image = Vips::Image.new_from_source source, "shrink=2"
312312
# ```
313313
#
314314
# or alternatively:
315315
#
316316
# ```
317-
# image = Vips::Image.new_from_stream stream, "", shrink: 2
317+
# image = Vips::Image.new_from_source source, "", shrink: 2
318318
# ```
319319
#
320320
# The options available depend on the file format. Try something like:
321321
#
322322
# ```
323-
# $ vips jpegload_stream
323+
# $ vips jpegload_source
324324
# ```
325325
#
326326
# at the command-line to see the available options. Not all loaders
327-
# support load from stream, but at least JPEG, PNG and
327+
# support load from source, but at least JPEG, PNG and
328328
# TIFF images will work.
329329
#
330330
# Loading is fast: only enough data is read to be able to fill
331331
# out the header. Pixels will only be read and decompressed when they are
332332
# needed.
333333
#
334-
# @param stream [Vips::Streami] the stream to load from
334+
# @param source [Vips::Source] the source to load from
335335
# @param option_string [String] load options as a string
336336
# @macro vips.loadopts
337337
# @return [Image] the loaded image
338-
def self.new_from_stream stream, option_string, **opts
339-
loader = Vips::vips_foreign_find_load_stream stream
338+
def self.new_from_source source, option_string, **opts
339+
loader = Vips::vips_foreign_find_load_source source
340340
raise Vips::Error if loader.nil?
341341

342-
Vips::Operation.call loader, [stream], opts, option_string
342+
Vips::Operation.call loader, [source], opts, option_string
343343
end
344344

345345
def self.matrix_from_array width, height, array
@@ -512,41 +512,41 @@ def write_to_buffer format_string, **opts
512512
return buffer
513513
end
514514

515-
# Write this image to a stream. Save options may be encoded in
515+
# Write this image to a target. Save options may be encoded in
516516
# the format_string or given as a hash. For example:
517517
#
518518
# ```ruby
519-
# stream = Vips::Streamo.new_to_file "k2.jpg"
520-
# image.write_to_stream stream, ".jpg[Q=90]"
519+
# target = Vips::Target.new_to_file "k2.jpg"
520+
# image.write_to_target target, ".jpg[Q=90]"
521521
# ```
522522
#
523523
# or equivalently:
524524
#
525525
# ```ruby
526-
# image.write_to_stream stream, ".jpg", Q: 90
526+
# image.write_to_target target, ".jpg", Q: 90
527527
# ```
528528
#
529529
# The full set of save options depend on the selected saver. Try
530530
# something like:
531531
#
532532
# ```
533-
# $ vips jpegsave_stream
533+
# $ vips jpegsave_target
534534
# ```
535535
#
536536
# to see all the available options for JPEG save.
537537
#
538-
# @param stream [Vips::Streamo] the stream to write to
538+
# @param target [Vips::Target] the target to write to
539539
# @param format_string [String] save format plus string options
540540
# @macro vips.saveopts
541-
def write_to_stream stream, format_string, **opts
541+
def write_to_target target, format_string, **opts
542542
filename = Vips::p2str(Vips::vips_filename_get_filename format_string)
543543
option_string = Vips::p2str(Vips::vips_filename_get_options format_string)
544-
saver = Vips::vips_foreign_find_save_stream filename
544+
saver = Vips::vips_foreign_find_save_target filename
545545
if saver == nil
546-
raise Vips::Error, "No known stream saver for '#{filename}'."
546+
raise Vips::Error, "No known target saver for '#{filename}'."
547547
end
548548

549-
Vips::Operation.call saver, [self, stream], opts, option_string
549+
Vips::Operation.call saver, [self, target], opts, option_string
550550
write_gc
551551
end
552552

lib/vips/source.rb

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
# This module provides an interface to the top level bits of libvips
2+
# via ruby-ffi.
3+
#
4+
# Author:: John Cupitt (mailto:jcupitt@gmail.com)
5+
# License:: MIT
6+
7+
require 'ffi'
8+
9+
module Vips
10+
if Vips::at_least_libvips?(8, 9)
11+
attach_function :vips_source_new_from_descriptor, [:int], :pointer
12+
attach_function :vips_source_new_from_file, [:pointer], :pointer
13+
attach_function :vips_source_new_from_memory, [:pointer, :size_t], :pointer
14+
end
15+
16+
# A source. For example:
17+
#
18+
# ```ruby
19+
# source = Vips::Source.new_from_file("k2.jpg")
20+
# image = Vips::Image.new_from_source(source)
21+
# ```
22+
class Source < Vips::Connection
23+
module SourceLayout
24+
def self.included(base)
25+
base.class_eval do
26+
layout :parent, Vips::Connection::Struct
27+
# rest opaque
28+
end
29+
end
30+
end
31+
32+
class Struct < Vips::Connection::Struct
33+
include SourceLayout
34+
end
35+
36+
class ManagedStruct < Vips::Connection::ManagedStruct
37+
include SourceLayout
38+
end
39+
40+
# Create a new source from a file descriptor. File descriptors are
41+
# small integers, for example 0 is stdin.
42+
#
43+
# Pass sources to Vips::Image.new_from_source to load images from
44+
# them.
45+
#
46+
# @param descriptor [Integer] the file descriptor
47+
# @return [Source] the new Vips::Source
48+
def self.new_from_descriptor(descriptor)
49+
ptr = Vips::vips_source_new_from_descriptor descriptor
50+
raise Vips::Error if ptr.null?
51+
52+
Vips::Source.new ptr
53+
end
54+
55+
# Create a new source from a file name.
56+
#
57+
# Pass sourca to Vips::Image.new_from_source to load images from
58+
# them.
59+
#
60+
# @param filename [String] the name of the file
61+
# @return [Source] the new Vips::Source
62+
def self.new_from_file(filename)
63+
raise Vips::Error, "filename is nil" if filename.nil?
64+
ptr = Vips::vips_source_new_from_file filename
65+
raise Vips::Error if ptr.null?
66+
67+
Vips::Source.new ptr
68+
end
69+
70+
# Create a new source from an area of memory. Memory areas can be
71+
# strings, arrays and so forth -- anything that supports bytesize.
72+
#
73+
# Pass sources to Vips::Image.new_from_source to load images from
74+
# them.
75+
#
76+
# @param data [String] memory area
77+
# @return [Source] the new Vips::Source
78+
def self.new_from_memory(data)
79+
ptr = Vips::vips_source_new_from_memory data, data.bytesize
80+
raise Vips::Error if ptr.null?
81+
82+
# FIXME do we need to keep a ref to the underlying memory area? what
83+
# about Image.new_from_buffer? Does that need a secret ref too?
84+
85+
Vips::Source.new ptr
86+
end
87+
88+
end
89+
end

lib/vips/streamiu.rb renamed to lib/vips/sourcecustom.rb

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,45 @@
88

99
module Vips
1010
if Vips::at_least_libvips?(8, 9)
11-
attach_function :vips_streamiu_new, [], :pointer
11+
attach_function :vips_source_custom_new, [], :pointer
1212
end
1313

14-
# A user input stream you can attach action signal handlers to to implememt
14+
# A source you can attach action signal handlers to to implememt
1515
# custom input types.
1616
#
1717
# ```ruby
18-
# stream = Vips::Streamiu.new
19-
# stream.signal_connect "read" do |buf, len|
20-
# # read up to len bytes into buf, return the nuber of bytes
21-
# # actually read
18+
# source = Vips::SourceCustom.new
19+
# source.on_read do |len|
20+
# # return up to len bytes
2221
# end
2322
# ```
24-
class Streamiu < Vips::Streami
25-
module StreamiuLayout
23+
class SourceCustom < Vips::Source
24+
module SourceCustomLayout
2625
def self.included(base)
2726
base.class_eval do
28-
layout :parent, Vips::Streami::Struct
27+
layout :parent, Vips::Source::Struct
2928
# rest opaque
3029
end
3130
end
3231
end
3332

34-
class Struct < Vips::Streami::Struct
35-
include StreamiuLayout
33+
class Struct < Vips::Source::Struct
34+
include SourceCustomLayout
3635
end
3736

38-
class ManagedStruct < Vips::Streami::ManagedStruct
39-
include StreamiuLayout
37+
class ManagedStruct < Vips::Source::ManagedStruct
38+
include SourceCustomLayout
4039
end
4140

4241
def initialize
43-
pointer = Vips::vips_streamiu_new
42+
pointer = Vips::vips_source_custom_new
4443
raise Vips::Error if pointer.null?
4544

4645
super pointer
4746
end
4847

4948
# The block is executed to read data from the source. The interface is
50-
# exactly as IO::seek, ie. it takes a maximum number of bytes to read and
49+
# exactly as IO::read, ie. it takes a maximum number of bytes to read and
5150
# returns a string of bytes from the source, or nil if the source is already
5251
# at end of file.
5352
def on_read &block
@@ -67,7 +66,7 @@ def on_read &block
6766
# position.
6867
#
6968
# This handler is optional -- if you do not attach a seek handler,
70-
# ruby-vips will treat your stream as an unseekable pipe-like object, and
69+
# ruby-vips will treat your source as an unseekable pipe-like object, and
7170
# do extra caching.
7271
def on_seek &block
7372
signal_connect "seek" do |offset, whence|

0 commit comments

Comments
 (0)