Class: Pxlsrt::Kim
- Inherits:
-
Object
- Object
- Pxlsrt::Kim
- Defined in:
- lib/pxlsrt/kim.rb
Overview
Uses Kim Asendorf’s pixel sorting algorithm, orginally written in Processing. github.com/kimasendorf/ASDFPixelSort
Class Method Summary collapse
-
.getFirstBrightX(img, x, y, brightnessValue) ⇒ Object
Brightness.
- .getFirstBrightY(img, x, y, brightnessValue) ⇒ Object
-
.getFirstNotBlackX(img, x, y, blackValue) ⇒ Object
Helper methods Black.
- .getFirstNotBlackY(img, x, y, blackValue) ⇒ Object
-
.getFirstNotWhiteX(img, x, y, whiteValue) ⇒ Object
White.
- .getFirstNotWhiteY(img, x, y, whiteValue) ⇒ Object
- .getNextBlackX(img, x, y, blackValue) ⇒ Object
- .getNextBlackY(img, x, y, blackValue) ⇒ Object
- .getNextDarkX(img, x, y, brightnessValue) ⇒ Object
- .getNextDarkY(img, x, y, brightnessValue) ⇒ Object
- .getNextWhiteX(img, x, y, whiteValue) ⇒ Object
- .getNextWhiteY(img, x, y, whiteValue) ⇒ Object
-
.kim(input, o = {}) ⇒ Object
The main attraction of the Kim class.
-
.suite(inputFileName, outputFileName, o = {}) ⇒ Object
Uses Pxlsrt::Kim.kim to input and output from one method.
Class Method Details
.getFirstBrightX(img, x, y, brightnessValue) ⇒ Object
Brightness
185 186 187 188 189 190 191 192 193 |
# File 'lib/pxlsrt/kim.rb', line 185 def self.getFirstBrightX(img, x, y, brightnessValue) if x < img.getWidth while ChunkyPNG::Color.to_hsb(img[x, y])[2] * 255 < brightnessValue x += 1; return -1 if x >= img.getWidth end end return x end |
.getFirstBrightY(img, x, y, brightnessValue) ⇒ Object
194 195 196 197 198 199 200 201 202 |
# File 'lib/pxlsrt/kim.rb', line 194 def self.getFirstBrightY(img, x, y, brightnessValue) if y < img.getHeight while ChunkyPNG::Color.to_hsb(img[x, y])[2] * 255 < brightnessValue y += 1; return -1 if y >= img.getHeight end end return y end |
.getFirstNotBlackX(img, x, y, blackValue) ⇒ Object
Helper methods Black
146 147 148 149 150 151 152 153 154 |
# File 'lib/pxlsrt/kim.rb', line 146 def self.getFirstNotBlackX(img, x, y, blackValue) if x < img.getWidth while img[x, y] < blackValue x += 1 return -1 if x >= img.getWidth end end return x end |
.getFirstNotBlackY(img, x, y, blackValue) ⇒ Object
155 156 157 158 159 160 161 162 163 |
# File 'lib/pxlsrt/kim.rb', line 155 def self.getFirstNotBlackY(img, x, y, blackValue) if y < img.getHeight while img[x, y] < blackValue y += 1 return -1 if y >= img.getHeight end end return y end |
.getFirstNotWhiteX(img, x, y, whiteValue) ⇒ Object
White
224 225 226 227 228 229 230 231 232 |
# File 'lib/pxlsrt/kim.rb', line 224 def self.getFirstNotWhiteX(img, x, y, whiteValue) if x < img.getWidth while img[x, y] > whiteValue x += 1 return -1 if x >= img.getWidth end end return x end |
.getFirstNotWhiteY(img, x, y, whiteValue) ⇒ Object
233 234 235 236 237 238 239 240 241 |
# File 'lib/pxlsrt/kim.rb', line 233 def self.getFirstNotWhiteY(img, x, y, whiteValue) if y < img.getHeight while img[x, y] > whiteValue y += 1 return -1 if y >= img.getHeight end end return y end |
.getNextBlackX(img, x, y, blackValue) ⇒ Object
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/pxlsrt/kim.rb', line 164 def self.getNextBlackX(img, x, y, blackValue) x += 1 if x < img.getWidth while img[x, y] > blackValue x += 1 return (img.getWidth - 1) if x >= img.getWidth end end return x - 1 end |
.getNextBlackY(img, x, y, blackValue) ⇒ Object
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/pxlsrt/kim.rb', line 174 def self.getNextBlackY(img, x, y, blackValue) y += 1 if y < img.getHeight while img[x, y] > blackValue y += 1 return (img.getHeight - 1) if y >= img.getHeight end end return y - 1 end |
.getNextDarkX(img, x, y, brightnessValue) ⇒ Object
203 204 205 206 207 208 209 210 211 212 |
# File 'lib/pxlsrt/kim.rb', line 203 def self.getNextDarkX(img, x, y, brightnessValue) x += 1 if x < img.getWidth while ChunkyPNG::Color.to_hsb(img[x, y])[2] * 255 > brightnessValue x += 1 return (img.getWidth - 1) if x >= img.getWidth end end return x - 1 end |
.getNextDarkY(img, x, y, brightnessValue) ⇒ Object
213 214 215 216 217 218 219 220 221 222 |
# File 'lib/pxlsrt/kim.rb', line 213 def self.getNextDarkY(img, x, y, brightnessValue) y += 1 if y < img.getHeight while ChunkyPNG::Color.to_hsb(img[x, y])[2] * 255 > brightnessValue y += 1 return (img.getHeight - 1) if y >= img.getHeight end end return y - 1 end |
.getNextWhiteX(img, x, y, whiteValue) ⇒ Object
242 243 244 245 246 247 248 249 250 251 |
# File 'lib/pxlsrt/kim.rb', line 242 def self.getNextWhiteX(img, x, y, whiteValue) x += 1 if x < img.getWidth while img[x, y] < whiteValue x += 1 return (img.getWidth - 1) if x >= img.getWidth end end return x - 1 end |
.getNextWhiteY(img, x, y, whiteValue) ⇒ Object
252 253 254 255 256 257 258 259 260 261 |
# File 'lib/pxlsrt/kim.rb', line 252 def self.getNextWhiteY(img, x, y, whiteValue) y += 1 if y < img.getHeight while img[x, y] < whiteValue y += 1 return (img.getHeight - 1) if y >= img.getHeight end end return y - 1 end |
.kim(input, o = {}) ⇒ Object
The main attraction of the Kim 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 |
# File 'lib/pxlsrt/kim.rb', line 17 def self.kim(input, o = {}) startTime = Time.now defOptions={ :method => "brightness", :verbose => false, :value => false, :trusted => false } defRules = { :method => ["brightness", "black", "white"], :verbose => [false, true], :value => [false, {:class => [Fixnum]}], :trusted => [false, true] } = 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("Kim Asendorf mode.") if [:verbose] Pxlsrt::Helpers.verbose("Creating Pxlsrt::Image object") if [:verbose] png = Pxlsrt::Image.new(input) column = 0 row = 0 [:value] ||= ChunkyPNG::Color.rgba(11, 220, 0, 1) if [:method] == "black" [:value] ||= 60 if [:method] == "brightness" [:value] ||= ChunkyPNG::Color.rgba(57, 167, 192, 1) if [:method] == "white" Pxlsrt::Helpers.progress("Sorting columns", column, png.getWidth) if [:verbose] while column < png.getWidth x = column y = 0 yend = 0 while yend < png.getHeight case [:method] when "black" y = self.getFirstNotBlackY(png, x, y, [:value]) yend = self.getNextBlackY(png, x, y, [:value]) when "brightness" y = self.getFirstBrightY(png, x, y, [:value]) yend = self.getNextDarkY(png, x, y, [:value]) when "white" y = self.getFirstNotWhiteY(png, x, y, [:value]) yend = self.getNextWhiteY(png, x, y, [:value]) end if y < 0 break end sortLength = yend - y; unsorted = [] sorted = [] for i in (0...sortLength) unsorted[i] = png[x, y + i]; end sorted = unsorted.sort for i in (0...sortLength) png[x, y + i] = sorted[i]; end y = yend + 1; end column += 1 Pxlsrt::Helpers.progress("Sorting columns", column, png.getWidth) if [:verbose] end Pxlsrt::Helpers.progress("Sorting rows", row, png.getHeight) if [:verbose] while row < png.getHeight x = 0 y = row xend = 0 while xend < png.getWidth case [:method] when "black" x = self.getFirstNotBlackX(png, x, y, [:value]) xend = self.getNextBlackX(png, x, y, [:value]) when "brightness" x = self.getFirstBrightX(png, x, y, [:value]) xend = self.getNextDarkX(png, x, y, [:value]) when "white" x = self.getFirstNotWhiteX(png, x, y, [:value]) xend = self.getNextWhiteX(png, x, y, [:value]) end if x < 0 break end sortLength = xend - x unsorted = [] sorted = [] for i in (0...sortLength) unsorted[i] = png[x + i, y] end sorted = unsorted.sort for i in (0...sortLength) png[x + i, y] = sorted[i]; end x = xend + 1; end row += 1 Pxlsrt::Helpers.progress("Sorting rows", row, png.getHeight) 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::Kim.kim to input and output from one method.
9 10 11 12 13 14 |
# File 'lib/pxlsrt/kim.rb', line 9 def self.suite(inputFileName, outputFileName, o={}) kml=Pxlsrt::Kim.kim(inputFileName, o) if Pxlsrt::Helpers.contented(kml) kml.save(outputFileName) end end |