KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > imageio > plugins > jpeg > JPEG


1 /*
2  * @(#)JPEG.java 1.15 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.imageio.plugins.jpeg;
9
10 import javax.imageio.metadata.IIOMetadataFormatImpl JavaDoc;
11 import javax.imageio.ImageTypeSpecifier JavaDoc;
12 import javax.imageio.plugins.jpeg.JPEGQTable JavaDoc;
13 import javax.imageio.plugins.jpeg.JPEGHuffmanTable JavaDoc;
14
15 import java.awt.image.ColorModel JavaDoc;
16 import java.awt.image.BufferedImage JavaDoc;
17 import java.awt.image.DataBuffer JavaDoc;
18 import java.awt.color.ColorSpace JavaDoc;
19 import java.awt.color.ICC_ColorSpace JavaDoc;
20
21 /**
22  * A class containing JPEG-related constants, definitions, and
23  * static methods. This class and its constants must be public so that
24  * <code>JPEGImageWriteParam</code> can see it.
25  */

26 public class JPEG {
27
28     // List of all the JPEG markers (pre-JPEG2000)
29

30     /** For temporary use in arithmetic coding */
31     public static final int TEM = 0x01;
32
33     // Codes 0x02 - 0xBF are reserved
34

35     // SOF markers for Nondifferential Huffman coding
36
/** Baseline DCT */
37     public static final int SOF0 = 0xC0;
38     /** Extended Sequential DCT */
39     public static final int SOF1 = 0xC1;
40     /** Progressive DCT */
41     public static final int SOF2 = 0xC2;
42     /** Lossless Sequential */
43     public static final int SOF3 = 0xC3;
44
45     /** Define Huffman Tables */
46     public static final int DHT = 0xC4;
47
48     // SOF markers for Differential Huffman coding
49
/** Differential Sequential DCT */
50     public static final int SOF5 = 0xC5;
51     /** Differential Progressive DCT */
52     public static final int SOF6 = 0xC6;
53     /** Differential Lossless */
54     public static final int SOF7 = 0xC7;
55
56     /** Reserved for JPEG extensions */
57     public static final int JPG = 0xC8;
58
59     // SOF markers for Nondifferential arithmetic coding
60
/** Extended Sequential DCT, Arithmetic coding */
61     public static final int SOF9 = 0xC9;
62     /** Progressive DCT, Arithmetic coding */
63     public static final int SOF10 = 0xCA;
64     /** Lossless Sequential, Arithmetic coding */
65     public static final int SOF11 = 0xCB;
66
67     /** Define Arithmetic conditioning tables */
68     public static final int DAC = 0xCC;
69
70     // SOF markers for Differential arithmetic coding
71
/** Differential Sequential DCT, Arithmetic coding */
72     public static final int SOF13 = 0xCD;
73     /** Differential Progressive DCT, Arithmetic coding */
74     public static final int SOF14 = 0xCE;
75     /** Differential Lossless, Arithmetic coding */
76     public static final int SOF15 = 0xCF;
77
78     // Restart Markers
79
public static final int RST0 = 0xD0;
80     public static final int RST1 = 0xD1;
81     public static final int RST2 = 0xD2;
82     public static final int RST3 = 0xD3;
83     public static final int RST4 = 0xD4;
84     public static final int RST5 = 0xD5;
85     public static final int RST6 = 0xD6;
86     public static final int RST7 = 0xD7;
87     /** Number of restart markers */
88     public static final int RESTART_RANGE = 8;
89
90     /** Start of Image */
91     public static final int SOI = 0xD8;
92     /** End of Image */
93     public static final int EOI = 0xD9;
94     /** Start of Scan */
95     public static final int SOS = 0xDA;
96
97     /** Define Quantisation Tables */
98     public static final int DQT = 0xDB;
99
100     /** Define Number of lines */
101     public static final int DNL = 0xDC;
102
103     /** Define Restart Interval */
104     public static final int DRI = 0xDD;
105
106     /** Define Heirarchical progression */
107     public static final int DHP = 0xDE;
108
109     /** Expand reference image(s) */
110     public static final int EXP = 0xDF;
111
112     // Application markers
113
/** APP0 used by JFIF */
114     public static final int APP0 = 0xE0;
115     public static final int APP1 = 0xE1;
116     public static final int APP2 = 0xE2;
117     public static final int APP3 = 0xE3;
118     public static final int APP4 = 0xE4;
119     public static final int APP5 = 0xE5;
120     public static final int APP6 = 0xE6;
121     public static final int APP7 = 0xE7;
122     public static final int APP8 = 0xE8;
123     public static final int APP9 = 0xE9;
124     public static final int APP10 = 0xEA;
125     public static final int APP11 = 0xEB;
126     public static final int APP12 = 0xEC;
127     public static final int APP13 = 0xED;
128     /** APP14 used by Adobe */
129     public static final int APP14 = 0xEE;
130     public static final int APP15 = 0xEF;
131
132     // codes 0xF0 to 0xFD are reserved
133

134     /** Comment marker */
135     public static final int COM = 0xFE;
136
137     // JFIF Resolution units
138
/** The X and Y units simply indicate the aspect ratio of the pixels. */
139     public static final int DENSITY_UNIT_ASPECT_RATIO = 0;
140     /** Pixel density is in pixels per inch. */
141     public static final int DENSITY_UNIT_DOTS_INCH = 1;
142     /** Pixel density is in pixels per centemeter. */
143     public static final int DENSITY_UNIT_DOTS_CM = 2;
144     /** The max known value for DENSITY_UNIT */
145     public static final int NUM_DENSITY_UNIT = 3;
146
147     // Adobe transform values
148
public static final int ADOBE_IMPOSSIBLE = -1;
149     public static final int ADOBE_UNKNOWN = 0;
150     public static final int ADOBE_YCC = 1;
151     public static final int ADOBE_YCCK = 2;
152
153     // Spi initialization stuff
154
public static final String JavaDoc vendor = "Sun Microsystems, Inc.";
155     public static final String JavaDoc version = "0.5";
156     // Names of the formats we can read or write
157
public static final String JavaDoc [] names = {"JPEG", "jpeg", "JPG", "jpg"};
158     public static final String JavaDoc [] suffixes = {"jpg", "jpeg"};
159     public static final String JavaDoc [] MIMETypes = {"image/jpeg"};
160     public static final String JavaDoc nativeImageMetadataFormatName =
161         "javax_imageio_jpeg_image_1.0";
162     public static final String JavaDoc nativeImageMetadataFormatClassName =
163         "com.sun.imageio.plugins.jpeg.JPEGImageMetadataFormat";
164     public static final String JavaDoc nativeStreamMetadataFormatName =
165         "javax_imageio_jpeg_stream_1.0";
166     public static final String JavaDoc nativeStreamMetadataFormatClassName =
167         "com.sun.imageio.plugins.jpeg.JPEGStreamMetadataFormat";
168
169     // IJG Color codes.
170
public static final int JCS_UNKNOWN = 0; // error/unspecified
171
public static final int JCS_GRAYSCALE = 1; // monochrome
172
public static final int JCS_RGB = 2; // red/green/blue
173
public static final int JCS_YCbCr = 3; // Y/Cb/Cr (also known as YUV)
174
public static final int JCS_CMYK = 4; // C/M/Y/K
175
public static final int JCS_YCC = 5; // PhotoYCC
176
public static final int JCS_RGBA = 6; // RGB-Alpha
177
public static final int JCS_YCbCrA = 7; // Y/Cb/Cr/Alpha
178
// 8 and 9 were old "Legacy" codes which the old code never identified
179
// on reading anyway. Support for writing them is being dropped, too.
180
public static final int JCS_YCCA = 10; // PhotoYCC-Alpha
181
public static final int JCS_YCCK = 11; // Y/Cb/Cr/K
182

183     public static final int NUM_JCS_CODES = JCS_YCCK+1;
184
185     /** IJG can handle up to 4-channel JPEGs */
186     public static final int [] [] bandOffsets = {{0},
187                                           {0, 1},
188                                           {0, 1, 2},
189                                           {0, 1, 2, 3}};
190
191     public static final int [] bOffsRGB = { 2, 1, 0 };
192
193     protected static final ColorSpace JavaDoc sRGB =
194         ColorSpace.getInstance(ColorSpace.CS_sRGB);
195     protected static ColorSpace JavaDoc YCC = null; // Can't be final
196

197     static {
198         try {
199             YCC = ColorSpace.getInstance(ColorSpace.CS_PYCC);
200         } catch (IllegalArgumentException JavaDoc e) {
201             // PYCC.pf may not always be installed
202
}
203     }
204      
205     // Default value for ImageWriteParam
206
public static final float DEFAULT_QUALITY = 0.75F;
207
208     /**
209      * Returns <code>true</code> if the given <code>ColorSpace</code>
210      * object is an instance of ICC_ColorSpace but is not one of the
211      * standard <code>ColorSpaces</code> returned by
212      * <code>ColorSpace.getInstance()</code>.
213      */

214     static boolean isNonStandardICC(ColorSpace JavaDoc cs) {
215         boolean retval = false;
216         if ((cs instanceof ICC_ColorSpace JavaDoc)
217             && (!cs.isCS_sRGB())
218             && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_CIEXYZ)))
219             && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_GRAY)))
220             && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB)))
221             && (!cs.equals(ColorSpace.getInstance(ColorSpace.CS_PYCC)))
222             ) {
223             retval = true;
224         }
225         return retval;
226     }
227
228
229     /**
230      * Returns <code>true</code> if the given imageType can be used
231      * in a JFIF file. If <code>input</code> is true, then the
232      * image type is considered before colorspace conversion.
233      */

234     static boolean isJFIFcompliant(ImageTypeSpecifier JavaDoc imageType,
235                                    boolean input) {
236         ColorModel JavaDoc cm = imageType.getColorModel();
237         // Can't have alpha
238
if (cm.hasAlpha()) {
239             return false;
240         }
241         // Gray is OK, always
242
int numComponents = imageType.getNumComponents();
243         if (numComponents == 1) {
244             return true;
245         }
246         
247         // If it isn't gray, it must have 3 channels
248
if (numComponents != 3) {
249             return false;
250         }
251         
252         if (input) {
253             // Must be RGB
254
if (cm.getColorSpace().getType() == ColorSpace.TYPE_RGB) {
255                 return true;
256             }
257         } else {
258             // Must be YCbCr
259
if (cm.getColorSpace().getType() == ColorSpace.TYPE_YCbCr) {
260                 return true;
261             }
262         }
263
264         return false;
265     }
266
267     /**
268      * Given an image type, return the Adobe transform corresponding to
269      * that type, or ADOBE_IMPOSSIBLE if the image type is incompatible
270      * with an Adobe marker segment. If <code>input</code> is true, then
271      * the image type is considered before colorspace conversion.
272      */

273     static int transformForType(ImageTypeSpecifier JavaDoc imageType, boolean input) {
274         int retval = ADOBE_IMPOSSIBLE;
275         ColorModel JavaDoc cm = imageType.getColorModel();
276         switch (cm.getColorSpace().getType()) {
277         case ColorSpace.TYPE_GRAY:
278             retval = ADOBE_UNKNOWN;
279             break;
280         case ColorSpace.TYPE_RGB:
281             retval = input ? ADOBE_YCC : ADOBE_UNKNOWN;
282             break;
283         case ColorSpace.TYPE_YCbCr:
284             retval = ADOBE_YCC;
285             break;
286         case ColorSpace.TYPE_CMYK:
287             retval = input ? ADOBE_YCCK : ADOBE_IMPOSSIBLE;
288         }
289         return retval;
290     }
291
292     /**
293      * Converts an ImageWriteParam (i.e. IJG) non-linear quality value
294      * to a float suitable for passing to JPEGQTable.getScaledInstance().
295      */

296     static float convertToLinearQuality(float quality) {
297         // The following is converted from the IJG code.
298
if (quality <= 0.0F) {
299             quality = 0.01F;
300         }
301         
302         if (quality > 1.00F) {
303             quality = 1.00F;
304         }
305         
306         if (quality < 0.5F) {
307             quality = 0.5F / quality;
308         } else {
309             quality = 2.0F - (quality * 2.0F);
310         }
311
312         return quality;
313     }
314
315     /**
316      * Return an array of default, visually lossless quantization tables.
317      */

318     static JPEGQTable JavaDoc [] getDefaultQTables() {
319         JPEGQTable JavaDoc [] qTables = new JPEGQTable JavaDoc[2];
320         qTables[0] = JPEGQTable.K1Div2Luminance;
321         qTables[1] = JPEGQTable.K2Div2Chrominance;
322         return qTables;
323     }
324
325     /**
326      * Return an array of default Huffman tables.
327      */

328     static JPEGHuffmanTable JavaDoc [] getDefaultHuffmanTables(boolean wantDC) {
329         JPEGHuffmanTable JavaDoc [] tables = new JPEGHuffmanTable JavaDoc[2];
330         if (wantDC) {
331             tables[0] = JPEGHuffmanTable.StdDCLuminance;
332             tables[1] = JPEGHuffmanTable.StdDCChrominance;
333         } else {
334             tables[0] = JPEGHuffmanTable.StdACLuminance;
335             tables[1] = JPEGHuffmanTable.StdACChrominance;
336         }
337         return tables;
338     }
339
340 }
341
Popular Tags