Module: ChunkyPNG::Chunk

### Home Classes Methods

### In Files

- lib/chunky_png/chunk.rb

### Namespace

- CLASS ChunkyPNG::Chunk::Base
- CLASS ChunkyPNG::Chunk::CompressedText
- CLASS ChunkyPNG::Chunk::End
- CLASS ChunkyPNG::Chunk::Generic
- CLASS ChunkyPNG::Chunk::Header
- CLASS ChunkyPNG::Chunk::ImageData
- CLASS ChunkyPNG::Chunk::InternationalText
- CLASS ChunkyPNG::Chunk::Palette
- CLASS ChunkyPNG::Chunk::Text
- CLASS ChunkyPNG::Chunk::Transparency

### Methods

- ::read
- ::read_bytes
- ::verify_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

A PNG datastream consists of multiple chunks. This module, and the classes contained within, help with handling these chunks. It supports both reading and writing chunks.

All chunk types are instances of the {ChunkyPNG::Chunk::Base} class. For some chunk types a specialized class is available, e.g. the IHDR chunk is represented by the {ChunkyPNG::Chunk::Header} class. These specialized classes help accessing the content of the chunk. All other chunks are represented by the {ChunkyPNG::Chunk::Generic} class.

@see ChunkyPNG::Datastream

### Constants

CHUNK_TYPES

  Maps chunk types to classes, based on the four byte chunk type indicator at the beginning of a chunk.

  If a chunk type is not specified in this hash, the Generic chunk type will be used.

  @see ChunkyPNG::Chunk.read

### Public Class Methods

read(io) click to toggle source

Reads a chunk from an IO stream.

@param io [IO, read] The IO stream to read from. @return [ChunkyPNG::Chung::Base] The loaded chunk instance.

    # File lib/chunky_png/chunk.rb, line 18
    def self.read(io)
      length, type = read_bytes(io, 8).unpack('Na4')

      content = read_bytes(io, length)
      crc     = read_bytes(io, 4).unpack('N').first
      verify_crc!(type, content, crc)

      CHUNK_TYPES.fetch(type, Generic).read(type, content)
    end

read_bytes(io, length) click to toggle source

Reads an exact number of bytes from an IO stream. @param io [IO, read] The IO stream to read from. @param length [Integer] The IO exact number of bytes to read. @return [String] A binary string of exactly length bytes. @raise [ChunkyPNG::ExpectationFailed] If not exactly length

    bytes could be read from the IO stream.

    # File lib/chunky_png/chunk.rb, line 34
    def self.read_bytes(io, length)
      data = io.read(length)
      raise ExpectationFailed, "Couldn't read #{length} bytes from IO stream." if data.nil? || data.bytesize != length
      data
    end

verify_crc!(type, content, found_crc) click to toggle source

Verifies the CRC of a chunk. @param type [String] The chunk's type. @param content [String] The chunk's content. @param found_crc [Integer] The chunk's found CRC value. @raise [ChunkyPNG::CRCMismatch] An exception is raised if

    the found CRC value is not equal to the expected CRC value.

    # File lib/chunky_png/chunk.rb, line 46
    def self.verify_crc!(type, content, found_crc)
      expected_crc = Zlib.crc32(content, Zlib.crc32(type))
      raise ChunkyPNG::CRCMismatch, "Chuck CRC mismatch!" if found_crc != expected_crc
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.