Skip to content

Commit dab4f39

Browse files
committed
fix stream*u example
all seems to be working now
1 parent 64dd392 commit dab4f39

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

example/stream.rb

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,55 @@
33
require 'vips'
44

55
class Mystreami < Vips::Streamiu
6-
def initialize(filename)
7-
puts "Mystreami: initialize"
8-
6+
def initialize(filename, pipe_mode=false)
97
@filename = filename
10-
@contents = File.read(@filename)
8+
@contents = File.open(@filename, "rb").read
119
@length = @contents.length
10+
@pipe_mode = pipe_mode
1211
@read_point = 0
1312

1413
super()
1514

1615
signal_connect "read" do |buf, len|
17-
puts "Mystreami read: #{len} bytes"
18-
1916
bytes_available = @length - @read_point
2017
bytes_to_copy = [bytes_available, len].min
21-
puts "Mystreami read: #{bytes_available} bytes_available"
22-
puts "Mystreami read: #{bytes_to_copy} bytes_to_copy"
2318
buf.put_bytes(0, @contents, @read_point, bytes_to_copy)
2419
@read_point += bytes_to_copy
2520

2621
bytes_to_copy
2722
end
2823

2924
signal_connect "seek" do |offset, whence|
30-
puts "Mystreami seek: offset #{offset}, whence #{whence}"
31-
-1
25+
return -1 if @pipe_mode
26+
27+
case whence
28+
when 0
29+
# SEEK_SET
30+
new_read_point = offset
31+
when 1
32+
# SEEK_CUR
33+
new_read_point = self.read_point + offset
34+
when 2
35+
# SEEK_END
36+
new_read_point = self.length + offset
37+
else
38+
raise "bad whence #{whence}"
39+
end
40+
41+
@read_point = [0, [@length, new_read_point].min].max
3242
end
33-
3443
end
3544
end
3645

3746
class Mystreamo < Vips::Streamou
3847
def initialize(filename)
39-
puts "Mystreamo: initialize"
40-
4148
@filename = filename
4249
@f = File.open(@filename, "wb")
4350

4451
super()
4552

4653
signal_connect "write" do |buf, len|
47-
puts "Mystreami write: #{len} bytes"
48-
@f.write(buf)
54+
@f.write(buf.get_bytes(0, len))
4955
len
5056
end
5157

@@ -59,8 +65,8 @@ def initialize(filename)
5965

6066
streamiu = Mystreami.new(ARGV[0])
6167
image = Vips::Image.new_from_stream(streamiu, "", access: "sequential")
62-
image.avg
68+
#puts "avg = #{image.avg}"
6369

64-
#streamio = Mystreamo.new(ARGV[1])
65-
#image.write_to_stream(streamio, ".png")
70+
streamio = Mystreamo.new(ARGV[1])
71+
image.write_to_stream(streamio, ".png")
6672

lib/vips/object.rb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,34 +68,24 @@ class Progress < FFI::Struct
6868

6969
MARSHAL_READ = lambda do |handler|
7070
FFI::Function.new(:int64_t, [:pointer, :pointer, :int64_t]) do |i, p, len|
71-
puts "MARSHAL_READ handler: #{len} bytes to read"
72-
bytes_read = handler.(p, len)
73-
puts " bytes_read = #{bytes_read}"
74-
75-
bytes_read
71+
handler.(p, len)
7672
end
7773
end
7874

7975
MARSHAL_SEEK = lambda do |handler|
8076
FFI::Function.new(:int64_t, [:pointer, :int64_t, :int]) do |i, off, whence|
81-
puts "MARSHAL_SEEK handler: off #{off}, whence #{whence}"
82-
new_position = handler.(off, whence)
83-
puts " new_position = #{new_position}"
84-
85-
new_position
77+
handler.(off, whence)
8678
end
8779
end
8880

8981
MARSHAL_WRITE = lambda do |handler|
9082
FFI::Function.new(:int64_t, [:pointer, :pointer, :int64_t]) do |i, p, len|
91-
puts "MARSHAL_WRITE handler: #{len} bytes to write"
9283
handler.(p, len)
9384
end
9485
end
9586

9687
MARSHAL_FINISH = lambda do |handler|
9788
FFI::Function.new(:void, [:pointer, :pointer]) do |i, cb|
98-
puts "MARSHAL_FINISH handler:"
9989
handler.()
10090
end
10191
end

0 commit comments

Comments
 (0)