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
@return [Hash] The hash of metadata fields for this PNG image.
Public Class Methods
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.
# File lib/chunky_png/image.rb 71 def self.from_datastream(ds) 72 image = super(ds) 73 image.metadata = ds.metadata 74 image 75 end
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
ChunkyPNG::Canvas::new
# File lib/chunky_png/image.rb 20 def initialize(width, height, bg_color = ChunkyPNG::Color::TRANSPARENT, metadata = {}) 21 super(width, height, bg_color) 22 @metadata = metadata 23 end
Public Instance Methods
Initializes a copy of another ChunkyPNG::Image
instance.
@param [ChunkyPNG::Image] other The other image to copy.
ChunkyPNG::Canvas#initialize_copy
# File lib/chunky_png/image.rb 28 def initialize_copy(other) 29 super(other) 30 @metadata = other.metadata 31 end
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 41 def metadata_chunks 42 metadata.map do |key, value| 43 if value.length >= METADATA_COMPRESSION_TRESHOLD 44 ChunkyPNG::Chunk::CompressedText.new(key, value) 45 else 46 ChunkyPNG::Chunk::Text.new(key, value) 47 end 48 end 49 end
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
ChunkyPNG::Canvas::PNGEncoding#to_datastream
# File lib/chunky_png/image.rb 59 def to_datastream(constraints = {}) 60 ds = super(constraints) 61 ds.other_chunks += metadata_chunks 62 ds 63 end