1 /*
   2  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.imageio.plugins.bmp;
  27 
  28 import java.awt.image.DataBuffer;
  29 import java.awt.image.SampleModel;
  30 import java.awt.image.SinglePixelPackedSampleModel;
  31 
  32 import javax.imageio.spi.ImageWriterSpi;
  33 import javax.imageio.spi.ServiceRegistry;
  34 import javax.imageio.spi.IIORegistry;
  35 import javax.imageio.stream.ImageOutputStream;
  36 import javax.imageio.ImageWriter;
  37 import javax.imageio.ImageTypeSpecifier;
  38 import javax.imageio.IIOException;
  39 import java.util.Locale;
  40 
  41 import javax.imageio.plugins.bmp.BMPImageWriteParam;
  42 
  43 public class BMPImageWriterSpi extends ImageWriterSpi {
  44     private static String [] readerSpiNames =
  45         {"com.sun.imageio.plugins.bmp.BMPImageReaderSpi"};
  46     private static String[] formatNames = {"bmp", "BMP"};
  47     private static String[] entensions = {"bmp"};
  48     private static String[] mimeType = {"image/bmp"};
  49 
  50     private boolean registered = false;
  51 
  52     public BMPImageWriterSpi() {
  53         super("Oracle Corporation",
  54               "1.0",
  55               formatNames,
  56               entensions,
  57               mimeType,
  58               "com.sun.imageio.plugins.bmp.BMPImageWriter",
  59               new Class<?>[] { ImageOutputStream.class },
  60               readerSpiNames,
  61               false,
  62               null, null, null, null,
  63               true,
  64               BMPMetadata.nativeMetadataFormatName,
  65               "com.sun.imageio.plugins.bmp.BMPMetadataFormat",
  66               null, null);
  67     }
  68 
  69     public String getDescription(Locale locale) {
  70         return "Standard BMP Image Writer";
  71     }
  72 
  73     public void onRegistration(ServiceRegistry registry,
  74                                Class<?> category) {
  75         if (registered) {
  76             return;
  77         }
  78 
  79         registered = true;
  80     }
  81 
  82     public boolean canEncodeImage(ImageTypeSpecifier type) {
  83         int dataType= type.getSampleModel().getDataType();
  84         if (dataType < DataBuffer.TYPE_BYTE || dataType > DataBuffer.TYPE_INT)
  85             return false;
  86 
  87         SampleModel sm = type.getSampleModel();
  88         int numBands = sm.getNumBands();
  89         if (!(numBands == 1 || numBands == 3))
  90             return false;
  91 
  92         if (numBands == 1 && dataType != DataBuffer.TYPE_BYTE)
  93             return false;
  94 
  95         if (dataType > DataBuffer.TYPE_BYTE &&
  96               !(sm instanceof SinglePixelPackedSampleModel))
  97             return false;
  98 
  99         return true;
 100     }
 101 
 102     public ImageWriter createWriterInstance(Object extension)
 103         throws IIOException {
 104         return new BMPImageWriter(this);
 105     }
 106 }