Class: ChunkyPNG::PixelMatrix
- Inherits:
-
Object
- Object
- ChunkyPNG::PixelMatrix
- 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.
Defined Under Namespace
Instance Attribute Summary collapse
-
#height ⇒ Integer
readonly
The number of rows in this pixel matrix.
-
#pixels ⇒ Array<ChunkyPNG::Pixel>
readonly
The list of pixels in this matrix.
-
#width ⇒ Integer
readonly
The number of columns in this pixel matrix.
Instance Method Summary collapse
-
#[](x, y) ⇒ ChunkyPNG::Pixel
Returns a single pixel from this matrix.
-
#[]=(x, y, pixel) ⇒ Object
Replaces a single pixel in this matrix.
-
#each_scanline {|Array<ChunkyPNG::Pixel>| ... } ⇒ Object
Passes to this matrix of pixels line by line.
-
#eql?(other) ⇒ true, false
(also: #==)
Equality check to compare this pixel matrix with other matrices.
-
#initialize(width, height, initial = ChunkyPNG::Pixel::TRANSPARENT) ⇒ PixelMatrix
constructor
Initializes a new PixelMatrix instance.
-
#palette ⇒ ChunkyPNG::Palette
Returns the palette used for this pixel matrix.
-
#to_datastream(constraints = {}) ⇒ Object
Converts this PixelMatrix to a datastream, so that it can be saved as a PNG image.
Methods included from Decoding
Methods included from Encoding
Constructor Details
#initialize(width, height, initial = ChunkyPNG::Pixel::TRANSPARENT) ⇒ PixelMatrix
Initializes a new PixelMatrix instance
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
#height ⇒ Integer (readonly)
Returns The number of rows in this pixel matrix.
21 22 23 |
# File 'lib/chunky_png/pixel_matrix.rb', line 21 def height @height end |
#pixels ⇒ Array<ChunkyPNG::Pixel> (readonly)
Returns 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 |
#width ⇒ Integer (readonly)
Returns 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.
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.
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.
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.
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 |
#palette ⇒ ChunkyPNG::Palette
Returns the palette used for this pixel matrix.
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.
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 |