Class | ChunkyPNG::Chunk::Base |
In: |
lib/chunky_png/chunk.rb
|
Parent: | Object |
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
@abstract
type | [RW] | 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. @return [String] |
Initializes the chunk instance. @param [String] type The four character chunk type indicator. @param [Hash] attributes A hash of attributes to set on this chunk.
# File lib/chunky_png/chunk.rb, line 59 59: def initialize(type, attributes = {}) 60: self.type = type 61: attributes.each { |k, v| send("#{k}=", v) } 62: end
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. @param [IO] io The IO stream to write to.
# File lib/chunky_png/chunk.rb, line 78 78: def write(io) 79: write_with_crc(io, content || '') 80: end
Writes the chunk to the IO stream, using the provided content. The checksum will be calculated and appended to the stream. @param [IO] io The IO stream to write to. @param [String] content The content for this chunk.
# File lib/chunky_png/chunk.rb, line 68 68: def write_with_crc(io, content) 69: io << [content.length].pack('N') << type << content 70: io << [Zlib.crc32(content, Zlib.crc32(type))].pack('N') 71: end