Class: Cul::Image::Properties::Jpeg

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) ⇒ Jpeg

Returns a new instance of Jpeg.



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
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/cul_image_props/image/properties/types.rb', line 145

def initialize(srcfile=nil)
  super
  header_bytes = @src.read(2)
  unless header_bytes[0...2].unpack('C*') == Cul::Image::Magic::JPEG
    raise "Source file is not a jpeg: #{hex_inspect(header_bytes[0...2])}"
  end
  xpix = 0
  ypix = 0
  while (!@src.eof?)
    if 0xff == ord_value(@src.read(1))
      mrkr = [0xff, ord_value(@src.read(1))]
      blen = @src.read(2).unpack('n')[0]
      if Cul::Image::Magic::JFM_BYTES.include? mrkr  # SOFn, Start of frame for scans
        @src.read(1) #skip bits per sample
        self.length= @src.read(2).unpack('n')[0]
        self.width= @src.read(2).unpack('n')[0]
        @src.seek(0, IO::SEEK_END)
      else
        @src.seek(blen - 2, IO::SEEK_CUR)
      end
    else
      @src.seek(0, IO::SEEK_END)
    end
  end

  @src.rewind
  tags = Cul::Image::Properties::Exif.process_file(@src)
  if tags.include? 'Image ImageWidth'
    self.width= tags['Image ImageWidth'].values[0]
  end
  if tags.include? 'Image ImageLength'
    self.length= tags['Image ImageLength'].values[0]
  end
  if tags.include? 'Image XResolution'
    self.x_sampling_freq= tags['Image XResolution'].values[0]
  end
  if tags.include? 'Image YResolution'
    self.y_sampling_freq= tags['Image YResolution'].values[0]
  end
  if tags.include? 'Image ResolutionUnit'
    if (tags['Image ResolutionUnit'].values[0] == 3)
      self.sampling_unit=3
    elsif (tags['Image ResolutionUnit'].values[0] == 2)
      self.sampling_unit=2
    else
      self.sampling_unit=1
    end
  end
  self.extent= srcfile.stat.size unless srcfile.nil?
  self.format= MIME[:jpg]
end