BMPImageWriter.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /* BMPImageWriter.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.io.IOException;
  33. import javax.imageio.IIOImage;
  34. import javax.imageio.ImageTypeSpecifier;
  35. import javax.imageio.ImageWriteParam;
  36. import javax.imageio.ImageWriter;
  37. import javax.imageio.metadata.IIOMetadata;
  38. import javax.imageio.spi.ImageWriterSpi;
  39. import javax.imageio.stream.ImageOutputStream;
  40. public class BMPImageWriter
  41. extends ImageWriter
  42. {
  43. protected BMPEncoder encoder;
  44. protected BMPFileHeader fileHeader;
  45. protected BMPInfoHeader infoHeader;
  46. /**
  47. * Construct an bmp image writer.
  48. *
  49. * @param originatingProvider - the provider that is constructing this image
  50. * writer, or null
  51. */
  52. protected BMPImageWriter(ImageWriterSpi originatingProvider)
  53. {
  54. super(originatingProvider);
  55. encoder = null;
  56. fileHeader = null;
  57. infoHeader = null;
  58. }
  59. /**
  60. * Convert IIOMetadata from an input reader format, returning an IIOMetadata
  61. * suitable for use by an image writer. The ImageTypeSpecifier specifies the
  62. * destination image type. An optional ImageWriteParam argument is available
  63. * in case the image writing parameters affect the metadata conversion.
  64. *
  65. * @param inData - the metadata coming from an image reader
  66. * @param imageType - the output image type of the writer
  67. * @param param - the image writing parameters or null
  68. * @return the converted metadata that should be used by the image writer, or
  69. * null if this ImageTranscoder has no knowledge of the input metadata
  70. * @exception IllegalArgumentException if either inData or imageType is null
  71. */
  72. public IIOMetadata convertImageMetadata(IIOMetadata inData,
  73. ImageTypeSpecifier imageType,
  74. ImageWriteParam param)
  75. {
  76. // FIXME: Support metadata.
  77. if (inData == null || imageType == null)
  78. throw new IllegalArgumentException("IIOMetadata and ImageTypeSpecifier cannot be null.");
  79. return null;
  80. }
  81. /**
  82. * Convert IIOMetadata from an input stream format, returning an
  83. * IIOMetadata suitable for use by an image writer.
  84. *
  85. * An optional ImageWriteParam argument is available in case the
  86. * image writing parameters affect the metadata conversion.
  87. *
  88. * @param inData - the metadata coming from an input image stream
  89. * @param param - the image writing parameters or null
  90. * @return the converted metadata that should be used by the image
  91. * writer, or null if this ImageTranscoder has no knowledge of the
  92. * input metadata
  93. *
  94. * @exception IllegalArgumentException if inData is null
  95. */
  96. public IIOMetadata convertStreamMetadata (IIOMetadata inData,
  97. ImageWriteParam param)
  98. {
  99. // FIXME: Support metadata.
  100. if (inData == null)
  101. throw new IllegalArgumentException("IIOMetadata cannot be null.");
  102. return null;
  103. }
  104. /**
  105. * Get a metadata object appropriate for encoding an image specified
  106. * by the given image type specifier and optional image write
  107. * parameters.
  108. *
  109. * @param imageType - an image type specifier
  110. * @param param - image writing parameters, or null
  111. * @return a metadata object appropriate for encoding an image of
  112. * the given type with the given parameters
  113. */
  114. public IIOMetadata getDefaultImageMetadata (ImageTypeSpecifier imageType, ImageWriteParam param)
  115. {
  116. // FIXME: Support metadata.
  117. return null;
  118. }
  119. /**
  120. * Get a metadata object appropriate for encoding the default image
  121. * type handled by this writer, optionally considering image write
  122. * parameters.
  123. *
  124. * @param param - image writing parameters, or null
  125. * @return a metadata object appropriate for encoding an image of
  126. * the default type with the given parameters
  127. */
  128. public IIOMetadata getDefaultStreamMetadata (ImageWriteParam param)
  129. {
  130. // FIXME: Support metadata.
  131. return null;
  132. }
  133. /**
  134. * Write an image stream, including thumbnails and metadata to the
  135. * output stream. The output must have been set prior to this
  136. * method being called. Metadata associated with the stream may be
  137. * supplied, or it can be left null. IIOImage may contain raster
  138. * data if this writer supports rasters, or it will contain a
  139. * rendered image. Thumbnails are resized if need be. Image
  140. * writing parameters may be specified to affect writing, or may be
  141. * left null.
  142. *
  143. * @param streamMetadata - metadata associated with this stream, or
  144. * null
  145. * @param image - an IIOImage containing image data, metadata and
  146. * thumbnails to be written
  147. * @param param - image writing parameters, or null
  148. * @exception IOException if a write error occurs
  149. * @throws BMPException if the encoder has not been initialized.
  150. */
  151. public void write(IIOMetadata streamMetadata, IIOImage image,
  152. ImageWriteParam param) throws IOException, BMPException
  153. {
  154. checkStream();
  155. ImageOutputStream out = (ImageOutputStream) output;
  156. fileHeader = new BMPFileHeader(out, image);
  157. infoHeader = new BMPInfoHeader(out, image, param);
  158. encoder = BMPEncoder.getEncoder(fileHeader, infoHeader);
  159. if (encoder != null)
  160. encoder.encode(out, streamMetadata, image, param);
  161. else
  162. throw new BMPException("Encoder has not been initialized.");
  163. }
  164. /**
  165. * Checks the output stream.
  166. *
  167. * @throws IOException if there is an error with the output stream
  168. */
  169. private void checkStream() throws IOException
  170. {
  171. if (!(output instanceof ImageOutputStream))
  172. throw new IllegalStateException("Output not an ImageOutputStream.");
  173. if (output == null)
  174. throw new IllegalStateException("No output stream.");
  175. }
  176. }