MetaData.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. using PaintDotNet.SystemLayer;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Specialized;
  5. using System.Drawing.Imaging;
  6. namespace PaintDotNet
  7. {
  8. /// <summary>
  9. /// This class exposes two types of metadata: system, and user.
  10. /// It is provided mostly for batching operations: loading all the data, modifying the copy,
  11. /// and then saving back all the data.
  12. /// </summary>
  13. public class Metadata
  14. {
  15. /// <summary>
  16. /// This is the name of the section where EXIF tags are stored.
  17. /// </summary>
  18. /// <remarks>
  19. /// All entries in here are expected to be PropertyItem objects which were serialized
  20. /// using PdnGraphics.SerializePropertyItem. The name of each entry in this section is
  21. /// irrelevant, as some EXIF tags are allowed to occur more than once. Thus, if you
  22. /// want to search for EXIF tags of a certain ID you will have to deserialize each
  23. /// one and compare the Id property.
  24. /// It is the responsibility of the FileType implementation to load and save these.
  25. /// </remarks>
  26. public const string ExifSectionName = "$exif";
  27. /// <summary>
  28. /// This is the name of the section where user-defined metadata may go.
  29. /// </summary>
  30. public const string UserSectionName = "$user";
  31. /// <summary>
  32. /// This is the name of the section where the main document metadata goes that
  33. /// can be user-provided but is not necessarily user-defined.
  34. /// </summary>
  35. public const string MainSectionName = "$main";
  36. private NameValueCollection userMetaData;
  37. private const string sectionSeparator = ".";
  38. private int suppressChangeEvents = 0;
  39. public event EventHandler Changing;
  40. protected virtual void OnChanging()
  41. {
  42. if (suppressChangeEvents <= 0 && Changing != null)
  43. {
  44. Changing(this, EventArgs.Empty);
  45. }
  46. }
  47. public event EventHandler Changed;
  48. protected virtual void OnChanged()
  49. {
  50. if (suppressChangeEvents <= 0 && Changed != null)
  51. {
  52. Changed(this, EventArgs.Empty);
  53. }
  54. }
  55. private class ExifInfo
  56. {
  57. public string[] names;
  58. public PropertyItem[] items;
  59. public ExifInfo(string[] names, PropertyItem[] items)
  60. {
  61. this.names = names;
  62. this.items = items;
  63. }
  64. }
  65. private Hashtable exifIdToExifInfo = new Hashtable(); // maps short -> ExifInfo
  66. public string[] GetKeys(string section)
  67. {
  68. string sectionName = section + sectionSeparator;
  69. ArrayList keys = new ArrayList();
  70. foreach (string key in userMetaData.Keys)
  71. {
  72. if (key.StartsWith(sectionName))
  73. {
  74. keys.Add(key.Substring(sectionName.Length));
  75. }
  76. }
  77. return (string[])keys.ToArray(typeof(string));
  78. }
  79. public string[] GetSections()
  80. {
  81. Set sections = new Set();
  82. foreach (string key in userMetaData.Keys)
  83. {
  84. int dotIndex = key.IndexOf(sectionSeparator);
  85. if (dotIndex != -1)
  86. {
  87. string sectionName = key.Substring(0, dotIndex);
  88. if (!sections.Contains(sectionName))
  89. {
  90. sections.Add(sectionName);
  91. }
  92. }
  93. }
  94. return (string[])sections.ToArray(typeof(string));
  95. }
  96. /// <summary>
  97. /// Gets a value from the metadata collection.
  98. /// </summary>
  99. /// <param name="section">The logical section to retrieve from.</param>
  100. /// <param name="name">The name of the value to retrieve.</param>
  101. /// <returns>A string containing the value, or null if the value wasn't present.</returns>
  102. public string GetValue(string section, string name)
  103. {
  104. return userMetaData.Get(section + sectionSeparator + name);
  105. }
  106. public PropertyItem[] GetExifValues(ExifTagID id)
  107. {
  108. return GetExifValues((short)id);
  109. }
  110. public PropertyItem[] GetExifValues(short id)
  111. {
  112. ExifInfo info = (ExifInfo)this.exifIdToExifInfo[id];
  113. if (info == null)
  114. {
  115. return new PropertyItem[0];
  116. }
  117. else
  118. {
  119. return (PropertyItem[])info.items.Clone();
  120. }
  121. }
  122. public string GetUserValue(string name)
  123. {
  124. return GetValue(UserSectionName, name);
  125. }
  126. /// <summary>
  127. /// Removes a value from the metadata collection.
  128. /// </summary>
  129. /// <param name="section">The logical section to remove from.</param>
  130. /// <param name="name">The name of the value to retrieve.</param>
  131. public void RemoveValue(string section, string name)
  132. {
  133. OnChanging();
  134. userMetaData.Remove(section + sectionSeparator + name);
  135. OnChanged();
  136. }
  137. public void ReplaceExifValues(ExifTagID id, PropertyItem[] items)
  138. {
  139. ReplaceExifValues((short)id, items);
  140. }
  141. public void ReplaceExifValues(short id, PropertyItem[] items)
  142. {
  143. OnChanging();
  144. ++suppressChangeEvents;
  145. RemoveExifValues(id);
  146. AddExifValues(items);
  147. --suppressChangeEvents;
  148. OnChanged();
  149. }
  150. public void RemoveExifValues(ExifTagID id)
  151. {
  152. RemoveExifValues((short)id);
  153. }
  154. public void RemoveExifValues(short id)
  155. {
  156. object idObj = (object)id;
  157. ExifInfo info = (ExifInfo)this.exifIdToExifInfo[idObj];
  158. OnChanging();
  159. ++suppressChangeEvents;
  160. if (info != null)
  161. {
  162. foreach (string name in info.names)
  163. {
  164. RemoveValue(ExifSectionName, name);
  165. }
  166. this.exifIdToExifInfo.Remove(idObj);
  167. }
  168. --suppressChangeEvents;
  169. OnChanged();
  170. }
  171. public void RemoveUserValue(string name)
  172. {
  173. RemoveValue(UserSectionName, name);
  174. }
  175. private void SetValueConcrete(string section, string name, string value)
  176. {
  177. OnChanging();
  178. userMetaData.Set(section + sectionSeparator + name, value);
  179. OnChanged();
  180. }
  181. /// <summary>
  182. /// Sets a value in the metadata collection.
  183. /// </summary>
  184. /// <param name="section">The logical section to add or update date in.</param>
  185. /// <param name="name">The name of the value to set.</param>
  186. /// <param name="value">The value to set.</param>
  187. public void SetValue(string section, string name, string value)
  188. {
  189. if (section == ExifSectionName)
  190. {
  191. throw new ArgumentException("you must use AddExifValues() to add items to the " + ExifSectionName + " section");
  192. }
  193. SetValueConcrete(section, name, value);
  194. }
  195. public void SetUserValue(string name, string value)
  196. {
  197. SetValue(Metadata.UserSectionName, name, value);
  198. }
  199. public void AddExifValues(PropertyItem[] items)
  200. {
  201. if (items.Length == 0)
  202. {
  203. return;
  204. }
  205. short id = unchecked((short)items[0].Id);
  206. for (int i = 1; i < items.Length; ++i)
  207. {
  208. if (unchecked((short)items[i].Id) != id)
  209. {
  210. throw new ArgumentException("all PropertyItem instances in items must have the same id");
  211. }
  212. }
  213. string[] names = new string[items.Length];
  214. OnChanging();
  215. ++suppressChangeEvents;
  216. for (int i = 0; i < items.Length; ++i)
  217. {
  218. names[i] = GetUniqueExifName();
  219. string blob = PdnGraphics.SerializePropertyItem(items[i]);
  220. SetValueConcrete(ExifSectionName, names[i], blob);
  221. }
  222. object idObj = (object)id; // avoid boxing twice
  223. ExifInfo info = (ExifInfo)this.exifIdToExifInfo[idObj];
  224. if (info == null)
  225. {
  226. PropertyItem[] newItems = new PropertyItem[items.Length];
  227. for (int i = 0; i < newItems.Length; ++i)
  228. {
  229. newItems[i] = PdnGraphics.ClonePropertyItem(items[i]);
  230. }
  231. info = new ExifInfo(names, newItems);
  232. }
  233. else
  234. {
  235. string[] names2 = new string[info.names.Length + names.Length];
  236. PropertyItem[] items2 = new PropertyItem[info.items.Length + items.Length];
  237. info.names.CopyTo(names2, 0);
  238. names.CopyTo(names2, info.names.Length);
  239. info.items.CopyTo(items2, 0);
  240. for (int i = 0; i < items.Length; ++i)
  241. {
  242. items2[i + info.items.Length] = PdnGraphics.ClonePropertyItem(items[i]);
  243. }
  244. info = new ExifInfo(names2, items2);
  245. }
  246. this.exifIdToExifInfo[idObj] = info;
  247. --suppressChangeEvents;
  248. OnChanged();
  249. }
  250. private int nextUniqueId = 0;
  251. private string GetUniqueExifName()
  252. {
  253. int num = nextUniqueId;
  254. const string namePrefix = "tag";
  255. while (true)
  256. {
  257. string name = namePrefix + num.ToString();
  258. if (GetValue(ExifSectionName, name) == null)
  259. {
  260. nextUniqueId = num + 1;
  261. return name;
  262. }
  263. else
  264. {
  265. ++num;
  266. }
  267. }
  268. }
  269. public void ReplaceWithDataFrom(Metadata source)
  270. {
  271. OnChanging();
  272. ++suppressChangeEvents;
  273. if (source != this && source.userMetaData != this.userMetaData)
  274. {
  275. Clear();
  276. foreach (string key in source.userMetaData.Keys)
  277. {
  278. string value = source.userMetaData.Get(key);
  279. this.userMetaData.Set(key, value);
  280. }
  281. ReconstructExifInfoCache();
  282. }
  283. --suppressChangeEvents;
  284. OnChanged();
  285. }
  286. public void Clear()
  287. {
  288. OnChanging();
  289. ++suppressChangeEvents;
  290. this.userMetaData.Clear();
  291. this.exifIdToExifInfo.Clear();
  292. --suppressChangeEvents;
  293. OnChanged();
  294. }
  295. private void ReconstructExifInfoCache()
  296. {
  297. OnChanging();
  298. ++suppressChangeEvents;
  299. exifIdToExifInfo.Clear();
  300. string[] exifKeys = GetKeys(ExifSectionName);
  301. string[] piBlobs = new string[exifKeys.Length];
  302. for (int i = 0; i < exifKeys.Length; ++i)
  303. {
  304. piBlobs[i] = GetValue(ExifSectionName, exifKeys[i]);
  305. this.RemoveValue(ExifSectionName, exifKeys[i]);
  306. }
  307. foreach (string piBlob in piBlobs)
  308. {
  309. PropertyItem pi = PdnGraphics.DeserializePropertyItem(piBlob);
  310. AddExifValues(new PropertyItem[] { pi });
  311. }
  312. --suppressChangeEvents;
  313. OnChanged();
  314. }
  315. internal Metadata(NameValueCollection userMetaData)
  316. {
  317. this.userMetaData = userMetaData;
  318. ReconstructExifInfoCache();
  319. }
  320. }
  321. }