Module: ChunkyPNG::Canvas::PNGEncoding
- Included in:
- ChunkyPNG::Canvas
- Defined in:
- lib/chunky_png/canvas/png_encoding.rb
Overview
Methods for encoding a Canvas instance into a PNG datastream.
Overview of the encoding process:
-
The image is split up in scanlines (i.e. rows of pixels);
-
All pixels are encoded as a pixelstream, based on the color mode.
-
All the pixel bytes in the pixelstream are adjusted using a filtering method if one is specified.
-
Compress the resulting string using deflate compression.
-
Split compressed data over one or more PNG chunks.
-
These chunks should be embedded in a datastream with at least a IHDR and IEND chunk and possibly a PLTE chunk.
For interlaced images, the initial image is first split into 7 subimages. These images get encoded exactly as above, and the result gets combined before the compression step.
Instance Attribute Summary collapse
-
#encoding_palette ⇒ ChunkyPNG::Palette
The palette used for encoding the image.This is only in used for images that get encoded using indexed colors.
Instance Method Summary collapse
-
#save(filename, constraints = {})
Writes the canvas to a file, encoded as a PNG image.
-
#to_blob(constraints = {}) ⇒ String
(also: #to_string, #to_s)
Encoded the canvas to a PNG formatted string.
-
#to_datastream(constraints = {}) ⇒ ChunkyPNG::Datastream
Converts this Canvas to a datastream, so that it can be saved as a PNG image.
-
#write(io, constraints = {})
Writes the canvas to an IO stream, encoded as a PNG image.
Instance Attribute Details
#encoding_palette ⇒ ChunkyPNG::Palette
The palette used for encoding the image.This is only in used for images that get encoded using indexed colors.
28 29 30 |
# File 'lib/chunky_png/canvas/png_encoding.rb', line 28 def encoding_palette @encoding_palette end |
Instance Method Details
#save(filename, constraints = {})
This method returns an undefined value.
Writes the canvas to a file, encoded as a PNG image.
42 43 44 |
# File 'lib/chunky_png/canvas/png_encoding.rb', line 42 def save(filename, constraints = {}) File.open(filename, "wb") { |io| write(io, constraints) } end |
#to_blob(constraints = {}) ⇒ String Also known as: to_string, to_s
Encoded the canvas to a PNG formatted string.
49 50 51 |
# File 'lib/chunky_png/canvas/png_encoding.rb', line 49 def to_blob(constraints = {}) to_datastream(constraints).to_blob end |
#to_datastream(constraints = {}) ⇒ ChunkyPNG::Datastream
Converts this Canvas to a datastream, so that it can be saved as a PNG image.
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/chunky_png/canvas/png_encoding.rb', line 74 def to_datastream(constraints = {}) encoding = determine_png_encoding(constraints) ds = Datastream.new ds.header_chunk = Chunk::Header.new( width: width, height: height, color: encoding[:color_mode], depth: encoding[:bit_depth], interlace: encoding[:interlace] ) if encoding[:color_mode] == ChunkyPNG::COLOR_INDEXED ds.palette_chunk = encoding_palette.to_plte_chunk ds.transparency_chunk = encoding_palette.to_trns_chunk unless encoding_palette.opaque? end data = encode_png_pixelstream(encoding[:color_mode], encoding[:bit_depth], encoding[:interlace], encoding[:filtering]) ds.data_chunks = Chunk::ImageData.split_in_chunks(data, encoding[:compression]) ds.end_chunk = Chunk::End.new ds end |
#write(io, constraints = {})
This method returns an undefined value.
Writes the canvas to an IO stream, encoded as a PNG image.
34 35 36 |
# File 'lib/chunky_png/canvas/png_encoding.rb', line 34 def write(io, constraints = {}) to_datastream(constraints).write(io) end |