pixelflut 0.1.3 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2e00558ad6f4afeeda5a86d00ee8fcbfffc09bf3978aa02b7997c69541b4ec71
4
- data.tar.gz: fb2ec338298452be18a2a9c5352b49100628efb5234b94ee0d25f2f57e759607
3
+ metadata.gz: 289cd742f8feabae71182b94738bf9b21246bf1f3afae4a4b2bc1c5b1be87d4c
4
+ data.tar.gz: 2ba98d609131de67a2be99f55681087b45eeaf4deb6bf488b393894c1b31c6c9
5
5
  SHA512:
6
- metadata.gz: aa654604990a4af74b65d074a65635ba04d302b4974b37d68fe1a2d155a9dbc4495e62c5422b2c1ce55b9dfbd63b0ca208111ddbe26cf3426ffaf26a55d03573
7
- data.tar.gz: '099c5c9b3d013d26eb19616e0cef6a7fb2a1a391ddd4bae4e5ce77d263eee1ffcf5cb0311a868a7ab7656eb3cbb621ba7ab02d9e83cc19b3722ca16da9ec6b7c'
6
+ metadata.gz: 9a8b53a0309a0c52a8898bab91f9c1ba95bb1622f733fee1236914b036dabae5ad2b375a55b6bb5c5cb3d1d9ba33f9d200cde650d014232e193f598ce8d3de8f
7
+ data.tar.gz: ba1480300f12b1fae6dba2fcec945537b519dd04fabffd345288a5b3c2c1f1c8e5ae6d9115314663772feeaa6d74c8b8198c8d905f6c8c8e0fab51c27a8594e2
data/bin/pxf CHANGED
@@ -12,7 +12,8 @@ def err(msg, code = 1)
12
12
  exit(code)
13
13
  end
14
14
 
15
- OPTS = Struct
15
+ OPTS =
16
+ Struct
16
17
  .new(:host, :port, :count, :x, :y, :mode, :scale, :image)
17
18
  .new('127.0.0.1', 1234, 4, 0, 0, :rgbx)
18
19
 
@@ -62,8 +63,8 @@ end
62
63
 
63
64
  begin
64
65
  parse(ARGV)
65
- addr = Pixelflut::Sender.address(OPTS.host, OPTS.port)
66
- slices = Pixelflut.as_slices(
66
+ address = Pixelflut::Sender.address(OPTS.host, OPTS.port)
67
+ data = Pixelflut.sliced(
67
68
  source: OPTS.image,
68
69
  count: OPTS.count,
69
70
  x: OPTS.x,
@@ -71,18 +72,19 @@ begin
71
72
  scale: OPTS.scale,
72
73
  mode: OPTS.mode
73
74
  )
74
- slices.size.times do |i|
75
+ data.size.times do |i|
75
76
  next unless fork
76
77
  Process.setproctitle($name = format('pxf-%02d', i + 1))
77
- sender = Pixelflut::Sender.new(addr, slices[i].join)
78
- slices.clear
79
- puts("#{$name}: run - #{sender.size} bytes")
80
- sender.run{ puts("#{$name}: sent partly") }
78
+ data = data[i].join
79
+ puts("#{$name}: run - #{data.bytesize} bytes")
80
+ Pixelflut::Sender.send(address, data)
81
81
  end
82
82
  rescue SocketError => e
83
83
  err(e, 3)
84
84
  rescue LoadError => e
85
85
  err(e, 4)
86
+ rescue Errno::ECONNREFUSED
87
+ err('unable to connect', 2)
86
88
  rescue Errno::EPIPE
87
89
  err('connection lost', 2)
88
90
  rescue Interrupt
@@ -11,9 +11,9 @@ module Pixelflut
11
11
  Convert.each_line(image, x, y)
12
12
  end
13
13
 
14
- def self.as_slices(source:, count: 4, x: 0, y: 0, scale: nil, mode: :rgbx)
14
+ def self.sliced(source:, count: 4, x: 0, y: 0, scale: nil, mode: :rgbx)
15
15
  image = Image.new(source)
16
16
  image.scale(scale) if scale
17
- Convert.as_slices(image, x, y, mode, count)
17
+ Convert.random_slices(image, x, y, mode, count)
18
18
  end
19
19
  end
@@ -2,6 +2,16 @@ require 'rmagick'
2
2
 
3
3
  module Pixelflut
4
4
  module Convert
5
+ def self.random_slices(image, dx, dy, mode, count)
6
+ mode = MODE.fetch(mode) if Symbol === mode
7
+ ret, idx = Array.new(count){ [] }, 0
8
+ image.each_pixel.to_a.shuffle!.each do |x, y, px|
9
+ ret[idx % count] << "PX #{x + dx} #{y + dy} #{mode.call(px)}\n"
10
+ idx += 1
11
+ end
12
+ ret
13
+ end
14
+
5
15
  MODE = {
6
16
  rgb: lambda do |pixel|
7
17
  pixel.to_color(Magick::AllCompliance, false, 8, true)[1, 6]
@@ -17,25 +27,5 @@ module Pixelflut
17
27
  end
18
28
  end
19
29
  }.freeze
20
-
21
- def self.each_line(image, dx, dy, mode)
22
- return to_enum(__method__, image, dx, dy, mode) unless block_given?
23
- mode = MODE.fetch(mode) if Symbol === mode
24
- image.each_pixel do |x, y, px|
25
- yield("PX #{x + dx} #{y + dy} #{mode.call(px)}\n")
26
- end
27
- end
28
-
29
- def self.as_slices(image, dx, dy, mode, count)
30
- ret = each_line(image, dx, dy, mode).to_a.shuffle!
31
- ret = ret.each_slice(ret.size / (count + 1)).to_a
32
- rest = ret.pop
33
- i = 0
34
- rest.each do |line|
35
- ret[i] << line
36
- i = 0 if (i+= 1) == ret.size
37
- end
38
- ret
39
- end
40
30
  end
41
31
  end
@@ -1,47 +1,27 @@
1
1
  require 'socket'
2
2
 
3
3
  module Pixelflut
4
- class Sender
5
- def self.address(host, port)
6
- Addrinfo.tcp(host, port)
7
- end
8
-
9
- def initialize(address, data)
10
- @data = data.freeze
11
- @addr = Socket.pack_sockaddr_in(address.ip_port, address.ip_address)
12
- @socket = configure(Socket.new(address.ipv6? ? :INET6 : :INET, :STREAM))
13
- end
4
+ module Sender
5
+ module_function
14
6
 
15
- def size
16
- @data.bytesize
17
- end
18
-
19
- def run(&block)
20
- @socket.connect(@addr)
21
- send(@socket, @data, @data.bytesize, &block)
22
- @socket.close
7
+ def address(host, port)
8
+ Addrinfo.tcp(host, port)
23
9
  end
24
10
 
25
- private
26
-
27
- def send(socket, data, bytesize)
28
- index, curr, size = 0, data, bytesize
29
- loop do
30
- written = socket.write(curr)
31
- if (size -= written) > 0
32
- yield(self) if block_given?
33
- curr = data.byteslice(index += written, size)
34
- else
35
- index, curr, size = 0, data, bytesize
36
- end
37
- end
11
+ def send(address, data)
12
+ socket = create_socket(address)
13
+ loop{ socket.write(data) }
38
14
  end
39
15
 
40
- def configure(socket)
41
- socket.sync = true
42
- socket.setsockopt(:TCP, :NODELAY, 1)
16
+ def create_socket(address)
17
+ socket = Socket.new(address.ipv6? ? :INET6 : :INET, :STREAM)
18
+ # socket.sync = true
19
+ # socket.setsockopt(:TCP, :NODELAY, 1)
43
20
  socket.setsockopt(:SOCKET, :KEEPALIVE, 0)
44
21
  socket.do_not_reverse_lookup = true
22
+ socket.connect(
23
+ Socket.pack_sockaddr_in(address.ip_port, address.ip_address)
24
+ )
45
25
  socket
46
26
  end
47
27
  end
@@ -1,3 +1,3 @@
1
1
  module Pixelflut
2
- VERSION = '0.1.3'.freeze
2
+ VERSION = '0.1.5'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pixelflut
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Blumtritt
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-11-27 00:00:00.000000000 Z
11
+ date: 2019-11-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rmagick