class ChunkyPNG::Datastream
@see ChunkyPNG::Chunk
(IEND) chunk.
chunks, starting with a header (IHDR) chunk and finishing with an end
A PNG datastream begins with the PNG signature, and than contains multiple
both reading from and writing to strings, stremas and files.
The Datastream class represents a PNG formatted datastream. It supports
def chunks
- See: ChunkyPNG::Datastream#each_chunk -
Returns:
-
(Enumerable::Enumerator)
- An enumerator for the :each_chunk method.
def chunks enum_for(:each_chunk) end
def each_chunk
- See: ChunkyPNG::Datastream#chunks -
Other tags:
- Yieldparam: chunk - A chunk in this datastream.
Other tags:
- Yield: - Yields the chunks in this datastrean, one by one in the correct order.
def each_chunk yield(header_chunk) other_chunks.each { |chunk| yield(chunk) } yield(palette_chunk) if palette_chunk yield(transparency_chunk) if transparency_chunk data_chunks.each { |chunk| yield(chunk) } yield(end_chunk) end
def from_blob(str)
-
(ChunkyPNG::Datastream)
- The loaded datastream instance.
Parameters:
-
str
(String
) -- The PNG encoded string to load from.
def from_blob(str) from_io(StringIO.new(str)) end
def from_file(filename)
-
(ChunkyPNG::Datastream)
- The loaded datastream instance.
Parameters:
-
filename
(String
) -- The path of the file to load from.
def from_file(filename) ds = nil File.open(filename, 'rb') { |f| ds = from_io(f) } ds end
def from_io(io)
-
(ChunkyPNG::Datastream)
- The loaded datastream instance.
Parameters:
-
io
(IO
) -- The stream to read from.
def from_io(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 initialize
def initialize @other_chunks = [] @data_chunks = [] end
def metadata
def metadata metadata = {} other_chunks.select do |chunk| metadata[chunk.keyword] = chunk.value if chunk.respond_to?(:keyword) end metadata end
def save(filename)
-
filename
(String
) -- The filename to use.
def save(filename) File.open(filename, 'wb') { |f| write(f) } end
def to_blob
-
(String)
- The encoded PNG datastream.
def to_blob str = StringIO.new write(str) return str.string end
def verify_signature!(io)
-
(RuntimeError)
- An exception is raised if the PNG signature is not found at
def verify_signature!(io) signature = io.read(ChunkyPNG::Datastream::SIGNATURE.length) raise "PNG signature not found!" unless signature == ChunkyPNG::Datastream::SIGNATURE end
def write(io)
-
io
(IO
) -- The output stream to write to.
def write(io) io << SIGNATURE each_chunk { |c| c.write(io) } end