Class: ChunkyPNG::Image
Overview
ChunkyPNG::Image is an extension of the Canvas class, that also includes support for metadata.
Constant Summary collapse
- METADATA_COMPRESSION_TRESHOLD =
The minimum size of bytes the value of a metadata field should be before compression is enabled for the chunk.
300
Instance Attribute Summary collapse
-
#metadata ⇒ Hash
The hash of metadata fields for this PNG image.
Attributes inherited from Canvas
Attributes included from Canvas::PNGEncoding
Class Method Summary collapse
-
.from_datastream(ds) ⇒ Object
Reads a ChunkyPNG::Image instance from a data stream.
Instance Method Summary collapse
-
#initialize(width, height, bg_color = ChunkyPNG::Color::TRANSPARENT, metadata = {}) ⇒ Image
constructor
Initializes a new ChunkyPNG::Image instance.
-
#initialize_copy(other) ⇒ Object
Initializes a copy of another ChunkyPNG::Image instance.
-
#metadata_chunks ⇒ Array<ChunkyPNG::Chunk>
Returns the metadata for this image as PNG chunks.
-
#to_datastream(constraints = {}) ⇒ ChunkyPNG::Datastream
Encodes the image to a PNG datastream for saving to disk or writing to an IO stream.
Methods inherited from Canvas
#[], #[]=, #area, #column, #dimension, #eql?, from_canvas, #get_pixel, #include_point?, #include_x?, #include_xy?, #include_y?, #palette, #replace_column!, #replace_row!, #row, #set_pixel, #set_pixel_if_within_bounds, #to_image
Methods included from Canvas::PNGDecoding
#decode_png_pixelstream, #from_blob, #from_datastream, #from_file, #from_io
Methods included from Canvas::Adam7Interlacing
#adam7_extract_pass, #adam7_merge_pass, #adam7_multiplier_offset, #adam7_pass_size, #adam7_pass_sizes
Methods included from Canvas::StreamImporting
#from_abgr_stream, #from_bgr_stream, #from_rgb_stream, #from_rgba_stream
Methods included from Canvas::DataUrlImporting
Methods included from Canvas::Masking
#change_mask_color!, #change_theme_color!, #extract_mask
Methods included from Canvas::Resampling
#resample_bilinear, #resample_bilinear!, #resample_nearest_neighbor, #resample_nearest_neighbor!, #steps, #steps_residues
Methods included from Canvas::Drawing
#bezier_curve, #circle, #compose_pixel, #compose_pixel_unsafe, #line_xiaolin_wu, #polygon, #rect
Methods included from Canvas::Operations
#border, #border!, #compose, #compose!, #crop, #crop!, #flip_horizontally, #flip_horizontally!, #flip_vertically, #flip_vertically!, #grayscale, #grayscale!, #replace, #replace!, #rotate_180, #rotate_180!, #rotate_left, #rotate_left!, #rotate_right, #rotate_right!, #trim, #trim!
Methods included from Canvas::DataUrlExporting
Methods included from Canvas::StreamExporting
#to_abgr_stream, #to_alpha_channel_stream, #to_grayscale_stream, #to_rgb_stream, #to_rgba_stream
Methods included from Canvas::PNGEncoding
Constructor Details
#initialize(width, height, bg_color = ChunkyPNG::Color::TRANSPARENT, metadata = {}) ⇒ Image
Initializes a new ChunkyPNG::Image instance.
22 23 24 25 |
# File 'lib/chunky_png/image.rb', line 22 def initialize(width, height, bg_color = ChunkyPNG::Color::TRANSPARENT, = {}) super(width, height, bg_color) @metadata = end |
Instance Attribute Details
#metadata ⇒ Hash
Returns The hash of metadata fields for this PNG image.
14 15 16 |
# File 'lib/chunky_png/image.rb', line 14 def @metadata end |
Class Method Details
.from_datastream(ds) ⇒ Object
Reads a ChunkyPNG::Image instance from a data stream.
Besides decoding the canvas, this will also read the metadata fields from the datastream.
73 74 75 76 77 |
# File 'lib/chunky_png/image.rb', line 73 def self.from_datastream(ds) image = super(ds) image. = ds. image end |
Instance Method Details
#initialize_copy(other) ⇒ Object
Initializes a copy of another ChunkyPNG::Image instance.
30 31 32 33 |
# File 'lib/chunky_png/image.rb', line 30 def initialize_copy(other) super(other) @metadata = other. end |
#metadata_chunks ⇒ Array<ChunkyPNG::Chunk>
Returns the metadata for this image as PNG chunks.
Chunks will either be of the Chunk::Text type for small values (in bytes), or of the Chunk::CompressedText type for values that are larger in size.
43 44 45 46 47 48 49 50 51 |
# File 'lib/chunky_png/image.rb', line 43 def .map do |key, value| if value.length >= METADATA_COMPRESSION_TRESHOLD ChunkyPNG::Chunk::CompressedText.new(key, value) else ChunkyPNG::Chunk::Text.new(key, value) end end end |
#to_datastream(constraints = {}) ⇒ ChunkyPNG::Datastream
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.
61 62 63 64 65 |
# File 'lib/chunky_png/image.rb', line 61 def to_datastream(constraints = {}) ds = super(constraints) ds.other_chunks += ds end |