Class: Pxlsrt::Seed
- Inherits:
-
Object
- Object
- Pxlsrt::Seed
- Defined in:
- lib/pxlsrt/seed.rb
Overview
Plant seeds, have them spiral out and sort.
Class Method Summary collapse
-
.seed(input, o = {}) ⇒ Object
The main attraction of the Seed class.
-
.suite(inputFileName, outputFileName, o = {}) ⇒ Object
Uses Pxlsrt::Seed.seed to input and output from one method.
Class Method Details
.seed(input, o = {}) ⇒ Object
The main attraction of the Seed class. Returns a ChunkyPNG::Image that is sorted according to the options provided. Will raise any error that occurs.
17 18 19 20 21 22 23 24 25 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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 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 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/pxlsrt/seed.rb', line 17 def self.seed(input, o = {}) startTime = Time.now defOptions = { :reverse => false, :smooth => false, :method => "sum-rgb", :verbose => false, :trusted => false, :middle => false, :random => false, :distance => 100, :threshold => 0.1 } defRules = { :reverse => :anything, :smooth => [false, true], :method => Pxlsrt::Colors::METHODS, :verbose => [false, true], :trusted => [false, true], :middle => :anything, :random => [false, {:class => [Fixnum]}], :distance => [false, {:class => [Fixnum]}], :threshold => [{:class => [Float, Fixnum]}] } = defOptions.merge(o) if o.length == 0 or [:trusted] == true or ([:trusted] == false and o.length != 0 and Pxlsrt::Helpers.checkOptions(, defRules) != false) if input.class == String Pxlsrt::Helpers.verbose("Getting image from file...") if [:verbose] if File.file?(input) if Pxlsrt::Colors.isPNG?(input) input = ChunkyPNG::Image.from_file(input) else Pxlsrt::Helpers.error("File #{input} is not a valid PNG.") if [:verbose] raise "Invalid PNG" end else Pxlsrt::Helpers.error("File #{input} doesn't exist!") if [:verbose] raise "File doesn't exit" end elsif input.class != String and input.class != ChunkyPNG::Image Pxlsrt::Helpers.error("Input is not a filename or ChunkyPNG::Image") if [:verbose] raise "Invalid input (must be filename or ChunkyPNG::Image)" end Pxlsrt::Helpers.verbose("Seed mode.") if [:verbose] Pxlsrt::Helpers.verbose("Creating Pxlsrt::Image object") if [:verbose] png = Pxlsrt::Image.new(input) traversed = [false] * (png.getWidth * png.getHeight) count = 0 seeds = [] if [:random] != false Pxlsrt::Helpers.progress("Planting seeds", 0, [:random]) if [:verbose] for s in (0...[:random]) x = (0...png.getWidth).to_a.sample y = (0...png.getHeight).to_a.sample seeds.push({ :spiral => Pxlsrt::Spiral.new(x, y), :pixels => [png[x, y]], :xy => [{:x => x, :y=>y}], :placed => true, :retired => false, :anchor => { :x => x, :y => y } }) Pxlsrt::Helpers.progress("Planting seeds", s + 1, [:random]) if [:verbose] end else Pxlsrt::Helpers.progress("Planting seeds", 0, png.getWidth * png.getHeight) if [:verbose] kernel = [[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]] i = (png.getWidth + png.getHeight - 2) * 2 for y in (1...(png.getHeight - 1)) for x in (1...(png.getWidth - 1)) sum = 0 for ky in ((-1)..1) for kx in ((-1)..1) sum += kernel[ky + 1][kx + 1] * (ChunkyPNG::Color.r(png[x + kx, y + ky])) end end if sum < [:threshold] seeds.push({ :spiral => Pxlsrt::Spiral.new(x, y), :pixels => [png[x, y]], :xy => [{:x => x, :y=>y}], :placed => true, :retired => false, :anchor => { :x => x, :y => y } }) end i += 1 Pxlsrt::Helpers.progress("Planting seeds", i, png.getWidth * png.getHeight) if [:verbose] end end if [:distance] != false Pxlsrt::Helpers.progress("Removing seed clusters", 0, seeds.length) if [:verbose] results = [] i = 0 for current in seeds add = true for other in results d = Math.sqrt((current[:anchor][:x] - other[:anchor][:x]) ** 2 + (current[:anchor][:y] - other[:anchor][:y]) ** 2) add = false if d > 0 and d < [:distance] end if add results.push(current) end i += 1 Pxlsrt::Helpers.progress("Removing seed clusters", i, seeds.length) if [:verbose] end seeds = results end end for r in (0...seeds.length) traversed[seeds[r][:anchor][:x] + seeds[r][:anchor][:y] * png.getWidth] = r count += 1 end Pxlsrt::Helpers.verbose("Planted #{seeds.length} seeds") if [:verbose] step = 0 Pxlsrt::Helpers.progress("Watch them grow!", count, traversed.length) if [:verbose] while count < traversed.length and seeds.length != 0 r = 0 retired = [] for seed in seeds if !seed[:retired] n = seed[:spiral].next if n[:x] >= 0 and n[:y] >= 0 and n[:x] < png.getWidth and n[:y] < png.getHeight and !traversed[n[:x] + n[:y] * png.getWidth] seed[:pixels].push(png[n[:x], n[:y]]) traversed[n[:x] + n[:y] * png.getWidth] = r seed[:xy].push(n) seed[:placed] = true count += 1 else if seed[:placed] == true seed[:placed] = { :count => 1, :direction => seed[:spiral].direction, :cycle => seed[:spiral].cycles } case seed[:placed][:direction] when "up", "down" seed[:placed][:value] = seed[:spiral].pos[:y] seed[:placed][:valueS] = :y when "left", "right" seed[:placed][:value] = seed[:spiral].pos[:x] seed[:placed][:valueS] = :x end else seed[:placed][:count] += 1 if seed[:spiral].cycles != seed[:placed][:cycle] and seed[:placed][:direction] == seed[:spiral].direction and seed[:placed][:value] == seed[:spiral].pos[seed[:placed][:valueS]] seed[:retired] = true end end end end r += 1 end step += 1 Pxlsrt::Helpers.progress("Watch them grow!", count, traversed.length) if [:verbose] end Pxlsrt::Helpers.progress("Sort seeds and place pixels", 0, seeds.length) if [:verbose] r = 0 for seed in seeds spiral = Pxlsrt::Spiral.new(seed[:anchor][:x], seed[:anchor][:y]) band = Pxlsrt::Helpers.handlePixelSort(seed[:pixels], ) i = 0 for k in seed[:xy] png[k[:x], k[:y]] = band[i] i += 1 end r += 1 Pxlsrt::Helpers.progress("Sort seeds and place pixels", r, seeds.length) if [:verbose] end endTime=Time.now timeElapsed=endTime-startTime if timeElapsed < 60 Pxlsrt::Helpers.verbose("Took #{timeElapsed.round(4)} second#{ timeElapsed!=1.0 ? "s" : "" }.") if [:verbose] else minutes=(timeElapsed/60).floor seconds=(timeElapsed % 60).round(4) Pxlsrt::Helpers.verbose("Took #{minutes} minute#{ minutes!=1 ? "s" : "" } and #{seconds} second#{ seconds!=1.0 ? "s" : "" }.") if [:verbose] end Pxlsrt::Helpers.verbose("Returning ChunkyPNG::Image...") if [:verbose] return png.returnModified else Pxlsrt::Helpers.error("Options specified do not follow the correct format.") if [:verbose] raise "Bad options" end end |
.suite(inputFileName, outputFileName, o = {}) ⇒ Object
Uses Pxlsrt::Seed.seed to input and output from one method.
9 10 11 12 13 14 |
# File 'lib/pxlsrt/seed.rb', line 9 def self.suite(inputFileName, outputFileName, o={}) kml = Pxlsrt::Seed.seed(inputFileName, o) if Pxlsrt::Helpers.contented(kml) kml.save(outputFileName) end end |