Class: ChunkyPNG::PixelMatrix

Inherits:
Object
  • Object
show all
Extended by:
Decoding
Includes:
Encoding
Defined in:
lib/chunky_png/pixel_matrix.rb,
lib/chunky_png/pixel_matrix/decoding.rb,
lib/chunky_png/pixel_matrix/encoding.rb

Overview

The ChunkPNG::PixelMatrix class represents a matrix of pixels of which an i mage consists. This class supports loading a PixelMatrix from a PNG datastream, and creating a PNG datastream bse don this matrix.

This class offers per-pixel access to the matrix by using x,y coordinates. It uses a palette (see Palette) to keep track of the different colors used in this matrix.

See Also:

Defined Under Namespace

Modules: Decoding, Encoding

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Decoding

decode, decode_pixelstream

Methods included from Encoding

#encode

Constructor Details

#initialize(width, height, initial = ChunkyPNG::Pixel::TRANSPARENT) ⇒ PixelMatrix

Initializes a new PixelMatrix instance

Parameters:

  • width (Integer)

    The width in pixels of this matrix

  • width (Integer)

    The height in pixels of this matrix

  • initial (ChunkyPNG::Pixel, Array<ChunkyPNG::Pixel>) (defaults to: ChunkyPNG::Pixel::TRANSPARENT)

    The initial value of te pixels:

    • If a color is passed to this parameter, this color will be used as background color.

    • If an array of pixels is provided, these pixels will be used as initial value. Note that the amount of pixels in this array should equal width * height.



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/chunky_png/pixel_matrix.rb', line 36

def initialize(width, height, initial = ChunkyPNG::Pixel::TRANSPARENT)
  
  @width, @height = width, height
  
  if initial.kind_of?(ChunkyPNG::Pixel)
    @pixels = Array.new(width * height, initial)
  elsif initial.kind_of?(Array) && initial.size == width * height
    @pixels = initial
  else 
    raise "Cannot use this value as initial pixel matrix: #{initial.inspect}!"
  end
end

Instance Attribute Details

#heightInteger (readonly)

Returns The number of rows in this pixel matrix.

Returns:

  • (Integer)

    The number of rows in this pixel matrix



21
22
23
# File 'lib/chunky_png/pixel_matrix.rb', line 21

def height
  @height
end

#pixelsArray<ChunkyPNG::Pixel> (readonly)

Returns The list of pixels in this matrix. This array always should have width * height elements.

Returns:

  • (Array<ChunkyPNG::Pixel>)

    The list of pixels in this matrix. This array always should have width * height elements.



25
26
27
# File 'lib/chunky_png/pixel_matrix.rb', line 25

def pixels
  @pixels
end

#widthInteger (readonly)

Returns The number of columns in this pixel matrix.

Returns:

  • (Integer)

    The number of columns in this pixel matrix



18
19
20
# File 'lib/chunky_png/pixel_matrix.rb', line 18

def width
  @width
end

Instance Method Details

#[](x, y) ⇒ ChunkyPNG::Pixel

Returns a single pixel from this matrix.

Parameters:

  • x (Integer)

    The x-coordinate of the pixel (column)

  • y (Integer)

    The y-coordinate of the pixel (row)

Returns:



61
62
63
# File 'lib/chunky_png/pixel_matrix.rb', line 61

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

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

Replaces a single pixel in this matrix.

Parameters:

  • x (Integer)

    The x-coordinate of the pixel (column)

  • y (Integer)

    The y-coordinate of the pixel (row)

  • pixel (ChunkyPNG::Pixel)

    The new pixel for the provided coordinates.



53
54
55
# File 'lib/chunky_png/pixel_matrix.rb', line 53

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

#each_scanline {|Array<ChunkyPNG::Pixel>| ... } ⇒ Object

Passes to this matrix of pixels line by line.

Yields:



67
68
69
70
71
72
# File 'lib/chunky_png/pixel_matrix.rb', line 67

def each_scanline(&block)
  height.times do |i|
    scanline = @pixels[width * i, width]
    yield(scanline)
  end
end

#eql?(other) ⇒ true, false Also known as: ==

Equality check to compare this pixel matrix with other matrices.

Parameters:

  • other

    The object to compare this Matrix to.

Returns:

  • (true, false)

    True if the size and pixel values of the other matrix are exactly the same as this matrix size and pixel values.



98
99
100
101
# File 'lib/chunky_png/pixel_matrix.rb', line 98

def eql?(other)
  other.kind_of?(self.class) && other.pixels == self.pixels &&
        other.width == self.width && other.height == self.height
end

#paletteChunkyPNG::Palette

Returns the palette used for this pixel matrix.

Returns:

  • (ChunkyPNG::Palette)

    A pallete which contains all the colors that are being used for this image.



77
78
79
# File 'lib/chunky_png/pixel_matrix.rb', line 77

def palette
  ChunkyPNG::Palette.from_pixels(@pixels)
end

#to_datastream(constraints = {}) ⇒ Object

Converts this PixelMatrix to a datastream, so that it can be saved as a PNG image.

Parameters:

  • constraints (Hash) (defaults to: {})

    The constraints to use when encoding the matrix.



83
84
85
86
87
88
89
90
91
92
# File 'lib/chunky_png/pixel_matrix.rb', line 83

def to_datastream(constraints = {})
  data = encode(constraints)
  ds = Datastream.new
  ds.header_chunk       = Chunk::Header.new(data[:header])
  ds.palette_chunk      = data[:palette_chunk]      if data[:palette_chunk]
  ds.transparency_chunk = data[:transparency_chunk] if data[:transparency_chunk]
  ds.data_chunks        = ds.idat_chunks(data[:pixelstream])
  ds.end_chunk          = Chunk::End.new
  return ds
end