Module: ImageFilterDsl::Dsl::FilterInstructions

Defined in:
lib/image_filter_dsl/dsl/filter_instructions.rb

Overview

Module defining filter kernel instruction logic

Constant Summary collapse

OP_INS =

Hash of binary instruction symbols

{
    # math = 0x0X-0xAF
    add:    0x01,
    mult:   0x02,
    div:    0x03,
    mod:    0x04,
    abs:    0x05,
    # collection op = 0xB0-0xCF
    min:    0xb0,
    max:    0xb1,
    avg:    0xb2,
    # logic/mem/conv op = 0xD0-??
    copy:   0xd0,
    above:  0xd1,
    below:  0xd2,
    floor:  0xd3,
    ceil:   0xd4,
    float:  0xd5,
    round:  0xd6,
    switch: 0xd7,
    eq:     0xd8,
    bnot:   0xd9,
    mix:    0xda,
    between: 0xdb,
    clamp:  0xdc,
    band:   0xdd,
    bor:    0xde,
    # generation
    rand:   0xf0,
    sin:    0xf1,
    cos:    0xf2,
    tan:    0xf3
}
OPS =

Array of all valid filter instructions

OP_INS.keys

Class Method Summary collapse

Class Method Details

.above(i) ⇒ Integer|Float

Above instruction

Parameters:

  • i (Array)

    input values ‘(a,b,trueVal,falseVal)` `trueVal` defaults to `1`, `falseVal` defaults to `0`

Returns:

  • (Integer|Float)

    output value



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 131

def self.above(i)
    i = [i,1,0].flatten if i.length == 2
    if(i[0]>i[1])
        if i.length < 3
            1
        else
            i[2]
        end
    else
        if i.length < 4
            0
        else
            i[3]
        end
    end
end

.abs(i) ⇒ Integer|Float

Absolute value

Parameters:

  • i (Array)

    input value

Returns:

  • (Integer|Float)

    output value



90
91
92
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 90

def self.abs(i)
    i[0].abs
end

.add(i) ⇒ Integer|Float

Add instruction

Parameters:

  • i (Array)

    input values

Returns:

  • (Integer|Float)

    output value



57
58
59
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 57

def self.add(i) 
    i.sum
end

.avg(i) ⇒ Integer|Float

Average instruction

Parameters:

  • i (Array)

    input values

Returns:

  • (Integer|Float)

    output value



114
115
116
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 114

def self.avg(i)
    i.sum / (1.0 * i.length)
end

.band(i) ⇒ Integer

Logical AND instruction

Parameters:

  • i (Array)

    input values (ints or floats)

Returns:

  • (Integer)

    1 if all values are ‘1` or `1.0`, else 0



266
267
268
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 266

def self.band(i)
    (i.reduce(:+).to_i == i.length)? 1 : 0
end

.below(i) ⇒ Integer|Float

Below instruction

Parameters:

  • i (Array)

    input values ‘(a,b,trueVal,falseVal)` `trueVal` defaults to `1`, `falseVal` defaults to `0`

Returns:

  • (Integer|Float)

    output value



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 153

def self.below(i)
    i = [i,1,0].flatten if i.length == 2
    if(i[0]<i[1])
        if i.length < 3
            1
        else
            i[2]
        end
    else
        if i.length < 4
            0
        else
            i[3]
        end
    end
end

.between(i) ⇒ Integer

Between instruction (check if value is between two others)

Parameters:

  • i (Array)

    input values ‘[min, max, value, trueVal, falseVal]` `trueVal` defaults to `1`, `falseVal` defaults to `0`

Returns:

  • (Integer)

    1 if true, 0 if false



175
176
177
178
179
180
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 175

def self.between(i)
    i = [i,1,0].flatten if i.length == 3
    a,b,v,t,f = i
    r = (v>=a) && (v<=b)
    (r)? t : f
end

.bnot(i) ⇒ Integer|Float

Logic invert instruction

Parameters:

  • i (Array)

    input value (0 or 1)

Returns:

  • (Integer|Float)

    output value (1 if in 0, 0 if in 1)



254
255
256
257
258
259
260
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 254

def self.bnot(i)
    if i[0].to_i == 1
        0
    else
        1
    end
end

.bor(i) ⇒ Integer

Logical OR instruction

Parameters:

  • i (Array)

    input values (ints or floats)

Returns:

  • (Integer)

    1 if any values are ‘1` or `1.0`, else 0



274
275
276
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 274

def self.bor(i)
    (i.reduce(:+).to_i > 0)? 1 : 0
end

.ceil(i) ⇒ Integer|Float

Ceil instruction

Parameters:

  • i (Array)

    input value (v)

Returns:

  • (Integer|Float)

    output value



204
205
206
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 204

def self.ceil(i)
    i[0].ceil
end

.clamp(i) ⇒ Integer|Float

Clamp value between a min and a max

Parameters:

  • i (Array)

    input values ‘[min,max,val]`

Returns:

  • (Integer|Float)

    Value forced to be no greater than ‘max` and no less than `min`



187
188
189
190
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 187

def self.clamp(i)
    a,b,v = i
    [b,[a,v].max].min
end

.copy(i) ⇒ Integer|Float

Copy instruction

Parameters:

  • i (Array)

    input values (src)

Returns:

  • (Integer|Float)

    output value



122
123
124
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 122

def self.copy(i)
    i[0]
end

.cos(i) ⇒ Float

Cosine function

Parameters:

  • i (Array)

    input value to use cos on

Returns:

  • (Float)

    Cosine of input value



309
310
311
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 309

def self.cos(i)
    Math.cos(i[0])
end

.div(i) ⇒ Integer|Float

Multiply instruction

Parameters:

  • i (Array)

    input values

Returns:

  • (Integer|Float)

    output value



75
76
77
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 75

def self.div(i)
    i[0]/i[1]
end

.eq(i) ⇒ Integer|Float

Equal condition instruction

Parameters:

  • i (Array)

    input values (a, b)

Returns:

  • (Integer|Float)

    output value 1 true 0 falsew



242
243
244
245
246
247
248
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 242

def self.eq(i)
    if i[0] == i[1]
        1
    else
        0
    end
end

.float(i) ⇒ Integer|Float

Float cast instruction

Parameters:

  • i (Array)

    input value (v)

Returns:

  • (Integer|Float)

    output value



212
213
214
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 212

def self.float(i)
    i[0].to_f
end

.floor(i) ⇒ Integer|Float

Floor instruction

Parameters:

  • i (Array)

    input value (v)

Returns:

  • (Integer|Float)

    output value



196
197
198
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 196

def self.floor(i)
    i[0].floor
end

.max(i) ⇒ Integer|Float

Maximum instruction

Parameters:

  • i (Array)

    input values

Returns:

  • (Integer|Float)

    output value



106
107
108
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 106

def self.max(i)
    i.max
end

.min(i) ⇒ Integer|Float

Minimum instruction

Parameters:

  • i (Array)

    input values

Returns:

  • (Integer|Float)

    output value



98
99
100
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 98

def self.min(i)
    i.min
end

.mix(i) ⇒ Float

Mix two values with a ratio

Parameters:

  • i (Array)

    input values (ratio (0-1.0), a, b)

Returns:

  • (Float)

    output value of ((ratio*a) + ((1.0-ratio)*b))



282
283
284
285
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 282

def self.mix(i)
    ratio, a, b = i
    (ratio*a)+((1.0-ratio)*b)
end

.mod(i) ⇒ Integer|

Calculate modulo

Parameters:

  • i (Array)

    input values

Returns:

  • (Integer|)

    output value



82
83
84
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 82

def self.mod(i)
    i[0] % i[1]
end

.mult(i) ⇒ Integer|Float

Multiply instruction

Parameters:

  • i (Array)

    input values

Returns:

  • (Integer|Float)

    output value



65
66
67
68
69
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 65

def self.mult(i)
    v=1;
    i.each{|n|v=v*n}
    v
end

.rand(i) ⇒ Float

Random number instruction

Parameters:

  • i (Array)

    input values (min, max)

Returns:

  • (Float)

    Random number between min and max



291
292
293
294
295
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 291

def self.rand(i)
    r=Random.new
    min,max = i
    (-min) + (r.rand * (max + min))
end

.round(i) ⇒ Integer|Float

Round instruction

Parameters:

  • i (Array)

    input values (val, decimal_places)

Returns:

  • (Integer|Float)

    output value



220
221
222
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 220

def self.round(i)
    i[0].round(i[1])
end

.sin(i) ⇒ Float

Sine function

Parameters:

  • i (Array)

    input value to use sin on

Returns:

  • (Float)

    Sine of input value



301
302
303
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 301

def self.sin(i)
    Math.sin(i[0])
end

.switch(i) ⇒ Integer|Float

‘Switch’ instruction (basically if)

switch condition (0/1/0.0/1.0), trueval, falseval]

Parameters:

  • i (Array)

    input value (condition, true val, false val)

Returns:

  • (Integer|Float)

    output value



230
231
232
233
234
235
236
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 230

def self.switch(i)
    if i[0].to_i == 1
        i[1]
    else
        i[2]
    end
end

.tan(i) ⇒ Float

Tangent function

Parameters:

  • i (Array)

    input value to use tan on

Returns:

  • (Float)

    Tangent of input value



317
318
319
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 317

def self.tan(i)
    Math.tan(i[0])
end