Class: Spittle::ImageData

Inherits:
Object
  • Object
show all
Defined in:
lib/spittle/image_data.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ImageData

Returns a new instance of ImageData.



3
4
5
6
# File 'lib/spittle/image_data.rb', line 3

def initialize(options = {})
  @data = (options.delete :data) || []
  @properties = options
end

Instance Method Details

#<<(row) ⇒ Object



73
74
75
76
# File 'lib/spittle/image_data.rb', line 73

def <<(row)
  @data << row
  self
end

#==(other) ⇒ Object



94
95
96
# File 'lib/spittle/image_data.rb', line 94

def == (other)
  @data == other
end

#[](row) ⇒ Object



45
46
47
# File 'lib/spittle/image_data.rb', line 45

def [](row)
  @data[row]
end

#[]=(idx, row_data) ⇒ Object



49
50
51
# File 'lib/spittle/image_data.rb', line 49

def []=(idx, row_data)
  @data[idx] = row_data
end

#compatible?(image) ⇒ Boolean

need better checks, because currently compatible is similar color type, or depth.. maybe it doesn’t matter…

Returns:

  • (Boolean)


15
16
17
# File 'lib/spittle/image_data.rb', line 15

def compatible?(image)
  self.pixel_width == image.pixel_width
end

#each(&block) ⇒ Object



78
79
80
# File 'lib/spittle/image_data.rb', line 78

def each(&block)
  @data.each &block
end

#each_with_index(&block) ⇒ Object



82
83
84
# File 'lib/spittle/image_data.rb', line 82

def each_with_index(&block)
  @data.each_with_index(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/spittle/image_data.rb', line 57

def empty?
  @data.empty?
end

#fill_to_height(desired_height) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/spittle/image_data.rb', line 33

def fill_to_height( desired_height )
  return self if desired_height == height

  img = ImageData.new(@properties.merge(:data => @data.clone))
  empty_row = [0] * ( scanline_width )

  ( desired_height - height ).times do
    img << empty_row
  end
  img
end

#flatten!Object



86
87
88
# File 'lib/spittle/image_data.rb', line 86

def flatten!
  @data.flatten!
end

#heightObject



61
62
63
# File 'lib/spittle/image_data.rb', line 61

def height
  size
end

#lastObject



69
70
71
# File 'lib/spittle/image_data.rb', line 69

def last
  @data.last
end

#last_scanline(idx) ⇒ Object



19
20
21
22
# File 'lib/spittle/image_data.rb', line 19

def last_scanline(idx)
  last_row_index = idx - 1
  (last_row_index < 0 ? [] : @data[last_row_index])
end

#merge_left(other) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/spittle/image_data.rb', line 24

def merge_left( other )
  merged = ImageData.new(@properties.merge(:scanline_width => self.scanline_width + other.scanline_width,
                                           :name => "#{self.name}_#{other.name}"))
  other.each_with_index do |row, idx|
    merged[idx] = row + self[idx]
  end
  merged
end

#nameObject



8
# File 'lib/spittle/image_data.rb', line 8

def name; @properties[:name] || "default"; end

#pack(*args) ⇒ Object



90
91
92
# File 'lib/spittle/image_data.rb', line 90

def pack(*args)
  @data.pack(*args)
end

#pixel_widthObject



11
# File 'lib/spittle/image_data.rb', line 11

def pixel_width; @properties[:pixel_width]; end

#scanline(row) ⇒ Object



53
54
55
# File 'lib/spittle/image_data.rb', line 53

def scanline(row)
  self[row]
end

#scanline_widthObject



9
# File 'lib/spittle/image_data.rb', line 9

def scanline_width; @properties[:scanline_width]; end

#sizeObject



65
66
67
# File 'lib/spittle/image_data.rb', line 65

def size
  @data.size
end

#widthObject



10
# File 'lib/spittle/image_data.rb', line 10

def width; scanline_width / pixel_width; end