Dont throw EncodingFoundException unless asked to. Should remove the occassional...
[beagle.git] / Filters / FilterJpeg.cs
blob08da8ace45a501777d183a14672526b342fb7dc9
1 //
2 // FilterJpeg.cs
3 //
4 // Copyright (C) 2004 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;
34 using SemWeb;
36 namespace Beagle.Filters {
38 public class FilterJpeg : FilterImage {
40 public FilterJpeg () : base ()
42 AddSupportedFlavor (FilterFlavor.NewFromMimeType ("image/jpeg"));
45 // FIXME: This is not particularly efficient
46 protected override void PullImageProperties ()
48 JpegHeader header = new JpegHeader (Stream);
49 byte [] commentdata = header.GetJFIFComment();
50 if (commentdata != null && commentdata.Length != 0)
52 string comment = System.Text.Encoding.Default.GetString( commentdata, 0, commentdata.Length );
53 if (comment != null && comment != "") {
54 AddProperty (Beagle.Property.New ("jfif:Comment", comment));
55 AddProperty (Beagle.Property.NewUnstored ("fixme:comment", comment));
59 byte [] data = header.GetRawExif ();
60 if (data == null || data.Length == 0)
61 return;
62 ExifData exif = new ExifData (data, (uint) data.Length);
63 if (exif == null)
64 return;
66 string str;
68 str = exif.LookupFirstValue (ExifTag.UserComment);
69 if (str != null && str != "") {
70 AddProperty (Beagle.Property.New ("exif:UserComment", str));
71 AddProperty (Beagle.Property.NewUnstored ("fixme:comment", str));
75 str = exif.LookupFirstValue (ExifTag.ImageDescription);
76 if (str != null && str != "") {
77 AddProperty (Beagle.Property.New ("exif:ImageDescription", str));
78 AddProperty (Beagle.Property.NewUnstored ("fixme:comment", str));
81 str = exif.LookupFirstValue (ExifTag.PixelXDimension);
82 if (str != null && str != "") {
83 Width = Int32.Parse (str);
84 AddProperty (Beagle.Property.NewUnsearched ("exif:PixelXDimension", str));
87 str = exif.LookupFirstValue (ExifTag.PixelYDimension);
88 if (str != null && str != "") {
89 Height = Int32.Parse (str);
90 AddProperty (Beagle.Property.NewUnsearched ("exif:PixelYDimension", str));
93 str = exif.LookupFirstValue (ExifTag.ISOSpeedRatings);
94 if (str != null && str != "")
95 AddProperty (Beagle.Property.NewUnsearched ("exif:ISOSpeedRatings", str));
97 str = exif.LookupFirstValue (ExifTag.ShutterSpeedValue);
98 if (str != null && str != "")
99 AddProperty (Beagle.Property.NewUnsearched ("exif:ShutterSpeedValue", str));
101 str = exif.LookupFirstValue (ExifTag.ExposureTime);
102 if (str != null && str != "")
103 AddProperty (Beagle.Property.NewUnsearched ("exif:ExposureTime", str));
105 str = exif.LookupFirstValue (ExifTag.FNumber);
106 if (str != null && str != "")
107 AddProperty (Beagle.Property.NewUnsearched ("exif:FNumber", str));
109 str = exif.LookupFirstValue (ExifTag.ApertureValue);
110 if (str != null && str != "")
111 AddProperty (Beagle.Property.NewUnsearched ("exif:ApertureValue", str));
113 str = exif.LookupFirstValue (ExifTag.FocalLength);
114 if (str != null && str != "")
115 AddProperty (Beagle.Property.NewUnsearched ("exif:FocalLength", str));
117 str = exif.LookupFirstValue (ExifTag.Flash);
118 if (str != null && str != "")
119 AddProperty (Beagle.Property.NewUnsearched ("exif:Flash", str));
121 str = exif.LookupFirstValue (ExifTag.Model);
122 if (str != null && str != "")
123 AddProperty (Beagle.Property.NewKeyword ("exif:Model", str));
125 str = exif.LookupFirstValue (ExifTag.Copyright);
126 if (str != null && str != "")
127 AddProperty (Beagle.Property.New ("exif:Copyright", str));
129 str = exif.LookupFirstValue (ExifTag.DateTime);
130 if (str != null && str != "") {
131 try {
132 DateTime dt = ExifUtil.DateTimeFromString (str);
133 AddProperty (Beagle.Property.NewDate ("exif:DateTime", dt));
134 } catch (ArgumentOutOfRangeException e) {
135 Logger.Log.Debug("EXIF DateTime '{0}' is invalid.", str);
139 byte [] xmp_data = header.GetRawXmp ();
141 if (xmp_data != null) {
142 int i;
143 for (i = 0; i < xmp_data.Length; i++)
144 if (xmp_data [i] == 0) {
145 i++;
146 break;
149 XmpFile xmp = new XmpFile (new MemoryStream (xmp_data, i, xmp_data.Length - i));
150 AddXmpProperties (xmp);
153 Finished (); // That's all folks...