class ChunkyPNG::Datastream
The Datastream
class represents a PNG formatted datastream. It supports both reading from and writing to strings, streams and files.
A PNG datastream begins with the PNG signature, and then contains multiple chunks, starting with a header (IHDR) chunk and finishing with an end (IEND) chunk.
@see ChunkyPNG::Chunk
Constants
- SIGNATURE
The signature that each PNG file or stream should begin with.
Attributes
The chunks that together compose the images pixel data. @return [Array<ChunkyPNG::Chunk::ImageData>]
The empty chunk that signals the end of this datastream @return [ChunkyPNG::Chunk::Header]
The header chunk of this datastream. @return [ChunkyPNG::Chunk::Header]
All other chunks in this PNG file. @return [Array<ChunkyPNG::Chunk::Generic>]
The chunk containing the image’s palette. @return [ChunkyPNG::Chunk::Palette]
The chunk containing the physical dimensions of the PNG’s pixels. @return [ChunkyPNG::Chunk::Physical]
The chunk containing the transparency information of the palette. @return [ChunkyPNG::Chunk::Transparency]
Public Class Methods
Reads a PNG datastream from a string. @param [String] str The PNG encoded string to load from. @return [ChunkyPNG::Datastream] The loaded datastream instance.
# File lib/chunky_png/datastream.rb 58 def from_blob(str) 59 from_io(StringIO.new(str, "rb")) 60 end
Reads a PNG datastream from a file. @param [String] filename The path of the file to load from. @return [ChunkyPNG::Datastream] The loaded datastream instance.
# File lib/chunky_png/datastream.rb 67 def from_file(filename) 68 ds = nil 69 File.open(filename, "rb") { |f| ds = from_io(f) } 70 ds 71 end
Reads a PNG datastream from an input stream @param [IO] io The stream to read from. @return [ChunkyPNG::Datastream] The loaded datastream instance.
# File lib/chunky_png/datastream.rb 76 def from_io(io) 77 io.set_encoding(::Encoding::BINARY) 78 verify_signature!(io) 79 80 ds = new 81 while ds.end_chunk.nil? 82 chunk = ChunkyPNG::Chunk.read(io) 83 case chunk 84 when ChunkyPNG::Chunk::Header then ds.header_chunk = chunk 85 when ChunkyPNG::Chunk::Palette then ds.palette_chunk = chunk 86 when ChunkyPNG::Chunk::Transparency then ds.transparency_chunk = chunk 87 when ChunkyPNG::Chunk::ImageData then ds.data_chunks << chunk 88 when ChunkyPNG::Chunk::Physical then ds.physical_chunk = chunk 89 when ChunkyPNG::Chunk::End then ds.end_chunk = chunk 90 else ds.other_chunks << chunk 91 end 92 end 93 ds 94 end
Initializes a new Datastream
instance.
# File lib/chunky_png/datastream.rb 45 def initialize 46 @other_chunks = [] 47 @data_chunks = [] 48 end
Verifies that the current stream is a PNG datastream by checking its signature.
This method reads the PNG signature from the stream, setting the current position of the stream directly after the signature, where the IHDR chunk should begin.
@param [IO] io The stream to read the PNG signature from. @raise [RuntimeError] An exception is raised if the PNG signature is not found at
the beginning of the stream.
# File lib/chunky_png/datastream.rb 104 def verify_signature!(io) 105 signature = io.read(ChunkyPNG::Datastream::SIGNATURE.length) 106 unless signature == ChunkyPNG::Datastream::SIGNATURE 107 raise ChunkyPNG::SignatureMismatch, "PNG signature not found, found #{signature.inspect} instead of #{ChunkyPNG::Datastream::SIGNATURE.inspect}!" 108 end 109 end
Public Instance Methods
Returns an enumerator instance for this datastream’s chunks. @return [Enumerable::Enumerator] An enumerator for the :each_chunk method. @see ChunkyPNG::Datastream#each_chunk
# File lib/chunky_png/datastream.rb 137 def chunks 138 enum_for(:each_chunk) 139 end
Enumerates the chunks in this datastream.
This will iterate over the chunks using the order in which the chunks should appear in the PNG file.
@yield [chunk] Yields the chunks in this datastream, one by one in the correct order. @yieldparam [ChunkyPNG::Chunk::Base] chunk A chunk in this datastream. @see ChunkyPNG::Datastream#chunks
# File lib/chunky_png/datastream.rb 124 def each_chunk 125 yield(header_chunk) 126 other_chunks.each { |chunk| yield(chunk) } 127 yield(palette_chunk) if palette_chunk 128 yield(transparency_chunk) if transparency_chunk 129 yield(physical_chunk) if physical_chunk 130 data_chunks.each { |chunk| yield(chunk) } 131 yield(end_chunk) 132 end
Returns the uncompressed image data, combined from all the IDAT chunks @return [String] The uncompressed image data for this datastream
# File lib/chunky_png/datastream.rb 153 def imagedata 154 ChunkyPNG::Chunk::ImageData.combine_chunks(data_chunks) 155 end
Returns all the textual metadata key/value pairs as hash. @return [Hash] A hash containing metadata fields and their values.
# File lib/chunky_png/datastream.rb 143 def metadata 144 metadata = {} 145 other_chunks.each do |chunk| 146 metadata[chunk.keyword] = chunk.value if chunk.respond_to?(:keyword) && chunk.respond_to?(:value) 147 end 148 metadata 149 end
Saves this datastream as a PNG file. @param [String] filename The filename to use.
# File lib/chunky_png/datastream.rb 170 def save(filename) 171 File.open(filename, "wb") { |f| write(f) } 172 end
Encodes this datastream into a string. @return [String] The encoded PNG datastream.
# File lib/chunky_png/datastream.rb 176 def to_blob 177 str = StringIO.new 178 str.set_encoding("ASCII-8BIT") 179 write(str) 180 str.string 181 end
Writes the datastream to the given output stream. @param [IO] io The output stream to write to.
# File lib/chunky_png/datastream.rb 163 def write(io) 164 io << SIGNATURE 165 each_chunk { |c| c.write(io) } 166 end