~ubuntu-branches/ubuntu/jaunty/beagle/jaunty-security

« back to all changes in this revision

Viewing changes to Filters/FilterJpeg.cs

  • Committer: Bazaar Package Importer
  • Author(s): Stefan Ebner
  • Date: 2008-05-04 00:31:32 UTC
  • mfrom: (1.1.21 upstream)
  • Revision ID: james.westby@ubuntu.com-20080504003132-2tkm5o8moo5952ri
Tags: 0.3.7-2ubuntu1
 * Merge from Debian unstable. (LP: #225746) Remaining Ubuntu changes:
  - debian/control:
    + Rename ice{weasel,dove}-beagle to {mozilla,thunderbird}-beagle and
      and update the dependencies accordingly.
    + Change Maintainer to Ubuntu Mono Team.
  - debian/rules:
    + Install the mozilla-beagle and thunderbird-beagle extensions.
  - ice{dove,weasel}.dirs:
    + Renamed to {mozilla,thunderbird}-beagle.dirs.
    + Fixed paths to point to usr/lib/{firefox,thunderbird}

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
// FilterJpeg.cs
3
3
//
4
4
// Copyright (C) 2004 Novell, Inc.
 
5
// Copyright (C) 2008 D Bera <dbera.web@gmail.com>
5
6
//
6
7
 
7
8
//
29
30
using System.Text;
30
31
 
31
32
using Beagle.Util;
32
 
using Beagle.Util.Exif;
33
 
using Beagle.Util.Xmp;
34
 
using Beagle.Util.Iptc;
35
 
using Tiff = Beagle.Util.Tiff;
 
33
using Exif;
 
34
using FSpot.Xmp;
 
35
using FSpot.Iptc;
 
36
using Tiff = FSpot.Tiff;
36
37
using Beagle.Daemon;
37
38
 
38
39
using SemWeb;
39
40
 
40
41
namespace Beagle.Filters {
41
 
        
42
 
        [PropertyKeywordMapping (Keyword="imagemodel", PropertyName="exif:Model", IsKeyword=false, Description="Camera model as specified in exif or IPTC tags")]
43
 
        [PropertyKeywordMapping (Keyword="imagetag", PropertyName="image:tag", IsKeyword=false, Description="FSpot, Digikam image tags")]
44
 
        [PropertyKeywordMapping (Keyword="imagecomment", PropertyName="fspot:Description", IsKeyword=false, Description="F-Spot User comments")]
45
 
        [PropertyKeywordMapping (Keyword="imagecomment", PropertyName="digikam:caption", IsKeyword=false, Description="Digikam User comments")]
46
 
        [PropertyKeywordMapping (Keyword="imagecomment", PropertyName="jfif:Comment", IsKeyword=false, Description="JFIF comments")]
47
 
        [PropertyKeywordMapping (Keyword="imagecomment", PropertyName="exif:UserComment", IsKeyword=false, Description="Exif comments")]
48
 
        [PropertyKeywordMapping (Keyword="imagecomment", PropertyName="iptc:caption", IsKeyword=false, Description="IPTC caption")]
 
42
 
49
43
        public class FilterJpeg : FilterImage {
50
44
 
51
45
                public FilterJpeg ()
66
60
                        AddProperty (Beagle.Property.New ("jfif:Comment", comment));
67
61
                }
68
62
 
 
63
                private static string GetExifString (Tiff.ImageDirectory directory, Tiff.TagId id)
 
64
                {
 
65
                        if (directory == null)
 
66
                                return null;
 
67
 
 
68
                        Tiff.DirectoryEntry entry = directory.Lookup (id);
 
69
                        if (entry == null)
 
70
                                return null;
 
71
 
 
72
                        return entry.ValueAsString [0];
 
73
                }
 
74
 
 
75
                private static double GetExifRational (Tiff.ImageDirectory directory, Tiff.TagId id)
 
76
                {
 
77
                        if (directory == null)
 
78
                                return 0;
 
79
 
 
80
                        Tiff.DirectoryEntry entry = directory.Lookup (id);
 
81
                        if (entry == null)
 
82
                                return -1;
 
83
 
 
84
                        return entry.RationalValue [0].Value;
 
85
                }
 
86
 
 
87
                static string GetFlashString (ushort flash_value)
 
88
                {
 
89
                        switch (flash_value) {
 
90
                        case 0x0000 : return "Flash did not fire";
 
91
                        case 0x0001 : return "Flash fired";
 
92
                        case 0x0005 : return "Strobe return light not detected";
 
93
                        case 0x0007 : return "Strobe return light detected";
 
94
                        case 0x0009 : return "Flash fired, compulsory flash mode";
 
95
                        case 0x000D : return "Flash fired, compulsory flash mode, return light not detected";
 
96
                        case 0x000F : return "Flash fired, compulsory flash mode, return light detected";
 
97
                        case 0x0010 : return "Flash did not fire, compulsory flash mode";
 
98
                        case 0x0018 : return "Flash did not fire, auto mode";
 
99
                        case 0x0019 : return "Flash fired, auto mode";
 
100
                        case 0x001D : return "Flash fired, auto mode, return light not detected";
 
101
                        case 0x001F : return "Flash fired, auto mode, return light detected";
 
102
                        case 0x0020 : return "No flash function";
 
103
                        case 0x0041 : return "Flash fired, red-eye reduction mode";
 
104
                        case 0x0045 : return "Flash fired, red-eye reduction mode, return light not detected";
 
105
                        case 0x0047 : return "Flash fired, red-eye reduction mode, return light detected";
 
106
                        case 0x0049 : return "Flash fired, compulsory flash mode, red-eye reduction mode";
 
107
                        case 0x004D : return "Flash fired, compulsory flash mode, red-eye reduction mode, return light not detected";
 
108
                        case 0x004F : return "Flash fired, compulsory flash mode, red-eye reduction mode, return light detected";
 
109
                        case 0x0059 : return "Flash fired, auto mode, red-eye reduction mode";
 
110
                        case 0x005D : return "Flash fired, auto mode, return light not detected, red-eye reduction mode";
 
111
                        case 0x005F : return "Flash fired, auto mode, return light detected, red-eye reduction mode";
 
112
                        default     : return null;
 
113
                        }
 
114
                }
 
115
 
69
116
                private void AddExifProperties (JpegHeader header)
70
117
                {
71
 
                        ExifData exif = header.Exif;
72
 
                        if (exif == null)
 
118
                        Tiff.Header ifd = header.GetExifHeader ();
 
119
                        if (ifd == null)
73
120
                                return;
 
121
                        //ifd.Dump ("foo"); // Uncomment to debug
74
122
 
75
123
                        string str;
76
 
                        
77
 
                        str = exif.LookupFirstValue (ExifTag.UserComment);
78
 
                        AddProperty (Beagle.Property.New ("exif:UserComment", str));
79
 
 
80
 
                        str = exif.LookupFirstValue (ExifTag.ImageDescription);
 
124
                        Tiff.DirectoryEntry entry;
 
125
 
 
126
                        // Add IFD 0 properies
 
127
 
 
128
                        str = GetExifString (ifd.Directory, Tiff.TagId.Model);
 
129
                        AddProperty (Beagle.Property.New ("exif:Model", str));
 
130
 
 
131
                        str = GetExifString (ifd.Directory, Tiff.TagId.ImageDescription);
81
132
                        AddProperty (Beagle.Property.New ("exif:ImageDescription", str));
82
133
 
83
 
                        str = exif.LookupFirstValue (ExifTag.PixelXDimension);
84
 
                        if (str != null && str != String.Empty) {
85
 
                                Width = Int32.Parse (str);
86
 
                                AddProperty (Beagle.Property.NewUnsearched ("exif:PixelXDimension", str));
87
 
                        }
88
 
 
89
 
                        str = exif.LookupFirstValue (ExifTag.PixelYDimension);
90
 
                        if (str != null && str != String.Empty) {
91
 
                                Height = Int32.Parse (str);
92
 
                                AddProperty (Beagle.Property.NewUnsearched ("exif:PixelYDimension", str));
93
 
                        }
94
 
 
95
 
                        str = exif.LookupFirstValue (ExifTag.ISOSpeedRatings);
 
134
                        try {
 
135
                                entry = ifd.Directory.Lookup (Tiff.TagId.DateTime);
 
136
                                if (entry != null)
 
137
                                        // Assume datetime stored in the images are local times
 
138
                                        AddProperty (Beagle.Property.NewDate ("exif:DateTime", entry.ValueAsDate.ToUniversalTime ()));
 
139
                        } catch (FormatException) {
 
140
                                Logger.Log.Debug ("EXIF DateTime '{0}' is invalid.", GetExifString (ifd.Directory, Tiff.TagId.DateTime));
 
141
                        } catch (ArgumentOutOfRangeException) {
 
142
                                Logger.Log.Debug ("EXIF DateTime '{0}' is invalid.", GetExifString (ifd.Directory, Tiff.TagId.DateTime));
 
143
                        }
 
144
 
 
145
                        str = GetExifString (ifd.Directory, Tiff.TagId.Copyright);
 
146
                        AddProperty (Beagle.Property.New ("dc:rights", str));
 
147
 
 
148
                        // Add IFD 1 properties
 
149
 
 
150
                        Tiff.SubdirectoryEntry subdir = (Tiff.SubdirectoryEntry) ifd.Directory.Lookup (Tiff.TagId.ExifIfdPointer);
 
151
                        if (subdir == null)
 
152
                                return;
 
153
 
 
154
                        Tiff.ImageDirectory exif = subdir.Directory [0];
 
155
                        if (exif == null)
 
156
                                return;
 
157
 
 
158
                        entry = exif.Lookup (Tiff.TagId.UserComment);
 
159
                        if (entry != null)
 
160
                                AddProperty (Beagle.Property.New ("exif:UserComment", entry.UserCommentValue));
 
161
 
 
162
                        uint val = 0;
 
163
                        entry = exif.Lookup (Tiff.TagId.PixelXDimension);
 
164
                        if (entry != null)
 
165
                                val = entry.ValueAsLong [0];
 
166
                        if (val > 0) {
 
167
                                Width = (int) val;
 
168
                                AddProperty (Beagle.Property.NewUnsearched ("exif:PixelXDimension", val));
 
169
                        }
 
170
 
 
171
                        val = 0;
 
172
                        entry = exif.Lookup (Tiff.TagId.PixelYDimension);
 
173
                        if (entry != null)
 
174
                                val = entry.ValueAsLong [0];
 
175
                        if (val > 0) {
 
176
                                Height = (int) val;
 
177
                                AddProperty (Beagle.Property.NewUnsearched ("exif:PixelYDimension", val));
 
178
                        }
 
179
 
 
180
                        str = GetExifString (exif, Tiff.TagId.ISOSpeedRatings);
96
181
                        AddProperty (Beagle.Property.NewUnsearched ("exif:ISOSpeedRatings", str));
97
182
 
98
 
                        str = exif.LookupFirstValue (ExifTag.ShutterSpeedValue);
 
183
                        str = GetExifString (exif, Tiff.TagId.ShutterSpeedValue);
99
184
                        AddProperty (Beagle.Property.NewUnsearched ("exif:ShutterSpeedValue", str));
100
185
 
101
 
                        str = exif.LookupFirstValue (ExifTag.ExposureTime);
102
 
                        AddProperty (Beagle.Property.NewUnsearched ("exif:ExposureTime", str));
103
 
 
104
 
                        str = exif.LookupFirstValue (ExifTag.FNumber);
105
 
                        AddProperty (Beagle.Property.NewUnsearched ("exif:FNumber", str));
106
 
 
107
 
                        str = exif.LookupFirstValue (ExifTag.ApertureValue);
108
 
                        AddProperty (Beagle.Property.NewUnsearched ("exif:ApertureValue", str));
109
 
 
110
 
                        str = exif.LookupFirstValue (ExifTag.FocalLength);
111
 
                        AddProperty (Beagle.Property.NewUnsearched ("exif:FocalLength", str));
112
 
 
113
 
                        str = exif.LookupFirstValue (ExifTag.Flash);
114
 
                        AddProperty (Beagle.Property.NewUnsearched ("exif:Flash", str));
115
 
 
116
 
                        str = exif.LookupFirstValue (ExifTag.Model);
117
 
                        AddProperty (Beagle.Property.New ("exif:Model", str));
118
 
 
119
 
                        str = exif.LookupFirstValue (ExifTag.Copyright);
120
 
                        AddProperty (Beagle.Property.New ("dc:rights", str));
121
 
 
122
 
                        str = exif.LookupFirstValue (ExifTag.DateTime);
123
 
                        if (str != null && str != String.Empty) {
124
 
                                try {
125
 
                                        DateTime dt = ExifUtil.DateTimeFromString (str);
126
 
                                        AddProperty (Beagle.Property.NewDate ("exif:DateTime", dt));
127
 
                                } catch (ArgumentOutOfRangeException) {
128
 
                                        Logger.Log.Debug("EXIF DateTime '{0}' is invalid.", str);
129
 
                                }
 
186
                        str = GetExifString (exif, Tiff.TagId.ExposureTime);
 
187
                        if (! String.IsNullOrEmpty (str))
 
188
                                AddProperty (Beagle.Property.NewUnsearched ("exif:ExposureTime", str + " sec."));
 
189
 
 
190
                        double rational_val;
 
191
 
 
192
                        rational_val = GetExifRational (exif, Tiff.TagId.FNumber);
 
193
                        if (rational_val > 0)
 
194
                                AddProperty (Beagle.Property.NewUnsearched ("exif:FNumber", String.Format ("f/{0}", rational_val)));
 
195
 
 
196
                        rational_val = GetExifRational (exif, Tiff.TagId.ApertureValue);
 
197
                        if (rational_val > 0)
 
198
                                AddProperty (Beagle.Property.NewUnsearched ("exif:ApertureValue", rational_val));
 
199
 
 
200
                        rational_val = GetExifRational (exif, Tiff.TagId.FocalLength);
 
201
                        if (rational_val > 0)
 
202
                                AddProperty (Beagle.Property.NewUnsearched ("exif:FocalLength", String.Format ("{0} mm", rational_val)));
 
203
 
 
204
                        entry = exif.Lookup (Tiff.TagId.Flash);
 
205
                        if (entry != null) {
 
206
                                ushort flash = entry.ShortValue [0];
 
207
                                AddProperty (Beagle.Property.NewUnsearched ("exif:Flash", GetFlashString (flash)));
130
208
                        }
131
 
 
132
209
                }
133
210
 
134
211
                private void AddXmpProperties (JpegHeader header)