Class: ChunkyPNG::Chunk::Base Abstract
- Inherits:
-
Object
- Object
- ChunkyPNG::Chunk::Base
- Defined in:
- lib/chunky_png/chunk.rb
Overview
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, InternationalText, Physical, Text
Instance Attribute Summary collapse
-
#type ⇒ String
The four-character type indicator for the chunk.
Instance Method Summary collapse
-
#initialize(type, attributes = {}) ⇒ Base
constructor
Initializes the chunk instance.
-
#write(io) ⇒ Object
Writes the chunk to the IO stream.
-
#write_with_crc(io, content) ⇒ Object
Writes the chunk to the IO stream, using the provided content.
Constructor Details
#initialize(type, attributes = {}) ⇒ Base
Initializes the chunk instance.
69 70 71 72 |
# File 'lib/chunky_png/chunk.rb', line 69 def initialize(type, attributes = {}) self.type = type attributes.each { |k, v| send("#{k}=", v) } end |
Instance Attribute Details
#type ⇒ String
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.
64 65 66 |
# File 'lib/chunky_png/chunk.rb', line 64 def type @type end |
Instance Method Details
#write(io) ⇒ Object
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.
88 89 90 |
# File 'lib/chunky_png/chunk.rb', line 88 def write(io) write_with_crc(io, content || "") end |
#write_with_crc(io, content) ⇒ Object
Writes the chunk to the IO stream, using the provided content. The checksum will be calculated and appended to the stream.
78 79 80 81 |
# File 'lib/chunky_png/chunk.rb', line 78 def write_with_crc(io, content) io << [content.length].pack("N") << type << content io << [Zlib.crc32(content, Zlib.crc32(type))].pack("N") end |