Merge the recent changes from HEAD onto the branch
[beagle.git] / Filters / FilterTiff.cs
blob9adc7677bd76700b27937f78048b8076d67d23b6
1 //
2 // FilterTiff.cs
3 //
4 // Copyright (C) 2006 Novell, Inc.
5 //
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining a
9 // copy of this software and associated documentation files (the "Software"),
10 // to deal in the Software without restriction, including without limitation
11 // the rights to use, copy, modify, merge, publish, distribute, sublicense,
12 // and/or sell copies of the Software, and to permit persons to whom the
13 // Software is furnished to do so, subject to the following conditions:
15 // The above copyright notice and this permission notice shall be included in
16 // all copies or substantial portions of the Software.
18 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
24 // DEALINGS IN THE SOFTWARE.
27 using System;
28 using System.IO;
29 using System.Text;
31 using Beagle.Util;
32 using Beagle.Daemon;
33 using Beagle.Util.Tiff;
35 using SemWeb;
37 namespace Beagle.Filters {
38 public class FilterTiff : FilterImage {
39 public FilterTiff () : base ()
41 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("image/tiff"));
44 protected override void PullImageProperties ()
46 DirectoryEntry e;
47 Header header = new Header (Stream);
49 e = header.Directory.Lookup (TagId.ExifIfdPointer);
50 if (e != null) {
51 ImageDirectory exif = ((SubdirectoryEntry)e).Directory [0];
52 AddTiffDirectoryProperties (this, exif);
55 e = header.Directory.Lookup (TagId.SubIFDs);
56 if (e != null) {
57 ImageDirectory [] dirs = ((SubdirectoryEntry)e).Directory;
59 foreach (ImageDirectory subdir in dirs)
60 AddTiffDirectoryProperties (this, subdir);
63 // we filter ifd0 last so that we pick up the
64 // width and height from the full resolution
65 // version if it exists
67 AddTiffDirectoryProperties (this, header.Directory);
70 internal static void AddTiffDirectoryProperties (FilterTiff filter, ImageDirectory directory)
72 foreach (DirectoryEntry e in directory.Entries) {
73 switch (e.Id) {
74 case TagId.ImageDescription:
75 filter.AddProperty (Beagle.Property.New ("exif:ImageDescription", e.ValueAsString [0]));
76 break;
77 case TagId.UserComment:
78 filter.AddProperty (Beagle.Property.New ("exif:UserComment", e.ValueAsString [0]));
79 break;
80 case TagId.DateTimeOriginal:
81 AddDateProperty (filter, "exif:DateTimeOriginal", e.ValueAsString [0]);
82 break;
83 case TagId.DateTimeDigitized:
84 AddDateProperty (filter, "exif:DateTimeDigitized", e.ValueAsString [0]);
85 break;
86 case TagId.DateTime:
87 AddDateProperty (filter, "exif:DateTime", e.ValueAsString [0]);
88 break;
89 case TagId.PixelXDimension:
90 filter.Width = (int) e.ValueAsLong [0];
91 //filter.AddProperty (Beagle.Property.NewUnsearched ("exif:PixelXDimension", e.ValueAsString [0]));
92 break;
93 case TagId.PixelYDimension:
94 filter.Height = (int) e.ValueAsLong [0];
95 //filter.AddProperty (Beagle.Property.NewUnsearched ("exif:PixelYDimension", e.ValueAsString [0]));
96 break;
97 case TagId.ImageWidth:
98 uint wval = e.ValueAsLong [0];
99 if (filter.Width < wval)
100 filter.Width = (int) wval;
101 break;
102 case TagId.ImageLength:
103 uint hval = e.ValueAsLong [0];
104 if (filter.Height < hval)
105 filter.Height = (int) hval;
106 break;
107 case TagId.ShutterSpeedValue:
108 case TagId.ExposureTime:
109 case TagId.ISOSpeedRatings:
110 case TagId.ApertureValue:
111 break;
112 case TagId.XMP:
113 XmpFile xmp = new XmpFile (new System.IO.MemoryStream (e.RawData));
114 filter.AddXmpProperties (xmp);
115 break;
120 private static void AddDateProperty (FilterImage filter, string name, string value) {
121 try {
122 DateTime time = DirectoryEntry.DateTimeFromString (value);
123 filter.AddProperty (Beagle.Property.NewDate (name, time));
124 } catch (ArgumentOutOfRangeException) {
125 Logger.Log.Debug ("{0} = '{1}' is invalid.", name, value);
126 } catch (FormatException) {
127 Logger.Log.Debug ("{0} = '{1}' is invalid.", name, value);