Sha256: df18d053e4d789fd0e9ba5dd717e953e0e336e10b3ea12ca150f358d96712c6b

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

module ChunkyPNG
  
  class Datastream
    
    SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10].pack('C8')
    
    attr_accessor :header_chunk
    attr_accessor :other_chunks
    attr_accessor :palette_chunk
    attr_accessor :transparency_chunk
    attr_accessor :data_chunks
    attr_accessor :end_chunk

    def self.read(io)
      verify_signature!(io)

      ds = self.new
      until io.eof?
        chunk = ChunkyPNG::Chunk.read(io)
        case chunk
          when ChunkyPNG::Chunk::Header       then ds.header_chunk        = chunk
          when ChunkyPNG::Chunk::Palette      then ds.palette_chunk       = chunk
          when ChunkyPNG::Chunk::Transparency then ds.transparency_chunk  = chunk
          when ChunkyPNG::Chunk::ImageData    then ds.data_chunks        << chunk
          when ChunkyPNG::Chunk::End          then ds.end_chunk           = chunk
          else ds.other_chunks << chunk
        end
      end
      return ds
    end

    def self.verify_signature!(io)
      signature = io.read(ChunkyPNG::Datastream::SIGNATURE.length)
      raise "PNG signature not found!" unless signature == ChunkyPNG::Datastream::SIGNATURE
    end
    
    def chunks
      cs = [header_chunk]
      cs += other_chunks
      cs << palette_chunk      if palette_chunk
      cs << transparency_chunk if transparency_chunk
      cs += data_chunks
      cs << end_chunk
      return cs
    end
    
    def initialize
      @other_chunks = []
      @data_chunks  = []
    end
    
    def write(io)
      io << SIGNATURE
      chunks.each { |c| c.write(io) }
    end
    
    def idat_chunks(data)
      streamdata = Zlib::Deflate.deflate(data)
      # TODO: Split long streamdata over multiple chunks
      return [ ChunkyPNG::Chunk::ImageData.new('IDAT', streamdata) ]
    end
    
    def pixel_matrix=(pixel_matrix)
      @pixel_matrix = pixel_matrix
    end
    
    def pixel_matrix
      @pixel_matrix ||= ChunkyPNG::PixelMatrix.decode(self)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
chunky_png-0.0.5 lib/chunky_png/datastream.rb
chunky_png-0.0.4 lib/chunky_png/datastream.rb
chunky_png-0.0.3 lib/chunky_png/datastream.rb
chunky_png-0.0.2 lib/chunky_png/datastream.rb