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
-
.above(i) ⇒ Integer|Float
Above instruction.
-
.abs(i) ⇒ Integer|Float
Absolute value.
-
.add(i) ⇒ Integer|Float
Add instruction.
-
.avg(i) ⇒ Integer|Float
Average instruction.
-
.band(i) ⇒ Integer
Logical AND instruction.
-
.below(i) ⇒ Integer|Float
Below instruction.
-
.between(i) ⇒ Integer
Between instruction (check if value is between two others).
-
.bnot(i) ⇒ Integer|Float
Logic invert instruction.
-
.bor(i) ⇒ Integer
Logical OR instruction.
-
.ceil(i) ⇒ Integer|Float
Ceil instruction.
-
.clamp(i) ⇒ Integer|Float
Clamp value between a min and a max.
-
.copy(i) ⇒ Integer|Float
Copy instruction.
-
.cos(i) ⇒ Float
Cosine function.
-
.div(i) ⇒ Integer|Float
Multiply instruction.
-
.eq(i) ⇒ Integer|Float
Equal condition instruction.
-
.float(i) ⇒ Integer|Float
Float cast instruction.
-
.floor(i) ⇒ Integer|Float
Floor instruction.
-
.max(i) ⇒ Integer|Float
Maximum instruction.
-
.min(i) ⇒ Integer|Float
Minimum instruction.
-
.mix(i) ⇒ Float
Mix two values with a ratio.
-
.mod(i) ⇒ Integer|
Calculate modulo.
-
.mult(i) ⇒ Integer|Float
Multiply instruction.
-
.rand(i) ⇒ Float
Random number instruction.
-
.round(i) ⇒ Integer|Float
Round instruction.
-
.sin(i) ⇒ Float
Sine function.
-
.switch(i) ⇒ Integer|Float
‘Switch’ instruction (basically if).
-
.tan(i) ⇒ Float
Tangent function.
Class Method Details
.above(i) ⇒ Integer|Float
Above instruction
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
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
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
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
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
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)
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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]
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
317 318 319 |
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 317 def self.tan(i) Math.tan(i[0]) end |