Class: ChunkyPNG::Chunk::Base Abstract

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

Overview

This class is abstract.

The base chunk class is the superclass for every chunk type. It contains methods to write the chunk to an output stream.

A subclass should implement the content method, which gets called when the chunk gets written to a PNG datastream

Direct Known Subclasses

CompressedText, End, Generic, Header, Text

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Base) initialize(type, attributes = {})

Initializes the chunk instance.

Parameters:

  • type (String)

    The four character chunk type indicator.

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

    A hash of attributes to set on this chunk.



55
56
57
58
# File 'lib/chunky_png/chunk.rb', line 55

def initialize(type, attributes = {})
  self.type = type
  attributes.each { |k, v| send("#{k}=", v) }
end

Instance Attribute Details

- (String) type

The four-character type indicator for the chunk. This field is used to find the correct class for a chunk when it is loaded from a PNG stream.

Returns:



50
51
52
# File 'lib/chunky_png/chunk.rb', line 50

def type
  @type
end

Instance Method Details

- (Object) write(io)

Writes the chunk to the IO stream.

It will call the content method to get the content for this chunk, and will calculate and append the checksum automatically.

Parameters:

  • io (IO)

    The IO stream to write to.



74
75
76
# File 'lib/chunky_png/chunk.rb', line 74

def write(io)
  write_with_crc(io, content || '')
end

- (Object) write_with_crc(io, content)

Writes the chunk to the IO stream, using the provided content. The checksum will be calculated and appended to the stream.

Parameters:

  • io (IO)

    The IO stream to write to.

  • content (String)

    The content for this chunk.



64
65
66
67
# File 'lib/chunky_png/chunk.rb', line 64

def write_with_crc(io, content)
  io << [content.length].pack('N') << type << content
  io << [Zlib.crc32(content, Zlib.crc32(type))].pack('N')
end