exif.rb
# Detect if thumbnail has been found and define function to extract
# thumbnail if available
Detect if thumbnail has been found and define function to extract
thumbnail if available
Detect if thumbnail has been found and define function to extract thumbnail if available
def handle_embedded
      data = [["ThumbnailOffset","ThumbnailLength"],["PreviewImageStart","PreviewImageLength"]]
      data.map{|offset, length|
        @@DATA.map do |k,v|
          if v.has_key?(offset)
            ext = ".jpeg"
            case k
            when "IFD0"
              kind = "small_preview"
            when "IFD1"
              kind = "thumbnail"
            when "IFD2"
              kind = "rgb_uncompress_preview"
              ext = ".raw"
            when "IFD3"
              kind = "lossless_preview"
              ext = ".raw"
            end
            instance_variable_set("@#{kind}", true)
            self.class.instance_eval {
              define_method(("extract_%s" % kind).to_sym) do |path=nil|
                path ||= File.dirname(@filename)
                extract_name = File.basename(@filename)
                extract_name.gsub!(File.extname(extract_name), ext)
                extract_name = File.join(path, "#{kind}_%s_#{extract_name}" % Time.now.strftime("%Y-%m-%d_%H%M%S"))
                _offset = v[offset][:value]
                _length = v[length][:value]
                File.open(@filename, "rb") do |io|
                  io.seek(@TIFF_header_offset + _offset, IO::SEEK_SET)
                  extracted_f = File.open(extract_name, "wb")
                  extracted_f << io.read(_length)
                end
                extract_name
              end
            }
          end
        end
      }
    end
def handle_embedded data = [ [ "ThumbnailOffset" , "ThumbnailLength" ] , [ "PreviewImageStart" , "PreviewImageLength" ] ] data . map { | offset , length | @@DATA . map do | k , v | if v . has_key? ( offset ) ext = ".jpeg" case k when "IFD0" kind = "small_preview" when "IFD1" kind = "thumbnail" when "IFD2" kind = "rgb_uncompress_preview" ext = ".raw" when "IFD3" kind = "lossless_preview" ext = ".raw" end instance_variable_set ( "@#{kind}" , true ) self . class . instance_eval { define_method ( ( "extract_%s" % kind ) . to_sym ) do | path = nil | path ||= File . dirname ( @filename ) extract_name = File . basename ( @filename ) extract_name . gsub! ( File . extname ( extract_name ) , ext ) extract_name = File . join ( path , "#{kind}_%s_#{extract_name}" % Time . now . strftime ( "%Y-%m-%d_%H%M%S" ) ) _offset = v [ offset ] [ :value ] _length = v [ length ] [ :value ] File . open ( @filename , "rb" ) do | io | io . seek ( @TIFF_header_offset + _offset , IO :: SEEK_SET ) extracted_f = File . open ( extract_name , "wb" ) extracted_f << io . read ( _length ) end extract_name end } end end } end
Detect if thumbnail has been found and define function to extract
thumbnail if available
Detect if thumbnail has been found and define function to extract thumbnail if available
# ThumbnailOffset -> thumbnail # PreviewImageStart -> preview # dynamically create methods to extract preview/thumbnail