Class: ChunkyPNG::Chunk::Header

Inherits:
Base
  • Object
show all
Defined in:
lib/chunky_png/chunk.rb

Overview

The header (IHDR) chunk is the first chunk of every PNG image, and contains information about the image: i.e. its width, height, color depth, color mode, compression method, filtering method and interlace method.

ChunkyPNG supports all values for these variables that are defined in the PNG spec, except for color depth: Only 8-bit depth images are supported. Note that it is still possible to access the chunk for such an image, but ChunkyPNG will raise an exception if you try to access the pixel data.

Instance Attribute Summary collapse

Attributes inherited from Base

#type

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#write, #write_with_crc

Constructor Details

#initialize(attrs = {}) ⇒ Header

Returns a new instance of Header.



112
113
114
115
116
117
118
119
# File 'lib/chunky_png/chunk.rb', line 112

def initialize(attrs = {})
  super('IHDR', attrs)
  @depth       ||= 8
  @color       ||= ChunkyPNG::COLOR_TRUECOLOR
  @compression ||= ChunkyPNG::COMPRESSION_DEFAULT
  @filtering   ||= ChunkyPNG::FILTERING_DEFAULT
  @interlace   ||= ChunkyPNG::INTERLACING_NONE
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



109
110
111
# File 'lib/chunky_png/chunk.rb', line 109

def color
  @color
end

#compressionObject

Returns the value of attribute compression.



109
110
111
# File 'lib/chunky_png/chunk.rb', line 109

def compression
  @compression
end

#depthObject

Returns the value of attribute depth.



109
110
111
# File 'lib/chunky_png/chunk.rb', line 109

def depth
  @depth
end

#filteringObject

Returns the value of attribute filtering.



109
110
111
# File 'lib/chunky_png/chunk.rb', line 109

def filtering
  @filtering
end

#heightObject

Returns the value of attribute height.



109
110
111
# File 'lib/chunky_png/chunk.rb', line 109

def height
  @height
end

#interlaceObject

Returns the value of attribute interlace.



109
110
111
# File 'lib/chunky_png/chunk.rb', line 109

def interlace
  @interlace
end

#widthObject

Returns the value of attribute width.



109
110
111
# File 'lib/chunky_png/chunk.rb', line 109

def width
  @width
end

Class Method Details

.read(type, content) ⇒ ChunkyPNG::Chunk::End

Reads the 13 bytes of content from the header chunk to set the image attributes.

Parameters:

  • type (String)

    The four character chunk type indicator (= “IHDR”).

  • content (String)

    The 13 bytes of content read from the chunk.

Returns:

  • (ChunkyPNG::Chunk::End)

    The new Header chunk instance with the variables set to the values according to the content.



127
128
129
130
131
132
133
134
135
136
# File 'lib/chunky_png/chunk.rb', line 127

def self.read(type, content)
  fields = content.unpack('NNC5')
  new(:width => fields[0],
      :height => fields[1],
      :depth => fields[2],
      :color => fields[3],
      :compression => fields[4],
      :filtering => fields[5],
      :interlace => fields[6])
end

Instance Method Details

#contentString

Returns the content for this chunk when it gets written to a file, by packing the image information variables into the correct format.

Returns:

  • (String)

    The 13-byte content for the header chunk.



141
142
143
144
# File 'lib/chunky_png/chunk.rb', line 141

def content
  [width, height, depth, color, compression, filtering, interlace].
    pack('NNC5')
end