class ChunkyPNG::Canvas
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.
Attributes
@return [Integer] The number of rows in this canvas
@return [Array<ChunkyPNG::Color>] The list of pixels in this canvas.
This array always should have +width * height+ elements.
@return [Integer] The number of columns in this canvas
Public Class Methods
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 103 def self.from_canvas(canvas) 104 new(canvas.width, canvas.height, canvas.pixels.dup) 105 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 77 def initialize(width, height, initial = ChunkyPNG::Color::TRANSPARENT) 78 @width, @height = width, height 79 80 if initial.is_a?(Array) 81 pixel_count = width * height 82 unless initial.length == pixel_count 83 raise ArgumentError, "The initial array should have #{width}x#{height} = #{pixel_count} elements!" 84 end 85 @pixels = initial 86 else 87 @pixels = Array.new(width * height, ChunkyPNG::Color.parse(initial)) 88 end 89 end
Public Instance Methods
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 172 def [](x, y) 173 assert_xy!(x, y) 174 @pixels[y * width + x] 175 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.
<tt>color</tt>.
@raise [ChunkyPNG::OutOfBounds] when the coordinates are outside of the
image's dimensions.
@see set_pixel
# File lib/chunky_png/canvas.rb 133 def []=(x, y, color) 134 assert_xy!(x, y) 135 @pixels[y * width + x] = ChunkyPNG::Color.parse(color) 136 end
Returns the area of this canvas in number of pixels. @return [Integer] The number of pixels in this canvas
# File lib/chunky_png/canvas.rb 120 def area 121 pixels.length 122 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 198 def column(x) 199 assert_x!(x) 200 (0...height).inject([]) { |pixels, y| pixels << get_pixel(x, y) } 201 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 114 def dimension 115 ChunkyPNG::Dimension.new(width, height) 116 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 275 def eql?(other) 276 other.is_a?(self.class) && 277 other.pixels == pixels && 278 other.width == width && 279 other.height == height 280 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 183 def get_pixel(x, y) 184 @pixels[y * width + x] 185 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 231 def include_point?(*point_like) 232 dimension.include?(ChunkyPNG::Point(*point_like)) 233 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 260 def include_x?(x) 261 x >= 0 && x < width 262 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 244 def include_xy?(x, y) 245 y >= 0 && y < height && x >= 0 && x < width 246 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 252 def include_y?(y) 253 y >= 0 && y < height 254 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 95 def initialize_copy(other) 96 @width, @height = other.width, other.height 97 @pixels = other.pixels.dup 98 end
Alternative implementation of the inspect method. @return [String] A nicely formatted string representation of this canvas. @private
# File lib/chunky_png/canvas.rb 297 def inspect 298 inspected = "<#{self.class.name} #{width}x#{height} [" 299 for y in 0...height 300 inspected << "\n\t[" << row(y).map { |p| ChunkyPNG::Color.to_hex(p) }.join(" ") << "]" 301 end 302 inspected << "\n]>" 303 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 267 def palette 268 ChunkyPNG::Palette.from_canvas(self) 269 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 218 def replace_column!(x, vector) 219 assert_x!(x) && assert_height!(vector.length) 220 for y in 0...height do 221 set_pixel(x, y, vector[y]) 222 end 223 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 208 def replace_row!(y, vector) 209 assert_y!(y) && assert_width!(vector.length) 210 pixels[y * width, width] = vector 211 end
Returns an extracted row as vector of pixels @param [Integer] y The 0-based row index @return [Array<Integer>] The vector of pixels in the requested row
# File lib/chunky_png/canvas.rb 190 def row(y) 191 assert_y!(y) 192 pixels.slice(y * width, width) 193 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] color The new color for the provided coordinates. @return [Integer] The new color value for this pixel, i.e.
<tt>color</tt>.
# File lib/chunky_png/canvas.rb 148 def set_pixel(x, y, color) 149 @pixels[y * width + x] = color 150 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] color The new color value for the provided coordinates. @return [Integer] The new color value for this pixel, i.e.
<tt>color</tt>, or <tt>nil</tt> if the coordinates are out of bounds.
# File lib/chunky_png/canvas.rb 160 def set_pixel_if_within_bounds(x, y, color) 161 return unless include_xy?(x, y) 162 @pixels[y * width + x] = color 163 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 290 def to_image 291 ChunkyPNG::Image.from_canvas(self) 292 end
Protected Instance Methods
Throws an exception if the vector_length does not match this canvas' height.
# File lib/chunky_png/canvas.rb 342 def assert_height!(vector_length) 343 if height != vector_length 344 raise ChunkyPNG::ExpectationFailed, 345 "The length of the vector (#{vector_length}) does not match the canvas height (#{height})!" 346 end 347 true 348 end
Throws an exception if the matrix width and height does not match this canvas' dimensions.
# File lib/chunky_png/canvas.rb 361 def assert_size!(matrix_width, matrix_height) 362 if width != matrix_width 363 raise ChunkyPNG::ExpectationFailed, 364 "The width of the matrix does not match the canvas width!" 365 end 366 if height != matrix_height 367 raise ChunkyPNG::ExpectationFailed, 368 "The height of the matrix does not match the canvas height!" 369 end 370 true 371 end
Throws an exception if the vector_length does not match this canvas' width.
# File lib/chunky_png/canvas.rb 352 def assert_width!(vector_length) 353 if width != vector_length 354 raise ChunkyPNG::ExpectationFailed, 355 "The length of the vector (#{vector_length}) does not match the canvas width (#{width})!" 356 end 357 true 358 end
Throws an exception if the x-coordinate is out of bounds.
# File lib/chunky_png/canvas.rb 317 def assert_x!(x) 318 unless include_x?(x) 319 raise ChunkyPNG::OutOfBounds, "Column index #{x} out of bounds!" 320 end 321 true 322 end
Throws an exception if the x- or y-coordinate is out of bounds.
# File lib/chunky_png/canvas.rb 333 def assert_xy!(x, y) 334 unless include_xy?(x, y) 335 raise ChunkyPNG::OutOfBounds, "Coordinates (#{x},#{y}) out of bounds!" 336 end 337 true 338 end
Throws an exception if the y-coordinate is out of bounds.
# File lib/chunky_png/canvas.rb 325 def assert_y!(y) 326 unless include_y?(y) 327 raise ChunkyPNG::OutOfBounds, "Row index #{y} out of bounds!" 328 end 329 true 330 end
Replaces the image, given a new width, new height, and a new pixel array.
# File lib/chunky_png/canvas.rb 308 def replace_canvas!(new_width, new_height, new_pixels) 309 unless new_pixels.length == new_width * new_height 310 raise ArgumentError, "The provided pixel array should have #{new_width * new_height} items" 311 end 312 @width, @height, @pixels = new_width, new_height, new_pixels 313 self 314 end