Class: PureJPEG::Source::RawSource

Inherits:
Object
  • Object
show all
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.

Examples:

From a block

source = RawSource.new(256, 256) { |x, y| [x, y, 128] }

Setting pixels individually

source = RawSource.new(256, 256)
source.set(0, 0, 255, 0, 0)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(width, height) {|x, y| ... } ⇒ RawSource

Returns a new instance of RawSource.

Parameters:

  • width (Integer)

    image width

  • height (Integer)

    image height

Yield Parameters:

  • x (Integer)

    column

  • y (Integer)

    row

Yield Returns:

  • (Array<Integer>)

    [r, g, b] values, each 0-255



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

#heightInteger (readonly)

Returns image height in pixels.

Returns:

  • (Integer)

    image height in pixels



20
21
22
# File 'lib/pure_jpeg/source/raw_source.rb', line 20

def height
  @height
end

#widthInteger (readonly)

Returns image width in pixels.

Returns:

  • (Integer)

    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.

Parameters:

  • x (Integer)

    column (0-based)

  • y (Integer)

    row (0-based)

Returns:

  • (Pixel, nil)

    the pixel, or nil if not yet set



59
60
61
# File 'lib/pure_jpeg/source/raw_source.rb', line 59

def [](x, y)
  @pixels[y * @width + x]
end

#set(x, y, r, g, b) ⇒ Pixel

Set a pixel at the given coordinate.

Parameters:

  • x (Integer)

    column (0-based)

  • y (Integer)

    row (0-based)

  • r (Integer)

    red (0-255)

  • g (Integer)

    green (0-255)

  • b (Integer)

    blue (0-255)

Returns:



50
51
52
# File 'lib/pure_jpeg/source/raw_source.rb', line 50

def set(x, y, r, g, b)
  @pixels[y * @width + x] = Pixel.new(r, g, b)
end