Class: IPTC::JPEG::Markers::APP13Marker

Inherits:
IPTC::JPEG::Marker show all
Defined in:
lib/iptc/jpeg/markers.rb

Overview

The APP13Marker

The APP13 marker, know as the IPTC Marker See also the IPTC::MarkerNomenclature.

Constant Summary collapse

SHORT =
1
WORD =
2
LONG =
4

Instance Attribute Summary

Attributes inherited from IPTC::JPEG::Marker

#prefix, #values

Instance Method Summary collapse

Methods inherited from IPTC::JPEG::Marker

NewMarker, #l, #read

Constructor Details

#initialize(type, data) ⇒ APP13Marker

Returns a new instance of APP13Marker.



45
46
47
48
49
50
51
# File 'lib/iptc/jpeg/markers.rb', line 45

def initialize(type, data)
  @header = "Photoshop 3.0\000"

  super(type, data)
  @prefix = "iptc"

end

Instance Method Details

#[](item) ⇒ Object



150
151
152
# File 'lib/iptc/jpeg/markers.rb', line 150

def [](item)
  return @values[item]
end

#longObject



63
64
65
# File 'lib/iptc/jpeg/markers.rb', line 63

def long
  read(LONG).unpack("N")[0]
end

#parseObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/iptc/jpeg/markers.rb', line 67

def parse
  l "APP13 marker parsed"
  @markers = Array.new

  # http://www.fileformat.info/format/psd/egff.htm
  # this one is much up to date.

  while @content.pos < @content.length - 1

    eight_bim = read(LONG)
    # Not a 8BIM packet. Go away !

    if eight_bim != "8BIM"
      l "At #{@content.pos}/#{@content.length} we were unable to find an 8BIM marker (#{eight_bim})."
      return
    end

    @bim_type = word()

    # Read name length and normalize to even number of bytes
    # Weird, always read 4 bytes
    padding = read(4)
    bim_size = word()

    # http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/Photoshop.html
    case @bim_type 

    when 0x0404 # IPTC
      content = StringIO.new(read(bim_size))

      while !content.eof?

        header = content.read(2).unpack("n")[0]

        # http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/IPTC.html
        case header
        when 0x1C01
          # skip the envelope
          while !content.eof?
            if content.read(1) == "\x1c"
              content.pos = content.pos - 1
              break
            end
          end
        when 0x1C02

          type = content.read(1).unpack('c')[0]
          size = content.read(2)
          value = content.read(size.unpack('n')[0])

          l "Found marker 0x#{type.to_s(16)}"
          marker = IPTC::Marker.new(type, value)
          k = @prefix+"/"+IPTC::MarkerNomenclature.markers(type.to_i).name
          if @values.has_key?(k)
            if @values[k].is_a?(Array)
              @values[k] << value
            else
              @values[k] = [@values[k], value]
            end
          else
              @values[k] = value
          end
          @markers << marker

        else
          # raise InvalidBlockException.new("Invalid BIM segment #{header.inspect} in marker\n#{@original_content.inspect}")
        end
      end
    when 0x03ED
      hRes = long()
      hResUnit = word()
      widthUnit = word()
      vRes = long()
      vResUnit = word()
      heightUnit = word()
    else
      read(bim_size)
    end
    read(1) if bim_size%2 == 1
  end
  return @values
end

#propertiesObject



166
167
168
# File 'lib/iptc/jpeg/markers.rb', line 166

def properties
  return IPTC::TAGS.values.sort
end

#set(property, value) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/iptc/jpeg/markers.rb', line 169

def set(property, value)
  numerical_tag = IPTC::TAGS.index(property)
  if numerical_tag!=nil
  else
    throw InvalidPropertyException.new("Invalid property #{property} for IPTC marker")
  end
  marker = IPTC::Marker.new(numerical_tag, value)
  @markers << marker
end

#to_binaryObject



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/iptc/jpeg/markers.rb', line 153

def to_binary
  marker = ""
  @markers.each do |value|
    marker += value.to_binary
  end

  marker =  @header+@bim_type+@bim_dummy+[marker.length].pack('n')+marker

  # build the complete marker
  marker = super(marker)

  return marker
end

#valid?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/iptc/jpeg/markers.rb', line 52

def valid?
  return read(@header.length)==@header
end

#wordObject



60
61
62
# File 'lib/iptc/jpeg/markers.rb', line 60

def word
  read(WORD).unpack("n")[0]
end