Class: Spittle::ImageData
- Inherits:
-
Object
- Object
- Spittle::ImageData
- Defined in:
- lib/spittle/image_data.rb
Constant Summary collapse
- RGB_WIDTH =
3
- RGBA_WIDTH =
4
Instance Method Summary collapse
- #<<(row) ⇒ Object
- #==(other) ⇒ Object
- #[](row) ⇒ Object
- #[]=(idx, row_data) ⇒ Object
-
#compatible?(image) ⇒ Boolean
need better checks, because currently compatible is similar color type, or depth..
- #each(&block) ⇒ Object
- #each_with_index(&block) ⇒ Object
- #empty? ⇒ Boolean
- #fill_to_height(desired_height) ⇒ Object
- #flatten! ⇒ Object
- #height ⇒ Object
-
#initialize(options = {}) ⇒ ImageData
constructor
A new instance of ImageData.
- #last ⇒ Object
- #last_scanline(idx) ⇒ Object
- #merge_left(other) ⇒ Object
- #name ⇒ Object
- #pack(*args) ⇒ Object
- #pixel_width ⇒ Object
- #scanline(row) ⇒ Object
- #scanline_width ⇒ Object
- #size ⇒ Object
- #to_rgba(alpha_value = 255) ⇒ Object
- #to_s ⇒ Object
- #width ⇒ Object
Constructor Details
#initialize(options = {}) ⇒ ImageData
Returns a new instance of ImageData.
8 9 10 11 |
# File 'lib/spittle/image_data.rb', line 8 def initialize( = {}) @data = (.delete :data) || [] @properties = end |
Instance Method Details
#<<(row) ⇒ Object
103 104 105 106 |
# File 'lib/spittle/image_data.rb', line 103 def <<(row) @data << row self end |
#==(other) ⇒ Object
124 125 126 |
# File 'lib/spittle/image_data.rb', line 124 def == (other) @data == other end |
#[](row) ⇒ Object
75 76 77 |
# File 'lib/spittle/image_data.rb', line 75 def [](row) @data[row] end |
#[]=(idx, row_data) ⇒ Object
79 80 81 |
# File 'lib/spittle/image_data.rb', line 79 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…
20 21 22 |
# File 'lib/spittle/image_data.rb', line 20 def compatible?(image) self.pixel_width == image.pixel_width end |
#each(&block) ⇒ Object
108 109 110 |
# File 'lib/spittle/image_data.rb', line 108 def each(&block) @data.each &block end |
#each_with_index(&block) ⇒ Object
112 113 114 |
# File 'lib/spittle/image_data.rb', line 112 def each_with_index(&block) @data.each_with_index(&block) end |
#empty? ⇒ Boolean
87 88 89 |
# File 'lib/spittle/image_data.rb', line 87 def empty? @data.empty? end |
#fill_to_height(desired_height) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/spittle/image_data.rb', line 42 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
116 117 118 |
# File 'lib/spittle/image_data.rb', line 116 def flatten! @data.flatten! end |
#height ⇒ Object
91 92 93 |
# File 'lib/spittle/image_data.rb', line 91 def height size end |
#last ⇒ Object
99 100 101 |
# File 'lib/spittle/image_data.rb', line 99 def last @data.last end |
#last_scanline(idx) ⇒ Object
28 29 30 31 |
# File 'lib/spittle/image_data.rb', line 28 def last_scanline(idx) last_row_index = idx - 1 (last_row_index < 0 ? [] : @data[last_row_index]) end |
#merge_left(other) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/spittle/image_data.rb', line 33 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 |
#name ⇒ Object
13 |
# File 'lib/spittle/image_data.rb', line 13 def name; @properties[:name] || "default"; end |
#pack(*args) ⇒ Object
120 121 122 |
# File 'lib/spittle/image_data.rb', line 120 def pack(*args) @data.pack(*args) end |
#pixel_width ⇒ Object
16 |
# File 'lib/spittle/image_data.rb', line 16 def pixel_width; @properties[:pixel_width]; end |
#scanline(row) ⇒ Object
83 84 85 |
# File 'lib/spittle/image_data.rb', line 83 def scanline(row) self[row] end |
#scanline_width ⇒ Object
14 |
# File 'lib/spittle/image_data.rb', line 14 def scanline_width; @properties[:scanline_width]; end |
#size ⇒ Object
95 96 97 |
# File 'lib/spittle/image_data.rb', line 95 def size @data.size end |
#to_rgba(alpha_value = 255) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/spittle/image_data.rb', line 54 def to_rgba( alpha_value=255 ) # test that conversion updates pixel width, scanline width return self if pixel_width == RGBA_WIDTH # so ruby 1.9 has different ideas then 1.8 on how Enumerable should work # 1.9 returns Enumerable object if no block given # 1.8 complains if no block, unless using enum_x methods slice_method = (RUBY_VERSION.include?( "1.9" )) ? :each_slice : :enum_slice rgba_data = @data.map do |row| pixels = row.send( slice_method, RGB_WIDTH ) pixels.inject([]){|result, pixel| result + pixel + [alpha_value] } end ImageData.new( :data => rgba_data, :pixel_width => 4, :scanline_width => (@properties[:scanline_width] / RGB_WIDTH) * RGBA_WIDTH, :name => name) end |
#to_s ⇒ Object
24 25 26 |
# File 'lib/spittle/image_data.rb', line 24 def to_s "#{name} pixel width: #{pixel_width}" end |
#width ⇒ Object
15 |
# File 'lib/spittle/image_data.rb', line 15 def width; scanline_width / pixel_width; end |