Class: Cul::Image::Properties::Exif::EXIF_header

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

Overview

class that handles an EXIF header

Constant Summary collapse

X00 =
[0x00].pack('C')

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, endian, offset, fake_exif, strict, detail = true) ⇒ EXIF_header

Returns a new instance of EXIF_header.



115
116
117
118
119
120
121
122
123
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 115

def initialize(file, endian, offset, fake_exif, strict, detail=true)
    @file = file
    @endian = endian
    @offset = offset
    @fake_exif = fake_exif
    @strict = strict
    @detail = detail
    @tags = {}
end

Instance Attribute Details

#tagsObject

Returns the value of attribute tags.



114
115
116
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 114

def tags
  @tags
end

Instance Method Details

#canon_decode_tag(value, dict) ⇒ Object

decode Canon MakerNote tag based on offset within tag see http =>//www.burren.cx/david/canon.html by David Burren



510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 510

def canon_decode_tag(value, dict)
    (1 ... len(value)).each { |i|
        x=dict.get(i, ['Unknown'])

        name=x[0]
        if len(x) > 1
            val=x[1].get(value[i], 'Unknown')
        else
            val=value[i]
        end
        # it's not a real IFD Tag but we fake one to make everybody
        # happy. this will have a "proprietary" type
        self.tags['MakerNote '+name]=IFD_Tag(str(val), None, 0, None,
                                             None, None)
    }
end

#decode_maker_noteObject

Note is the data that comprises this MakerNote. The MakerNote will likely have pointers in it that point to other parts of the file. We’ll use self.offset as the starting point for most of those pointers, since they are relative to the beginning of the file.

If the MakerNote is in a newer format, it may use relative addressing within the MakerNote. In that case we’ll use relative addresses for the pointers.

As an aside => it’s not just to be annoying that the manufacturers use relative offsets. It’s so that if the makernote has to be moved by the picture software all of the offsets don’t have to be adjusted. Overall, this is probably the right strategy for makernotes, though the spec is ambiguous. (The spec does not appear to imagine that makernotes would follow EXIF format internally. Once they did, it’s ambiguous whether the offsets should be from the header at the start of all the EXIF info, or from the header at the start of the makernote.)



429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 429

def decode_maker_note()
    note = self.tags['EXIF MakerNote']
    
    # Some apps use MakerNote tags but do not use a format for which we
    # have a description, so just do a raw dump for these.

    make = self.tags['Image Make'].printable

    # Nikon
    # The maker note usually starts with the word Nikon, followed by the
    # type of the makernote (1 or 2, as a short).  If the word Nikon is
    # not at the start of the makernote, it's probably type 2, since some
    # cameras work that way.
    if make.include? 'NIKON'
        if note.values[0,7] == [78, 105, 107, 111, 110, 0, 1]
            self.dump_IFD(note.field_offset+8, 'MakerNote',
                          :dict=>MAKERNOTE_NIKON_OLDER_TAGS)
        elsif note.values[0, 7] == [78, 105, 107, 111, 110, 0, 2]
            if note.values[12,2] != [0, 42] and note.values[12,2] != [42, 0]
                raise "Missing marker tag '42' in MakerNote."
            end
            # skip the Makernote label and the TIFF header
            self.dump_IFD(note.field_offset+10+8, 'MakerNote',
                          :dict=>MAKERNOTE_NIKON_NEWER_TAGS, :relative=>true)
        else
            # E99x or D1
            self.dump_IFD(note.field_offset, 'MakerNote',
                          :dict=>MAKERNOTE_NIKON_NEWER_TAGS)
        end
        return
    end
    # Olympus
    if make.index('OLYMPUS') == 0
        self.dump_IFD(note.field_offset+8, 'MakerNote',
                      :dict=>MAKERNOTE_OLYMPUS_TAGS)
        return
    end
    # Casio
    if make.include? 'CASIO' or make.include? 'Casio'
        self.dump_IFD(note.field_offset, 'MakerNote',
                      :dict=>MAKERNOTE_CASIO_TAGS)
        return
    end
    # Fujifilm
    if make == 'FUJIFILM'
        # bug => everything else is "Motorola" endian, but the MakerNote
        # is "Intel" endian
        endian = self.endian
        self.endian = 'I'
        # bug => IFD offsets are from beginning of MakerNote, not
        # beginning of file header
        offset = self.offset
        self.offset += note.field_offset
        # process note with bogus values (note is actually at offset 12)
        self.dump_IFD(12, 'MakerNote', :dict=>MAKERNOTE_FUJIFILM_TAGS)
        # reset to correct values
        self.endian = endian
        self.offset = offset
        return
    end
    # Canon
    if make == 'Canon'
        self.dump_IFD(note.field_offset, 'MakerNote',
                      :dict=>MAKERNOTE_CANON_TAGS)
        [['MakerNote Tag 0x0001', MAKERNOTE_CANON_TAG_0x001],
                  ['MakerNote Tag 0x0004', MAKERNOTE_CANON_TAG_0x004]].each { |i|
            begin
              self.canon_decode_tag(self.tags[i[0]].values, i[1])  # gd added 
            rescue
            end
        }
        return
    end
end

#dump_IFD(ifd, ifd_name, opts) ⇒ Object

def dump_IFD(ifd, ifd_name, dict=EXIF_TAGS, relative=false, stop_tag=‘UNDEF’)



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 226

def dump_IFD(ifd, ifd_name, opts)
    opts = {:dict => EXIF_TAGS, :relative => false, :stop_tag => 'UNDEF'}.merge(opts)
    dict = opts[:dict]
    relative = opts[:relative]
    stop_tag = opts[:stop_tag]
    @file.seek(@offset + ifd)
    entries = unpack_number(@file.read(2))
    entries_ptr = ifd + 2
    puts ifd_name + " had zero entries!" if entries == 0
    (0 ... entries).each { |i|
        # entry is index of start of this IFD in the file
        entry = ifd + 2 + (12 * i)
        @file.seek(@offset + entry)
        tag_id = unpack_number(@file.read(2))

        # get tag name early to avoid errors, help debug
        tag_entry = dict[tag_id]
        if tag_entry
            tag_name = tag_entry.name
        else
            tag_name = 'Tag 0x%04X' % tag_id
        end

        # ignore certain tags for faster processing
        if not (not @detail and IGNORE_TAGS.include? tag_id)
            # The 12 byte Tag format is ID (short) TYPE (short) COUNT (long) VALUE (long)
            # if actual values would exceed 4 bytes (long), VALUE
            # is instead a pointer to the actual values.
            field_type = unpack_number(@file.read(2))
            
            # unknown field type
            if 0 > field_type or field_type >= FIELD_TYPES.length
                if not @strict
                    next
                else
                    raise format("unknown type %d in tag 0x%04X", field_type, tag)
                end
            end
            typelen = FIELD_TYPES[field_type][0]
            count = unpack_number(@file.read(4))

            # If the value exceeds 4 bytes, it is a pointer to values.
            if (count * typelen) > 4
                # Note that 'relative' is a fix for the Nikon type 3 makernote.
                field_offset = unpack_number(@file.read(4))
                if relative
                    field_offset = field_offset + ifd - 8
                    if @fake_exif
                        field_offset = field_offset + 18
                    end
                end
            else
              field_offset = entry + 8
            end

            if field_type == 2
                # special case => null-terminated ASCII string
                # XXX investigate
                # sometimes gets too big to fit in int value
                if count != 0 and count < (2**31)
                    @file.seek(@offset + field_offset)
                    values = @file.read(count)
                    #print values
                    # Drop any garbage after a null.
                    values = values.split(X00, 1)[0]
                else
                    values = ''
                end
            else
                values = []
                signed = [6, 8, 9, 10].include? field_type
                
                # @todo investigate
                # some entries get too big to handle could be malformed file
                if count < 1000 or tag_name == 'MakerNote'
                    @file.seek(@offset + field_offset)
                    count.times {
                        if field_type == 5 or field_type == 10
                            # a ratio
                            value = Ratio.new(unpack_number(@file.read(4), signed),
                                          unpack_number(@file.read(4), signed) )
                        else
                            value = unpack_number(@file.read(typelen), signed)
                        end
                        values << value
                    }
                end
            end
            # now 'values' is either a string or an array
            if count == 1 and field_type != 2
                printable=values[0].to_s
            elsif count > 50 and values.length > 20
                printable= (field_type == 2) ? (values[0...20] + '...') : ("[" + values[0...20].join(',') + ", ... ]")
            else
                printable=values.inspect
            end
            # compute printable version of values
            if tag_entry
                if tag_entry.value
                    # optional 2nd tag element is present
                    if tag_entry.value.respond_to? :call
                        # call mapping function
                        printable = tag_entry.value.call(values)
                    else
                        printable = ''
                        values.each { |i|
                            # use lookup table for this tag
                            printable += (tag_entry.value.include? i)?tag_entry.value[i] : i.inspect
                        }
                    end
                 end
            end
            self.tags[ifd_name + ' ' + tag_name] = IFD_Tag.new(printable, tag_id,
                                                      field_type,
                                                      values, field_offset,
                                                      count * typelen)
        end
        if tag_name == stop_tag
            break
        end
    }
end

#extract_TIFF_thumbnail(thumb_ifd) ⇒ Object

extract uncompressed TIFF thumbnail (like pulling teeth) we take advantage of the pre-existing layout in the thumbnail IFD as much as possible



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 352

def extract_TIFF_thumbnail(thumb_ifd)
    entries = self.s2n(thumb_ifd, 2)
    # this is header plus offset to IFD ...
    if @endian == 'M'
        tiff = "MM#{X00}*" + [0x00,0x00,0x00,0x00,0x08].pack('C*')
    else
        tiff = 'II*' + [0x00,0x08,0x00,0x00,0x00].pack('C*')
    end
    # ... plus thumbnail IFD data plus a null "next IFD" pointer
    self.file.seek(self.offset+thumb_ifd)
    tiff += self.file.read(entries*12+2)+[0x00,0x00,0x00,0x00].pack('C*')

    # fix up large value offset pointers into data area
    (0...entries).each { |i|
        entry = thumb_ifd + 2 + 12 * i
        tag = self.s2n(entry, 2)
        field_type = self.s2n(entry+2, 2)
        typelen = FIELD_TYPES[field_type][0]
        count = self.s2n(entry+4, 4)
        oldoff = self.s2n(entry+8, 4)
        # start of the 4-byte pointer area in entry
        ptr = i * 12 + 18
        # remember strip offsets location
        if tag == 0x0111
            strip_off = ptr
            strip_len = count * typelen
        end
        # is it in the data area?
        if count * typelen > 4
            # update offset pointer (nasty "strings are immutable" crap)
            # should be able to say "tiff[ptr..ptr+4]=newoff"
            newoff = len(tiff)
            tiff = tiff[ 0..ptr] + self.n2s(newoff, 4) + tiff[ptr+4...tiff.length]
            # remember strip offsets location
            if tag == 0x0111
                strip_off = newoff
                strip_len = 4
            end
            # get original data and store it
            self.file.seek(self.offset + oldoff)
            tiff += self.file.read(count * typelen)
        end
    }
    # add pixel strips and update strip offset info
    old_offsets = self.tags['Thumbnail StripOffsets'].values
    old_counts = self.tags['Thumbnail StripByteCounts'].values
    (0...len(old_offsets)).each { |i|
        # update offset pointer (more nasty "strings are immutable" crap)
        offset = self.n2s(len(tiff), strip_len)
        tiff = tiff[ 0..strip_off] + offset + tiff[strip_off + strip_len ... tiff.length]
        strip_off += strip_len
        # add pixel strip to end
        self.file.seek(self.offset + old_offsets[i])
        tiff += self.file.read(old_counts[i])
    }
    self.tags['TIFFThumbnail'] = tiff
end

#first_IFDObject

return first IFD



200
201
202
203
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 200

def first_IFD()
    @file.seek(@offset + 4)
    return unpack_number(@file.read(4))
end

#list_IFDsObject

return list of IFDs in header



214
215
216
217
218
219
220
221
222
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 214

def list_IFDs()
    i=self.first_IFD()
    a=[]
    while i != 0
        a << i
        i=self.next_IFD(i)
    end
    return a
end

#n2s(offset, length) ⇒ Object

convert offset to string



186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 186

def n2s(offset, length)
    s = ''
    length.times {
        if @endian == 'I'
            s = s + chr(offset & 0xFF)
        else
            s = chr(offset & 0xFF) + s
        end
        offset = offset >> 8
    }
    return s
end

#next_IFD(ifd) ⇒ Object

return pointer to next IFD



206
207
208
209
210
211
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 206

def next_IFD(ifd)
    @file.seek(@offset + ifd)
    entries = unpack_number(@file.read(2))
    @file.seek((12*entries), IO::SEEK_CUR)
    return unpack_number(@file.read(4))
end

#olympus_decode_tag(value, dict) ⇒ Object

XXX TODO decode Olympus MakerNote tag based on offset within tag



505
506
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 505

def olympus_decode_tag(value, dict)
end

#s2n(offset, length, signed = false) ⇒ Object

convert slice to integer, based on sign and endian flags usually this offset is assumed to be relative to the beginning of the start of the EXIF information. For some cameras that use relative tags, this offset may be relative to some other starting point.



174
175
176
177
178
179
180
181
182
183
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 174

def s2n(offset, length, signed=false)
    @file.seek(@offset+offset)
    if @file.eof? and length != 0
      # raise "Read past EOF"
      puts "Read past EOF"
      return 0
    end
    slice=@file.read(length)
    return unpack_number(slice,signed)
end

#s2n_intel(src) ⇒ Object

extract multibyte integer in Intel format (little endian)



140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 140

def s2n_intel(src)
  x = 0
  l = src.length
  if l == 1
    return src[0]
  elsif l == 2
    return src.unpack('v')[0]
  elsif l == 4 
    return src.unpack('V')[0]
  else
    raise "Unexpected packed Fixnum length: " + l.to_s
  end
end

#s2n_motorola(src) ⇒ Object

extract multibyte integer in Motorola format (big/network endian)



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 126

def s2n_motorola(src)
    x = 0
    l = src.length
    if l == 1
      return src[0]
    elsif l == 2
      return src.unpack('n')[0]
    elsif l == 4
      return src.unpack('N')[0]
    else
      raise "Unexpected packed Fixnum length: " + l.to_s
    end
end

#unpack_number(src, signed = false) ⇒ Object



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 154

def unpack_number(src, signed=false)
    if @endian == 'I'
        val=s2n_intel(src)
    else
        val=s2n_motorola(src)
    end
    # Sign extension ?
    if signed
        msb= 1 << (8*src.length-1)
        if val & msb
            val=val-(msb << 1)
        end
    end
    return val
end