image_filter_dsl 0.1.0 → 0.1.1

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: 64f070bf94d94ef0c6e0de6a78fa7ccd8f29f181e866d4aa374f26a4e427f18f
4
- data.tar.gz: 6347c8d1b350387e369e2e56d023f0fa944dd70a4870035af55619287e11bd7f
3
+ metadata.gz: b3c5ec517d5379d54dcac83356f816f4e460ea651956600c4a406766483fff9e
4
+ data.tar.gz: a26273761002413d8f2944fce7c6d9da591a3b3124be8a640f9c375a029df739
5
5
  SHA512:
6
- metadata.gz: a8c55bf124bf7e454021ec8b644373566467eeec81786875bc9fa4d89ee0fa86e7e7044c71c83aa0396a3e97c7b0347d3cdd55e952e98c256c02b9f48a524d4e
7
- data.tar.gz: 4590228397df810c7b75a5c59c1a4a1510e4e7ce723dd36e168f7a9f377dc6decf1b434cdca926e0c6ecc7a4196dae96b7ba6d0beda91b3743994b6e96bd910d
6
+ metadata.gz: 573236b32a4096fe6d8e57106cf9e4e79e9c4571583d0b9d1ceb710e8cb2dd227cadc51597bd5d8751fd55efd86ee4038207598833a38f3f41da8d8e3c9b5e47
7
+ data.tar.gz: f3703bc27fce75391023a2e8ab056f721471f3e522326f4201fa98600f62f70b75e3fb9b7a5850208c395883746e5d8ee5d4bd0b0e847f4b1e25b101bdeb2a21
@@ -2,9 +2,11 @@
2
2
 
3
3
  require 'image_filter_dsl'
4
4
 
5
- printf "Image Filter DSL > Process Image\n"
6
- if ARGV.length == 0
7
- printf "Usage: image_filter_dsl <kernel_file> <image_in> <image_out>\n"
8
- else
9
- ImageFilterDsl.cli_process_image(ARGV)
10
- end
5
+ # printf "Image Filter DSL > Process Image\n"
6
+ # if ARGV.length == 0
7
+ # printf "Usage: image_filter_dsl <kernel_file> <image_in> <image_out>\n"
8
+ # else
9
+ # ImageFilterDsl.cli_process_image(ARGV)
10
+ # end
11
+
12
+ ImageFilterDsl::Engine::CLI.run
@@ -5,7 +5,8 @@ require_relative './image_filter_dsl/binary/struct.rb'
5
5
  require_relative './image_filter_dsl/binary/serialize.rb'
6
6
  require_relative './image_filter_dsl/engine/io.rb'
7
7
  require_relative './image_filter_dsl/engine/image_processor.rb'
8
-
8
+ require_relative './image_filter_dsl/serialize.rb'
9
+ require_relative './image_filter_dsl/engine/cli.rb'
9
10
  ##
10
11
  # Image Filter DSL Library
11
12
  # (c) 2018-2020 VDTDEV/Wade H. ~ MIT License
@@ -5,7 +5,6 @@
5
5
  module ImageFilterDsl
6
6
  ##
7
7
  # Module defining DSL
8
- # {include:file:dsl.md}
9
8
  module Dsl
10
9
  ##
11
10
  # Module defining filter kernel instruction logic
@@ -0,0 +1,83 @@
1
+ ##
2
+ # Image Filter DSL Library
3
+ # (c) 2018-2020 VDTDEV/Wade H. ~ MIT License
4
+ # @author Wade H. <vdtdev.prod@gmail.com>
5
+ module ImageFilterDsl
6
+ module Engine
7
+ ##
8
+ # CLI Argument parser and handler module
9
+ module CLI
10
+ extend self
11
+
12
+ Options = Struct.new(:action, :filter, :input, :output)
13
+ RequiredArgs = {
14
+ process: [:filter, :input, :output],
15
+ compile: [:input, :output]
16
+ }
17
+
18
+ def parse(args)
19
+ opts = Options.new
20
+ args=args.map{|s|s.strip}
21
+
22
+ p = args[0]
23
+
24
+ if !p.nil? && p.downcase == '--process'
25
+ opts.action = :process
26
+ elsif !p.nil? && p.downcase == '--compile'
27
+ opts.action = :compile
28
+ end
29
+
30
+ unless opts.action.nil?
31
+ case opts.action
32
+ when :process
33
+ f,i,o = args[1..-1]
34
+ opts.filter = f
35
+ opts.input = i
36
+ opts.output = o
37
+ when :compile
38
+ s,k = args[1..-1]
39
+ opts.input = s
40
+ opts.output = k
41
+ end
42
+ end
43
+
44
+ return opts
45
+ end
46
+
47
+ def run()
48
+ options = parse(ARGV)
49
+ if options.action.nil?
50
+ show_usage
51
+ else
52
+ p options
53
+ if RequiredArgs[options.action].select{|o| options[o].nil?}.length == 0
54
+ do_action(options)
55
+ else
56
+ show_usage(options.action)
57
+ end
58
+ end
59
+ end
60
+
61
+ def show_usage(action=nil)
62
+ if !action.nil?
63
+ print "Invalid arguments for #{action.to_s}\n"
64
+ end
65
+ print [
66
+ "Usage: image_filter_dsl <action<",
67
+ "\t--process <kernel_file> <image in> <image out>\t\tProcess image with binary kernel",
68
+ "\t--compile <source_file> <output_kernel>\t\tCompile given ruby source to binary kernel"
69
+ ].join("\n")
70
+ print "\n"
71
+ end
72
+
73
+ def do_action(opts)
74
+ if opts.action == :process
75
+ ip = ImageFilterDsl.image_processor(opts.filter)
76
+ ip.process_image(opts.input, opts.output)
77
+ elsif opts.action == :compile
78
+ print "(Not yet implemented :)\n"
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,69 @@
1
+ require_relative './serializers/common.rb'
2
+ require_relative './serializers/msg_pack.rb'
3
+ require_relative './serializers/json.rb'
4
+ require_relative './serializers/yaml.rb'
5
+ require_relative './serializers/intermediate.rb'
6
+ ##
7
+ # Image Filter DSL Library
8
+ # (c) 2018-2020 VDTDEV/Wade H. ~ MIT License
9
+ # @author Wade H. <vdtdev.prod@gmail.com>
10
+ module ImageFilterDsl
11
+ ##
12
+ # Module providing access to kernel serialization and
13
+ # deserialization functionality. Actual serialization/deserialization
14
+ # logic defined in modules within `Serializers`
15
+ module Serialize
16
+ extend self
17
+ # include Serialize::Json
18
+ # include Serializers::Json
19
+
20
+ ##
21
+ # Intermediate serializer module
22
+ INTERMEDIATE = Serializers::Intermediate
23
+
24
+ ##
25
+ # Available serializers, mapping format symbol to
26
+ # module
27
+ SERIALIZERS = {
28
+ json: Serializers::Json,
29
+ msgpack: Serializers::MsgPack,
30
+ yaml: Serializers::Yaml
31
+ }
32
+ ##
33
+ # Available serializer symbols
34
+ FORMATS = SERIALIZERS.keys
35
+
36
+ ##
37
+ # Serialize kernel, optionally using intermediate step
38
+ # @param [ImageFilterDsl::Dsl::Kernel] kernel Kernel
39
+ # @param [Symbol] format Format to serialize to from FORMATS, or
40
+ # `nil` to convert to intermediate
41
+ # @return [Hash|String] serialized kernel
42
+ def from_kernel(kernel, format=nil)
43
+ i = INTERMEDIATE.from_kernel(kernel)
44
+ if format.nil?
45
+ return i
46
+ else
47
+ m = SERIALIZERS[format]
48
+ return m.send(:serialize, i)
49
+ end
50
+ end
51
+
52
+ ##
53
+ # Deserialize to kernel, optionally using intermediate step
54
+ # @param [Hash|String] data Serialized data
55
+ # @param [Symbol] format Format to convert from from FORMATS, or
56
+ # `nil` to convert from intermediate
57
+ # @return [Kernel] Kernal from deserialization
58
+ def to_kernel(data,format=nil)
59
+ if format.nil?
60
+ return INTERMEDIATE.to_kernel(data)
61
+ else
62
+ i = SERIALIZERS[format].send(:deserialize, data)
63
+ return INTERMEDIATE.to_kernel(i)
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+
@@ -0,0 +1,57 @@
1
+ #
2
+ # Image Filter DSL Library
3
+ # (c) 2018-2020 VDTDEV/Wade H. ~ MIT License
4
+ # @author Wade H. <vdtdev.prod@gmail.com>
5
+ module ImageFilterDsl
6
+ module Serializers
7
+ ##
8
+ # Common helper methods for serializers; Include in serializer modules
9
+ # with `include Common`
10
+ module Common
11
+ extend self
12
+ ##
13
+ # Normalize symbol/value, stripping out
14
+ # any colons and converting to symbol; Non strings
15
+ # are returned unaltered; if array, logic is applied to all items
16
+ # @param [String|Number|Symbol|Array] val
17
+ # @return [Symbol|Number|Array] Normalized value
18
+ def normalize_sym(val)
19
+ if val.is_a?(String)
20
+ val.gsub(':','').to_sym
21
+ elsif val.is_a?(Array)
22
+ val.map{|v|normalize_sym(v)}
23
+ else
24
+ val
25
+ end
26
+ end
27
+ ##
28
+ # 'Fix' hash, converting keys to symbols (if keys provided)
29
+ # and converting values/arrays of values so any strings are
30
+ # normalized and cast to symbols using {normalize_sym}
31
+ # @param [Hash] hash Hash to fix
32
+ # @param [Array] keys Optional array of key symbols to check for and
33
+ # convert string equivalents over to symbols if found
34
+ # @return [Hash] Fixed copy of input hash
35
+ def fix_hash(hash,keys=[])
36
+ h2=hash.dup
37
+ unless keys.length == 0
38
+ keys.each do |k|
39
+ if !h2.has_key?(k) && h2.has_key?(k.to_s)
40
+ h2[k] = h2[k.to_s]
41
+ h2.delete k.to_s
42
+ end
43
+ end
44
+ end
45
+ h2.keys.each do |k|
46
+ if h2[k].is_a?(Array)
47
+ h2[k] = h2[k].map{|v|normalize_sym(v)}
48
+ else
49
+ h2[k] = normalize_sym(h2[k])
50
+ end
51
+ end
52
+ return h2
53
+ end
54
+ end
55
+ end
56
+ end
57
+
@@ -0,0 +1,65 @@
1
+ ##
2
+ # Image Filter DSL Library
3
+ # (c) 2018-2020 VDTDEV/Wade H. ~ MIT License
4
+ # @author Wade H. <vdtdev.prod@gmail.com>
5
+ module ImageFilterDsl
6
+ ##
7
+ # Kernel data serializers
8
+ module Serializers
9
+ ##
10
+ # Provides serialization/deserialization for Kernels
11
+ # to an intermediate format representing kernel as hash.
12
+ # Used by other serializers as intermediate between serializing
13
+ # from and deserializing to kernels
14
+ module Intermediate
15
+ include Common
16
+ extend self
17
+ ##
18
+ # Convert kernel to intermediate hash struct
19
+ # @param [ImageFilterDsl::Dsl::Kernel] kernel Source kernel
20
+ # @return [Hash] Intermediate hash version of kernel
21
+ def from_kernel(kernel)
22
+ data = {
23
+ inputs: kernel.inputs,
24
+ outputs: kernel.outputs
25
+ }
26
+ data[:instructions] = kernel.instructions.map do |i|
27
+ {op: i.op, in: i.inputs, out: i.out}
28
+ end
29
+ data
30
+ end
31
+
32
+ ##
33
+ # Convert intermediate kernel hash to kernel object
34
+ # @param [Hash] data Intermediate kernel data
35
+ # @return [ImageFilterDsl::Dsl::Kernel] Constructed kernel
36
+ def to_kernel(data)
37
+ ins = data[:instructions].map{|i| fix_instruction_symbols(i) }
38
+ filt = ImageFilterDsl::Dsl::Filter.define(data[:inputs],data[:outputs]) do
39
+ ins.each do |i|
40
+ send(i[:op], i[:in], i[:out])
41
+ end
42
+ end
43
+ return filt
44
+ end
45
+
46
+ private
47
+
48
+ def fix_instruction_symbols(ins_hash)
49
+ fix_hash(ins_hash,[:op,:in,:out])
50
+ # r_k=Proc.new {|k| (ins_hash.has_key?(k))? k : k.to_s}
51
+ # op_h = r_k.call(:op)
52
+ # in_h = r_k.call(:in)
53
+ # out_h = r_k.call(:out)
54
+ # norm_sym = Proc.new {|s| (s.is_a?(String))? s.gsub(':','').to_sym : s}
55
+ # {
56
+ # op: norm_sym.call(ins_hash[op_h]),
57
+ # in: ins_hash[in_h].map { |i|
58
+ # norm_sym.call(i)
59
+ # },
60
+ # out: norm_sym.call(ins_hash[out_h])
61
+ # }
62
+ end
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,43 @@
1
+ require 'json'
2
+ ##
3
+ # Image Filter DSL Library
4
+ # (c) 2018-2020 VDTDEV/Wade H. ~ MIT License
5
+ # @author Wade H. <vdtdev.prod@gmail.com>
6
+ module ImageFilterDsl
7
+ module Serializers
8
+ ##
9
+ # Provides serialization/deserialization to/from JSON using
10
+ # intermediate serializer
11
+ module Json
12
+ include Common
13
+ extend self
14
+ ##
15
+ # Serialize kernel to JSON string
16
+ # @param [Hash] kernel Kernel in intermediate format
17
+ # @return [String] JSON serialized version of kernel
18
+ def serialize(kernel)
19
+ {
20
+ header: ImageFilterDsl::Binary::Struct::HEADER_VALUES,
21
+ inputs: kernel[:inputs],
22
+ outputs: kernel[:outputs],
23
+ instructions: kernel[:instructions]
24
+ }.to_json
25
+ end
26
+ ##
27
+ # Deserialize JSON kernel to intermediate format
28
+ # @param [String] data JSON kernel data
29
+ # @return [Hash] Intermediate format of kernel data
30
+ def deserialize(data)
31
+ data = JSON.load(data)
32
+ data = fix_hash(data,[:inputs,:outputs,:instructions])
33
+ data[:instructions] = data[:instructions].map {|i| fix_hash(i,[:op,:in,:out])}
34
+
35
+ {
36
+ inputs: data[:inputs],
37
+ outputs: data[:outputs],
38
+ instructions: data[:instructions]
39
+ }
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,51 @@
1
+ require 'msgpack'
2
+ ##
3
+ # Image Filter DSL Library
4
+ # (c) 2018-2020 VDTDEV/Wade H. ~ MIT License
5
+ # @author Wade H. <vdtdev.prod@gmail.com>
6
+ module ImageFilterDsl
7
+ module Serializers
8
+ ##
9
+ # Provides serialization/deserialization to/from MessagePack strings
10
+ # using intermediate serializer
11
+ module MsgPack
12
+ include Common
13
+ extend self
14
+ ##
15
+ # 'Header' added to start of array packed with MsgPack
16
+ HEADER = ['mp',ImageFilterDsl::Binary::Struct::HEADER_VALUES.values].flatten
17
+ ##
18
+ # Serialize kernel to MessagePack string
19
+ # @param [Hash] kernel Kernel in intermediate format
20
+ # @return [String] MessagePack serialized version of kernel
21
+ def serialize(kernel)
22
+ [
23
+ HEADER,
24
+ kernel[:inputs],
25
+ kernel[:outputs],
26
+ kernel[:instructions].map do |i|
27
+ [i[:op],i[:in],i[:out]]
28
+ end
29
+ ].to_msgpack
30
+ end
31
+ ##
32
+ # Deserialize MessagePack kernel to intermediate format
33
+ # @param [String] data MessagePack kernel data
34
+ # @return [Hash] Intermediate format of kernel data
35
+ def deserialize(data)
36
+ data = MessagePack.unpack(data)
37
+ head,inps,outs,ins=data
38
+ d = {
39
+ inputs: normalize_sym(inps),
40
+ outputs: normalize_sym(outs),
41
+ instructions: ins.map do |i|
42
+ op,iin,iout=i
43
+ fix_hash({op: op, in: iin, out: iout})
44
+ end
45
+ }
46
+
47
+ return d
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,42 @@
1
+ require 'yaml'
2
+ ##
3
+ # Image Filter DSL Library
4
+ # (c) 2018-2020 VDTDEV/Wade H. ~ MIT License
5
+ # @author Wade H. <vdtdev.prod@gmail.com>
6
+ module ImageFilterDsl
7
+ module Serializers
8
+ ##
9
+ # Provides serialization/deserialization to/from YAML using
10
+ # intermediate serializer
11
+ module Yaml
12
+ include Common
13
+ extend self
14
+ ##
15
+ # Serialize kernel to YAML string
16
+ # @param [Hash] kernel Kernel in intermediate format
17
+ # @return [String] YAML serialized version of kernel
18
+ def serialize(kernel)
19
+ {
20
+ header: ImageFilterDsl::Binary::Struct::HEADER_VALUES,
21
+ inputs: kernel[:inputs],
22
+ outputs: kernel[:outputs],
23
+ instructions: kernel[:instructions]
24
+ }.to_yaml
25
+ end
26
+ ##
27
+ # Deserialize YAML kernel to intermediate format
28
+ # @param [String] data YAML kernel data
29
+ # @return [Hash] Intermediate format of kernel data
30
+ def deserialize(data)
31
+ data = YAML.load(data)
32
+ data = fix_hash(data,[:inputs,:outputs,:instructions])
33
+ data[:instructions] = data[:instructions].map {|i| fix_hash(i,[:op,:in,:out])}
34
+ {
35
+ inputs: data[:inputs],
36
+ outputs: data[:outputs],
37
+ instructions: data[:instructions]
38
+ }
39
+ end
40
+ end
41
+ end
42
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: image_filter_dsl
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Wade H.
@@ -89,14 +89,21 @@ files:
89
89
  - lib/image_filter_dsl/dsl/filter.rb
90
90
  - lib/image_filter_dsl/dsl/filter_instructions.rb
91
91
  - lib/image_filter_dsl/dsl/kernel.rb
92
+ - lib/image_filter_dsl/engine/cli.rb
92
93
  - lib/image_filter_dsl/engine/image_processor.rb
93
94
  - lib/image_filter_dsl/engine/io.rb
95
+ - lib/image_filter_dsl/serialize.rb
96
+ - lib/image_filter_dsl/serializers/common.rb
97
+ - lib/image_filter_dsl/serializers/intermediate.rb
98
+ - lib/image_filter_dsl/serializers/json.rb
99
+ - lib/image_filter_dsl/serializers/msg_pack.rb
100
+ - lib/image_filter_dsl/serializers/yaml.rb
94
101
  homepage: https://bitbucket.org/WadeH/image_filter_dsl
95
102
  licenses:
96
103
  - MIT
97
104
  metadata:
98
105
  source_code_uri: https://bitbucket.org/WadeH/image_filter_dsl
99
- documentation_uri: https://rubydoc.info/gems/image_filter_dsl/0.1.0
106
+ documentation_uri: https://rubydoc.info/gems/image_filter_dsl/0.1.1
100
107
  post_install_message:
101
108
  rdoc_options: []
102
109
  require_paths:
@@ -112,7 +119,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
112
119
  - !ruby/object:Gem::Version
113
120
  version: '0'
114
121
  requirements: []
115
- rubygems_version: 3.0.1
122
+ rubygems_version: 3.0.0
116
123
  signing_key:
117
124
  specification_version: 4
118
125
  summary: Image Filter DSL