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
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
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
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
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
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
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)
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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]
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
318 319 320 |
# File 'lib/image_filter_dsl/dsl/filter_instructions.rb', line 318 def self.tan(i) Math.tan(i[0]) end |