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.



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.



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.



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.



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