forked from timheuer/taglib-sharp-portable
/
File.cs
253 lines (216 loc) · 6.71 KB
/
File.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
//
// File.cs:
//
// Author:
// Ruben Vermeersch (ruben@savanne.be)
//
// Copyright (C) 2009 Ruben Vermeersch
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License version
// 2.1 as published by the Free Software Foundation.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
// USA
//
using System;
using System.Collections.Generic;
using System.IO;
using TagLib.Image;
using TagLib.IFD;
using TagLib.IFD.Entries;
using TagLib.IFD.Tags;
using TagLib.Xmp;
namespace TagLib.Tiff
{
/// <summary>
/// This class extends <see cref="TagLib.Tiff.BaseTiffFile" /> to provide tagging
/// and properties support for Tiff files.
/// </summary>
[SupportedMimeType("taglib/tiff", "tiff")]
[SupportedMimeType("taglib/tif", "tif")]
[SupportedMimeType("image/tiff")]
public class File : BaseTiffFile
{
#region Private Fields
/// <summary>
/// Contains the media properties.
/// </summary>
private Properties properties;
#endregion
#region Constructors
/// <summary>
/// Constructs and initializes a new instance of <see
/// cref="File" /> for a specified file abstraction and
/// specified read style.
/// </summary>
/// <param name="abstraction">
/// A <see cref="File.IFileAbstraction" /> object to use when
/// reading from and writing to the file.
/// </param>
/// <param name="propertiesStyle">
/// A <see cref="ReadStyle" /> value specifying at what level
/// of accuracy to read the media properties, or <see
/// cref="ReadStyle.None" /> to ignore the properties.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="abstraction" /> is <see langword="null"
/// />.
/// </exception>
public File (File.IFileAbstraction abstraction,
ReadStyle propertiesStyle) : base (abstraction)
{
ImageTag = new CombinedImageTag (TagTypes.TiffIFD | TagTypes.XMP);
Mode = AccessMode.Read;
try {
Read (propertiesStyle);
TagTypesOnDisk = TagTypes;
} finally {
Mode = AccessMode.Closed;
}
}
/// <summary>
/// Constructs and initializes a new instance of <see
/// cref="File" /> for a specified file abstraction.
/// </summary>
/// <param name="abstraction">
/// A <see cref="IFileAbstraction" /> object to use when
/// reading from and writing to the file.
/// </param>
/// <exception cref="ArgumentNullException">
/// <paramref name="abstraction" /> is <see langword="null"
/// />.
/// </exception>
protected File (IFileAbstraction abstraction) : base (abstraction)
{
}
#endregion
#region Public Properties
/// <summary>
/// Gets the media properties of the file represented by the
/// current instance.
/// </summary>
/// <value>
/// A <see cref="TagLib.Properties" /> object containing the
/// media properties of the file represented by the current
/// instance.
/// </value>
public override TagLib.Properties Properties {
get { return properties; }
}
#endregion
#region Public Methods
/// <summary>
/// Saves the changes made in the current instance to the
/// file it represents.
/// </summary>
public override void Save ()
{
Mode = AccessMode.Write;
try {
WriteFile ();
TagTypesOnDisk = TagTypes;
} finally {
Mode = AccessMode.Closed;
}
}
#endregion
#region Private Methods
/// <summary>
/// Render the whole file and write it back.
/// </summary>
private void WriteFile ()
{
// Check, if IFD0 is contained
IFDTag exif = ImageTag.Exif;
if (exif == null)
throw new Exception ("Tiff file without tags");
UpdateTags (exif);
// first IFD starts at 8
uint first_ifd_offset = 8;
ByteVector data = RenderHeader (first_ifd_offset);
var renderer = new IFDRenderer (IsBigEndian, exif.Structure, first_ifd_offset);
data.Add (renderer.Render ());
Insert (data, 0, Length);
}
/// <summary>
/// Update the XMP stored in the Tiff IFD
/// </summary>
/// <param name="exif">
/// A <see cref="IFDTag"/> The Tiff IFD to update the entries
/// </param>
private void UpdateTags (IFDTag exif)
{
// update the XMP entry
exif.Structure.RemoveTag (0, (ushort) IFDEntryTag.XMP);
XmpTag xmp = ImageTag.Xmp;
if (xmp != null)
exif.Structure.AddEntry (0, new ByteVectorIFDEntry ((ushort) IFDEntryTag.XMP, xmp.Render ()));
}
/// <summary>
/// Reads the file with a specified read style.
/// </summary>
/// <param name="propertiesStyle">
/// A <see cref="ReadStyle" /> value specifying at what level
/// of accuracy to read the media properties, or <see
/// cref="ReadStyle.None" /> to ignore the properties.
/// </param>
protected void Read (ReadStyle propertiesStyle)
{
Mode = AccessMode.Read;
try {
uint first_ifd_offset = ReadHeader ();
ReadIFD (first_ifd_offset);
// Find XMP data
var xmp_entry = ImageTag.Exif.Structure.GetEntry (0, (ushort) IFDEntryTag.XMP) as ByteVectorIFDEntry;
if (xmp_entry != null) {
ImageTag.AddTag (new XmpTag (xmp_entry.Data.ToString (), this));
}
if (propertiesStyle == ReadStyle.None)
return;
properties = ExtractProperties ();
} finally {
Mode = AccessMode.Closed;
}
}
/// <summary>
/// Attempts to extract the media properties of the main
/// photo.
/// </summary>
/// <returns>
/// A <see cref="Properties" /> object with a best effort guess
/// at the right values. When no guess at all can be made,
/// <see langword="null" /> is returned.
/// </returns>
protected virtual Properties ExtractProperties ()
{
int width = 0, height = 0;
IFDTag tag = GetTag (TagTypes.TiffIFD) as IFDTag;
IFDStructure structure = tag.Structure;
width = (int) (structure.GetLongValue (0, (ushort) IFDEntryTag.ImageWidth) ?? 0);
height = (int) (structure.GetLongValue (0, (ushort) IFDEntryTag.ImageLength) ?? 0);
if (width > 0 && height > 0) {
return new Properties (TimeSpan.Zero, CreateCodec (width, height));
}
return null;
}
/// <summary>
/// Create a codec that describes the photo properties.
/// </summary>
/// <returns>
/// A <see cref="Codec" /> object.
/// </returns>
protected virtual Codec CreateCodec (int width, int height)
{
return new Codec (width, height);
}
#endregion
}
}