ImageFormat.jvm.cs 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. //
  2. // System.Drawing.Imaging.ImageFormat.cs
  3. //
  4. // Authors:
  5. // Everaldo Canuto ([email protected])
  6. // Andreas Nahr ([email protected])
  7. // Dennis Hayes ([email protected])
  8. // Jordi Mas i Hernandez ([email protected])
  9. //
  10. // (C) 2002-4 Ximian, Inc. http://www.ximian.com
  11. //
  12. //
  13. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  14. //
  15. // Permission is hereby granted, free of charge, to any person obtaining
  16. // a copy of this software and associated documentation files (the
  17. // "Software"), to deal in the Software without restriction, including
  18. // without limitation the rights to use, copy, modify, merge, publish,
  19. // distribute, sublicense, and/or sell copies of the Software, and to
  20. // permit persons to whom the Software is furnished to do so, subject to
  21. // the following conditions:
  22. //
  23. // The above copyright notice and this permission notice shall be
  24. // included in all copies or substantial portions of the Software.
  25. //
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  27. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  28. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  29. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  30. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  31. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  32. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  33. //
  34. using System;
  35. using System.ComponentModel;
  36. using javax.imageio;
  37. using javax.imageio.spi;
  38. namespace System.Drawing.Imaging {
  39. public sealed class ImageFormat {
  40. #region Statics
  41. static ImageFormat () {
  42. //remember mime according to the beginning of the string
  43. //workaround for com.sun.media.imageioimpl.plugins.bmp.BMPImageReader
  44. //which has mimeType="image/bmp, image/x-bmp, image/x-windows-bmp"
  45. String [] mimeReaders = javax.imageio.ImageIO.getReaderMIMETypes ();
  46. String [] mimeWriters = javax.imageio.ImageIO.getWriterMIMETypes ();
  47. String [] mimeTypes = new string [mimeReaders.Length+mimeWriters.Length];
  48. mimeReaders.CopyTo (mimeTypes, 0);
  49. mimeWriters.CopyTo (mimeTypes, mimeReaders.Length);
  50. for (int i = 0; i < mimeTypes.Length; i++) {
  51. if (mimeTypes [i].StartsWith ("image/bmp"))
  52. mimeBmp = mimeTypes [i];
  53. else if (mimeTypes [i].StartsWith ("image/gif"))
  54. mimeGif = mimeTypes [i];
  55. else if (mimeTypes [i].StartsWith ("image/tiff"))
  56. mimeTiff = mimeTypes [i];
  57. else if (mimeTypes [i].StartsWith ("image/png"))
  58. mimePng = mimeTypes [i];
  59. else if (mimeTypes [i].StartsWith ("image/x-ico"))
  60. mimeIcon = mimeTypes [i];
  61. else if (mimeTypes [i].StartsWith ("image/jpeg"))
  62. mimeJpeg = mimeTypes [i];
  63. }
  64. BmpImageFormat = new ImageFormat (mimeBmp, guidBmp, null, null);
  65. EmfImageFormat = new ImageFormat (null, guidEmf, null, null);
  66. ExifImageFormat = new ImageFormat (null, guidExif, null, null);
  67. GifImageFormat = new ImageFormat (mimeGif, guidGif, null, null);
  68. TiffImageFormat = new ImageFormat (mimeTiff, guidTiff, null, null);
  69. PngImageFormat = new ImageFormat (mimePng, guidPng, null, null);
  70. MemoryBmpImageFormat = new ImageFormat (null, guidMemoryBmp, null, null);
  71. IconImageFormat = new ImageFormat (mimeIcon, guidIcon, null, null);
  72. JpegImageFormat = new ImageFormat(mimeJpeg, guidJpeg, null, null);
  73. WmfImageFormat = new ImageFormat (null, guidWmf, null, null);
  74. }
  75. static Guid guidBmp = new Guid ("b96b3cab-0728-11d3-9d7b-0000f81ef32e");
  76. static Guid guidEmf = new Guid ("b96b3cac-0728-11d3-9d7b-0000f81ef32e");
  77. static Guid guidExif = new Guid ("b96b3cb2-0728-11d3-9d7b-0000f81ef32e");
  78. static Guid guidGif = new Guid ("b96b3cb0-0728-11d3-9d7b-0000f81ef32e");
  79. static Guid guidTiff = new Guid ("b96b3cb1-0728-11d3-9d7b-0000f81ef32e");
  80. static Guid guidPng = new Guid("b96b3caf-0728-11d3-9d7b-0000f81ef32e");
  81. static Guid guidMemoryBmp = new Guid ("b96b3caa-0728-11d3-9d7b-0000f81ef32e");
  82. static Guid guidIcon = new Guid ("b96b3cb5-0728-11d3-9d7b-0000f81ef32e");
  83. static Guid guidJpeg = new Guid("b96b3cae-0728-11d3-9d7b-0000f81ef32e");
  84. static Guid guidWmf = new Guid ("b96b3cad-0728-11d3-9d7b-0000f81ef32e");
  85. static string mimeBmp, mimeGif, mimeTiff, mimePng, mimeIcon, mimeJpeg;
  86. static ImageFormat BmpImageFormat;
  87. static ImageFormat EmfImageFormat;
  88. static ImageFormat ExifImageFormat;
  89. static ImageFormat GifImageFormat;
  90. static ImageFormat TiffImageFormat;
  91. static ImageFormat PngImageFormat;
  92. static ImageFormat MemoryBmpImageFormat;
  93. static ImageFormat IconImageFormat;
  94. static ImageFormat JpegImageFormat;
  95. static ImageFormat WmfImageFormat;
  96. static string GuidToMime (Guid guid) {
  97. if (guid.Equals (guidBmp))
  98. return mimeBmp;
  99. if (guid.Equals (guidGif))
  100. return mimeGif;
  101. if (guid.Equals (guidJpeg))
  102. return mimeJpeg;
  103. if (guid.Equals (guidPng))
  104. return mimePng;
  105. if (guid.Equals (guidTiff))
  106. return mimeTiff;
  107. if (guid.Equals (guidIcon))
  108. return mimeIcon;
  109. // Default
  110. return null;
  111. }
  112. static Guid MimeToGuid (string mime) {
  113. if (mime == mimeBmp)
  114. return guidBmp;
  115. if (mime == mimeGif)
  116. return guidGif;
  117. if (mime == mimeJpeg)
  118. return guidJpeg;
  119. if (mime == mimePng)
  120. return guidPng;
  121. if (mime == mimeTiff)
  122. return guidTiff;
  123. // Default
  124. return Guid.Empty;
  125. }
  126. #endregion
  127. #region Variables
  128. Guid _guid;
  129. string _mimeType;
  130. ImageReaderSpi _readerSpi;
  131. ImageWriterSpi _writerSpi;
  132. #endregion
  133. #region Constructors
  134. public ImageFormat (Guid guid)
  135. :this (GuidToMime (guid), guid, null, null) {}
  136. internal ImageFormat (string mimeType, ImageReaderSpi readerSpi, ImageWriterSpi writerSpi)
  137. :this (mimeType, MimeToGuid (mimeType), readerSpi, writerSpi) {}
  138. private ImageFormat (string mimeType, Guid guid,
  139. ImageReaderSpi readerSpi /*can be null*/,
  140. ImageWriterSpi writerSpi /*can be null*/)
  141. {
  142. _mimeType = mimeType;
  143. _guid = guid;
  144. if (writerSpi == null && mimeType != null) {
  145. java.util.Iterator iter = ImageIO.getImageWritersByMIMEType (mimeType);
  146. if (iter.hasNext ()) {
  147. ImageWriter writer = (ImageWriter) iter.next ();
  148. _writerSpi = writer.getOriginatingProvider ();
  149. } else
  150. _writerSpi = null;
  151. } else
  152. _writerSpi = writerSpi;
  153. if (readerSpi == null && mimeType != null) {
  154. java.util.Iterator iter = ImageIO.getImageReadersByMIMEType (mimeType);
  155. if (iter.hasNext ()) {
  156. ImageReader reader = (ImageReader) iter.next ();
  157. _readerSpi = reader.getOriginatingProvider ();
  158. } else
  159. _readerSpi = null;
  160. } else
  161. _readerSpi = readerSpi;
  162. }
  163. #endregion
  164. #region Methods
  165. public override bool Equals(object o) {
  166. ImageFormat f = o as ImageFormat;
  167. if (f != null) {
  168. if (f._guid == _guid)
  169. return true;
  170. else if (f._mimeType == _mimeType)
  171. return true;
  172. else if (f._readerSpi == _readerSpi && f._writerSpi == _writerSpi)
  173. return true;
  174. }
  175. return false;
  176. }
  177. public override int GetHashCode() {
  178. if (_guid != Guid.Empty)
  179. return _guid.GetHashCode ();
  180. else if (_mimeType != null)
  181. return _mimeType.GetHashCode ();
  182. else if (_readerSpi != null)
  183. return _readerSpi.GetHashCode();
  184. else if (_writerSpi != null)
  185. return _writerSpi.GetHashCode ();
  186. else
  187. return 0;
  188. }
  189. public override string ToString() {
  190. if (Guid.Equals (Bmp.Guid))
  191. return "Bmp";
  192. if (Guid.Equals (Emf.Guid))
  193. return "Emf";
  194. if (Guid.Equals (Gif.Guid))
  195. return "Gif";
  196. if (Guid.Equals (Jpeg.Guid))
  197. return "Jpeg";
  198. if (Guid.Equals (MemoryBmp.Guid))
  199. return "MemoryBmp";
  200. if (Guid.Equals (Png.Guid))
  201. return "Png";
  202. if (Guid.Equals (Tiff.Guid))
  203. return "Tiff";
  204. if (Guid.Equals (Wmf.Guid))
  205. return "Wmf";
  206. // Default
  207. return String.Concat("[ImageFormat: ", _guid, "]");
  208. }
  209. #endregion
  210. #region Properties
  211. public Guid Guid {
  212. get { return _guid; }
  213. }
  214. internal ImageReaderSpi ImageReaderSpi {
  215. get {
  216. return _readerSpi;
  217. }
  218. }
  219. internal ImageWriterSpi ImageWriterSpi {
  220. get {
  221. return _writerSpi;
  222. }
  223. }
  224. #endregion
  225. #region Static properties
  226. public static ImageFormat Bmp {
  227. get { return BmpImageFormat; }
  228. }
  229. public static ImageFormat Emf {
  230. get { return EmfImageFormat; }
  231. }
  232. public static ImageFormat Exif {
  233. get { return ExifImageFormat; }
  234. }
  235. public static ImageFormat Gif {
  236. get { return GifImageFormat; }
  237. }
  238. public static ImageFormat Icon {
  239. get { return IconImageFormat; }
  240. }
  241. public static ImageFormat Jpeg {
  242. get { return JpegImageFormat; }
  243. }
  244. public static ImageFormat MemoryBmp {
  245. get { return MemoryBmpImageFormat; }
  246. }
  247. public static ImageFormat Png {
  248. get { return PngImageFormat; }
  249. }
  250. public static ImageFormat Tiff {
  251. get { return TiffImageFormat; }
  252. }
  253. public static ImageFormat Wmf {
  254. get { return WmfImageFormat; }
  255. }
  256. #endregion
  257. }
  258. }