Source for gnu.javax.imageio.bmp.BMPImageWriter

   1: /* BMPImageWriter.java --
   2:    Copyright (C) 2006 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   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)
   9: any later version.
  10: 
  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.
  15: 
  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
  19: 02110-1301 USA.
  20: 
  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
  24: combination.
  25: 
  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. */
  37: 
  38: 
  39: package gnu.javax.imageio.bmp;
  40: 
  41: import java.io.IOException;
  42: 
  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;
  50: 
  51: public class BMPImageWriter
  52:     extends ImageWriter
  53: {
  54:   protected BMPEncoder encoder;
  55:   protected BMPFileHeader fileHeader;
  56:   protected BMPInfoHeader infoHeader;
  57: 
  58:   /**
  59:    * Construct an bmp image writer.
  60:    *
  61:    * @param originatingProvider - the provider that is constructing this image
  62:    *          writer, or null
  63:    */
  64:   protected BMPImageWriter(ImageWriterSpi originatingProvider)
  65:   {
  66:     super(originatingProvider);
  67:     encoder = null;
  68:     fileHeader = null;
  69:     infoHeader = null;
  70:   }
  71: 
  72:   /**
  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.
  77:    *
  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
  84:    */
  85:   public IIOMetadata convertImageMetadata(IIOMetadata inData,
  86:                                           ImageTypeSpecifier imageType,
  87:                                           ImageWriteParam param)
  88:   {
  89:     // FIXME: Support metadata.
  90:     if (inData == null || imageType == null)
  91:       throw new IllegalArgumentException("IIOMetadata and ImageTypeSpecifier cannot be null.");
  92:     return null;
  93:   }
  94: 
  95:   /**
  96:    * Convert IIOMetadata from an input stream format, returning an
  97:    * IIOMetadata suitable for use by an image writer.
  98:    *
  99:    * An optional ImageWriteParam argument is available in case the
 100:    * image writing parameters affect the metadata conversion.
 101:    *
 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
 106:    * input metadata
 107:    *
 108:    * @exception IllegalArgumentException if inData is null
 109:    */
 110:   public IIOMetadata convertStreamMetadata (IIOMetadata inData,
 111:                                  ImageWriteParam param)
 112:   {
 113:     // FIXME: Support metadata.
 114:     if (inData == null)
 115:       throw new IllegalArgumentException("IIOMetadata cannot be null.");
 116:     return null;
 117:   }
 118: 
 119:   /**
 120:    * Get a metadata object appropriate for encoding an image specified
 121:    * by the given image type specifier and optional image write
 122:    * parameters.
 123:    *
 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
 128:    */
 129:   public IIOMetadata getDefaultImageMetadata (ImageTypeSpecifier imageType, ImageWriteParam param)
 130:   {
 131:     // FIXME: Support metadata.
 132:     return null;
 133:   }
 134: 
 135:   /**
 136:    * Get a metadata object appropriate for encoding the default image
 137:    * type handled by this writer, optionally considering image write
 138:    * parameters.
 139:    *
 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
 143:    */
 144:   public IIOMetadata getDefaultStreamMetadata (ImageWriteParam param)
 145:   {
 146:     // FIXME: Support metadata.
 147:     return null;
 148:   }
 149: 
 150:   /**
 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
 158:    * left null.
 159:    *
 160:    * @param streamMetadata - metadata associated with this stream, or
 161:    * null
 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.
 167:    */
 168:   public void write(IIOMetadata streamMetadata, IIOImage image,
 169:                     ImageWriteParam param) throws IOException, BMPException
 170:   {
 171:     checkStream();
 172:     ImageOutputStream out = (ImageOutputStream) output;
 173:     fileHeader = new BMPFileHeader(out, image);
 174:     infoHeader = new BMPInfoHeader(out, image, param);
 175:     encoder = BMPEncoder.getEncoder(fileHeader, infoHeader);
 176: 
 177:     if (encoder != null)
 178:       encoder.encode(out, streamMetadata, image, param);
 179:     else
 180:       throw new BMPException("Encoder has not been initialized.");
 181:   }
 182: 
 183:   /**
 184:    * Checks the output stream.
 185:    *
 186:    * @throws IOException if there is an error with the output stream
 187:    */
 188:   private void checkStream() throws IOException
 189:   {
 190:     if (!(output instanceof ImageOutputStream))
 191:       throw new IllegalStateException("Output not an ImageOutputStream.");
 192:     if (output == null)
 193:       throw new IllegalStateException("No output stream.");
 194:   }
 195: }