1 /*
   2  * Copyright (c) 1995, 2003, 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  *      Reads JPEG images from an InputStream and reports the
  27  *      image data to an InputStreamImageSource object.
  28  *
  29  * The native implementation of the JPEG image decoder was adapted from
  30  * release 6 of the free JPEG software from the Independent JPEG Group.
  31  */
  32 package sun.awt.image;
  33 
  34 import java.util.Vector;
  35 import java.util.Hashtable;
  36 import java.io.InputStream;
  37 import java.io.IOException;
  38 import java.awt.image.*;
  39 
  40 /**
  41  * JPEG Image converter
  42  *
  43  * @author Jim Graham
  44  */
  45 public class JPEGImageDecoder extends ImageDecoder {
  46     private static ColorModel RGBcolormodel;
  47     private static ColorModel ARGBcolormodel;
  48     private static ColorModel Graycolormodel;
  49 
  50     private static final Class InputStreamClass = InputStream.class;
  51     private static native void initIDs(Class InputStreamClass);
  52 
  53     private ColorModel colormodel;
  54 
  55     static {
  56         java.security.AccessController.doPrivileged(
  57                   new sun.security.action.LoadLibraryAction("jpeg"));
  58         initIDs(InputStreamClass);
  59         RGBcolormodel = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
  60         ARGBcolormodel = ColorModel.getRGBdefault();
  61         byte g[] = new byte[256];
  62         for (int i = 0; i < 256; i++) {
  63             g[i] = (byte) i;
  64         }
  65         Graycolormodel = new IndexColorModel(8, 256, g, g, g);
  66     }
  67 
  68     private native void readImage(InputStream is, byte buf[])
  69         throws ImageFormatException, IOException;
  70 
  71     Hashtable props = new Hashtable();
  72 
  73     public JPEGImageDecoder(InputStreamImageSource src, InputStream is) {
  74         super(src, is);
  75     }
  76 
  77     /**
  78      * An error has occurred. Throw an exception.
  79      */
  80     private static void error(String s1) throws ImageFormatException {
  81         throw new ImageFormatException(s1);
  82     }
  83 
  84     public boolean sendHeaderInfo(int width, int height,
  85                                   boolean gray, boolean hasalpha,
  86                                   boolean multipass)
  87     {
  88         setDimensions(width, height);
  89 
  90         setProperties(props);
  91         if (gray) {
  92             colormodel = Graycolormodel;
  93         } else {
  94             if (hasalpha) {
  95                 colormodel = ARGBcolormodel;
  96             } else {
  97                 colormodel = RGBcolormodel;
  98             }
  99         }
 100 
 101         setColorModel(colormodel);
 102 
 103         int flags = hintflags;
 104         if (!multipass) {
 105             flags |= ImageConsumer.SINGLEPASS;
 106         }
 107         setHints(hintflags);
 108         headerComplete();
 109 
 110         return true;
 111     }
 112 
 113     public boolean sendPixels(int pixels[], int y) {
 114         int count = setPixels(0, y, pixels.length, 1, colormodel,
 115                               pixels, 0, pixels.length);
 116         if (count <= 0) {
 117             aborted = true;
 118         }
 119         return !aborted;
 120     }
 121 
 122     public boolean sendPixels(byte pixels[], int y) {
 123         int count = setPixels(0, y, pixels.length, 1, colormodel,
 124                               pixels, 0, pixels.length);
 125         if (count <= 0) {
 126             aborted = true;
 127         }
 128         return !aborted;
 129     }
 130 
 131     /**
 132      * produce an image from the stream.
 133      */
 134     public void produceImage() throws IOException, ImageFormatException {
 135         try {
 136             readImage(input, new byte[1024]);
 137             if (!aborted) {
 138                 imageComplete(ImageConsumer.STATICIMAGEDONE, true);
 139             }
 140         } catch (IOException e) {
 141             if (!aborted) {
 142                 throw e;
 143             }
 144         } finally {
 145             close();
 146         }
 147     }
 148 
 149     /**
 150      * The ImageConsumer hints flag for a JPEG image.
 151      */
 152     private static final int hintflags =
 153         ImageConsumer.TOPDOWNLEFTRIGHT | ImageConsumer.COMPLETESCANLINES |
 154         ImageConsumer.SINGLEFRAME;
 155 }