KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > image > codec > jpeg > JPEGCodec


1 /*
2  * @(#)JPEGCodec.java 1.8 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 /**********************************************************************
9  **********************************************************************
10  **********************************************************************
11  *** COPYRIGHT (c) 1997-1998 Eastman Kodak Company. ***
12  *** As an unpublished work pursuant to Title 17 of the United ***
13  *** States Code. All rights reserved. ***
14  **********************************************************************
15  **********************************************************************
16  **********************************************************************/

17
18 package com.sun.image.codec.jpeg;
19
20 import sun.awt.image.codec.JPEGImageDecoderImpl;
21 import sun.awt.image.codec.JPEGImageEncoderImpl;
22 import sun.awt.image.codec.JPEGParam;
23 import java.awt.image.BufferedImage JavaDoc;
24 import java.awt.image.Raster JavaDoc;
25 import java.awt.image.ColorModel JavaDoc;
26 import java.io.InputStream JavaDoc;
27 import java.io.OutputStream JavaDoc;
28
29 /**
30  * This class is a factory for implementations of the JPEG Image
31  * Decoder/Encoder.
32  * <p>
33  * Note that the classes in the com.sun.image.codec.jpeg package are not
34  * part of the core Java APIs. They are a part of Sun's JDK and JRE
35  * distributions. Although other licensees may choose to distribute these
36  * classes, developers cannot depend on their availability in non-Sun
37  * implementations. We expect that equivalent functionality will eventually
38  * be available in a core API or standard extension.
39  * <p>
40  *
41  * @see JPEGImageDecoder
42  * @see JPEGImageEncoder
43  */

44 public class JPEGCodec {
45     private JPEGCodec() { }
46
47     /**
48      * This creates an instance of a JPEGImageDecoder that can be used
49      * to decode JPEG Data streams.
50      */

51     public static JPEGImageDecoder createJPEGDecoder(InputStream JavaDoc src) {
52         return new JPEGImageDecoderImpl(src);
53     }
54
55     /**
56      * This creates an instance of a JPEGImageDecoder that can be used
57      * to decode JPEG Data streams.
58      */

59     public static JPEGImageDecoder createJPEGDecoder(InputStream JavaDoc src,
60                                                      JPEGDecodeParam jdp) {
61         return new JPEGImageDecoderImpl(src, jdp);
62     }
63
64     /**
65      * This creates an instance of a JPEGImageEncoder that can be used
66      * to encode image data as JPEG Data streams.
67      */

68     public static JPEGImageEncoder createJPEGEncoder(OutputStream JavaDoc dest) {
69         return new JPEGImageEncoderImpl(dest);
70     }
71     /**
72      * This creates an instance of a JPEGImageEncoder that can be used
73      * to encode image data as JPEG Data streams.
74      */

75     public static JPEGImageEncoder createJPEGEncoder(OutputStream JavaDoc dest,
76                                                      JPEGEncodeParam jep) {
77         return new JPEGImageEncoderImpl(dest, jep);
78     }
79
80     /**
81       * This is a factory method for creating JPEGEncodeParam objects.
82       * The returned object should do a credible job of encoding the
83       * given BufferedImage.
84       * @param bi A BufferedImage that is similar to the BufferedImage(s)
85       * that will encoded using the returned JPEGEncodeParam object.
86       */

87     public static JPEGEncodeParam getDefaultJPEGEncodeParam(BufferedImage JavaDoc bi)
88     {
89     int colorID = JPEGParam.getDefaultColorId(bi.getColorModel());
90     return getDefaultJPEGEncodeParam(bi.getRaster(), colorID);
91     }
92
93     /**
94       * This is a factory method for creating JPEGEncodeParam objects.
95       * It is the users responsiblity to match the colorID with the
96       * data contained in the Raster. Failure to do so may lead to
97       * either poor compression or poor image quality. If you don't
98       * understand much about JPEG it is strongly recommended that you
99       * stick to the BufferedImage interface.
100       * @param ras Raster that is similar to those to be encoded later.
101       * @param colorID the COLOR_ID for the encoded data. This should
102       * match the data in the raster.
103       */

104     public static JPEGEncodeParam getDefaultJPEGEncodeParam(Raster JavaDoc ras,
105                                                             int colorID)
106     {
107     JPEGParam ret = new JPEGParam(colorID, ras.getNumBands());
108     ret.setWidth(ras.getWidth());
109     ret.setHeight(ras.getHeight());
110     
111     return ret;
112     }
113
114     /**
115       * This is a factory method for creating JPEGEncodeParam objects. It
116       * is the users responsiblity to match the colorID with the given
117       * number of bands, which should match the data being encoded.
118       * Failure to do so may lead to poor compression and/or poor image
119       * quality. If you don't understand much about JPEG it is strongly
120       * recommended that you stick to the BufferedImage interface.
121       *
122       * This can also be used as a factory for a JPEGDecodeParam object.
123       * However this usage is extremely rare, as one needs to be decoding
124       * abbreviated JPEG streams where the JPEG tables are coming from
125       * some source other than a JPEG tables only stream.
126       *
127       * @param numBands the number of bands that will be encoded (max of four).
128       * @param colorID the COLOR_ID for the encoded data. This is used to
129       * set reasonable defaults in the parameter object. This must match
130       * the number of bands given.
131       */

132     public static JPEGEncodeParam getDefaultJPEGEncodeParam(int numBands,
133                                                                 int colorID)
134             throws ImageFormatException
135     {
136     return new JPEGParam(colorID, numBands);
137     }
138
139     /**
140      * This is a factory method for creating a JPEGEncodeParam from a
141      * JPEGDecodeParam. This will return a new JPEGEncodeParam object
142      * that is initialized from the JPEGDecodeParam object. All major
143      * pieces of information will be initialized from the DecodeParam
144      * (Markers, Tables, mappings).
145      * @param jdp The JPEGDecodeParam object to copy.
146      */

147
148     public static JPEGEncodeParam getDefaultJPEGEncodeParam(JPEGDecodeParam jdp)
149         throws ImageFormatException {
150         return new JPEGParam(jdp);
151     }
152 }
153
Popular Tags