Class: ZPNG::Chunk

Inherits:
Object
  • Object
show all
Includes:
DeepCopyable
Defined in:
lib/zpng/chunk.rb,
lib/zpng/text_chunk.rb

Direct Known Subclasses

IDAT, IEND, IHDR, PLTE, TRNS, TextChunk

Defined Under Namespace

Classes: IDAT, IEND, IHDR, ITXT, PLTE, TEXT, TRNS, ZTXT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DeepCopyable

#deep_copy

Constructor Details

#initialize(x = {}) ⇒ Chunk

Returns a new instance of Chunk.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/zpng/chunk.rb', line 23

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

#crcObject

Returns the value of attribute crc.



3
4
5
# File 'lib/zpng/chunk.rb', line 3

def crc
  @crc
end

#dataObject

Returns the value of attribute data.



3
4
5
# File 'lib/zpng/chunk.rb', line 3

def data
  @data
end

#idxObject

Returns the value of attribute idx.



3
4
5
# File 'lib/zpng/chunk.rb', line 3

def idx
  @idx
end

#sizeObject

Returns the value of attribute size.



3
4
5
# File 'lib/zpng/chunk.rb', line 3

def size
  @size
end

#typeObject

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



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/zpng/chunk.rb', line 7

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

Returns:

  • (Boolean)


64
65
66
67
# File 'lib/zpng/chunk.rb', line 64

def crc_ok?
  expected_crc = Zlib.crc32(data, Zlib.crc32(type))
  expected_crc == crc
end

#exportObject



45
46
47
48
49
50
# File 'lib/zpng/chunk.rb', line 45

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_dataObject



52
53
54
55
# File 'lib/zpng/chunk.rb', line 52

def export_data
  #STDERR.puts "[!] Chunk::#{type} must realize 'export_data' virtual method".yellow if @size != 0
  @data || ''
end

#inspect(verbosity = 10) ⇒ Object



57
58
59
60
61
62
# File 'lib/zpng/chunk.rb', line 57

def inspect verbosity = 10
  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