Class: ZIMG::PNG::Chunk::IHDR

Inherits:
ZIMG::PNG::Chunk show all
Defined in:
lib/zimg/png/chunks.rb

Direct Known Subclasses

BMP::BmpHdrPseudoChunk

Constant Summary collapse

SIZE =
13
FORMAT =
"NNC5"
PALETTE_USED =
1
COLOR_USED =
2
ALPHA_USED =
4
SAMPLES_PER_COLOR =
{
  COLOR_GRAYSCALE => 1,
  COLOR_RGB => 3,
  COLOR_INDEXED => 1,
  COLOR_GRAY_ALPHA => 2,
  COLOR_RGBA => 4,
}.freeze
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],
}.freeze

Constants inherited from ZIMG::PNG::Chunk

KNOWN_TYPES, VALID_SIZE_RANGE

Instance Attribute Summary collapse

Attributes inherited from ZIMG::PNG::Chunk

#crc, #data, #idx, #offset, #size, #type

Instance Method Summary collapse

Methods inherited from ZIMG::PNG::Chunk

#check, #crc_ok?, #export, #fix_crc!, from_stream, #valid?

Methods included from DeepCopyable

#deep_copy

Methods inherited from Chunk

#cli_color

Constructor Details

#initialize(x) ⇒ IHDR

Returns a new instance of IHDR.



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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/zimg/png/chunks.rb', line 132

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
      raise "[!] don't use :depth when :bpp is set" if @depth

      @color, @depth = case x[:bpp]
                       when 1, 2, 4, 8 then [@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 then      [COLOR_RGB,  8]
                       when 32 then      [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
  return unless data

  data.unpack(FORMAT).each_with_index do |value, idx|
    instance_variable_set "@#{vars[idx]}", value
  end
end

Instance Attribute Details

#colorObject

Returns the value of attribute color.



106
107
108
# File 'lib/zimg/png/chunks.rb', line 106

def color
  @color
end

#compressionObject

Returns the value of attribute compression.



106
107
108
# File 'lib/zimg/png/chunks.rb', line 106

def compression
  @compression
end

#depthObject

Returns the value of attribute depth.



106
107
108
# File 'lib/zimg/png/chunks.rb', line 106

def depth
  @depth
end

#filterObject

Returns the value of attribute filter.



106
107
108
# File 'lib/zimg/png/chunks.rb', line 106

def filter
  @filter
end

#heightObject

Returns the value of attribute height.



106
107
108
# File 'lib/zimg/png/chunks.rb', line 106

def height
  @height
end

#interlaceObject

Returns the value of attribute interlace.



106
107
108
# File 'lib/zimg/png/chunks.rb', line 106

def interlace
  @interlace
end

#widthObject

Returns the value of attribute width.



106
107
108
# File 'lib/zimg/png/chunks.rb', line 106

def width
  @width
end

Instance Method Details

#alpha_used?Boolean

Returns:

  • (Boolean)


206
207
208
# File 'lib/zimg/png/chunks.rb', line 206

def alpha_used?
  (@color & ALPHA_USED) != 0
end

#bppObject

bits per pixel



189
190
191
192
# File 'lib/zimg/png/chunks.rb', line 189

def bpp
  spc = SAMPLES_PER_COLOR[@color]
  spc ? spc * depth : nil
end

#color_used?Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/zimg/png/chunks.rb', line 194

def color_used?
  (@color & COLOR_USED) != 0
end

#export_dataObject



184
185
186
# File 'lib/zimg/png/chunks.rb', line 184

def export_data
  [@width, @height, @depth, @color, @compression, @filter, @interlace].pack(FORMAT)
end

#grayscale?Boolean

Returns:

  • (Boolean)


198
199
200
# File 'lib/zimg/png/chunks.rb', line 198

def grayscale?
  !color_used?
end

#inspect(verbosity = 10) ⇒ Object



210
211
212
213
214
215
# File 'lib/zimg/png/chunks.rb', line 210

def inspect(verbosity = 10)
  vars = instance_variables - %i[@type @crc @data @size]
  vars -= [:@idx] if verbosity <= 0
  info = vars.map { |var| ", #{var.to_s.tr("@", "")}=#{instance_variable_get(var)}" }.join
  "#{super.chop.rstrip}#{info} >"
end

#palette_used?Boolean

Returns:

  • (Boolean)


202
203
204
# File 'lib/zimg/png/chunks.rb', line 202

def palette_used?
  (@color & PALETTE_USED) != 0
end