Module: ChunkyPNG

Defined in:
lib/chunky_png.rb,
lib/chunky_png/chunk.rb,
lib/chunky_png/color.rb,
lib/chunky_png/image.rb,
lib/chunky_png/point.rb,
lib/chunky_png/canvas.rb,
lib/chunky_png/vector.rb,
lib/chunky_png/palette.rb,
lib/chunky_png/rmagick.rb,
lib/chunky_png/dimension.rb,
lib/chunky_png/datastream.rb,
lib/chunky_png/canvas/drawing.rb,
lib/chunky_png/canvas/masking.rb,
lib/chunky_png/canvas/operations.rb,
lib/chunky_png/canvas/resampling.rb,
lib/chunky_png/canvas/png_decoding.rb,
lib/chunky_png/canvas/png_encoding.rb,
lib/chunky_png/canvas/stream_exporting.rb,
lib/chunky_png/canvas/stream_importing.rb,
lib/chunky_png/canvas/adam7_interlacing.rb,
lib/chunky_png/canvas/data_url_exporting.rb,
lib/chunky_png/canvas/data_url_importing.rb

Overview

ChunkyPNG - the pure ruby library to access PNG files.

The ChunkyPNG module defines some constants that are used in the PNG specification, specifies some exception classes, and serves as a namespace for all the other modules and classes in this library.

Image

class to represent PNG images, including metadata.

Canvas

class to represent the image’s canvas.

Color

module to work with color values.

Palette

represents the palette of colors used on a Canvas.

Datastream

represents the internal structure of a PNG Image.

Color

represents one chunk of data within a Datastream.

Point

geometry helper class representing a 2-dimensional point.

Dimension

geometry helper class representing a dimension (i.e. width x height).

Vector

geometry helper class representing a series of points.

Author:

  • Willem van Bergen

Defined Under Namespace

Modules: Chunk, Color, RMagick Classes: CRCMismatch, Canvas, Datastream, Dimension, Exception, ExpectationFailed, Image, NotSupported, OutOfBounds, Palette, Point, SignatureMismatch, Vector

Constant Summary collapse

VERSION =

The current version of ChunkyPNG. This value will be updated automatically by them gem:release rake task.

"1.2.5"

Class Method Summary collapse

Class Method Details

.Color(r, g, b, a) ⇒ Integer .Color(r, g, b) ⇒ Integer .Color(hex_value, opacity = nil) ⇒ Integer .Color(color_name, opacity = nil) ⇒ Integer .Color(color_value, opacity = nil) ⇒ Integer

Factory method to return a color value, based on the arguments given.

Overloads:

  • .Color(r, g, b, a) ⇒ Integer

    Returns The rgba color value.

    Returns:

    • (Integer)

      The rgba color value.

  • .Color(r, g, b) ⇒ Integer

    Returns The rgb color value.

    Returns:

    • (Integer)

      The rgb color value.

  • .Color(hex_value, opacity = nil) ⇒ Integer

    Returns The hex color value, with the opacity applied if one was given.

    Returns:

    • (Integer)

      The hex color value, with the opacity applied if one was given.

  • .Color(color_name, opacity = nil) ⇒ Integer

    Returns The hex color value, with the opacity applied if one was given.

    Returns:

    • (Integer)

      The hex color value, with the opacity applied if one was given.

  • .Color(color_value, opacity = nil) ⇒ Integer

    Returns The color value, with the opacity applied if one was given.

    Parameters:

    • The (Integer, :to_i)

      color value.

    Returns:

    • (Integer)

      The color value, with the opacity applied if one was given.

Returns:

  • (Integer)

    The determined color value as RGBA integer.

Raises:

  • (ArgumentError)

    if the arguments weren’t understood as a color.

See Also:



29
30
31
32
33
34
35
36
37
# File 'lib/chunky_png/color.rb', line 29

def self.Color(*args)
  case args.length
    when 1; ChunkyPNG::Color.parse(args.first)
    when 2; (ChunkyPNG::Color.parse(args.first) & 0xffffff00) | args[1].to_i
    when 3; ChunkyPNG::Color.rgb(*args)
    when 4; ChunkyPNG::Color.rgba(*args)
    else raise ArgumentError, "Don't know how to create a color from #{args.inspect}!"
  end
end

.Dimension(width, height) ⇒ ChunkyPNG::Dimension .Dimension(string) ⇒ ChunkyPNG::Dimension .Dimension(ary) ⇒ ChunkyPNG::Dimension .Dimension(hash) ⇒ ChunkyPNG::Dimension

Creates a Dimension instance using arguments that can be interpreted as width and height.

Overloads:

  • .Dimension(width, height) ⇒ ChunkyPNG::Dimension

    Returns The instantiated dimension.

    Parameters:

    • width (Integer)

      The width-component of the dimension.

    • height (Integer)

      The height-component of the dimension.

    Returns:

  • .Dimension(string) ⇒ ChunkyPNG::Dimension

    Returns The instantiated dimension.

    Parameters:

    • string (String)

      A string from which a width and height value can be parsed, e.g. '10x20' or '[10, 20]'.

    Returns:

  • .Dimension(ary) ⇒ ChunkyPNG::Dimension

    Returns The instantiated dimension.

    Parameters:

    • ary (Array)

      An array with the desired width as first element and the desired height as second element, e.g. [10, 20].

    Returns:

  • .Dimension(hash) ⇒ ChunkyPNG::Dimension

    Returns The instantiated dimension.

    Parameters:

    • hash (Hash)

      An hash with a 'height' or :height key for the desired height and with a 'width' or :width key for the desired width.

    Returns:

Returns:

Raises:

  • (ArgumentError)

    If the argument(s) given where not understood as a dimension.

See Also:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chunky_png/dimension.rb', line 30

def self.Dimension(*args)

  case args.length
    when 2; ChunkyPNG::Dimension.new(*args)
    when 1; case source = args.first
        when ChunkyPNG::Dimension; source
        when ChunkyPNG::Point; ChunkyPNG::Dimension.new(source.x, source.y)
        when Array; ChunkyPNG::Dimension.new(source[0], source[1])
        when Hash;  ChunkyPNG::Dimension.new(source[:width] || source['width'], source[:height] || source['height'])
        when ChunkyPNG::Dimension::DIMENSION_REGEXP; ChunkyPNG::Dimension.new($1, $2)
        else
          if source.respond_to?(:width) && source.respond_to?(:height)
            ChunkyPNG::Dimension.new(source.width, source.height)
          else
            raise ArgumentError, "Don't know how to construct a point from #{source.inspect}!"
          end
      end
    else raise ArgumentError, "Don't know how to construct a point from #{args.inspect}!"
  end
end

.Point(x, y) ⇒ ChunkyPNG::Point .Point(array) ⇒ ChunkyPNG::Point .Point(hash) ⇒ ChunkyPNG::Point .Point(string) ⇒ ChunkyPNG::Point

Factory method to create Point instances.

This method tries to be as flexible as possible with regards to the given input: besides explicit coordinates, this method also accepts arrays, hashes, strings, Dimension instances and anything that responds to :x and :y.

Overloads:

  • .Point(x, y) ⇒ ChunkyPNG::Point

    Returns The instantiated point.

    Parameters:

    • x (Integer, :to_i)

      The x-coordinate

    • y (Integer, :to_i)

      The y-coordinate

    Returns:

  • .Point(array) ⇒ ChunkyPNG::Point

    Returns The instantiated point.

    Parameters:

    • array (Array<Integer>)

      A two element array which represent the x- and y-coordinate.

    Returns:

  • .Point(hash) ⇒ ChunkyPNG::Point

    Returns The instantiated point.

    Parameters:

    • array (Hash)

      A hash with the :x or 'x' and :y or 'y' keys set, which will be used as coordinates.

    Returns:

  • .Point(string) ⇒ ChunkyPNG::Point

    Returns The instantiated point.

    Parameters:

    • string (String)

      A string that contains the coordinates, e.g. '0, 4', '(0 4)', [0,4}', etc.

    Returns:

Returns:

Raises:

  • (ArgumentError)

    if the arguments weren’t understood.

See Also:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/chunky_png/point.rb', line 31

def self.Point(*args)
  case args.length
    when 2; ChunkyPNG::Point.new(*args)
    when 1; case source = args.first
        when ChunkyPNG::Point; source
        when ChunkyPNG::Dimension; ChunkyPNG::Point.new(source.width, source.height)
        when Array; ChunkyPNG::Point.new(source[0], source[1])
        when Hash; ChunkyPNG::Point.new(source[:x] || source['x'], source[:y] || source['y'])
        when ChunkyPNG::Point::POINT_REGEXP; ChunkyPNG::Point.new($1.to_i, $2.to_i)
        else 
          if source.respond_to?(:x) && source.respond_to?(:y)
            ChunkyPNG::Point.new(source.x, source.y)
          else
            raise ArgumentError, "Don't know how to construct a point from #{source.inspect}!"
          end
      end
    else raise ArgumentError, "Don't know how to construct a point from #{args.inspect}!"
  end
end

.Vector(x0, y0, x1, y1, x2, y2, ...) ⇒ ChunkyPNG::Vector .Vector(string) ⇒ ChunkyPNG::Vector .Vector(pointlike, pointlike, pointlike, ...) ⇒ ChunkyPNG::Vector

Factory method for Vector instances.

Overloads:

  • .Vector(x0, y0, x1, y1, x2, y2, ...) ⇒ ChunkyPNG::Vector

    Creates a vector by parsing two subsequent values in the argument list as x- and y-coordinate of a point.

    Returns:

  • .Vector(string) ⇒ ChunkyPNG::Vector

    Creates a vector by parsing coordinates from the input string.

    Returns:

  • .Vector(pointlike, pointlike, pointlike, ...) ⇒ ChunkyPNG::Vector

    Creates a vector by converting every argument to a point using Point.

    Returns:

Returns:

Raises:

  • (ArgumentError)

    If the given arguments could not be understood as a vector.

See Also:



19
20
21
22
23
24
25
26
27
28
# File 'lib/chunky_png/vector.rb', line 19

def self.Vector(*args)
  
  return args.first if args.length == 1 && args.first.kind_of?(ChunkyPNG::Vector)
  
  if args.length == 1 && args.first.respond_to?(:scan)
    ChunkyPNG::Vector.new(ChunkyPNG::Vector.multiple_from_string(args.first)) # e.g. ['1,1 2,2 3,3']
  else
    ChunkyPNG::Vector.new(ChunkyPNG::Vector.multiple_from_array(args)) # e.g. [[1,1], [2,2], [3,3]] or [1,1,2,2,3,3]
  end
end