Class: ZPNG::Chunk::IHDR
- Inherits:
-
ZPNG::Chunk
- Object
- ZPNG::Chunk
- ZPNG::Chunk::IHDR
- Defined in:
- lib/zpng/chunk.rb
Constant Summary collapse
- PALETTE_USED =
1
- COLOR_USED =
2
- ALPHA_USED =
4
- SAMPLES_PER_COLOR =
Each pixel is an R,G,B triple, followed by an alpha sample.
{ COLOR_GRAYSCALE => 1, COLOR_RGB => 3, COLOR_INDEXED => 1, COLOR_GRAY_ALPHA => 2, COLOR_RGBA => 4 }
- ALLOWED_DEPTHS =
{ COLOR_GRAYSCALE => [ 1, 2, 4, 8, 16 ], COLOR_RGB => [ 8, 16 ], COLOR_INDEXED => [ 1, 2, 4, 8 ], COLOR_GRAY_ALPHA => [ 8, 16 ], COLOR_RGBA => [ 8, 16 ], }
- FORMAT =
'NNC5'
Instance Attribute Summary collapse
-
#color ⇒ Object
Returns the value of attribute color.
-
#compression ⇒ Object
Returns the value of attribute compression.
-
#depth ⇒ Object
Returns the value of attribute depth.
-
#filter ⇒ Object
Returns the value of attribute filter.
-
#height ⇒ Object
Returns the value of attribute height.
-
#interlace ⇒ Object
Returns the value of attribute interlace.
-
#width ⇒ Object
Returns the value of attribute width.
Attributes inherited from ZPNG::Chunk
#crc, #data, #idx, #size, #type
Instance Method Summary collapse
- #alpha_used? ⇒ Boolean
-
#bpp ⇒ Object
bits per pixel.
- #color_used? ⇒ Boolean
- #export_data ⇒ Object
- #grayscale? ⇒ Boolean
-
#initialize(x) ⇒ IHDR
constructor
A new instance of IHDR.
- #inspect(verbosity = 10) ⇒ Object
- #palette_used? ⇒ Boolean
Methods inherited from ZPNG::Chunk
#crc_ok?, #export, from_stream
Methods included from DeepCopyable
Constructor Details
#initialize(x) ⇒ IHDR
Returns a new instance of IHDR.
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/zpng/chunk.rb', line 106 def initialize x super vars = %w'width height depth color compression filter interlace' # order is important if x.respond_to?(:read) # IO elsif x.respond_to?(:[]) # Hash vars.each{ |k| instance_variable_set "@#{k}", x[k.to_sym] } raise "[!] width not set" unless @width raise "[!] height not set" unless @height # allow easier image creation like # img = Image.new :width => 16, :height => 16, :bpp => 4, :color => false # img = Image.new :width => 16, :height => 16, :bpp => 1, :color => true # img = Image.new :width => 16, :height => 16, :bpp => 32 if x[:bpp] unless [true,false,nil].include?(@color) raise "[!] :color must be either 'true' or 'false' when :bpp is set" end if @depth raise "[!] don't use :depth when :bpp is set" end @color, @depth = case x[:bpp] when 1,2,4,8; [ @color ? COLOR_INDEXED : COLOR_GRAYSCALE, x[:bpp] ] when 16; raise "[!] I don't know how to make COLOR 16 bpp PNG. do you?" if @color [ COLOR_GRAY_ALPHA, 8 ] when 24; [ COLOR_RGB, 8 ] when 32; [ COLOR_RGBA, 8 ] else raise "[!] unsupported bpp=#{x[:bpp].inspect}" end end @color ||= COLOR_RGBA @depth ||= 8 @compression ||= 0 @filter ||= 0 @interlace ||= 0 unless ALLOWED_DEPTHS[@color].include?(@depth) raise "[!] invalid color mode (#{@color.inspect}) / bit depth (#{@depth.inspect}) combination" end end if data data.unpack(FORMAT).each_with_index do |value,idx| instance_variable_set "@#{vars[idx]}", value end end end |
Instance Attribute Details
#color ⇒ Object
Returns the value of attribute color.
70 71 72 |
# File 'lib/zpng/chunk.rb', line 70 def color @color end |
#compression ⇒ Object
Returns the value of attribute compression.
70 71 72 |
# File 'lib/zpng/chunk.rb', line 70 def compression @compression end |
#depth ⇒ Object
Returns the value of attribute depth.
70 71 72 |
# File 'lib/zpng/chunk.rb', line 70 def depth @depth end |
#filter ⇒ Object
Returns the value of attribute filter.
70 71 72 |
# File 'lib/zpng/chunk.rb', line 70 def filter @filter end |
#height ⇒ Object
Returns the value of attribute height.
70 71 72 |
# File 'lib/zpng/chunk.rb', line 70 def height @height end |
#interlace ⇒ Object
Returns the value of attribute interlace.
70 71 72 |
# File 'lib/zpng/chunk.rb', line 70 def interlace @interlace end |
#width ⇒ Object
Returns the value of attribute width.
70 71 72 |
# File 'lib/zpng/chunk.rb', line 70 def width @width end |
Instance Method Details
#alpha_used? ⇒ Boolean
179 180 181 |
# File 'lib/zpng/chunk.rb', line 179 def alpha_used? (@color & ALPHA_USED) != 0 end |
#bpp ⇒ Object
bits per pixel
163 164 165 |
# File 'lib/zpng/chunk.rb', line 163 def bpp SAMPLES_PER_COLOR[@color] * depth end |
#color_used? ⇒ Boolean
167 168 169 |
# File 'lib/zpng/chunk.rb', line 167 def color_used? (@color & COLOR_USED) != 0 end |
#export_data ⇒ Object
158 159 160 |
# File 'lib/zpng/chunk.rb', line 158 def export_data [@width, @height, @depth, @color, @compression, @filter, @interlace].pack(FORMAT) end |
#grayscale? ⇒ Boolean
171 172 173 |
# File 'lib/zpng/chunk.rb', line 171 def grayscale? !color_used? end |
#inspect(verbosity = 10) ⇒ Object
183 184 185 186 187 188 189 |
# File 'lib/zpng/chunk.rb', line 183 def inspect verbosity = 10 vars = instance_variables - [:@type, :@crc, :@data, :@size] vars -= [:@idx] if verbosity <= 0 super.sub(/ *>$/,'') + ", " + vars.map{ |var| "#{var.to_s.tr('@','')}=#{instance_variable_get(var)}" }. join(", ") + ">" end |
#palette_used? ⇒ Boolean
175 176 177 |
# File 'lib/zpng/chunk.rb', line 175 def palette_used? (@color & PALETTE_USED) != 0 end |