ImageReaderWriterSpi.java 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. /* ImageReaderWriterSpi.java -- Superclass for image reader and writer spis.
  2. Copyright (C) 2004, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package javax.imageio.spi;
  32. import javax.imageio.metadata.IIOMetadataFormat;
  33. import javax.imageio.metadata.IIOMetadataFormatImpl;
  34. /**
  35. * An abstract superclass that contains the common parts of {@link
  36. * javax.imageio.spi.ImageReaderSpi} and {@link
  37. * javax.imageio.spi.ImageWriterSpi}.
  38. *
  39. * @since 1.4
  40. *
  41. * @author Sascha Brawer (brawer@dandelis.ch)
  42. */
  43. public abstract class ImageReaderWriterSpi
  44. extends IIOServiceProvider
  45. {
  46. /**
  47. * The human-readable, localized names of the supported image
  48. * formats. This value should be non-<code>null</code> after
  49. * construction.
  50. *
  51. * @see #getFormatNames()
  52. */
  53. protected String[] names;
  54. /**
  55. * The file suffixes of the supported image formats. This value
  56. * should be non-<code>null</code> after construction.
  57. *
  58. * @see #getFileSuffixes()
  59. */
  60. protected String[] suffixes;
  61. /**
  62. * The MIME types of the supported image formats. This value
  63. * should be non-<code>null</code> after construction.
  64. *
  65. * @see #getMIMETypes()
  66. */
  67. protected String[] MIMETypes;
  68. /**
  69. * The fully qualified name of the class that implements the {@link
  70. * javax.imageio.ImageReader} or {@link javax.imageio.ImageWriter}
  71. * interface. This value should be non-<code>null</code> after
  72. * construction.
  73. *
  74. * @see #getPluginClassName()
  75. */
  76. protected String pluginClassName;
  77. /**
  78. * Indicates whether the per-stream {@linkplain
  79. * javax.imageio.metadata.IIOMetadata metadata objects} associated
  80. * with this plug-in support format
  81. * <code>&#x201c;javax_imageio_1.0&#x201d;</code> in their
  82. * <code>getAsTree</code> and <code>setAsTree</code> methods.
  83. *
  84. * @see #isStandardStreamMetadataFormatSupported()
  85. */
  86. protected boolean supportsStandardStreamMetadataFormat;
  87. /**
  88. * The name of the format that allows encoding all stream metadata
  89. * without loss, or <code>null</code> if this plug-in does not
  90. * provide a format that preserves all stream metadata.
  91. */
  92. protected String nativeStreamMetadataFormatName;
  93. protected String nativeStreamMetadataFormatClassName;
  94. /**
  95. * The names of additional formats for encoding stream metadata,
  96. * other than the {@linkplain
  97. * #isStandardStreamMetadataFormatSupported() standard} and the
  98. * {@linkplain #getNativeStreamMetadataFormatName() native} formats,
  99. * or <code>null</code> if this plug-in does not provide any extra
  100. * formats.
  101. */
  102. protected String[] extraStreamMetadataFormatNames;
  103. protected String[] extraStreamMetadataFormatClassNames;
  104. /**
  105. * Indicates whether the per-image {@linkplain
  106. * javax.imageio.metadata.IIOMetadata metadata objects} associated
  107. * with this plug-in support format
  108. * <code>&#x201c;javax_imageio_1.0&#x201d;</code> in their
  109. * <code>getAsTree</code> and <code>setAsTree</code> methods.
  110. *
  111. * @see #isStandardImageMetadataFormatSupported()
  112. */
  113. protected boolean supportsStandardImageMetadataFormat;
  114. /**
  115. * The name of the format that allows encoding all image metadata
  116. * without loss, or <code>null</code> if this plug-in does not
  117. * provide a format that preserves all image metadata.
  118. */
  119. protected String nativeImageMetadataFormatName;
  120. protected String nativeImageMetadataFormatClassName;
  121. /**
  122. * The names of additional formats for encoding image metadata,
  123. * other than the {@linkplain
  124. * #isStandardImageMetadataFormatSupported() standard} and the
  125. * {@linkplain #getNativeImageMetadataFormatName() native} formats,
  126. * or <code>null</code> if this plug-in does not provide any extra
  127. * formats.
  128. */
  129. protected String[] extraImageMetadataFormatNames;
  130. protected String[] extraImageMetadataFormatClassNames;
  131. /**
  132. * Constructs an <code>ImageReaderWriteSpi</code> instance, without
  133. * specifying a number of parameters. Constructors of concrete
  134. * subclasses must ensure that they set all inherited fields to
  135. * meaningful values.
  136. */
  137. public ImageReaderWriterSpi()
  138. {
  139. }
  140. /**
  141. * Constructs an <code>ImageReaderWriteSpi</code> instance,
  142. * specifying a number of parameters.
  143. *
  144. * @param names the human-readable, localized names of the supported
  145. * image formats, for example <code>[&#x201c;Tagged Image File
  146. * Format&#x201d;, &#x201c;Portable Network
  147. * Graphics&#x201d;]</code>.
  148. *
  149. * @param suffixes the file suffixes of the supported image formats,
  150. * for example <code>[&#x201c;tiff&#x201d;, &#x201c;tif&#x201d;,
  151. * &#x201c;png&#x201d;]</code>.
  152. *
  153. * @param MIMETypes the MIME types of the supported image formats,
  154. * for example <code>[&#x201c;image/tiff&#x201d;,
  155. * &#x201c;image/png&#x201d;]</code>.
  156. *
  157. * @param pluginClassName the fully qualified name of the class that
  158. * implements the {@link javax.imageio.ImageReader} or {@link
  159. * javax.imageio.ImageWriter} interface.
  160. *
  161. * @param supportsStandardStreamMetadataFormat whether the
  162. * per-stream {@linkplain javax.imageio.metadata.IIOMetadata
  163. * metadata objects} associated with this plug-in support format
  164. * <code>&#x201c;javax_imageio_1.0&#x201d;</code> in their
  165. * <code>getAsTree</code> and <code>setAsTree</code> methods.
  166. *
  167. * @param nativeStreamMetadataFormatName the name of the format that
  168. * allows encoding all stream metadata without loss, or
  169. * <code>null</code> if this plug-in does not provide a format that
  170. * preserves all stream metadata.
  171. *
  172. * @param extraStreamMetadataFormatNames the names of additional
  173. * formats for encoding stream metadata, other than the {@linkplain
  174. * #isStandardStreamMetadataFormatSupported() standard} and the
  175. * {@linkplain #getNativeStreamMetadataFormatName() native} formats,
  176. * or <code>null</code> if this plug-in does not provide any extra
  177. * formats.
  178. *
  179. * @param supportsStandardImageMetadataFormat whether the per-image
  180. * {@linkplain javax.imageio.metadata.IIOMetadata metadata objects}
  181. * associated with this plug-in support format
  182. * <code>&#x201c;javax_imageio_1.0&#x201d;</code> in their
  183. * <code>getAsTree</code> and <code>setAsTree</code> methods.
  184. *
  185. * @param nativeImageMetadataFormatName the name of the format that
  186. * allows encoding all image metadata without loss, or
  187. * <code>null</code> if this plug-in does not provide a format that
  188. * preserves all image metadata.
  189. *
  190. * @param extraImageMetadataFormatNames the names of additional
  191. * formats for encoding image metadata, other than the {@linkplain
  192. * #isStandardImageMetadataFormatSupported() standard} and the
  193. * {@linkplain #getNativeImageMetadataFormatName() native} formats,
  194. * or <code>null</code> if this plug-in does not provide any extra
  195. * formats.
  196. *
  197. * @throws IllegalArgumentException if <code>vendorName</code>
  198. * or <code>version</code> is <code>null</code>.
  199. */
  200. public ImageReaderWriterSpi(String vendorName, String version,
  201. String[] names, String[] suffixes,
  202. String[] MIMETypes, String pluginClassName,
  203. boolean supportsStandardStreamMetadataFormat,
  204. String nativeStreamMetadataFormatName,
  205. String nativeStreamMetadataFormatClassName,
  206. String[] extraStreamMetadataFormatNames,
  207. String[] extraStreamMetadataFormatClassNames,
  208. boolean supportsStandardImageMetadataFormat,
  209. String nativeImageMetadataFormatName,
  210. String nativeImageMetadataFormatClassName,
  211. String[] extraImageMetadataFormatNames,
  212. String[] extraImageMetadataFormatClassNames)
  213. {
  214. /* The inherited constructor will throw IllegalArgumentException
  215. * if one of its arguments is null.
  216. */
  217. super(vendorName, version);
  218. if (names == null || names.length == 0 || pluginClassName == null)
  219. throw new IllegalArgumentException();
  220. this.names = names;
  221. this.suffixes = suffixes;
  222. this.MIMETypes = MIMETypes;
  223. this.pluginClassName = pluginClassName;
  224. this.supportsStandardStreamMetadataFormat
  225. = supportsStandardStreamMetadataFormat;
  226. this.nativeStreamMetadataFormatName
  227. = nativeStreamMetadataFormatName;
  228. this.nativeStreamMetadataFormatClassName
  229. = nativeStreamMetadataFormatClassName;
  230. this.extraStreamMetadataFormatNames
  231. = extraStreamMetadataFormatNames;
  232. this.extraStreamMetadataFormatClassNames
  233. = extraStreamMetadataFormatClassNames;
  234. this.supportsStandardImageMetadataFormat
  235. = supportsStandardImageMetadataFormat;
  236. this.nativeImageMetadataFormatName
  237. = nativeImageMetadataFormatName;
  238. this.nativeImageMetadataFormatClassName
  239. = nativeImageMetadataFormatClassName;
  240. this.extraImageMetadataFormatNames
  241. = extraImageMetadataFormatNames;
  242. this.extraImageMetadataFormatClassNames
  243. = extraImageMetadataFormatClassNames;
  244. }
  245. /**
  246. * Returns the human-readable, localized names of the supported
  247. * image formats. For example, a plug-in might return an array with
  248. * the elements <code>[&#x201c;Tagged Image File Format&#x201d;,
  249. * &#x201c;Portable Network Graphics&#x201d;]</code>.
  250. */
  251. public String[] getFormatNames()
  252. {
  253. return (String[]) names.clone();
  254. }
  255. /**
  256. * Returns the file suffixes of the supported image formats, for
  257. * example <code>[&#x201c;tiff&#x201d;, &#x201c;tif&#x201d;,
  258. * &#x201c;png&#x201d;]</code>.
  259. */
  260. public String[] getFileSuffixes()
  261. {
  262. return suffixes;
  263. }
  264. /**
  265. * Returns the MIME types of the supported image formats, for
  266. * example <code>[&#x201c;image/tiff&#x201d;,
  267. * &#x201c;image/png&#x201d;]</code>.
  268. *
  269. * @return an array of MIME type strings, or <code>null</code> if
  270. * none of the supported formats has an associated MIME type.
  271. */
  272. public String[] getMIMETypes()
  273. {
  274. return MIMETypes;
  275. }
  276. /**
  277. * Returns the fully qualified name of the class that implements the
  278. * {@link javax.imageio.ImageReader} or {@link
  279. * javax.imageio.ImageWriter} interface.
  280. */
  281. public String getPluginClassName()
  282. {
  283. return pluginClassName;
  284. }
  285. /**
  286. * Returns whether the per-stream {@linkplain
  287. * javax.imageio.metadata.IIOMetadata metadata objects} associated
  288. * with this plug-in support format
  289. * <code>&#x201c;javax_imageio_1.0&#x201d;</code> in their
  290. * <code>getAsTree</code> and <code>setAsTree</code> methods.
  291. */
  292. public boolean isStandardStreamMetadataFormatSupported()
  293. {
  294. return supportsStandardStreamMetadataFormat;
  295. }
  296. /**
  297. * Returns the name of the format that allows encoding all stream
  298. * metadata without loss, or <code>null</code> if this plug-in does
  299. * not provide a format that preserves all stream metadata.
  300. *
  301. * @see #getNativeImageMetadataFormatName()
  302. */
  303. public String getNativeStreamMetadataFormatName()
  304. {
  305. return nativeStreamMetadataFormatName;
  306. }
  307. /**
  308. * Returns the names of additional formats for encoding stream
  309. * metadata, other than the {@linkplain
  310. * #isStandardStreamMetadataFormatSupported() standard} and the
  311. * {@linkplain #getNativeStreamMetadataFormatName() native} formats,
  312. * or <code>null</code> if this plug-in does not provide any extra
  313. * formats.
  314. *
  315. * @see #getExtraImageMetadataFormatNames()
  316. */
  317. public String[] getExtraStreamMetadataFormatNames()
  318. {
  319. return extraStreamMetadataFormatNames;
  320. }
  321. /**
  322. * Returns whether the per-image {@linkplain
  323. * javax.imageio.metadata.IIOMetadata metadata objects} associated
  324. * with this plug-in support format
  325. * <code>&#x201c;javax_imageio_1.0&#x201d;</code> in their
  326. * <code>getAsTree</code> and <code>setAsTree</code> methods.
  327. */
  328. public boolean isStandardImageMetadataFormatSupported()
  329. {
  330. return supportsStandardImageMetadataFormat;
  331. }
  332. /**
  333. * Returns the name of the format that allows encoding all image
  334. * metadata without loss, or <code>null</code> if this plug-in does
  335. * not provide a format that preserves all image metadata.
  336. *
  337. * @see #getNativeStreamMetadataFormatName()
  338. */
  339. public String getNativeImageMetadataFormatName()
  340. {
  341. return nativeImageMetadataFormatName;
  342. }
  343. /**
  344. * Returns the names of additional formats for encoding image
  345. * metadata, other than the {@linkplain
  346. * #isStandardImageMetadataFormatSupported() standard} and the
  347. * {@linkplain #getNativeImageMetadataFormatName() native} formats,
  348. * or <code>null</code> if this plug-in does not provide any extra
  349. * formats.
  350. *
  351. * @see #getExtraStreamMetadataFormatNames()
  352. */
  353. public String[] getExtraImageMetadataFormatNames()
  354. {
  355. return extraImageMetadataFormatNames;
  356. }
  357. /**
  358. * Returns an IIOMetadataFormat object that represents the requested
  359. * stream metadata format or null if the given format is supported
  360. * but no IIOMetadataFormat can be created for it.
  361. *
  362. * @param formatName the requested stream metadata format name
  363. *
  364. * @return an IIOMetadataFormat object or null
  365. *
  366. * @throws IllegalArgumentException if formatName is null or is not
  367. * one of the standard metadata format or this provider's native or
  368. * extra stream metadata formats
  369. */
  370. public IIOMetadataFormat getStreamMetadataFormat (String formatName)
  371. {
  372. if (formatName == null)
  373. throw new IllegalArgumentException ("null stream metadata format name");
  374. if (!formatName.equals (getNativeStreamMetadataFormatName())
  375. && !formatName.equals (IIOMetadataFormatImpl.standardMetadataFormatName))
  376. {
  377. String[] extraNames = getExtraStreamMetadataFormatNames ();
  378. boolean foundName = false;
  379. for (int i = 0; i < extraNames.length; i++)
  380. {
  381. if (formatName.equals(extraNames[i]))
  382. {
  383. foundName = true;
  384. break;
  385. }
  386. }
  387. if (!foundName)
  388. throw new IllegalArgumentException ("unsupported stream metadata format name");
  389. }
  390. if (formatName.equals (IIOMetadataFormatImpl.standardMetadataFormatName))
  391. return IIOMetadataFormatImpl.getStandardFormatInstance ();
  392. else
  393. // Default implementation returns null.
  394. return null;
  395. }
  396. /**
  397. * Returns an IIOMetadataFormat object that represents the requested
  398. * image metadata format or null if the given format is supported
  399. * but no IIOMetadataFormat can be created for it.
  400. *
  401. * @param formatName the requested image metadata format name
  402. *
  403. * @return an IIOMetadataFormat object or null
  404. *
  405. * @throws IllegalArgumentException if formatName is null or is not
  406. * one of the standard metadata format or this provider's native or
  407. * extra image metadata formats
  408. */
  409. public IIOMetadataFormat getImageMetadataFormat (String formatName)
  410. {
  411. if (formatName == null)
  412. throw new IllegalArgumentException ("null image metadata format name");
  413. if (!formatName.equals (getNativeImageMetadataFormatName())
  414. && !formatName.equals (IIOMetadataFormatImpl.standardMetadataFormatName))
  415. {
  416. String[] extraNames = getExtraImageMetadataFormatNames ();
  417. boolean foundName = false;
  418. for (int i = 0; i < extraNames.length; i++)
  419. {
  420. if (formatName.equals(extraNames[i]))
  421. {
  422. foundName = true;
  423. break;
  424. }
  425. }
  426. if (!foundName)
  427. throw new IllegalArgumentException ("unsupported image metadata format name");
  428. }
  429. if (formatName.equals (IIOMetadataFormatImpl.standardMetadataFormatName))
  430. return IIOMetadataFormatImpl.getStandardFormatInstance ();
  431. else
  432. // Default implementation returns null.
  433. return null;
  434. }
  435. }