Class: PureJPEG::Source::RawSource
- Inherits:
-
Object
- Object
- PureJPEG::Source::RawSource
- Defined in:
- lib/pure_jpeg/source/raw_source.rb
Overview
An in-memory pixel source backed by a flat array.
Pixels can be populated via a block at construction time or set individually with #set.
Instance Attribute Summary collapse
-
#height ⇒ Integer
readonly
Image height in pixels.
-
#width ⇒ Integer
readonly
Image width in pixels.
Instance Method Summary collapse
-
#[](x, y) ⇒ Pixel?
Retrieve a pixel at the given coordinate.
-
#initialize(width, height) {|x, y| ... } ⇒ RawSource
constructor
A new instance of RawSource.
-
#set(x, y, r, g, b) ⇒ Pixel
Set a pixel at the given coordinate.
Constructor Details
#initialize(width, height) {|x, y| ... } ⇒ RawSource
Returns a new instance of RawSource.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/pure_jpeg/source/raw_source.rb', line 27 def initialize(width, height, &block) @width = width @height = height @pixels = Array.new(width * height) { Pixel.new(0, 0, 0) } if block height.times do |y| width.times do |x| r, g, b = block.call(x, y) @pixels[y * width + x] = Pixel.new(r, g, b) end end end end |
Instance Attribute Details
#height ⇒ Integer (readonly)
Returns image height in pixels.
20 21 22 |
# File 'lib/pure_jpeg/source/raw_source.rb', line 20 def height @height end |
#width ⇒ Integer (readonly)
Returns image width in pixels.
18 19 20 |
# File 'lib/pure_jpeg/source/raw_source.rb', line 18 def width @width end |
Instance Method Details
#[](x, y) ⇒ Pixel?
Retrieve a pixel at the given coordinate.
59 60 61 |
# File 'lib/pure_jpeg/source/raw_source.rb', line 59 def [](x, y) @pixels[y * @width + x] end |