Class: CooCoo::Image::Base

Inherits:
Object show all
Defined in:
lib/coo-coo/image.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height, bpp = 1, pixels = nil, background = nil, repeat_x = false, repeat_y = false) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
16
17
18
# File 'lib/coo-coo/image.rb', line 9

def initialize(width, height, bpp = 1, pixels = nil, background = nil, repeat_x = false, repeat_y = false)
  @width = width
  @height = height
  @bpp = bpp
  @span = @width * @bpp
  @background = background || bpp.times.collect { 0 }
  @repeat_x = repeat_x
  @repeat_y = repeat_y
  @pixels = pixels || Array.new(@width * @height * @bpp, 0)
end

Instance Attribute Details

#backgroundObject (readonly)

Returns the value of attribute background.



6
7
8
# File 'lib/coo-coo/image.rb', line 6

def background
  @background
end

#bppObject (readonly)

Returns the value of attribute bpp.



6
7
8
# File 'lib/coo-coo/image.rb', line 6

def bpp
  @bpp
end

#heightObject (readonly)

Returns the value of attribute height.



6
7
8
# File 'lib/coo-coo/image.rb', line 6

def height
  @height
end

#repeat_xObject

Returns the value of attribute repeat_x.



7
8
9
# File 'lib/coo-coo/image.rb', line 7

def repeat_x
  @repeat_x
end

#repeat_yObject

Returns the value of attribute repeat_y.



7
8
9
# File 'lib/coo-coo/image.rb', line 7

def repeat_y
  @repeat_y
end

#widthObject (readonly)

Returns the value of attribute width.



6
7
8
# File 'lib/coo-coo/image.rb', line 6

def width
  @width
end

Instance Method Details

#*(transform) ⇒ Object



54
55
56
# File 'lib/coo-coo/image.rb', line 54

def *(transform)
  TransformedImage.new(self, transform)
end

#[](x, y, byte = nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/coo-coo/image.rb', line 20

def [](x, y, byte = nil)
  if (@repeat_x == false && (x < 0 || x >= width)) ||
      (@repeat_y == false && (y < 0 || y >= height))
    p = @background
    if byte
      p[byte]
    else
      p
    end
  else
    i = pixel_index(x, y, byte || 0)
    if byte
      @pixels[i] || @background[byte]
    else
      p = @pixels[i, @bpp]
      if p && !p.empty?
        p
      else
        @background
      end
    end
  end
end

#[]=(x, y, v) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/coo-coo/image.rb', line 44

def []=(x, y, v)
  @bpp.times do |byte|
    c = v
    if v.respond_to?(:[])
      c = v[byte]
    end
    @pixels[*pixel_index(x, y, byte)] = c
  end
end

#filter(f) ⇒ Object



58
59
60
# File 'lib/coo-coo/image.rb', line 58

def filter(f)
  TransformedImage.new(self, nil, f)
end

#to_aObject



62
63
64
65
66
67
68
69
70
# File 'lib/coo-coo/image.rb', line 62

def to_a
  @pixels.each_slice(@span).collect do |row|
    if @bpp > 1
      row.each_slice(@bpp).to_a
    else
      row.to_a
    end
  end
end