Class: ChunkyPNG::Chunk::Base

### Home Classes Methods

### In Files

- lib/chunky_png/chunk.rb

### Parent

Object

### Methods

- ::new
- #write
- #write_with_crc

### Files

- BENCHMARKING.rdoc
- CHANGELOG.rdoc
- CONTRIBUTING.rdoc
- README.rdoc

### Class/Module Index

Quicksearch

- ChunkyPNG
- ChunkyPNG::CRCMismatch
- ChunkyPNG::Canvas
- ChunkyPNG::Canvas::Adam7Interlacing
- ChunkyPNG::Canvas::DataUrlExporting
- ChunkyPNG::Canvas::DataUrlImporting
- ChunkyPNG::Canvas::Drawing
- ChunkyPNG::Canvas::Masking
- ChunkyPNG::Canvas::Operations
- ChunkyPNG::Canvas::PNGDecoding
- ChunkyPNG::Canvas::PNGEncoding
- ChunkyPNG::Canvas::Resampling
- ChunkyPNG::Canvas::StreamExporting
- ChunkyPNG::Canvas::StreamImporting
- ChunkyPNG::Chunk
- ChunkyPNG::Chunk::Base
- ChunkyPNG::Chunk::CompressedText
- ChunkyPNG::Chunk::End
- ChunkyPNG::Chunk::Generic
- ChunkyPNG::Chunk::Header
- ChunkyPNG::Chunk::ImageData
- ChunkyPNG::Chunk::InternationalText
- ChunkyPNG::Chunk::Palette
- ChunkyPNG::Chunk::Text
- ChunkyPNG::Chunk::Transparency
- ChunkyPNG::Color
- ChunkyPNG::Datastream
- ChunkyPNG::Dimension
- ChunkyPNG::Exception
- ChunkyPNG::ExpectationFailed
- ChunkyPNG::Image
- ChunkyPNG::NotSupported
- ChunkyPNG::OutOfBounds
- ChunkyPNG::Palette
- ChunkyPNG::Point
- ChunkyPNG::RMagick
- ChunkyPNG::SignatureMismatch
- ChunkyPNG::Vector
- Enumerable
- String

No matching classes.

# ChunkyPNG::Chunk::Base

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

### Attributes

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]

### Public Class Methods

new(type, attributes = {}) click to toggle source

Initializes the chunk instance. @param type [String] The four character chunk type indicator. @param attributes [Hash] A hash of attributes to set on this chunk.

    # File lib/chunky_png/chunk.rb, line 67
    def initialize(type, attributes = {})
      self.type = type
      attributes.each { |k, v| send("#{k}=", v) }
    end

### Public Instance Methods

write(io) click to toggle source

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 86
    def write(io)
      write_with_crc(io, content || '')
    end

write_with_crc(io, content) click to toggle source

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 content [String] The content for this chunk.

    # File lib/chunky_png/chunk.rb, line 76
    def write_with_crc(io, content)
      io << [content.length].pack('N') << type << content
      io << [Zlib.crc32(content, Zlib.crc32(type))].pack('N')
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.