Class: Cul::Image::Properties::Png

Inherits:
Base
  • Object
show all
Defined in:
lib/cul_image_props/image/properties/types.rb

Constant Summary

Constants inherited from Base

Base::BASE_XML

Instance Attribute Summary

Attributes inherited from Base

#nodeset

Instance Method Summary collapse

Methods inherited from Base

#[], #add_dt_prop, #extent=, #format=, #hex_inspect, #inspect, #length=, #ord_value, #sampling_unit=, #width=, #x_sampling_freq=, #y_sampling_freq=

Constructor Details

#initialize(srcfile = nil) ⇒ Png

Returns a new instance of Png.



199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/cul_image_props/image/properties/types.rb', line 199

def initialize(srcfile=nil)
  super
  header_bytes = @src.read(8)
  unless header_bytes[0...8].unpack('C*') == Cul::Image::Magic::PNG
    raise "Source file is not a png #{hex_inspect(header_bytes[0...8])}"
  end
  until @src.eof?
    clen = @src.read(4).unpack('N')[0]
    ctype = @src.read(4)
    case ctype
    when 'pHYs'
      pHYs(clen)
    when 'IHDR'
      IHDR(clen)
    when 'tEXt'
      tEXt(clen)
    when 'IEND'
      IEND(clen)
    else
      @src.seek(clen+4, IO::SEEK_CUR)
    end
  end
  self.extent= srcfile.stat.size unless srcfile.nil?
  self.format=MIME[:png]
end

Instance Method Details

#IEND(len) ⇒ Object



249
250
251
# File 'lib/cul_image_props/image/properties/types.rb', line 249

def IEND(len)
  @src.seek(0, IO::SEEK_END)
end

#IHDR(len) ⇒ Object



240
241
242
243
244
245
# File 'lib/cul_image_props/image/properties/types.rb', line 240

def IHDR(len)
  val = @src.read(8)
  self.width= val[0,4].unpack('N')[0]
  self.length= val[4,4].unpack('N')[0]
  @src.seek(len - 4, IO::SEEK_CUR) # remaining block + end tag
end

#pHYs(len) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/cul_image_props/image/properties/types.rb', line 224

def pHYs(len)
  val = @src.read(9)
  xres = val[0,4].unpack('N')[0]
  yres = val[4,4].unpack('N')[0]
  unit = ord_value(val[8])
  if unit == 1 # resolution unit is METER
    xres = (xres / 100).ceil
    yres = (yres / 100).ceil
    self.sampling_unit=3
  else
    self.sampling_unit=1
  end
  self.x_sampling_freq= xres
  self.y_sampling_freq= yres
  @src.seek(len - 5, IO::SEEK_CUR) # remaining block + end tag
end

#tEXt(len) ⇒ Object



246
247
248
# File 'lib/cul_image_props/image/properties/types.rb', line 246

def tEXt(len)
  @src.seek(len + 4, IO::SEEK_CUR) # remaining block + end tag
end