/Pinta.Core/Managers/ImageConverterManager.cs

http://github.com/jpobst/Pinta · C# · 220 lines · 115 code · 35 blank · 70 comment · 18 complexity · 5b2fc785ef2117ec3405e6dd8d9b21b2 MD5 · raw file

  1. //
  2. // ImageConverterManager.cs
  3. //
  4. // Author:
  5. // Jonathan Pobst <monkey@jpobst.com>
  6. //
  7. // Copyright (c) 2010 Jonathan Pobst
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. using System;
  27. using System.Collections.Generic;
  28. using System.Linq;
  29. using Gdk;
  30. using System.IO;
  31. namespace Pinta.Core
  32. {
  33. public class ImageConverterManager
  34. {
  35. private List<FormatDescriptor> formats;
  36. public ImageConverterManager ()
  37. {
  38. formats = new List<FormatDescriptor> ();
  39. // Create all the formats supported by Gdk
  40. foreach (var format in Pixbuf.Formats) {
  41. string formatName = format.Name.ToLowerInvariant ();
  42. string formatNameUpperCase = formatName.ToUpperInvariant ();
  43. string[] extensions;
  44. switch (formatName) {
  45. case "jpeg":
  46. extensions = new string[] { "jpg", "jpeg", "JPG", "JPEG" };
  47. break;
  48. case "tiff":
  49. extensions = new string[] { "tif", "tiff", "TIF", "TIFF" };
  50. break;
  51. default:
  52. extensions = new string[] { formatName, formatNameUpperCase };
  53. break;
  54. }
  55. GdkPixbufFormat importer = new GdkPixbufFormat (format.Name.ToLowerInvariant ());
  56. IImageExporter exporter;
  57. if (formatName == "jpeg")
  58. exporter = importer = new JpegFormat ();
  59. else if (formatName == "tga")
  60. exporter = new TgaExporter ();
  61. else if (format.IsWritable)
  62. exporter = importer;
  63. else
  64. exporter = null;
  65. RegisterFormat (new FormatDescriptor (formatNameUpperCase, extensions, importer, exporter));
  66. }
  67. // Create all the formats we have our own importers/exporters for
  68. OraFormat oraHandler = new OraFormat ();
  69. RegisterFormat (new FormatDescriptor ("OpenRaster", new string[] { "ora", "ORA" }, oraHandler, oraHandler));
  70. }
  71. public IEnumerable<FormatDescriptor> Formats { get { return formats; } }
  72. /// <summary>
  73. /// Registers a new file format.
  74. /// </summary>
  75. public void RegisterFormat (FormatDescriptor fd)
  76. {
  77. formats.Add (fd);
  78. }
  79. /// <summary>
  80. /// Unregisters the file format for the given extension.
  81. /// </summary>
  82. public void UnregisterFormatByExtension (string extension)
  83. {
  84. extension = NormalizeExtension (extension);
  85. formats.RemoveAll (f => f.Extensions.Contains (extension));
  86. }
  87. /// <summary>
  88. /// Returns the default format that should be used when saving a file.
  89. /// This is normally the last format that was chosen by the user.
  90. /// </summary>
  91. public FormatDescriptor GetDefaultSaveFormat ()
  92. {
  93. string extension = PintaCore.Settings.GetSetting<string> ("default-image-type", "jpeg");
  94. var fd = GetFormatByExtension (extension);
  95. // We found the last one we used
  96. if (fd != null)
  97. return fd;
  98. // Return any format we have
  99. foreach (var f in Formats)
  100. if (!f.IsReadOnly ())
  101. return f;
  102. // We don't have any formats
  103. throw new InvalidOperationException ("There are no image formats supported.");
  104. }
  105. /// <summary>
  106. /// Finds the correct exporter to use for opening the given file, or null
  107. /// if no exporter exists for the file.
  108. /// </summary>
  109. public IImageExporter GetExporterByFile (string file)
  110. {
  111. string extension = Path.GetExtension (file);
  112. return GetExporterByExtension (extension);
  113. }
  114. /// <summary>
  115. /// Finds the file format for the given file name, or null
  116. /// if no file format exists for that file.
  117. /// </summary>
  118. public FormatDescriptor GetFormatByFile (string file)
  119. {
  120. string extension = Path.GetExtension (file);
  121. extension = NormalizeExtension (extension);
  122. return Formats.Where (p => p.Extensions.Contains (extension)).FirstOrDefault ();
  123. }
  124. /// <summary>
  125. /// Finds the correct importer to use for opening the given file, or null
  126. /// if no importer exists for the file.
  127. /// </summary>
  128. public IImageImporter GetImporterByFile (string file)
  129. {
  130. string extension = Path.GetExtension (file);
  131. if (extension == null) {
  132. return null;
  133. } else {
  134. return GetImporterByExtension (extension);
  135. }
  136. }
  137. /// <summary>
  138. /// Sets the default format used when saving files to the given extension.
  139. /// </summary>
  140. public void SetDefaultFormat (string extension)
  141. {
  142. extension = NormalizeExtension (extension);
  143. PintaCore.Settings.PutSetting ("default-image-type", extension);
  144. PintaCore.Settings.SaveSettings ();
  145. }
  146. /// <summary>
  147. /// Finds the correct exporter to use for the given file extension, or null
  148. /// if no exporter exists for that extension.
  149. /// </summary>
  150. private IImageExporter GetExporterByExtension (string extension)
  151. {
  152. FormatDescriptor format = GetFormatByExtension (extension);
  153. if (format == null)
  154. return null;
  155. return format.Exporter;
  156. }
  157. /// <summary>
  158. /// Finds the correct importer to use for the given file extension, or null
  159. /// if no importer exists for that extension.
  160. /// </summary>
  161. private IImageImporter GetImporterByExtension (string extension)
  162. {
  163. FormatDescriptor format = GetFormatByExtension (extension);
  164. if (format == null)
  165. return null;
  166. return format.Importer;
  167. }
  168. /// <summary>
  169. /// Finds the file format for the given file extension, or null
  170. /// if no file format exists for that extension.
  171. /// </summary>
  172. private FormatDescriptor GetFormatByExtension (string extension)
  173. {
  174. extension = NormalizeExtension (extension);
  175. return Formats.Where (p => p.Extensions.Contains (extension)).FirstOrDefault ();
  176. }
  177. /// <summary>
  178. /// Normalizes the extension.
  179. /// </summary>
  180. private static string NormalizeExtension (string extension)
  181. {
  182. return extension.ToLowerInvariant ().TrimStart ('.').Trim ();
  183. }
  184. }
  185. }