1 /*
   2  * Copyright (c) 2000, 2004, 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.jpeg;
  27 
  28 import javax.imageio.spi.ImageWriterSpi;
  29 import javax.imageio.spi.ServiceRegistry;
  30 import javax.imageio.spi.IIORegistry;
  31 import javax.imageio.stream.ImageOutputStream;
  32 import javax.imageio.ImageWriter;
  33 import javax.imageio.ImageTypeSpecifier;
  34 import javax.imageio.IIOException;
  35 
  36 import java.awt.image.ColorModel;
  37 import java.awt.image.IndexColorModel;
  38 import java.awt.image.SampleModel;
  39 import java.util.Locale;
  40 
  41 public class JPEGImageWriterSpi extends ImageWriterSpi {
  42 
  43     private static String [] readerSpiNames =
  44         {"com.sun.imageio.plugins.jpeg.JPEGImageReaderSpi"};
  45 
  46     public JPEGImageWriterSpi() {
  47         super(JPEG.vendor,
  48               JPEG.version,
  49               JPEG.names,
  50               JPEG.suffixes,
  51               JPEG.MIMETypes,
  52               "com.sun.imageio.plugins.jpeg.JPEGImageWriter",
  53               new Class<?>[] { ImageOutputStream.class },
  54               readerSpiNames,
  55               true,
  56               JPEG.nativeStreamMetadataFormatName,
  57               JPEG.nativeStreamMetadataFormatClassName,
  58               null, null,
  59               true,
  60               JPEG.nativeImageMetadataFormatName,
  61               JPEG.nativeImageMetadataFormatClassName,
  62               null, null
  63               );
  64     }
  65 
  66     public String getDescription(Locale locale) {
  67         return "Standard JPEG Image Writer";
  68     }
  69 
  70     public boolean isFormatLossless() {
  71         return false;
  72     }
  73 
  74     public boolean canEncodeImage(ImageTypeSpecifier type) {
  75         SampleModel sampleModel = type.getSampleModel();
  76 
  77         // Find the maximum bit depth across all channels
  78         int[] sampleSize = sampleModel.getSampleSize();
  79         int bitDepth = sampleSize[0];
  80         for (int i = 1; i < sampleSize.length; i++) {
  81             if (sampleSize[i] > bitDepth) {
  82                 bitDepth = sampleSize[i];
  83             }
  84         }
  85 
  86         // 4450894: Ensure bitDepth is between 1 and 8
  87         if (bitDepth < 1 || bitDepth > 8) {
  88             return false;
  89         }
  90 
  91         return true;
  92     }
  93 
  94     public ImageWriter createWriterInstance(Object extension)
  95         throws IIOException {
  96         return new JPEGImageWriter(this);
  97     }
  98 }