Class: ZPNG::Block

Inherits:
Object
  • Object
show all
Defined in:
lib/zpng/block.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#heightObject

Returns the value of attribute height.



3
4
5
# File 'lib/zpng/block.rb', line 3

def height
  @height
end

#pixelsObject

Returns the value of attribute pixels.



3
4
5
# File 'lib/zpng/block.rb', line 3

def pixels
  @pixels
end

#widthObject

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_sObject



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