The ChunkyPNG::Canvas class represents a raster image as a matrix of pixels.
This class supports loading a Canvas from a PNG datastream, and creating a {ChunkyPNG::Datastream PNG datastream} based on this matrix. ChunkyPNG only supports 8-bit color depth, otherwise all of the PNG format‘s variations are supported for both reading and writing.
This class offers per-pixel access to the matrix by using x,y coordinates. It uses a palette (see {ChunkyPNG::Palette}) to keep track of the different colors used in this matrix.
The pixels in the canvas are stored as 4-byte fixnum, representing 32-bit RGBA colors (8 bit per channel). The module {ChunkyPNG::Color} is provided to work more easily with these number as color values.
The module {ChunkyPNG::Canvas::Operations} is imported for operations on the whole canvas, like cropping and alpha compositing. Simple drawing functions are imported from the {ChunkyPNG::Canvas::Drawing} module.
height | [R] | @return [Integer] The number of rows in this canvas |
pixels | [R] |
@return [Array<ChunkyPNG::Color>] The list
of pixels in this canvas.
This array always should have +width * height+ elements. |
width | [R] | @return [Integer] The number of columns in this canvas |
Creates a new canvas instance by duplicating another instance. @param [ChunkyPNG::Canvas] canvas The canvas to duplicate @return [ChunkyPNG::Canvas] The newly constructed canvas instance.
# File lib/chunky_png/canvas.rb, line 98 98: def self.from_canvas(canvas) 99: self.new(canvas.width, canvas.height, canvas.pixels.dup) 100: end
Initializes a new Canvas instance.
@overload initialize(width, height, background_color)
@param [Integer] width The width in pixels of this canvas @param [Integer] height The height in pixels of this canvas @param [Integer, ...] background_color The initial background color of this canvas. This can be a color value or any value that {ChunkyPNG::Color.parse} can handle.
@overload initialize(width, height, initial)
@param [Integer] width The width in pixels of this canvas @param [Integer] height The height in pixels of this canvas @param [Array<Integer>] initial The initial pizel values. Must be an array with <tt>width * height</tt> elements.
# File lib/chunky_png/canvas.rb, line 74 74: def initialize(width, height, initial = ChunkyPNG::Color::TRANSPARENT) 75: 76: @width, @height = width, height 77: 78: if initial.kind_of?(Array) 79: raise ArgumentError, "The initial array should have #{width}x#{height} = #{width*height} elements!" unless initial.length == width * height 80: @pixels = initial 81: else 82: @pixels = Array.new(width * height, ChunkyPNG::Color.parse(initial)) 83: end 84: end
Returns a single pixel‘s color value from this canvas. @param [Integer] x The x-coordinate of the pixel (column) @param [Integer] y The y-coordinate of the pixel (row) @return [Integer] The current color value at the provided coordinates. @raise [ChunkyPNG::OutOfBounds] when the coordinates are outside of the image‘s dimensions. @see get_pixel
# File lib/chunky_png/canvas.rb, line 163 163: def [](x, y) 164: assert_xy!(x, y) 165: @pixels[y * width + x] 166: end
Replaces a single pixel in this canvas. @param [Integer] x The x-coordinate of the pixel (column) @param [Integer] y The y-coordinate of the pixel (row) @param [Integer] color The new color for the provided coordinates. @return [Integer] The new color value for this pixel, i.e. color. @raise [ChunkyPNG::OutOfBounds] when the coordinates are outside of the image‘s dimensions. @see set_pixel
# File lib/chunky_png/canvas.rb, line 126 126: def []=(x, y, color) 127: assert_xy!(x, y) 128: @pixels[y * width + x] = ChunkyPNG::Color.parse(color) 129: end
Returns an extracted column as vector of pixels. @param [Integer] x The 0-based column index. @return [Array<Integer>] The vector of pixels in the requested column.
# File lib/chunky_png/canvas.rb, line 187 187: def column(x) 188: assert_x!(x) 189: (0...height).inject([]) { |pixels, y| pixels << get_pixel(x, y) } 190: end
Returns the dimension (width x height) for this canvas. @return [ChunkyPNG::Dimension] A dimension instance with the width and height set for this canvas.
# File lib/chunky_png/canvas.rb, line 109 109: def dimension 110: ChunkyPNG::Dimension.new(width, height) 111: end
Equality check to compare this canvas with other matrices. @param other The object to compare this Matrix to. @return [true, false] True if the size and pixel values of the other canvas
are exactly the same as this canvas's size and pixel values.
# File lib/chunky_png/canvas.rb, line 256 256: def eql?(other) 257: other.kind_of?(self.class) && other.pixels == self.pixels && 258: other.width == self.width && other.height == self.height 259: end
Returns a single pixel from this canvas, without checking bounds. The return value for this method is undefined if the coordinates are out of bounds. @param (see #[]) @return [Integer] The current pixel at the provided coordinates.
# File lib/chunky_png/canvas.rb, line 172 172: def get_pixel(x, y) 173: @pixels[y * width + x] 174: end
Checks whether the given coordinates are in the range of the canvas @param [ChunkyPNG::Point, Array, Hash, String] point_like The point to check. @return [true, false] True if the x and y coordinates of the point are
within the limits of this canvas.
@see ChunkyPNG.Point
# File lib/chunky_png/canvas.rb, line 217 217: def include_point?(*point_like) 218: dimension.include?(ChunkyPNG::Point(*point_like)) 219: end
Checks whether the given x-coordinate is in the range of the canvas @param [Integer] x The y-coordinate of the pixel (column) @return [true, false] True if the x-coordinate is in the range of this canvas.
# File lib/chunky_png/canvas.rb, line 241 241: def include_x?(x) 242: x >= 0 && x < width 243: end
Checks whether the given x- and y-coordinate are in the range of the canvas @param [Integer] x The x-coordinate of the pixel (column) @param [Integer] y The y-coordinate of the pixel (row) @return [true, false] True if the x- and y-coordinate is in the range of this canvas.
# File lib/chunky_png/canvas.rb, line 227 227: def include_xy?(x, y) 228: y >= 0 && y < height && x >= 0 && x < width 229: end
Checks whether the given y-coordinate is in the range of the canvas @param [Integer] y The y-coordinate of the pixel (row) @return [true, false] True if the y-coordinate is in the range of this canvas.
# File lib/chunky_png/canvas.rb, line 234 234: def include_y?(y) 235: y >= 0 && y < height 236: end
Initializes a new Canvas instances when being cloned. @param [ChunkyPNG::Canvas] other The canvas to duplicate @return [void] @private
# File lib/chunky_png/canvas.rb, line 90 90: def initialize_copy(other) 91: @width, @height = other.width, other.height 92: @pixels = other.pixels.dup 93: end
Alternative implementation of the inspect method. @return [String] A nicely formatted string representation of this canvas. @private
# File lib/chunky_png/canvas.rb, line 276 276: def inspect 277: inspected = "<#{self.class.name} #{width}x#{height} [" 278: for y in 0...height 279: inspected << "\n\t[" << row(y).map { |p| ChunkyPNG::Color.to_hex(p) }.join(' ') << ']' 280: end 281: inspected << "\n]>" 282: end
Returns the palette used for this canvas. @return [ChunkyPNG::Palette] A palette which contains all the colors that are
being used for this image.
# File lib/chunky_png/canvas.rb, line 248 248: def palette 249: ChunkyPNG::Palette.from_canvas(self) 250: end
Replaces a column of pixels on this canvas. @param [Integer] x The 0-based column index. @param [Array<Integer>] vector The vector of pixels to replace the column with. @return [void]
# File lib/chunky_png/canvas.rb, line 205 205: def replace_column!(x, vector) 206: assert_x!(x) && assert_height!(vector.length) 207: for y in 0...height do 208: set_pixel(x, y, vector[y]) 209: end 210: end
Replaces a row of pixels on this canvas. @param [Integer] y The 0-based row index. @param [Array<Integer>] vector The vector of pixels to replace the row with. @return [void]
# File lib/chunky_png/canvas.rb, line 196 196: def replace_row!(y, vector) 197: assert_y!(y) && assert_width!(vector.length) 198: pixels[y * width, width] = vector 199: end
Replaces a single pixel in this canvas, without bounds checking.
This method return value and effects are undefined for coordinates out of bounds of the canvas.
@param [Integer] x The x-coordinate of the pixel (column) @param [Integer] y The y-coordinate of the pixel (row) @param [Integer] pixel The new color for the provided coordinates. @return [Integer] The new color value for this pixel, i.e. color.
# File lib/chunky_png/canvas.rb, line 140 140: def set_pixel(x, y, color) 141: @pixels[y * width + x] = color 142: end
Replaces a single pixel in this canvas, with bounds checking. It will do noting if the provided coordinates are out of bounds.
@param [Integer] x The x-coordinate of the pixel (column) @param [Integer] y The y-coordinate of the pixel (row) @param [Integer] pixel The new color value for the provided coordinates. @return [Integer] The new color value for this pixel, i.e. color, or
<tt>nil</tt> if the coordinates are out of bounds.
# File lib/chunky_png/canvas.rb, line 152 152: def set_pixel_if_within_bounds(x, y, color) 153: return unless include_xy?(x, y) 154: @pixels[y * width + x] = color 155: end
Creates an ChunkyPNG::Image object from this canvas. @return [ChunkyPNG::Image] This canvas wrapped in an Image instance.
# File lib/chunky_png/canvas.rb, line 269 269: def to_image 270: ChunkyPNG::Image.from_canvas(self) 271: end
Throws an exception if the vector_length does not match this canvas’ height.
# File lib/chunky_png/canvas.rb, line 312 312: def assert_height!(vector_length) 313: raise ChunkyPNG::ExpectationFailed, "The length of the vector (#{vector_length}) does not match the canvas height (#{height})!" if height != vector_length 314: return true 315: end
Throws an exception if the matrix width and height does not match this canvas’ dimensions.
# File lib/chunky_png/canvas.rb, line 324 324: def assert_size!(matrix_width, matrix_height) 325: raise ChunkyPNG::ExpectationFailed, "The width of the matrix does not match the canvas width!" if width != matrix_width 326: raise ChunkyPNG::ExpectationFailed, "The height of the matrix does not match the canvas height!" if height != matrix_height 327: return true 328: end
Throws an exception if the vector_length does not match this canvas’ width.
# File lib/chunky_png/canvas.rb, line 318 318: def assert_width!(vector_length) 319: raise ChunkyPNG::ExpectationFailed, "The length of the vector (#{vector_length}) does not match the canvas width (#{width})!" if width != vector_length 320: return true 321: end
Throws an exception if the x-coordinate is out of bounds.
# File lib/chunky_png/canvas.rb, line 294 294: def assert_x!(x) 295: raise ChunkyPNG::OutOfBounds, "Column index #{x} out of bounds!" unless include_x?(x) 296: return true 297: end
Throws an exception if the x- or y-coordinate is out of bounds.
# File lib/chunky_png/canvas.rb, line 306 306: def assert_xy!(x, y) 307: raise ChunkyPNG::OutOfBounds, "Coordinates (#{x},#{y}) out of bounds!" unless include_xy?(x, y) 308: return true 309: end
Throws an exception if the y-coordinate is out of bounds.
# File lib/chunky_png/canvas.rb, line 300 300: def assert_y!(y) 301: raise ChunkyPNG::OutOfBounds, "Row index #{y} out of bounds!" unless include_y?(y) 302: return true 303: end
Replaces the image, given a new width, new height, and a new pixel array.
# File lib/chunky_png/canvas.rb, line 287 287: def replace_canvas!(new_width, new_height, new_pixels) 288: raise ArgumentError, "The provided pixel array should have #{new_width * new_height} items" unless new_pixels.length == new_width * new_height 289: @width, @height, @pixels = new_width, new_height, new_pixels 290: return self 291: end