#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Library to extract EXIF information in digital camera image files
#
# To use this library call with:
#    f=open(path_name, 'rb')
#    tags=EXIF.process_file(f)
#
# If you want to ignore makerNote, pass the -q or --quick
# command line arguments, or as
#    tags=EXIF.process_file(f, details=False)
#
# This is usefull when you are retrieving a large list of images
#
# Returned tags will be a dictionary mapping names of EXIF tags to their
# values in the file named by path_name.  You can process the tags
# as you wish.  In particular, you can iterate through all the tags with:
#     for tag in tags.keys():
#         if tag not in ('JPEGThumbnail', 'TIFFThumbnail', 'Filename',
#                        'EXIF MakerNote'):
#             print "Key: %s, value %s" % (tag, tags[tag])
# (This code uses the if statement to avoid printing out a few of the
# tags that tend to be long or boring.)
#
# The tags dictionary will include keys for all of the usual EXIF
# tags, and will also include keys for Makernotes used by some
# cameras, for which we have a good specification.
#
# Contains code from "exifdump.py" originally written by Thierry Bousch
# <bousch@topo.math.u-psud.fr> and released into the public domain.
#
# Updated and turned into general-purpose library by Gene Cash
#
# This copyright license is intended to be similar to the FreeBSD license.
#
# Copyright (c) 2002 Gene Cash All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
#    1. Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#    2. Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in the
#       documentation and/or other materials provided with the
#       distribution.
#
# THIS SOFTWARE IS PROVIDED BY GENE CASH ``AS IS'' AND ANY EXPRESS OR
# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# This means you may do anything you want with this code, except claim you
# wrote it. Also, if it breaks you get to keep both pieces.
#
# Patch Contributors:
# * Simon J. Gerraty <sjg@crufty.net>
#   s2n fix & orientation decode
# * John T. Riedl <riedl@cs.umn.edu>
#   Added support for newer Nikon type 3 Makernote format for D70 and some
#   other Nikon cameras.
# * Joerg Schaefer <schaeferj@gmx.net>
#   Fixed subtle bug when faking an EXIF header, which affected maker notes
#   using relative offsets, and a fix for Nikon D100.
#
# 21-AUG-99 TB  Last update by Thierry Bousch to his code.
# 17-JAN-02 CEC Discovered code on web.
#               Commented everything.
#               Made small code improvements.
#               Reformatted for readability.
# 19-JAN-02 CEC Added ability to read TIFFs and JFIF-format JPEGs.
#               Added ability to extract JPEG formatted thumbnail.
#               Added ability to read GPS IFD (not tested).
#               Converted IFD data structure to dictionaries indexed by
#               tag name.
#               Factored into library returning dictionary of IFDs plus
#               thumbnail, if any.
# 20-JAN-02 CEC Added MakerNote processing logic.
#               Added Olympus MakerNote.
#               Converted data structure to single-level dictionary, avoiding
#               tag name collisions by prefixing with IFD name.  This makes
#               it much easier to use.
# 23-JAN-02 CEC Trimmed nulls from end of string values.
# 25-JAN-02 CEC Discovered JPEG thumbnail in Olympus TIFF MakerNote.
# 26-JAN-02 CEC Added ability to extract TIFF thumbnails.
#               Added Nikon, Fujifilm, Casio MakerNotes.
# 30-NOV-03 CEC Fixed problem with canon_decode_tag() not creating an
#               IFD_Tag() object.
# 15-FEB-04 CEC Finally fixed bit shift warning by converting Y to 0L.
#
# ---------------------------- End original notices ------------------------- #

# 2007-18-01 - Ianaré Sévi <ianare@gmail.com>
# Fixed a couple errors and assuming maintenance of the library.

# 2007-24-03 - Ianaré Sévi
# Can now ignore MakerNotes Tags for faster processing.

# 2007-03-05 - Martin Stone <mj_stone@users.sourceforge.net>
# Fix for inverted detailed flag and Photoshop header





# ===== CODE START ==== #
