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 then contains multiple
both reading from and writing to strings, streams 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 datastream, 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 yield(physical_chunk) if physical_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, "rb")) 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) io.set_encoding(::Encoding::BINARY) verify_signature!(io) ds = new while ds.end_chunk.nil? 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::Physical then ds.physical_chunk = chunk when ChunkyPNG::Chunk::End then ds.end_chunk = chunk else ds.other_chunks << chunk end end ds end
def imagedata
-
(String)
- The uncompressed image data for this datastream
def imagedata ChunkyPNG::Chunk::ImageData.combine_chunks(data_chunks) end
def initialize
def initialize @other_chunks = [] @data_chunks = [] end
def metadata
-
(Hash)
- A hash containing metadata fields and their values.
def metadata metadata = {} other_chunks.each do |chunk| metadata[chunk.keyword] = chunk.value if chunk.respond_to?(:keyword) && chunk.respond_to?(:value) 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 str.set_encoding("ASCII-8BIT") write(str) str.string end
def verify_signature!(io)
-
(RuntimeError)
- An exception is raised if the PNG signature is not found at
Parameters:
-
io
(IO
) -- The stream to read the PNG signature from.
def verify_signature!(io) signature = io.read(ChunkyPNG::Datastream::SIGNATURE.length) unless signature == ChunkyPNG::Datastream::SIGNATURE raise ChunkyPNG::SignatureMismatch, "PNG signature not found, found #{signature.inspect} instead of #{ChunkyPNG::Datastream::SIGNATURE.inspect}!" end end
def write(io)
-
io
(IO
) -- The output stream to write to.
def write(io) io << SIGNATURE each_chunk { |c| c.write(io) } end