Class: ZPNG::Chunk
- Inherits:
-
Object
- Object
- ZPNG::Chunk
- Defined in:
- lib/zpng/chunk.rb
Defined Under Namespace
Classes: IDAT, IEND, IHDR, PLTE, ZTXT
Instance Attribute Summary collapse
-
#crc ⇒ Object
Returns the value of attribute crc.
-
#data ⇒ Object
Returns the value of attribute data.
-
#idx ⇒ Object
Returns the value of attribute idx.
-
#size ⇒ Object
Returns the value of attribute size.
-
#type ⇒ Object
Returns the value of attribute type.
Class Method Summary collapse
Instance Method Summary collapse
- #crc_ok? ⇒ Boolean
- #export ⇒ Object
- #export_data ⇒ Object
-
#initialize(x = {}) ⇒ Chunk
constructor
A new instance of Chunk.
- #inspect ⇒ Object
Constructor Details
#initialize(x = {}) ⇒ Chunk
Returns a new instance of Chunk.
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/zpng/chunk.rb', line 21 def initialize x = {} if x.respond_to?(:read) # IO @size, @type = x.read(8).unpack('Na4') @data = x.read(size) @crc = x.read(4).to_s.unpack('N').first elsif x.respond_to?(:[]) # Hash %w'size type data crc'.each do |k| instance_variable_set "@#{k}", x[k.to_sym] end if !@type && self.class.superclass == ZPNG::Chunk # guess @type from self class name, e.g. ZPNG::Chunk::IHDR => "IHDR" @type = self.class.to_s.split("::").last end if !@size && @data # guess @size from @data @size = @data.size end end end |
Instance Attribute Details
#crc ⇒ Object
Returns the value of attribute crc.
3 4 5 |
# File 'lib/zpng/chunk.rb', line 3 def crc @crc end |
#data ⇒ Object
Returns the value of attribute data.
3 4 5 |
# File 'lib/zpng/chunk.rb', line 3 def data @data end |
#idx ⇒ Object
Returns the value of attribute idx.
3 4 5 |
# File 'lib/zpng/chunk.rb', line 3 def idx @idx end |
#size ⇒ Object
Returns the value of attribute size.
3 4 5 |
# File 'lib/zpng/chunk.rb', line 3 def size @size end |
#type ⇒ Object
Returns the value of attribute type.
3 4 5 |
# File 'lib/zpng/chunk.rb', line 3 def type @type end |
Class Method Details
.from_stream(io) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'lib/zpng/chunk.rb', line 5 def self.from_stream io size, type = io.read(8).unpack('Na4') io.seek(-8,IO::SEEK_CUR) begin if const_defined?(type.upcase) klass = const_get(type.upcase) klass.new(io) else Chunk.new(io) end rescue NameError # invalid chunk type? Chunk.new(io) end end |
Instance Method Details
#crc_ok? ⇒ Boolean
62 63 64 65 |
# File 'lib/zpng/chunk.rb', line 62 def crc_ok? expected_crc = Zlib.crc32(data, Zlib.crc32(type)) expected_crc == crc end |
#export ⇒ Object
43 44 45 46 47 48 |
# File 'lib/zpng/chunk.rb', line 43 def export @data = self.export_data # virtual @size = @data.size # XXX hmm.. is it always is? @crc = Zlib.crc32(data, Zlib.crc32(type)) [@size,@type].pack('Na4') + @data + [@crc].pack('N') end |
#export_data ⇒ Object
50 51 52 53 |
# File 'lib/zpng/chunk.rb', line 50 def export_data #STDERR.puts "[!] Chunk::#{type} must realize 'export_data' virtual method".yellow if @size != 0 @data || '' end |
#inspect ⇒ Object
55 56 57 58 59 60 |
# File 'lib/zpng/chunk.rb', line 55 def inspect size = @size ? sprintf("%6d",@size) : sprintf("%6s","???") crc = @crc ? sprintf("%08x",@crc) : sprintf("%8s","???") type = @type.to_s.gsub(/[^0-9a-z]/i){ |x| sprintf("\\x%02X",x.ord) } sprintf "<Chunk #%02d %4s size=%s, crc=%s >", idx.to_i, type, size, crc end |