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

Class Method 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.



153
154
155
156
157
158
159
160
161
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 153

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

#endianObject (readonly)

Returns the value of attribute endian.



152
153
154
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 152

def endian
  @endian
end

#offsetObject (readonly)

Returns the value of attribute offset.



152
153
154
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 152

def offset
  @offset
end

#tagsObject

Returns the value of attribute tags.



151
152
153
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 151

def tags
  @tags
end

Class Method Details

.s2n_intel(src) ⇒ Object

extract multibyte integer in Intel format (little endian)



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 178

def self.s2n_intel(src)
  x = 0
  l = src.bytesize
  if l == 1
    return src.unpack('C')[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)



164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 164

def self.s2n_motorola(src)
  x = 0
  l = src.bytesize
  if l == 1
    return src.unpack('C')[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

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



524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 524

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.)



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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 443

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’)



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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 264

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] || TagEntry.new('Tag 0x%04X' % tag_id)

        # 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].length
            signed = FIELD_TYPES[field_type].signed
            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) # ascii string of count bytes
                    #print values
                    # Drop any garbage after a null.
                    values = values.split(X00, 1)[0] # a null-terminated string
                else
                    values = ''
                end
            else
                values = []
                
                # @todo investigate
                # some entries get too big to handle could be malformed file
                if count < 1000 or tag_entry.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
        if (tag_id == 0x8769)
            puts "tag_id #{tag_id} field_type #{field_type} count #{count} values #{values.inspect}"

        end
            
            self.tags[ifd_name + ' ' + tag_entry.name] = IFD_Tag.new(tag_id,
                                                      field_type, tag_entry,
                                                      values, field_offset,
                                                      count)
        end
        if tag_entry.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



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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 366

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].length
        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



238
239
240
241
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 238

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

#list_IFDsObject

return list of IFDs in header



252
253
254
255
256
257
258
259
260
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 252

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



224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 224

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



244
245
246
247
248
249
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 244

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



519
520
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 519

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.



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

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

#unpack_number(src, signed = false) ⇒ Object



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/cul_image_props/image/properties/exif/types.rb', line 192

def unpack_number(src, signed=false)
    if @endian == 'I'
        val = EXIF_header.s2n_intel(src)
    else
        val = EXIF_header.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