1 /*
   2  * Copyright (c) 2005, 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.gif;
  27 
  28 import java.awt.image.ColorModel;
  29 import java.awt.image.SampleModel;
  30 import java.util.Locale;
  31 import javax.imageio.ImageTypeSpecifier;
  32 import javax.imageio.ImageWriter;
  33 import javax.imageio.spi.ImageWriterSpi;
  34 import javax.imageio.stream.ImageOutputStream;
  35 import com.sun.imageio.plugins.common.PaletteBuilder;
  36 
  37 public class GIFImageWriterSpi extends ImageWriterSpi {
  38 
  39     private static final String vendorName = "Oracle Corporation";
  40 
  41     private static final String version = "1.0";
  42 
  43     private static final String[] names = { "gif", "GIF" };
  44 
  45     private static final String[] suffixes = { "gif" };
  46 
  47     private static final String[] MIMETypes = { "image/gif" };
  48 
  49     private static final String writerClassName =
  50     "com.sun.imageio.plugins.gif.GIFImageWriter";
  51 
  52     private static final String[] readerSpiNames = {
  53         "com.sun.imageio.plugins.gif.GIFImageReaderSpi"
  54     };
  55 
  56     public GIFImageWriterSpi() {
  57         super(vendorName,
  58               version,
  59               names,
  60               suffixes,
  61               MIMETypes,
  62               writerClassName,
  63               new Class[] { ImageOutputStream.class },
  64               readerSpiNames,
  65               true,
  66               GIFWritableStreamMetadata.NATIVE_FORMAT_NAME,
  67               "com.sun.imageio.plugins.gif.GIFStreamMetadataFormat",
  68               null, null,
  69               true,
  70               GIFWritableImageMetadata.NATIVE_FORMAT_NAME,
  71               "com.sun.imageio.plugins.gif.GIFImageMetadataFormat",
  72               null, null
  73               );
  74     }
  75 
  76     public boolean canEncodeImage(ImageTypeSpecifier type) {
  77         if (type == null) {
  78             throw new IllegalArgumentException("type == null!");
  79         }
  80 
  81         SampleModel sm = type.getSampleModel();
  82         ColorModel cm = type.getColorModel();
  83 
  84         boolean canEncode = sm.getNumBands() == 1 &&
  85             sm.getSampleSize(0) <= 8 &&
  86             sm.getWidth() <= 65535 &&
  87             sm.getHeight() <= 65535 &&
  88             (cm == null || cm.getComponentSize()[0] <= 8);
  89 
  90         if (canEncode) {
  91             return true;
  92         } else {
  93             return PaletteBuilder.canCreatePalette(type);
  94         }
  95     }
  96 
  97     public String getDescription(Locale locale) {
  98         return "Standard GIF image writer";
  99     }
 100 
 101     public ImageWriter createWriterInstance(Object extension) {
 102         return new GIFImageWriter(this);
 103     }
 104 }