Class: ZPNG::Block
- Inherits:
-
Object
- Object
- ZPNG::Block
- Defined in:
- lib/zpng/block.rb
Instance Attribute Summary collapse
-
#height ⇒ Object
Returns the value of attribute height.
-
#pixels ⇒ Object
Returns the value of attribute pixels.
-
#width ⇒ Object
Returns the value of attribute width.
Instance Method Summary collapse
-
#initialize(image, x, y, w, h) ⇒ Block
constructor
A new instance of Block.
- #to_binary_string(c_white = ' ', c_black = 'X') ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(image, x, y, w, h) ⇒ Block
Returns a new instance of Block.
4 5 6 7 8 9 10 11 12 |
# File 'lib/zpng/block.rb', line 4 def initialize image, x, y, w, h @width, @height = w,h @pixels = [] h.times do |i| w.times do |j| @pixels << image[x+j,y+i] end end end |
Instance Attribute Details
#height ⇒ Object
Returns the value of attribute height.
3 4 5 |
# File 'lib/zpng/block.rb', line 3 def height @height end |
#pixels ⇒ Object
Returns the value of attribute pixels.
3 4 5 |
# File 'lib/zpng/block.rb', line 3 def pixels @pixels end |
#width ⇒ Object
Returns the value of attribute width.
3 4 5 |
# File 'lib/zpng/block.rb', line 3 def width @width end |
Instance Method Details
#to_binary_string(c_white = ' ', c_black = 'X') ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/zpng/block.rb', line 26 def to_binary_string c_white = ' ', c_black = 'X' @pixels.each do |p| raise "pixel #{p.inspect} is not white nor black" if !p.white? && !p.black? end a = [] height.times do |i| b = [] width.times do |j| b << (pixels[i*width+j].black? ? c_black : c_white) end a << b.join(" ") end a.join "\n" end |
#to_s ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/zpng/block.rb', line 14 def to_s a = [] height.times do |i| b = [] width.times do |j| b << pixels[i*width+j].to_s end a << b.join(" ") end a.join "\n" end |