From 95fef909d8afb979aa3a1d62d5327d0b0043d279 Mon Sep 17 00:00:00 2001 From: John-Mark Gurney Date: Thu, 4 May 2023 11:59:51 -0700 Subject: [PATCH] checkpoint this work, confirmed parsing CRW and CR2 files... This still needs some cleanup and additional tests.. This isn't hooked into the testing system yet as I still haven't decided if I'm going to commit fixtures or not (or maybe make this it's own repo).. IFD needs serious cleanup.. I should be using a classmethod instead of the janky nextptr bs. --- ui/medashare/metadata/BitField.py | 62 ++ ui/medashare/metadata/EXIF.py | 1194 ++++++++++++++++++++++++++++ ui/medashare/metadata/__init__.py | 0 ui/medashare/metadata/crw.py | 1234 +++++++++++++++++++++++++++++ 4 files changed, 2490 insertions(+) create mode 100644 ui/medashare/metadata/BitField.py create mode 100644 ui/medashare/metadata/EXIF.py create mode 100644 ui/medashare/metadata/__init__.py create mode 100644 ui/medashare/metadata/crw.py diff --git a/ui/medashare/metadata/BitField.py b/ui/medashare/metadata/BitField.py new file mode 100644 index 0000000..7a57556 --- /dev/null +++ b/ui/medashare/metadata/BitField.py @@ -0,0 +1,62 @@ +class BitFieldMetaClass: + def __init__(self, name, bases, dct): + for base in bases: + if base.__class__ is not BitFieldMetaClass: + raise TypeError('BitField base class must be bit field') + bases = filter(lambda x: x is not BitField, bases) + self.__name__ = name + self.__bases__ = bases + d = self.__dict = {} + for key, value in dct.items(): + if key[:2] == '__': + continue + assert isinstance(value, int) + d[key] = value + + def __call__(self, val): + return BitFieldInstance(self.__name__, self.__dict, val) + +class BitFieldInstance(object): + def __init__(self, name, d, v): + self.__bfname = name + self.__dict = d + self.__value = v + + name = property(lambda x: x.__bfname) + + def __int__(self): + return self.__value + + def __getattr__(self, k): + if k[:19] == '_BitFieldInstance__': + return object.__getattr__(self, k) + + try: + return bool(self.__value & 1 << (self.__dict[k])) + except KeyError: + raise AttributeError('no field named: %s' % repr(k)) + + def __setattr__(self, k, v): + if k[:19] == '_BitFieldInstance__': + return object.__setattr__(self, k, v) + + bit = self.__dict[k] + self.__value &= ~(1 << bit) + self.__value |= bool(v) << bit + + def __str__(self): + return '<%s>' % ','.join([ k for k in self.__dict if getattr(self, k) ]) + + def __repr__(self): + return 'BitFieldInstance(%s, %s, %s)' % (repr(self.__bfname), repr(self.__dict), repr(self.__value)) + +BitField = BitFieldMetaClass("BitField", (), {}) +__all__ = [ 'BitField' ] + +def bftest(): + class foo(BitField): + bar = 3 + foo = 4 + bleh = 16 + + return foo(16) diff --git a/ui/medashare/metadata/EXIF.py b/ui/medashare/metadata/EXIF.py new file mode 100644 index 0000000..f6bd9c3 --- /dev/null +++ b/ui/medashare/metadata/EXIF.py @@ -0,0 +1,1194 @@ +# 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) +# tags will now 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 +# 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 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 +# s2n fix & orientation decode +# * John T. Riedl +# Added support for newer Nikon type 3 Makernote format for D70 and some +# other Nikon cameras. +# * Joerg Schaefer +# 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. +# + +# field type descriptions as (length, abbreviation, full name) tuples +FIELD_TYPES=( + (0, 'X', 'Proprietary'), # no such type + (1, 'B', 'Byte'), + (1, 'A', 'ASCII'), + (2, 'S', 'Short'), + (4, 'L', 'Long'), + (8, 'R', 'Ratio'), + (1, 'SB', 'Signed Byte'), + (1, 'U', 'Undefined'), + (2, 'SS', 'Signed Short'), + (4, 'SL', 'Signed Long'), + (8, 'SR', 'Signed Ratio') + ) + +# dictionary of main EXIF tag names +# first element of tuple is tag name, optional second element is +# another dictionary giving names to values +EXIF_TAGS={ + 0x0100: ('ImageWidth', ), + 0x0101: ('ImageLength', ), + 0x0102: ('BitsPerSample', ), + 0x0103: ('Compression', + {1: 'Uncompressed TIFF', + 6: 'JPEG Compressed'}), + 0x0106: ('PhotometricInterpretation', ), + 0x010A: ('FillOrder', ), + 0x010D: ('DocumentName', ), + 0x010E: ('ImageDescription', ), + 0x010F: ('Make', ), + 0x0110: ('Model', ), + 0x0111: ('StripOffsets', ), + 0x0112: ('Orientation', + {1: 'Horizontal (normal)', + 2: 'Mirrored horizontal', + 3: 'Rotated 180', + 4: 'Mirrored vertical', + 5: 'Mirrored horizontal then rotated 90 CCW', + 6: 'Rotated 90 CW', + 7: 'Mirrored horizontal then rotated 90 CW', + 8: 'Rotated 90 CCW'}), + 0x0115: ('SamplesPerPixel', ), + 0x0116: ('RowsPerStrip', ), + 0x0117: ('StripByteCounts', ), + 0x011A: ('XResolution', ), + 0x011B: ('YResolution', ), + 0x011C: ('PlanarConfiguration', ), + 0x0128: ('ResolutionUnit', + {1: 'Not Absolute', + 2: 'Pixels/Inch', + 3: 'Pixels/Centimeter'}), + 0x012D: ('TransferFunction', ), + 0x0131: ('Software', ), + 0x0132: ('DateTime', ), + 0x013B: ('Artist', ), + 0x013E: ('WhitePoint', ), + 0x013F: ('PrimaryChromaticities', ), + 0x0156: ('TransferRange', ), + 0x0200: ('JPEGProc', ), + 0x0201: ('JPEGInterchangeFormat', ), + 0x0202: ('JPEGInterchangeFormatLength', ), + 0x0211: ('YCbCrCoefficients', ), + 0x0212: ('YCbCrSubSampling', ), + 0x0213: ('YCbCrPositioning', ), + 0x0214: ('ReferenceBlackWhite', ), + 0x828D: ('CFARepeatPatternDim', ), + 0x828E: ('CFAPattern', ), + 0x828F: ('BatteryLevel', ), + 0x8298: ('Copyright', ), + 0x829A: ('ExposureTime', ), + 0x829D: ('FNumber', ), + 0x83BB: ('IPTC/NAA', ), + 0x8769: ('ExifOffset', ), + 0x8773: ('InterColorProfile', ), + 0x8822: ('ExposureProgram', + {0: 'Unidentified', + 1: 'Manual', + 2: 'Program Normal', + 3: 'Aperture Priority', + 4: 'Shutter Priority', + 5: 'Program Creative', + 6: 'Program Action', + 7: 'Portrait Mode', + 8: 'Landscape Mode'}), + 0x8824: ('SpectralSensitivity', ), + 0x8825: ('GPSInfo', ), + 0x8827: ('ISOSpeedRatings', ), + 0x8828: ('OECF', ), + # print as string + 0x9000: ('ExifVersion', lambda x: ''.join(map(chr, x))), + 0x9003: ('DateTimeOriginal', ), + 0x9004: ('DateTimeDigitized', ), + 0x9101: ('ComponentsConfiguration', + {0: '', + 1: 'Y', + 2: 'Cb', + 3: 'Cr', + 4: 'Red', + 5: 'Green', + 6: 'Blue'}), + 0x9102: ('CompressedBitsPerPixel', ), + 0x9201: ('ShutterSpeedValue', ), + 0x9202: ('ApertureValue', ), + 0x9203: ('BrightnessValue', ), + 0x9204: ('ExposureBiasValue', ), + 0x9205: ('MaxApertureValue', ), + 0x9206: ('SubjectDistance', ), + 0x9207: ('MeteringMode', + {0: 'Unidentified', + 1: 'Average', + 2: 'CenterWeightedAverage', + 3: 'Spot', + 4: 'MultiSpot'}), + 0x9208: ('LightSource', + {0: 'Unknown', + 1: 'Daylight', + 2: 'Fluorescent', + 3: 'Tungsten', + 10: 'Flash', + 17: 'Standard Light A', + 18: 'Standard Light B', + 19: 'Standard Light C', + 20: 'D55', + 21: 'D65', + 22: 'D75', + 255: 'Other'}), + 0x9209: ('Flash', {0: 'No', + 1: 'Fired', + 5: 'Fired (?)', # no return sensed + 7: 'Fired (!)', # return sensed + 9: 'Fill Fired', + 13: 'Fill Fired (?)', + 15: 'Fill Fired (!)', + 16: 'Off', + 24: 'Auto Off', + 25: 'Auto Fired', + 29: 'Auto Fired (?)', + 31: 'Auto Fired (!)', + 32: 'Not Available'}), + 0x920A: ('FocalLength', ), + 0x927C: ('MakerNote', ), + # print as string + 0x9286: ('UserComment', lambda x: ''.join(map(chr, x))), + 0x9290: ('SubSecTime', ), + 0x9291: ('SubSecTimeOriginal', ), + 0x9292: ('SubSecTimeDigitized', ), + # print as string + 0xA000: ('FlashPixVersion', lambda x: ''.join(map(chr, x))), + 0xA001: ('ColorSpace', ), + 0xA002: ('ExifImageWidth', ), + 0xA003: ('ExifImageLength', ), + 0xA005: ('InteroperabilityOffset', ), + 0xA20B: ('FlashEnergy', ), # 0x920B in TIFF/EP + 0xA20C: ('SpatialFrequencyResponse', ), # 0x920C - - + 0xA20E: ('FocalPlaneXResolution', ), # 0x920E - - + 0xA20F: ('FocalPlaneYResolution', ), # 0x920F - - + 0xA210: ('FocalPlaneResolutionUnit', ), # 0x9210 - - + 0xA214: ('SubjectLocation', ), # 0x9214 - - + 0xA215: ('ExposureIndex', ), # 0x9215 - - + 0xA217: ('SensingMethod', ), # 0x9217 - - + 0xA300: ('FileSource', + {3: 'Digital Camera'}), + 0xA301: ('SceneType', + {1: 'Directly Photographed'}), + 0xA302: ('CVAPattern',), + } + +# interoperability tags +INTR_TAGS={ + 0x0001: ('InteroperabilityIndex', ), + 0x0002: ('InteroperabilityVersion', ), + 0x1000: ('RelatedImageFileFormat', ), + 0x1001: ('RelatedImageWidth', ), + 0x1002: ('RelatedImageLength', ), + } + +# GPS tags (not used yet, haven't seen camera with GPS) +GPS_TAGS={ + 0x0000: ('GPSVersionID', ), + 0x0001: ('GPSLatitudeRef', ), + 0x0002: ('GPSLatitude', ), + 0x0003: ('GPSLongitudeRef', ), + 0x0004: ('GPSLongitude', ), + 0x0005: ('GPSAltitudeRef', ), + 0x0006: ('GPSAltitude', ), + 0x0007: ('GPSTimeStamp', ), + 0x0008: ('GPSSatellites', ), + 0x0009: ('GPSStatus', ), + 0x000A: ('GPSMeasureMode', ), + 0x000B: ('GPSDOP', ), + 0x000C: ('GPSSpeedRef', ), + 0x000D: ('GPSSpeed', ), + 0x000E: ('GPSTrackRef', ), + 0x000F: ('GPSTrack', ), + 0x0010: ('GPSImgDirectionRef', ), + 0x0011: ('GPSImgDirection', ), + 0x0012: ('GPSMapDatum', ), + 0x0013: ('GPSDestLatitudeRef', ), + 0x0014: ('GPSDestLatitude', ), + 0x0015: ('GPSDestLongitudeRef', ), + 0x0016: ('GPSDestLongitude', ), + 0x0017: ('GPSDestBearingRef', ), + 0x0018: ('GPSDestBearing', ), + 0x0019: ('GPSDestDistanceRef', ), + 0x001A: ('GPSDestDistance', ) + } + +# Nikon E99x MakerNote Tags +# http://members.tripod.com/~tawba/990exif.htm +MAKERNOTE_NIKON_NEWER_TAGS={ + 0x0002: ('ISOSetting', ), + 0x0003: ('ColorMode', ), + 0x0004: ('Quality', ), + 0x0005: ('Whitebalance', ), + 0x0006: ('ImageSharpening', ), + 0x0007: ('FocusMode', ), + 0x0008: ('FlashSetting', ), + 0x0009: ('AutoFlashMode', ), + 0x000B: ('WhiteBalanceBias', ), + 0x000C: ('WhiteBalanceRBCoeff', ), + 0x000F: ('ISOSelection', ), + 0x0012: ('FlashCompensation', ), + 0x0013: ('ISOSpeedRequested', ), + 0x0016: ('PhotoCornerCoordinates', ), + 0x0018: ('FlashBracketCompensationApplied', ), + 0x0019: ('AEBracketCompensationApplied', ), + 0x0080: ('ImageAdjustment', ), + 0x0081: ('ToneCompensation', ), + 0x0082: ('AuxiliaryLens', ), + 0x0083: ('LensType', ), + 0x0084: ('LensMinMaxFocalMaxAperture', ), + 0x0085: ('ManualFocusDistance', ), + 0x0086: ('DigitalZoomFactor', ), + 0x0088: ('AFFocusPosition', + {0x0000: 'Center', + 0x0100: 'Top', + 0x0200: 'Bottom', + 0x0300: 'Left', + 0x0400: 'Right'}), + 0x0089: ('BracketingMode', + {0x00: 'Single frame, no bracketing', + 0x01: 'Continuous, no bracketing', + 0x02: 'Timer, no bracketing', + 0x10: 'Single frame, exposure bracketing', + 0x11: 'Continuous, exposure bracketing', + 0x12: 'Timer, exposure bracketing', + 0x40: 'Single frame, white balance bracketing', + 0x41: 'Continuous, white balance bracketing', + 0x42: 'Timer, white balance bracketing'}), + 0x008D: ('ColorMode', ), + 0x008F: ('SceneMode?', ), + 0x0090: ('LightingType', ), + 0x0092: ('HueAdjustment', ), + 0x0094: ('Saturation', + {-3: 'B&W', + -2: '-2', + -1: '-1', + 0: '0', + 1: '1', + 2: '2'}), + 0x0095: ('NoiseReduction', ), + 0x00A7: ('TotalShutterReleases', ), + 0x00A9: ('ImageOptimization', ), + 0x00AA: ('Saturation', ), + 0x00AB: ('DigitalVariProgram', ), + 0x0010: ('DataDump', ) + } + +MAKERNOTE_NIKON_OLDER_TAGS={ + 0x0003: ('Quality', + {1: 'VGA Basic', + 2: 'VGA Normal', + 3: 'VGA Fine', + 4: 'SXGA Basic', + 5: 'SXGA Normal', + 6: 'SXGA Fine'}), + 0x0004: ('ColorMode', + {1: 'Color', + 2: 'Monochrome'}), + 0x0005: ('ImageAdjustment', + {0: 'Normal', + 1: 'Bright+', + 2: 'Bright-', + 3: 'Contrast+', + 4: 'Contrast-'}), + 0x0006: ('CCDSpeed', + {0: 'ISO 80', + 2: 'ISO 160', + 4: 'ISO 320', + 5: 'ISO 100'}), + 0x0007: ('WhiteBalance', + {0: 'Auto', + 1: 'Preset', + 2: 'Daylight', + 3: 'Incandescent', + 4: 'Fluorescent', + 5: 'Cloudy', + 6: 'Speed Light'}) + } + +# decode Olympus SpecialMode tag in MakerNote +def olympus_special_mode(v): + a={ + 0: 'Normal', + 1: 'Unknown', + 2: 'Fast', + 3: 'Panorama'} + b={ + 0: 'Non-panoramic', + 1: 'Left to right', + 2: 'Right to left', + 3: 'Bottom to top', + 4: 'Top to bottom'} + return '%s - sequence %d - %s' % (a[v[0]], v[1], b[v[2]]) + +MAKERNOTE_OLYMPUS_TAGS={ + # ah HAH! those sneeeeeaky bastids! this is how they get past the fact + # that a JPEG thumbnail is not allowed in an uncompressed TIFF file + 0x0100: ('JPEGThumbnail', ), + 0x0200: ('SpecialMode', olympus_special_mode), + 0x0201: ('JPEGQual', + {1: 'SQ', + 2: 'HQ', + 3: 'SHQ'}), + 0x0202: ('Macro', + {0: 'Normal', + 1: 'Macro'}), + 0x0204: ('DigitalZoom', ), + 0x0207: ('SoftwareRelease', ), + 0x0208: ('PictureInfo', ), + # print as string + 0x0209: ('CameraID', lambda x: ''.join(map(chr, x))), + 0x0F00: ('DataDump', ) + } + +MAKERNOTE_CASIO_TAGS={ + 0x0001: ('RecordingMode', + {1: 'Single Shutter', + 2: 'Panorama', + 3: 'Night Scene', + 4: 'Portrait', + 5: 'Landscape'}), + 0x0002: ('Quality', + {1: 'Economy', + 2: 'Normal', + 3: 'Fine'}), + 0x0003: ('FocusingMode', + {2: 'Macro', + 3: 'Auto Focus', + 4: 'Manual Focus', + 5: 'Infinity'}), + 0x0004: ('FlashMode', + {1: 'Auto', + 2: 'On', + 3: 'Off', + 4: 'Red Eye Reduction'}), + 0x0005: ('FlashIntensity', + {11: 'Weak', + 13: 'Normal', + 15: 'Strong'}), + 0x0006: ('Object Distance', ), + 0x0007: ('WhiteBalance', + {1: 'Auto', + 2: 'Tungsten', + 3: 'Daylight', + 4: 'Fluorescent', + 5: 'Shade', + 129: 'Manual'}), + 0x000B: ('Sharpness', + {0: 'Normal', + 1: 'Soft', + 2: 'Hard'}), + 0x000C: ('Contrast', + {0: 'Normal', + 1: 'Low', + 2: 'High'}), + 0x000D: ('Saturation', + {0: 'Normal', + 1: 'Low', + 2: 'High'}), + 0x0014: ('CCDSpeed', + {64: 'Normal', + 80: 'Normal', + 100: 'High', + 125: '+1.0', + 244: '+3.0', + 250: '+2.0',}) + } + +MAKERNOTE_FUJIFILM_TAGS={ + 0x0000: ('NoteVersion', lambda x: ''.join(map(chr, x))), + 0x1000: ('Quality', ), + 0x1001: ('Sharpness', + {1: 'Soft', + 2: 'Soft', + 3: 'Normal', + 4: 'Hard', + 5: 'Hard'}), + 0x1002: ('WhiteBalance', + {0: 'Auto', + 256: 'Daylight', + 512: 'Cloudy', + 768: 'DaylightColor-Fluorescent', + 769: 'DaywhiteColor-Fluorescent', + 770: 'White-Fluorescent', + 1024: 'Incandescent', + 3840: 'Custom'}), + 0x1003: ('Color', + {0: 'Normal', + 256: 'High', + 512: 'Low'}), + 0x1004: ('Tone', + {0: 'Normal', + 256: 'High', + 512: 'Low'}), + 0x1010: ('FlashMode', + {0: 'Auto', + 1: 'On', + 2: 'Off', + 3: 'Red Eye Reduction'}), + 0x1011: ('FlashStrength', ), + 0x1020: ('Macro', + {0: 'Off', + 1: 'On'}), + 0x1021: ('FocusMode', + {0: 'Auto', + 1: 'Manual'}), + 0x1030: ('SlowSync', + {0: 'Off', + 1: 'On'}), + 0x1031: ('PictureMode', + {0: 'Auto', + 1: 'Portrait', + 2: 'Landscape', + 4: 'Sports', + 5: 'Night', + 6: 'Program AE', + 256: 'Aperture Priority AE', + 512: 'Shutter Priority AE', + 768: 'Manual Exposure'}), + 0x1100: ('MotorOrBracket', + {0: 'Off', + 1: 'On'}), + 0x1300: ('BlurWarning', + {0: 'Off', + 1: 'On'}), + 0x1301: ('FocusWarning', + {0: 'Off', + 1: 'On'}), + 0x1302: ('AEWarning', + {0: 'Off', + 1: 'On'}) + } + +MAKERNOTE_CANON_TAGS={ + 0x0006: ('ImageType', ), + 0x0007: ('FirmwareVersion', ), + 0x0008: ('ImageNumber', ), + 0x0009: ('OwnerName', ) + } + +# see http://www.burren.cx/david/canon.html by David Burren +# this is in element offset, name, optional value dictionary format +MAKERNOTE_CANON_TAG_0x001={ + 1: ('Macromode', + {1: 'Macro', + 2: 'Normal'}), + 2: ('SelfTimer', ), + 3: ('Quality', + {2: 'Normal', + 3: 'Fine', + 5: 'Superfine'}), + 4: ('FlashMode', + {0: 'Flash Not Fired', + 1: 'Auto', + 2: 'On', + 3: 'Red-Eye Reduction', + 4: 'Slow Synchro', + 5: 'Auto + Red-Eye Reduction', + 6: 'On + Red-Eye Reduction', + 16: 'external flash'}), + 5: ('ContinuousDriveMode', + {0: 'Single Or Timer', + 1: 'Continuous'}), + 7: ('FocusMode', + {0: 'One-Shot', + 1: 'AI Servo', + 2: 'AI Focus', + 3: 'MF', + 4: 'Single', + 5: 'Continuous', + 6: 'MF'}), + 10: ('ImageSize', + {0: 'Large', + 1: 'Medium', + 2: 'Small'}), + 11: ('EasyShootingMode', + {0: 'Full Auto', + 1: 'Manual', + 2: 'Landscape', + 3: 'Fast Shutter', + 4: 'Slow Shutter', + 5: 'Night', + 6: 'B&W', + 7: 'Sepia', + 8: 'Portrait', + 9: 'Sports', + 10: 'Macro/Close-Up', + 11: 'Pan Focus'}), + 12: ('DigitalZoom', + {0: 'None', + 1: '2x', + 2: '4x'}), + 13: ('Contrast', + {0xFFFF: 'Low', + 0: 'Normal', + 1: 'High'}), + 14: ('Saturation', + {0xFFFF: 'Low', + 0: 'Normal', + 1: 'High'}), + 15: ('Sharpness', + {0xFFFF: 'Low', + 0: 'Normal', + 1: 'High'}), + 16: ('ISO', + {0: 'See ISOSpeedRatings Tag', + 15: 'Auto', + 16: '50', + 17: '100', + 18: '200', + 19: '400'}), + 17: ('MeteringMode', + {3: 'Evaluative', + 4: 'Partial', + 5: 'Center-weighted'}), + 18: ('FocusType', + {0: 'Manual', + 1: 'Auto', + 3: 'Close-Up (Macro)', + 8: 'Locked (Pan Mode)'}), + 19: ('AFPointSelected', + {0x3000: 'None (MF)', + 0x3001: 'Auto-Selected', + 0x3002: 'Right', + 0x3003: 'Center', + 0x3004: 'Left'}), + 20: ('ExposureMode', + {0: 'Easy Shooting', + 1: 'Program', + 2: 'Tv-priority', + 3: 'Av-priority', + 4: 'Manual', + 5: 'A-DEP'}), + 23: ('LongFocalLengthOfLensInFocalUnits', ), + 24: ('ShortFocalLengthOfLensInFocalUnits', ), + 25: ('FocalUnitsPerMM', ), + 28: ('FlashActivity', + {0: 'Did Not Fire', + 1: 'Fired'}), + 29: ('FlashDetails', + {14: 'External E-TTL', + 13: 'Internal Flash', + 11: 'FP Sync Used', + 7: '2nd("Rear")-Curtain Sync Used', + 4: 'FP Sync Enabled'}), + 32: ('FocusMode', + {0: 'Single', + 1: 'Continuous'}) + } + +MAKERNOTE_CANON_TAG_0x004={ + 7: ('WhiteBalance', + {0: 'Auto', + 1: 'Sunny', + 2: 'Cloudy', + 3: 'Tungsten', + 4: 'Fluorescent', + 5: 'Flash', + 6: 'Custom'}), + 9: ('SequenceNumber', ), + 14: ('AFPointUsed', ), + 15: ('FlashBias', + {0XFFC0: '-2 EV', + 0XFFCC: '-1.67 EV', + 0XFFD0: '-1.50 EV', + 0XFFD4: '-1.33 EV', + 0XFFE0: '-1 EV', + 0XFFEC: '-0.67 EV', + 0XFFF0: '-0.50 EV', + 0XFFF4: '-0.33 EV', + 0X0000: '0 EV', + 0X000C: '0.33 EV', + 0X0010: '0.50 EV', + 0X0014: '0.67 EV', + 0X0020: '1 EV', + 0X002C: '1.33 EV', + 0X0030: '1.50 EV', + 0X0034: '1.67 EV', + 0X0040: '2 EV'}), + 19: ('SubjectDistance', ) + } + +# extract multibyte integer in Motorola format (little endian) +def s2n_motorola(str): + x=0 + for c in str: + x=(x << 8) | ord(c) + return x + +# extract multibyte integer in Intel format (big endian) +def s2n_intel(str): + x=0 + y=0L + for c in str: + x=x | (ord(c) << y) + y=y+8 + return x + +# ratio object that eventually will be able to reduce itself to lowest +# common denominator for printing +def gcd(a, b): + if b == 0: + return a + else: + return gcd(b, a % b) + +class Ratio: + def __init__(self, num, den): + self.num=num + self.den=den + + def __repr__(self): + self.reduce() + if self.den == 1: + return str(self.num) + return '%d/%d' % (self.num, self.den) + + def reduce(self): + div=gcd(self.num, self.den) + if div > 1: + self.num=self.num/div + self.den=self.den/div + +# for ease of dealing with tags +class IFD_Tag: + def __init__(self, printable, tag, field_type, values, field_offset, + field_length): + # printable version of data + self.printable=printable + # tag ID number + self.tag=tag + # field type as index into FIELD_TYPES + self.field_type=field_type + # offset of start of field in bytes from beginning of IFD + self.field_offset=field_offset + # length of data field in bytes + self.field_length=field_length + # either a string or array of data items + self.values=values + + def __str__(self): + return self.printable + + def __repr__(self): + return '(0x%04X) %s=%s @ %d' % (self.tag, + FIELD_TYPES[self.field_type][2], + self.printable, + self.field_offset) + +# class that handles an EXIF header +class EXIF_header: + def __init__(self, file, endian, offset, fake_exif, debug=0): + self.file=file + self.endian=endian + self.offset=offset + self.fake_exif=fake_exif + self.debug=debug + self.tags={} + + # 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. + def s2n(self, offset, length, signed=0): + self.file.seek(self.offset+offset) + slice=self.file.read(length) + if self.endian == 'I': + val=s2n_intel(slice) + else: + val=s2n_motorola(slice) + # Sign extension ? + if signed: + msb=1L << (8*length-1) + if val & msb: + val=val-(msb << 1) + return val + + # convert offset to string + def n2s(self, offset, length): + s='' + for i in range(length): + if self.endian == 'I': + s=s+chr(offset & 0xFF) + else: + s=chr(offset & 0xFF)+s + offset=offset >> 8 + return s + + # return first IFD + def first_IFD(self): + return self.s2n(4, 4) + + # return pointer to next IFD + def next_IFD(self, ifd): + entries=self.s2n(ifd, 2) + return self.s2n(ifd+2+12*entries, 4) + + # return list of IFDs in header + def list_IFDs(self): + i=self.first_IFD() + a=[] + while i: + a.append(i) + i=self.next_IFD(i) + return a + + # return list of entries in this IFD + def dump_IFD(self, ifd, ifd_name, dict=EXIF_TAGS, relative=0): + entries=self.s2n(ifd, 2) + for i in range(entries): + # entry is index of start of this IFD in the file + entry=ifd+2+12*i + tag=self.s2n(entry, 2) + # get tag name. We do it early to make debugging easier + tag_entry=dict.get(tag) + if tag_entry: + tag_name=tag_entry[0] + else: + tag_name='Tag 0x%04X' % tag + field_type=self.s2n(entry+2, 2) + if not 0 < field_type < len(FIELD_TYPES): + # unknown field type + raise ValueError, \ + 'unknown type %d in tag 0x%04X' % (field_type, tag) + typelen=FIELD_TYPES[field_type][0] + count=self.s2n(entry+4, 4) + offset=entry+8 + if count*typelen > 4: + # offset is not the value; it's a pointer to the value + # if relative we set things up so s2n will seek to the right + # place when it adds self.offset. Note that this 'relative' + # is for the Nikon type 3 makernote. Other cameras may use + # other relative offsets, which would have to be computed here + # slightly differently. + if relative: + tmp_offset=self.s2n(offset, 4) + offset=tmp_offset+ifd-self.offset+4 + if self.fake_exif: + offset=offset+18 + else: + offset=self.s2n(offset, 4) + field_offset=offset + if field_type == 2: + # special case: null-terminated ASCII string + if count != 0: + self.file.seek(self.offset+offset) + values=self.file.read(count) + values=values.strip().replace('\x00','') + else: + values='' + else: + values=[] + signed=(field_type in [6, 8, 9, 10]) + for j in range(count): + if field_type in (5, 10): + # a ratio + value_j=Ratio(self.s2n(offset, 4, signed), + self.s2n(offset+4, 4, signed)) + else: + value_j=self.s2n(offset, typelen, signed) + values.append(value_j) + offset=offset+typelen + # now "values" is either a string or an array + if count == 1 and field_type != 2: + printable=str(values[0]) + else: + printable=str(values) + # compute printable version of values + if tag_entry: + if len(tag_entry) != 1: + # optional 2nd tag element is present + if callable(tag_entry[1]): + # call mapping function + printable=tag_entry[1](values) + else: + printable='' + for i in values: + # use lookup table for this tag + printable+=tag_entry[1].get(i, repr(i)) + self.tags[ifd_name+' '+tag_name]=IFD_Tag(printable, tag, + field_type, + values, field_offset, + count*typelen) + if self.debug: + print ' debug: %s: %s' % (tag_name, + repr(self.tags[ifd_name+' '+tag_name])) + + # extract uncompressed TIFF thumbnail (like pulling teeth) + # we take advantage of the pre-existing layout in the thumbnail IFD as + # much as possible + def extract_TIFF_thumbnail(self, thumb_ifd): + entries=self.s2n(thumb_ifd, 2) + # this is header plus offset to IFD ... + if self.endian == 'M': + tiff='MM\x00*\x00\x00\x00\x08' + else: + tiff='II*\x00\x08\x00\x00\x00' + # ... plus thumbnail IFD data plus a null "next IFD" pointer + self.file.seek(self.offset+thumb_ifd) + tiff+=self.file.read(entries*12+2)+'\x00\x00\x00\x00' + + # fix up large value offset pointers into data area + for i in range(entries): + 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 + # 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[:ptr]+self.n2s(newoff, 4)+tiff[ptr+4:] + # remember strip offsets location + if tag == 0x0111: + strip_off=newoff + strip_len=4 + # get original data and store it + self.file.seek(self.offset+oldoff) + tiff+=self.file.read(count*typelen) + + # add pixel strips and update strip offset info + old_offsets=self.tags['Thumbnail StripOffsets'].values + old_counts=self.tags['Thumbnail StripByteCounts'].values + for i in range(len(old_offsets)): + # update offset pointer (more nasty "strings are immutable" crap) + offset=self.n2s(len(tiff), strip_len) + tiff=tiff[:strip_off]+offset+tiff[strip_off+strip_len:] + 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 + + # decode all the camera-specific MakerNote formats + + # 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.) + def decode_maker_note(self): + note=self.tags['EXIF MakerNote'] + make=self.tags['Image Make'].printable + model=self.tags['Image Model'].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 in ('NIKON', 'NIKON CORPORATION'): + if note.values[0:7] == [78, 105, 107, 111, 110, 00, 01]: + if self.debug: + print "Looks like a type 1 Nikon MakerNote." + self.dump_IFD(note.field_offset+8, 'MakerNote', + dict=MAKERNOTE_NIKON_OLDER_TAGS) + elif note.values[0:7] == [78, 105, 107, 111, 110, 00, 02]: + if self.debug: + print "Looks like a labeled type 2 Nikon MakerNote" + if note.values[12:14] != [0, 42] and note.values[12:14] != [42L, 0L]: + raise ValueError, "Missing marker tag '42' in MakerNote." + # skip the Makernote label and the TIFF header + self.dump_IFD(note.field_offset+10+8, 'MakerNote', + dict=MAKERNOTE_NIKON_NEWER_TAGS, relative=1) + else: + # E99x or D1 + if self.debug: + print "Looks like an unlabeled type 2 Nikon MakerNote" + self.dump_IFD(note.field_offset, 'MakerNote', + dict=MAKERNOTE_NIKON_NEWER_TAGS) + return + + # Olympus + if make[:7] == 'OLYMPUS': + self.dump_IFD(note.field_offset+8, 'MakerNote', + dict=MAKERNOTE_OLYMPUS_TAGS) + return + + # Casio + if make == 'Casio': + self.dump_IFD(note.field_offset, 'MakerNote', + dict=MAKERNOTE_CASIO_TAGS) + return + + # 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 + + # Canon + if make == 'Canon': + self.dump_IFD(note.field_offset, 'MakerNote', + dict=MAKERNOTE_CANON_TAGS) + for i in (('MakerNote Tag 0x0001', MAKERNOTE_CANON_TAG_0x001), + ('MakerNote Tag 0x0004', MAKERNOTE_CANON_TAG_0x004)): + self.canon_decode_tag(self.tags[i[0]].values, i[1]) + return + + # decode Canon MakerNote tag based on offset within tag + # see http://www.burren.cx/david/canon.html by David Burren + def canon_decode_tag(self, value, dict): + for i in range(1, len(value)): + x=dict.get(i, ('Unknown', )) + if self.debug: + print i, x + name=x[0] + if len(x) > 1: + val=x[1].get(value[i], 'Unknown') + else: + val=value[i] + # 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) + +# process an image file (expects an open file object) +# this is the function that has to deal with all the arbitrary nasty bits +# of the EXIF standard +def process_file(file, debug=0): + # determine whether it's a JPEG or TIFF + data=file.read(12) + fake_exif=0 + if data[0:4] in ['II*\x00', 'MM\x00*']: + # it's a TIFF file + file.seek(0) + endian=file.read(1) + file.read(1) + offset=0 + elif data[0:2] == '\xFF\xD8': + # it's a JPEG file + # skip JFIF style header(s) + while data[2] == '\xFF' and data[6:10] in ('JFIF', 'JFXX', 'OLYM'): + length=ord(data[4])*256+ord(data[5]) + file.read(length-8) + # fake an EXIF beginning of file + data='\xFF\x00'+file.read(10) + fake_exif=1 + if data[2] == '\xFF' and data[6:10] == 'Exif': + # detected EXIF header + offset=file.tell() + endian=file.read(1) + else: + # no EXIF information + return {} + else: + # file format not recognized + return {} + + # deal with the EXIF info we found + if debug: + print {'I': 'Intel', 'M': 'Motorola'}[endian], 'format' + hdr=EXIF_header(file, endian, offset, fake_exif, debug) + ifd_list=hdr.list_IFDs() + ctr=0 + for i in ifd_list: + if ctr == 0: + IFD_name='Image' + elif ctr == 1: + IFD_name='Thumbnail' + thumb_ifd=i + else: + IFD_name='IFD %d' % ctr + if debug: + print ' IFD %d (%s) at offset %d:' % (ctr, IFD_name, i) + hdr.dump_IFD(i, IFD_name) + # EXIF IFD + exif_off=hdr.tags.get(IFD_name+' ExifOffset') + if exif_off: + if debug: + print ' EXIF SubIFD at offset %d:' % exif_off.values[0] + hdr.dump_IFD(exif_off.values[0], 'EXIF') + # Interoperability IFD contained in EXIF IFD + intr_off=hdr.tags.get('EXIF SubIFD InteroperabilityOffset') + if intr_off: + if debug: + print ' EXIF Interoperability SubSubIFD at offset %d:' \ + % intr_off.values[0] + hdr.dump_IFD(intr_off.values[0], 'EXIF Interoperability', + dict=INTR_TAGS) + # GPS IFD + gps_off=hdr.tags.get(IFD_name+' GPSInfo') + if gps_off: + if debug: + print ' GPS SubIFD at offset %d:' % gps_off.values[0] + hdr.dump_IFD(gps_off.values[0], 'GPS', dict=GPS_TAGS) + ctr+=1 + + # extract uncompressed TIFF thumbnail + thumb=hdr.tags.get('Thumbnail Compression') + if thumb and thumb.printable == 'Uncompressed TIFF': + hdr.extract_TIFF_thumbnail(thumb_ifd) + + # JPEG thumbnail (thankfully the JPEG data is stored as a unit) + thumb_off=hdr.tags.get('Thumbnail JPEGInterchangeFormat') + if thumb_off: + file.seek(offset+thumb_off.values[0]) + size=hdr.tags['Thumbnail JPEGInterchangeFormatLength'].values[0] + hdr.tags['JPEGThumbnail']=file.read(size) + + # deal with MakerNote contained in EXIF IFD + if hdr.tags.has_key('EXIF MakerNote'): + hdr.decode_maker_note() + + # Sometimes in a TIFF file, a JPEG thumbnail is hidden in the MakerNote + # since it's not allowed in a uncompressed TIFF IFD + if not hdr.tags.has_key('JPEGThumbnail'): + thumb_off=hdr.tags.get('MakerNote JPEGThumbnail') + if thumb_off: + file.seek(offset+thumb_off.values[0]) + hdr.tags['JPEGThumbnail']=file.read(thumb_off.field_length) + + return hdr.tags + +# library test/debug function (dump given files) +if __name__ == '__main__': + import sys + + if len(sys.argv) < 2: + print 'Usage: %s files...\n' % sys.argv[0] + sys.exit(0) + + for filename in sys.argv[1:]: + try: + file=open(filename, 'rb') + except: + print filename, 'unreadable' + print + continue + print filename+':' + # data=process_file(file, 1) # with debug info + data=process_file(file) + if not data: + print 'No EXIF information found' + continue + + x=data.keys() + x.sort() + for i in x: + if i in ('JPEGThumbnail', 'TIFFThumbnail'): + continue + try: + print ' %s (%s): %s' % \ + (i, FIELD_TYPES[data[i].field_type][2], data[i].printable) + except: + print 'error', i, '"', data[i], '"' + if data.has_key('JPEGThumbnail'): + print 'File has JPEG thumbnail' + print diff --git a/ui/medashare/metadata/__init__.py b/ui/medashare/metadata/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ui/medashare/metadata/crw.py b/ui/medashare/metadata/crw.py new file mode 100644 index 0000000..02e9229 --- /dev/null +++ b/ui/medashare/metadata/crw.py @@ -0,0 +1,1234 @@ +#!/usr/bin/env python + +from .BitField import BitField + +from fractions import Fraction +from io import BytesIO + +import enum +import io +import pathlib +import string +import struct + + +# At least for Canon G2 CRW's + +class TagName: + '''Create an object that lets you compare against either + a name, or a integer pair.''' + + _cache = {} + _names = set() + _unkn = set() + + def __new__(cls, name, tag): + try: + r = cls._cache[tag] + + if r.name != name: + raise RuntimeError('tag %s created with different name' % repr(tag)) + + except KeyError: + pass + + if name in cls._names: + raise RuntimeError('name %s already defined' % repr(name)) + + r = super().__new__(cls) + cls.__init__(r, name, tag) + + cls._cache[tag] = r + cls._names.add(name) + + return r + + @classmethod + def find(cls, tag): + '''Return the proper TagName for the specified tag.''' + + try: + return cls._cache[tag] + except KeyError: + cls._unkn.add(tag) + return tag + + def __init__(self, name, tag): + self._name = name + self._tag = tag + + @property + def name(self): + '''The name of the tag. This is a str.''' + + return self._name + + @property + def tag(self): + '''The tag. This is a pair of ints.''' + + return self._tag + + def __hash__(self): + return hash(self.tag) + + def __lt__(self, o): + return self.tag < o + + def __gt__(self, o): + return self.tag > o + + def __getitem__(self, k): + return self.tag[k] + + def __repr__(self): + return 'TagName(%s, %s)' % (repr(self._name), repr(self._tag)) + + def __eq__(self, o): + if self._name == o or self._tag == o: + return True + + return False + +_tags = { + # Unknown what the prefix should be + 'UNKN_COLORINFO': (0, 0x32), + 'UNKN_COLORINFO2': (2, 44), + 'UNKN_UNKNOWN_1': (0, 0x36), + 'UNKN_UNKNOWN_2': (2, 127), + 'UNKN_ORIGFNAME': (1, 0x16), + 'UNKN_ORIGTHMBFNAME': (1, 0x17), + + # General CRW info, at base + 'CRW_CCDDATA': (4, 5), + 'CRW_THMBBIG': (4, 7), + 'CRW_THMBSML': (4, 8), + 'CRW_FREEBYTES': (0, 1), + + # Picture Information + 'INFO_TIME': (3, 14), # Image time stamp (seconds since epoch) + 'INFO_IMAGE': (3, 16), # Image info (xres, yres, aspect, cw rotation, bit depth, color bit depth, colorbw) + 'INFO_IMGFMT': (3, 3), # 0x20001 for CRW followed by 32-bit float for target compression + 'INFO_TARGETTYPE': (2, 10), + 'INFO_IMGTYPE': (1, 0x15), + 'INFO_DESCRIPTION': (1, 5), + + 'EXPOSEINFO_FOCAL': (2, 41), # Focal Info ( focal mm, sensor x in mils, sensor y in mils) + 'EXPOSEINFO_EXPOSEINFO': (2, 42), # Exposure Info + 'EXPOSEINFO_CAMERASETTINGS': (2, 45), # (macro, self-timer, quality, flash, drive, focus, record, image size, easymode, digital zoom, contrast, saturation, sharpness, cameraiso, metering, focus range, afpoint, exposuremode, lenstype, long focal, short focal, maxap, minap, flash activity, flash bits, focus continuous, aesetting, imagestabilization, display ap, zoomsource width, target, photo effect, color tone) + 'EXPOSEINFO_SENSORPROPS': (2, 49), #Sensor Props ( len, sensor width, sensor height, ?, ?, left offset, top offset, ?, last vertical, last horz, ... ) + 'EXPOSEINFO_BASEISO': (2, 28), + 'EXPOSEINFO_FLASHINFO': (2, 40), + 'EXPOSEINFO_WHITESAMPLE': (2, 48), + + # Camera Information + 'CAMERA_IDENT': (1, 16), # Camera Owner + 'CAMERA_MAKEMODEL': (1, 10), # Camera Make and Model + 'CAMERA_SHUTTERCOUNT': (3, 4), + 'CAMERA_SERIALNO': (3, 11), + 'CAMERA_MODELID': (3, 0x34), # Camera model id + 'CAMERA_DECODERTABLE': (3, 0x35), + + 'MANUFACTURE_COUNTRY': (1, 13), # Camera Counter + 'MANUFACTURE_FIRMWARE': (1, 11), # Camera Firmware + unknown + + 'CRW_INFO': (6, 10), # Everything useful is under this + + 'INFO_CAMERA_SPEC': (5, 4), + 'INFO_CAMERA': (5, 7), # Info about this camera + 'INFO_MEASURE': (6, 3), + 'INFO_EXPOSEINFO': (6, 11), # Image Exposure Settings + + 'CAMERA_MANUFACTURE': (6, 4), # Camera Manufacture Info +} + +_inv_tags = { v: TagName(k, v) for k, v in _tags.items() } + +for i in _tags: + locals()[i] = _inv_tags[_tags[i]] + +HEAPFILEHEADER = "2sI4s4sIII" +DC_RECORD_ENTRY = "HII" +DC_OFFSETTBL = "H" + +#type, bits 13-11: +#000: single data record (byte alignment ) +#001: single data record (byte alignment character string) +#010: single data record (2 byte alignment) +#011: single data record (4 byte alignment) +#100: single data record (arbitrary structure, treated as BYTE alignment) +#101: heap record +#110: heap record +#111: reserved +gettypeCode = lambda x: (x >> 14) & 0x3 +getdataType = lambda x: (x >> 11) & 0x7 +getIDCode = lambda x: x & 0x7ff +heapRECs = [ 5, 6 ] + +class CRWWhiteBalance(enum.IntEnum): + Auto = 0 + Daylight = 1 + Cloudy = 2 + Tungsten = 3 + Fluorescent = 4 + Flash = 5 + Custom = 6 + BlackWhite = 7 + Shade = 8 + Manual = 9 + PCSet1 = 10 + PCSet2 = 11 + PCSet3 = 12 + DaylightFlourescent = 14 + Custom1 = 15 + Custom2 = 16 + Underwater = 17 + +class CRWMacroMode(enum.IntEnum): + Macro = 1 + Normal = 2 + +class CRWQuality(enum.IntEnum): + Economy = 1 + Normal = 2 + Fine = 3 + RAW = 4 + SuperFine = 5 + +class CRWFlashMode(enum.IntEnum): + Off = 0 + Auto = 1 + On = 2 + RedEye = 3 + SlowSync = 4 + RedEyeAuto = 5 + RedEyeOn = 6 + External = 16 + +class CRWDrive(enum.IntEnum): + Single = 0 + Continuous = 1 + Movie = 2 + ContinuousSpeed = 3 + ContinuousLow = 4 + ContinuousHigh = 5 + +class CRWFocusMode(enum.IntEnum): + OneShotAF = 0 + AIServoAF = 1 + AIFocusAF = 2 + Manual = 3 + Single = 4 + Continuous = 5 + ManualF = 6 + PanFocus = 16 + +class CRWRecordMode(enum.IntEnum): + JPEG = 1 + CRW_THM = 2 + AVI_THM = 3 + TIFF = 4 + TIFF_JPEG = 5 + CR2 = 6 + CR2_JPEG = 7 + +class CRWImageSize(enum.IntEnum): + Large = 0 + Medium = 1 + Small = 2 + Medium1 = 5 + Medium2 = 6 + Medium3 = 7 + Postcard = 8 + Widescreen = 9 + +class CRWEasyMode(enum.IntEnum): + FullAuto = 0 + Manual = 1 + Landscape = 2 + FastShutter = 3 + SlowShutter = 4 + Night = 5 + GrayScale = 6 + Sepia = 7 + Portrait = 8 + Sports = 9 + Macro = 10 + BlackWhite = 11 + PanFocus = 12 + Vivid = 13 + Neutral = 14 + FlashOff = 15 + LongShutter = 16 + SuperMacro = 17 + Foliage = 18 + Indoor = 19 + Fireworks = 20 + Beach = 21 + Underwater = 22 + Snow = 23 + KidsPets = 24 + NightSnapshot = 25 + DigitalMacro = 26 + MyColors = 27 + StillImage = 28 + +class CRWDigitalZoom(enum.IntEnum): + No = 0 + x2 = 1 + x4 = 2 + Other = 3 + +class CRWMeteringMode(enum.IntEnum): + Default = 0 + Spot = 1 + Average = 2 + Evaluative = 3 + Partial = 4 + CenterWeightedAveraging = 5 + +class CRWFocusRange(enum.IntEnum): + Manual = 0 + Auto = 1 + NotKnown = 2 + Macro = 3 + VeryClose = 4 + Close = 5 + MiddleRange = 6 + FarRange = 7 + PanFocus = 8 + SuperMacro = 9 + Infinity = 10 + +class CRWAFPoint(enum.IntEnum): + ManualAFPoint = 0x2005 + NoneMF = 0x3000 + AutoSelected = 0x3001 + Right = 0x3002 + Center = 0x3003 + Left = 0x3004 + AutoAFPoint = 0x4001 + +class CRWExposureMode(enum.IntEnum): + Easy = 0 + Program = 1 + ShutterPriority = 2 + AperturePriority = 3 + Manual = 4 + DepthofField = 5 + MDep = 6 + +class CRWLensType(enum.IntEnum): + CanonEF50f1p8 = 1 + CanonEF28f2p8 = 2 + BuiltIn = 65535 + +class CRWFocalUnits(enum.IntEnum): + mm = 32 # Applying scaling, so if not mm, may include scale factor + +class CRWFlashBits(BitField): + Manual = 0 + TTL = 1 + ATTL = 2 + ETTL = 3 + FPSyncEnabled = 4 + Curtain2nd = 7 + FPSyncUsed = 11 + BuiltIn = 13 + External = 14 + +class CRWFocusContinuous(enum.IntEnum): + Single = 0 + Continuous = 1 + +class CRWAESetting(enum.IntEnum): + Normal = 0 + ExposureCompensation = 1 + AELock = 2 + AELockExposureCompensation = 3 + NoAE = 4 + +class CRWImageStabilization(enum.IntEnum): + Off = 0 + On = 1 + OnShotOnly = 2 + NotCapabile = 65535 + +class CRWPhotoEffect(enum.IntEnum): + Off = 0 + Vivid = 1 + Neutral = 2 + Smooth = 3 + Sepia = 4 + BW = 5 + Custom = 6 + MyColorData = 100 + +class CanonMakerNote(enum.IntEnum): + CameraSettings = 1 + FocalLength = 2 + FlashInfo = 3 + ShotInfo = 4 + Model = 6 + FirmwareVersion = 7 + ImageNumber = 8 + OwnerName = 9 + SerialNumber = 0xc + CameraInfo = 0xd + CustomFunctions = 0xf + ModelID = 0x10 + AFInfo = 0x12 + ThumbNailImageArea = 0x13 + SerialNumberFormat = 0x15 + Unknown = 0x19 + OriginalDecisionDataOff = 0x83 + FileInfo = 0x93 + Lens = 0x95 + SerialInfo = 0x96 + ProcessingInfo = 0xa0 + MeasuredColor = 0xaa + ColorSpace = 0xb4 + VRDOffset = 0xd0 + SensorInfo = 0xe0 + ColorData = 0x4001 + CRWParam = 0x4002 + Flavor = 0x4005 + PictureStyleUserDef = 0x4008 + PictureStylePC = 0x4009 + +canonmakernotehandlers = { + CanonMakerNote.FirmwareVersion: lambda fh, endian, res, off: + tuple(res.split(b'\x00', 1)), + CanonMakerNote.Lens: lambda fh, endian, res, off: + res.split(b'\x00', 1)[0], + } + +# Needed by Exif +class TIFFResolutionUnit(enum.IntEnum): + NoUnit = 1 + Inch = 2 + Centimeter = 3 + +class ExifExposureProgram(enum.IntEnum): + NotDefined = 0 + Manual = 1 + Normal = 2 + AperturePriority = 3 + ShutterPriority = 4 + Creative = 5 + Action = 6 + Portrait = 7 + Landscape = 8 + +class ExifComponentsConfiguration(enum.IntEnum): + NotExist = 0 + Y = 1 + Cb = 2 + Cr = 3 + R = 4 + G = 5 + B = 6 + +class ExifColorSpace(enum.IntEnum): + sRGB = 1 + Uncalibrated = 0xffff + +class ExifTag(enum.IntEnum): + ExposureTime = 33434 + FNumber = 33437 + ExposureProgram = 34850 + SpectralSensitivity = 34852 + ISOSpeedRatings = 34855 + OECF = 34856 + ExifVersion = 36864 + DateTimeOriginal = 36867 + DateTimeDigitized = 36868 + ComponentsConfiguration = 37121 + ShutterSpeedValue = 37377 + ApertureValue = 37378 + BrightnessValue = 37379 + ExposureBiasValue = 37380 + MaxApertureValue = 37381 + SubjectDistance = 37382 + MeteringMode = 37383 + LightSource = 37384 + Flash = 37385 + FocalLength = 37386 + SubjectArea = 37396 + MakerNote = 37500 + UserComment = 37510 + SubSecTime = 37520 + SubSecTimeOriginal = 37521 + SubSecTimeDigitized = 37522 + FlashpixVersion = 40960 + ColorSpace = 40961 + PixelXDimension = 40962 + PixelYDimension = 40963 + RelatedSoundFile = 40964 + InteroperabilityIFDPointer = 40965 + FocalPlaneXResolution = 41486 + FocalPlaneYResolution = 41487 + FocalPlaneResolutionUnit = 41488 + SubjectLocation = 41492 + CustomRendered = 41985 + ExposureMode = 41986 + WhiteBalance = 41987 + DigitalZoomRatio = 41988 + FocalLengthIn35mmFilm = 41989 + SceneCaptureType = 41990 + ImageUniqueID = 42016 + +class AutoName(enum.Enum): + def _generate_next_value_(name, start, count, last_values): + return name + +class Unknown(AutoName): + pass + +exifhandlers = { + ExifTag.ExposureProgram: lambda x, y, v, o: + ExifExposureProgram(v[0]), + ExifTag.ComponentsConfiguration: lambda fh, endian, res, off: + tuple(map(lambda x: ExifComponentsConfiguration(x), + res)), + ExifTag.MakerNote: lambda fh, endian, res, off: + IFD(CanonMakerNote, canonmakernotehandlers, fh, endian, off), + ExifTag.ColorSpace: lambda x, y, v, o: ExifColorSpace(v[0]), + ExifTag.InteroperabilityIFDPointer: lambda fh, endian, res, off: + IFD(int, {}, fh, endian, res[0]), + ExifTag.FocalPlaneResolutionUnit: lambda x, y, v, o: + TIFFResolutionUnit(v[0]), + } + +class TIFFCompression(enum.IntEnum): + Uncompressed = 1 + CCITT1D = 2 + Group3Fax = 3 + Group4Fax = 4 + LZW = 5 + JPEG = 6 + PackBits = 32773 + +class TIFFPhotometricInterpretation(enum.IntEnum): + WhiteIsZero = 0 + BlackIsZero = 1 + RGB = 2 + RGBPalette = 3 + TransparencyMask = 4 + CMYK = 5 + YCbCr = 6 + CIELab = 7 + +class TIFFOrientation(enum.IntEnum): + Normal = 1 + MirrorLR = 2 + Rotate180 = 3 + MirrorTB = 4 + MirrorLRRotate90CCW = 5 + Rotate90CW = 6 + MirrorLRRotate90CW = 7 + Rotate90CCW = 8 + +class TIFFTag(enum.IntEnum): + NewSubFileType = 254 + SubfileType = 255 + ImageWidth = 256 + ImageLength = 257 + BitsPerSample = 258 + Compression = 259 + PhotometricInterpretation = 262 + Threshholding = 263 + CellWidth = 264 + CellLength = 265 + FillOrder = 266 + DocumentName = 269 + ImageDescription = 270 + Make = 271 + Model = 272 + StripOffsets = 273 + Orientation = 274 + SamplesPerPixel = 277 + RowsPerStrip = 278 + StripByteCounts = 279 + MinSampleValue = 280 + MaxSampleValue = 281 + XResolution = 282 + YResolution = 283 + PlanarConfiguration = 284 + PageName = 285 + XPosition = 286 + YPosition = 287 + FreeOffsets = 288 + FreeByteCounts = 289 + GrayResponseUnit = 290 + GrayResponseCurve = 291 + T4Options = 292 + T6Options = 293 + ResolutionUnit = 296 + PageNumber = 297 + TransferFunction = 301 + Software = 305 + DateTime = 306 + Artist = 315 + HostComputer = 316 + Predictor = 317 + WhitePoint = 318 + PrimaryChromaticities = 319 + ColorMap = 320 + JPEGInterchangeFormat = 513 + JPEGLength = 514 + YCbCrCoefficients = 529 + YCbCrSubSampling = 530 + YCbCrPositioning = 531 + ReferenceBlackWhite = 532 + Copyright = 33432 + ExifIFDPointer = 34665 + GPSInfoIFDPointer = 34853 + + # possibly: https://web.archive.org/web/20230326011043/https://exiftool.org/TagNames/EXIF.html + CanonSpecificUnknown = 50648 + CanonSpecificUnknown2 = 50649 + CanonSpecificUnknown3 = 50656 # CR2CFAPattern? + CanonSpecificUnknown4 = 50752 # RawImageSegmentation? + +singleitem = lambda fh, endian, x, off: x[0] + +tifftaghandlers = { + TIFFTag.ImageWidth: singleitem, + TIFFTag.ImageLength: singleitem, + TIFFTag.Compression: lambda x, y, v, o: TIFFCompression(v[0]), + TIFFTag.Threshholding: singleitem, # enum + TIFFTag.CellWidth: singleitem, + TIFFTag.CellLength: singleitem, + TIFFTag.FillOrder: singleitem, + TIFFTag.Orientation: lambda x, y, v, o: TIFFOrientation(v[0]), + TIFFTag.ResolutionUnit: lambda x, y, v, o: + TIFFResolutionUnit(v[0]), + TIFFTag.ExifIFDPointer: lambda fh, endian, res, off: + IFD(ExifTag, exifhandlers, fh, endian, res[0]), + } + +class IFD(object): + def __init__(self, enum, taghandlers, fh, endian, off, nextptr=[]): + self.__enum = enum + + del nextptr[:] + r = {} + for tag, res, off in tiff_ifd(fh, endian, off): + if tag is None: + nextptr.append(res) + break + tag = self.getEnum(tag) + if tag in taghandlers: + res = taghandlers[tag](fh, endian, res, off) + + if tag in r: + if isinstance(r[tag], list): + r[tag].append(res) + else: + r[tag] = [r[tag], res] + else: + r[tag] = res + + self.__tags = r + + if tag is not None: + raise RuntimeError('tiff_ifd did not return a None tag') + + def getEnum(self, key): + try: + return self.__enum(key) + except KeyError: + return key + + def __repr__(self): + #names = map(lambda x, y = self: y.getName(x), self.__tags) + names = sorted(self.__tags.keys()) + + # Don't recurse into other IFD's + # If we are a EnumInstance, str it instead of repr, as it + # produces cleaner output. + return '' % ', '.join([ '%s: %s' % (n, + '' if isinstance(self[n], IFD) else + (self[n] if isinstance(self[n], enum.EnumMeta) else + repr(self[n]))) for n in names ]) + + def __getattr__(self, attr): + try: + return self.__tags[getattr(self.__enum, attr)] + except KeyError: + raise AttributeError('tag %s not found' % repr(attr)) + + def __getitem__(self, key): + try: + return self.__tags[key] + except KeyError: + pass + + try: + return self.__tags[getattr(self.__enum, key)] + except (TypeError, AttributeError): + raise KeyError('tag %s not found' % key) + + def __len__(self): + return len(self.__tags) + + def keys(self): + return self.__tags.keys() + + def __iter__(self): + return iter(self.__tags) + +def getstructs(fh, aoff, len, endian, fmt): + fh.seek(aoff) + base = fmt + asz = struct.calcsize(base) + assert(len % asz == 0 and len / asz > 0) + f = "%s%d%s" % (endian, len / asz, base) + data = struct.unpack(f, fh.read(struct.calcsize(f))) + + return data + +def printcode(code, data): + print("0x%04x: %s" % (code, data)) + +def defdata(code, fh, aoff, len, endian): + fh.seek(aoff) + data = fh.read(len) + return data + +def defbyte(code, fh, aoff, len, endian): + data = getstructs(fh, aoff, len, endian, "B") + return data + +def defhalf(code, fh, aoff, len, endian): + data = getstructs(fh, aoff, len, endian, "H") + return data + +def defword(code, fh, aoff, len, endian): + data = getstructs(fh, aoff, len, endian, "I") + return data + +def dofocal(*args): + data = defhalf(*args) + fl = data[1] + if data[0] == 2: + fl /= 32.0 + return (fl, data[2], data[3]) + +def docamerasettings(*args): + data = defhalf(*args) + return ( + CRWMacroMode(data[1]), + data[2], # self-timer + CRWQuality(data[3]), + CRWFlashMode(data[4]), + CRWDrive(data[5]), + CRWFocusMode(data[7]), + CRWRecordMode(data[9]), + CRWImageSize(data[10]), + CRWEasyMode(data[11]), + CRWDigitalZoom(data[12]), + data[13], # contrast + data[14], # saturation + data[15], # sharpness + data[16], # cameraiso + CRWMeteringMode(data[17]), + CRWFocusRange(data[18]), + CRWAFPoint(data[19]), + CRWExposureMode(data[20]), + CRWLensType(data[22]), + data[23] / float(data[25]), # longfocal + data[24] / float(data[25]), # shortfocal + data[26], # max aperture + data[27], # min aperture + data[28], # flash + CRWFlashBits(data[29]), + CRWFocusContinuous(data[32]), + CRWAESetting(data[33]), + CRWImageStabilization(data[34]), + data[35], # display aperture + data[36], # zoom source width + data[37], # zoom target width + ) + (len(data) > 40 and ( + CRWPhotoEffect(data[40]), + data[42], # color tone + ) or ()) + +def doexposeinfo(*args): + data = defhalf(*args) + # May need to do iso * (data[1] + 1) + iso = 2 ** (data[2]/32.0 - 4) * 50 + ap = 2 ** (data[4]/64.0) + shutval = data[5] + if shutval > 32767: # convert to signed + shutval = -(65536 - shutval) + shut = 2 ** (-shutval/32.0) + wbi = data[7] + if wbi > 17: + wbi = 0 + wbi = CRWWhiteBalance(wbi) + if shut > 1e6: + shut = data[24] / 10.0 + return (iso, ap, shut, wbi) + +datahandles = [ + #(4, 7, "Thumbnail Big", None), + #(4, 8, "Thumbnail Small", None), + (1, 0x16, "File CRW", None), + (1, 0x17, "File THM", None), + (2, 0x29, "Focal length (mm), sensor x, y in mils", dofocal), + (2, 0x2a, 'ISO, Aperture, Shutter, WBI', doexposeinfo), + (2, 0x2d, 'Lots of settings', docamerasettings), + (0, None, "def byte alignment", defbyte), + (1, None, "def character string", defdata), + (2, None, "def half words", defhalf), + (3, None, "def words", defword), + (4, None, "def arbitrary structure", defdata), +] + +THMB_BIG = 0x2007 +THMB_SML = 0x2008 +FILE_CRW = 0x0816 +FILE_THM = 0x0817 + +#0805 desc str +#0815 fmt:desc str +#080a make NUL model NUL +#080b firmware str +#0810 owner's name +#080d location? +#102a shr[7] == white balance index +#102c g2? white balance red = shr[51] / shr[50], blue = shr[52] / shr[53] +#0031 eos d30? red = shr[37] / shr[36], blue = shr[38] / shr[39] +#10a9 d60 +#1031 wid = shr[1], height = shr[2] +#1835 decoder table? + +def readstruct(fh, fmt): + return struct.unpack(fmt, fh.read(struct.calcsize(fmt))) + +#''.join(filter(lambda x: x not in string.printable, map(chr, range(256)))) +#lambda x: string.maketrans(x, '.' * len(x)) +#def makeprintable(x, a = string.maketrans + +class heapcontainer(list): + def __init__(self): + list.__init__(self) + + def append(self, value): + assert isinstance(value, tuple) + assert isinstance(value[0], (tuple, TagName)) + assert len(value) == 2 + assert len(value[0]) == 2 if isinstance(value[0], tuple) else True + list.append(self, value) + + def __getitem__(self, key): + if isinstance(key, int): + return list.__getitem__(self, key) + + for i in self: + if key == i[0]: + return i[1] + + raise KeyError("unknown key: %s" % key) + + def keys(self): + return map(lambda x: x[0], self) + + def find(self, k): + r = self.searchheap(lambda k, v, matk=k: k == matk) + + if len(r) != 1: + raise RuntimeError('more than one found: %s' % repr(k)) + + return r[0][2] + + def searchheap(self, fun, ret=None, stack=None): + '''Search all the items in the heap. For each non-container entry + in the heap, the key and value is passed to fun. If the return value + is truthy, then the value is included in the results. + + Return is a list of entries. Each entry is a tuple of (stack, res, value). + stack is a list of the keys to get to the value. + res is the results of the fun call. + value is the value of what was found. + ''' + + if ret is None: + ret = [] + + if stack is None: + stack = [] + + for k, v in self: + if isinstance(v, heapcontainer): + v.searchheap(fun, ret, stack + [ k ]) + else: + try: + mat = fun(k, v) + if mat: + ret.append((stack + [ k ], mat, v)) + except Exception: + pass + + return ret + +# CR2 Info from +# http://www.figuiere.net/hub/blog/?2006/01/15/354-parsing-raw-files: +# The Canon CR2 file is basically a TIFF/EP file, where IFD0 (Image File +# Directory) contains the reduced resolution JPEG preview, IFD1 contains +# the 160x120 thumbnail (this IFD contain only 2 tags, the offset and +# the size of the JPEG data), IFD2 contains an reduced RGB version of +# the image, and IFD3 contain the RAW data (CFA array). When trying to +# read IFD1, I get an error about missing ImageLength, because this IFD +# only contains the offset and the length of the JPEG thumbnail data. + +def tiff_bytes(endian, data): + return map(ord, data) + +def tiff_ascii(endian, data): + if data[-1] != '\x00': + return data # XXX - Canon MakerNote requires this. + #raise ValueError, 'string does not terminate with NUL' + return data[:-1] + +def tiff_short(endian, data): + return struct.unpack(endian + 'H' * (len(data) // 2), data) + +def tiff_long(endian, data): + return struct.unpack(endian + 'I' * (len(data) // 4), data) + +def tiff_slong(endian, data): + return struct.unpack(endian + 'i' * (len(data) // 4), data) + +def rational_dcomp(args): + return tuple(Fraction(args[x], args[x + 1]) for x in range(0, len(args), 2)) + +def tiff_rational(endian, data): + return rational_dcomp(tiff_long(endian, data)) + +def tiff_srational(endian, data): + return rational_dcomp(tiff_slong(endian, data)) + +tifftypes = { + 1: (tiff_bytes, 1), + 2: (tiff_ascii, 1), + 3: (tiff_short, 2), + 4: (tiff_long, 4), + 5: (tiff_rational, 8), + + # Exif Types + 7: (lambda x, y: y, 1), # byte string + 9: (tiff_slong, 4), + 10: (tiff_srational, 8), +} + +TIFF_IFD_CNT = 'H' +TIFF_IFD_ENTRY = 'HHII' +TIFF_IFD_ENTRY_CNT = 4 # number of items returned by TIFF_IFD_ENTRY +TIFF_IFD_OFFSET = 'I' + +def tiff_ifd(fh, endian, off): + fh.seek(off) + cnt = readstruct(fh, endian + TIFF_IFD_CNT)[0] + entries = readstruct(fh, endian + TIFF_IFD_ENTRY * cnt) + nextifd = readstruct(fh, endian + TIFF_IFD_OFFSET)[0] + + for i in range(cnt): + tag, ttype, length, valoff = entries[i * + TIFF_IFD_ENTRY_CNT:(i + 1) * TIFF_IFD_ENTRY_CNT] + typefun, typequantum = tifftypes[ttype] + blength = length * typequantum + if blength <= 4: + # make valoff into a byte stream + data = struct.pack(endian + 'I', valoff)[:blength] + valoff = None + else: + fh.seek(valoff) + data = fh.read(blength) + yield (tag, typefun(endian, data), valoff) + + yield (None, nextifd, None) + +def parse_exif(fh, endian, off): + r = [] + for tag, res in tiff_ifd(fh, endian, off): + if tag is None: + return res, r + r.append((tag, res)) + + raise RuntimeError('tiff_ifd did not return a None tag') + +def parse_ciff(fh, offset, length, endian): + ret = heapcontainer() + #print offset, length + fh.seek(offset + length - struct.calcsize("I")) + tboff = readstruct(fh, endian + "I")[0] + offset + fh.seek(tboff) + nrecs = readstruct(fh, endian + DC_OFFSETTBL)[0] + base = fh.tell() + for i in range(nrecs): + try: + fh.seek(save) + except NameError: + pass + type = readstruct(fh, endian + "H")[0] + typeCode = gettypeCode(type) + if typeCode == 1: + len = 8 + aoff = fh.tell() + fh.read(8) + elif typeCode == 0: + len, roff = readstruct(fh, endian + "II") + aoff = offset + roff; + else: + raise ValueError("Invalid storage: %x" % type >> 14) + + save = fh.tell() + olen = len + #limitlen = 8192 + #if len > limitlen: + # print 'limiting value, originally', len + # len = limitlen + + dataType = getdataType(type) + code = getIDCode(type) + dtc = TagName.find((dataType, code)) + #print (dataType, code), aoff, len + if dataType in heapRECs: + #print 'recursing in parse_ciff, type: %d, code: 0x%04x' % (dataType, code) + ret.append((dtc, parse_ciff(fh, aoff, olen, endian))) + #print 'back' + continue + + for itype, iidcode, string, fun in datahandles: + if itype == dataType and iidcode == None or iidcode == code: + #print "found:", string + if fun: + ret.append((dtc, fun(getIDCode(type), fh, aoff, len, endian))) + break + + continue + + if 1: + if type >> 8 in [ 0x28, 0x30]: + print('recursing in parse_ciff', aoff, olen) + parse_ciff(fh, aoff, olen, endian) + print('back') + elif 0 and type in [ THMB_BIG, THMB_SML ]: + fh.seek(aoff) + open('%x.jpg' % type, "a+").write(fh.read(olen)) + else: + fh.seek(aoff) + data = fh.read(len) + print("%04x: %s" % (type, ''.join(map(lambda x: '%02x' % ord(x), data)))) + print(" %s" % repr(data)) + + elif type == 0x080a: + # handle camera name + pass + elif type == 0x1835: + fh.seek(aoff + 2) + width, height = readstruct(fh, "HH") + + return ret + +def getendian(val): + '''Return '>' (big) or '<' (small) endian depending upon + the passed in value. This is either b'MM' or b'II' for the TIFF + files. + ''' + + if val == b'MM': + endian = '>' + elif val == b'II': + endian = '<' + else: + raise ValueError('unknown byteOffset: %s' % val) + + return endian + +class fileoff: + '''A wrapper around a file object that pretends it + starts at off. + ''' + + def __init__(self, fh, off): + self.fh = fh + self.off = off + + def seek(self, arg, whence=0): + if whence == 0: + arg += self.off + return self.fh.seek(arg, whence) + + def read(self, *args): + return self.fh.read(*args) + + def tell(self): + return self.th.tell() - self.off + +def idcrw(fh): + fh.seek(0) + isjpeg = False + try: + endian = getendian(fh.read(2)) + except ValueError as x: + # Try to see if it's a JPEG file + fh.seek(0) + data = fh.read(12) + if data[:2] != '\xff\xd8': + raise x + + if data[2] != '\xff' or data[6:10] != 'Exif': + raise ValueError('Exif data not at start of JPEG file') + + fh = fileoff(fh, fh.tell()) + endian = getendian(fh.read(2)) + isjpeg = True + + hlen = readstruct(fh, endian + "H")[0] + + if hlen == 0x2a: + #Tiff + hoff, idstr, ver, hlen = readstruct(fh, endian + "I2sHI") + if not isjpeg and hoff < 0x10 and idstr != 'CR' and ver != 2: + raise NotImplementedError('normal TIFF, not a CR2') + nextoff = [ hoff ] + r = [] + while nextoff and nextoff[0] != 0: + # We could make IFD create the object when .next is + # called, and create a magic container class that + # chains as necessary + res = IFD(TIFFTag, tifftaghandlers, fh, endian, nextoff[0], nextoff) + r.append(res) + + return r + + elif hlen == 0x1a: + #CRW + fh.seek(0) + bO, hlen, type, subType, version, rsvd1, rsvd2 = \ + readstruct(fh, endian + HEAPFILEHEADER) + if type != b'HEAP': + raise ValueError('not a heap file: %s' % type) + if subType != b'CCDR': + raise ValueError('not of CCDR subtype: %s' % subType) + if version != 0x00010002: + raise ValueError('incorrect version: %08x' % version) + fh.seek(0, io.SEEK_END) + return parse_ciff(fh, hlen, fh.tell() - hlen, endian) + else: + raise ValueError('unknown value: %d' % hlen) + +def usage(): + sys.stderr.write('Usage: crw.py [ -s regex ] files ...\n') + +__all__ = [ + 'idcrw', +] + list(_tags.keys()) + +if __name__ == '__main__': + import getopt + import pprint + import sys + import re + + try: + opts, args = getopt.getopt(sys.argv[1:], "s:", ["help"]) + except getopt.GetoptError: + # print help information and exit: + usage() + sys.exit(2) + searchreg = None + for o, a in opts: + if o in ("-s"): + searchreg = re.compile(a, re.M) + elif o in ("-h", "--help"): + usage() + sys.exit() + elif o in ("-o", "--output"): + output = a + + for i in args: + print("%s:" % i) + ci = idcrw(open(i)) + if searchreg: + print(ci.searchheap(lambda x, y: searchreg.search(y))) + else: + pprint.pprint(ci) + +import unittest + +class _TestCRW(unittest.TestCase): + def setUp(self): + self.fixtures = pathlib.Path('fixtures').resolve() + + def test_tagname(self): + a = TagName('foo', (1, 2)) + + self.assertEqual(repr(a), "TagName('foo', (1, 2))") + + self.assertEqual(a, 'foo') + self.assertEqual(a, (1, 2)) + + self.assertEqual(a[0], 1) + self.assertEqual(a[1], 2) + + with self.assertRaises(IndexError): + a[3] + + with self.assertRaises(RuntimeError): + TagName('bar', (1, 2)) + + with self.assertRaises(RuntimeError): + TagName('foo', (123, 123123)) + + l = [ a, TagName('bar', (384, 239847234)) ] + l.sort() + set(l) + + def test_bogus(self): + # make sure various bogus "files" raise an error + + with self.assertRaises(ValueError): + idcrw(BytesIO(b'asldfkjasdklfj')) + + with self.assertRaises(ValueError): + idcrw(BytesIO(b'IIldfkjasdklfj')) + + with self.assertRaises(struct.error): + idcrw(BytesIO(b'II\x1a\x00ldfkjasdklfj')) + + with self.assertRaises(ValueError): + idcrw(BytesIO(b'II\x1a\x00ldfkjasdklfjasoijeflsdkfjsldkfj')) + + with self.assertRaises(ValueError): + idcrw(BytesIO(b'II\x1a\x00\x00\x00HEAPldfkjasdklfjasoijeflsdkfjsldkfj')) + + with self.assertRaises(ValueError): + idcrw(BytesIO(b'II\x1a\x00\x00\x00HEAPCCDRldfkjasdklfjasoijeflsdkfjsldkfj')) + + def test_crw(self): + with open(self.fixtures / 'RAW_CANON_G2.CRW', 'rb') as fp: + ci = idcrw(fp) + + self.assertEqual(ci['CRW_INFO']['INFO_EXPOSEINFO']['CAMERA_MODELID'], (17825792, 2222501223)) + self.assertEqual(ci.find('CAMERA_MODELID'), (17825792, 2222501223)) + + #print(repr(list(ci.keys()))) + #print(repr(list(ci['CRW_INFO'].keys()))) + #print('unkn:', sorted(TagName._unkn)) + + # print all the paths + #import pprint + #pprint.pprint(sorted(set(tuple(x[0]) for x in ci.searchheap(lambda k, v: True)))) + + def test_cr2(self): + with open(self.fixtures / 'RAW_CANON_5D_ARGB.CR2', 'rb') as fp: + ci = idcrw(fp) + + self.assertEqual(ci[0][TIFFTag.ExifIFDPointer][ExifTag.ExposureTime][0], Fraction(1, 200)) + + print(repr(ci))