3
3
require 'vips'
4
4
5
5
class Mystreami < Vips ::Streamiu
6
- def initialize ( filename )
7
- puts "Mystreami: initialize"
8
-
6
+ def initialize ( filename , pipe_mode = false )
9
7
@filename = filename
10
- @contents = File . read ( @filename )
8
+ @contents = File . open ( @filename , "rb" ) . read
11
9
@length = @contents . length
10
+ @pipe_mode = pipe_mode
12
11
@read_point = 0
13
12
14
13
super ( )
15
14
16
15
signal_connect "read" do |buf , len |
17
- puts "Mystreami read: #{ len } bytes"
18
-
19
16
bytes_available = @length - @read_point
20
17
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"
23
18
buf . put_bytes ( 0 , @contents , @read_point , bytes_to_copy )
24
19
@read_point += bytes_to_copy
25
20
26
21
bytes_to_copy
27
22
end
28
23
29
24
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
32
42
end
33
-
34
43
end
35
44
end
36
45
37
46
class Mystreamo < Vips ::Streamou
38
47
def initialize ( filename )
39
- puts "Mystreamo: initialize"
40
-
41
48
@filename = filename
42
49
@f = File . open ( @filename , "wb" )
43
50
44
51
super ( )
45
52
46
53
signal_connect "write" do |buf , len |
47
- puts "Mystreami write: #{ len } bytes"
48
- @f . write ( buf )
54
+ @f . write ( buf . get_bytes ( 0 , len ) )
49
55
len
50
56
end
51
57
@@ -59,8 +65,8 @@ def initialize(filename)
59
65
60
66
streamiu = Mystreami . new ( ARGV [ 0 ] )
61
67
image = Vips ::Image . new_from_stream ( streamiu , "" , access : "sequential" )
62
- image . avg
68
+ #puts "avg = #{ image.avg}"
63
69
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" )
66
72
0 commit comments