1 /* BMPImageWriter.java --
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package gnu
.javax
.imageio
.bmp
;
41 import java
.io
.IOException
;
43 import javax
.imageio
.IIOImage
;
44 import javax
.imageio
.ImageTypeSpecifier
;
45 import javax
.imageio
.ImageWriteParam
;
46 import javax
.imageio
.ImageWriter
;
47 import javax
.imageio
.metadata
.IIOMetadata
;
48 import javax
.imageio
.spi
.ImageWriterSpi
;
49 import javax
.imageio
.stream
.ImageOutputStream
;
51 public class BMPImageWriter
54 protected BMPEncoder encoder
;
55 protected BMPFileHeader fileHeader
;
56 protected BMPInfoHeader infoHeader
;
59 * Construct an bmp image writer.
61 * @param originatingProvider - the provider that is constructing this image
64 protected BMPImageWriter(ImageWriterSpi originatingProvider
)
66 super(originatingProvider
);
73 * Convert IIOMetadata from an input reader format, returning an IIOMetadata
74 * suitable for use by an image writer. The ImageTypeSpecifier specifies the
75 * destination image type. An optional ImageWriteParam argument is available
76 * in case the image writing parameters affect the metadata conversion.
78 * @param inData - the metadata coming from an image reader
79 * @param imageType - the output image type of the writer
80 * @param param - the image writing parameters or null
81 * @return the converted metadata that should be used by the image writer, or
82 * null if this ImageTranscoder has no knowledge of the input metadata
83 * @exception IllegalArgumentException if either inData or imageType is null
85 public IIOMetadata
convertImageMetadata(IIOMetadata inData
,
86 ImageTypeSpecifier imageType
,
87 ImageWriteParam param
)
89 // FIXME: Support metadata.
90 if (inData
== null || imageType
== null)
91 throw new IllegalArgumentException("IIOMetadata and ImageTypeSpecifier cannot be null.");
96 * Convert IIOMetadata from an input stream format, returning an
97 * IIOMetadata suitable for use by an image writer.
99 * An optional ImageWriteParam argument is available in case the
100 * image writing parameters affect the metadata conversion.
102 * @param inData - the metadata coming from an input image stream
103 * @param param - the image writing parameters or null
104 * @return the converted metadata that should be used by the image
105 * writer, or null if this ImageTranscoder has no knowledge of the
108 * @exception IllegalArgumentException if inData is null
110 public IIOMetadata
convertStreamMetadata (IIOMetadata inData
,
111 ImageWriteParam param
)
113 // FIXME: Support metadata.
115 throw new IllegalArgumentException("IIOMetadata cannot be null.");
120 * Get a metadata object appropriate for encoding an image specified
121 * by the given image type specifier and optional image write
124 * @param imageType - an image type specifier
125 * @param param - image writing parameters, or null
126 * @return a metadata object appropriate for encoding an image of
127 * the given type with the given parameters
129 public IIOMetadata
getDefaultImageMetadata (ImageTypeSpecifier imageType
, ImageWriteParam param
)
131 // FIXME: Support metadata.
136 * Get a metadata object appropriate for encoding the default image
137 * type handled by this writer, optionally considering image write
140 * @param param - image writing parameters, or null
141 * @return a metadata object appropriate for encoding an image of
142 * the default type with the given parameters
144 public IIOMetadata
getDefaultStreamMetadata (ImageWriteParam param
)
146 // FIXME: Support metadata.
151 * Write an image stream, including thumbnails and metadata to the
152 * output stream. The output must have been set prior to this
153 * method being called. Metadata associated with the stream may be
154 * supplied, or it can be left null. IIOImage may contain raster
155 * data if this writer supports rasters, or it will contain a
156 * rendered image. Thumbnails are resized if need be. Image
157 * writing parameters may be specified to affect writing, or may be
160 * @param streamMetadata - metadata associated with this stream, or
162 * @param image - an IIOImage containing image data, metadata and
163 * thumbnails to be written
164 * @param param - image writing parameters, or null
165 * @exception IOException if a write error occurs
166 * @throws BMPException if the encoder has not been initialized.
168 public void write(IIOMetadata streamMetadata
, IIOImage image
,
169 ImageWriteParam param
) throws IOException
, BMPException
172 ImageOutputStream out
= (ImageOutputStream
) output
;
173 fileHeader
= new BMPFileHeader(out
, image
);
174 infoHeader
= new BMPInfoHeader(out
, image
, param
);
175 encoder
= BMPEncoder
.getEncoder(fileHeader
, infoHeader
);
178 encoder
.encode(out
, streamMetadata
, image
, param
);
180 throw new BMPException("Encoder has not been initialized.");
184 * Checks the output stream.
186 * @throws IOException if there is an error with the output stream
188 private void checkStream() throws IOException
190 if (!(output
instanceof ImageOutputStream
))
191 throw new IllegalStateException("Output not an ImageOutputStream.");
193 throw new IllegalStateException("No output stream.");