Module: ImageFilterDsl::Binary::Serialize

Defined in:
lib/image_filter_dsl/binary/serialize.rb

Overview

Module providing serialization functionality for converting between FilterKernel instance and binary IfdKernel record

Object -> Binary collapse

Binary -> Object collapse

Class Method Details

.from_kernel(filter) ⇒ Binary::Struct::IfdKernel

Build binary IfdKernel record from Kernel object

Parameters:

Returns:



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/image_filter_dsl/binary/serialize.rb', line 26

def self.from_kernel(filter)
    variables = {
        inputs: nil,
        outputs: nil,
        pool: nil,
        symbols: nil
    }

    parts = {}

    # Collect variables
    pool = (filter.inputs + filter.outputs).uniq
    filter.instructions.each do |i|
        pool += i.inputs
        pool += [i.out]
        pool.uniq!
    end
    variables[:pool] = pool

    # Generate variable symbols
    variables[:symbols] = {}
    count = 1
    variables[:pool].each do |v|
        variables[:symbols][v] = count
        count += 1
    end

    # Build variables record

    vars_rec = Binary::Struct::IfdVariables.new(
        field_count: variables[:pool].length,
        input_count: filter.inputs.length,
        output_count: filter.outputs.length,
        input_fields: filter.inputs.map { |i| variables[:symbols][i] },
        output_fields: filter.outputs.map { |i| variables[:symbols][i] }
    )

    fkinds = Binary::Struct::FIELD_KINDS

    defs = variables[:symbols].keys.map do |k|
        f_field = Binary::Struct::IfdField.new(
            symbol: variables[:symbols][k]
        )

        if k.is_a?(Symbol)
            f_field.kind = fkinds[:var]
            f_field.name_length = k.to_s.length
            f_field.variable.assign(k.to_s)
        elsif k.is_a?(Integer)
            f_field.kind = fkinds[:literal_int]
            f_field.int_value = k
        else
            f_field.kind = fkinds[:literal_float]
            f_field.float_value = k
        end

        f_field
    end

    vars_rec.definition = defs
    parts[:variables] = vars_rec

    # Build instructions records
    parts[:instructions] = filter.instructions.map do |i|
        inp_fields = i.inputs.map { |f| variables[:symbols][f] }
        f_ins = Binary::Struct::IfdInstruction.new(
            operation: Binary::Struct.instruction_symbol(i.op),
            input_count: i.inputs.length,
            input_fields: inp_fields,
            output_field: variables[:symbols][i.out]
        )
        f_ins
    end

    # Build Kernel Record
    kernel = Binary::Struct::IfdKernel.new(
        header: Binary::Struct::IfdHeader.new,
        variables: parts[:variables],
        instruction_count: filter.instructions.length,
        instructions: parts[:instructions]
    )

    return kernel
end

.from_record(record) ⇒ Dsl::Kernel::FilterKernel

Convert IfdKernel record to FilterKernel object

Parameters:

Returns:



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/image_filter_dsl/binary/serialize.rb', line 125

def self.from_record(record)
    ins = []
    outs = []

    vars = record.variables
    vardefs = vars.definition

    vars_val = Proc.new do |vs,force_string=false|
        r = nil
        i = vardefs.select{|v| v.symbol == vs}[0]
        if i.kind == Binary::Struct::FIELD_KINDS[:var]
            r = i.variable.strip.to_sym
            r = ":#{r.to_s}" if force_string
        elsif i.kind == Binary::Struct::FIELD_KINDS[:literal_int]
            r = i.int_value
        else
            r = i.float_value
        end
        r
    end

    op_sym = Proc.new do |opc|
        Dsl::FilterInstructions::OP_INS.select{|k,v| v==opc}.keys[0]
    end

    ins = vars.input_fields.map { |f|
        vardefs.select{|v| v.symbol == f}[0].variable.strip.to_sym
    }

    outs = vars.output_fields.map { |f|
        vardefs.select{|v| v.symbol == f}[0].variable.strip.to_sym
    }

    instructions = record.instructions.map do |i|
        [
            "#{op_sym.call(i.operation)} ",
            i.input_fields.map{|f| vars_val.call(f) }.to_s,
            " , ", vars_val.call(i.output_field, true).to_s
        ].join("")
    end

    f = Dsl::Filter.define(ins, outs){
        self.instance_eval(instructions.join("\n"))
    }

    return f
end

.to_kernel(record) ⇒ Object

Alias for Serialize::from_record



117
118
119
# File 'lib/image_filter_dsl/binary/serialize.rb', line 117

def self.to_kernel(record)
    from_record(record)
end

.to_record(filter) ⇒ Object

Alias for Serialize::from_kernel



18
19
20
# File 'lib/image_filter_dsl/binary/serialize.rb', line 18

def self.to_record(filter)
    from_kernel(filter)
end