lib/chunky_png/datastream.rb in chunky_png-1.3.11 vs lib/chunky_png/datastream.rb in chunky_png-1.3.12
- old
+ new
@@ -1,19 +1,17 @@
module ChunkyPNG
-
# 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
class Datastream
-
# The signature that each PNG file or stream should begin with.
- SIGNATURE = ChunkyPNG.force_binary([137, 80, 78, 71, 13, 10, 26, 10].pack('C8'))
+ SIGNATURE = [137, 80, 78, 71, 13, 10, 26, 10].pack("C8").force_encoding(Encoding::BINARY).freeze
# The header chunk of this datastream.
# @return [ChunkyPNG::Chunk::Header]
attr_accessor :header_chunk
@@ -50,49 +48,49 @@
##############################################################################
# LOADING DATASTREAMS
##############################################################################
class << self
-
# Reads a PNG datastream from a string.
# @param [String] str The PNG encoded string to load from.
# @return [ChunkyPNG::Datastream] The loaded datastream instance.
def from_blob(str)
- from_io(StringIO.new(str))
+ from_io(StringIO.new(str, "rb"))
end
- alias :from_string :from_blob
+ alias from_string from_blob
# 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.
def from_file(filename)
ds = nil
- File.open(filename, 'rb') { |f| ds = from_io(f) }
+ File.open(filename, "rb") { |f| ds = from_io(f) }
ds
end
# Reads a PNG datastream from an input stream
# @param [IO] io The stream to read from.
# @return [ChunkyPNG::Datastream] The loaded datastream instance.
def from_io(io)
+ io.set_encoding(Encoding::BINARY)
verify_signature!(io)
- ds = self.new
+ ds = new
while ds.end_chunk.nil?
chunk = ChunkyPNG::Chunk.read(io)
case chunk
- when ChunkyPNG::Chunk::Header; ds.header_chunk = chunk
- when ChunkyPNG::Chunk::Palette; ds.palette_chunk = chunk
- when ChunkyPNG::Chunk::Transparency; ds.transparency_chunk = chunk
- when ChunkyPNG::Chunk::ImageData; ds.data_chunks << chunk
- when ChunkyPNG::Chunk::Physical; ds.physical_chunk = chunk
- when ChunkyPNG::Chunk::End; ds.end_chunk = 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
- return ds
+ ds
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
@@ -101,11 +99,11 @@
# @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.
def verify_signature!(io)
signature = io.read(ChunkyPNG::Datastream::SIGNATURE.length)
- unless ChunkyPNG.force_binary(signature) == ChunkyPNG::Datastream::SIGNATURE
+ unless signature == ChunkyPNG::Datastream::SIGNATURE
raise ChunkyPNG::SignatureMismatch, "PNG signature not found, found #{signature.inspect} instead of #{ChunkyPNG::Datastream::SIGNATURE.inspect}!"
end
end
end
@@ -125,11 +123,11 @@
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) }
+ data_chunks.each { |chunk| yield(chunk) }
yield(end_chunk)
end
# Returns an enumerator instance for this datastream's chunks.
# @return [Enumerable::Enumerator] An enumerator for the :each_chunk method.
@@ -172,21 +170,21 @@
end
# Saves this datastream as a PNG file.
# @param [String] filename The filename to use.
def save(filename)
- File.open(filename, 'wb') { |f| write(f) }
+ File.open(filename, "wb") { |f| write(f) }
end
# Encodes this datastream into a string.
# @return [String] The encoded PNG datastream.
def to_blob
str = StringIO.new
- str.set_encoding('ASCII-8BIT')
+ str.set_encoding("ASCII-8BIT")
write(str)
- return str.string
+ str.string
end
- alias :to_string :to_blob
- alias :to_s :to_blob
+ alias to_string to_blob
+ alias to_s to_blob
end
end