1 /*
   2  * Copyright (c) 1995, 2019, 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         jdk.internal.access.SharedSecrets.getJavaLangAccess().loadLibrary("javajpeg");
  57         initIDs(InputStreamClass);
  58         RGBcolormodel = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
  59         ARGBcolormodel = ColorModel.getRGBdefault();
  60         byte[] g = new byte[256];
  61         for (int i = 0; i < 256; i++) {
  62             g[i] = (byte) i;
  63         }
  64         Graycolormodel = new IndexColorModel(8, 256, g, g, g);
  65     }
  66 
  67     private native void readImage(InputStream is, byte[] buf)
  68         throws ImageFormatException, IOException;
  69 
  70     Hashtable<String, Object> props = new Hashtable<>();
  71 
  72     public JPEGImageDecoder(InputStreamImageSource src, InputStream is) {
  73         super(src, is);
  74     }
  75 
  76     /**
  77      * An error has occurred. Throw an exception.
  78      */
  79     private static void error(String s1) throws ImageFormatException {
  80         throw new ImageFormatException(s1);
  81     }
  82 
  83     public boolean sendHeaderInfo(int width, int height,
  84                                   boolean gray, boolean hasalpha,
  85                                   boolean multipass)
  86     {
  87         setDimensions(width, height);
  88 
  89         setProperties(props);
  90         if (gray) {
  91             colormodel = Graycolormodel;
  92         } else {
  93             if (hasalpha) {
  94                 colormodel = ARGBcolormodel;
  95             } else {
  96                 colormodel = RGBcolormodel;
  97             }
  98         }
  99 
 100         setColorModel(colormodel);
 101 
 102         int flags = hintflags;
 103         if (!multipass) {
 104             flags |= ImageConsumer.SINGLEPASS;
 105         }
 106         setHints(hintflags);
 107         headerComplete();
 108 
 109         return true;
 110     }
 111 
 112     public boolean sendPixels(int[] pixels, int y) {
 113         int count = setPixels(0, y, pixels.length, 1, colormodel,
 114                               pixels, 0, pixels.length);
 115         if (count <= 0) {
 116             aborted = true;
 117         }
 118         return !aborted;
 119     }
 120 
 121     public boolean sendPixels(byte[] pixels, int y) {
 122         int count = setPixels(0, y, pixels.length, 1, colormodel,
 123                               pixels, 0, pixels.length);
 124         if (count <= 0) {
 125             aborted = true;
 126         }
 127         return !aborted;
 128     }
 129 
 130     /**
 131      * produce an image from the stream.
 132      */
 133     public void produceImage() throws IOException, ImageFormatException {
 134         try {
 135             readImage(input, new byte[1024]);
 136             if (!aborted) {
 137                 imageComplete(ImageConsumer.STATICIMAGEDONE, true);
 138             }
 139         } catch (IOException e) {
 140             if (!aborted) {
 141                 throw e;
 142             }
 143         } finally {
 144             close();
 145         }
 146     }
 147 
 148     /**
 149      * The ImageConsumer hints flag for a JPEG image.
 150      */
 151     private static final int hintflags =
 152         ImageConsumer.TOPDOWNLEFTRIGHT | ImageConsumer.COMPLETESCANLINES |
 153         ImageConsumer.SINGLEFRAME;
 154 }