Class: ChunkyPNG::Canvas
- Inherits:
-
Object
- Object
- ChunkyPNG::Canvas
- Extended by:
- Adam7Interlacing, PNGDecoding, StreamImporting
- Includes:
- Drawing, Operations, PNGEncoding, StreamExporting
- Defined in:
- lib/chunky_png/canvas.rb,
lib/chunky_png/canvas/drawing.rb,
lib/chunky_png/canvas/operations.rb,
lib/chunky_png/canvas/png_decoding.rb,
lib/chunky_png/canvas/png_encoding.rb,
lib/chunky_png/canvas/stream_exporting.rb,
lib/chunky_png/canvas/stream_importing.rb,
lib/chunky_png/canvas/adam7_interlacing.rb
Overview
The ChunkPNG::Canvas class represents a raster image as a matrix of pixels.
This class supports loading a Canvas from a PNG datastream, and creating a 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 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 Color is provided to work more easily with these number as color values.
The module Operations is imported for operations on the whole canvas, like cropping and alpha compositing. Simple drawing functions are imported from the Drawing module.
Direct Known Subclasses
Defined Under Namespace
Modules: Adam7Interlacing, Drawing, Operations, PNGDecoding, PNGEncoding, StreamExporting, StreamImporting
Instance Attribute Summary collapse
-
#height ⇒ Integer
readonly
The number of rows in this canvas.
-
#pixels ⇒ Array<ChunkyPNG::Color>
readonly
The list of pixels in this canvas.
-
#width ⇒ Integer
readonly
The number of columns in this canvas.
Attributes included from PNGDecoding
Attributes included from PNGEncoding
Class Method Summary collapse
-
.from_canvas(canvas) ⇒ ChunkyPNG::Canvas
Creates a new canvas instance by duplicating another instance.
Instance Method Summary collapse
-
#[](x, y) ⇒ ChunkyPNG::Color
Returns a single pixel from this canvas.
-
#[]=(x, y, color) ⇒ Integer
Replaces a single pixel in this canvas.
-
#column(x) ⇒ Array<Integer>
Returns an extracted column as vector of pixels.
-
#eql?(other) ⇒ true, false
(also: #==)
Equality check to compare this canvas with other matrices.
-
#get_pixel(x, y) ⇒ ChunkyPNG::Color
Returns a single pixel from this canvas, without checking bounds.
-
#include_x?(x) ⇒ true, false
Checks whether the given x-coordinate is in the range of the canvas.
-
#include_xy?(x, y) ⇒ true, false
(also: #include?)
Checks whether the given coordinates are in the range of the canvas.
-
#include_y?(y) ⇒ true, false
Checks whether the given y-coordinate is in the range of the canvas.
-
#initialize(width, height, initial = ChunkyPNG::Color::TRANSPARENT) ⇒ Canvas
constructor
Initializes a new Canvas instance.
-
#initialize_copy(other) ⇒ Object
Initializes a new Canvas instances when being cloned.
-
#palette ⇒ ChunkyPNG::Palette
Returns the palette used for this canvas.
-
#replace_column!(x, vector) ⇒ Object
Replaces a column of pixels on this canvas.
-
#replace_row!(y, vector) ⇒ Object
Replaces a row of pixels on this canvas.
-
#row(y) ⇒ Array<Integer>
Returns an extracted row as vector of pixels.
-
#set_pixel(x, y, color) ⇒ Integer
Replaces a single pixel in this canvas, without bounds checking.
-
#size ⇒ Object
Returns the size ([width, height]) for this canvas.
- #tap {|_self| ... } ⇒ Object
-
#to_image ⇒ ChunkyPNG::Image
Creates an ChunkyPNG::Image object from this canvas.
Methods included from PNGDecoding
decode_png_pixelstream, from_blob, from_datastream, from_file, from_io
Methods included from Adam7Interlacing
adam7_extract_pass, adam7_merge_pass, adam7_multiplier_offset, adam7_pass_size, adam7_pass_sizes
Methods included from StreamImporting
from_abgr_stream, from_bgr_stream, from_rgb_stream, from_rgba_stream
Methods included from Drawing
#line_xiaolin_wu, #point, #rect
Methods included from Operations
#change_mask_color!, #change_theme_color!, #compose, #crop, #extract_mask, #flip_horizontally, #flip_vertically, #replace, #rotate_180, #rotate_left, #rotate_right
Methods included from StreamExporting
#to_abgr_stream, #to_rgb_stream, #to_rgba_stream
Methods included from PNGEncoding
#save, #to_blob, #to_datastream, #write
Constructor Details
#initialize(width, height, initial = ChunkyPNG::Color::TRANSPARENT) ⇒ Canvas
Initializes a new Canvas instance
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/chunky_png/canvas.rb', line 58 def initialize(width, height, initial = ChunkyPNG::Color::TRANSPARENT) @width, @height = width, height if initial.kind_of?(Integer) @pixels = Array.new(width * height, initial) elsif initial.kind_of?(Array) && initial.size == width * height @pixels = initial else raise ChunkyPNG::ExpectationFailed, "Cannot use this value as initial canvas: #{initial.inspect}!" end end |
Instance Attribute Details
#height ⇒ Integer (readonly)
Returns The number of rows in this canvas.
38 39 40 |
# File 'lib/chunky_png/canvas.rb', line 38 def height @height end |
#pixels ⇒ Array<ChunkyPNG::Color> (readonly)
Returns The list of pixels in this canvas. This array always should have width * height elements.
42 43 44 |
# File 'lib/chunky_png/canvas.rb', line 42 def pixels @pixels end |
#width ⇒ Integer (readonly)
Returns The number of columns in this canvas.
35 36 37 |
# File 'lib/chunky_png/canvas.rb', line 35 def width @width end |
Class Method Details
.from_canvas(canvas) ⇒ ChunkyPNG::Canvas
Creates a new canvas instance by duplicating another instance.
81 82 83 |
# File 'lib/chunky_png/canvas.rb', line 81 def self.from_canvas(canvas) self.new(canvas.width, canvas.height, canvas.pixels.dup) end |
Instance Method Details
#[](x, y) ⇒ ChunkyPNG::Color
Returns a single pixel from this canvas.
119 120 121 122 |
# File 'lib/chunky_png/canvas.rb', line 119 def [](x, y) assert_xy!(x, y) @pixels[y * width + x] end |
#[]=(x, y, color) ⇒ Integer
Replaces a single pixel in this canvas.
102 103 104 105 |
# File 'lib/chunky_png/canvas.rb', line 102 def []=(x, y, color) assert_xy!(x, y) @pixels[y * width + x] = color end |
#column(x) ⇒ Array<Integer>
Returns an extracted column as vector of pixels.
142 143 144 145 |
# File 'lib/chunky_png/canvas.rb', line 142 def column(x) assert_x!(x) (0...height).inject([]) { |pixels, y| pixels << get_pixel(x, y) } end |
#eql?(other) ⇒ true, false Also known as: ==
Equality check to compare this canvas with other matrices.
201 202 203 204 |
# File 'lib/chunky_png/canvas.rb', line 201 def eql?(other) other.kind_of?(self.class) && other.pixels == self.pixels && other.width == self.width && other.height == self.height end |
#get_pixel(x, y) ⇒ ChunkyPNG::Color
Returns a single pixel from this canvas, without checking bounds.
127 128 129 |
# File 'lib/chunky_png/canvas.rb', line 127 def get_pixel(x, y) @pixels[y * width + x] end |
#include_x?(x) ⇒ true, false
Checks whether the given x-coordinate is in the range of the canvas
186 187 188 |
# File 'lib/chunky_png/canvas.rb', line 186 def include_x?(x) x >= 0 && x < width end |
#include_xy?(x, y) ⇒ true, false Also known as: include?
Checks whether the given coordinates are in the range of the canvas
170 171 172 |
# File 'lib/chunky_png/canvas.rb', line 170 def include_xy?(x, y) include_x?(x) && include_y?(y) end |
#include_y?(y) ⇒ true, false
Checks whether the given y-coordinate is in the range of the canvas
179 180 181 |
# File 'lib/chunky_png/canvas.rb', line 179 def include_y?(y) y >= 0 && y < height end |
#initialize_copy(other) ⇒ Object
Initializes a new Canvas instances when being cloned.
73 74 75 76 |
# File 'lib/chunky_png/canvas.rb', line 73 def initialize_copy(other) @width, @height = other.width, other.height @pixels = other.pixels.dup end |
#palette ⇒ ChunkyPNG::Palette
Returns the palette used for this canvas.
193 194 195 |
# File 'lib/chunky_png/canvas.rb', line 193 def palette ChunkyPNG::Palette.from_canvas(self) end |
#replace_column!(x, vector) ⇒ Object
Replaces a column of pixels on this canvas.
158 159 160 161 162 163 |
# File 'lib/chunky_png/canvas.rb', line 158 def replace_column!(x, vector) assert_x!(x) && assert_height!(vector.length) for y in 0...height do set_pixel(x, y, vector[y]) end end |
#replace_row!(y, vector) ⇒ Object
Replaces a row of pixels on this canvas.
150 151 152 153 |
# File 'lib/chunky_png/canvas.rb', line 150 def replace_row!(y, vector) assert_y!(y) && assert_width!(vector.length) pixels[y * width, width] = vector end |
#row(y) ⇒ Array<Integer>
Returns an extracted row as vector of pixels
134 135 136 137 |
# File 'lib/chunky_png/canvas.rb', line 134 def row(y) assert_y!(y) pixels.slice(y * width, width) end |
#set_pixel(x, y, color) ⇒ Integer
Replaces a single pixel in this canvas, without bounds checking.
110 111 112 |
# File 'lib/chunky_png/canvas.rb', line 110 def set_pixel(x, y, color) @pixels[y * width + x] = color end |
#size ⇒ Object
Returns the size ([width, height]) for this canvas.
92 93 94 |
# File 'lib/chunky_png/canvas.rb', line 92 def size [@width, @height] end |
#tap {|_self| ... } ⇒ Object
223 224 225 226 |
# File 'lib/chunky_png/canvas.rb', line 223 def tap(&block) yield(self) self end |
#to_image ⇒ ChunkyPNG::Image
Creates an ChunkyPNG::Image object from this canvas.
214 215 216 |
# File 'lib/chunky_png/canvas.rb', line 214 def to_image ChunkyPNG::Image.from_canvas(self) end |