Showing the changes to java/pkg2/sun/awt/image/JPEGImageDecoder.java (1 of 18352 files). Show all
Unified · Split
| @@ -0,0 +1,160 @@ | |||
| 1 | /* | ||
| 2 | * @(#)JPEGImageDecoder.java 1.7 97/02/23 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.7 02/23/97 | ||
| 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("jpeg"); | ||
| 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, IOException; | ||
| 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 | setDimensions(width, height); | ||
| 89 | makeStore(width, height, gray); | ||
| 90 | |||
| 91 | setProperties(props); | ||
| 92 | store.setProperties(props); | ||
| 93 | |||
| 94 | ColorModel colormodel = gray ? Graycolormodel : RGBcolormodel; | ||
| 95 | setColorModel(colormodel); | ||
| 96 | store.setColorModel(colormodel); | ||
| 97 | |||
| 98 | int flags = hintflags; | ||
| 99 | if (!multipass) { | ||
| 100 | flags |= ImageConsumer.SINGLEPASS; | ||
| 101 | } | ||
| 102 | setHints(hintflags); | ||
| 103 | store.setHints(hintflags); | ||
| 104 | |||
| 105 | headerComplete(); | ||
| 106 | |||
| 107 | return true; | ||
| 108 | } | ||
| 109 | |||
| 110 | public boolean sendPixels(int pixels[], int y) { | ||
| 111 | int count = setPixels(0, y, pixels.length, 1, RGBcolormodel, | ||
| 112 | pixels, 0, pixels.length); | ||
| 113 | if (count > 0) { | ||
| 114 | store.setPixels(0, y, pixels.length, 1, pixels, 0, pixels.length); | ||
| 115 | } else { | ||
| 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, Graycolormodel, | ||
| 123 | pixels, 0, pixels.length); | ||
| 124 | if (count > 0) { | ||
| 125 | store.setPixels(0, y, pixels.length, 1, pixels, 0, pixels.length); | ||
| 126 | } else { | ||
| 127 | aborted = true; | ||
| 128 | } | ||
| 129 | return !aborted; | ||
| 130 | } | ||
| 131 | |||
| 132 | /** | ||
| 133 | * produce an image from the stream. | ||
| 134 | */ | ||
| 135 | public void produceImage() throws IOException, ImageFormatException { | ||
| 136 | try { | ||
| 137 | readImage(input, new byte[1024]); | ||
| 138 | if (!aborted) { | ||
| 139 | store.imageComplete(); | ||
| 140 | if (store.getBitState() != PixelStore.BITS_LOST) { | ||
| 141 | source.setPixelStore(this, store); | ||
| 142 | } | ||
| 143 | imageComplete(ImageConsumer.STATICIMAGEDONE, true); | ||
| 144 | } | ||
| 145 | } catch (IOException e) { | ||
| 146 | if (!aborted) { | ||
| 147 | throw e; | ||
| 148 | } | ||
| 149 | } finally { | ||
| 150 | close(); | ||
| 151 | } | ||
| 152 | } | ||
| 153 | |||
| 154 | /** | ||
| 155 | * The ImageConsumer hints flag for a JPEG image. | ||
| 156 | */ | ||
| 157 | private static final int hintflags = | ||
| 158 | ImageConsumer.TOPDOWNLEFTRIGHT | ImageConsumer.COMPLETESCANLINES | | ||
| 159 | ImageConsumer.SINGLEFRAME; | ||
| 160 | } | ||