home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 25 / CDROM25.iso / Share / prog / VJ11 / VJTRIAL.EXE / IE30Java.exe / classd.exe / sun / awt / image / JPEGImageDecoder.java < prev    next >
Encoding:
Java Source  |  1997-01-27  |  4.4 KB  |  154 lines

  1. /*
  2.  * @(#)JPEGImageDecoder.java    1.6 95/11/28 Jim Graham
  3.  *
  4.  * Copyright (c) 1994 Sun Microsystems, Inc. All Rights Reserved.
  5.  *
  6.  * Permission to use, copy, modify, and distribute this software
  7.  * and its documentation for NON-COMMERCIAL purposes and without
  8.  * fee is hereby granted provided that this copyright notice
  9.  * appears in all copies. Please refer to the file "copyright.html"
  10.  * for further important copyright and licensing information.
  11.  *
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
  13.  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
  14.  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  15.  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
  16.  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
  17.  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
  18.  */
  19.  
  20. /*-
  21.  *    Reads JPEG images from an InputStream and reports the
  22.  *    image data to an InputStreamImageSource object.
  23.  *
  24.  * The native implementation of the JPEG image decoder was adapted from
  25.  * release 6 of the free JPEG software from the Independent JPEG Group.
  26.  */
  27. package sun.awt.image;
  28.  
  29. import java.util.Vector;
  30. import java.util.Hashtable;
  31. import java.io.InputStream;
  32. import java.io.IOException;
  33. import java.awt.image.*;
  34.  
  35. /**
  36.  * JPEG Image converter
  37.  * 
  38.  * @version 1.6 11/28/95
  39.  * @author Jim Graham
  40.  */
  41. public class JPEGImageDecoder extends ImageDecoder {
  42.     private static ColorModel RGBcolormodel;
  43.     private static ColorModel Graycolormodel;
  44.  
  45.     static {
  46.     System.loadLibrary("msawt");
  47.     RGBcolormodel = new DirectColorModel(24, 0xff0000, 0xff00, 0xff);
  48.     byte g[] = new byte[256];
  49.     for (int i = 0; i < 256; i++) {
  50.         g[i] = (byte) i;
  51.     }
  52.     Graycolormodel = new IndexColorModel(8, 256, g, g, g);
  53.     }
  54.  
  55.     private native void readImage(InputStream is, byte buf[])
  56.     throws ImageFormatException;
  57.  
  58.     PixelStore store;
  59.     Hashtable props = new Hashtable();
  60.  
  61.     public JPEGImageDecoder(InputStreamImageSource src, InputStream is) {
  62.     super(src, is);
  63.     }
  64.  
  65.     public synchronized boolean catchupConsumer(InputStreamImageSource src,
  66.                         ImageConsumer ic)
  67.     {
  68.     return ((store == null) || store.replay(src, ic));
  69.     }
  70.  
  71.     public synchronized void makeStore(int width, int height, boolean gray) {
  72.     if (gray) {
  73.         store = new PixelStore8(width, height);
  74.     } else {
  75.         store = new PixelStore32(width, height);
  76.     }
  77.     }
  78.  
  79.     /**
  80.      * An error has occurred. Throw an exception.
  81.      */
  82.     private static void error(String s1) throws ImageFormatException {
  83.     throw new ImageFormatException(s1);
  84.     }
  85.  
  86.     public boolean sendHeaderInfo(int width, int height,
  87.                   boolean gray, boolean multipass) {
  88.     source.setDimensions(width, height);
  89.     makeStore(width, height, gray);
  90.  
  91.     source.setProperties(props);
  92.     store.setProperties(props);
  93.  
  94.     ColorModel colormodel = gray ? Graycolormodel : RGBcolormodel;
  95.     source.setColorModel(colormodel);
  96.     store.setColorModel(colormodel);
  97.  
  98.     int flags = hintflags;
  99.     if (!multipass) {
  100.         flags |= ImageConsumer.SINGLEPASS;
  101.     }
  102.     source.setHints(hintflags);
  103.     store.setHints(hintflags);
  104.  
  105.     return true;
  106.     }
  107.  
  108.     public boolean sendPixels(int pixels[], int y) {
  109.     int count = source.setPixels(0, y, pixels.length, 1, RGBcolormodel,
  110.                      pixels, 0, pixels.length);
  111.     if (store.setPixels(0, y, pixels.length, 1,
  112.                 pixels, 0, pixels.length)) {
  113.         count++;
  114.     }
  115.     return (count > 0);
  116.     }
  117.  
  118.     public boolean sendPixels(byte pixels[], int y) {
  119.     int count = source.setPixels(0, y, pixels.length, 1, Graycolormodel,
  120.                      pixels, 0, pixels.length);
  121.     if (store.setPixels(0, y, pixels.length, 1,
  122.                 pixels, 0, pixels.length)) {
  123.         count++;
  124.     }
  125.     return (count > 0);
  126.     }
  127.  
  128.     /**
  129.      * produce an image from the stream.
  130.      */
  131.     public void produceImage() throws IOException, ImageFormatException {
  132.     try {
  133.         readImage(input, new byte[1024]);
  134.         store.imageComplete();
  135.         if (store.getBitState() != PixelStore.BITS_LOST) {
  136.         source.setPixelStore(store);
  137.         }
  138.         source.imageComplete(ImageConsumer.STATICIMAGEDONE);
  139.     } finally {
  140.         try {
  141.         input.close();
  142.         } catch (IOException e) {
  143.         }
  144.     }
  145.     }
  146.  
  147.     /**
  148.      * The ImageConsumer hints flag for a JPEG image.
  149.      */
  150.     private static final int hintflags =
  151.     ImageConsumer.TOPDOWNLEFTRIGHT | ImageConsumer.COMPLETESCANLINES |
  152.     ImageConsumer.SINGLEFRAME;
  153. }
  154.