class ChunkyPNG::Image

ChunkyPNG::Image is an extension of the {ChunkyPNG::Canvas} class, that also includes support for metadata.

@see ChunkyPNG::Canvas

Constants

METADATA_COMPRESSION_TRESHOLD

The minimum size of bytes the value of a metadata field should be before compression is enabled for the chunk.

Attributes

metadata[RW]

@return [Hash] The hash of metadata fields for this PNG image.

Public Class Methods

from_datastream(ds) click to toggle source

Reads a ChunkyPNG::Image instance from a data stream.

Besides decoding the canvas, this will also read the metadata fields from the datastream.

@param [ChunkyPNG::Datastream] ds The datastream to read from.

Calls superclass method
   # File lib/chunky_png/image.rb
73 def self.from_datastream(ds)
74   image = super(ds)
75   image.metadata = ds.metadata
76   image
77 end
new(width, height, bg_color = ChunkyPNG::Color::TRANSPARENT, metadata = {}) click to toggle source

Initializes a new ChunkyPNG::Image instance. @param [Integer] width The width of the new image. @param [Integer] height The height of the new image. @param [Integer] bg_color The background color of the new image. @param [Hash] metadata A hash of metadata fields and values for this image. @see ChunkyPNG::Canvas#initialize

Calls superclass method ChunkyPNG::Canvas::new
   # File lib/chunky_png/image.rb
22 def initialize(width, height, bg_color = ChunkyPNG::Color::TRANSPARENT, metadata = {})
23   super(width, height, bg_color)
24   @metadata = metadata
25 end

Public Instance Methods

initialize_copy(other) click to toggle source

Initializes a copy of another ChunkyPNG::Image instance.

@param [ChunkyPNG::Image] other The other image to copy.

Calls superclass method ChunkyPNG::Canvas#initialize_copy
   # File lib/chunky_png/image.rb
30 def initialize_copy(other)
31   super(other)
32   @metadata = other.metadata
33 end
metadata_chunks() click to toggle source

Returns the metadata for this image as PNG chunks.

Chunks will either be of the {ChunkyPNG::Chunk::Text} type for small values (in bytes), or of the {ChunkyPNG::Chunk::CompressedText} type for values that are larger in size.

@return [Array<ChunkyPNG::Chunk>] An array of metadata chunks. @see ChunkyPNG::Image::METADATA_COMPRESSION_TRESHOLD

   # File lib/chunky_png/image.rb
43 def metadata_chunks
44   metadata.map do |key, value|
45     if value.length >= METADATA_COMPRESSION_TRESHOLD
46       ChunkyPNG::Chunk::CompressedText.new(key, value)
47     else
48       ChunkyPNG::Chunk::Text.new(key, value)
49     end
50   end
51 end
to_datastream(constraints = {}) click to toggle source

Encodes the image to a PNG datastream for saving to disk or writing to an IO stream.

Besides encoding the canvas, it will also encode the metadata fields to text chunks.

@param [Hash] constraints The constraints to use when encoding the canvas. @return [ChunkyPNG::Datastream] The datastream that contains this image. @see ChunkyPNG::Canvas::PNGEncoding#to_datastream @see metadata_chunks

   # File lib/chunky_png/image.rb
61 def to_datastream(constraints = {})
62   ds = super(constraints)
63   ds.other_chunks += metadata_chunks
64   ds
65 end