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



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

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



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

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

.add(i) ⇒ Integer|Float

Add instruction

Parameters:

  • i (Array)

    input values

Returns:

  • (Integer|Float)

    output value



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

def self.add(i) 
    i.sum
end

.avg(i) ⇒ Integer|Float

Average instruction

Parameters:

  • i (Array)

    input values

Returns:

  • (Integer|Float)

    output value



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

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



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

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



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

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



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

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)



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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

.max(i) ⇒ Integer|Float

Maximum instruction

Parameters:

  • i (Array)

    input values

Returns:

  • (Integer|Float)

    output value



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

def self.max(i)
    i.max
end

.min(i) ⇒ Integer|Float

Minimum instruction

Parameters:

  • i (Array)

    input values

Returns:

  • (Integer|Float)

    output value



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

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))



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

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



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

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



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

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



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

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



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

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



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

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



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

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



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

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