BMPImageWriterSpi.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* BMPImageWriterSpi.java --
  2. Copyright (C) 2006 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 gnu.javax.imageio.bmp;
  32. import java.util.Locale;
  33. import javax.imageio.ImageTypeSpecifier;
  34. import javax.imageio.ImageWriter;
  35. import javax.imageio.spi.ImageWriterSpi;
  36. public class BMPImageWriterSpi
  37. extends ImageWriterSpi
  38. {
  39. static final String vendorName = "GNU";
  40. static final String version = "0.1";
  41. static final String writerClassName = "gnu.javax.imageio.bmp.BMPImageWriter";
  42. static final String[] names = { "bmp", "BMP", "Microsoft Windows BMP" };
  43. static final String[] suffixes = { ".bmp", ".bm" };
  44. static final String[] MIMETypes = { "image/bmp", "image/x-windows-bmp" };
  45. static final String[] readerSpiNames = { "gnu.javax.imageio.bmp.BMPImageReaderSpi" };
  46. static final boolean supportsStandardStreamMetadataFormat = false;
  47. static final String nativeStreamMetadataFormatName = null;
  48. static final String nativeStreamMetadataFormatClassName = null;
  49. static final String[] extraStreamMetadataFormatNames = null;
  50. static final String[] extraStreamMetadataFormatClassNames = null;
  51. static final boolean supportsStandardImageMetadataFormat = false;
  52. static final String nativeImageMetadataFormatName = null;
  53. static final String nativeImageMetadataFormatClassName = null;
  54. static final String[] extraImageMetadataFormatNames = null;
  55. static final String[] extraImageMetadataFormatClassNames = null;
  56. private BMPImageWriter writerInstance;
  57. public BMPImageWriterSpi()
  58. {
  59. super(vendorName, version, names, suffixes, MIMETypes, writerClassName,
  60. STANDARD_OUTPUT_TYPE, readerSpiNames, supportsStandardStreamMetadataFormat,
  61. nativeStreamMetadataFormatName, nativeStreamMetadataFormatClassName,
  62. extraStreamMetadataFormatNames, extraStreamMetadataFormatClassNames,
  63. supportsStandardImageMetadataFormat, nativeImageMetadataFormatName,
  64. nativeImageMetadataFormatClassName, extraImageMetadataFormatNames,
  65. extraImageMetadataFormatClassNames);
  66. }
  67. /**
  68. * Returns true if the image can be encoded.
  69. *
  70. * @param type - the image type specifier.
  71. * @return true if image can be encoded, otherwise false.
  72. */
  73. public boolean canEncodeImage(ImageTypeSpecifier type)
  74. {
  75. if (type == null)
  76. return false;
  77. BMPInfoHeader ih = writerInstance.infoHeader;
  78. if (ih != null)
  79. {
  80. int compressionType = ih.getCompression();
  81. int bytes = type.getColorModel().getPixelSize();
  82. if ((compressionType == BMPInfoHeader.BI_RLE4 && (bytes != 4 || bytes != 8))
  83. || (compressionType == BMPInfoHeader.BI_RGB && ((bytes != 1
  84. || bytes != 4
  85. || bytes != 8
  86. || bytes != 16
  87. || bytes != 24
  88. || bytes != 32))))
  89. return false;
  90. }
  91. return true;
  92. }
  93. /**
  94. * Creates an instance of ImageWriter using the given extension.
  95. *
  96. * @param extension - the provider that is constructing this image writer, or
  97. * null
  98. */
  99. public ImageWriter createWriterInstance(Object extension)
  100. {
  101. if (extension != null && extension instanceof ImageWriterSpi)
  102. writerInstance = new BMPImageWriter((ImageWriterSpi) extension);
  103. else
  104. writerInstance = new BMPImageWriter(this);
  105. return writerInstance;
  106. }
  107. /**
  108. * Gets the instance of ImageWriter, if already created.
  109. */
  110. public BMPImageWriter getWriterInstance()
  111. {
  112. if (writerInstance != null)
  113. return writerInstance;
  114. return (BMPImageWriter) createWriterInstance(null);
  115. }
  116. /**
  117. * Returns a short description of this service provider that can be
  118. * presented to a human user.
  119. *
  120. * @param locale - the locale for which the description string should
  121. * be localized.
  122. */
  123. public String getDescription(Locale locale)
  124. {
  125. return "Microsoft BMP v3";
  126. }
  127. }