Class: ChunkyPNG::Image

Inherits:
Canvas
  • Object
show all
Defined in:
lib/chunky_png/image.rb

Overview

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

See Also:

Constant Summary

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

Attributes inherited from Canvas

#height, #pixels, #width

Attributes included from Canvas::PNGDecoding

#decoding_palette, #transparent_color

Attributes included from Canvas::PNGEncoding

#encoding_palette

Class Method Summary collapse

Instance Method Summary collapse

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::DataUrlImporting

#from_data_url

Methods included from Canvas::StreamImporting

#from_abgr_stream, #from_bgr_stream, #from_rgb_stream, #from_rgba_stream

Methods included from Canvas::Adam7Interlacing

#adam7_extract_pass, #adam7_merge_pass, #adam7_multiplier_offset, #adam7_pass_size, #adam7_pass_sizes

Methods included from Canvas::PNGDecoding

#decode_png_pixelstream, #from_blob, #from_datastream, #from_file, #from_io

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

#to_data_url

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

#save, #to_blob, #write

Constructor Details

#initialize(width, height, bg_color = ChunkyPNG::Color::TRANSPARENT, metadata = {}) ⇒ Image

Initializes a new ChunkyPNG::Image instance.

Parameters:

  • width (Integer)

    The width of the new image.

  • height (Integer)

    The height of the new image.

  • bg_color (Integer) (defaults to: ChunkyPNG::Color::TRANSPARENT)

    The background color of the new image.

  • metadata (Hash) (defaults to: {})

    A hash of metadata fields and values for this image.

See Also:



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

#metadataHash

Returns The hash of metadata fields for this PNG image.

Returns:

  • (Hash)

    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.

Parameters:



73
74
75
76
77
# File 'lib/chunky_png/image.rb', line 73

def self.from_datastream(ds)
  image = super(ds)
  image. = ds.
  return image
end

Instance Method Details

#initialize_copy(other) ⇒ Object

Initializes a copy of another ChunkyPNG::Image instance.

Parameters:



30
31
32
33
# File 'lib/chunky_png/image.rb', line 30

def initialize_copy(other)
  super(other)
  @metadata = other.
end

#metadata_chunksArray<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.

Returns:

See Also:



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.

Parameters:

  • constraints (Hash) (defaults to: {})

    The constraints to use when encoding the canvas.

Returns:

See Also:



61
62
63
64
65
# File 'lib/chunky_png/image.rb', line 61

def to_datastream(constraints = {})
  ds = super(constraints)
  ds.other_chunks += 
  return ds
end