Class: IPTC::JPEG::Image

Inherits:
Object
  • Object
show all
Defined in:
lib/iptc/jpeg/image.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data_name, content, quick = true) ⇒ Image

Real constructor. Should never be called directly take a “data name” that is used for error reporting.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/iptc/jpeg/image.rb', line 31

def initialize data_name, content, quick=true
  @logger = Logger.new(STDOUT)
  @logger.datetime_format = "%H:%M:%S"
  @logger.level = $DEBUG?(Logger::DEBUG):(Logger::INFO)

  @data_name = data_name
  @content = content

  @position = 0

  if MARKERS[read(2)]!="SOI"
    raise  NotJPEGFileException.new("Not JPEG data: #{@data_name}")
  end

  @markers = Array.new()

  begin

    catch(:end_of_metadata) do
      while true
        @markers << read_marker
      end
    end
  
  rescue Exception=>e
    @logger.info "Exception in data #{@data_name}:\n"+e.to_s
    raise e
  end
  # Markers all read
  # move back
  seek(-2)

  # in full mode, read the rest
  if !quick
    @data = read_rest
  end

  @values = MultipleHash.new

  @markers.each do |marker|
    # puts "processing marker: #{marker.inspect}"
    marker.parse
    # puts marker.valid?
    @values.add(marker, marker.values)
  end
end

Instance Attribute Details

#valuesObject (readonly)

Array of MultipleHashItem objects



10
11
12
# File 'lib/iptc/jpeg/image.rb', line 10

def values
  @values
end

Class Method Details

.from_blob(blob, quick = true) ⇒ Object

creates a JPEG image from a data blob and does a “quick” load (Only the metadata are loaded, not the whole file).



14
15
16
# File 'lib/iptc/jpeg/image.rb', line 14

def Image.from_blob blob, quick=true
  return Image.new("Data blob", blob, true)
end

.from_file(filename, quick = true) ⇒ Object

creates a JPEG image from a file and does a “quick” load (Only the metadata are loaded, not the whole file).



19
20
21
22
23
24
25
26
27
28
# File 'lib/iptc/jpeg/image.rb', line 19

def Image.from_file filename, quick=true
  content = nil
  raise "File #{filename} not found" if !File.exists?(filename)
  File.open(filename) do |f|
    f.binmode
    content = f.read
  end
  return Image.new(filename, content, quick)        

end

Instance Method Details

#l(message) ⇒ Object



86
87
88
# File 'lib/iptc/jpeg/image.rb', line 86

def l message
  @logger.debug message
end

#read(count) ⇒ Object



78
79
80
81
# File 'lib/iptc/jpeg/image.rb', line 78

def read(count)
  @position += count
  return @content[@position-count...@position]
end

#read_markerObject



103
104
105
106
107
108
109
110
111
# File 'lib/iptc/jpeg/image.rb', line 103

def read_marker
  type = read(2)
  # finished reading all the metadata
  throw :end_of_metadata if MARKERS[type]=='SOS'
  size = read(2)
  data = read(size.unpack('n')[0]-2)

  return Marker.NewMarker(MARKERS[type], type+size+data, @logger)
end

#read_restObject



112
113
114
115
# File 'lib/iptc/jpeg/image.rb', line 112

def read_rest
  rest = @content[@position..-1]
  return Marker.new("BIN",rest)
end

#seek(count) ⇒ Object



82
83
84
# File 'lib/iptc/jpeg/image.rb', line 82

def seek(count)
  @position += count
end

#to_sObject



116
117
118
119
# File 'lib/iptc/jpeg/image.rb', line 116

def to_s
	"Image #{@data_name}:\n" +
	@markers.map{ |m| m.to_s  }.join("\n")
end

#write(data_name) ⇒ Object

write the image to the disk



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/iptc/jpeg/image.rb', line 91

def write data_name
  f = File.open(data_name,"wb+")
  f.print "\xFF\xD8"

  @markers.each do |marker|
    f.print marker.to_binary
  end
  f.print @data.to_binary
  f.close

end